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 | * |
| 3 | * auth-rsa.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Mon Mar 27 01:46:52 1995 ylo |
| 11 | * |
| 12 | * RSA-based authentication. This code determines whether to admit a login |
| 13 | * based on RSA authentication. This file also contains functions to check |
| 14 | * validity of the host key. |
| 15 | * |
| 16 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | |
| 18 | #include "includes.h" |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 19 | RCSID("$Id: auth-rsa.c,v 1.12 2000/01/20 11:44:09 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | |
| 21 | #include "rsa.h" |
| 22 | #include "packet.h" |
| 23 | #include "xmalloc.h" |
| 24 | #include "ssh.h" |
| 25 | #include "mpaux.h" |
| 26 | #include "uidswap.h" |
Damien Miller | 6d7b2cd | 1999-11-12 15:19:27 +1100 | [diff] [blame] | 27 | #include "servconf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 29 | #ifdef HAVE_OPENSSL |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | #include <openssl/rsa.h> |
| 31 | #include <openssl/md5.h> |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 32 | #endif |
| 33 | #ifdef HAVE_SSL |
| 34 | #include <ssl/rsa.h> |
| 35 | #include <ssl/md5.h> |
| 36 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | /* Flags that may be set in authorized_keys options. */ |
| 39 | extern int no_port_forwarding_flag; |
| 40 | extern int no_agent_forwarding_flag; |
| 41 | extern int no_x11_forwarding_flag; |
| 42 | extern int no_pty_flag; |
| 43 | extern char *forced_command; |
| 44 | extern struct envstring *custom_environment; |
| 45 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 46 | /* |
| 47 | * Session identifier that is used to bind key exchange and authentication |
| 48 | * responses to a particular session. |
| 49 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | extern unsigned char session_id[16]; |
| 51 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 52 | /* |
| 53 | * The .ssh/authorized_keys file contains public keys, one per line, in the |
| 54 | * following format: |
| 55 | * options bits e n comment |
| 56 | * where bits, e and n are decimal numbers, |
| 57 | * and comment is any string of characters up to newline. The maximum |
| 58 | * length of a line is 8000 characters. See the documentation for a |
| 59 | * description of the options. |
| 60 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 62 | /* |
| 63 | * Performs the RSA authentication challenge-response dialog with the client, |
| 64 | * and returns true (non-zero) if the client gave the correct answer to |
| 65 | * our challenge; returns zero if the client gives a wrong answer. |
| 66 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | |
| 68 | int |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 69 | auth_rsa_challenge_dialog(BIGNUM *e, BIGNUM *n) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | BIGNUM *challenge, *encrypted_challenge, *aux; |
| 72 | RSA *pk; |
| 73 | BN_CTX *ctx = BN_CTX_new(); |
| 74 | unsigned char buf[32], mdbuf[16], response[16]; |
| 75 | MD5_CTX md; |
| 76 | unsigned int i; |
| 77 | int plen, len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 79 | encrypted_challenge = BN_new(); |
| 80 | challenge = BN_new(); |
| 81 | aux = BN_new(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 83 | /* Generate a random challenge. */ |
| 84 | BN_rand(challenge, 256, 0, 0); |
| 85 | BN_mod(challenge, challenge, n, ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 87 | /* Create the public key data structure. */ |
| 88 | pk = RSA_new(); |
| 89 | pk->e = BN_new(); |
| 90 | BN_copy(pk->e, e); |
| 91 | pk->n = BN_new(); |
| 92 | BN_copy(pk->n, n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | /* Encrypt the challenge with the public key. */ |
| 95 | rsa_public_encrypt(encrypted_challenge, challenge, pk); |
| 96 | RSA_free(pk); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 97 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 98 | /* Send the encrypted challenge to the client. */ |
| 99 | packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE); |
| 100 | packet_put_bignum(encrypted_challenge); |
| 101 | packet_send(); |
| 102 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 105 | len = BN_num_bytes(challenge); |
| 106 | if (len <= 0 || len > 32) |
| 107 | fatal("auth_rsa_challenge_dialog: bad challenge length %d", len); |
| 108 | memset(buf, 0, 32); |
| 109 | BN_bn2bin(challenge, buf + 32 - len); |
| 110 | MD5_Init(&md); |
| 111 | MD5_Update(&md, buf, 32); |
| 112 | MD5_Update(&md, session_id, 16); |
| 113 | MD5_Final(mdbuf, &md); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | /* We will no longer need these. */ |
| 116 | BN_clear_free(encrypted_challenge); |
| 117 | BN_clear_free(challenge); |
| 118 | BN_clear_free(aux); |
| 119 | BN_CTX_free(ctx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | /* Wait for a response. */ |
| 122 | packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE); |
| 123 | packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE); |
| 124 | for (i = 0; i < 16; i++) |
| 125 | response[i] = packet_get_char(); |
| 126 | |
| 127 | /* Verify that the response is the original challenge. */ |
| 128 | if (memcmp(response, mdbuf, 16) != 0) { |
| 129 | /* Wrong answer. */ |
| 130 | return 0; |
| 131 | } |
| 132 | /* Correct answer. */ |
| 133 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | } |
| 135 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 136 | /* |
| 137 | * Performs the RSA authentication dialog with the client. This returns |
| 138 | * 0 if the client could not be authenticated, and 1 if authentication was |
| 139 | * successful. This may exit if there is a serious protocol violation. |
| 140 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | |
| 142 | int |
Damien Miller | 6d7b2cd | 1999-11-12 15:19:27 +1100 | [diff] [blame] | 143 | auth_rsa(struct passwd *pw, BIGNUM *client_n) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | extern ServerOptions options; |
| 146 | char line[8192], file[1024]; |
| 147 | int authenticated; |
| 148 | unsigned int bits; |
| 149 | FILE *f; |
| 150 | unsigned long linenum = 0; |
| 151 | struct stat st; |
| 152 | BIGNUM *e, *n; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | /* Temporarily use the user's uid. */ |
| 155 | temporarily_use_uid(pw->pw_uid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 156 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | /* The authorized keys. */ |
| 158 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, |
| 159 | SSH_USER_PERMITTED_KEYS); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 160 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | /* Fail quietly if file does not exist */ |
| 162 | if (stat(file, &st) < 0) { |
| 163 | /* Restore the privileged uid. */ |
| 164 | restore_uid(); |
| 165 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | /* Open the file containing the authorized keys. */ |
| 168 | f = fopen(file, "r"); |
| 169 | if (!f) { |
| 170 | /* Restore the privileged uid. */ |
| 171 | restore_uid(); |
| 172 | packet_send_debug("Could not open %.900s for reading.", file); |
| 173 | packet_send_debug("If your home is on an NFS volume, it may need to be world-readable."); |
| 174 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | if (options.strict_modes) { |
| 177 | int fail = 0; |
| 178 | char buf[1024]; |
| 179 | /* Check open file in order to avoid open/stat races */ |
| 180 | if (fstat(fileno(f), &st) < 0 || |
| 181 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 182 | (st.st_mode & 022) != 0) { |
| 183 | snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: " |
| 184 | "bad ownership or modes for '%s'.", pw->pw_name, file); |
| 185 | fail = 1; |
| 186 | } else { |
| 187 | /* Check path to SSH_USER_PERMITTED_KEYS */ |
| 188 | int i; |
| 189 | static const char *check[] = { |
| 190 | "", SSH_USER_DIR, NULL |
| 191 | }; |
| 192 | for (i = 0; check[i]; i++) { |
| 193 | snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]); |
| 194 | if (stat(line, &st) < 0 || |
| 195 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 196 | (st.st_mode & 022) != 0) { |
| 197 | snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: " |
| 198 | "bad ownership or modes for '%s'.", pw->pw_name, line); |
| 199 | fail = 1; |
| 200 | break; |
| 201 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 202 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 203 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | if (fail) { |
| 205 | log(buf); |
| 206 | packet_send_debug(buf); |
| 207 | restore_uid(); |
| 208 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 209 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | } |
| 211 | /* Flag indicating whether authentication has succeeded. */ |
| 212 | authenticated = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | e = BN_new(); |
| 215 | n = BN_new(); |
| 216 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 217 | /* |
| 218 | * Go though the accepted keys, looking for the current key. If |
| 219 | * found, perform a challenge-response dialog to verify that the |
| 220 | * user really has the corresponding private key. |
| 221 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 222 | while (fgets(line, sizeof(line), f)) { |
| 223 | char *cp; |
| 224 | char *options; |
| 225 | |
| 226 | linenum++; |
| 227 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 228 | /* Skip leading whitespace, empty and comment lines. */ |
| 229 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 230 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | if (!*cp || *cp == '\n' || *cp == '#') |
| 232 | continue; |
| 233 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 234 | /* |
| 235 | * Check if there are options for this key, and if so, |
| 236 | * save their starting address and skip the option part |
| 237 | * for now. If there are no options, set the starting |
| 238 | * address to NULL. |
| 239 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | if (*cp < '0' || *cp > '9') { |
| 241 | int quoted = 0; |
| 242 | options = cp; |
| 243 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 244 | if (*cp == '\\' && cp[1] == '"') |
| 245 | cp++; /* Skip both */ |
| 246 | else if (*cp == '"') |
| 247 | quoted = !quoted; |
| 248 | } |
| 249 | } else |
| 250 | options = NULL; |
| 251 | |
| 252 | /* Parse the key from the line. */ |
| 253 | if (!auth_rsa_read_key(&cp, &bits, e, n)) { |
| 254 | debug("%.100s, line %lu: bad key syntax", |
| 255 | SSH_USER_PERMITTED_KEYS, linenum); |
| 256 | packet_send_debug("%.100s, line %lu: bad key syntax", |
| 257 | SSH_USER_PERMITTED_KEYS, linenum); |
| 258 | continue; |
| 259 | } |
| 260 | /* cp now points to the comment part. */ |
| 261 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 262 | /* Check if the we have found the desired key (identified by its modulus). */ |
| 263 | if (BN_cmp(n, client_n) != 0) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 264 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 266 | /* check the real bits */ |
| 267 | if (bits != BN_num_bits(n)) |
| 268 | log("Warning: %s, line %ld: keysize mismatch: " |
| 269 | "actual %d vs. announced %d.", |
| 270 | file, linenum, BN_num_bits(n), bits); |
| 271 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 272 | /* We have found the desired key. */ |
| 273 | |
| 274 | /* Perform the challenge-response dialog for this key. */ |
| 275 | if (!auth_rsa_challenge_dialog(e, n)) { |
| 276 | /* Wrong response. */ |
| 277 | verbose("Wrong response to RSA authentication challenge."); |
| 278 | packet_send_debug("Wrong response to RSA authentication challenge."); |
| 279 | continue; |
| 280 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 281 | /* |
| 282 | * Correct response. The client has been successfully |
| 283 | * authenticated. Note that we have not yet processed the |
| 284 | * options; this will be reset if the options cause the |
| 285 | * authentication to be rejected. |
| 286 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 287 | authenticated = 1; |
| 288 | |
| 289 | /* RSA part of authentication was accepted. Now process the options. */ |
| 290 | if (options) { |
| 291 | while (*options && *options != ' ' && *options != '\t') { |
| 292 | cp = "no-port-forwarding"; |
| 293 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 294 | packet_send_debug("Port forwarding disabled."); |
| 295 | no_port_forwarding_flag = 1; |
| 296 | options += strlen(cp); |
| 297 | goto next_option; |
| 298 | } |
| 299 | cp = "no-agent-forwarding"; |
| 300 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 301 | packet_send_debug("Agent forwarding disabled."); |
| 302 | no_agent_forwarding_flag = 1; |
| 303 | options += strlen(cp); |
| 304 | goto next_option; |
| 305 | } |
| 306 | cp = "no-X11-forwarding"; |
| 307 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 308 | packet_send_debug("X11 forwarding disabled."); |
| 309 | no_x11_forwarding_flag = 1; |
| 310 | options += strlen(cp); |
| 311 | goto next_option; |
| 312 | } |
| 313 | cp = "no-pty"; |
| 314 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 315 | packet_send_debug("Pty allocation disabled."); |
| 316 | no_pty_flag = 1; |
| 317 | options += strlen(cp); |
| 318 | goto next_option; |
| 319 | } |
| 320 | cp = "command=\""; |
| 321 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 322 | int i; |
| 323 | options += strlen(cp); |
| 324 | forced_command = xmalloc(strlen(options) + 1); |
| 325 | i = 0; |
| 326 | while (*options) { |
| 327 | if (*options == '"') |
| 328 | break; |
| 329 | if (*options == '\\' && options[1] == '"') { |
| 330 | options += 2; |
| 331 | forced_command[i++] = '"'; |
| 332 | continue; |
| 333 | } |
| 334 | forced_command[i++] = *options++; |
| 335 | } |
| 336 | if (!*options) { |
| 337 | debug("%.100s, line %lu: missing end quote", |
| 338 | SSH_USER_PERMITTED_KEYS, linenum); |
| 339 | packet_send_debug("%.100s, line %lu: missing end quote", |
| 340 | SSH_USER_PERMITTED_KEYS, linenum); |
| 341 | continue; |
| 342 | } |
| 343 | forced_command[i] = 0; |
| 344 | packet_send_debug("Forced command: %.900s", forced_command); |
| 345 | options++; |
| 346 | goto next_option; |
| 347 | } |
| 348 | cp = "environment=\""; |
| 349 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 350 | int i; |
| 351 | char *s; |
| 352 | struct envstring *new_envstring; |
| 353 | options += strlen(cp); |
| 354 | s = xmalloc(strlen(options) + 1); |
| 355 | i = 0; |
| 356 | while (*options) { |
| 357 | if (*options == '"') |
| 358 | break; |
| 359 | if (*options == '\\' && options[1] == '"') { |
| 360 | options += 2; |
| 361 | s[i++] = '"'; |
| 362 | continue; |
| 363 | } |
| 364 | s[i++] = *options++; |
| 365 | } |
| 366 | if (!*options) { |
| 367 | debug("%.100s, line %lu: missing end quote", |
| 368 | SSH_USER_PERMITTED_KEYS, linenum); |
| 369 | packet_send_debug("%.100s, line %lu: missing end quote", |
| 370 | SSH_USER_PERMITTED_KEYS, linenum); |
| 371 | continue; |
| 372 | } |
| 373 | s[i] = 0; |
| 374 | packet_send_debug("Adding to environment: %.900s", s); |
| 375 | debug("Adding to environment: %.900s", s); |
| 376 | options++; |
| 377 | new_envstring = xmalloc(sizeof(struct envstring)); |
| 378 | new_envstring->s = s; |
| 379 | new_envstring->next = custom_environment; |
| 380 | custom_environment = new_envstring; |
| 381 | goto next_option; |
| 382 | } |
| 383 | cp = "from=\""; |
| 384 | if (strncmp(options, cp, strlen(cp)) == 0) { |
| 385 | char *patterns = xmalloc(strlen(options) + 1); |
| 386 | int i; |
| 387 | options += strlen(cp); |
| 388 | i = 0; |
| 389 | while (*options) { |
| 390 | if (*options == '"') |
| 391 | break; |
| 392 | if (*options == '\\' && options[1] == '"') { |
| 393 | options += 2; |
| 394 | patterns[i++] = '"'; |
| 395 | continue; |
| 396 | } |
| 397 | patterns[i++] = *options++; |
| 398 | } |
| 399 | if (!*options) { |
| 400 | debug("%.100s, line %lu: missing end quote", |
| 401 | SSH_USER_PERMITTED_KEYS, linenum); |
| 402 | packet_send_debug("%.100s, line %lu: missing end quote", |
| 403 | SSH_USER_PERMITTED_KEYS, linenum); |
| 404 | continue; |
| 405 | } |
| 406 | patterns[i] = 0; |
| 407 | options++; |
| 408 | if (!match_hostname(get_canonical_hostname(), patterns, |
| 409 | strlen(patterns)) && |
| 410 | !match_hostname(get_remote_ipaddr(), patterns, |
| 411 | strlen(patterns))) { |
| 412 | log("RSA authentication tried for %.100s with correct key but not from a permitted host (host=%.200s, ip=%.200s).", |
| 413 | pw->pw_name, get_canonical_hostname(), |
| 414 | get_remote_ipaddr()); |
| 415 | packet_send_debug("Your host '%.200s' is not permitted to use this key for login.", |
| 416 | get_canonical_hostname()); |
| 417 | xfree(patterns); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 418 | /* key invalid for this host, reset flags */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 419 | authenticated = 0; |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 420 | no_agent_forwarding_flag = 0; |
| 421 | no_port_forwarding_flag = 0; |
| 422 | no_pty_flag = 0; |
| 423 | no_x11_forwarding_flag = 0; |
| 424 | while (custom_environment) { |
| 425 | struct envstring *ce = custom_environment; |
| 426 | custom_environment = ce->next; |
| 427 | xfree(ce->s); |
| 428 | xfree(ce); |
| 429 | } |
| 430 | if (forced_command) { |
| 431 | xfree(forced_command); |
| 432 | forced_command = NULL; |
| 433 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | xfree(patterns); |
| 437 | /* Host name matches. */ |
| 438 | goto next_option; |
| 439 | } |
| 440 | bad_option: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 441 | log("Bad options in %.100s file, line %lu: %.50s", |
| 442 | SSH_USER_PERMITTED_KEYS, linenum, options); |
| 443 | packet_send_debug("Bad options in %.100s file, line %lu: %.50s", |
| 444 | SSH_USER_PERMITTED_KEYS, linenum, options); |
| 445 | authenticated = 0; |
| 446 | break; |
| 447 | |
| 448 | next_option: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 449 | /* |
| 450 | * Skip the comma, and move to the next option |
| 451 | * (or break out if there are no more). |
| 452 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 453 | if (!*options) |
| 454 | fatal("Bugs in auth-rsa.c option processing."); |
| 455 | if (*options == ' ' || *options == '\t') |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 456 | break; /* End of options. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 457 | if (*options != ',') |
| 458 | goto bad_option; |
| 459 | options++; |
| 460 | /* Process the next option. */ |
| 461 | continue; |
| 462 | } |
| 463 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 464 | /* |
| 465 | * Break out of the loop if authentication was successful; |
| 466 | * otherwise continue searching. |
| 467 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 468 | if (authenticated) |
| 469 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 470 | } |
| 471 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 472 | /* Restore the privileged uid. */ |
| 473 | restore_uid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 474 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 475 | /* Close the file. */ |
| 476 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 477 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | BN_clear_free(n); |
| 479 | BN_clear_free(e); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 480 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 481 | if (authenticated) |
| 482 | packet_send_debug("RSA authentication accepted."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 483 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 484 | /* Return authentication result. */ |
| 485 | return authenticated; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | } |