blob: 6bd524e0b69041e429e048f927ba21e75de60041 [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 Lindstrom95fb2dd2001-01-23 03:12:10 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.39 2001/01/22 23:06:40 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
Kevin Stevesef4eea92001-02-05 12:42:17 +0000148ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
Damien Miller874d77b2000-10-14 16:23:11 +1100149 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);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000251 BN_free(dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000252#ifdef DEBUG_KEXDH
253 fprintf(stderr, "hash == ");
254 for (i = 0; i< 20; i++)
255 fprintf(stderr, "%02x", (hash[i])&0xff);
256 fprintf(stderr, "\n");
257#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000258 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100259 fatal("key_verify failed for server_host_key");
Damien Millereba71ba2000-04-29 23:57:08 +1000260 key_free(server_host_key);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000261 xfree(signature);
Damien Millereba71ba2000-04-29 23:57:08 +1000262
263 kex_derive_keys(kex, hash, shared_secret);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000264 BN_clear_free(shared_secret);
Damien Millereba71ba2000-04-29 23:57:08 +1000265 packet_set_kex(kex);
266
Damien Millereba71ba2000-04-29 23:57:08 +1000267 /* save session id */
268 session_id2_len = 20;
269 session_id2 = xmalloc(session_id2_len);
270 memcpy(session_id2, hash, session_id2_len);
Damien Millerb1715dc2000-05-30 13:44:51 +1000271}
272
Damien Miller874d77b2000-10-14 16:23:11 +1100273/* diffie-hellman-group-exchange-sha1 */
274
275/*
276 * Estimates the group order for a Diffie-Hellman group that has an
277 * attack complexity approximately the same as O(2**bits). Estimate
278 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
279 */
280
281int
282dh_estimate(int bits)
Damien Millerb1715dc2000-05-30 13:44:51 +1000283{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000284
Damien Miller874d77b2000-10-14 16:23:11 +1100285 if (bits < 64)
286 return (512); /* O(2**63) */
287 if (bits < 128)
288 return (1024); /* O(2**86) */
289 if (bits < 192)
290 return (2048); /* O(2**116) */
291 return (4096); /* O(2**156) */
292}
Damien Millerb1715dc2000-05-30 13:44:51 +1000293
Damien Miller874d77b2000-10-14 16:23:11 +1100294void
295ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
296 Buffer *client_kexinit, Buffer *server_kexinit)
297{
298#ifdef DEBUG_KEXDH
299 int i;
300#endif
301 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000302 u_int klen, kout;
Damien Miller874d77b2000-10-14 16:23:11 +1100303 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000304 u_int slen, nbits;
Damien Miller874d77b2000-10-14 16:23:11 +1100305 char *server_host_key_blob = NULL;
306 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000307 u_int sbloblen;
Damien Miller874d77b2000-10-14 16:23:11 +1100308 DH *dh;
309 BIGNUM *dh_server_pub = 0;
310 BIGNUM *shared_secret = 0;
311 BIGNUM *p = 0, *g = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000312 u_char *kbuf;
313 u_char *hash;
Damien Millerb1715dc2000-05-30 13:44:51 +1000314
Damien Miller874d77b2000-10-14 16:23:11 +1100315 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
Damien Millerb1715dc2000-05-30 13:44:51 +1000316
Damien Miller874d77b2000-10-14 16:23:11 +1100317 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
318 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
319 packet_put_int(nbits);
Damien Millereba71ba2000-04-29 23:57:08 +1000320 packet_send();
321 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000322
323#ifdef DEBUG_KEXDH
Damien Miller874d77b2000-10-14 16:23:11 +1100324 fprintf(stderr, "\nnbits = %d", nbits);
325#endif
326
327 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
328
329 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
330
331 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
332
333 if ((p = BN_new()) == NULL)
334 fatal("BN_new");
335 packet_get_bignum2(p, &dlen);
336 if ((g = BN_new()) == NULL)
337 fatal("BN_new");
338 packet_get_bignum2(g, &dlen);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000339 dh = dh_new_group(g, p);
Damien Miller874d77b2000-10-14 16:23:11 +1100340
Kevin Steves6b875862000-12-15 23:31:01 +0000341 dh_gen_key(dh);
342
Damien Miller874d77b2000-10-14 16:23:11 +1100343#ifdef DEBUG_KEXDH
344 fprintf(stderr, "\np= ");
345 BN_print_fp(stderr, dh->p);
346 fprintf(stderr, "\ng= ");
347 BN_print_fp(stderr, dh->g);
348 fprintf(stderr, "\npub= ");
349 BN_print_fp(stderr, dh->pub_key);
350 fprintf(stderr, "\n");
351 DHparams_print_fp(stderr, dh);
352#endif
353
354 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
355 /* generate and send 'e', client DH public key */
356 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
357 packet_put_bignum2(dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000358 packet_send();
359 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100360
361 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
362
363 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
364
365 debug("Got SSH2_MSG_KEXDH_REPLY.");
366
367 /* key, cert */
368 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Miller874d77b2000-10-14 16:23:11 +1100370 if (server_host_key == NULL)
371 fatal("cannot decode server_host_key_blob");
372
373 check_host_key(host, hostaddr, server_host_key,
374 options.user_hostfile2, options.system_hostfile2);
375
376 /* DH paramter f, server public DH key */
377 dh_server_pub = BN_new();
378 if (dh_server_pub == NULL)
379 fatal("dh_server_pub == NULL");
380 packet_get_bignum2(dh_server_pub, &dlen);
381
382#ifdef DEBUG_KEXDH
383 fprintf(stderr, "\ndh_server_pub= ");
384 BN_print_fp(stderr, dh_server_pub);
385 fprintf(stderr, "\n");
386 debug("bits %d", BN_num_bits(dh_server_pub));
Damien Millereba71ba2000-04-29 23:57:08 +1000387#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100388
389 /* signed H */
390 signature = packet_get_string(&slen);
391 packet_done();
392
393 if (!dh_pub_is_valid(dh, dh_server_pub))
394 packet_disconnect("bad server public DH value");
395
396 klen = DH_size(dh);
397 kbuf = xmalloc(klen);
398 kout = DH_compute_key(kbuf, dh_server_pub, dh);
399#ifdef DEBUG_KEXDH
400 debug("shared secret: len %d/%d", klen, kout);
401 fprintf(stderr, "shared secret == ");
402 for (i = 0; i< kout; i++)
403 fprintf(stderr, "%02x", (kbuf[i])&0xff);
404 fprintf(stderr, "\n");
405#endif
406 shared_secret = BN_new();
407
408 BN_bin2bn(kbuf, kout, shared_secret);
409 memset(kbuf, 0, klen);
410 xfree(kbuf);
411
412 /* calc and verify H */
413 hash = kex_hash_gex(
414 client_version_string,
415 server_version_string,
416 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
417 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
418 server_host_key_blob, sbloblen,
Kevin Stevesef4eea92001-02-05 12:42:17 +0000419 nbits, dh->p, dh->g,
Damien Miller874d77b2000-10-14 16:23:11 +1100420 dh->pub_key,
421 dh_server_pub,
422 shared_secret
423 );
424 xfree(server_host_key_blob);
425 DH_free(dh);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000426 BN_free(dh_server_pub);
Damien Miller874d77b2000-10-14 16:23:11 +1100427#ifdef DEBUG_KEXDH
428 fprintf(stderr, "hash == ");
429 for (i = 0; i< 20; i++)
430 fprintf(stderr, "%02x", (hash[i])&0xff);
431 fprintf(stderr, "\n");
432#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000433 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100434 fatal("key_verify failed for server_host_key");
Damien Miller874d77b2000-10-14 16:23:11 +1100435 key_free(server_host_key);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000436 xfree(signature);
Damien Miller874d77b2000-10-14 16:23:11 +1100437
438 kex_derive_keys(kex, hash, shared_secret);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000439 BN_clear_free(shared_secret);
Damien Miller874d77b2000-10-14 16:23:11 +1100440 packet_set_kex(kex);
441
442 /* save session id */
443 session_id2_len = 20;
444 session_id2 = xmalloc(session_id2_len);
445 memcpy(session_id2, hash, session_id2_len);
Damien Millereba71ba2000-04-29 23:57:08 +1000446}
Damien Millerb1715dc2000-05-30 13:44:51 +1000447
Damien Millereba71ba2000-04-29 23:57:08 +1000448/*
449 * Authenticate user
450 */
Damien Miller62cee002000-09-23 17:15:56 +1100451
452typedef struct Authctxt Authctxt;
453typedef struct Authmethod Authmethod;
454
455typedef int sign_cb_fn(
456 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000457 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100458
459struct Authctxt {
460 const char *server_user;
461 const char *host;
462 const char *service;
463 AuthenticationConnection *agent;
Damien Miller62cee002000-09-23 17:15:56 +1100464 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100465 int success;
Damien Miller62cee002000-09-23 17:15:56 +1100466};
467struct Authmethod {
468 char *name; /* string to compare against server's list */
469 int (*userauth)(Authctxt *authctxt);
470 int *enabled; /* flag in option struct that enables method */
471 int *batch_flag; /* flag in option struct that disables method */
472};
473
474void input_userauth_success(int type, int plen, void *ctxt);
475void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000476void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100477void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100478void input_userauth_info_req(int type, int plen, void *ctxt);
479
480int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100481int userauth_pubkey(Authctxt *authctxt);
482int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100483int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100484
Ben Lindstrom46c16222000-12-22 01:43:59 +0000485void authmethod_clear(void);
Damien Miller874d77b2000-10-14 16:23:11 +1100486Authmethod *authmethod_get(char *authlist);
487Authmethod *authmethod_lookup(const char *name);
Damien Miller62cee002000-09-23 17:15:56 +1100488
489Authmethod authmethods[] = {
490 {"publickey",
491 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100493 NULL},
494 {"password",
495 userauth_passwd,
496 &options.password_authentication,
497 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100498 {"keyboard-interactive",
499 userauth_kbdint,
500 &options.kbd_interactive_authentication,
501 &options.batch_mode},
502 {"none",
503 userauth_none,
504 NULL,
505 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100506 {NULL, NULL, NULL, NULL}
507};
508
509void
510ssh_userauth2(const char *server_user, char *host)
511{
512 Authctxt authctxt;
513 int type;
514 int plen;
515
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000516 if (options.challenge_reponse_authentication)
517 options.kbd_interactive_authentication = 1;
518
Damien Miller62cee002000-09-23 17:15:56 +1100519 debug("send SSH2_MSG_SERVICE_REQUEST");
520 packet_start(SSH2_MSG_SERVICE_REQUEST);
521 packet_put_cstring("ssh-userauth");
522 packet_send();
523 packet_write_wait();
524 type = packet_read(&plen);
525 if (type != SSH2_MSG_SERVICE_ACCEPT) {
526 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
527 }
528 if (packet_remaining() > 0) {
529 char *reply = packet_get_string(&plen);
530 debug("service_accept: %s", reply);
531 xfree(reply);
532 packet_done();
533 } else {
534 debug("buggy server: service_accept w/o service");
535 }
536 packet_done();
537 debug("got SSH2_MSG_SERVICE_ACCEPT");
538
539 /* setup authentication context */
540 authctxt.agent = ssh_get_authentication_connection();
541 authctxt.server_user = server_user;
542 authctxt.host = host;
543 authctxt.service = "ssh-connection"; /* service name */
544 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100545 authctxt.method = authmethod_lookup("none");
546 if (authctxt.method == NULL)
547 fatal("ssh_userauth2: internal error: cannot send userauth none request");
548 authmethod_clear();
Damien Miller62cee002000-09-23 17:15:56 +1100549
550 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100551 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100552
553 dispatch_init(&input_userauth_error);
554 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
555 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000556 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100557 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
558
559 if (authctxt.agent != NULL)
560 ssh_close_authentication_connection(authctxt.agent);
561
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000562 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100563}
564void
565input_userauth_error(int type, int plen, void *ctxt)
566{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000567 fatal("input_userauth_error: bad message during authentication: "
568 "type %d", type);
569}
570void
571input_userauth_banner(int type, int plen, void *ctxt)
572{
573 char *msg, *lang;
574 debug3("input_userauth_banner");
575 msg = packet_get_string(NULL);
576 lang = packet_get_string(NULL);
577 fprintf(stderr, "%s", msg);
578 xfree(msg);
579 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100580}
581void
582input_userauth_success(int type, int plen, void *ctxt)
583{
584 Authctxt *authctxt = ctxt;
585 if (authctxt == NULL)
586 fatal("input_userauth_success: no authentication context");
587 authctxt->success = 1; /* break out */
588}
589void
590input_userauth_failure(int type, int plen, void *ctxt)
591{
592 Authmethod *method = NULL;
593 Authctxt *authctxt = ctxt;
594 char *authlist = NULL;
595 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100596
597 if (authctxt == NULL)
598 fatal("input_userauth_failure: no authentication context");
599
Damien Miller874d77b2000-10-14 16:23:11 +1100600 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100601 partial = packet_get_char();
602 packet_done();
603
604 if (partial != 0)
605 debug("partial success");
606 debug("authentications that can continue: %s", authlist);
607
608 for (;;) {
Damien Miller62cee002000-09-23 17:15:56 +1100609 method = authmethod_get(authlist);
610 if (method == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000611 fatal("Unable to find an authentication method");
Damien Miller874d77b2000-10-14 16:23:11 +1100612 authctxt->method = method;
Damien Miller62cee002000-09-23 17:15:56 +1100613 if (method->userauth(authctxt) != 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100614 debug2("we sent a %s packet, wait for reply", method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100615 break;
616 } else {
617 debug2("we did not send a packet, disable method");
618 method->enabled = NULL;
619 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000620 }
Damien Miller62cee002000-09-23 17:15:56 +1100621 xfree(authlist);
622}
623
Damien Millereba71ba2000-04-29 23:57:08 +1000624int
Damien Miller874d77b2000-10-14 16:23:11 +1100625userauth_none(Authctxt *authctxt)
626{
627 /* initial userauth request */
628 packet_start(SSH2_MSG_USERAUTH_REQUEST);
629 packet_put_cstring(authctxt->server_user);
630 packet_put_cstring(authctxt->service);
631 packet_put_cstring(authctxt->method->name);
632 packet_send();
633 packet_write_wait();
634 return 1;
635}
636
637int
Damien Miller62cee002000-09-23 17:15:56 +1100638userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000639{
Damien Millere247cc42000-05-07 12:03:14 +1000640 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000641 char prompt[80];
642 char *password;
643
Damien Millerd3a18572000-06-07 19:55:44 +1000644 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000645 return 0;
646
Damien Millerd3a18572000-06-07 19:55:44 +1000647 if(attempt != 1)
648 error("Permission denied, please try again.");
649
Damien Millereba71ba2000-04-29 23:57:08 +1000650 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100651 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000652 password = read_passphrase(prompt, 0);
653 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100654 packet_put_cstring(authctxt->server_user);
655 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100656 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000657 packet_put_char(0);
658 packet_put_cstring(password);
659 memset(password, 0, strlen(password));
660 xfree(password);
661 packet_send();
662 packet_write_wait();
663 return 1;
664}
665
Damien Millerad833b32000-08-23 10:46:23 +1000666int
Damien Miller62cee002000-09-23 17:15:56 +1100667sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000668{
669 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000670 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000671 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000672 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000673 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100674 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000675
Ben Lindstromd121f612000-12-03 17:00:47 +0000676 debug3("sign_and_send_pubkey");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677 if (key_to_blob(k, &blob, &bloblen) == 0) {
678 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000679 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100680 return 0;
681 }
Damien Millereba71ba2000-04-29 23:57:08 +1000682 /* data to be signed */
683 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100684 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000685 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000686 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100687 } else {
688 buffer_put_string(&b, session_id2, session_id2_len);
689 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000690 }
Damien Millereba71ba2000-04-29 23:57:08 +1000691 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100692 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000693 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000694 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000695 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100696 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000697 if (datafellows & SSH_BUG_PKAUTH) {
698 buffer_put_char(&b, have_sig);
699 } else {
700 buffer_put_cstring(&b, authctxt->method->name);
701 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000702 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000703 }
Damien Millereba71ba2000-04-29 23:57:08 +1000704 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000705
706 /* generate signature */
Damien Miller62cee002000-09-23 17:15:56 +1100707 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000708 if (ret == -1) {
709 xfree(blob);
710 buffer_free(&b);
711 return 0;
712 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100713#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000714 buffer_dump(&b);
715#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000716 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000717 buffer_clear(&b);
718 buffer_append(&b, session_id2, session_id2_len);
719 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100720 buffer_put_cstring(&b, authctxt->server_user);
721 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100722 buffer_put_cstring(&b, authctxt->method->name);
723 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000724 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000725 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000726 buffer_put_string(&b, blob, bloblen);
727 }
728 xfree(blob);
Damien Millereba71ba2000-04-29 23:57:08 +1000729 /* append signature */
730 buffer_put_string(&b, signature, slen);
731 xfree(signature);
732
733 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000734 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100735 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000736 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000737
738 /* put remaining data from buffer into packet */
739 packet_start(SSH2_MSG_USERAUTH_REQUEST);
740 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
741 buffer_free(&b);
742
743 /* send */
744 packet_send();
745 packet_write_wait();
Damien Millerad833b32000-08-23 10:46:23 +1000746
747 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000748}
749
Damien Miller62cee002000-09-23 17:15:56 +1100750/* sign callback */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000751int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
752 u_char *data, int datalen)
Damien Miller62cee002000-09-23 17:15:56 +1100753{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100754 return key_sign(key, sigp, lenp, data, datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100755}
756
Damien Miller994cf142000-07-21 10:19:44 +1000757int
Damien Miller62cee002000-09-23 17:15:56 +1100758userauth_pubkey_identity(Authctxt *authctxt, char *filename)
Damien Miller994cf142000-07-21 10:19:44 +1000759{
760 Key *k;
Damien Miller62cee002000-09-23 17:15:56 +1100761 int i, ret, try_next;
Damien Miller994cf142000-07-21 10:19:44 +1000762 struct stat st;
763
764 if (stat(filename, &st) != 0) {
765 debug("key does not exist: %s", filename);
766 return 0;
767 }
768 debug("try pubkey: %s", filename);
769
Damien Miller0bc1bd82000-11-13 22:57:25 +1100770 k = key_new(KEY_UNSPEC);
Damien Miller994cf142000-07-21 10:19:44 +1000771 if (!load_private_key(filename, "", k, NULL)) {
772 int success = 0;
773 char *passphrase;
774 char prompt[300];
775 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100776 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100777 for (i = 0; i < options.number_of_password_prompts; i++) {
778 passphrase = read_passphrase(prompt, 0);
779 if (strcmp(passphrase, "") != 0) {
780 success = load_private_key(filename, passphrase, k, NULL);
781 try_next = 0;
782 } else {
783 debug2("no passphrase given, try next key");
784 try_next = 1;
785 }
786 memset(passphrase, 0, strlen(passphrase));
787 xfree(passphrase);
788 if (success || try_next)
789 break;
790 debug2("bad passphrase given, try again...");
791 }
Damien Miller994cf142000-07-21 10:19:44 +1000792 if (!success) {
793 key_free(k);
794 return 0;
795 }
796 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100797 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
Damien Millerad833b32000-08-23 10:46:23 +1000798 key_free(k);
799 return ret;
800}
801
Damien Miller62cee002000-09-23 17:15:56 +1100802/* sign callback */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000803int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
804 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000805{
Damien Miller62cee002000-09-23 17:15:56 +1100806 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000807}
808
809int
Damien Miller62cee002000-09-23 17:15:56 +1100810userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000811{
812 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000814 char *comment;
815 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000816
817 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100818 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
819 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100820 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000821 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100823 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100824 debug2("userauth_pubkey_agent: no more keys");
825 } else {
826 debug("userauth_pubkey_agent: trying agent key %s", comment);
827 xfree(comment);
828 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
829 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100830 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100831 if (ret == 0)
832 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000833 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000834}
835
Damien Miller62cee002000-09-23 17:15:56 +1100836int
837userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000838{
Damien Miller62cee002000-09-23 17:15:56 +1100839 static int idx = 0;
840 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000841
Damien Miller0bc1bd82000-11-13 22:57:25 +1100842 if (authctxt->agent != NULL) {
843 do {
844 sent = userauth_pubkey_agent(authctxt);
845 } while(!sent && authctxt->agent->howmany > 0);
846 }
847 while (!sent && idx < options.num_identity_files) {
848 if (options.identity_files_type[idx] != KEY_RSA1)
849 sent = userauth_pubkey_identity(authctxt,
850 options.identity_files[idx]);
851 idx++;
852 }
Damien Miller62cee002000-09-23 17:15:56 +1100853 return sent;
854}
Damien Millereba71ba2000-04-29 23:57:08 +1000855
Damien Miller874d77b2000-10-14 16:23:11 +1100856/*
857 * Send userauth request message specifying keyboard-interactive method.
858 */
859int
860userauth_kbdint(Authctxt *authctxt)
861{
862 static int attempt = 0;
863
864 if (attempt++ >= options.number_of_password_prompts)
865 return 0;
866
867 debug2("userauth_kbdint");
868 packet_start(SSH2_MSG_USERAUTH_REQUEST);
869 packet_put_cstring(authctxt->server_user);
870 packet_put_cstring(authctxt->service);
871 packet_put_cstring(authctxt->method->name);
872 packet_put_cstring(""); /* lang */
873 packet_put_cstring(options.kbd_interactive_devices ?
874 options.kbd_interactive_devices : "");
875 packet_send();
876 packet_write_wait();
877
878 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
879 return 1;
880}
881
882/*
883 * parse SSH2_MSG_USERAUTH_INFO_REQUEST, prompt user and send
884 * SSH2_MSG_USERAUTH_INFO_RESPONSE
885 */
886void
887input_userauth_info_req(int type, int plen, void *ctxt)
888{
889 Authctxt *authctxt = ctxt;
890 char *name = NULL;
891 char *inst = NULL;
892 char *lang = NULL;
893 char *prompt = NULL;
894 char *response = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000895 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100896 int echo = 0;
897
898 debug2("input_userauth_info_req");
899
900 if (authctxt == NULL)
901 fatal("input_userauth_info_req: no authentication context");
902
903 name = packet_get_string(NULL);
904 inst = packet_get_string(NULL);
905 lang = packet_get_string(NULL);
906
907 if (strlen(name) > 0)
908 cli_mesg(name);
909 xfree(name);
910
911 if (strlen(inst) > 0)
912 cli_mesg(inst);
913 xfree(inst);
914 xfree(lang); /* unused */
915
916 num_prompts = packet_get_int();
917 /*
918 * Begin to build info response packet based on prompts requested.
919 * We commit to providing the correct number of responses, so if
920 * further on we run into a problem that prevents this, we have to
921 * be sure and clean this up and send a correct error response.
922 */
923 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
924 packet_put_int(num_prompts);
925
926 for (i = 0; i < num_prompts; i++) {
927 prompt = packet_get_string(NULL);
928 echo = packet_get_char();
929
930 response = cli_prompt(prompt, echo);
931
932 packet_put_cstring(response);
933 memset(response, 0, strlen(response));
934 xfree(response);
935 xfree(prompt);
936 }
937 packet_done(); /* done with parsing incoming message. */
938
939 packet_send();
940 packet_write_wait();
941}
Damien Miller62cee002000-09-23 17:15:56 +1100942
943/* find auth method */
944
945#define DELIM ","
946
947static char *def_authlist = "publickey,password";
948static char *authlist_current = NULL; /* clean copy used for comparison */
949static char *authname_current = NULL; /* last used auth method */
950static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
951static char *authlist_state = NULL; /* state variable for strtok_r() */
952
953/*
954 * Before starting to use a new authentication method list sent by the
955 * server, reset internal variables. This should also be called when
956 * finished processing server list to free resources.
957 */
958void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000959authmethod_clear(void)
Damien Miller62cee002000-09-23 17:15:56 +1100960{
961 if (authlist_current != NULL) {
962 xfree(authlist_current);
963 authlist_current = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000964 }
Damien Miller62cee002000-09-23 17:15:56 +1100965 if (authlist_working != NULL) {
966 xfree(authlist_working);
967 authlist_working = NULL;
968 }
969 if (authname_current != NULL) {
970 xfree(authname_current);
971 authlist_state = NULL;
972 }
973 if (authlist_state != NULL)
974 authlist_state = NULL;
975 return;
976}
977
978/*
979 * given auth method name, if configurable options permit this method fill
980 * in auth_ident field and return true, otherwise return false.
981 */
982int
983authmethod_is_enabled(Authmethod *method)
984{
985 if (method == NULL)
986 return 0;
987 /* return false if options indicate this method is disabled */
988 if (method->enabled == NULL || *method->enabled == 0)
989 return 0;
990 /* return false if batch mode is enabled but method needs interactive mode */
991 if (method->batch_flag != NULL && *method->batch_flag != 0)
992 return 0;
993 return 1;
994}
995
996Authmethod *
997authmethod_lookup(const char *name)
998{
999 Authmethod *method = NULL;
1000 if (name != NULL)
1001 for (method = authmethods; method->name != NULL; method++)
1002 if (strcmp(name, method->name) == 0)
1003 return method;
1004 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1005 return NULL;
1006}
1007
1008/*
1009 * Given the authentication method list sent by the server, return the
1010 * next method we should try. If the server initially sends a nil list,
1011 * use a built-in default list. If the server sends a nil list after
1012 * previously sending a valid list, continue using the list originally
1013 * sent.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001014 */
Damien Miller62cee002000-09-23 17:15:56 +11001015
1016Authmethod *
1017authmethod_get(char *authlist)
1018{
Damien Miller69b69aa2000-10-28 14:19:58 +11001019 char *name = NULL, *authname_old;
Damien Miller62cee002000-09-23 17:15:56 +11001020 Authmethod *method = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001021
Damien Miller62cee002000-09-23 17:15:56 +11001022 /* Use a suitable default if we're passed a nil list. */
1023 if (authlist == NULL || strlen(authlist) == 0)
1024 authlist = def_authlist;
1025
1026 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1027 /* start over if passed a different list */
Damien Miller874d77b2000-10-14 16:23:11 +11001028 debug3("start over, passed a different list");
Damien Miller62cee002000-09-23 17:15:56 +11001029 authmethod_clear();
1030 authlist_current = xstrdup(authlist);
1031 authlist_working = xstrdup(authlist);
1032 name = strtok_r(authlist_working, DELIM, &authlist_state);
Damien Millereba71ba2000-04-29 23:57:08 +10001033 } else {
Damien Miller62cee002000-09-23 17:15:56 +11001034 /*
1035 * try to use previously used authentication method
1036 * or continue to use previously passed list
1037 */
1038 name = (authname_current != NULL) ?
1039 authname_current : strtok_r(NULL, DELIM, &authlist_state);
Damien Millereba71ba2000-04-29 23:57:08 +10001040 }
Damien Millereba71ba2000-04-29 23:57:08 +10001041
Damien Miller62cee002000-09-23 17:15:56 +11001042 while (name != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +11001043 debug3("authmethod_lookup %s", name);
Damien Miller62cee002000-09-23 17:15:56 +11001044 method = authmethod_lookup(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001045 if (method != NULL && authmethod_is_enabled(method)) {
1046 debug3("authmethod_is_enabled %s", name);
Damien Millereba71ba2000-04-29 23:57:08 +10001047 break;
Damien Miller874d77b2000-10-14 16:23:11 +11001048 }
Damien Miller62cee002000-09-23 17:15:56 +11001049 name = strtok_r(NULL, DELIM, &authlist_state);
Damien Miller874d77b2000-10-14 16:23:11 +11001050 method = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +10001051 }
Damien Miller62cee002000-09-23 17:15:56 +11001052
Damien Miller69b69aa2000-10-28 14:19:58 +11001053 authname_old = authname_current;
Damien Miller874d77b2000-10-14 16:23:11 +11001054 if (method != NULL) {
Damien Miller62cee002000-09-23 17:15:56 +11001055 debug("next auth method to try is %s", name);
1056 authname_current = xstrdup(name);
Damien Miller62cee002000-09-23 17:15:56 +11001057 } else {
1058 debug("no more auth methods to try");
1059 authname_current = NULL;
Damien Miller62cee002000-09-23 17:15:56 +11001060 }
Damien Miller69b69aa2000-10-28 14:19:58 +11001061
1062 if (authname_old != NULL)
1063 xfree(authname_old);
1064
1065 return (method);
Damien Millereba71ba2000-04-29 23:57:08 +10001066}