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