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