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