blob: 491b4f67d1cac52acbb4d638f031f2df25ee1985 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
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 Millereba71ba2000-04-29 23:57:08 +10005 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
7 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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 Millereba71ba2000-04-29 23:57:08 +100013 */
14
15#include "includes.h"
Ben Lindstrome143f612002-08-20 18:41:15 +000016RCSID("$OpenBSD: sshconnect1.c,v 1.52 2002/08/08 13:50:23 aaron Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100017
18#include <openssl/bn.h>
Damien Miller2ce18da2002-02-13 13:54:27 +110019#include <openssl/md5.h>
Damien Millereba71ba2000-04-29 23:57:08 +100020
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#ifdef KRB4
22#include <krb.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +000024#ifdef KRB5
25#include <krb5.h>
Damien Millerfd4c9ee2002-04-13 11:04:40 +100026#ifndef HEIMDAL
27#define krb5_get_err_text(context,code) error_message(code)
28#endif /* !HEIMDAL */
Ben Lindstromec95ed92001-07-04 04:21:14 +000029#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#ifdef AFS
31#include <kafs.h>
Ben Lindstromb1985f72001-01-23 00:19:15 +000032#include "radix.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#endif
34
35#include "ssh.h"
36#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "xmalloc.h"
38#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "buffer.h"
40#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "mpaux.h"
42#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "readconf.h"
45#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100046#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "sshconnect.h"
48#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "readpass.h"
50#include "cipher.h"
51#include "canohost.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000052#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053
54/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000055u_char session_id[16];
56u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100057
58extern Options options;
59extern char *__progname;
60
61/*
62 * Checks if the user has an authentication agent, and if so, tries to
63 * authenticate using the agent.
64 */
Ben Lindstrombba81212001-06-25 05:01:22 +000065static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000066try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100067{
Damien Millerad833b32000-08-23 10:46:23 +100068 int type;
Damien Millereba71ba2000-04-29 23:57:08 +100069 char *comment;
70 AuthenticationConnection *auth;
Ben Lindstrom46c16222000-12-22 01:43:59 +000071 u_char response[16];
72 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +100073 Key *key;
74 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +100075
76 /* Get connection to the agent. */
77 auth = ssh_get_authentication_connection();
78 if (!auth)
79 return 0;
80
Damien Millerda755162002-01-22 23:09:22 +110081 if ((challenge = BN_new()) == NULL)
82 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100083 /* Loop through identities served by the agent. */
Damien Millerad833b32000-08-23 10:46:23 +100084 for (key = ssh_get_first_identity(auth, &comment, 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +110085 key != NULL;
86 key = ssh_get_next_identity(auth, &comment, 1)) {
Damien Millereba71ba2000-04-29 23:57:08 +100087
88 /* Try this identity. */
89 debug("Trying RSA authentication via agent with '%.100s'", comment);
90 xfree(comment);
91
92 /* Tell the server that we are willing to authenticate using this key. */
93 packet_start(SSH_CMSG_AUTH_RSA);
Damien Millerad833b32000-08-23 10:46:23 +100094 packet_put_bignum(key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +100095 packet_send();
96 packet_write_wait();
97
98 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +110099 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000100
101 /* The server sends failure if it doesn\'t like our key or
102 does not support RSA authentication. */
103 if (type == SSH_SMSG_FAILURE) {
104 debug("Server refused our key.");
Damien Millerad833b32000-08-23 10:46:23 +1000105 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000106 continue;
107 }
108 /* Otherwise it should have sent a challenge. */
109 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
110 packet_disconnect("Protocol error during RSA authentication: %d",
111 type);
112
Damien Millerd432ccf2002-01-22 23:14:44 +1100113 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100114 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000115
116 debug("Received RSA challenge from server.");
117
118 /* Ask the agent to decrypt the challenge. */
Damien Millerad833b32000-08-23 10:46:23 +1000119 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
120 /*
121 * The agent failed to authenticate this identifier
122 * although it advertised it supports this. Just
123 * return a wrong value.
124 */
Damien Miller996acd22003-04-09 20:59:48 +1000125 logit("Authentication agent failed to decrypt challenge.");
Damien Millereba71ba2000-04-29 23:57:08 +1000126 memset(response, 0, sizeof(response));
127 }
Damien Millerad833b32000-08-23 10:46:23 +1000128 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000129 debug("Sending response to RSA challenge.");
130
131 /* Send the decrypted challenge back to the server. */
132 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
133 for (i = 0; i < 16; i++)
134 packet_put_char(response[i]);
135 packet_send();
136 packet_write_wait();
137
138 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100139 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000140
141 /* The server returns success if it accepted the authentication. */
142 if (type == SSH_SMSG_SUCCESS) {
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000143 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000144 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000145 debug("RSA authentication accepted by server.");
Damien Millereba71ba2000-04-29 23:57:08 +1000146 return 1;
147 }
148 /* Otherwise it should return failure. */
149 if (type != SSH_SMSG_FAILURE)
150 packet_disconnect("Protocol error waiting RSA auth response: %d",
151 type);
152 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000153 ssh_close_authentication_connection(auth);
Damien Millereba71ba2000-04-29 23:57:08 +1000154 BN_clear_free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000155 debug("RSA authentication using agent refused.");
156 return 0;
157}
158
159/*
160 * Computes the proper response to a RSA challenge, and sends the response to
161 * the server.
162 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static void
Damien Millereba71ba2000-04-29 23:57:08 +1000164respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
165{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000166 u_char buf[32], response[16];
Damien Millereba71ba2000-04-29 23:57:08 +1000167 MD5_CTX md;
168 int i, len;
169
170 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100171 /* XXX think about Bleichenbacher, too */
172 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
173 packet_disconnect(
174 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000175
176 /* Compute the response. */
177 /* The response is MD5 of decrypted challenge plus session id. */
178 len = BN_num_bytes(challenge);
179 if (len <= 0 || len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100180 packet_disconnect(
181 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000182
183 memset(buf, 0, sizeof(buf));
184 BN_bn2bin(challenge, buf + sizeof(buf) - len);
185 MD5_Init(&md);
186 MD5_Update(&md, buf, 32);
187 MD5_Update(&md, session_id, 16);
188 MD5_Final(response, &md);
189
190 debug("Sending response to host key RSA challenge.");
191
192 /* Send the response back to the server. */
193 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
194 for (i = 0; i < 16; i++)
195 packet_put_char(response[i]);
196 packet_send();
197 packet_write_wait();
198
199 memset(buf, 0, sizeof(buf));
200 memset(response, 0, sizeof(response));
201 memset(&md, 0, sizeof(md));
202}
203
204/*
205 * Checks if the user has authentication file, and if so, tries to authenticate
206 * the user using it.
207 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000208static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000209try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000210{
211 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000212 Key *public, *private;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000213 char buf[300], *passphrase, *comment, *authfile;
Damien Millerdff50992002-01-22 23:16:32 +1100214 int i, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000215
Ben Lindstromc5b68002001-07-04 04:52:03 +0000216 public = options.identity_keys[idx];
217 authfile = options.identity_files[idx];
218 comment = xstrdup(authfile);
219
Damien Millereba71ba2000-04-29 23:57:08 +1000220 debug("Trying RSA authentication with key '%.100s'", comment);
221
222 /* Tell the server that we are willing to authenticate using this key. */
223 packet_start(SSH_CMSG_AUTH_RSA);
224 packet_put_bignum(public->rsa->n);
225 packet_send();
226 packet_write_wait();
227
Damien Millereba71ba2000-04-29 23:57:08 +1000228 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100229 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000230
231 /*
232 * The server responds with failure if it doesn\'t like our key or
233 * doesn\'t support RSA authentication.
234 */
235 if (type == SSH_SMSG_FAILURE) {
236 debug("Server refused our key.");
237 xfree(comment);
238 return 0;
239 }
240 /* Otherwise, the server should respond with a challenge. */
241 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
242 packet_disconnect("Protocol error during RSA authentication: %d", type);
243
244 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100245 if ((challenge = BN_new()) == NULL)
246 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100247 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100248 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000249
250 debug("Received RSA challenge from server.");
251
Damien Millereba71ba2000-04-29 23:57:08 +1000252 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000253 * If the key is not stored in external hardware, we have to
254 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000255 * fails, ask for a passphrase.
256 */
Ben Lindstrome143f612002-08-20 18:41:15 +0000257 if (public->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000258 private = public;
259 else
260 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000261 if (private == NULL && !options.batch_mode) {
262 snprintf(buf, sizeof(buf),
263 "Enter passphrase for RSA key '%.100s': ", comment);
264 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000265 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000266 if (strcmp(passphrase, "") != 0) {
267 private = key_load_private_type(KEY_RSA1,
268 authfile, passphrase, NULL);
269 quit = 0;
270 } else {
271 debug2("no passphrase given, try next key");
272 quit = 1;
273 }
Damien Millereba71ba2000-04-29 23:57:08 +1000274 memset(passphrase, 0, strlen(passphrase));
275 xfree(passphrase);
Ben Lindstrom05209452001-06-25 05:16:02 +0000276 if (private != NULL || quit)
277 break;
278 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000279 }
Damien Millereba71ba2000-04-29 23:57:08 +1000280 }
281 /* We no longer need the comment. */
282 xfree(comment);
283
Ben Lindstrom05209452001-06-25 05:16:02 +0000284 if (private == NULL) {
285 if (!options.batch_mode)
286 error("Bad passphrase.");
287
288 /* Send a dummy response packet to avoid protocol error. */
289 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
290 for (i = 0; i < 16; i++)
291 packet_put_char(0);
292 packet_send();
293 packet_write_wait();
294
295 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100296 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000297 BN_clear_free(challenge);
298 return 0;
299 }
300
Damien Millereba71ba2000-04-29 23:57:08 +1000301 /* Compute and send a response to the challenge. */
302 respond_to_rsa_challenge(challenge, private->rsa);
303
Ben Lindstromc5b68002001-07-04 04:52:03 +0000304 /* Destroy the private key unless it in external hardware. */
305 if (!(private->flags & KEY_FLAG_EXT))
306 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000307
308 /* We no longer need the challenge. */
309 BN_clear_free(challenge);
310
311 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100312 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000313 if (type == SSH_SMSG_SUCCESS) {
314 debug("RSA authentication accepted by server.");
315 return 1;
316 }
317 if (type != SSH_SMSG_FAILURE)
318 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
319 debug("RSA authentication refused.");
320 return 0;
321}
322
323/*
324 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
325 * authentication and RSA host authentication.
326 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000328try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000329{
330 int type;
331 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000332
333 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
334
335 /* Tell the server that we are willing to authenticate using this key. */
336 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000337 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000338 packet_put_int(BN_num_bits(host_key->rsa->n));
339 packet_put_bignum(host_key->rsa->e);
340 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000341 packet_send();
342 packet_write_wait();
343
344 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100345 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000346
347 /* The server responds with failure if it doesn't admit our
348 .rhosts authentication or doesn't know our host key. */
349 if (type == SSH_SMSG_FAILURE) {
350 debug("Server refused our rhosts authentication or host key.");
351 return 0;
352 }
353 /* Otherwise, the server should respond with a challenge. */
354 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
355 packet_disconnect("Protocol error during RSA authentication: %d", type);
356
357 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100358 if ((challenge = BN_new()) == NULL)
359 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100360 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100361 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000362
363 debug("Received RSA challenge for host key from server.");
364
365 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000366 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000367
368 /* We no longer need the challenge. */
369 BN_clear_free(challenge);
370
371 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100372 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000373 if (type == SSH_SMSG_SUCCESS) {
374 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
375 return 1;
376 }
377 if (type != SSH_SMSG_FAILURE)
378 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
379 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
380 return 0;
381}
382
383#ifdef KRB4
Ben Lindstrombba81212001-06-25 05:01:22 +0000384static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000385try_krb4_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000386{
387 KTEXT_ST auth; /* Kerberos data */
388 char *reply;
389 char inst[INST_SZ];
390 char *realm;
391 CREDENTIALS cred;
Damien Millerdff50992002-01-22 23:16:32 +1100392 int r, type;
Damien Millereba71ba2000-04-29 23:57:08 +1000393 socklen_t slen;
394 Key_schedule schedule;
395 u_long checksum, cksum;
396 MSG_DAT msg_data;
397 struct sockaddr_in local, foreign;
398 struct stat st;
399
400 /* Don't do anything if we don't have any tickets. */
401 if (stat(tkt_string(), &st) < 0)
402 return 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100403
Ben Lindstromec95ed92001-07-04 04:21:14 +0000404 strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
405 INST_SZ);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100406
Ben Lindstromec95ed92001-07-04 04:21:14 +0000407 realm = (char *)krb_realmofhost(get_canonical_hostname(1));
Damien Millereba71ba2000-04-29 23:57:08 +1000408 if (!realm) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000409 debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
Damien Millereba71ba2000-04-29 23:57:08 +1000410 return 0;
411 }
412 /* This can really be anything. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000413 checksum = (u_long)getpid();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100414
Damien Millereba71ba2000-04-29 23:57:08 +1000415 r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
416 if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000417 debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
Damien Millereba71ba2000-04-29 23:57:08 +1000418 return 0;
419 }
420 /* Get session key to decrypt the server's reply with. */
421 r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
422 if (r != KSUCCESS) {
423 debug("get_cred failed: %s", krb_err_txt[r]);
424 return 0;
425 }
426 des_key_sched((des_cblock *) cred.session, schedule);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100427
Damien Millereba71ba2000-04-29 23:57:08 +1000428 /* Send authentication info to server. */
429 packet_start(SSH_CMSG_AUTH_KERBEROS);
430 packet_put_string((char *) auth.dat, auth.length);
431 packet_send();
432 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100433
Damien Millereba71ba2000-04-29 23:57:08 +1000434 /* Zero the buffer. */
435 (void) memset(auth.dat, 0, MAX_KTXT_LEN);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100436
Damien Millereba71ba2000-04-29 23:57:08 +1000437 slen = sizeof(local);
438 memset(&local, 0, sizeof(local));
439 if (getsockname(packet_get_connection_in(),
Ben Lindstromec95ed92001-07-04 04:21:14 +0000440 (struct sockaddr *)&local, &slen) < 0)
Damien Millereba71ba2000-04-29 23:57:08 +1000441 debug("getsockname failed: %s", strerror(errno));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100442
Damien Millereba71ba2000-04-29 23:57:08 +1000443 slen = sizeof(foreign);
444 memset(&foreign, 0, sizeof(foreign));
445 if (getpeername(packet_get_connection_in(),
Ben Lindstromec95ed92001-07-04 04:21:14 +0000446 (struct sockaddr *)&foreign, &slen) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000447 debug("getpeername failed: %s", strerror(errno));
448 fatal_cleanup();
449 }
450 /* Get server reply. */
Damien Millerdff50992002-01-22 23:16:32 +1100451 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000452 switch (type) {
453 case SSH_SMSG_FAILURE:
454 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000455 debug("Kerberos v4 authentication failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000456 return 0;
457 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100458
Damien Millereba71ba2000-04-29 23:57:08 +1000459 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
460 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000461 debug("Kerberos v4 authentication accepted.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100462
Damien Millereba71ba2000-04-29 23:57:08 +1000463 /* Get server's response. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000464 reply = packet_get_string((u_int *) &auth.length);
Ben Lindstrom5c159582002-03-22 01:08:07 +0000465 if (auth.length >= MAX_KTXT_LEN)
466 fatal("Kerberos v4: Malformed response from server");
Damien Millereba71ba2000-04-29 23:57:08 +1000467 memcpy(auth.dat, reply, auth.length);
468 xfree(reply);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100469
Damien Miller48b03fc2002-01-22 23:11:40 +1100470 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100471
Damien Millereba71ba2000-04-29 23:57:08 +1000472 /*
473 * If his response isn't properly encrypted with the session
474 * key, and the decrypted checksum fails to match, he's
475 * bogus. Bail out.
476 */
477 r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
Ben Lindstromec95ed92001-07-04 04:21:14 +0000478 &foreign, &local, &msg_data);
Damien Millereba71ba2000-04-29 23:57:08 +1000479 if (r != KSUCCESS) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000480 debug("Kerberos v4 krb_rd_priv failed: %s",
481 krb_err_txt[r]);
482 packet_disconnect("Kerberos v4 challenge failed!");
Damien Millereba71ba2000-04-29 23:57:08 +1000483 }
484 /* Fetch the (incremented) checksum that we supplied in the request. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000485 memcpy((char *)&cksum, (char *)msg_data.app_data,
486 sizeof(cksum));
Damien Millereba71ba2000-04-29 23:57:08 +1000487 cksum = ntohl(cksum);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100488
Damien Millereba71ba2000-04-29 23:57:08 +1000489 /* If it matches, we're golden. */
490 if (cksum == checksum + 1) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000491 debug("Kerberos v4 challenge successful.");
Damien Millereba71ba2000-04-29 23:57:08 +1000492 return 1;
493 } else
Ben Lindstromec95ed92001-07-04 04:21:14 +0000494 packet_disconnect("Kerberos v4 challenge failed!");
Damien Millereba71ba2000-04-29 23:57:08 +1000495 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100496
Damien Millereba71ba2000-04-29 23:57:08 +1000497 default:
Ben Lindstromec95ed92001-07-04 04:21:14 +0000498 packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000499 }
500 return 0;
501}
502
503#endif /* KRB4 */
504
Ben Lindstromec95ed92001-07-04 04:21:14 +0000505#ifdef KRB5
Ben Lindstrombba81212001-06-25 05:01:22 +0000506static int
Ben Lindstromec95ed92001-07-04 04:21:14 +0000507try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
508{
509 krb5_error_code problem;
510 const char *tkfile;
511 struct stat buf;
512 krb5_ccache ccache = NULL;
513 const char *remotehost;
514 krb5_data ap;
Damien Millerdff50992002-01-22 23:16:32 +1100515 int type;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000516 krb5_ap_rep_enc_part *reply = NULL;
517 int ret;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100518
Ben Lindstromec95ed92001-07-04 04:21:14 +0000519 memset(&ap, 0, sizeof(ap));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100520
Ben Lindstromec95ed92001-07-04 04:21:14 +0000521 problem = krb5_init_context(context);
522 if (problem) {
523 debug("Kerberos v5: krb5_init_context failed");
524 ret = 0;
525 goto out;
526 }
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000527
528 problem = krb5_auth_con_init(*context, auth_context);
529 if (problem) {
530 debug("Kerberos v5: krb5_auth_con_init failed");
531 ret = 0;
532 goto out;
533 }
534
535#ifndef HEIMDAL
536 problem = krb5_auth_con_setflags(*context, *auth_context,
537 KRB5_AUTH_CONTEXT_RET_TIME);
538 if (problem) {
539 debug("Keberos v5: krb5_auth_con_setflags failed");
540 ret = 0;
541 goto out;
542 }
543#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100544
Ben Lindstromec95ed92001-07-04 04:21:14 +0000545 tkfile = krb5_cc_default_name(*context);
546 if (strncmp(tkfile, "FILE:", 5) == 0)
547 tkfile += 5;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100548
Ben Lindstromec95ed92001-07-04 04:21:14 +0000549 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
550 debug("Kerberos v5: could not get default ccache (permission denied).");
551 ret = 0;
552 goto out;
553 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100554
Ben Lindstromec95ed92001-07-04 04:21:14 +0000555 problem = krb5_cc_default(*context, &ccache);
556 if (problem) {
557 debug("Kerberos v5: krb5_cc_default failed: %s",
558 krb5_get_err_text(*context, problem));
559 ret = 0;
560 goto out;
561 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100562
Ben Lindstromec95ed92001-07-04 04:21:14 +0000563 remotehost = get_canonical_hostname(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100564
Ben Lindstromec95ed92001-07-04 04:21:14 +0000565 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
566 "host", remotehost, NULL, ccache, &ap);
567 if (problem) {
568 debug("Kerberos v5: krb5_mk_req failed: %s",
569 krb5_get_err_text(*context, problem));
570 ret = 0;
571 goto out;
572 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100573
Ben Lindstromec95ed92001-07-04 04:21:14 +0000574 packet_start(SSH_CMSG_AUTH_KERBEROS);
575 packet_put_string((char *) ap.data, ap.length);
576 packet_send();
577 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100578
Ben Lindstromec95ed92001-07-04 04:21:14 +0000579 xfree(ap.data);
580 ap.length = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100581
Damien Millerdff50992002-01-22 23:16:32 +1100582 type = packet_read();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000583 switch (type) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100584 case SSH_SMSG_FAILURE:
585 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
586 debug("Kerberos v5 authentication failed.");
587 ret = 0;
588 break;
589
Ben Lindstromec95ed92001-07-04 04:21:14 +0000590 case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
592 debug("Kerberos v5 authentication accepted.");
593
594 /* Get server's response. */
595 ap.data = packet_get_string((unsigned int *) &ap.length);
Damien Miller48b03fc2002-01-22 23:11:40 +1100596 packet_check_eom();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100597 /* XXX je to dobre? */
598
599 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
600 if (problem) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000601 ret = 0;
602 }
603 ret = 1;
604 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605
Ben Lindstromec95ed92001-07-04 04:21:14 +0000606 default:
607 packet_disconnect("Protocol error on Kerberos v5 response: %d",
608 type);
609 ret = 0;
610 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100611
Ben Lindstromec95ed92001-07-04 04:21:14 +0000612 }
Damien Miller9f0f5c62001-12-21 14:45:46 +1100613
Ben Lindstromec95ed92001-07-04 04:21:14 +0000614 out:
615 if (ccache != NULL)
616 krb5_cc_close(*context, ccache);
617 if (reply != NULL)
618 krb5_free_ap_rep_enc_part(*context, reply);
619 if (ap.length > 0)
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000620#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000621 krb5_data_free(&ap);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000622#else
623 krb5_free_data_contents(*context, &ap);
624#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +1100625
Ben Lindstromec95ed92001-07-04 04:21:14 +0000626 return (ret);
627}
628
629static void
630send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
631{
Damien Millerdff50992002-01-22 23:16:32 +1100632 int fd, type;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000633 krb5_error_code problem;
634 krb5_data outbuf;
635 krb5_ccache ccache = NULL;
636 krb5_creds creds;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000637#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000638 krb5_kdc_flags flags;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000639#else
640 int forwardable;
641#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000642 const char *remotehost;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100643
Ben Lindstromec95ed92001-07-04 04:21:14 +0000644 memset(&creds, 0, sizeof(creds));
645 memset(&outbuf, 0, sizeof(outbuf));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100646
Ben Lindstromec95ed92001-07-04 04:21:14 +0000647 fd = packet_get_connection_in();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100648
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000649#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000650 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000651#else
652 problem = krb5_auth_con_genaddrs(context, auth_context, fd,
653 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
654 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
655#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000656 if (problem)
657 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100658
Ben Lindstromec95ed92001-07-04 04:21:14 +0000659 problem = krb5_cc_default(context, &ccache);
660 if (problem)
661 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100662
Ben Lindstromec95ed92001-07-04 04:21:14 +0000663 problem = krb5_cc_get_principal(context, ccache, &creds.client);
664 if (problem)
665 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100666
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000667 remotehost = get_canonical_hostname(1);
668
669#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000670 problem = krb5_build_principal(context, &creds.server,
671 strlen(creds.client->realm), creds.client->realm,
672 "krbtgt", creds.client->realm, NULL);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000673#else
674 problem = krb5_build_principal(context, &creds.server,
675 creds.client->realm.length, creds.client->realm.data,
676 "host", remotehost, NULL);
677#endif
Ben Lindstromec95ed92001-07-04 04:21:14 +0000678 if (problem)
679 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100680
Ben Lindstromec95ed92001-07-04 04:21:14 +0000681 creds.times.endtime = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100682
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000683#ifdef HEIMDAL
Ben Lindstromec95ed92001-07-04 04:21:14 +0000684 flags.i = 0;
685 flags.b.forwarded = 1;
686 flags.b.forwardable = krb5_config_get_bool(context, NULL,
687 "libdefaults", "forwardable", NULL);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000688 problem = krb5_get_forwarded_creds(context, auth_context,
689 ccache, flags.i, remotehost, &creds, &outbuf);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000690#else
691 forwardable = 1;
692 problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
693 creds.client, creds.server, ccache, forwardable, &outbuf);
694#endif
695
Ben Lindstromec95ed92001-07-04 04:21:14 +0000696 if (problem)
697 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100698
Ben Lindstromec95ed92001-07-04 04:21:14 +0000699 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
700 packet_put_string((char *)outbuf.data, outbuf.length);
701 packet_send();
702 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100703
Damien Millerdff50992002-01-22 23:16:32 +1100704 type = packet_read();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100705
Ben Lindstromec95ed92001-07-04 04:21:14 +0000706 if (type == SSH_SMSG_SUCCESS) {
707 char *pname;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708
Ben Lindstromec95ed92001-07-04 04:21:14 +0000709 krb5_unparse_name(context, creds.client, &pname);
710 debug("Kerberos v5 TGT forwarded (%s).", pname);
711 xfree(pname);
712 } else
713 debug("Kerberos v5 TGT forwarding failed.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100714
Ben Lindstromec95ed92001-07-04 04:21:14 +0000715 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100716
Ben Lindstromec95ed92001-07-04 04:21:14 +0000717 out:
718 if (problem)
719 debug("Kerberos v5 TGT forwarding failed: %s",
720 krb5_get_err_text(context, problem));
721 if (creds.client)
722 krb5_free_principal(context, creds.client);
723 if (creds.server)
724 krb5_free_principal(context, creds.server);
725 if (ccache)
726 krb5_cc_close(context, ccache);
727 if (outbuf.data)
728 xfree(outbuf.data);
729}
730#endif /* KRB5 */
731
732#ifdef AFS
733static void
734send_krb4_tgt(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000735{
736 CREDENTIALS *creds;
Damien Millereba71ba2000-04-29 23:57:08 +1000737 struct stat st;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000738 char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
Damien Millerdff50992002-01-22 23:16:32 +1100739 int problem, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100740
Damien Millereba71ba2000-04-29 23:57:08 +1000741 /* Don't do anything if we don't have any tickets. */
742 if (stat(tkt_string(), &st) < 0)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000743 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100744
Damien Millereba71ba2000-04-29 23:57:08 +1000745 creds = xmalloc(sizeof(*creds));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100746
Ben Lindstromec95ed92001-07-04 04:21:14 +0000747 problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
748 if (problem)
749 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100750
Ben Lindstromec95ed92001-07-04 04:21:14 +0000751 problem = krb_get_cred("krbtgt", prealm, prealm, creds);
752 if (problem)
753 goto out;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100754
Damien Millereba71ba2000-04-29 23:57:08 +1000755 if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
Ben Lindstromec95ed92001-07-04 04:21:14 +0000756 problem = RD_AP_EXP;
757 goto out;
Damien Millereba71ba2000-04-29 23:57:08 +1000758 }
Ben Lindstromec95ed92001-07-04 04:21:14 +0000759 creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
Damien Miller9f0f5c62001-12-21 14:45:46 +1100760
Damien Millereba71ba2000-04-29 23:57:08 +1000761 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000762 packet_put_cstring(buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000763 packet_send();
764 packet_write_wait();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100765
Damien Millerdff50992002-01-22 23:16:32 +1100766 type = packet_read();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100767
Ben Lindstromec95ed92001-07-04 04:21:14 +0000768 if (type == SSH_SMSG_SUCCESS)
769 debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
770 creds->pname, creds->pinst[0] ? "." : "",
771 creds->pinst, creds->realm);
772 else
773 debug("Kerberos v4 TGT rejected.");
Damien Miller9f0f5c62001-12-21 14:45:46 +1100774
Ben Lindstromec95ed92001-07-04 04:21:14 +0000775 xfree(creds);
776 return;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100777
Ben Lindstromec95ed92001-07-04 04:21:14 +0000778 out:
779 debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
780 xfree(creds);
Damien Millereba71ba2000-04-29 23:57:08 +1000781}
782
Ben Lindstrombba81212001-06-25 05:01:22 +0000783static void
Damien Millereba71ba2000-04-29 23:57:08 +1000784send_afs_tokens(void)
785{
786 CREDENTIALS creds;
787 struct ViceIoctl parms;
788 struct ClearToken ct;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000789 int i, type, len;
Damien Millereba71ba2000-04-29 23:57:08 +1000790 char buf[2048], *p, *server_cell;
791 char buffer[8192];
Damien Miller9f0f5c62001-12-21 14:45:46 +1100792
Damien Millereba71ba2000-04-29 23:57:08 +1000793 /* Move over ktc_GetToken, here's something leaner. */
794 for (i = 0; i < 100; i++) { /* just in case */
795 parms.in = (char *) &i;
796 parms.in_size = sizeof(i);
797 parms.out = buf;
798 parms.out_size = sizeof(buf);
799 if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
800 break;
801 p = buf;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100802
Damien Millereba71ba2000-04-29 23:57:08 +1000803 /* Get secret token. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000804 memcpy(&creds.ticket_st.length, p, sizeof(u_int));
Damien Millereba71ba2000-04-29 23:57:08 +1000805 if (creds.ticket_st.length > MAX_KTXT_LEN)
806 break;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000807 p += sizeof(u_int);
Damien Millereba71ba2000-04-29 23:57:08 +1000808 memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
809 p += creds.ticket_st.length;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100810
Damien Millereba71ba2000-04-29 23:57:08 +1000811 /* Get clear token. */
812 memcpy(&len, p, sizeof(len));
813 if (len != sizeof(struct ClearToken))
814 break;
815 p += sizeof(len);
816 memcpy(&ct, p, len);
817 p += len;
818 p += sizeof(len); /* primary flag */
819 server_cell = p;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100820
Damien Millereba71ba2000-04-29 23:57:08 +1000821 /* Flesh out our credentials. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000822 strlcpy(creds.service, "afs", sizeof(creds.service));
Damien Millereba71ba2000-04-29 23:57:08 +1000823 creds.instance[0] = '\0';
824 strlcpy(creds.realm, server_cell, REALM_SZ);
825 memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
826 creds.issue_date = ct.BeginTimestamp;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000827 creds.lifetime = krb_time_to_life(creds.issue_date,
828 ct.EndTimestamp);
Damien Millereba71ba2000-04-29 23:57:08 +1000829 creds.kvno = ct.AuthHandle;
830 snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
831 creds.pinst[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100832
Damien Millereba71ba2000-04-29 23:57:08 +1000833 /* Encode token, ship it off. */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000834 if (creds_to_radix(&creds, (u_char *)buffer,
835 sizeof(buffer)) <= 0)
Damien Millereba71ba2000-04-29 23:57:08 +1000836 break;
837 packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000838 packet_put_cstring(buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000839 packet_send();
840 packet_write_wait();
841
842 /* Roger, Roger. Clearance, Clarence. What's your vector,
843 Victor? */
Damien Millerdff50992002-01-22 23:16:32 +1100844 type = packet_read();
Damien Miller9f0f5c62001-12-21 14:45:46 +1100845
Damien Millereba71ba2000-04-29 23:57:08 +1000846 if (type == SSH_SMSG_FAILURE)
847 debug("AFS token for cell %s rejected.", server_cell);
848 else if (type != SSH_SMSG_SUCCESS)
849 packet_disconnect("Protocol error on AFS token response: %d", type);
850 }
851}
852
853#endif /* AFS */
854
855/*
856 * Tries to authenticate with any string-based challenge/response system.
857 * Note that the client code is not tied to s/key or TIS.
858 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000860try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000861{
862 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000863 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000864 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000865 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000866
867 debug("Doing challenge response authentication.");
868
Damien Millereba71ba2000-04-29 23:57:08 +1000869 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000870 /* request a challenge */
871 packet_start(SSH_CMSG_AUTH_TIS);
872 packet_send();
873 packet_write_wait();
874
Damien Millerdff50992002-01-22 23:16:32 +1100875 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000876 if (type != SSH_SMSG_FAILURE &&
877 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
878 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000879 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000880 }
881 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000882 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000883 return 0;
884 }
885 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100886 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000887 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100888 strchr(challenge, '\n') ? "" : "\nResponse: ");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000889 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000890 if (i != 0)
891 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000892 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000893 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000894 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000895 response = read_passphrase(prompt, 0);
896 if (strcmp(response, "") == 0) {
897 xfree(response);
898 break;
899 }
Damien Millereba71ba2000-04-29 23:57:08 +1000900 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100901 ssh_put_password(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000902 memset(response, 0, strlen(response));
903 xfree(response);
904 packet_send();
905 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100906 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000907 if (type == SSH_SMSG_SUCCESS)
908 return 1;
909 if (type != SSH_SMSG_FAILURE)
910 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000911 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000912 }
913 /* failure */
914 return 0;
915}
916
917/*
918 * Tries to authenticate with plain passwd authentication.
919 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000920static int
Damien Millereba71ba2000-04-29 23:57:08 +1000921try_password_authentication(char *prompt)
922{
Damien Millerdff50992002-01-22 23:16:32 +1100923 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000924 char *password;
925
926 debug("Doing password authentication.");
927 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000928 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000929 for (i = 0; i < options.number_of_password_prompts; i++) {
930 if (i != 0)
931 error("Permission denied, please try again.");
932 password = read_passphrase(prompt, 0);
933 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100934 ssh_put_password(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000935 memset(password, 0, strlen(password));
936 xfree(password);
937 packet_send();
938 packet_write_wait();
939
Damien Millerdff50992002-01-22 23:16:32 +1100940 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000941 if (type == SSH_SMSG_SUCCESS)
942 return 1;
943 if (type != SSH_SMSG_FAILURE)
944 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
945 }
946 /* failure */
947 return 0;
948}
949
950/*
951 * SSH1 key exchange
952 */
953void
954ssh_kex(char *host, struct sockaddr *hostaddr)
955{
956 int i;
957 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100958 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000959 int bits, rbits;
960 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000961 u_char session_key[SSH_SESSION_KEY_LENGTH];
962 u_char cookie[8];
963 u_int supported_ciphers;
964 u_int server_flags, client_flags;
Damien Millereba71ba2000-04-29 23:57:08 +1000965 u_int32_t rand = 0;
966
967 debug("Waiting for server public key.");
968
969 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100970 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000971
972 /* Get cookie from the packet. */
973 for (i = 0; i < 8; i++)
974 cookie[i] = packet_get_char();
975
976 /* Get the public key. */
Damien Millerda755162002-01-22 23:09:22 +1100977 server_key = key_new(KEY_RSA1);
978 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100979 packet_get_bignum(server_key->rsa->e);
980 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000981
Damien Millerda755162002-01-22 23:09:22 +1100982 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000983 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000984 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000985 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000986 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000987 }
988 /* Get the host key. */
Damien Millerda755162002-01-22 23:09:22 +1100989 host_key = key_new(KEY_RSA1);
990 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100991 packet_get_bignum(host_key->rsa->e);
992 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000993
Damien Millerda755162002-01-22 23:09:22 +1100994 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000995 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000996 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000997 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000998 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000999 }
1000
1001 /* Get protocol flags. */
1002 server_flags = packet_get_int();
1003 packet_set_protocol_flags(server_flags);
1004
1005 supported_ciphers = packet_get_int();
1006 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001007 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +10001008
1009 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +11001010 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +10001011
Damien Millerda755162002-01-22 23:09:22 +11001012 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +10001013 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +10001014
1015 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
1016
Damien Millerda755162002-01-22 23:09:22 +11001017 compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +10001018
1019 /* Generate a session key. */
1020 arc4random_stir();
1021
1022 /*
1023 * Generate an encryption key for the session. The key is a 256 bit
1024 * random number, interpreted as a 32-byte key, with the least
1025 * significant 8 bits being the first byte of the key.
1026 */
1027 for (i = 0; i < 32; i++) {
1028 if (i % 4 == 0)
1029 rand = arc4random();
1030 session_key[i] = rand & 0xff;
1031 rand >>= 8;
1032 }
1033
1034 /*
1035 * According to the protocol spec, the first byte of the session key
1036 * is the highest byte of the integer. The session key is xored with
1037 * the first 16 bytes of the session id.
1038 */
Damien Millerda755162002-01-22 23:09:22 +11001039 if ((key = BN_new()) == NULL)
1040 fatal("respond_to_rsa_challenge: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +10001041 BN_set_word(key, 0);
1042 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1043 BN_lshift(key, key, 8);
1044 if (i < 16)
1045 BN_add_word(key, session_key[i] ^ session_id[i]);
1046 else
1047 BN_add_word(key, session_key[i]);
1048 }
1049
1050 /*
1051 * Encrypt the integer using the public key and host key of the
1052 * server (key with smaller modulus first).
1053 */
Damien Millerda755162002-01-22 23:09:22 +11001054 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +10001055 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +11001056 if (BN_num_bits(host_key->rsa->n) <
1057 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1058 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +11001059 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +11001060 BN_num_bits(host_key->rsa->n),
1061 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +11001062 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +10001063 }
Damien Millerda755162002-01-22 23:09:22 +11001064 rsa_public_encrypt(key, key, server_key->rsa);
1065 rsa_public_encrypt(key, key, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +10001066 } else {
1067 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +11001068 if (BN_num_bits(server_key->rsa->n) <
1069 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1070 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +11001071 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +11001072 BN_num_bits(server_key->rsa->n),
1073 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +11001074 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +10001075 }
Damien Millerda755162002-01-22 23:09:22 +11001076 rsa_public_encrypt(key, key, host_key->rsa);
1077 rsa_public_encrypt(key, key, server_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +10001078 }
1079
1080 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +11001081 key_free(server_key);
1082 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +10001083
Damien Millere39cacc2000-11-29 12:18:44 +11001084 if (options.cipher == SSH_CIPHER_NOT_SET) {
1085 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
1086 options.cipher = ssh_cipher_default;
1087 } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1088 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +10001089 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +11001090 cipher_name(ssh_cipher_default));
1091 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +10001092 }
1093 /* Check that the selected cipher is supported. */
1094 if (!(supported_ciphers & (1 << options.cipher)))
1095 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001096 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +10001097
1098 debug("Encryption type: %.100s", cipher_name(options.cipher));
1099
1100 /* Send the encrypted session key to the server. */
1101 packet_start(SSH_CMSG_SESSION_KEY);
1102 packet_put_char(options.cipher);
1103
1104 /* Send the cookie back to the server. */
1105 for (i = 0; i < 8; i++)
1106 packet_put_char(cookie[i]);
1107
1108 /* Send and destroy the encrypted encryption key integer. */
1109 packet_put_bignum(key);
1110 BN_clear_free(key);
1111
1112 /* Send protocol flags. */
1113 packet_put_int(client_flags);
1114
1115 /* Send the packet now. */
1116 packet_send();
1117 packet_write_wait();
1118
1119 debug("Sent encrypted session key.");
1120
1121 /* Set the encryption key. */
1122 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
1123
1124 /* We will no longer need the session key here. Destroy any extra copies. */
1125 memset(session_key, 0, sizeof(session_key));
1126
1127 /*
1128 * Expect a success message from the server. Note that this message
1129 * will be received in encrypted form.
1130 */
Damien Millerdff50992002-01-22 23:16:32 +11001131 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +10001132
1133 debug("Received encrypted confirmation.");
1134}
1135
1136/*
1137 * Authenticate user
1138 */
1139void
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001140ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001141 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +10001142{
Ben Lindstromec95ed92001-07-04 04:21:14 +00001143#ifdef KRB5
1144 krb5_context context = NULL;
1145 krb5_auth_context auth_context = NULL;
1146#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001147 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001148
Damien Millereba71ba2000-04-29 23:57:08 +10001149 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001150 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +10001151
1152 /* Send the name of the user to log in as on the server. */
1153 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001154 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +10001155 packet_send();
1156 packet_write_wait();
1157
1158 /*
1159 * The server should respond with success if no authentication is
1160 * needed (the user has no password). Otherwise the server responds
1161 * with failure.
1162 */
Damien Millerdff50992002-01-22 23:16:32 +11001163 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +10001164
1165 /* check whether the connection was accepted without authentication. */
1166 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001167 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001168 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001169 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001170
Ben Lindstromec95ed92001-07-04 04:21:14 +00001171#ifdef KRB5
1172 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
Damien Miller9f0f5c62001-12-21 14:45:46 +11001173 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +00001174 debug("Trying Kerberos v5 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001175
Ben Lindstromec95ed92001-07-04 04:21:14 +00001176 if (try_krb5_authentication(&context, &auth_context)) {
Damien Millerdff50992002-01-22 23:16:32 +11001177 type = packet_read();
Ben Lindstromec95ed92001-07-04 04:21:14 +00001178 if (type == SSH_SMSG_SUCCESS)
1179 goto success;
1180 if (type != SSH_SMSG_FAILURE)
1181 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
1182 }
Damien Millereba71ba2000-04-29 23:57:08 +10001183 }
Ben Lindstromec95ed92001-07-04 04:21:14 +00001184#endif /* KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +11001185
Damien Millereba71ba2000-04-29 23:57:08 +10001186#ifdef KRB4
1187 if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1188 options.kerberos_authentication) {
Ben Lindstromec95ed92001-07-04 04:21:14 +00001189 debug("Trying Kerberos v4 authentication.");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001190
Ben Lindstromec95ed92001-07-04 04:21:14 +00001191 if (try_krb4_authentication()) {
Damien Millerdff50992002-01-22 23:16:32 +11001192 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +10001193 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001194 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001195 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001196 packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
Damien Millereba71ba2000-04-29 23:57:08 +10001197 }
1198 }
1199#endif /* KRB4 */
Damien Miller9f0f5c62001-12-21 14:45:46 +11001200
Damien Millereba71ba2000-04-29 23:57:08 +10001201 /*
1202 * Use rhosts authentication if running in privileged socket and we
1203 * do not wish to remain anonymous.
1204 */
1205 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
1206 options.rhosts_authentication) {
1207 debug("Trying rhosts authentication.");
1208 packet_start(SSH_CMSG_AUTH_RHOSTS);
Ben Lindstrom664408d2001-06-09 01:42:01 +00001209 packet_put_cstring(local_user);
Damien Millereba71ba2000-04-29 23:57:08 +10001210 packet_send();
1211 packet_write_wait();
1212
1213 /* The server should respond with success or failure. */
Damien Millerdff50992002-01-22 23:16:32 +11001214 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +10001215 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +00001216 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001217 if (type != SSH_SMSG_FAILURE)
1218 packet_disconnect("Protocol error: got %d in response to rhosts auth",
1219 type);
1220 }
1221 /*
1222 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
1223 * authentication.
1224 */
1225 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001226 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001227 for (i = 0; i < sensitive->nkeys; i++) {
1228 if (sensitive->keys[i] != NULL &&
1229 sensitive->keys[i]->type == KEY_RSA1 &&
1230 try_rhosts_rsa_authentication(local_user,
1231 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001232 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001233 }
Damien Millereba71ba2000-04-29 23:57:08 +10001234 }
1235 /* Try RSA authentication if the server supports it. */
1236 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
1237 options.rsa_authentication) {
1238 /*
1239 * Try RSA authentication using the authentication agent. The
1240 * agent is tried first because no passphrase is needed for
1241 * it, whereas identity files may require passphrases.
1242 */
1243 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +00001244 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001245
1246 /* Try RSA authentication for each identity. */
1247 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001248 if (options.identity_keys[i] != NULL &&
1249 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +00001250 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001251 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001252 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +00001253 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +10001254 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +00001255 options.challenge_response_authentication && !options.batch_mode) {
1256 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +00001257 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001258 }
1259 /* Try password authentication if the server supports it. */
1260 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
1261 options.password_authentication && !options.batch_mode) {
1262 char prompt[80];
1263
Ben Lindstrome0557162001-02-11 00:00:24 +00001264 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +10001265 server_user, host);
1266 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +00001267 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +10001268 }
1269 /* All authentication methods have failed. Exit with an error message. */
1270 fatal("Permission denied.");
1271 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +00001272
1273 success:
1274#ifdef KRB5
1275 /* Try Kerberos v5 TGT passing. */
1276 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1277 options.kerberos_tgt_passing && context && auth_context) {
1278 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +10001279 logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
Ben Lindstromec95ed92001-07-04 04:21:14 +00001280 send_krb5_tgt(context, auth_context);
1281 }
1282 if (auth_context)
1283 krb5_auth_con_free(context, auth_context);
1284 if (context)
1285 krb5_free_context(context);
1286#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +11001287
Ben Lindstromec95ed92001-07-04 04:21:14 +00001288#ifdef AFS
1289 /* Try Kerberos v4 TGT passing if the server supports it. */
1290 if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1291 options.kerberos_tgt_passing) {
1292 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +10001293 logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
Ben Lindstromec95ed92001-07-04 04:21:14 +00001294 send_krb4_tgt();
1295 }
1296 /* Try AFS token passing if the server supports it. */
1297 if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
1298 options.afs_token_passing && k_hasafs()) {
1299 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +10001300 logit("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
Ben Lindstromec95ed92001-07-04 04:21:14 +00001301 send_afs_tokens();
1302 }
1303#endif /* AFS */
Tim Rice024acc42001-07-04 21:27:20 -07001304
Damien Miller40eb1d82001-07-14 12:16:59 +10001305 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +10001306}