Annotation of src/usr.bin/ssh/auth-rhosts.c, Revision 1.22
1.1 deraadt 1: /*
1.10 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Rhosts authentication. This file contains code to check whether to admit
6: * the login based on rhosts authentication. This file also processes
7: * /etc/hosts.equiv.
1.13 markus 8: *
1.15 deraadt 9: * As far as I am concerned, the code I have written for this software
10: * can be used freely for any purpose. Any derived versions of this
11: * software must be clearly marked as such, and if the derived work is
12: * incompatible with the protocol description in the RFC file, it must be
13: * called by a name other than "ssh" or "Secure Shell".
1.10 deraadt 14: */
1.1 deraadt 15:
16: #include "includes.h"
1.22 ! markus 17: RCSID("$OpenBSD: auth-rhosts.c,v 1.21 2001/02/08 19:30:51 itojun Exp $");
1.1 deraadt 18:
19: #include "packet.h"
20: #include "xmalloc.h"
21: #include "uidswap.h"
1.19 markus 22: #include "pathnames.h"
23: #include "log.h"
1.6 markus 24: #include "servconf.h"
1.19 markus 25: #include "canohost.h"
1.21 itojun 26: #include "auth.h"
1.1 deraadt 27:
1.11 markus 28: /*
29: * This function processes an rhosts-style file (.rhosts, .shosts, or
30: * /etc/hosts.equiv). This returns true if authentication can be granted
31: * based on the file, and returns zero otherwise.
32: */
1.1 deraadt 33:
1.13 markus 34: int
1.9 markus 35: check_rhosts_file(const char *filename, const char *hostname,
36: const char *ipaddr, const char *client_user,
37: const char *server_user)
1.1 deraadt 38: {
1.9 markus 39: FILE *f;
40: char buf[1024]; /* Must not be larger than host, user, dummy below. */
1.1 deraadt 41:
1.9 markus 42: /* Open the .rhosts file, deny if unreadable */
43: f = fopen(filename, "r");
44: if (!f)
45: return 0;
46:
47: while (fgets(buf, sizeof(buf), f)) {
48: /* All three must be at least as big as buf to avoid overflows. */
49: char hostbuf[1024], userbuf[1024], dummy[1024], *host, *user, *cp;
50: int negated;
51:
52: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
53: ;
54: if (*cp == '#' || *cp == '\n' || !*cp)
55: continue;
56:
1.11 markus 57: /*
58: * NO_PLUS is supported at least on OSF/1. We skip it (we
59: * don't ever support the plus syntax).
60: */
1.9 markus 61: if (strncmp(cp, "NO_PLUS", 7) == 0)
62: continue;
63:
1.11 markus 64: /*
65: * This should be safe because each buffer is as big as the
66: * whole string, and thus cannot be overwritten.
67: */
1.9 markus 68: switch (sscanf(buf, "%s %s %s", hostbuf, userbuf, dummy)) {
69: case 0:
70: packet_send_debug("Found empty line in %.100s.", filename);
71: continue;
72: case 1:
73: /* Host name only. */
74: strlcpy(userbuf, server_user, sizeof(userbuf));
75: break;
76: case 2:
77: /* Got both host and user name. */
78: break;
79: case 3:
80: packet_send_debug("Found garbage in %.100s.", filename);
81: continue;
82: default:
83: /* Weird... */
84: continue;
85: }
86:
87: host = hostbuf;
88: user = userbuf;
89: negated = 0;
90:
91: /* Process negated host names, or positive netgroups. */
92: if (host[0] == '-') {
93: negated = 1;
94: host++;
95: } else if (host[0] == '+')
96: host++;
97:
98: if (user[0] == '-') {
99: negated = 1;
100: user++;
101: } else if (user[0] == '+')
102: user++;
103:
104: /* Check for empty host/user names (particularly '+'). */
105: if (!host[0] || !user[0]) {
106: /* We come here if either was '+' or '-'. */
107: packet_send_debug("Ignoring wild host/user names in %.100s.",
108: filename);
109: continue;
110: }
111: /* Verify that host name matches. */
112: if (host[0] == '@') {
113: if (!innetgr(host + 1, hostname, NULL, NULL) &&
114: !innetgr(host + 1, ipaddr, NULL, NULL))
115: continue;
116: } else if (strcasecmp(host, hostname) && strcmp(host, ipaddr) != 0)
117: continue; /* Different hostname. */
118:
119: /* Verify that user name matches. */
120: if (user[0] == '@') {
121: if (!innetgr(user + 1, NULL, client_user, NULL))
122: continue;
123: } else if (strcmp(user, client_user) != 0)
124: continue; /* Different username. */
125:
126: /* Found the user and host. */
127: fclose(f);
128:
129: /* If the entry was negated, deny access. */
130: if (negated) {
131: packet_send_debug("Matched negative entry in %.100s.",
132: filename);
133: return 0;
134: }
135: /* Accept authentication. */
136: return 1;
137: }
138:
139: /* Authentication using this file denied. */
140: fclose(f);
141: return 0;
1.1 deraadt 142: }
143:
1.11 markus 144: /*
145: * Tries to authenticate the user using the .shosts or .rhosts file. Returns
146: * true if authentication succeeds. If ignore_rhosts is true, only
147: * /etc/hosts.equiv will be considered (.rhosts and .shosts are ignored).
148: */
1.1 deraadt 149:
1.13 markus 150: int
1.9 markus 151: auth_rhosts(struct passwd *pw, const char *client_user)
1.1 deraadt 152: {
1.9 markus 153: extern ServerOptions options;
154: char buf[1024];
155: const char *hostname, *ipaddr;
156: struct stat st;
157: static const char *rhosts_files[] = {".shosts", ".rhosts", NULL};
1.17 markus 158: u_int rhosts_file_index;
1.9 markus 159:
1.16 markus 160: /* no user given */
161: if (pw == NULL)
162: return 0;
1.9 markus 163: /* Switch to the user's uid. */
1.22 ! markus 164: temporarily_use_uid(pw);
1.11 markus 165: /*
166: * Quick check: if the user has no .shosts or .rhosts files, return
167: * failure immediately without doing costly lookups from name
168: * servers.
169: */
1.9 markus 170: for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
171: rhosts_file_index++) {
172: /* Check users .rhosts or .shosts. */
173: snprintf(buf, sizeof buf, "%.500s/%.100s",
174: pw->pw_dir, rhosts_files[rhosts_file_index]);
175: if (stat(buf, &st) >= 0)
176: break;
177: }
178: /* Switch back to privileged uid. */
179: restore_uid();
180:
181: /* Deny if The user has no .shosts or .rhosts file and there are no system-wide files. */
182: if (!rhosts_files[rhosts_file_index] &&
1.18 markus 183: stat(_PATH_RHOSTS_EQUIV, &st) < 0 &&
184: stat(_PATH_SSH_HOSTS_EQUIV, &st) < 0)
1.9 markus 185: return 0;
186:
1.20 markus 187: hostname = get_canonical_hostname(options.reverse_mapping_check);
1.9 markus 188: ipaddr = get_remote_ipaddr();
189:
190: /* If not logging in as superuser, try /etc/hosts.equiv and shosts.equiv. */
191: if (pw->pw_uid != 0) {
1.21 itojun 192: if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr, client_user,
1.9 markus 193: pw->pw_name)) {
194: packet_send_debug("Accepted for %.100s [%.100s] by /etc/hosts.equiv.",
195: hostname, ipaddr);
196: return 1;
197: }
1.18 markus 198: if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr, client_user,
1.9 markus 199: pw->pw_name)) {
200: packet_send_debug("Accepted for %.100s [%.100s] by %.100s.",
1.18 markus 201: hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV);
1.9 markus 202: return 1;
203: }
204: }
1.11 markus 205: /*
206: * Check that the home directory is owned by root or the user, and is
207: * not group or world writable.
208: */
1.9 markus 209: if (stat(pw->pw_dir, &st) < 0) {
210: log("Rhosts authentication refused for %.100s: no home directory %.200s",
211: pw->pw_name, pw->pw_dir);
1.12 markus 212: packet_send_debug("Rhosts authentication refused for %.100s: no home directory %.200s",
1.9 markus 213: pw->pw_name, pw->pw_dir);
214: return 0;
215: }
216: if (options.strict_modes &&
217: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
218: (st.st_mode & 022) != 0)) {
219: log("Rhosts authentication refused for %.100s: bad ownership or modes for home directory.",
220: pw->pw_name);
221: packet_send_debug("Rhosts authentication refused for %.100s: bad ownership or modes for home directory.",
222: pw->pw_name);
223: return 0;
224: }
225: /* Temporarily use the user's uid. */
1.22 ! markus 226: temporarily_use_uid(pw);
1.9 markus 227:
228: /* Check all .rhosts files (currently .shosts and .rhosts). */
229: for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
230: rhosts_file_index++) {
231: /* Check users .rhosts or .shosts. */
232: snprintf(buf, sizeof buf, "%.500s/%.100s",
233: pw->pw_dir, rhosts_files[rhosts_file_index]);
234: if (stat(buf, &st) < 0)
235: continue;
236:
1.11 markus 237: /*
238: * Make sure that the file is either owned by the user or by
239: * root, and make sure it is not writable by anyone but the
240: * owner. This is to help avoid novices accidentally
241: * allowing access to their account by anyone.
242: */
1.9 markus 243: if (options.strict_modes &&
244: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
245: (st.st_mode & 022) != 0)) {
246: log("Rhosts authentication refused for %.100s: bad modes for %.200s",
247: pw->pw_name, buf);
248: packet_send_debug("Bad file modes for %.200s", buf);
249: continue;
250: }
251: /* Check if we have been configured to ignore .rhosts and .shosts files. */
252: if (options.ignore_rhosts) {
253: packet_send_debug("Server has been configured to ignore %.100s.",
254: rhosts_files[rhosts_file_index]);
255: continue;
256: }
257: /* Check if authentication is permitted by the file. */
258: if (check_rhosts_file(buf, hostname, ipaddr, client_user, pw->pw_name)) {
259: packet_send_debug("Accepted by %.100s.",
260: rhosts_files[rhosts_file_index]);
261: /* Restore the privileged uid. */
262: restore_uid();
263: return 1;
264: }
265: }
266:
267: /* Restore the privileged uid. */
268: restore_uid();
269: return 0;
1.1 deraadt 270: }