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