blob: aee547fbee63475f7db81c2b3c8ddf3d4938fb5b [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Ben Lindstrom46c16222000-12-22 01:43:59 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.33 2000/12/20 19:37:22 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include <openssl/bn.h>
29#include <openssl/rsa.h>
30#include <openssl/dsa.h>
31#include <openssl/md5.h>
32#include <openssl/dh.h>
33#include <openssl/hmac.h>
34
35#include "ssh.h"
36#include "xmalloc.h"
37#include "rsa.h"
38#include "buffer.h"
39#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "uidswap.h"
41#include "compat.h"
42#include "readconf.h"
43#include "bufaux.h"
44#include "ssh2.h"
45#include "kex.h"
46#include "myproposal.h"
47#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "sshconnect.h"
49#include "authfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110050#include "cli.h"
Damien Miller62cee002000-09-23 17:15:56 +110051#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100052#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053
Damien Miller874d77b2000-10-14 16:23:11 +110054void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
55void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
56
Damien Millereba71ba2000-04-29 23:57:08 +100057/* import */
58extern char *client_version_string;
59extern char *server_version_string;
60extern Options options;
61
62/*
63 * SSH2 key exchange
64 */
65
Ben Lindstrom46c16222000-12-22 01:43:59 +000066u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100067int session_id2_len = 0;
68
69void
Damien Miller874d77b2000-10-14 16:23:11 +110070ssh_kex2(char *host, struct sockaddr *hostaddr)
71{
72 int i, plen;
73 Kex *kex;
74 Buffer *client_kexinit, *server_kexinit;
75 char *sprop[PROPOSAL_MAX];
76
Damien Millere39cacc2000-11-29 12:18:44 +110077 if (options.ciphers == (char *)-1) {
78 log("No valid ciphers for protocol version 2 given, using defaults.");
79 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110080 }
81 if (options.ciphers != NULL) {
82 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
83 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
84 }
85 if (options.compression) {
86 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib";
87 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
88 } else {
89 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
90 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
91 }
92
93 /* buffers with raw kexinit messages */
94 server_kexinit = xmalloc(sizeof(*server_kexinit));
95 buffer_init(server_kexinit);
96 client_kexinit = kex_init(myproposal);
97
98 /* algorithm negotiation */
99 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
100 kex = kex_choose_conf(myproposal, sprop, 0);
101 for (i = 0; i < PROPOSAL_MAX; i++)
102 xfree(sprop[i]);
103
104 /* server authentication and session key agreement */
105 switch(kex->kex_type) {
106 case DH_GRP1_SHA1:
107 ssh_dh1_client(kex, host, hostaddr,
108 client_kexinit, server_kexinit);
109 break;
110 case DH_GEX_SHA1:
111 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
112 server_kexinit);
113 break;
114 default:
115 fatal("Unsupported key exchange %d", kex->kex_type);
116 }
117
118 buffer_free(client_kexinit);
119 buffer_free(server_kexinit);
120 xfree(client_kexinit);
121 xfree(server_kexinit);
122
123 debug("Wait SSH2_MSG_NEWKEYS.");
124 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
125 packet_done();
126 debug("GOT SSH2_MSG_NEWKEYS.");
127
128 debug("send SSH2_MSG_NEWKEYS.");
129 packet_start(SSH2_MSG_NEWKEYS);
130 packet_send();
131 packet_write_wait();
132 debug("done: send SSH2_MSG_NEWKEYS.");
133
134#ifdef DEBUG_KEXDH
135 /* send 1st encrypted/maced/compressed message */
136 packet_start(SSH2_MSG_IGNORE);
137 packet_put_cstring("markus");
138 packet_send();
139 packet_write_wait();
140#endif
141 debug("done: KEX2.");
142}
143
144/* diffie-hellman-group1-sha1 */
145
146void
147ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
148 Buffer *client_kexinit, Buffer *server_kexinit)
Damien Millereba71ba2000-04-29 23:57:08 +1000149{
Damien Miller62cee002000-09-23 17:15:56 +1100150#ifdef DEBUG_KEXDH
151 int i;
152#endif
Damien Millerb1715dc2000-05-30 13:44:51 +1000153 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000154 u_int klen, kout;
Damien Millereba71ba2000-04-29 23:57:08 +1000155 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000156 u_int slen;
Damien Millereba71ba2000-04-29 23:57:08 +1000157 char *server_host_key_blob = NULL;
158 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000159 u_int sbloblen;
Damien Millereba71ba2000-04-29 23:57:08 +1000160 DH *dh;
161 BIGNUM *dh_server_pub = 0;
162 BIGNUM *shared_secret = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000163 u_char *kbuf;
164 u_char *hash;
Damien Millereba71ba2000-04-29 23:57:08 +1000165
Damien Millereba71ba2000-04-29 23:57:08 +1000166 debug("Sending SSH2_MSG_KEXDH_INIT.");
Damien Millereba71ba2000-04-29 23:57:08 +1000167 /* generate and send 'e', client DH public key */
168 dh = dh_new_group1();
Kevin Steves6b875862000-12-15 23:31:01 +0000169 dh_gen_key(dh);
Damien Millereba71ba2000-04-29 23:57:08 +1000170 packet_start(SSH2_MSG_KEXDH_INIT);
171 packet_put_bignum2(dh->pub_key);
172 packet_send();
173 packet_write_wait();
174
175#ifdef DEBUG_KEXDH
176 fprintf(stderr, "\np= ");
Damien Miller62cee002000-09-23 17:15:56 +1100177 BN_print_fp(stderr, dh->p);
Damien Millereba71ba2000-04-29 23:57:08 +1000178 fprintf(stderr, "\ng= ");
Damien Miller62cee002000-09-23 17:15:56 +1100179 BN_print_fp(stderr, dh->g);
Damien Millereba71ba2000-04-29 23:57:08 +1000180 fprintf(stderr, "\npub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100181 BN_print_fp(stderr, dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000182 fprintf(stderr, "\n");
183 DHparams_print_fp(stderr, dh);
184#endif
185
186 debug("Wait SSH2_MSG_KEXDH_REPLY.");
187
Damien Millerb1715dc2000-05-30 13:44:51 +1000188 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
Damien Millereba71ba2000-04-29 23:57:08 +1000189
190 debug("Got SSH2_MSG_KEXDH_REPLY.");
191
192 /* key, cert */
193 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100194 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000195 if (server_host_key == NULL)
196 fatal("cannot decode server_host_key_blob");
197
198 check_host_key(host, hostaddr, server_host_key,
Damien Miller874d77b2000-10-14 16:23:11 +1100199 options.user_hostfile2, options.system_hostfile2);
Damien Millereba71ba2000-04-29 23:57:08 +1000200
201 /* DH paramter f, server public DH key */
202 dh_server_pub = BN_new();
203 if (dh_server_pub == NULL)
204 fatal("dh_server_pub == NULL");
205 packet_get_bignum2(dh_server_pub, &dlen);
206
207#ifdef DEBUG_KEXDH
208 fprintf(stderr, "\ndh_server_pub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100209 BN_print_fp(stderr, dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000210 fprintf(stderr, "\n");
211 debug("bits %d", BN_num_bits(dh_server_pub));
212#endif
213
214 /* signed H */
215 signature = packet_get_string(&slen);
216 packet_done();
217
218 if (!dh_pub_is_valid(dh, dh_server_pub))
219 packet_disconnect("bad server public DH value");
220
221 klen = DH_size(dh);
222 kbuf = xmalloc(klen);
223 kout = DH_compute_key(kbuf, dh_server_pub, dh);
224#ifdef DEBUG_KEXDH
225 debug("shared secret: len %d/%d", klen, kout);
226 fprintf(stderr, "shared secret == ");
227 for (i = 0; i< kout; i++)
228 fprintf(stderr, "%02x", (kbuf[i])&0xff);
229 fprintf(stderr, "\n");
230#endif
231 shared_secret = BN_new();
232
233 BN_bin2bn(kbuf, kout, shared_secret);
234 memset(kbuf, 0, klen);
235 xfree(kbuf);
236
237 /* calc and verify H */
238 hash = kex_hash(
239 client_version_string,
240 server_version_string,
241 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
242 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
243 server_host_key_blob, sbloblen,
244 dh->pub_key,
245 dh_server_pub,
246 shared_secret
247 );
248 xfree(server_host_key_blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000249 DH_free(dh);
Damien Millereba71ba2000-04-29 23:57:08 +1000250#ifdef DEBUG_KEXDH
251 fprintf(stderr, "hash == ");
252 for (i = 0; i< 20; i++)
253 fprintf(stderr, "%02x", (hash[i])&0xff);
254 fprintf(stderr, "\n");
255#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000256 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100257 fatal("key_verify failed for server_host_key");
Damien Millereba71ba2000-04-29 23:57:08 +1000258 key_free(server_host_key);
259
260 kex_derive_keys(kex, hash, shared_secret);
261 packet_set_kex(kex);
262
Damien Millereba71ba2000-04-29 23:57:08 +1000263 /* save session id */
264 session_id2_len = 20;
265 session_id2 = xmalloc(session_id2_len);
266 memcpy(session_id2, hash, session_id2_len);
Damien Millerb1715dc2000-05-30 13:44:51 +1000267}
268
Damien Miller874d77b2000-10-14 16:23:11 +1100269/* diffie-hellman-group-exchange-sha1 */
270
271/*
272 * Estimates the group order for a Diffie-Hellman group that has an
273 * attack complexity approximately the same as O(2**bits). Estimate
274 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
275 */
276
277int
278dh_estimate(int bits)
Damien Millerb1715dc2000-05-30 13:44:51 +1000279{
Damien Miller874d77b2000-10-14 16:23:11 +1100280
281 if (bits < 64)
282 return (512); /* O(2**63) */
283 if (bits < 128)
284 return (1024); /* O(2**86) */
285 if (bits < 192)
286 return (2048); /* O(2**116) */
287 return (4096); /* O(2**156) */
288}
Damien Millerb1715dc2000-05-30 13:44:51 +1000289
Damien Miller874d77b2000-10-14 16:23:11 +1100290void
291ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
292 Buffer *client_kexinit, Buffer *server_kexinit)
293{
294#ifdef DEBUG_KEXDH
295 int i;
296#endif
297 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000298 u_int klen, kout;
Damien Miller874d77b2000-10-14 16:23:11 +1100299 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000300 u_int slen, nbits;
Damien Miller874d77b2000-10-14 16:23:11 +1100301 char *server_host_key_blob = NULL;
302 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000303 u_int sbloblen;
Damien Miller874d77b2000-10-14 16:23:11 +1100304 DH *dh;
305 BIGNUM *dh_server_pub = 0;
306 BIGNUM *shared_secret = 0;
307 BIGNUM *p = 0, *g = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000308 u_char *kbuf;
309 u_char *hash;
Damien Millerb1715dc2000-05-30 13:44:51 +1000310
Damien Miller874d77b2000-10-14 16:23:11 +1100311 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
Damien Millerb1715dc2000-05-30 13:44:51 +1000312
Damien Miller874d77b2000-10-14 16:23:11 +1100313 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
314 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
315 packet_put_int(nbits);
Damien Millereba71ba2000-04-29 23:57:08 +1000316 packet_send();
317 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000318
319#ifdef DEBUG_KEXDH
Damien Miller874d77b2000-10-14 16:23:11 +1100320 fprintf(stderr, "\nnbits = %d", nbits);
321#endif
322
323 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
324
325 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
326
327 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
328
329 if ((p = BN_new()) == NULL)
330 fatal("BN_new");
331 packet_get_bignum2(p, &dlen);
332 if ((g = BN_new()) == NULL)
333 fatal("BN_new");
334 packet_get_bignum2(g, &dlen);
335 if ((dh = dh_new_group(g, p)) == NULL)
336 fatal("dh_new_group");
337
Kevin Steves6b875862000-12-15 23:31:01 +0000338 dh_gen_key(dh);
339
Damien Miller874d77b2000-10-14 16:23:11 +1100340#ifdef DEBUG_KEXDH
341 fprintf(stderr, "\np= ");
342 BN_print_fp(stderr, dh->p);
343 fprintf(stderr, "\ng= ");
344 BN_print_fp(stderr, dh->g);
345 fprintf(stderr, "\npub= ");
346 BN_print_fp(stderr, dh->pub_key);
347 fprintf(stderr, "\n");
348 DHparams_print_fp(stderr, dh);
349#endif
350
351 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
352 /* generate and send 'e', client DH public key */
353 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
354 packet_put_bignum2(dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000355 packet_send();
356 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100357
358 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
359
360 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
361
362 debug("Got SSH2_MSG_KEXDH_REPLY.");
363
364 /* key, cert */
365 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100366 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Miller874d77b2000-10-14 16:23:11 +1100367 if (server_host_key == NULL)
368 fatal("cannot decode server_host_key_blob");
369
370 check_host_key(host, hostaddr, server_host_key,
371 options.user_hostfile2, options.system_hostfile2);
372
373 /* DH paramter f, server public DH key */
374 dh_server_pub = BN_new();
375 if (dh_server_pub == NULL)
376 fatal("dh_server_pub == NULL");
377 packet_get_bignum2(dh_server_pub, &dlen);
378
379#ifdef DEBUG_KEXDH
380 fprintf(stderr, "\ndh_server_pub= ");
381 BN_print_fp(stderr, dh_server_pub);
382 fprintf(stderr, "\n");
383 debug("bits %d", BN_num_bits(dh_server_pub));
Damien Millereba71ba2000-04-29 23:57:08 +1000384#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100385
386 /* signed H */
387 signature = packet_get_string(&slen);
388 packet_done();
389
390 if (!dh_pub_is_valid(dh, dh_server_pub))
391 packet_disconnect("bad server public DH value");
392
393 klen = DH_size(dh);
394 kbuf = xmalloc(klen);
395 kout = DH_compute_key(kbuf, dh_server_pub, dh);
396#ifdef DEBUG_KEXDH
397 debug("shared secret: len %d/%d", klen, kout);
398 fprintf(stderr, "shared secret == ");
399 for (i = 0; i< kout; i++)
400 fprintf(stderr, "%02x", (kbuf[i])&0xff);
401 fprintf(stderr, "\n");
402#endif
403 shared_secret = BN_new();
404
405 BN_bin2bn(kbuf, kout, shared_secret);
406 memset(kbuf, 0, klen);
407 xfree(kbuf);
408
409 /* calc and verify H */
410 hash = kex_hash_gex(
411 client_version_string,
412 server_version_string,
413 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
414 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
415 server_host_key_blob, sbloblen,
416 nbits, dh->p, dh->g,
417 dh->pub_key,
418 dh_server_pub,
419 shared_secret
420 );
421 xfree(server_host_key_blob);
422 DH_free(dh);
423#ifdef DEBUG_KEXDH
424 fprintf(stderr, "hash == ");
425 for (i = 0; i< 20; i++)
426 fprintf(stderr, "%02x", (hash[i])&0xff);
427 fprintf(stderr, "\n");
428#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000429 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100430 fatal("key_verify failed for server_host_key");
Damien Miller874d77b2000-10-14 16:23:11 +1100431 key_free(server_host_key);
432
433 kex_derive_keys(kex, hash, shared_secret);
434 packet_set_kex(kex);
435
436 /* save session id */
437 session_id2_len = 20;
438 session_id2 = xmalloc(session_id2_len);
439 memcpy(session_id2, hash, session_id2_len);
Damien Millereba71ba2000-04-29 23:57:08 +1000440}
Damien Millerb1715dc2000-05-30 13:44:51 +1000441
Damien Millereba71ba2000-04-29 23:57:08 +1000442/*
443 * Authenticate user
444 */
Damien Miller62cee002000-09-23 17:15:56 +1100445
446typedef struct Authctxt Authctxt;
447typedef struct Authmethod Authmethod;
448
449typedef int sign_cb_fn(
450 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000451 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100452
453struct Authctxt {
454 const char *server_user;
455 const char *host;
456 const char *service;
457 AuthenticationConnection *agent;
Damien Miller62cee002000-09-23 17:15:56 +1100458 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100459 int success;
Damien Miller62cee002000-09-23 17:15:56 +1100460};
461struct Authmethod {
462 char *name; /* string to compare against server's list */
463 int (*userauth)(Authctxt *authctxt);
464 int *enabled; /* flag in option struct that enables method */
465 int *batch_flag; /* flag in option struct that disables method */
466};
467
468void input_userauth_success(int type, int plen, void *ctxt);
469void input_userauth_failure(int type, int plen, void *ctxt);
470void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100471void input_userauth_info_req(int type, int plen, void *ctxt);
472
473int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100474int userauth_pubkey(Authctxt *authctxt);
475int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100476int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100477
Ben Lindstrom46c16222000-12-22 01:43:59 +0000478void authmethod_clear(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100479Authmethod *authmethod_get(char *authlist);
480Authmethod *authmethod_lookup(const char *name);
Damien Miller62cee002000-09-23 17:15:56 +1100481
482Authmethod authmethods[] = {
483 {"publickey",
484 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100486 NULL},
487 {"password",
488 userauth_passwd,
489 &options.password_authentication,
490 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100491 {"keyboard-interactive",
492 userauth_kbdint,
493 &options.kbd_interactive_authentication,
494 &options.batch_mode},
495 {"none",
496 userauth_none,
497 NULL,
498 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100499 {NULL, NULL, NULL, NULL}
500};
501
502void
503ssh_userauth2(const char *server_user, char *host)
504{
505 Authctxt authctxt;
506 int type;
507 int plen;
508
509 debug("send SSH2_MSG_SERVICE_REQUEST");
510 packet_start(SSH2_MSG_SERVICE_REQUEST);
511 packet_put_cstring("ssh-userauth");
512 packet_send();
513 packet_write_wait();
514 type = packet_read(&plen);
515 if (type != SSH2_MSG_SERVICE_ACCEPT) {
516 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
517 }
518 if (packet_remaining() > 0) {
519 char *reply = packet_get_string(&plen);
520 debug("service_accept: %s", reply);
521 xfree(reply);
522 packet_done();
523 } else {
524 debug("buggy server: service_accept w/o service");
525 }
526 packet_done();
527 debug("got SSH2_MSG_SERVICE_ACCEPT");
528
529 /* setup authentication context */
530 authctxt.agent = ssh_get_authentication_connection();
531 authctxt.server_user = server_user;
532 authctxt.host = host;
533 authctxt.service = "ssh-connection"; /* service name */
534 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100535 authctxt.method = authmethod_lookup("none");
536 if (authctxt.method == NULL)
537 fatal("ssh_userauth2: internal error: cannot send userauth none request");
538 authmethod_clear();
Damien Miller62cee002000-09-23 17:15:56 +1100539
540 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100541 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100542
543 dispatch_init(&input_userauth_error);
544 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
545 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
546 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
547
548 if (authctxt.agent != NULL)
549 ssh_close_authentication_connection(authctxt.agent);
550
Damien Miller874d77b2000-10-14 16:23:11 +1100551 debug("ssh-userauth2 successfull: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100552}
553void
554input_userauth_error(int type, int plen, void *ctxt)
555{
556 fatal("input_userauth_error: bad message during authentication");
557}
558void
559input_userauth_success(int type, int plen, void *ctxt)
560{
561 Authctxt *authctxt = ctxt;
562 if (authctxt == NULL)
563 fatal("input_userauth_success: no authentication context");
564 authctxt->success = 1; /* break out */
565}
566void
567input_userauth_failure(int type, int plen, void *ctxt)
568{
569 Authmethod *method = NULL;
570 Authctxt *authctxt = ctxt;
571 char *authlist = NULL;
572 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100573
574 if (authctxt == NULL)
575 fatal("input_userauth_failure: no authentication context");
576
Damien Miller874d77b2000-10-14 16:23:11 +1100577 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100578 partial = packet_get_char();
579 packet_done();
580
581 if (partial != 0)
582 debug("partial success");
583 debug("authentications that can continue: %s", authlist);
584
585 for (;;) {
Damien Miller62cee002000-09-23 17:15:56 +1100586 method = authmethod_get(authlist);
587 if (method == NULL)
588 fatal("Unable to find an authentication method");
Damien Miller874d77b2000-10-14 16:23:11 +1100589 authctxt->method = method;
Damien Miller62cee002000-09-23 17:15:56 +1100590 if (method->userauth(authctxt) != 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100591 debug2("we sent a %s packet, wait for reply", method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100592 break;
593 } else {
594 debug2("we did not send a packet, disable method");
595 method->enabled = NULL;
596 }
597 }
598 xfree(authlist);
599}
600
Damien Millereba71ba2000-04-29 23:57:08 +1000601int
Damien Miller874d77b2000-10-14 16:23:11 +1100602userauth_none(Authctxt *authctxt)
603{
604 /* initial userauth request */
605 packet_start(SSH2_MSG_USERAUTH_REQUEST);
606 packet_put_cstring(authctxt->server_user);
607 packet_put_cstring(authctxt->service);
608 packet_put_cstring(authctxt->method->name);
609 packet_send();
610 packet_write_wait();
611 return 1;
612}
613
614int
Damien Miller62cee002000-09-23 17:15:56 +1100615userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000616{
Damien Millere247cc42000-05-07 12:03:14 +1000617 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000618 char prompt[80];
619 char *password;
620
Damien Millerd3a18572000-06-07 19:55:44 +1000621 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000622 return 0;
623
Damien Millerd3a18572000-06-07 19:55:44 +1000624 if(attempt != 1)
625 error("Permission denied, please try again.");
626
Damien Millereba71ba2000-04-29 23:57:08 +1000627 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100628 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000629 password = read_passphrase(prompt, 0);
630 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100631 packet_put_cstring(authctxt->server_user);
632 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100633 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000634 packet_put_char(0);
635 packet_put_cstring(password);
636 memset(password, 0, strlen(password));
637 xfree(password);
638 packet_send();
639 packet_write_wait();
640 return 1;
641}
642
Damien Millerad833b32000-08-23 10:46:23 +1000643int
Damien Miller62cee002000-09-23 17:15:56 +1100644sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000645{
646 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000647 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000648 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000649 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000650 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100651 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000652
Ben Lindstromd121f612000-12-03 17:00:47 +0000653 debug3("sign_and_send_pubkey");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100654 if (key_to_blob(k, &blob, &bloblen) == 0) {
655 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000656 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100657 return 0;
658 }
Damien Millereba71ba2000-04-29 23:57:08 +1000659 /* data to be signed */
660 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100661 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000662 buffer_append(&b, session_id2, session_id2_len);
663 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100664 } else {
665 buffer_put_string(&b, session_id2, session_id2_len);
666 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000667 }
Damien Millereba71ba2000-04-29 23:57:08 +1000668 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100669 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000670 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000671 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000672 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100673 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000674 if (datafellows & SSH_BUG_PKAUTH) {
675 buffer_put_char(&b, have_sig);
676 } else {
677 buffer_put_cstring(&b, authctxt->method->name);
678 buffer_put_char(&b, have_sig);
679 buffer_put_cstring(&b, key_ssh_name(k));
680 }
Damien Millereba71ba2000-04-29 23:57:08 +1000681 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000682
683 /* generate signature */
Damien Miller62cee002000-09-23 17:15:56 +1100684 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000685 if (ret == -1) {
686 xfree(blob);
687 buffer_free(&b);
688 return 0;
689 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100690#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000691 buffer_dump(&b);
692#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000693 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000694 buffer_clear(&b);
695 buffer_append(&b, session_id2, session_id2_len);
696 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100697 buffer_put_cstring(&b, authctxt->server_user);
698 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100699 buffer_put_cstring(&b, authctxt->method->name);
700 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000701 if (!(datafellows & SSH_BUG_PKAUTH))
702 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000703 buffer_put_string(&b, blob, bloblen);
704 }
705 xfree(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000706 /* append signature */
707 buffer_put_string(&b, signature, slen);
708 xfree(signature);
709
710 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000711 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100712 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000713 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000714
715 /* put remaining data from buffer into packet */
716 packet_start(SSH2_MSG_USERAUTH_REQUEST);
717 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
718 buffer_free(&b);
719
720 /* send */
721 packet_send();
722 packet_write_wait();
Damien Millerad833b32000-08-23 10:46:23 +1000723
724 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000725}
726
Damien Miller62cee002000-09-23 17:15:56 +1100727/* sign callback */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000728int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
729 u_char *data, int datalen)
Damien Miller62cee002000-09-23 17:15:56 +1100730{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100731 return key_sign(key, sigp, lenp, data, datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100732}
733
Damien Miller994cf142000-07-21 10:19:44 +1000734int
Damien Miller62cee002000-09-23 17:15:56 +1100735userauth_pubkey_identity(Authctxt *authctxt, char *filename)
Damien Miller994cf142000-07-21 10:19:44 +1000736{
737 Key *k;
Damien Miller62cee002000-09-23 17:15:56 +1100738 int i, ret, try_next;
Damien Miller994cf142000-07-21 10:19:44 +1000739 struct stat st;
740
741 if (stat(filename, &st) != 0) {
742 debug("key does not exist: %s", filename);
743 return 0;
744 }
745 debug("try pubkey: %s", filename);
746
Damien Miller0bc1bd82000-11-13 22:57:25 +1100747 k = key_new(KEY_UNSPEC);
Damien Miller994cf142000-07-21 10:19:44 +1000748 if (!load_private_key(filename, "", k, NULL)) {
749 int success = 0;
750 char *passphrase;
751 char prompt[300];
752 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100753 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100754 for (i = 0; i < options.number_of_password_prompts; i++) {
755 passphrase = read_passphrase(prompt, 0);
756 if (strcmp(passphrase, "") != 0) {
757 success = load_private_key(filename, passphrase, k, NULL);
758 try_next = 0;
759 } else {
760 debug2("no passphrase given, try next key");
761 try_next = 1;
762 }
763 memset(passphrase, 0, strlen(passphrase));
764 xfree(passphrase);
765 if (success || try_next)
766 break;
767 debug2("bad passphrase given, try again...");
768 }
Damien Miller994cf142000-07-21 10:19:44 +1000769 if (!success) {
770 key_free(k);
771 return 0;
772 }
773 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100774 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
Damien Millerad833b32000-08-23 10:46:23 +1000775 key_free(k);
776 return ret;
777}
778
Damien Miller62cee002000-09-23 17:15:56 +1100779/* sign callback */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000780int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
781 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000782{
Damien Miller62cee002000-09-23 17:15:56 +1100783 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000784}
785
786int
Damien Miller62cee002000-09-23 17:15:56 +1100787userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000788{
789 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100790 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000791 char *comment;
792 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000793
794 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
796 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100797 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000798 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100799 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100800 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100801 debug2("userauth_pubkey_agent: no more keys");
802 } else {
803 debug("userauth_pubkey_agent: trying agent key %s", comment);
804 xfree(comment);
805 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
806 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100807 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100808 if (ret == 0)
809 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000810 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000811}
812
Damien Miller62cee002000-09-23 17:15:56 +1100813int
814userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000815{
Damien Miller62cee002000-09-23 17:15:56 +1100816 static int idx = 0;
817 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000818
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 if (authctxt->agent != NULL) {
820 do {
821 sent = userauth_pubkey_agent(authctxt);
822 } while(!sent && authctxt->agent->howmany > 0);
823 }
824 while (!sent && idx < options.num_identity_files) {
825 if (options.identity_files_type[idx] != KEY_RSA1)
826 sent = userauth_pubkey_identity(authctxt,
827 options.identity_files[idx]);
828 idx++;
829 }
Damien Miller62cee002000-09-23 17:15:56 +1100830 return sent;
831}
Damien Millereba71ba2000-04-29 23:57:08 +1000832
Damien Miller874d77b2000-10-14 16:23:11 +1100833/*
834 * Send userauth request message specifying keyboard-interactive method.
835 */
836int
837userauth_kbdint(Authctxt *authctxt)
838{
839 static int attempt = 0;
840
841 if (attempt++ >= options.number_of_password_prompts)
842 return 0;
843
844 debug2("userauth_kbdint");
845 packet_start(SSH2_MSG_USERAUTH_REQUEST);
846 packet_put_cstring(authctxt->server_user);
847 packet_put_cstring(authctxt->service);
848 packet_put_cstring(authctxt->method->name);
849 packet_put_cstring(""); /* lang */
850 packet_put_cstring(options.kbd_interactive_devices ?
851 options.kbd_interactive_devices : "");
852 packet_send();
853 packet_write_wait();
854
855 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
856 return 1;
857}
858
859/*
860 * parse SSH2_MSG_USERAUTH_INFO_REQUEST, prompt user and send
861 * SSH2_MSG_USERAUTH_INFO_RESPONSE
862 */
863void
864input_userauth_info_req(int type, int plen, void *ctxt)
865{
866 Authctxt *authctxt = ctxt;
867 char *name = NULL;
868 char *inst = NULL;
869 char *lang = NULL;
870 char *prompt = NULL;
871 char *response = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000872 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100873 int echo = 0;
874
875 debug2("input_userauth_info_req");
876
877 if (authctxt == NULL)
878 fatal("input_userauth_info_req: no authentication context");
879
880 name = packet_get_string(NULL);
881 inst = packet_get_string(NULL);
882 lang = packet_get_string(NULL);
883
884 if (strlen(name) > 0)
885 cli_mesg(name);
886 xfree(name);
887
888 if (strlen(inst) > 0)
889 cli_mesg(inst);
890 xfree(inst);
891 xfree(lang); /* unused */
892
893 num_prompts = packet_get_int();
894 /*
895 * Begin to build info response packet based on prompts requested.
896 * We commit to providing the correct number of responses, so if
897 * further on we run into a problem that prevents this, we have to
898 * be sure and clean this up and send a correct error response.
899 */
900 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
901 packet_put_int(num_prompts);
902
903 for (i = 0; i < num_prompts; i++) {
904 prompt = packet_get_string(NULL);
905 echo = packet_get_char();
906
907 response = cli_prompt(prompt, echo);
908
909 packet_put_cstring(response);
910 memset(response, 0, strlen(response));
911 xfree(response);
912 xfree(prompt);
913 }
914 packet_done(); /* done with parsing incoming message. */
915
916 packet_send();
917 packet_write_wait();
918}
Damien Miller62cee002000-09-23 17:15:56 +1100919
920/* find auth method */
921
922#define DELIM ","
923
924static char *def_authlist = "publickey,password";
925static char *authlist_current = NULL; /* clean copy used for comparison */
926static char *authname_current = NULL; /* last used auth method */
927static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
928static char *authlist_state = NULL; /* state variable for strtok_r() */
929
930/*
931 * Before starting to use a new authentication method list sent by the
932 * server, reset internal variables. This should also be called when
933 * finished processing server list to free resources.
934 */
935void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000936authmethod_clear(void)
Damien Miller62cee002000-09-23 17:15:56 +1100937{
938 if (authlist_current != NULL) {
939 xfree(authlist_current);
940 authlist_current = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000941 }
Damien Miller62cee002000-09-23 17:15:56 +1100942 if (authlist_working != NULL) {
943 xfree(authlist_working);
944 authlist_working = NULL;
945 }
946 if (authname_current != NULL) {
947 xfree(authname_current);
948 authlist_state = NULL;
949 }
950 if (authlist_state != NULL)
951 authlist_state = NULL;
952 return;
953}
954
955/*
956 * given auth method name, if configurable options permit this method fill
957 * in auth_ident field and return true, otherwise return false.
958 */
959int
960authmethod_is_enabled(Authmethod *method)
961{
962 if (method == NULL)
963 return 0;
964 /* return false if options indicate this method is disabled */
965 if (method->enabled == NULL || *method->enabled == 0)
966 return 0;
967 /* return false if batch mode is enabled but method needs interactive mode */
968 if (method->batch_flag != NULL && *method->batch_flag != 0)
969 return 0;
970 return 1;
971}
972
973Authmethod *
974authmethod_lookup(const char *name)
975{
976 Authmethod *method = NULL;
977 if (name != NULL)
978 for (method = authmethods; method->name != NULL; method++)
979 if (strcmp(name, method->name) == 0)
980 return method;
981 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
982 return NULL;
983}
984
985/*
986 * Given the authentication method list sent by the server, return the
987 * next method we should try. If the server initially sends a nil list,
988 * use a built-in default list. If the server sends a nil list after
989 * previously sending a valid list, continue using the list originally
990 * sent.
991 */
992
993Authmethod *
994authmethod_get(char *authlist)
995{
Damien Miller69b69aa2000-10-28 14:19:58 +1100996 char *name = NULL, *authname_old;
Damien Miller62cee002000-09-23 17:15:56 +1100997 Authmethod *method = NULL;
998
999 /* Use a suitable default if we're passed a nil list. */
1000 if (authlist == NULL || strlen(authlist) == 0)
1001 authlist = def_authlist;
1002
1003 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1004 /* start over if passed a different list */
Damien Miller874d77b2000-10-14 16:23:11 +11001005 debug3("start over, passed a different list");
Damien Miller62cee002000-09-23 17:15:56 +11001006 authmethod_clear();
1007 authlist_current = xstrdup(authlist);
1008 authlist_working = xstrdup(authlist);
1009 name = strtok_r(authlist_working, DELIM, &authlist_state);
Damien Millereba71ba2000-04-29 23:57:08 +10001010 } else {
Damien Miller62cee002000-09-23 17:15:56 +11001011 /*
1012 * try to use previously used authentication method
1013 * or continue to use previously passed list
1014 */
1015 name = (authname_current != NULL) ?
1016 authname_current : strtok_r(NULL, DELIM, &authlist_state);
Damien Millereba71ba2000-04-29 23:57:08 +10001017 }
Damien Millereba71ba2000-04-29 23:57:08 +10001018
Damien Miller62cee002000-09-23 17:15:56 +11001019 while (name != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +11001020 debug3("authmethod_lookup %s", name);
Damien Miller62cee002000-09-23 17:15:56 +11001021 method = authmethod_lookup(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001022 if (method != NULL && authmethod_is_enabled(method)) {
1023 debug3("authmethod_is_enabled %s", name);
Damien Millereba71ba2000-04-29 23:57:08 +10001024 break;
Damien Miller874d77b2000-10-14 16:23:11 +11001025 }
Damien Miller62cee002000-09-23 17:15:56 +11001026 name = strtok_r(NULL, DELIM, &authlist_state);
Damien Miller874d77b2000-10-14 16:23:11 +11001027 method = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001028 }
Damien Miller62cee002000-09-23 17:15:56 +11001029
Damien Miller69b69aa2000-10-28 14:19:58 +11001030 authname_old = authname_current;
Damien Miller874d77b2000-10-14 16:23:11 +11001031 if (method != NULL) {
Damien Miller62cee002000-09-23 17:15:56 +11001032 debug("next auth method to try is %s", name);
1033 authname_current = xstrdup(name);
Damien Miller62cee002000-09-23 17:15:56 +11001034 } else {
1035 debug("no more auth methods to try");
1036 authname_current = NULL;
Damien Miller62cee002000-09-23 17:15:56 +11001037 }
Damien Miller69b69aa2000-10-28 14:19:58 +11001038
1039 if (authname_old != NULL)
1040 xfree(authname_old);
1041
1042 return (method);
Damien Millereba71ba2000-04-29 23:57:08 +10001043}