blob: 9b018c9d92f14f8b1632396146733f7049925961 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * 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.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * 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".
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000017RCSID("$OpenBSD: auth-rhosts.c,v 1.21 2001/02/08 19:30:51 itojun Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
21#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "pathnames.h"
23#include "log.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110024#include "servconf.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000026#include "auth.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller5428f641999-11-25 11:54:57 +110028/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller4af51302000-04-16 11:18:38 +100034int
Damien Miller95def091999-11-25 00:26:21 +110035check_rhosts_file(const char *filename, const char *hostname,
36 const char *ipaddr, const char *client_user,
37 const char *server_user)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038{
Damien Miller95def091999-11-25 00:26:21 +110039 FILE *f;
40 char buf[1024]; /* Must not be larger than host, user, dummy below. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Damien Miller95def091999-11-25 00:26:21 +110042 /* Open the .rhosts file, deny if unreadable */
43 f = fopen(filename, "r");
44 if (!f)
45 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Miller95def091999-11-25 00:26:21 +110047 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller95def091999-11-25 00:26:21 +110052 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
53 ;
54 if (*cp == '#' || *cp == '\n' || !*cp)
55 continue;
56
Damien Miller5428f641999-11-25 11:54:57 +110057 /*
58 * NO_PLUS is supported at least on OSF/1. We skip it (we
59 * don't ever support the plus syntax).
60 */
Damien Miller95def091999-11-25 00:26:21 +110061 if (strncmp(cp, "NO_PLUS", 7) == 0)
62 continue;
63
Damien Miller5428f641999-11-25 11:54:57 +110064 /*
65 * This should be safe because each buffer is as big as the
66 * whole string, and thus cannot be overwritten.
67 */
Damien Miller95def091999-11-25 00:26:21 +110068 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137 }
138
Damien Miller95def091999-11-25 00:26:21 +1100139 /* Authentication using this file denied. */
140 fclose(f);
141 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller4af51302000-04-16 11:18:38 +1000150int
Damien Miller95def091999-11-25 00:26:21 +1100151auth_rhosts(struct passwd *pw, const char *client_user)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Miller95def091999-11-25 00:26:21 +1100153 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};
Ben Lindstrom46c16222000-12-22 01:43:59 +0000158 u_int rhosts_file_index;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller874d77b2000-10-14 16:23:11 +1100160 /* no user given */
161 if (pw == NULL)
162 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100163 /* Switch to the user's uid. */
164 temporarily_use_uid(pw->pw_uid);
Damien Miller5428f641999-11-25 11:54:57 +1100165 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100170 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177 }
Damien Miller95def091999-11-25 00:26:21 +1100178 /* 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] &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000183 stat(_PATH_RHOSTS_EQUIV, &st) < 0 &&
184 stat(_PATH_SSH_HOSTS_EQUIV, &st) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100185 return 0;
186
Damien Miller33804262001-02-04 23:20:18 +1100187 hostname = get_canonical_hostname(options.reverse_mapping_check);
Damien Miller95def091999-11-25 00:26:21 +1100188 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) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000192 if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr, client_user,
Damien Miller95def091999-11-25 00:26:21 +1100193 pw->pw_name)) {
194 packet_send_debug("Accepted for %.100s [%.100s] by /etc/hosts.equiv.",
195 hostname, ipaddr);
196 return 1;
197 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000198 if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr, client_user,
Damien Miller95def091999-11-25 00:26:21 +1100199 pw->pw_name)) {
200 packet_send_debug("Accepted for %.100s [%.100s] by %.100s.",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000201 hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV);
Damien Miller95def091999-11-25 00:26:21 +1100202 return 1;
203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204 }
Damien Miller5428f641999-11-25 11:54:57 +1100205 /*
206 * Check that the home directory is owned by root or the user, and is
207 * not group or world writable.
208 */
Damien Miller95def091999-11-25 00:26:21 +1100209 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);
Damien Miller68e45de1999-12-27 23:54:55 +1100212 packet_send_debug("Rhosts authentication refused for %.100s: no home directory %.200s",
Damien Miller95def091999-11-25 00:26:21 +1100213 pw->pw_name, pw->pw_dir);
214 return 0;
215 }
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;
224 }
225 /* Temporarily use the user's uid. */
226 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227
Damien Miller95def091999-11-25 00:26:21 +1100228 /* 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Miller5428f641999-11-25 11:54:57 +1100237 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100243 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 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 }
266
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Restore the privileged uid. */
268 restore_uid();
269 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}