auth-rhosts.c revision 1.1.1.12 1 /* $OpenBSD: auth-rhosts.c,v 1.55 2022/02/23 11:15:57 djm 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 <fcntl.h>
21 #include <netgroup.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27
28 #include "packet.h"
29 #include "uidswap.h"
30 #include "pathnames.h"
31 #include "log.h"
32 #include "misc.h"
33 #include "xmalloc.h"
34 #include "sshbuf.h"
35 #include "sshkey.h"
36 #include "servconf.h"
37 #include "canohost.h"
38 #include "hostfile.h"
39 #include "auth.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 #define RBUFLN 1024
58 char buf[RBUFLN];/* Must not be larger than host, user, dummy below. */
59 int fd;
60 struct stat st;
61
62 /* Open the .rhosts file, deny if unreadable */
63 if ((fd = open(filename, O_RDONLY|O_NONBLOCK)) == -1)
64 return 0;
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 }
80 while (fgets(buf, sizeof(buf), f)) {
81 /* All three must have length >= buf to avoid overflows. */
82 char hostbuf[RBUFLN], userbuf[RBUFLN], dummy[RBUFLN];
83 char *host, *user, *cp;
84 int negated;
85
86 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
87 ;
88 if (*cp == '#' || *cp == '\n' || !*cp)
89 continue;
90
91 /*
92 * NO_PLUS is supported at least on OSF/1. We skip it (we
93 * don't ever support the plus syntax).
94 */
95 if (strncmp(cp, "NO_PLUS", 7) == 0)
96 continue;
97
98 /*
99 * This should be safe because each buffer is as big as the
100 * whole string, and thus cannot be overwritten.
101 */
102 switch (sscanf(buf, "%1023s %1023s %1023s", hostbuf, userbuf,
103 dummy)) {
104 case 0:
105 auth_debug_add("Found empty line in %.100s.", filename);
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:
115 auth_debug_add("Found garbage in %.100s.", filename);
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 '-'. */
142 auth_debug_add("Ignoring wild host/user names "
143 "in %.100s.", filename);
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;
151 } else if (strcasecmp(host, hostname) &&
152 strcmp(host, ipaddr) != 0)
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) {
167 auth_debug_add("Matched negative entry in %.100s.",
168 filename);
169 return 0;
170 }
171 /* Accept authentication. */
172 return 1;
173 }
174
175 /* Authentication using this file denied. */
176 fclose(f);
177 return 0;
178 }
179
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 */
185 int
186 auth_rhosts2(struct passwd *pw, const char *client_user, const char *hostname,
187 const char *ipaddr)
188 {
189 char *path = NULL;
190 struct stat st;
191 static const char * const rhosts_files[] = {".shosts", ".rhosts", NULL};
192 u_int rhosts_file_index;
193 int r;
194
195 debug2_f("clientuser %s hostname %s ipaddr %s",
196 client_user, hostname, ipaddr);
197
198 /* Switch to the user's uid. */
199 temporarily_use_uid(pw);
200 /*
201 * Quick check: if the user has no .shosts or .rhosts files and
202 * no system hosts.equiv/shosts.equiv files exist then return
203 * failure immediately without doing costly lookups from name
204 * servers.
205 */
206 for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
207 rhosts_file_index++) {
208 /* Check users .rhosts or .shosts. */
209 xasprintf(&path, "%s/%s",
210 pw->pw_dir, rhosts_files[rhosts_file_index]);
211 r = stat(path, &st);
212 free(path);
213 if (r >= 0)
214 break;
215 }
216 /* Switch back to privileged uid. */
217 restore_uid();
218
219 /*
220 * Deny if The user has no .shosts or .rhosts file and there
221 * are no system-wide files.
222 */
223 if (!rhosts_files[rhosts_file_index] &&
224 stat(_PATH_RHOSTS_EQUIV, &st) == -1 &&
225 stat(_PATH_SSH_HOSTS_EQUIV, &st) == -1) {
226 debug3_f("no hosts access files exist");
227 return 0;
228 }
229
230 /*
231 * If not logging in as superuser, try /etc/hosts.equiv and
232 * shosts.equiv.
233 */
234 if (pw->pw_uid == 0)
235 debug3_f("root user, ignoring system hosts files");
236 else {
237 if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr,
238 client_user, pw->pw_name)) {
239 auth_debug_add("Accepted for %.100s [%.100s] by "
240 "/etc/hosts.equiv.", hostname, ipaddr);
241 return 1;
242 }
243 if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr,
244 client_user, pw->pw_name)) {
245 auth_debug_add("Accepted for %.100s [%.100s] by "
246 "%.100s.", hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV);
247 return 1;
248 }
249 }
250
251 /*
252 * Check that the home directory is owned by root or the user, and is
253 * not group or world writable.
254 */
255 if (stat(pw->pw_dir, &st) == -1) {
256 logit("Rhosts authentication refused for %.100s: "
257 "no home directory %.200s", pw->pw_name, pw->pw_dir);
258 auth_debug_add("Rhosts authentication refused for %.100s: "
259 "no home directory %.200s", pw->pw_name, pw->pw_dir);
260 return 0;
261 }
262 if (options.strict_modes &&
263 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
264 (st.st_mode & 022) != 0)) {
265 logit("Rhosts authentication refused for %.100s: "
266 "bad ownership or modes for home directory.", pw->pw_name);
267 auth_debug_add("Rhosts authentication refused for %.100s: "
268 "bad ownership or modes for home directory.", pw->pw_name);
269 return 0;
270 }
271 /* Temporarily use the user's uid. */
272 temporarily_use_uid(pw);
273
274 /* Check all .rhosts files (currently .shosts and .rhosts). */
275 for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
276 rhosts_file_index++) {
277 /* Check users .rhosts or .shosts. */
278 xasprintf(&path, "%s/%s",
279 pw->pw_dir, rhosts_files[rhosts_file_index]);
280 if (stat(path, &st) == -1) {
281 free(path);
282 continue;
283 }
284
285 /*
286 * Make sure that the file is either owned by the user or by
287 * root, and make sure it is not writable by anyone but the
288 * owner. This is to help avoid novices accidentally
289 * allowing access to their account by anyone.
290 */
291 if (options.strict_modes &&
292 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
293 (st.st_mode & 022) != 0)) {
294 logit("Rhosts authentication refused for %.100s: "
295 "bad modes for %.200s", pw->pw_name, path);
296 auth_debug_add("Bad file modes for %.200s", path);
297 free(path);
298 continue;
299 }
300 /*
301 * Check if we have been configured to ignore .rhosts
302 * and .shosts files.
303 */
304 if (options.ignore_rhosts == IGNORE_RHOSTS_YES ||
305 (options.ignore_rhosts == IGNORE_RHOSTS_SHOSTS &&
306 strcmp(rhosts_files[rhosts_file_index], ".shosts") != 0)) {
307 auth_debug_add("Server has been configured to "
308 "ignore %.100s.", rhosts_files[rhosts_file_index]);
309 free(path);
310 continue;
311 }
312 /* Check if authentication is permitted by the file. */
313 if (check_rhosts_file(path, hostname, ipaddr,
314 client_user, pw->pw_name)) {
315 auth_debug_add("Accepted by %.100s.",
316 rhosts_files[rhosts_file_index]);
317 /* Restore the privileged uid. */
318 restore_uid();
319 auth_debug_add("Accepted host %s ip %s client_user "
320 "%s server_user %s", hostname, ipaddr,
321 client_user, pw->pw_name);
322 free(path);
323 return 1;
324 }
325 free(path);
326 }
327
328 /* Restore the privileged uid. */
329 restore_uid();
330 return 0;
331 }
332