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