5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Fri Mar 17 05:12:18 1995 ylo
12 Rhosts authentication. This file contains code to check whether to admit
13 the login based on rhosts authentication. This file also processes
27 /* This function processes an rhosts-style file (.rhosts, .shosts, or
28 /etc/hosts.equiv). This returns true if authentication can be granted
29 based on the file, and returns zero otherwise. */
31 int check_rhosts_file(const char *filename, const char *hostname,
32 const char *ipaddr, const char *client_user,
33 const char *server_user)
36 char buf[1024]; /* Must not be larger than host, user, dummy below. */
38 /* Open the .rhosts file. */
39 f = fopen(filename, "r");
41 return 0; /* Cannot read the .rhosts - deny access. */
43 /* Go through the file, checking every entry. */
44 while (fgets(buf, sizeof(buf), f))
46 /* All three must be at least as big as buf to avoid overflows. */
47 char hostbuf[1024], userbuf[1024], dummy[1024], *host, *user, *cp;
50 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
52 if (*cp == '#' || *cp == '\n' || !*cp)
55 /* NO_PLUS is supported at least on OSF/1. We skip it (we don't ever
56 support the plus syntax). */
57 if (strncmp(cp, "NO_PLUS", 7) == 0)
60 /* This should be safe because each buffer is as big as the whole
61 string, and thus cannot be overwritten. */
62 switch (sscanf(buf, "%s %s %s", hostbuf, userbuf, dummy))
65 packet_send_debug("Found empty line in %.100s.", filename);
66 continue; /* Empty line? */
69 strlcpy(userbuf, server_user, sizeof(userbuf));
72 /* Got both host and user name. */
75 packet_send_debug("Found garbage in %.100s.", filename);
76 continue; /* Extra garbage */
78 continue; /* Weird... */
85 /* Process negated host names, or positive netgroups. */
104 /* Check for empty host/user names (particularly '+'). */
105 if (!host[0] || !user[0])
107 /* We come here if either was '+' or '-'. */
108 packet_send_debug("Ignoring wild host/user names in %.100s.",
113 /* Verify that host name matches. */
116 if (!innetgr(host + 1, hostname, NULL, NULL) &&
117 !innetgr(host + 1, ipaddr, NULL, NULL))
121 if (strcasecmp(host, hostname) && strcmp(host, ipaddr) != 0)
122 continue; /* Different hostname. */
124 /* Verify that user name matches. */
127 if (!innetgr(user + 1, NULL, client_user, NULL))
131 if (strcmp(user, client_user) != 0)
132 continue; /* Different username. */
134 /* Found the user and host. */
137 /* If the entry was negated, deny access. */
140 packet_send_debug("Matched negative entry in %.100s.",
145 /* Accept authentication. */
149 /* Authentication using this file denied. */
154 /* Tries to authenticate the user using the .shosts or .rhosts file.
155 Returns true if authentication succeeds. If ignore_rhosts is
156 true, only /etc/hosts.equiv will be considered (.rhosts and .shosts
159 int auth_rhosts(struct passwd *pw, const char *client_user)
161 extern ServerOptions options;
163 const char *hostname, *ipaddr;
165 static const char *rhosts_files[] = { ".shosts", ".rhosts", NULL };
166 unsigned int rhosts_file_index;
168 /* Quick check: if the user has no .shosts or .rhosts files, return failure
169 immediately without doing costly lookups from name servers. */
170 /* Switch to the user's uid. */
171 temporarily_use_uid(pw->pw_uid);
172 for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
175 /* Check users .rhosts or .shosts. */
176 snprintf(buf, sizeof buf, "%.500s/%.100s",
177 pw->pw_dir, rhosts_files[rhosts_file_index]);
178 if (stat(buf, &st) >= 0)
181 /* Switch back to privileged uid. */
184 if (!rhosts_files[rhosts_file_index] && stat("/etc/hosts.equiv", &st) < 0 &&
185 stat(SSH_HOSTS_EQUIV, &st) < 0)
186 return 0; /* The user has no .shosts or .rhosts file and there are no
187 system-wide files. */
189 /* Get the name, address, and port of the remote host. */
190 hostname = get_canonical_hostname();
191 ipaddr = get_remote_ipaddr();
193 /* If not logging in as superuser, try /etc/hosts.equiv and shosts.equiv. */
196 if (check_rhosts_file("/etc/hosts.equiv", hostname, ipaddr, client_user,
199 packet_send_debug("Accepted for %.100s [%.100s] by /etc/hosts.equiv.",
203 if (check_rhosts_file(SSH_HOSTS_EQUIV, hostname, ipaddr, client_user,
206 packet_send_debug("Accepted for %.100s [%.100s] by %.100s.",
207 hostname, ipaddr, SSH_HOSTS_EQUIV);
212 /* Check that the home directory is owned by root or the user, and is not
213 group or world writable. */
214 if (stat(pw->pw_dir, &st) < 0)
216 log("Rhosts authentication refused for %.100: no home directory %.200s",
217 pw->pw_name, pw->pw_dir);
218 packet_send_debug("Rhosts authentication refused for %.100: no home directory %.200s",
219 pw->pw_name, pw->pw_dir);
222 if (options.strict_modes &&
223 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
224 (st.st_mode & 022) != 0))
226 log("Rhosts authentication refused for %.100s: bad ownership or modes for home directory.",
228 packet_send_debug("Rhosts authentication refused for %.100s: bad ownership or modes for home directory.",
233 /* Check all .rhosts files (currently .shosts and .rhosts). */
234 /* Temporarily use the user's uid. */
235 temporarily_use_uid(pw->pw_uid);
236 for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
239 /* Check users .rhosts or .shosts. */
240 snprintf(buf, sizeof buf, "%.500s/%.100s",
241 pw->pw_dir, rhosts_files[rhosts_file_index]);
242 if (stat(buf, &st) < 0)
243 continue; /* No such file. */
245 /* Make sure that the file is either owned by the user or by root,
246 and make sure it is not writable by anyone but the owner. This is
247 to help avoid novices accidentally allowing access to their account
249 if (options.strict_modes &&
250 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
251 (st.st_mode & 022) != 0))
253 log("Rhosts authentication refused for %.100s: bad modes for %.200s",
255 packet_send_debug("Bad file modes for %.200s", buf);
259 /* Check if we have been configured to ignore .rhosts and .shosts
261 if (options.ignore_rhosts)
263 packet_send_debug("Server has been configured to ignore %.100s.",
264 rhosts_files[rhosts_file_index]);
268 /* Check if authentication is permitted by the file. */
269 if (check_rhosts_file(buf, hostname, ipaddr, client_user, pw->pw_name))
271 packet_send_debug("Accepted by %.100s.",
272 rhosts_files[rhosts_file_index]);
273 /* Restore the privileged uid. */
279 /* Rhosts authentication denied. */
280 /* Restore the privileged uid. */