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