Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 17 | RCSID("$OpenBSD: auth-rhosts.c,v 1.21 2001/02/08 19:30:51 itojun Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
| 19 | #include "packet.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | #include "xmalloc.h" |
| 21 | #include "uidswap.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 22 | #include "pathnames.h" |
| 23 | #include "log.h" |
Damien Miller | 6d7b2cd | 1999-11-12 15:19:27 +1100 | [diff] [blame] | 24 | #include "servconf.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 25 | #include "canohost.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 26 | #include "auth.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 34 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | check_rhosts_file(const char *filename, const char *hostname, |
| 36 | const char *ipaddr, const char *client_user, |
| 37 | const char *server_user) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 39 | FILE *f; |
| 40 | char buf[1024]; /* Must not be larger than host, user, dummy below. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 42 | /* Open the .rhosts file, deny if unreadable */ |
| 43 | f = fopen(filename, "r"); |
| 44 | if (!f) |
| 45 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) |
| 53 | ; |
| 54 | if (*cp == '#' || *cp == '\n' || !*cp) |
| 55 | continue; |
| 56 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 57 | /* |
| 58 | * NO_PLUS is supported at least on OSF/1. We skip it (we |
| 59 | * don't ever support the plus syntax). |
| 60 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | if (strncmp(cp, "NO_PLUS", 7) == 0) |
| 62 | continue; |
| 63 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 64 | /* |
| 65 | * This should be safe because each buffer is as big as the |
| 66 | * whole string, and thus cannot be overwritten. |
| 67 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | } |
| 138 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | /* Authentication using this file denied. */ |
| 140 | fclose(f); |
| 141 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | } |
| 143 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 150 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | auth_rhosts(struct passwd *pw, const char *client_user) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 152 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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}; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 158 | u_int rhosts_file_index; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 160 | /* no user given */ |
| 161 | if (pw == NULL) |
| 162 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | /* Switch to the user's uid. */ |
| 164 | temporarily_use_uid(pw->pw_uid); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 177 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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] && |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 183 | stat(_PATH_RHOSTS_EQUIV, &st) < 0 && |
| 184 | stat(_PATH_SSH_HOSTS_EQUIV, &st) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | return 0; |
| 186 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 187 | hostname = get_canonical_hostname(options.reverse_mapping_check); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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) { |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 192 | if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr, client_user, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | pw->pw_name)) { |
| 194 | packet_send_debug("Accepted for %.100s [%.100s] by /etc/hosts.equiv.", |
| 195 | hostname, ipaddr); |
| 196 | return 1; |
| 197 | } |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 198 | if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr, client_user, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 199 | pw->pw_name)) { |
| 200 | packet_send_debug("Accepted for %.100s [%.100s] by %.100s.", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 201 | hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 202 | return 1; |
| 203 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 205 | /* |
| 206 | * Check that the home directory is owned by root or the user, and is |
| 207 | * not group or world writable. |
| 208 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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); |
Damien Miller | 68e45de | 1999-12-27 23:54:55 +1100 | [diff] [blame] | 212 | packet_send_debug("Rhosts authentication refused for %.100s: no home directory %.200s", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 213 | 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 227 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 236 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 265 | } |
| 266 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 267 | /* Restore the privileged uid. */ |
| 268 | restore_uid(); |
| 269 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | } |