Main Page | Namespace List | Class List | Directories | File List | Class Members | File Members

auth-rhosts.c File Reference

#include "includes.h"
#include "packet.h"
#include "uidswap.h"
#include "pathnames.h"
#include "log.h"
#include "servconf.h"
#include "canohost.h"
#include "auth.h"

Go to the source code of this file.

Functions

 RCSID ("$OpenBSD: auth-rhosts.c,v 1.33 2005/07/17 07:17:54 djm Exp $")
static int check_rhosts_file (const char *filename, const char *hostname, const char *ipaddr, const char *client_user, const char *server_user)
int auth_rhosts (struct passwd *pw, const char *client_user)
static int auth_rhosts2_raw (struct passwd *pw, const char *client_user, const char *hostname, const char *ipaddr)
int auth_rhosts2 (struct passwd *pw, const char *client_user, const char *hostname, const char *ipaddr)

Variables

ServerOptions options
int use_privsep


Function Documentation

int auth_rhosts struct passwd *  pw,
const char client_user
 

Definition at line 155 of file auth-rhosts.c.

References auth_rhosts2(), get_canonical_hostname(), get_remote_ipaddr(), hostname, and ServerOptions::use_dns.

Referenced by auth_rhosts_rsa_key_allowed().

00156 {
00157         const char *hostname, *ipaddr;
00158 
00159         hostname = get_canonical_hostname(options.use_dns);
00160         ipaddr = get_remote_ipaddr();
00161         return auth_rhosts2(pw, client_user, hostname, ipaddr);
00162 }

int auth_rhosts2 struct passwd *  pw,
const char client_user,
const char hostname,
const char ipaddr
 

Definition at line 285 of file auth-rhosts.c.

References auth_debug_reset(), auth_debug_send(), auth_rhosts2_raw(), and use_privsep.

Referenced by auth_rhosts(), and hostbased_key_allowed().

00287 {
00288         int ret;
00289 
00290         auth_debug_reset();
00291         ret = auth_rhosts2_raw(pw, client_user, hostname, ipaddr);
00292         if (!use_privsep)
00293                 auth_debug_send();
00294         return ret;
00295 }

static int auth_rhosts2_raw struct passwd *  pw,
const char client_user,
const char hostname,
const char ipaddr
[static]
 

Definition at line 165 of file auth-rhosts.c.

References _PATH_RHOSTS_EQUIV, _PATH_SSH_HOSTS_EQUIV, auth_debug_add(), check_rhosts_file(), debug2(), ServerOptions::ignore_rhosts, logit(), restore_uid(), snprintf(), ServerOptions::strict_modes, and temporarily_use_uid().

Referenced by auth_rhosts2().

00167 {
00168         char buf[1024];
00169         struct stat st;
00170         static const char *rhosts_files[] = {".shosts", ".rhosts", NULL};
00171         u_int rhosts_file_index;
00172 
00173         debug2("auth_rhosts2: clientuser %s hostname %s ipaddr %s",
00174             client_user, hostname, ipaddr);
00175 
00176         /* Switch to the user's uid. */
00177         temporarily_use_uid(pw);
00178         /*
00179          * Quick check: if the user has no .shosts or .rhosts files, return
00180          * failure immediately without doing costly lookups from name
00181          * servers.
00182          */
00183         for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
00184             rhosts_file_index++) {
00185                 /* Check users .rhosts or .shosts. */
00186                 snprintf(buf, sizeof buf, "%.500s/%.100s",
00187                          pw->pw_dir, rhosts_files[rhosts_file_index]);
00188                 if (stat(buf, &st) >= 0)
00189                         break;
00190         }
00191         /* Switch back to privileged uid. */
00192         restore_uid();
00193 
00194         /* Deny if The user has no .shosts or .rhosts file and there are no system-wide files. */
00195         if (!rhosts_files[rhosts_file_index] &&
00196             stat(_PATH_RHOSTS_EQUIV, &st) < 0 &&
00197             stat(_PATH_SSH_HOSTS_EQUIV, &st) < 0)
00198                 return 0;
00199 
00200         /* If not logging in as superuser, try /etc/hosts.equiv and shosts.equiv. */
00201         if (pw->pw_uid != 0) {
00202                 if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr,
00203                     client_user, pw->pw_name)) {
00204                         auth_debug_add("Accepted for %.100s [%.100s] by /etc/hosts.equiv.",
00205                             hostname, ipaddr);
00206                         return 1;
00207                 }
00208                 if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr,
00209                     client_user, pw->pw_name)) {
00210                         auth_debug_add("Accepted for %.100s [%.100s] by %.100s.",
00211                             hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV);
00212                         return 1;
00213                 }
00214         }
00215         /*
00216          * Check that the home directory is owned by root or the user, and is
00217          * not group or world writable.
00218          */
00219         if (stat(pw->pw_dir, &st) < 0) {
00220                 logit("Rhosts authentication refused for %.100s: "
00221                     "no home directory %.200s", pw->pw_name, pw->pw_dir);
00222                 auth_debug_add("Rhosts authentication refused for %.100s: "
00223                     "no home directory %.200s", pw->pw_name, pw->pw_dir);
00224                 return 0;
00225         }
00226         if (options.strict_modes &&
00227             ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
00228             (st.st_mode & 022) != 0)) {
00229                 logit("Rhosts authentication refused for %.100s: "
00230                     "bad ownership or modes for home directory.", pw->pw_name);
00231                 auth_debug_add("Rhosts authentication refused for %.100s: "
00232                     "bad ownership or modes for home directory.", pw->pw_name);
00233                 return 0;
00234         }
00235         /* Temporarily use the user's uid. */
00236         temporarily_use_uid(pw);
00237 
00238         /* Check all .rhosts files (currently .shosts and .rhosts). */
00239         for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
00240             rhosts_file_index++) {
00241                 /* Check users .rhosts or .shosts. */
00242                 snprintf(buf, sizeof buf, "%.500s/%.100s",
00243                          pw->pw_dir, rhosts_files[rhosts_file_index]);
00244                 if (stat(buf, &st) < 0)
00245                         continue;
00246 
00247                 /*
00248                  * Make sure that the file is either owned by the user or by
00249                  * root, and make sure it is not writable by anyone but the
00250                  * owner.  This is to help avoid novices accidentally
00251                  * allowing access to their account by anyone.
00252                  */
00253                 if (options.strict_modes &&
00254                     ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
00255                     (st.st_mode & 022) != 0)) {
00256                         logit("Rhosts authentication refused for %.100s: bad modes for %.200s",
00257                             pw->pw_name, buf);
00258                         auth_debug_add("Bad file modes for %.200s", buf);
00259                         continue;
00260                 }
00261                 /* Check if we have been configured to ignore .rhosts and .shosts files. */
00262                 if (options.ignore_rhosts) {
00263                         auth_debug_add("Server has been configured to ignore %.100s.",
00264                             rhosts_files[rhosts_file_index]);
00265                         continue;
00266                 }
00267                 /* Check if authentication is permitted by the file. */
00268                 if (check_rhosts_file(buf, hostname, ipaddr, client_user, pw->pw_name)) {
00269                         auth_debug_add("Accepted by %.100s.",
00270                             rhosts_files[rhosts_file_index]);
00271                         /* Restore the privileged uid. */
00272                         restore_uid();
00273                         auth_debug_add("Accepted host %s ip %s client_user %s server_user %s",
00274                                 hostname, ipaddr, client_user, pw->pw_name);
00275                         return 1;
00276                 }
00277         }
00278 
00279         /* Restore the privileged uid. */
00280         restore_uid();
00281         return 0;
00282 }

static int check_rhosts_file const char filename,
const char hostname,
const char ipaddr,
const char client_user,
const char server_user
[static]
 

Definition at line 38 of file auth-rhosts.c.

References auth_debug_add(), host, innetgr(), and strlcpy().

Referenced by auth_rhosts2_raw().

00041 {
00042         FILE *f;
00043         char buf[1024]; /* Must not be larger than host, user, dummy below. */
00044 
00045         /* Open the .rhosts file, deny if unreadable */
00046         f = fopen(filename, "r");
00047         if (!f)
00048                 return 0;
00049 
00050         while (fgets(buf, sizeof(buf), f)) {
00051                 /* All three must be at least as big as buf to avoid overflows. */
00052                 char hostbuf[1024], userbuf[1024], dummy[1024], *host, *user, *cp;
00053                 int negated;
00054 
00055                 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
00056                         ;
00057                 if (*cp == '#' || *cp == '\n' || !*cp)
00058                         continue;
00059 
00060                 /*
00061                  * NO_PLUS is supported at least on OSF/1.  We skip it (we
00062                  * don't ever support the plus syntax).
00063                  */
00064                 if (strncmp(cp, "NO_PLUS", 7) == 0)
00065                         continue;
00066 
00067                 /*
00068                  * This should be safe because each buffer is as big as the
00069                  * whole string, and thus cannot be overwritten.
00070                  */
00071                 switch (sscanf(buf, "%1023s %1023s %1023s", hostbuf, userbuf,
00072                     dummy)) {
00073                 case 0:
00074                         auth_debug_add("Found empty line in %.100s.", filename);
00075                         continue;
00076                 case 1:
00077                         /* Host name only. */
00078                         strlcpy(userbuf, server_user, sizeof(userbuf));
00079                         break;
00080                 case 2:
00081                         /* Got both host and user name. */
00082                         break;
00083                 case 3:
00084                         auth_debug_add("Found garbage in %.100s.", filename);
00085                         continue;
00086                 default:
00087                         /* Weird... */
00088                         continue;
00089                 }
00090 
00091                 host = hostbuf;
00092                 user = userbuf;
00093                 negated = 0;
00094 
00095                 /* Process negated host names, or positive netgroups. */
00096                 if (host[0] == '-') {
00097                         negated = 1;
00098                         host++;
00099                 } else if (host[0] == '+')
00100                         host++;
00101 
00102                 if (user[0] == '-') {
00103                         negated = 1;
00104                         user++;
00105                 } else if (user[0] == '+')
00106                         user++;
00107 
00108                 /* Check for empty host/user names (particularly '+'). */
00109                 if (!host[0] || !user[0]) {
00110                         /* We come here if either was '+' or '-'. */
00111                         auth_debug_add("Ignoring wild host/user names in %.100s.",
00112                             filename);
00113                         continue;
00114                 }
00115                 /* Verify that host name matches. */
00116                 if (host[0] == '@') {
00117                         if (!innetgr(host + 1, hostname, NULL, NULL) &&
00118                             !innetgr(host + 1, ipaddr, NULL, NULL))
00119                                 continue;
00120                 } else if (strcasecmp(host, hostname) && strcmp(host, ipaddr) != 0)
00121                         continue;       /* Different hostname. */
00122 
00123                 /* Verify that user name matches. */
00124                 if (user[0] == '@') {
00125                         if (!innetgr(user + 1, NULL, client_user, NULL))
00126                                 continue;
00127                 } else if (strcmp(user, client_user) != 0)
00128                         continue;       /* Different username. */
00129 
00130                 /* Found the user and host. */
00131                 fclose(f);
00132 
00133                 /* If the entry was negated, deny access. */
00134                 if (negated) {
00135                         auth_debug_add("Matched negative entry in %.100s.",
00136                             filename);
00137                         return 0;
00138                 }
00139                 /* Accept authentication. */
00140                 return 1;
00141         }
00142 
00143         /* Authentication using this file denied. */
00144         fclose(f);
00145         return 0;
00146 }

RCSID "$OpenBSD: auth-rhosts.  c,
v 1.33 2005/07/17 07:17:54 djm Exp $" 
 


Variable Documentation

ServerOptions options
 

Definition at line 110 of file ssh.c.

int use_privsep
 

Definition at line 204 of file sshd.c.

Referenced by auth_parse_options(), auth_rhosts2(), cleanup_exit(), do_authentication(), do_cleanup(), do_exec_no_pty(), do_exec_pty(), do_login(), do_ssh1_kex(), fill_default_server_options(), grace_alarm_handler(), initialize_server_options(), input_userauth_request(), main(), privsep_postauth(), process_server_config_line(), server_loop(), server_loop2(), and session_pty_req().


© sourcejam.com 2005-2008