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