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