Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 5 | * Code to connect to a remote host, and to perform the client side of the |
| 6 | * login (authentication) dialog. |
| 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include "includes.h" |
Damien Miller | 2ce18da | 2002-02-13 13:54:27 +1100 | [diff] [blame] | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.48 2002/02/11 16:15:46 markus Exp $"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 17 | |
| 18 | #include <openssl/bn.h> |
Damien Miller | 2ce18da | 2002-02-13 13:54:27 +1100 | [diff] [blame] | 19 | #include <openssl/md5.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 20 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 21 | #ifdef KRB4 |
| 22 | #include <krb.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 23 | #endif |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 24 | #ifdef KRB5 |
| 25 | #include <krb5.h> |
| 26 | #endif |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 27 | #ifdef AFS |
| 28 | #include <kafs.h> |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 29 | #include "radix.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #include "ssh.h" |
| 33 | #include "ssh1.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | #include "xmalloc.h" |
| 35 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 36 | #include "buffer.h" |
| 37 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 38 | #include "mpaux.h" |
| 39 | #include "uidswap.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 40 | #include "log.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "readconf.h" |
| 42 | #include "key.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 43 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "sshconnect.h" |
| 45 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 46 | #include "readpass.h" |
| 47 | #include "cipher.h" |
| 48 | #include "canohost.h" |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 49 | #include "auth.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 50 | |
| 51 | /* Session id for the current session. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 52 | u_char session_id[16]; |
| 53 | u_int supported_authentications = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 54 | |
| 55 | extern Options options; |
| 56 | extern char *__progname; |
| 57 | |
| 58 | /* |
| 59 | * Checks if the user has an authentication agent, and if so, tries to |
| 60 | * authenticate using the agent. |
| 61 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 62 | static int |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 63 | try_agent_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 64 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 65 | int type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 66 | char *comment; |
| 67 | AuthenticationConnection *auth; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 68 | u_char response[16]; |
| 69 | u_int i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 70 | Key *key; |
| 71 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 72 | |
| 73 | /* Get connection to the agent. */ |
| 74 | auth = ssh_get_authentication_connection(); |
| 75 | if (!auth) |
| 76 | return 0; |
| 77 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 78 | if ((challenge = BN_new()) == NULL) |
| 79 | fatal("try_agent_authentication: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 80 | /* Loop through identities served by the agent. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 81 | for (key = ssh_get_first_identity(auth, &comment, 1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 82 | key != NULL; |
| 83 | key = ssh_get_next_identity(auth, &comment, 1)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 84 | |
| 85 | /* Try this identity. */ |
| 86 | debug("Trying RSA authentication via agent with '%.100s'", comment); |
| 87 | xfree(comment); |
| 88 | |
| 89 | /* Tell the server that we are willing to authenticate using this key. */ |
| 90 | packet_start(SSH_CMSG_AUTH_RSA); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 91 | packet_put_bignum(key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 92 | packet_send(); |
| 93 | packet_write_wait(); |
| 94 | |
| 95 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 96 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 97 | |
| 98 | /* The server sends failure if it doesn\'t like our key or |
| 99 | does not support RSA authentication. */ |
| 100 | if (type == SSH_SMSG_FAILURE) { |
| 101 | debug("Server refused our key."); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 102 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 103 | continue; |
| 104 | } |
| 105 | /* Otherwise it should have sent a challenge. */ |
| 106 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 107 | packet_disconnect("Protocol error during RSA authentication: %d", |
| 108 | type); |
| 109 | |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 110 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 111 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 112 | |
| 113 | debug("Received RSA challenge from server."); |
| 114 | |
| 115 | /* Ask the agent to decrypt the challenge. */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 116 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
| 117 | /* |
| 118 | * The agent failed to authenticate this identifier |
| 119 | * although it advertised it supports this. Just |
| 120 | * return a wrong value. |
| 121 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 122 | log("Authentication agent failed to decrypt challenge."); |
| 123 | memset(response, 0, sizeof(response)); |
| 124 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 125 | key_free(key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 126 | debug("Sending response to RSA challenge."); |
| 127 | |
| 128 | /* Send the decrypted challenge back to the server. */ |
| 129 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 130 | for (i = 0; i < 16; i++) |
| 131 | packet_put_char(response[i]); |
| 132 | packet_send(); |
| 133 | packet_write_wait(); |
| 134 | |
| 135 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 136 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 137 | |
| 138 | /* The server returns success if it accepted the authentication. */ |
| 139 | if (type == SSH_SMSG_SUCCESS) { |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 140 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 141 | BN_clear_free(challenge); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 142 | debug("RSA authentication accepted by server."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 143 | return 1; |
| 144 | } |
| 145 | /* Otherwise it should return failure. */ |
| 146 | if (type != SSH_SMSG_FAILURE) |
| 147 | packet_disconnect("Protocol error waiting RSA auth response: %d", |
| 148 | type); |
| 149 | } |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 150 | ssh_close_authentication_connection(auth); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 151 | BN_clear_free(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 152 | debug("RSA authentication using agent refused."); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Computes the proper response to a RSA challenge, and sends the response to |
| 158 | * the server. |
| 159 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 160 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 161 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
| 162 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 163 | u_char buf[32], response[16]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 164 | MD5_CTX md; |
| 165 | int i, len; |
| 166 | |
| 167 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 168 | /* XXX think about Bleichenbacher, too */ |
| 169 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) |
| 170 | packet_disconnect( |
| 171 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 172 | |
| 173 | /* Compute the response. */ |
| 174 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 175 | len = BN_num_bytes(challenge); |
| 176 | if (len <= 0 || len > sizeof(buf)) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 177 | packet_disconnect( |
| 178 | "respond_to_rsa_challenge: bad challenge length %d", len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 179 | |
| 180 | memset(buf, 0, sizeof(buf)); |
| 181 | BN_bn2bin(challenge, buf + sizeof(buf) - len); |
| 182 | MD5_Init(&md); |
| 183 | MD5_Update(&md, buf, 32); |
| 184 | MD5_Update(&md, session_id, 16); |
| 185 | MD5_Final(response, &md); |
| 186 | |
| 187 | debug("Sending response to host key RSA challenge."); |
| 188 | |
| 189 | /* Send the response back to the server. */ |
| 190 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 191 | for (i = 0; i < 16; i++) |
| 192 | packet_put_char(response[i]); |
| 193 | packet_send(); |
| 194 | packet_write_wait(); |
| 195 | |
| 196 | memset(buf, 0, sizeof(buf)); |
| 197 | memset(response, 0, sizeof(response)); |
| 198 | memset(&md, 0, sizeof(md)); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Checks if the user has authentication file, and if so, tries to authenticate |
| 203 | * the user using it. |
| 204 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 205 | static int |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 206 | try_rsa_authentication(int idx) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 207 | { |
| 208 | BIGNUM *challenge; |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 209 | Key *public, *private; |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 210 | char buf[300], *passphrase, *comment, *authfile; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 211 | int i, type, quit; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 212 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 213 | public = options.identity_keys[idx]; |
| 214 | authfile = options.identity_files[idx]; |
| 215 | comment = xstrdup(authfile); |
| 216 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 217 | debug("Trying RSA authentication with key '%.100s'", comment); |
| 218 | |
| 219 | /* Tell the server that we are willing to authenticate using this key. */ |
| 220 | packet_start(SSH_CMSG_AUTH_RSA); |
| 221 | packet_put_bignum(public->rsa->n); |
| 222 | packet_send(); |
| 223 | packet_write_wait(); |
| 224 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 225 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 226 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * The server responds with failure if it doesn\'t like our key or |
| 230 | * doesn\'t support RSA authentication. |
| 231 | */ |
| 232 | if (type == SSH_SMSG_FAILURE) { |
| 233 | debug("Server refused our key."); |
| 234 | xfree(comment); |
| 235 | return 0; |
| 236 | } |
| 237 | /* Otherwise, the server should respond with a challenge. */ |
| 238 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 239 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 240 | |
| 241 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 242 | if ((challenge = BN_new()) == NULL) |
| 243 | fatal("try_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 244 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 245 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 246 | |
| 247 | debug("Received RSA challenge from server."); |
| 248 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 249 | /* |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 250 | * If the key is not stored in external hardware, we have to |
| 251 | * load the private key. Try first with empty passphrase; if it |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 252 | * fails, ask for a passphrase. |
| 253 | */ |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 254 | if (public->flags && KEY_FLAG_EXT) |
| 255 | private = public; |
| 256 | else |
| 257 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 258 | if (private == NULL && !options.batch_mode) { |
| 259 | snprintf(buf, sizeof(buf), |
| 260 | "Enter passphrase for RSA key '%.100s': ", comment); |
| 261 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 262 | passphrase = read_passphrase(buf, 0); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 263 | if (strcmp(passphrase, "") != 0) { |
| 264 | private = key_load_private_type(KEY_RSA1, |
| 265 | authfile, passphrase, NULL); |
| 266 | quit = 0; |
| 267 | } else { |
| 268 | debug2("no passphrase given, try next key"); |
| 269 | quit = 1; |
| 270 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 271 | memset(passphrase, 0, strlen(passphrase)); |
| 272 | xfree(passphrase); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 273 | if (private != NULL || quit) |
| 274 | break; |
| 275 | debug2("bad passphrase given, try again..."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 276 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 277 | } |
| 278 | /* We no longer need the comment. */ |
| 279 | xfree(comment); |
| 280 | |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 281 | if (private == NULL) { |
| 282 | if (!options.batch_mode) |
| 283 | error("Bad passphrase."); |
| 284 | |
| 285 | /* Send a dummy response packet to avoid protocol error. */ |
| 286 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 287 | for (i = 0; i < 16; i++) |
| 288 | packet_put_char(0); |
| 289 | packet_send(); |
| 290 | packet_write_wait(); |
| 291 | |
| 292 | /* Expect the server to reject it... */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 293 | packet_read_expect(SSH_SMSG_FAILURE); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 294 | BN_clear_free(challenge); |
| 295 | return 0; |
| 296 | } |
| 297 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 298 | /* Compute and send a response to the challenge. */ |
| 299 | respond_to_rsa_challenge(challenge, private->rsa); |
| 300 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 301 | /* Destroy the private key unless it in external hardware. */ |
| 302 | if (!(private->flags & KEY_FLAG_EXT)) |
| 303 | key_free(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 304 | |
| 305 | /* We no longer need the challenge. */ |
| 306 | BN_clear_free(challenge); |
| 307 | |
| 308 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 309 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 310 | if (type == SSH_SMSG_SUCCESS) { |
| 311 | debug("RSA authentication accepted by server."); |
| 312 | return 1; |
| 313 | } |
| 314 | if (type != SSH_SMSG_FAILURE) |
| 315 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 316 | debug("RSA authentication refused."); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv |
| 322 | * authentication and RSA host authentication. |
| 323 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 324 | static int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 325 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 326 | { |
| 327 | int type; |
| 328 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 329 | |
| 330 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); |
| 331 | |
| 332 | /* Tell the server that we are willing to authenticate using this key. */ |
| 333 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 334 | packet_put_cstring(local_user); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 335 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
| 336 | packet_put_bignum(host_key->rsa->e); |
| 337 | packet_put_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 338 | packet_send(); |
| 339 | packet_write_wait(); |
| 340 | |
| 341 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 342 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 343 | |
| 344 | /* The server responds with failure if it doesn't admit our |
| 345 | .rhosts authentication or doesn't know our host key. */ |
| 346 | if (type == SSH_SMSG_FAILURE) { |
| 347 | debug("Server refused our rhosts authentication or host key."); |
| 348 | return 0; |
| 349 | } |
| 350 | /* Otherwise, the server should respond with a challenge. */ |
| 351 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 352 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 353 | |
| 354 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 355 | if ((challenge = BN_new()) == NULL) |
| 356 | fatal("try_rhosts_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 357 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 358 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 359 | |
| 360 | debug("Received RSA challenge for host key from server."); |
| 361 | |
| 362 | /* Compute a response to the challenge. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 363 | respond_to_rsa_challenge(challenge, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 364 | |
| 365 | /* We no longer need the challenge. */ |
| 366 | BN_clear_free(challenge); |
| 367 | |
| 368 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 369 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 370 | if (type == SSH_SMSG_SUCCESS) { |
| 371 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); |
| 372 | return 1; |
| 373 | } |
| 374 | if (type != SSH_SMSG_FAILURE) |
| 375 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 376 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | #ifdef KRB4 |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 381 | static int |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 382 | try_krb4_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 383 | { |
| 384 | KTEXT_ST auth; /* Kerberos data */ |
| 385 | char *reply; |
| 386 | char inst[INST_SZ]; |
| 387 | char *realm; |
| 388 | CREDENTIALS cred; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 389 | int r, type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 390 | socklen_t slen; |
| 391 | Key_schedule schedule; |
| 392 | u_long checksum, cksum; |
| 393 | MSG_DAT msg_data; |
| 394 | struct sockaddr_in local, foreign; |
| 395 | struct stat st; |
| 396 | |
| 397 | /* Don't do anything if we don't have any tickets. */ |
| 398 | if (stat(tkt_string(), &st) < 0) |
| 399 | return 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 400 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 401 | strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)), |
| 402 | INST_SZ); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 403 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 404 | realm = (char *)krb_realmofhost(get_canonical_hostname(1)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 405 | if (!realm) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 406 | debug("Kerberos v4: no realm for %s", get_canonical_hostname(1)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | /* This can really be anything. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 410 | checksum = (u_long)getpid(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 411 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 412 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); |
| 413 | if (r != KSUCCESS) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 414 | debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | /* Get session key to decrypt the server's reply with. */ |
| 418 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); |
| 419 | if (r != KSUCCESS) { |
| 420 | debug("get_cred failed: %s", krb_err_txt[r]); |
| 421 | return 0; |
| 422 | } |
| 423 | des_key_sched((des_cblock *) cred.session, schedule); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 424 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 425 | /* Send authentication info to server. */ |
| 426 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
| 427 | packet_put_string((char *) auth.dat, auth.length); |
| 428 | packet_send(); |
| 429 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 430 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 431 | /* Zero the buffer. */ |
| 432 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 433 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 434 | slen = sizeof(local); |
| 435 | memset(&local, 0, sizeof(local)); |
| 436 | if (getsockname(packet_get_connection_in(), |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 437 | (struct sockaddr *)&local, &slen) < 0) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 438 | debug("getsockname failed: %s", strerror(errno)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 439 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 440 | slen = sizeof(foreign); |
| 441 | memset(&foreign, 0, sizeof(foreign)); |
| 442 | if (getpeername(packet_get_connection_in(), |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 443 | (struct sockaddr *)&foreign, &slen) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 444 | debug("getpeername failed: %s", strerror(errno)); |
| 445 | fatal_cleanup(); |
| 446 | } |
| 447 | /* Get server reply. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 448 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 449 | switch (type) { |
| 450 | case SSH_SMSG_FAILURE: |
| 451 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 452 | debug("Kerberos v4 authentication failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 453 | return 0; |
| 454 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 455 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 456 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
| 457 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 458 | debug("Kerberos v4 authentication accepted."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 459 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 460 | /* Get server's response. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 461 | reply = packet_get_string((u_int *) &auth.length); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 462 | memcpy(auth.dat, reply, auth.length); |
| 463 | xfree(reply); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 464 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 465 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 466 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 467 | /* |
| 468 | * If his response isn't properly encrypted with the session |
| 469 | * key, and the decrypted checksum fails to match, he's |
| 470 | * bogus. Bail out. |
| 471 | */ |
| 472 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 473 | &foreign, &local, &msg_data); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 474 | if (r != KSUCCESS) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 475 | debug("Kerberos v4 krb_rd_priv failed: %s", |
| 476 | krb_err_txt[r]); |
| 477 | packet_disconnect("Kerberos v4 challenge failed!"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 478 | } |
| 479 | /* Fetch the (incremented) checksum that we supplied in the request. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 480 | memcpy((char *)&cksum, (char *)msg_data.app_data, |
| 481 | sizeof(cksum)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 482 | cksum = ntohl(cksum); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 483 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 484 | /* If it matches, we're golden. */ |
| 485 | if (cksum == checksum + 1) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 486 | debug("Kerberos v4 challenge successful."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 487 | return 1; |
| 488 | } else |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 489 | packet_disconnect("Kerberos v4 challenge failed!"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 490 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 491 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 492 | default: |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 493 | packet_disconnect("Protocol error on Kerberos v4 response: %d", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | #endif /* KRB4 */ |
| 499 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 500 | #ifdef KRB5 |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 501 | static int |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 502 | try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) |
| 503 | { |
| 504 | krb5_error_code problem; |
| 505 | const char *tkfile; |
| 506 | struct stat buf; |
| 507 | krb5_ccache ccache = NULL; |
| 508 | const char *remotehost; |
| 509 | krb5_data ap; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 510 | int type; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 511 | krb5_ap_rep_enc_part *reply = NULL; |
| 512 | int ret; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 513 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 514 | memset(&ap, 0, sizeof(ap)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 515 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 516 | problem = krb5_init_context(context); |
| 517 | if (problem) { |
| 518 | debug("Kerberos v5: krb5_init_context failed"); |
| 519 | ret = 0; |
| 520 | goto out; |
| 521 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 522 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 523 | tkfile = krb5_cc_default_name(*context); |
| 524 | if (strncmp(tkfile, "FILE:", 5) == 0) |
| 525 | tkfile += 5; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 526 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 527 | if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { |
| 528 | debug("Kerberos v5: could not get default ccache (permission denied)."); |
| 529 | ret = 0; |
| 530 | goto out; |
| 531 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 532 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 533 | problem = krb5_cc_default(*context, &ccache); |
| 534 | if (problem) { |
| 535 | debug("Kerberos v5: krb5_cc_default failed: %s", |
| 536 | krb5_get_err_text(*context, problem)); |
| 537 | ret = 0; |
| 538 | goto out; |
| 539 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 540 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 541 | remotehost = get_canonical_hostname(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 542 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 543 | problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, |
| 544 | "host", remotehost, NULL, ccache, &ap); |
| 545 | if (problem) { |
| 546 | debug("Kerberos v5: krb5_mk_req failed: %s", |
| 547 | krb5_get_err_text(*context, problem)); |
| 548 | ret = 0; |
| 549 | goto out; |
| 550 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 551 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 552 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
| 553 | packet_put_string((char *) ap.data, ap.length); |
| 554 | packet_send(); |
| 555 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 556 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 557 | xfree(ap.data); |
| 558 | ap.length = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 559 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 560 | type = packet_read(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 561 | switch (type) { |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 562 | case SSH_SMSG_FAILURE: |
| 563 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ |
| 564 | debug("Kerberos v5 authentication failed."); |
| 565 | ret = 0; |
| 566 | break; |
| 567 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 568 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 569 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
| 570 | debug("Kerberos v5 authentication accepted."); |
| 571 | |
| 572 | /* Get server's response. */ |
| 573 | ap.data = packet_get_string((unsigned int *) &ap.length); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 574 | packet_check_eom(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 575 | /* XXX je to dobre? */ |
| 576 | |
| 577 | problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); |
| 578 | if (problem) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 579 | ret = 0; |
| 580 | } |
| 581 | ret = 1; |
| 582 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 583 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 584 | default: |
| 585 | packet_disconnect("Protocol error on Kerberos v5 response: %d", |
| 586 | type); |
| 587 | ret = 0; |
| 588 | break; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 589 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 590 | } |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 591 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 592 | out: |
| 593 | if (ccache != NULL) |
| 594 | krb5_cc_close(*context, ccache); |
| 595 | if (reply != NULL) |
| 596 | krb5_free_ap_rep_enc_part(*context, reply); |
| 597 | if (ap.length > 0) |
| 598 | krb5_data_free(&ap); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 599 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 600 | return (ret); |
| 601 | } |
| 602 | |
| 603 | static void |
| 604 | send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) |
| 605 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 606 | int fd, type; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 607 | krb5_error_code problem; |
| 608 | krb5_data outbuf; |
| 609 | krb5_ccache ccache = NULL; |
| 610 | krb5_creds creds; |
| 611 | krb5_kdc_flags flags; |
| 612 | const char *remotehost; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 613 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 614 | memset(&creds, 0, sizeof(creds)); |
| 615 | memset(&outbuf, 0, sizeof(outbuf)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 616 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 617 | fd = packet_get_connection_in(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 618 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 619 | problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); |
| 620 | if (problem) |
| 621 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 622 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 623 | problem = krb5_cc_default(context, &ccache); |
| 624 | if (problem) |
| 625 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 626 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 627 | problem = krb5_cc_get_principal(context, ccache, &creds.client); |
| 628 | if (problem) |
| 629 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 630 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 631 | problem = krb5_build_principal(context, &creds.server, |
| 632 | strlen(creds.client->realm), creds.client->realm, |
| 633 | "krbtgt", creds.client->realm, NULL); |
| 634 | if (problem) |
| 635 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 636 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 637 | creds.times.endtime = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 638 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 639 | flags.i = 0; |
| 640 | flags.b.forwarded = 1; |
| 641 | flags.b.forwardable = krb5_config_get_bool(context, NULL, |
| 642 | "libdefaults", "forwardable", NULL); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 643 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 644 | remotehost = get_canonical_hostname(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 645 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 646 | problem = krb5_get_forwarded_creds(context, auth_context, |
| 647 | ccache, flags.i, remotehost, &creds, &outbuf); |
| 648 | if (problem) |
| 649 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 650 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 651 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
| 652 | packet_put_string((char *)outbuf.data, outbuf.length); |
| 653 | packet_send(); |
| 654 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 655 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 656 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 657 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 658 | if (type == SSH_SMSG_SUCCESS) { |
| 659 | char *pname; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 660 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 661 | krb5_unparse_name(context, creds.client, &pname); |
| 662 | debug("Kerberos v5 TGT forwarded (%s).", pname); |
| 663 | xfree(pname); |
| 664 | } else |
| 665 | debug("Kerberos v5 TGT forwarding failed."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 666 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 667 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 668 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 669 | out: |
| 670 | if (problem) |
| 671 | debug("Kerberos v5 TGT forwarding failed: %s", |
| 672 | krb5_get_err_text(context, problem)); |
| 673 | if (creds.client) |
| 674 | krb5_free_principal(context, creds.client); |
| 675 | if (creds.server) |
| 676 | krb5_free_principal(context, creds.server); |
| 677 | if (ccache) |
| 678 | krb5_cc_close(context, ccache); |
| 679 | if (outbuf.data) |
| 680 | xfree(outbuf.data); |
| 681 | } |
| 682 | #endif /* KRB5 */ |
| 683 | |
| 684 | #ifdef AFS |
| 685 | static void |
| 686 | send_krb4_tgt(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 687 | { |
| 688 | CREDENTIALS *creds; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 689 | struct stat st; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 690 | char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 691 | int problem, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 692 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 693 | /* Don't do anything if we don't have any tickets. */ |
| 694 | if (stat(tkt_string(), &st) < 0) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 695 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 696 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 697 | creds = xmalloc(sizeof(*creds)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 698 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 699 | problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm); |
| 700 | if (problem) |
| 701 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 702 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 703 | problem = krb_get_cred("krbtgt", prealm, prealm, creds); |
| 704 | if (problem) |
| 705 | goto out; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 706 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 707 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 708 | problem = RD_AP_EXP; |
| 709 | goto out; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 710 | } |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 711 | creds_to_radix(creds, (u_char *)buffer, sizeof(buffer)); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 712 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 713 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 714 | packet_put_cstring(buffer); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 715 | packet_send(); |
| 716 | packet_write_wait(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 717 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 718 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 719 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 720 | if (type == SSH_SMSG_SUCCESS) |
| 721 | debug("Kerberos v4 TGT forwarded (%s%s%s@%s).", |
| 722 | creds->pname, creds->pinst[0] ? "." : "", |
| 723 | creds->pinst, creds->realm); |
| 724 | else |
| 725 | debug("Kerberos v4 TGT rejected."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 726 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 727 | xfree(creds); |
| 728 | return; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 729 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 730 | out: |
| 731 | debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]); |
| 732 | xfree(creds); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 733 | } |
| 734 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 735 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 736 | send_afs_tokens(void) |
| 737 | { |
| 738 | CREDENTIALS creds; |
| 739 | struct ViceIoctl parms; |
| 740 | struct ClearToken ct; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 741 | int i, type, len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 742 | char buf[2048], *p, *server_cell; |
| 743 | char buffer[8192]; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 744 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 745 | /* Move over ktc_GetToken, here's something leaner. */ |
| 746 | for (i = 0; i < 100; i++) { /* just in case */ |
| 747 | parms.in = (char *) &i; |
| 748 | parms.in_size = sizeof(i); |
| 749 | parms.out = buf; |
| 750 | parms.out_size = sizeof(buf); |
| 751 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) |
| 752 | break; |
| 753 | p = buf; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 754 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 755 | /* Get secret token. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 756 | memcpy(&creds.ticket_st.length, p, sizeof(u_int)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 757 | if (creds.ticket_st.length > MAX_KTXT_LEN) |
| 758 | break; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 759 | p += sizeof(u_int); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 760 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); |
| 761 | p += creds.ticket_st.length; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 762 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 763 | /* Get clear token. */ |
| 764 | memcpy(&len, p, sizeof(len)); |
| 765 | if (len != sizeof(struct ClearToken)) |
| 766 | break; |
| 767 | p += sizeof(len); |
| 768 | memcpy(&ct, p, len); |
| 769 | p += len; |
| 770 | p += sizeof(len); /* primary flag */ |
| 771 | server_cell = p; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 772 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 773 | /* Flesh out our credentials. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 774 | strlcpy(creds.service, "afs", sizeof(creds.service)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 775 | creds.instance[0] = '\0'; |
| 776 | strlcpy(creds.realm, server_cell, REALM_SZ); |
| 777 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); |
| 778 | creds.issue_date = ct.BeginTimestamp; |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 779 | creds.lifetime = krb_time_to_life(creds.issue_date, |
| 780 | ct.EndTimestamp); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 781 | creds.kvno = ct.AuthHandle; |
| 782 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); |
| 783 | creds.pinst[0] = '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 784 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 785 | /* Encode token, ship it off. */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 786 | if (creds_to_radix(&creds, (u_char *)buffer, |
| 787 | sizeof(buffer)) <= 0) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 788 | break; |
| 789 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 790 | packet_put_cstring(buffer); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 791 | packet_send(); |
| 792 | packet_write_wait(); |
| 793 | |
| 794 | /* Roger, Roger. Clearance, Clarence. What's your vector, |
| 795 | Victor? */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 796 | type = packet_read(); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 797 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 798 | if (type == SSH_SMSG_FAILURE) |
| 799 | debug("AFS token for cell %s rejected.", server_cell); |
| 800 | else if (type != SSH_SMSG_SUCCESS) |
| 801 | packet_disconnect("Protocol error on AFS token response: %d", type); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | #endif /* AFS */ |
| 806 | |
| 807 | /* |
| 808 | * Tries to authenticate with any string-based challenge/response system. |
| 809 | * Note that the client code is not tied to s/key or TIS. |
| 810 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 811 | static int |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 812 | try_challenge_response_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 813 | { |
| 814 | int type, i; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 815 | u_int clen; |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 816 | char prompt[1024]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 817 | char *challenge, *response; |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 818 | |
| 819 | debug("Doing challenge response authentication."); |
| 820 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 821 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 822 | /* request a challenge */ |
| 823 | packet_start(SSH_CMSG_AUTH_TIS); |
| 824 | packet_send(); |
| 825 | packet_write_wait(); |
| 826 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 827 | type = packet_read(); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 828 | if (type != SSH_SMSG_FAILURE && |
| 829 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 830 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 831 | "to SSH_CMSG_AUTH_TIS", type); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 832 | } |
| 833 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 834 | debug("No challenge."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | challenge = packet_get_string(&clen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 838 | packet_check_eom(); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 839 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 840 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 841 | xfree(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 842 | if (i != 0) |
| 843 | error("Permission denied, please try again."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 844 | if (options.cipher == SSH_CIPHER_NONE) |
| 845 | log("WARNING: Encryption is disabled! " |
| 846 | "Reponse will be transmitted in clear text."); |
| 847 | response = read_passphrase(prompt, 0); |
| 848 | if (strcmp(response, "") == 0) { |
| 849 | xfree(response); |
| 850 | break; |
| 851 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 852 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 853 | ssh_put_password(response); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 854 | memset(response, 0, strlen(response)); |
| 855 | xfree(response); |
| 856 | packet_send(); |
| 857 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 858 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 859 | if (type == SSH_SMSG_SUCCESS) |
| 860 | return 1; |
| 861 | if (type != SSH_SMSG_FAILURE) |
| 862 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 863 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 864 | } |
| 865 | /* failure */ |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Tries to authenticate with plain passwd authentication. |
| 871 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 872 | static int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 873 | try_password_authentication(char *prompt) |
| 874 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 875 | int type, i; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 876 | char *password; |
| 877 | |
| 878 | debug("Doing password authentication."); |
| 879 | if (options.cipher == SSH_CIPHER_NONE) |
| 880 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
| 881 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 882 | if (i != 0) |
| 883 | error("Permission denied, please try again."); |
| 884 | password = read_passphrase(prompt, 0); |
| 885 | packet_start(SSH_CMSG_AUTH_PASSWORD); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 886 | ssh_put_password(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 887 | memset(password, 0, strlen(password)); |
| 888 | xfree(password); |
| 889 | packet_send(); |
| 890 | packet_write_wait(); |
| 891 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 892 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 893 | if (type == SSH_SMSG_SUCCESS) |
| 894 | return 1; |
| 895 | if (type != SSH_SMSG_FAILURE) |
| 896 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); |
| 897 | } |
| 898 | /* failure */ |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * SSH1 key exchange |
| 904 | */ |
| 905 | void |
| 906 | ssh_kex(char *host, struct sockaddr *hostaddr) |
| 907 | { |
| 908 | int i; |
| 909 | BIGNUM *key; |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 910 | Key *host_key, *server_key; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 911 | int bits, rbits; |
| 912 | int ssh_cipher_default = SSH_CIPHER_3DES; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 913 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
| 914 | u_char cookie[8]; |
| 915 | u_int supported_ciphers; |
| 916 | u_int server_flags, client_flags; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 917 | u_int32_t rand = 0; |
| 918 | |
| 919 | debug("Waiting for server public key."); |
| 920 | |
| 921 | /* Wait for a public key packet from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 922 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 923 | |
| 924 | /* Get cookie from the packet. */ |
| 925 | for (i = 0; i < 8; i++) |
| 926 | cookie[i] = packet_get_char(); |
| 927 | |
| 928 | /* Get the public key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 929 | server_key = key_new(KEY_RSA1); |
| 930 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 931 | packet_get_bignum(server_key->rsa->e); |
| 932 | packet_get_bignum(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 933 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 934 | rbits = BN_num_bits(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 935 | if (bits != rbits) { |
| 936 | log("Warning: Server lies about size of server public key: " |
| 937 | "actual size is %d bits vs. announced %d.", rbits, bits); |
| 938 | log("Warning: This may be due to an old implementation of ssh."); |
| 939 | } |
| 940 | /* Get the host key. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 941 | host_key = key_new(KEY_RSA1); |
| 942 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 943 | packet_get_bignum(host_key->rsa->e); |
| 944 | packet_get_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 945 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 946 | rbits = BN_num_bits(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 947 | if (bits != rbits) { |
| 948 | log("Warning: Server lies about size of server host key: " |
| 949 | "actual size is %d bits vs. announced %d.", rbits, bits); |
| 950 | log("Warning: This may be due to an old implementation of ssh."); |
| 951 | } |
| 952 | |
| 953 | /* Get protocol flags. */ |
| 954 | server_flags = packet_get_int(); |
| 955 | packet_set_protocol_flags(server_flags); |
| 956 | |
| 957 | supported_ciphers = packet_get_int(); |
| 958 | supported_authentications = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 959 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 960 | |
| 961 | debug("Received server public key (%d bits) and host key (%d bits).", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 962 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 963 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 964 | if (verify_host_key(host, hostaddr, host_key) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 965 | fatal("Host key verification failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 966 | |
| 967 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; |
| 968 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 969 | compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 970 | |
| 971 | /* Generate a session key. */ |
| 972 | arc4random_stir(); |
| 973 | |
| 974 | /* |
| 975 | * Generate an encryption key for the session. The key is a 256 bit |
| 976 | * random number, interpreted as a 32-byte key, with the least |
| 977 | * significant 8 bits being the first byte of the key. |
| 978 | */ |
| 979 | for (i = 0; i < 32; i++) { |
| 980 | if (i % 4 == 0) |
| 981 | rand = arc4random(); |
| 982 | session_key[i] = rand & 0xff; |
| 983 | rand >>= 8; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * According to the protocol spec, the first byte of the session key |
| 988 | * is the highest byte of the integer. The session key is xored with |
| 989 | * the first 16 bytes of the session id. |
| 990 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 991 | if ((key = BN_new()) == NULL) |
| 992 | fatal("respond_to_rsa_challenge: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 993 | BN_set_word(key, 0); |
| 994 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { |
| 995 | BN_lshift(key, key, 8); |
| 996 | if (i < 16) |
| 997 | BN_add_word(key, session_key[i] ^ session_id[i]); |
| 998 | else |
| 999 | BN_add_word(key, session_key[i]); |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * Encrypt the integer using the public key and host key of the |
| 1004 | * server (key with smaller modulus first). |
| 1005 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1006 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1007 | /* Public key has smaller modulus. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1008 | if (BN_num_bits(host_key->rsa->n) < |
| 1009 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 1010 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1011 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1012 | BN_num_bits(host_key->rsa->n), |
| 1013 | BN_num_bits(server_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1014 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1015 | } |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1016 | rsa_public_encrypt(key, key, server_key->rsa); |
| 1017 | rsa_public_encrypt(key, key, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1018 | } else { |
| 1019 | /* Host key has smaller modulus (or they are equal). */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1020 | if (BN_num_bits(server_key->rsa->n) < |
| 1021 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 1022 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1023 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1024 | BN_num_bits(server_key->rsa->n), |
| 1025 | BN_num_bits(host_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1026 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1027 | } |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1028 | rsa_public_encrypt(key, key, host_key->rsa); |
| 1029 | rsa_public_encrypt(key, key, server_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /* Destroy the public keys since we no longer need them. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 1033 | key_free(server_key); |
| 1034 | key_free(host_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1035 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 1036 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
| 1037 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) |
| 1038 | options.cipher = ssh_cipher_default; |
| 1039 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || |
| 1040 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1041 | log("No valid SSH1 cipher, using %.100s instead.", |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1042 | cipher_name(ssh_cipher_default)); |
| 1043 | options.cipher = ssh_cipher_default; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1044 | } |
| 1045 | /* Check that the selected cipher is supported. */ |
| 1046 | if (!(supported_ciphers & (1 << options.cipher))) |
| 1047 | fatal("Selected cipher type %.100s not supported by server.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1048 | cipher_name(options.cipher)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1049 | |
| 1050 | debug("Encryption type: %.100s", cipher_name(options.cipher)); |
| 1051 | |
| 1052 | /* Send the encrypted session key to the server. */ |
| 1053 | packet_start(SSH_CMSG_SESSION_KEY); |
| 1054 | packet_put_char(options.cipher); |
| 1055 | |
| 1056 | /* Send the cookie back to the server. */ |
| 1057 | for (i = 0; i < 8; i++) |
| 1058 | packet_put_char(cookie[i]); |
| 1059 | |
| 1060 | /* Send and destroy the encrypted encryption key integer. */ |
| 1061 | packet_put_bignum(key); |
| 1062 | BN_clear_free(key); |
| 1063 | |
| 1064 | /* Send protocol flags. */ |
| 1065 | packet_put_int(client_flags); |
| 1066 | |
| 1067 | /* Send the packet now. */ |
| 1068 | packet_send(); |
| 1069 | packet_write_wait(); |
| 1070 | |
| 1071 | debug("Sent encrypted session key."); |
| 1072 | |
| 1073 | /* Set the encryption key. */ |
| 1074 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); |
| 1075 | |
| 1076 | /* We will no longer need the session key here. Destroy any extra copies. */ |
| 1077 | memset(session_key, 0, sizeof(session_key)); |
| 1078 | |
| 1079 | /* |
| 1080 | * Expect a success message from the server. Note that this message |
| 1081 | * will be received in encrypted form. |
| 1082 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1083 | packet_read_expect(SSH_SMSG_SUCCESS); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1084 | |
| 1085 | debug("Received encrypted confirmation."); |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Authenticate user |
| 1090 | */ |
| 1091 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1092 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
| 1093 | Key **keys, int nkeys) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1094 | { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1095 | #ifdef KRB5 |
| 1096 | krb5_context context = NULL; |
| 1097 | krb5_auth_context auth_context = NULL; |
| 1098 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1099 | int i, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1100 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1101 | if (supported_authentications == 0) |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1102 | fatal("ssh_userauth1: server supports no auth methods"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1103 | |
| 1104 | /* Send the name of the user to log in as on the server. */ |
| 1105 | packet_start(SSH_CMSG_USER); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1106 | packet_put_cstring(server_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1107 | packet_send(); |
| 1108 | packet_write_wait(); |
| 1109 | |
| 1110 | /* |
| 1111 | * The server should respond with success if no authentication is |
| 1112 | * needed (the user has no password). Otherwise the server responds |
| 1113 | * with failure. |
| 1114 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1115 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1116 | |
| 1117 | /* check whether the connection was accepted without authentication. */ |
| 1118 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1119 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1120 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1121 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1122 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1123 | #ifdef KRB5 |
| 1124 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1125 | options.kerberos_authentication) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1126 | debug("Trying Kerberos v5 authentication."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1127 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1128 | if (try_krb5_authentication(&context, &auth_context)) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1129 | type = packet_read(); |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1130 | if (type == SSH_SMSG_SUCCESS) |
| 1131 | goto success; |
| 1132 | if (type != SSH_SMSG_FAILURE) |
| 1133 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); |
| 1134 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1135 | } |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1136 | #endif /* KRB5 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1137 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1138 | #ifdef KRB4 |
| 1139 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && |
| 1140 | options.kerberos_authentication) { |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1141 | debug("Trying Kerberos v4 authentication."); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1142 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1143 | if (try_krb4_authentication()) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1144 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1145 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1146 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1147 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1148 | packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | #endif /* KRB4 */ |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1152 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1153 | /* |
| 1154 | * Use rhosts authentication if running in privileged socket and we |
| 1155 | * do not wish to remain anonymous. |
| 1156 | */ |
| 1157 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && |
| 1158 | options.rhosts_authentication) { |
| 1159 | debug("Trying rhosts authentication."); |
| 1160 | packet_start(SSH_CMSG_AUTH_RHOSTS); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1161 | packet_put_cstring(local_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1162 | packet_send(); |
| 1163 | packet_write_wait(); |
| 1164 | |
| 1165 | /* The server should respond with success or failure. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 1166 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1167 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1168 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1169 | if (type != SSH_SMSG_FAILURE) |
| 1170 | packet_disconnect("Protocol error: got %d in response to rhosts auth", |
| 1171 | type); |
| 1172 | } |
| 1173 | /* |
| 1174 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host |
| 1175 | * authentication. |
| 1176 | */ |
| 1177 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1178 | options.rhosts_rsa_authentication) { |
| 1179 | for (i = 0; i < nkeys; i++) { |
Ben Lindstrom | 9cb59af | 2001-04-17 18:08:15 +0000 | [diff] [blame] | 1180 | if (keys[i] != NULL && keys[i]->type == KEY_RSA1 && |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1181 | try_rhosts_rsa_authentication(local_user, keys[i])) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1182 | goto success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1183 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1184 | } |
| 1185 | /* Try RSA authentication if the server supports it. */ |
| 1186 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && |
| 1187 | options.rsa_authentication) { |
| 1188 | /* |
| 1189 | * Try RSA authentication using the authentication agent. The |
| 1190 | * agent is tried first because no passphrase is needed for |
| 1191 | * it, whereas identity files may require passphrases. |
| 1192 | */ |
| 1193 | if (try_agent_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1194 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1195 | |
| 1196 | /* Try RSA authentication for each identity. */ |
| 1197 | for (i = 0; i < options.num_identity_files; i++) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1198 | if (options.identity_keys[i] != NULL && |
| 1199 | options.identity_keys[i]->type == KEY_RSA1 && |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 1200 | try_rsa_authentication(i)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1201 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1202 | } |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 1203 | /* Try challenge response authentication if the server supports it. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1204 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1205 | options.challenge_response_authentication && !options.batch_mode) { |
| 1206 | if (try_challenge_response_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1207 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1208 | } |
| 1209 | /* Try password authentication if the server supports it. */ |
| 1210 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && |
| 1211 | options.password_authentication && !options.batch_mode) { |
| 1212 | char prompt[80]; |
| 1213 | |
Ben Lindstrom | e055716 | 2001-02-11 00:00:24 +0000 | [diff] [blame] | 1214 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1215 | server_user, host); |
| 1216 | if (try_password_authentication(prompt)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1217 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1218 | } |
| 1219 | /* All authentication methods have failed. Exit with an error message. */ |
| 1220 | fatal("Permission denied."); |
| 1221 | /* NOTREACHED */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1222 | |
| 1223 | success: |
| 1224 | #ifdef KRB5 |
| 1225 | /* Try Kerberos v5 TGT passing. */ |
| 1226 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && |
| 1227 | options.kerberos_tgt_passing && context && auth_context) { |
| 1228 | if (options.cipher == SSH_CIPHER_NONE) |
| 1229 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
| 1230 | send_krb5_tgt(context, auth_context); |
| 1231 | } |
| 1232 | if (auth_context) |
| 1233 | krb5_auth_con_free(context, auth_context); |
| 1234 | if (context) |
| 1235 | krb5_free_context(context); |
| 1236 | #endif |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1237 | |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 1238 | #ifdef AFS |
| 1239 | /* Try Kerberos v4 TGT passing if the server supports it. */ |
| 1240 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && |
| 1241 | options.kerberos_tgt_passing) { |
| 1242 | if (options.cipher == SSH_CIPHER_NONE) |
| 1243 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
| 1244 | send_krb4_tgt(); |
| 1245 | } |
| 1246 | /* Try AFS token passing if the server supports it. */ |
| 1247 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && |
| 1248 | options.afs_token_passing && k_hasafs()) { |
| 1249 | if (options.cipher == SSH_CIPHER_NONE) |
| 1250 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); |
| 1251 | send_afs_tokens(); |
| 1252 | } |
| 1253 | #endif /* AFS */ |
Tim Rice | 024acc4 | 2001-07-04 21:27:20 -0700 | [diff] [blame] | 1254 | |
Damien Miller | 40eb1d8 | 2001-07-14 12:16:59 +1000 | [diff] [blame] | 1255 | return; /* need statement after label */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1256 | } |