blob: 6f41b987a12bb299a8ecf147dfbd5a90880f6826 [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 Lindstrom226cfa02001-01-22 05:34:40 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.37 2001/01/21 19:06:00 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100029#include <openssl/md5.h>
30#include <openssl/dh.h>
31#include <openssl/hmac.h>
32
33#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "xmalloc.h"
36#include "rsa.h"
37#include "buffer.h"
38#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "uidswap.h"
40#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "kex.h"
44#include "myproposal.h"
45#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "sshconnect.h"
47#include "authfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110048#include "cli.h"
Damien Miller62cee002000-09-23 17:15:56 +110049#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100050#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054
Damien Miller874d77b2000-10-14 16:23:11 +110055void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
56void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
57
Damien Millereba71ba2000-04-29 23:57:08 +100058/* import */
59extern char *client_version_string;
60extern char *server_version_string;
61extern Options options;
62
63/*
64 * SSH2 key exchange
65 */
66
Ben Lindstrom46c16222000-12-22 01:43:59 +000067u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100068int session_id2_len = 0;
69
70void
Damien Miller874d77b2000-10-14 16:23:11 +110071ssh_kex2(char *host, struct sockaddr *hostaddr)
72{
73 int i, plen;
74 Kex *kex;
75 Buffer *client_kexinit, *server_kexinit;
76 char *sprop[PROPOSAL_MAX];
77
Damien Millere39cacc2000-11-29 12:18:44 +110078 if (options.ciphers == (char *)-1) {
79 log("No valid ciphers for protocol version 2 given, using defaults.");
80 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110081 }
82 if (options.ciphers != NULL) {
83 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
84 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
85 }
86 if (options.compression) {
87 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib";
88 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
89 } else {
90 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
91 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
92 }
93
94 /* buffers with raw kexinit messages */
95 server_kexinit = xmalloc(sizeof(*server_kexinit));
96 buffer_init(server_kexinit);
97 client_kexinit = kex_init(myproposal);
98
99 /* algorithm negotiation */
100 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
101 kex = kex_choose_conf(myproposal, sprop, 0);
102 for (i = 0; i < PROPOSAL_MAX; i++)
103 xfree(sprop[i]);
104
105 /* server authentication and session key agreement */
106 switch(kex->kex_type) {
107 case DH_GRP1_SHA1:
108 ssh_dh1_client(kex, host, hostaddr,
109 client_kexinit, server_kexinit);
110 break;
111 case DH_GEX_SHA1:
112 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
113 server_kexinit);
114 break;
115 default:
116 fatal("Unsupported key exchange %d", kex->kex_type);
117 }
118
119 buffer_free(client_kexinit);
120 buffer_free(server_kexinit);
121 xfree(client_kexinit);
122 xfree(server_kexinit);
123
124 debug("Wait SSH2_MSG_NEWKEYS.");
125 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
126 packet_done();
127 debug("GOT SSH2_MSG_NEWKEYS.");
128
129 debug("send SSH2_MSG_NEWKEYS.");
130 packet_start(SSH2_MSG_NEWKEYS);
131 packet_send();
132 packet_write_wait();
133 debug("done: send SSH2_MSG_NEWKEYS.");
134
135#ifdef DEBUG_KEXDH
136 /* send 1st encrypted/maced/compressed message */
137 packet_start(SSH2_MSG_IGNORE);
138 packet_put_cstring("markus");
139 packet_send();
140 packet_write_wait();
141#endif
142 debug("done: KEX2.");
143}
144
145/* diffie-hellman-group1-sha1 */
146
147void
148ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
149 Buffer *client_kexinit, Buffer *server_kexinit)
Damien Millereba71ba2000-04-29 23:57:08 +1000150{
Damien Miller62cee002000-09-23 17:15:56 +1100151#ifdef DEBUG_KEXDH
152 int i;
153#endif
Damien Millerb1715dc2000-05-30 13:44:51 +1000154 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000155 u_int klen, kout;
Damien Millereba71ba2000-04-29 23:57:08 +1000156 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000157 u_int slen;
Damien Millereba71ba2000-04-29 23:57:08 +1000158 char *server_host_key_blob = NULL;
159 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000160 u_int sbloblen;
Damien Millereba71ba2000-04-29 23:57:08 +1000161 DH *dh;
162 BIGNUM *dh_server_pub = 0;
163 BIGNUM *shared_secret = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000164 u_char *kbuf;
165 u_char *hash;
Damien Millereba71ba2000-04-29 23:57:08 +1000166
Damien Millereba71ba2000-04-29 23:57:08 +1000167 debug("Sending SSH2_MSG_KEXDH_INIT.");
Damien Millereba71ba2000-04-29 23:57:08 +1000168 /* generate and send 'e', client DH public key */
169 dh = dh_new_group1();
Kevin Steves6b875862000-12-15 23:31:01 +0000170 dh_gen_key(dh);
Damien Millereba71ba2000-04-29 23:57:08 +1000171 packet_start(SSH2_MSG_KEXDH_INIT);
172 packet_put_bignum2(dh->pub_key);
173 packet_send();
174 packet_write_wait();
175
176#ifdef DEBUG_KEXDH
177 fprintf(stderr, "\np= ");
Damien Miller62cee002000-09-23 17:15:56 +1100178 BN_print_fp(stderr, dh->p);
Damien Millereba71ba2000-04-29 23:57:08 +1000179 fprintf(stderr, "\ng= ");
Damien Miller62cee002000-09-23 17:15:56 +1100180 BN_print_fp(stderr, dh->g);
Damien Millereba71ba2000-04-29 23:57:08 +1000181 fprintf(stderr, "\npub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100182 BN_print_fp(stderr, dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000183 fprintf(stderr, "\n");
184 DHparams_print_fp(stderr, dh);
185#endif
186
187 debug("Wait SSH2_MSG_KEXDH_REPLY.");
188
Damien Millerb1715dc2000-05-30 13:44:51 +1000189 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
Damien Millereba71ba2000-04-29 23:57:08 +1000190
191 debug("Got SSH2_MSG_KEXDH_REPLY.");
192
193 /* key, cert */
194 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100195 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000196 if (server_host_key == NULL)
197 fatal("cannot decode server_host_key_blob");
198
199 check_host_key(host, hostaddr, server_host_key,
Damien Miller874d77b2000-10-14 16:23:11 +1100200 options.user_hostfile2, options.system_hostfile2);
Damien Millereba71ba2000-04-29 23:57:08 +1000201
202 /* DH paramter f, server public DH key */
203 dh_server_pub = BN_new();
204 if (dh_server_pub == NULL)
205 fatal("dh_server_pub == NULL");
206 packet_get_bignum2(dh_server_pub, &dlen);
207
208#ifdef DEBUG_KEXDH
209 fprintf(stderr, "\ndh_server_pub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100210 BN_print_fp(stderr, dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000211 fprintf(stderr, "\n");
212 debug("bits %d", BN_num_bits(dh_server_pub));
213#endif
214
215 /* signed H */
216 signature = packet_get_string(&slen);
217 packet_done();
218
219 if (!dh_pub_is_valid(dh, dh_server_pub))
220 packet_disconnect("bad server public DH value");
221
222 klen = DH_size(dh);
223 kbuf = xmalloc(klen);
224 kout = DH_compute_key(kbuf, dh_server_pub, dh);
225#ifdef DEBUG_KEXDH
226 debug("shared secret: len %d/%d", klen, kout);
227 fprintf(stderr, "shared secret == ");
228 for (i = 0; i< kout; i++)
229 fprintf(stderr, "%02x", (kbuf[i])&0xff);
230 fprintf(stderr, "\n");
231#endif
232 shared_secret = BN_new();
233
234 BN_bin2bn(kbuf, kout, shared_secret);
235 memset(kbuf, 0, klen);
236 xfree(kbuf);
237
238 /* calc and verify H */
239 hash = kex_hash(
240 client_version_string,
241 server_version_string,
242 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
243 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
244 server_host_key_blob, sbloblen,
245 dh->pub_key,
246 dh_server_pub,
247 shared_secret
248 );
249 xfree(server_host_key_blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000250 DH_free(dh);
Damien Millereba71ba2000-04-29 23:57:08 +1000251#ifdef DEBUG_KEXDH
252 fprintf(stderr, "hash == ");
253 for (i = 0; i< 20; i++)
254 fprintf(stderr, "%02x", (hash[i])&0xff);
255 fprintf(stderr, "\n");
256#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000257 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100258 fatal("key_verify failed for server_host_key");
Damien Millereba71ba2000-04-29 23:57:08 +1000259 key_free(server_host_key);
260
261 kex_derive_keys(kex, hash, shared_secret);
262 packet_set_kex(kex);
263
Damien Millereba71ba2000-04-29 23:57:08 +1000264 /* save session id */
265 session_id2_len = 20;
266 session_id2 = xmalloc(session_id2_len);
267 memcpy(session_id2, hash, session_id2_len);
Damien Millerb1715dc2000-05-30 13:44:51 +1000268}
269
Damien Miller874d77b2000-10-14 16:23:11 +1100270/* diffie-hellman-group-exchange-sha1 */
271
272/*
273 * Estimates the group order for a Diffie-Hellman group that has an
274 * attack complexity approximately the same as O(2**bits). Estimate
275 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
276 */
277
278int
279dh_estimate(int bits)
Damien Millerb1715dc2000-05-30 13:44:51 +1000280{
Damien Miller874d77b2000-10-14 16:23:11 +1100281
282 if (bits < 64)
283 return (512); /* O(2**63) */
284 if (bits < 128)
285 return (1024); /* O(2**86) */
286 if (bits < 192)
287 return (2048); /* O(2**116) */
288 return (4096); /* O(2**156) */
289}
Damien Millerb1715dc2000-05-30 13:44:51 +1000290
Damien Miller874d77b2000-10-14 16:23:11 +1100291void
292ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
293 Buffer *client_kexinit, Buffer *server_kexinit)
294{
295#ifdef DEBUG_KEXDH
296 int i;
297#endif
298 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000299 u_int klen, kout;
Damien Miller874d77b2000-10-14 16:23:11 +1100300 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000301 u_int slen, nbits;
Damien Miller874d77b2000-10-14 16:23:11 +1100302 char *server_host_key_blob = NULL;
303 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000304 u_int sbloblen;
Damien Miller874d77b2000-10-14 16:23:11 +1100305 DH *dh;
306 BIGNUM *dh_server_pub = 0;
307 BIGNUM *shared_secret = 0;
308 BIGNUM *p = 0, *g = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000309 u_char *kbuf;
310 u_char *hash;
Damien Millerb1715dc2000-05-30 13:44:51 +1000311
Damien Miller874d77b2000-10-14 16:23:11 +1100312 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
Damien Millerb1715dc2000-05-30 13:44:51 +1000313
Damien Miller874d77b2000-10-14 16:23:11 +1100314 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
315 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
316 packet_put_int(nbits);
Damien Millereba71ba2000-04-29 23:57:08 +1000317 packet_send();
318 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000319
320#ifdef DEBUG_KEXDH
Damien Miller874d77b2000-10-14 16:23:11 +1100321 fprintf(stderr, "\nnbits = %d", nbits);
322#endif
323
324 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
325
326 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
327
328 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
329
330 if ((p = BN_new()) == NULL)
331 fatal("BN_new");
332 packet_get_bignum2(p, &dlen);
333 if ((g = BN_new()) == NULL)
334 fatal("BN_new");
335 packet_get_bignum2(g, &dlen);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000336 dh = dh_new_group(g, p);
Damien Miller874d77b2000-10-14 16:23:11 +1100337
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);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000470void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100471void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100472void input_userauth_info_req(int type, int plen, void *ctxt);
473
474int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100475int userauth_pubkey(Authctxt *authctxt);
476int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100477int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100478
Ben Lindstrom46c16222000-12-22 01:43:59 +0000479void authmethod_clear(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100480Authmethod *authmethod_get(char *authlist);
481Authmethod *authmethod_lookup(const char *name);
Damien Miller62cee002000-09-23 17:15:56 +1100482
483Authmethod authmethods[] = {
484 {"publickey",
485 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100486 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100487 NULL},
488 {"password",
489 userauth_passwd,
490 &options.password_authentication,
491 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100492 {"keyboard-interactive",
493 userauth_kbdint,
494 &options.kbd_interactive_authentication,
495 &options.batch_mode},
496 {"none",
497 userauth_none,
498 NULL,
499 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100500 {NULL, NULL, NULL, NULL}
501};
502
503void
504ssh_userauth2(const char *server_user, char *host)
505{
506 Authctxt authctxt;
507 int type;
508 int plen;
509
510 debug("send SSH2_MSG_SERVICE_REQUEST");
511 packet_start(SSH2_MSG_SERVICE_REQUEST);
512 packet_put_cstring("ssh-userauth");
513 packet_send();
514 packet_write_wait();
515 type = packet_read(&plen);
516 if (type != SSH2_MSG_SERVICE_ACCEPT) {
517 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
518 }
519 if (packet_remaining() > 0) {
520 char *reply = packet_get_string(&plen);
521 debug("service_accept: %s", reply);
522 xfree(reply);
523 packet_done();
524 } else {
525 debug("buggy server: service_accept w/o service");
526 }
527 packet_done();
528 debug("got SSH2_MSG_SERVICE_ACCEPT");
529
530 /* setup authentication context */
531 authctxt.agent = ssh_get_authentication_connection();
532 authctxt.server_user = server_user;
533 authctxt.host = host;
534 authctxt.service = "ssh-connection"; /* service name */
535 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100536 authctxt.method = authmethod_lookup("none");
537 if (authctxt.method == NULL)
538 fatal("ssh_userauth2: internal error: cannot send userauth none request");
539 authmethod_clear();
Damien Miller62cee002000-09-23 17:15:56 +1100540
541 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100542 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100543
544 dispatch_init(&input_userauth_error);
545 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
546 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000547 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100548 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
549
550 if (authctxt.agent != NULL)
551 ssh_close_authentication_connection(authctxt.agent);
552
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000553 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100554}
555void
556input_userauth_error(int type, int plen, void *ctxt)
557{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000558 fatal("input_userauth_error: bad message during authentication: "
559 "type %d", type);
560}
561void
562input_userauth_banner(int type, int plen, void *ctxt)
563{
564 char *msg, *lang;
565 debug3("input_userauth_banner");
566 msg = packet_get_string(NULL);
567 lang = packet_get_string(NULL);
568 fprintf(stderr, "%s", msg);
569 xfree(msg);
570 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100571}
572void
573input_userauth_success(int type, int plen, void *ctxt)
574{
575 Authctxt *authctxt = ctxt;
576 if (authctxt == NULL)
577 fatal("input_userauth_success: no authentication context");
578 authctxt->success = 1; /* break out */
579}
580void
581input_userauth_failure(int type, int plen, void *ctxt)
582{
583 Authmethod *method = NULL;
584 Authctxt *authctxt = ctxt;
585 char *authlist = NULL;
586 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100587
588 if (authctxt == NULL)
589 fatal("input_userauth_failure: no authentication context");
590
Damien Miller874d77b2000-10-14 16:23:11 +1100591 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100592 partial = packet_get_char();
593 packet_done();
594
595 if (partial != 0)
596 debug("partial success");
597 debug("authentications that can continue: %s", authlist);
598
599 for (;;) {
Damien Miller62cee002000-09-23 17:15:56 +1100600 method = authmethod_get(authlist);
601 if (method == NULL)
602 fatal("Unable to find an authentication method");
Damien Miller874d77b2000-10-14 16:23:11 +1100603 authctxt->method = method;
Damien Miller62cee002000-09-23 17:15:56 +1100604 if (method->userauth(authctxt) != 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100605 debug2("we sent a %s packet, wait for reply", method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100606 break;
607 } else {
608 debug2("we did not send a packet, disable method");
609 method->enabled = NULL;
610 }
611 }
612 xfree(authlist);
613}
614
Damien Millereba71ba2000-04-29 23:57:08 +1000615int
Damien Miller874d77b2000-10-14 16:23:11 +1100616userauth_none(Authctxt *authctxt)
617{
618 /* initial userauth request */
619 packet_start(SSH2_MSG_USERAUTH_REQUEST);
620 packet_put_cstring(authctxt->server_user);
621 packet_put_cstring(authctxt->service);
622 packet_put_cstring(authctxt->method->name);
623 packet_send();
624 packet_write_wait();
625 return 1;
626}
627
628int
Damien Miller62cee002000-09-23 17:15:56 +1100629userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000630{
Damien Millere247cc42000-05-07 12:03:14 +1000631 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000632 char prompt[80];
633 char *password;
634
Damien Millerd3a18572000-06-07 19:55:44 +1000635 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000636 return 0;
637
Damien Millerd3a18572000-06-07 19:55:44 +1000638 if(attempt != 1)
639 error("Permission denied, please try again.");
640
Damien Millereba71ba2000-04-29 23:57:08 +1000641 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100642 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000643 password = read_passphrase(prompt, 0);
644 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100645 packet_put_cstring(authctxt->server_user);
646 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100647 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000648 packet_put_char(0);
649 packet_put_cstring(password);
650 memset(password, 0, strlen(password));
651 xfree(password);
652 packet_send();
653 packet_write_wait();
654 return 1;
655}
656
Damien Millerad833b32000-08-23 10:46:23 +1000657int
Damien Miller62cee002000-09-23 17:15:56 +1100658sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000659{
660 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000661 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000662 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000663 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000664 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100665 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000666
Ben Lindstromd121f612000-12-03 17:00:47 +0000667 debug3("sign_and_send_pubkey");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100668 if (key_to_blob(k, &blob, &bloblen) == 0) {
669 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000670 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100671 return 0;
672 }
Damien Millereba71ba2000-04-29 23:57:08 +1000673 /* data to be signed */
674 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100675 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000676 buffer_append(&b, session_id2, session_id2_len);
677 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100678 } else {
679 buffer_put_string(&b, session_id2, session_id2_len);
680 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000681 }
Damien Millereba71ba2000-04-29 23:57:08 +1000682 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100683 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000684 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000685 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000686 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100687 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000688 if (datafellows & SSH_BUG_PKAUTH) {
689 buffer_put_char(&b, have_sig);
690 } else {
691 buffer_put_cstring(&b, authctxt->method->name);
692 buffer_put_char(&b, have_sig);
693 buffer_put_cstring(&b, key_ssh_name(k));
694 }
Damien Millereba71ba2000-04-29 23:57:08 +1000695 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000696
697 /* generate signature */
Damien Miller62cee002000-09-23 17:15:56 +1100698 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000699 if (ret == -1) {
700 xfree(blob);
701 buffer_free(&b);
702 return 0;
703 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100704#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000705 buffer_dump(&b);
706#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000707 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000708 buffer_clear(&b);
709 buffer_append(&b, session_id2, session_id2_len);
710 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100711 buffer_put_cstring(&b, authctxt->server_user);
712 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100713 buffer_put_cstring(&b, authctxt->method->name);
714 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000715 if (!(datafellows & SSH_BUG_PKAUTH))
716 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000717 buffer_put_string(&b, blob, bloblen);
718 }
719 xfree(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000720 /* append signature */
721 buffer_put_string(&b, signature, slen);
722 xfree(signature);
723
724 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000725 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100726 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000727 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000728
729 /* put remaining data from buffer into packet */
730 packet_start(SSH2_MSG_USERAUTH_REQUEST);
731 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
732 buffer_free(&b);
733
734 /* send */
735 packet_send();
736 packet_write_wait();
Damien Millerad833b32000-08-23 10:46:23 +1000737
738 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000739}
740
Damien Miller62cee002000-09-23 17:15:56 +1100741/* sign callback */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000742int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
743 u_char *data, int datalen)
Damien Miller62cee002000-09-23 17:15:56 +1100744{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100745 return key_sign(key, sigp, lenp, data, datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100746}
747
Damien Miller994cf142000-07-21 10:19:44 +1000748int
Damien Miller62cee002000-09-23 17:15:56 +1100749userauth_pubkey_identity(Authctxt *authctxt, char *filename)
Damien Miller994cf142000-07-21 10:19:44 +1000750{
751 Key *k;
Damien Miller62cee002000-09-23 17:15:56 +1100752 int i, ret, try_next;
Damien Miller994cf142000-07-21 10:19:44 +1000753 struct stat st;
754
755 if (stat(filename, &st) != 0) {
756 debug("key does not exist: %s", filename);
757 return 0;
758 }
759 debug("try pubkey: %s", filename);
760
Damien Miller0bc1bd82000-11-13 22:57:25 +1100761 k = key_new(KEY_UNSPEC);
Damien Miller994cf142000-07-21 10:19:44 +1000762 if (!load_private_key(filename, "", k, NULL)) {
763 int success = 0;
764 char *passphrase;
765 char prompt[300];
766 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100767 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100768 for (i = 0; i < options.number_of_password_prompts; i++) {
769 passphrase = read_passphrase(prompt, 0);
770 if (strcmp(passphrase, "") != 0) {
771 success = load_private_key(filename, passphrase, k, NULL);
772 try_next = 0;
773 } else {
774 debug2("no passphrase given, try next key");
775 try_next = 1;
776 }
777 memset(passphrase, 0, strlen(passphrase));
778 xfree(passphrase);
779 if (success || try_next)
780 break;
781 debug2("bad passphrase given, try again...");
782 }
Damien Miller994cf142000-07-21 10:19:44 +1000783 if (!success) {
784 key_free(k);
785 return 0;
786 }
787 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100788 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
Damien Millerad833b32000-08-23 10:46:23 +1000789 key_free(k);
790 return ret;
791}
792
Damien Miller62cee002000-09-23 17:15:56 +1100793/* sign callback */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000794int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
795 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000796{
Damien Miller62cee002000-09-23 17:15:56 +1100797 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000798}
799
800int
Damien Miller62cee002000-09-23 17:15:56 +1100801userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000802{
803 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100804 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000805 char *comment;
806 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000807
808 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100809 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
810 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100811 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000812 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100814 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 debug2("userauth_pubkey_agent: no more keys");
816 } else {
817 debug("userauth_pubkey_agent: trying agent key %s", comment);
818 xfree(comment);
819 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
820 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100821 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 if (ret == 0)
823 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000824 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000825}
826
Damien Miller62cee002000-09-23 17:15:56 +1100827int
828userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000829{
Damien Miller62cee002000-09-23 17:15:56 +1100830 static int idx = 0;
831 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000832
Damien Miller0bc1bd82000-11-13 22:57:25 +1100833 if (authctxt->agent != NULL) {
834 do {
835 sent = userauth_pubkey_agent(authctxt);
836 } while(!sent && authctxt->agent->howmany > 0);
837 }
838 while (!sent && idx < options.num_identity_files) {
839 if (options.identity_files_type[idx] != KEY_RSA1)
840 sent = userauth_pubkey_identity(authctxt,
841 options.identity_files[idx]);
842 idx++;
843 }
Damien Miller62cee002000-09-23 17:15:56 +1100844 return sent;
845}
Damien Millereba71ba2000-04-29 23:57:08 +1000846
Damien Miller874d77b2000-10-14 16:23:11 +1100847/*
848 * Send userauth request message specifying keyboard-interactive method.
849 */
850int
851userauth_kbdint(Authctxt *authctxt)
852{
853 static int attempt = 0;
854
855 if (attempt++ >= options.number_of_password_prompts)
856 return 0;
857
858 debug2("userauth_kbdint");
859 packet_start(SSH2_MSG_USERAUTH_REQUEST);
860 packet_put_cstring(authctxt->server_user);
861 packet_put_cstring(authctxt->service);
862 packet_put_cstring(authctxt->method->name);
863 packet_put_cstring(""); /* lang */
864 packet_put_cstring(options.kbd_interactive_devices ?
865 options.kbd_interactive_devices : "");
866 packet_send();
867 packet_write_wait();
868
869 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
870 return 1;
871}
872
873/*
874 * parse SSH2_MSG_USERAUTH_INFO_REQUEST, prompt user and send
875 * SSH2_MSG_USERAUTH_INFO_RESPONSE
876 */
877void
878input_userauth_info_req(int type, int plen, void *ctxt)
879{
880 Authctxt *authctxt = ctxt;
881 char *name = NULL;
882 char *inst = NULL;
883 char *lang = NULL;
884 char *prompt = NULL;
885 char *response = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000886 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100887 int echo = 0;
888
889 debug2("input_userauth_info_req");
890
891 if (authctxt == NULL)
892 fatal("input_userauth_info_req: no authentication context");
893
894 name = packet_get_string(NULL);
895 inst = packet_get_string(NULL);
896 lang = packet_get_string(NULL);
897
898 if (strlen(name) > 0)
899 cli_mesg(name);
900 xfree(name);
901
902 if (strlen(inst) > 0)
903 cli_mesg(inst);
904 xfree(inst);
905 xfree(lang); /* unused */
906
907 num_prompts = packet_get_int();
908 /*
909 * Begin to build info response packet based on prompts requested.
910 * We commit to providing the correct number of responses, so if
911 * further on we run into a problem that prevents this, we have to
912 * be sure and clean this up and send a correct error response.
913 */
914 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
915 packet_put_int(num_prompts);
916
917 for (i = 0; i < num_prompts; i++) {
918 prompt = packet_get_string(NULL);
919 echo = packet_get_char();
920
921 response = cli_prompt(prompt, echo);
922
923 packet_put_cstring(response);
924 memset(response, 0, strlen(response));
925 xfree(response);
926 xfree(prompt);
927 }
928 packet_done(); /* done with parsing incoming message. */
929
930 packet_send();
931 packet_write_wait();
932}
Damien Miller62cee002000-09-23 17:15:56 +1100933
934/* find auth method */
935
936#define DELIM ","
937
938static char *def_authlist = "publickey,password";
939static char *authlist_current = NULL; /* clean copy used for comparison */
940static char *authname_current = NULL; /* last used auth method */
941static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
942static char *authlist_state = NULL; /* state variable for strtok_r() */
943
944/*
945 * Before starting to use a new authentication method list sent by the
946 * server, reset internal variables. This should also be called when
947 * finished processing server list to free resources.
948 */
949void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000950authmethod_clear(void)
Damien Miller62cee002000-09-23 17:15:56 +1100951{
952 if (authlist_current != NULL) {
953 xfree(authlist_current);
954 authlist_current = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000955 }
Damien Miller62cee002000-09-23 17:15:56 +1100956 if (authlist_working != NULL) {
957 xfree(authlist_working);
958 authlist_working = NULL;
959 }
960 if (authname_current != NULL) {
961 xfree(authname_current);
962 authlist_state = NULL;
963 }
964 if (authlist_state != NULL)
965 authlist_state = NULL;
966 return;
967}
968
969/*
970 * given auth method name, if configurable options permit this method fill
971 * in auth_ident field and return true, otherwise return false.
972 */
973int
974authmethod_is_enabled(Authmethod *method)
975{
976 if (method == NULL)
977 return 0;
978 /* return false if options indicate this method is disabled */
979 if (method->enabled == NULL || *method->enabled == 0)
980 return 0;
981 /* return false if batch mode is enabled but method needs interactive mode */
982 if (method->batch_flag != NULL && *method->batch_flag != 0)
983 return 0;
984 return 1;
985}
986
987Authmethod *
988authmethod_lookup(const char *name)
989{
990 Authmethod *method = NULL;
991 if (name != NULL)
992 for (method = authmethods; method->name != NULL; method++)
993 if (strcmp(name, method->name) == 0)
994 return method;
995 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
996 return NULL;
997}
998
999/*
1000 * Given the authentication method list sent by the server, return the
1001 * next method we should try. If the server initially sends a nil list,
1002 * use a built-in default list. If the server sends a nil list after
1003 * previously sending a valid list, continue using the list originally
1004 * sent.
1005 */
1006
1007Authmethod *
1008authmethod_get(char *authlist)
1009{
Damien Miller69b69aa2000-10-28 14:19:58 +11001010 char *name = NULL, *authname_old;
Damien Miller62cee002000-09-23 17:15:56 +11001011 Authmethod *method = NULL;
1012
1013 /* Use a suitable default if we're passed a nil list. */
1014 if (authlist == NULL || strlen(authlist) == 0)
1015 authlist = def_authlist;
1016
1017 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1018 /* start over if passed a different list */
Damien Miller874d77b2000-10-14 16:23:11 +11001019 debug3("start over, passed a different list");
Damien Miller62cee002000-09-23 17:15:56 +11001020 authmethod_clear();
1021 authlist_current = xstrdup(authlist);
1022 authlist_working = xstrdup(authlist);
1023 name = strtok_r(authlist_working, DELIM, &authlist_state);
Damien Millereba71ba2000-04-29 23:57:08 +10001024 } else {
Damien Miller62cee002000-09-23 17:15:56 +11001025 /*
1026 * try to use previously used authentication method
1027 * or continue to use previously passed list
1028 */
1029 name = (authname_current != NULL) ?
1030 authname_current : strtok_r(NULL, DELIM, &authlist_state);
Damien Millereba71ba2000-04-29 23:57:08 +10001031 }
Damien Millereba71ba2000-04-29 23:57:08 +10001032
Damien Miller62cee002000-09-23 17:15:56 +11001033 while (name != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +11001034 debug3("authmethod_lookup %s", name);
Damien Miller62cee002000-09-23 17:15:56 +11001035 method = authmethod_lookup(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001036 if (method != NULL && authmethod_is_enabled(method)) {
1037 debug3("authmethod_is_enabled %s", name);
Damien Millereba71ba2000-04-29 23:57:08 +10001038 break;
Damien Miller874d77b2000-10-14 16:23:11 +11001039 }
Damien Miller62cee002000-09-23 17:15:56 +11001040 name = strtok_r(NULL, DELIM, &authlist_state);
Damien Miller874d77b2000-10-14 16:23:11 +11001041 method = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001042 }
Damien Miller62cee002000-09-23 17:15:56 +11001043
Damien Miller69b69aa2000-10-28 14:19:58 +11001044 authname_old = authname_current;
Damien Miller874d77b2000-10-14 16:23:11 +11001045 if (method != NULL) {
Damien Miller62cee002000-09-23 17:15:56 +11001046 debug("next auth method to try is %s", name);
1047 authname_current = xstrdup(name);
Damien Miller62cee002000-09-23 17:15:56 +11001048 } else {
1049 debug("no more auth methods to try");
1050 authname_current = NULL;
Damien Miller62cee002000-09-23 17:15:56 +11001051 }
Damien Miller69b69aa2000-10-28 14:19:58 +11001052
1053 if (authname_old != NULL)
1054 xfree(authname_old);
1055
1056 return (method);
Damien Millereba71ba2000-04-29 23:57:08 +10001057}