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