Home | History | Annotate | Line # | Download | only in dist
auth-rhosts.c revision 1.1.1.15
      1 /* $OpenBSD: auth-rhosts.c,v 1.58 2024/05/17 00:30:23 djm Exp $ */
      2 /*
      3  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      4  * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
      5  *                    All rights reserved
      6  * Rhosts authentication.  This file contains code to check whether to admit
      7  * the login based on rhosts authentication.  This file also processes
      8  * /etc/hosts.equiv.
      9  *
     10  * As far as I am concerned, the code I have written for this software
     11  * can be used freely for any purpose.  Any derived versions of this
     12  * software must be clearly marked as such, and if the derived work is
     13  * incompatible with the protocol description in the RFC file, it must be
     14  * called by a name other than "ssh" or "Secure Shell".
     15  */
     16 
     17 #include <sys/types.h>
     18 #include <sys/stat.h>
     19 
     20 #include <errno.h>
     21 #include <fcntl.h>
     22 #include <netgroup.h>
     23 #include <pwd.h>
     24 #include <stdio.h>
     25 #include <string.h>
     26 #include <stdarg.h>
     27 #include <stdlib.h>
     28 #include <unistd.h>
     29 
     30 #include "packet.h"
     31 #include "uidswap.h"
     32 #include "pathnames.h"
     33 #include "log.h"
     34 #include "misc.h"
     35 #include "xmalloc.h"
     36 #include "sshbuf.h"
     37 #include "sshkey.h"
     38 #include "servconf.h"
     39 #include "canohost.h"
     40 #include "hostfile.h"
     41 #include "auth.h"
     42 
     43 /* import */
     44 extern ServerOptions options;
     45 
     46 /*
     47  * This function processes an rhosts-style file (.rhosts, .shosts, or
     48  * /etc/hosts.equiv).  This returns true if authentication can be granted
     49  * based on the file, and returns zero otherwise.
     50  */
     51 
     52 static int
     53 check_rhosts_file(const char *filename, const char *hostname,
     54 		  const char *ipaddr, const char *client_user,
     55 		  const char *server_user)
     56 {
     57 	FILE *f;
     58 #define RBUFLN 1024
     59 	char buf[RBUFLN];/* Must not be larger than host, user, dummy below. */
     60 	int fd;
     61 	struct stat st;
     62 
     63 	/* Open the .rhosts file, deny if unreadable */
     64 	if ((fd = open(filename, O_RDONLY|O_NONBLOCK)) == -1)
     65 		return 0;
     66 	if (fstat(fd, &st) == -1) {
     67 		close(fd);
     68 		return 0;
     69 	}
     70 	if (!S_ISREG(st.st_mode)) {
     71 		logit("User %s hosts file %s is not a regular file",
     72 		    server_user, filename);
     73 		close(fd);
     74 		return 0;
     75 	}
     76 	unset_nonblock(fd);
     77 	if ((f = fdopen(fd, "r")) == NULL) {
     78 		close(fd);
     79 		return 0;
     80 	}
     81 	while (fgets(buf, sizeof(buf), f)) {
     82 		/* All three must have length >= buf to avoid overflows. */
     83 		char hostbuf[RBUFLN], userbuf[RBUFLN], dummy[RBUFLN];
     84 		char *host, *user, *cp;
     85 		int negated;
     86 
     87 		for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
     88 			;
     89 		if (*cp == '#' || *cp == '\n' || !*cp)
     90 			continue;
     91 
     92 		/*
     93 		 * NO_PLUS is supported at least on OSF/1.  We skip it (we
     94 		 * don't ever support the plus syntax).
     95 		 */
     96 		if (strncmp(cp, "NO_PLUS", 7) == 0)
     97 			continue;
     98 
     99 		/*
    100 		 * This should be safe because each buffer is as big as the
    101 		 * whole string, and thus cannot be overwritten.
    102 		 */
    103 		switch (sscanf(buf, "%1023s %1023s %1023s", hostbuf, userbuf,
    104 		    dummy)) {
    105 		case 0:
    106 			auth_debug_add("Found empty line in %.100s.", filename);
    107 			continue;
    108 		case 1:
    109 			/* Host name only. */
    110 			strlcpy(userbuf, server_user, sizeof(userbuf));
    111 			break;
    112 		case 2:
    113 			/* Got both host and user name. */
    114 			break;
    115 		case 3:
    116 			auth_debug_add("Found garbage in %.100s.", filename);
    117 			continue;
    118 		default:
    119 			/* Weird... */
    120 			continue;
    121 		}
    122 
    123 		host = hostbuf;
    124 		user = userbuf;
    125 		negated = 0;
    126 
    127 		/* Process negated host names, or positive netgroups. */
    128 		if (host[0] == '-') {
    129 			negated = 1;
    130 			host++;
    131 		} else if (host[0] == '+')
    132 			host++;
    133 
    134 		if (user[0] == '-') {
    135 			negated = 1;
    136 			user++;
    137 		} else if (user[0] == '+')
    138 			user++;
    139 
    140 		/* Check for empty host/user names (particularly '+'). */
    141 		if (!host[0] || !user[0]) {
    142 			/* We come here if either was '+' or '-'. */
    143 			auth_debug_add("Ignoring wild host/user names "
    144 			    "in %.100s.", filename);
    145 			continue;
    146 		}
    147 		/* Verify that host name matches. */
    148 		if (host[0] == '@') {
    149 			if (!innetgr(host + 1, hostname, NULL, NULL) &&
    150 			    !innetgr(host + 1, ipaddr, NULL, NULL))
    151 				continue;
    152 		} else if (strcasecmp(host, hostname) &&
    153 		    strcmp(host, ipaddr) != 0)
    154 			continue;	/* Different hostname. */
    155 
    156 		/* Verify that user name matches. */
    157 		if (user[0] == '@') {
    158 			if (!innetgr(user + 1, NULL, client_user, NULL))
    159 				continue;
    160 		} else if (strcmp(user, client_user) != 0)
    161 			continue;	/* Different username. */
    162 
    163 		/* Found the user and host. */
    164 		fclose(f);
    165 
    166 		/* If the entry was negated, deny access. */
    167 		if (negated) {
    168 			auth_debug_add("Matched negative entry in %.100s.",
    169 			    filename);
    170 			return 0;
    171 		}
    172 		/* Accept authentication. */
    173 		return 1;
    174 	}
    175 
    176 	/* Authentication using this file denied. */
    177 	fclose(f);
    178 	return 0;
    179 }
    180 
    181 /*
    182  * Tries to authenticate the user using the .shosts or .rhosts file. Returns
    183  * true if authentication succeeds.  If ignore_rhosts is true, only
    184  * /etc/hosts.equiv will be considered (.rhosts and .shosts are ignored).
    185  */
    186 int
    187 auth_rhosts2(struct passwd *pw, const char *client_user, const char *hostname,
    188     const char *ipaddr)
    189 {
    190 	char *path = NULL;
    191 	struct stat st;
    192 	static const char * const rhosts_files[] = {".shosts", ".rhosts", NULL};
    193 	u_int rhosts_file_index;
    194 	int r;
    195 
    196 	debug2_f("clientuser %s hostname %s ipaddr %s",
    197 	    client_user, hostname, ipaddr);
    198 
    199 	/* Switch to the user's uid. */
    200 	temporarily_use_uid(pw);
    201 	/*
    202 	 * Quick check: if the user has no .shosts or .rhosts files and
    203 	 * no system hosts.equiv/shosts.equiv files exist then return
    204 	 * failure immediately without doing costly lookups from name
    205 	 * servers.
    206 	 */
    207 	for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
    208 	    rhosts_file_index++) {
    209 		/* Check users .rhosts or .shosts. */
    210 		xasprintf(&path, "%s/%s",
    211 		    pw->pw_dir, rhosts_files[rhosts_file_index]);
    212 		r = stat(path, &st);
    213 		free(path);
    214 		if (r >= 0)
    215 			break;
    216 	}
    217 	/* Switch back to privileged uid. */
    218 	restore_uid();
    219 
    220 	/*
    221 	 * Deny if The user has no .shosts or .rhosts file and there
    222 	 * are no system-wide files.
    223 	 */
    224 	if (!rhosts_files[rhosts_file_index] &&
    225 	    stat(_PATH_RHOSTS_EQUIV, &st) == -1 &&
    226 	    stat(_PATH_SSH_HOSTS_EQUIV, &st) == -1) {
    227 		debug3_f("no hosts access files exist");
    228 		return 0;
    229 	}
    230 
    231 	/*
    232 	 * If not logging in as superuser, try /etc/hosts.equiv and
    233 	 * shosts.equiv.
    234 	 */
    235 	if (pw->pw_uid == 0)
    236 		debug3_f("root user, ignoring system hosts files");
    237 	else {
    238 		if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr,
    239 		    client_user, pw->pw_name)) {
    240 			auth_debug_add("Accepted for %.100s [%.100s] by "
    241 			    "/etc/hosts.equiv.", hostname, ipaddr);
    242 			return 1;
    243 		}
    244 		if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr,
    245 		    client_user, pw->pw_name)) {
    246 			auth_debug_add("Accepted for %.100s [%.100s] by "
    247 			    "%.100s.", hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV);
    248 			return 1;
    249 		}
    250 	}
    251 
    252 	/*
    253 	 * Check that the home directory is owned by root or the user, and is
    254 	 * not group or world writable.
    255 	 */
    256 	if (stat(pw->pw_dir, &st) == -1) {
    257 		logit("Rhosts authentication refused for %.100s: "
    258 		    "no home directory %.200s", pw->pw_name, pw->pw_dir);
    259 		auth_debug_add("Rhosts authentication refused for %.100s: "
    260 		    "no home directory %.200s", pw->pw_name, pw->pw_dir);
    261 		return 0;
    262 	}
    263 	if (options.strict_modes &&
    264 	    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
    265 	    (st.st_mode & 022) != 0)) {
    266 		logit("Rhosts authentication refused for %.100s: "
    267 		    "bad ownership or modes for home directory.", pw->pw_name);
    268 		auth_debug_add("Rhosts authentication refused for %.100s: "
    269 		    "bad ownership or modes for home directory.", pw->pw_name);
    270 		return 0;
    271 	}
    272 	/* Temporarily use the user's uid. */
    273 	temporarily_use_uid(pw);
    274 
    275 	/* Check all .rhosts files (currently .shosts and .rhosts). */
    276 	for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
    277 	    rhosts_file_index++) {
    278 		/* Check users .rhosts or .shosts. */
    279 		xasprintf(&path, "%s/%s",
    280 		    pw->pw_dir, rhosts_files[rhosts_file_index]);
    281 		if (stat(path, &st) == -1) {
    282 			debug3_f("stat %s: %s", path, strerror(errno));
    283 			free(path);
    284 			continue;
    285 		}
    286 
    287 		/*
    288 		 * Make sure that the file is either owned by the user or by
    289 		 * root, and make sure it is not writable by anyone but the
    290 		 * owner.  This is to help avoid novices accidentally
    291 		 * allowing access to their account by anyone.
    292 		 */
    293 		if (options.strict_modes &&
    294 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
    295 		    (st.st_mode & 022) != 0)) {
    296 			logit("Rhosts authentication refused for %.100s: "
    297 			    "bad modes for %.200s", pw->pw_name, path);
    298 			auth_debug_add("Bad file modes for %.200s", path);
    299 			free(path);
    300 			continue;
    301 		}
    302 		/*
    303 		 * Check if we have been configured to ignore .rhosts
    304 		 * and .shosts files.
    305 		 */
    306 		if (options.ignore_rhosts == IGNORE_RHOSTS_YES ||
    307 		    (options.ignore_rhosts == IGNORE_RHOSTS_SHOSTS &&
    308 		    strcmp(rhosts_files[rhosts_file_index], ".shosts") != 0)) {
    309 			auth_debug_add("Server has been configured to "
    310 			    "ignore %.100s.", rhosts_files[rhosts_file_index]);
    311 			free(path);
    312 			continue;
    313 		}
    314 		/* Check if authentication is permitted by the file. */
    315 		if (check_rhosts_file(path, hostname, ipaddr,
    316 		    client_user, pw->pw_name)) {
    317 			auth_debug_add("Accepted by %.100s.",
    318 			    rhosts_files[rhosts_file_index]);
    319 			/* Restore the privileged uid. */
    320 			restore_uid();
    321 			auth_debug_add("Accepted host %s ip %s client_user "
    322 			    "%s server_user %s", hostname, ipaddr,
    323 			    client_user, pw->pw_name);
    324 			free(path);
    325 			return 1;
    326 		}
    327 		free(path);
    328 	}
    329 
    330 	/* Restore the privileged uid. */
    331 	restore_uid();
    332 	return 0;
    333 }
    334