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