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