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