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