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