2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: sshconnect2.c,v 1.48 2001/02/15 23:19:59 markus Exp $");
28 #include <openssl/bn.h>
29 #include <openssl/md5.h>
30 #include <openssl/dh.h>
31 #include <openssl/hmac.h>
44 #include "myproposal.h"
46 #include "sshconnect.h"
55 void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
56 void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
59 extern char *client_version_string;
60 extern char *server_version_string;
61 extern Options options;
67 u_char *session_id2 = NULL;
68 int session_id2_len = 0;
71 ssh_kex2(char *host, struct sockaddr *hostaddr)
75 Buffer *client_kexinit, *server_kexinit;
76 char *sprop[PROPOSAL_MAX];
78 if (options.ciphers == (char *)-1) {
79 log("No valid ciphers for protocol version 2 given, using defaults.");
80 options.ciphers = NULL;
82 if (options.ciphers != NULL) {
83 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
84 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
86 if (options.compression) {
87 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
88 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
90 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
91 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
93 if (options.macs != NULL) {
94 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
95 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
98 /* buffers with raw kexinit messages */
99 server_kexinit = xmalloc(sizeof(*server_kexinit));
100 buffer_init(server_kexinit);
101 client_kexinit = kex_init(myproposal);
103 /* algorithm negotiation */
104 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
105 kex = kex_choose_conf(myproposal, sprop, 0);
106 for (i = 0; i < PROPOSAL_MAX; i++)
109 /* server authentication and session key agreement */
110 switch(kex->kex_type) {
112 ssh_dh1_client(kex, host, hostaddr,
113 client_kexinit, server_kexinit);
116 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
120 fatal("Unsupported key exchange %d", kex->kex_type);
123 buffer_free(client_kexinit);
124 buffer_free(server_kexinit);
125 xfree(client_kexinit);
126 xfree(server_kexinit);
128 debug("Wait SSH2_MSG_NEWKEYS.");
129 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
131 debug("GOT SSH2_MSG_NEWKEYS.");
133 debug("send SSH2_MSG_NEWKEYS.");
134 packet_start(SSH2_MSG_NEWKEYS);
137 debug("done: send SSH2_MSG_NEWKEYS.");
140 /* send 1st encrypted/maced/compressed message */
141 packet_start(SSH2_MSG_IGNORE);
142 packet_put_cstring("markus");
146 debug("done: KEX2.");
149 /* diffie-hellman-group1-sha1 */
152 ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
153 Buffer *client_kexinit, Buffer *server_kexinit)
160 char *signature = NULL;
162 char *server_host_key_blob = NULL;
163 Key *server_host_key;
166 BIGNUM *dh_server_pub = 0;
167 BIGNUM *shared_secret = 0;
171 debug("Sending SSH2_MSG_KEXDH_INIT.");
172 /* generate and send 'e', client DH public key */
173 dh = dh_new_group1();
175 packet_start(SSH2_MSG_KEXDH_INIT);
176 packet_put_bignum2(dh->pub_key);
181 fprintf(stderr, "\np= ");
182 BN_print_fp(stderr, dh->p);
183 fprintf(stderr, "\ng= ");
184 BN_print_fp(stderr, dh->g);
185 fprintf(stderr, "\npub= ");
186 BN_print_fp(stderr, dh->pub_key);
187 fprintf(stderr, "\n");
188 DHparams_print_fp(stderr, dh);
191 debug("Wait SSH2_MSG_KEXDH_REPLY.");
193 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
195 debug("Got SSH2_MSG_KEXDH_REPLY.");
198 server_host_key_blob = packet_get_string(&sbloblen);
199 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
200 if (server_host_key == NULL)
201 fatal("cannot decode server_host_key_blob");
203 check_host_key(host, hostaddr, server_host_key,
204 options.user_hostfile2, options.system_hostfile2);
206 /* DH paramter f, server public DH key */
207 dh_server_pub = BN_new();
208 if (dh_server_pub == NULL)
209 fatal("dh_server_pub == NULL");
210 packet_get_bignum2(dh_server_pub, &dlen);
213 fprintf(stderr, "\ndh_server_pub= ");
214 BN_print_fp(stderr, dh_server_pub);
215 fprintf(stderr, "\n");
216 debug("bits %d", BN_num_bits(dh_server_pub));
220 signature = packet_get_string(&slen);
223 if (!dh_pub_is_valid(dh, dh_server_pub))
224 packet_disconnect("bad server public DH value");
227 kbuf = xmalloc(klen);
228 kout = DH_compute_key(kbuf, dh_server_pub, dh);
230 debug("shared secret: len %d/%d", klen, kout);
231 fprintf(stderr, "shared secret == ");
232 for (i = 0; i< kout; i++)
233 fprintf(stderr, "%02x", (kbuf[i])&0xff);
234 fprintf(stderr, "\n");
236 shared_secret = BN_new();
238 BN_bin2bn(kbuf, kout, shared_secret);
239 memset(kbuf, 0, klen);
242 /* calc and verify H */
244 client_version_string,
245 server_version_string,
246 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
247 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
248 server_host_key_blob, sbloblen,
253 xfree(server_host_key_blob);
255 BN_free(dh_server_pub);
257 fprintf(stderr, "hash == ");
258 for (i = 0; i< 20; i++)
259 fprintf(stderr, "%02x", (hash[i])&0xff);
260 fprintf(stderr, "\n");
262 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
263 fatal("key_verify failed for server_host_key");
264 key_free(server_host_key);
267 kex_derive_keys(kex, hash, shared_secret);
268 BN_clear_free(shared_secret);
271 /* save session id */
272 session_id2_len = 20;
273 session_id2 = xmalloc(session_id2_len);
274 memcpy(session_id2, hash, session_id2_len);
277 /* diffie-hellman-group-exchange-sha1 */
280 * Estimates the group order for a Diffie-Hellman group that has an
281 * attack complexity approximately the same as O(2**bits). Estimate
282 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
286 dh_estimate(int bits)
290 return (512); /* O(2**63) */
292 return (1024); /* O(2**86) */
294 return (2048); /* O(2**116) */
295 return (4096); /* O(2**156) */
299 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
300 Buffer *client_kexinit, Buffer *server_kexinit)
307 char *signature = NULL;
309 char *server_host_key_blob = NULL;
310 Key *server_host_key;
313 BIGNUM *dh_server_pub = 0;
314 BIGNUM *shared_secret = 0;
315 BIGNUM *p = 0, *g = 0;
319 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
321 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
322 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
323 packet_put_int(nbits);
328 fprintf(stderr, "\nnbits = %d", nbits);
331 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
333 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
335 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
337 if ((p = BN_new()) == NULL)
339 packet_get_bignum2(p, &dlen);
340 if ((g = BN_new()) == NULL)
342 packet_get_bignum2(g, &dlen);
343 dh = dh_new_group(g, p);
348 fprintf(stderr, "\np= ");
349 BN_print_fp(stderr, dh->p);
350 fprintf(stderr, "\ng= ");
351 BN_print_fp(stderr, dh->g);
352 fprintf(stderr, "\npub= ");
353 BN_print_fp(stderr, dh->pub_key);
354 fprintf(stderr, "\n");
355 DHparams_print_fp(stderr, dh);
358 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
359 /* generate and send 'e', client DH public key */
360 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
361 packet_put_bignum2(dh->pub_key);
365 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
367 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
369 debug("Got SSH2_MSG_KEXDH_REPLY.");
372 server_host_key_blob = packet_get_string(&sbloblen);
373 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
374 if (server_host_key == NULL)
375 fatal("cannot decode server_host_key_blob");
377 check_host_key(host, hostaddr, server_host_key,
378 options.user_hostfile2, options.system_hostfile2);
380 /* DH paramter f, server public DH key */
381 dh_server_pub = BN_new();
382 if (dh_server_pub == NULL)
383 fatal("dh_server_pub == NULL");
384 packet_get_bignum2(dh_server_pub, &dlen);
387 fprintf(stderr, "\ndh_server_pub= ");
388 BN_print_fp(stderr, dh_server_pub);
389 fprintf(stderr, "\n");
390 debug("bits %d", BN_num_bits(dh_server_pub));
394 signature = packet_get_string(&slen);
397 if (!dh_pub_is_valid(dh, dh_server_pub))
398 packet_disconnect("bad server public DH value");
401 kbuf = xmalloc(klen);
402 kout = DH_compute_key(kbuf, dh_server_pub, dh);
404 debug("shared secret: len %d/%d", klen, kout);
405 fprintf(stderr, "shared secret == ");
406 for (i = 0; i< kout; i++)
407 fprintf(stderr, "%02x", (kbuf[i])&0xff);
408 fprintf(stderr, "\n");
410 shared_secret = BN_new();
412 BN_bin2bn(kbuf, kout, shared_secret);
413 memset(kbuf, 0, klen);
416 /* calc and verify H */
418 client_version_string,
419 server_version_string,
420 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
421 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
422 server_host_key_blob, sbloblen,
428 xfree(server_host_key_blob);
430 BN_free(dh_server_pub);
432 fprintf(stderr, "hash == ");
433 for (i = 0; i< 20; i++)
434 fprintf(stderr, "%02x", (hash[i])&0xff);
435 fprintf(stderr, "\n");
437 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
438 fatal("key_verify failed for server_host_key");
439 key_free(server_host_key);
442 kex_derive_keys(kex, hash, shared_secret);
443 BN_clear_free(shared_secret);
446 /* save session id */
447 session_id2_len = 20;
448 session_id2 = xmalloc(session_id2_len);
449 memcpy(session_id2, hash, session_id2_len);
456 typedef struct Authctxt Authctxt;
457 typedef struct Authmethod Authmethod;
459 typedef int sign_cb_fn(
460 Authctxt *authctxt, Key *key,
461 u_char **sigp, int *lenp, u_char *data, int datalen);
464 const char *server_user;
467 AuthenticationConnection *agent;
472 char *name; /* string to compare against server's list */
473 int (*userauth)(Authctxt *authctxt);
474 int *enabled; /* flag in option struct that enables method */
475 int *batch_flag; /* flag in option struct that disables method */
478 void input_userauth_success(int type, int plen, void *ctxt);
479 void input_userauth_failure(int type, int plen, void *ctxt);
480 void input_userauth_banner(int type, int plen, void *ctxt);
481 void input_userauth_error(int type, int plen, void *ctxt);
482 void input_userauth_info_req(int type, int plen, void *ctxt);
484 int userauth_none(Authctxt *authctxt);
485 int userauth_pubkey(Authctxt *authctxt);
486 int userauth_passwd(Authctxt *authctxt);
487 int userauth_kbdint(Authctxt *authctxt);
489 void authmethod_clear(void);
490 Authmethod *authmethod_get(char *authlist);
491 Authmethod *authmethod_lookup(const char *name);
493 Authmethod authmethods[] = {
496 &options.pubkey_authentication,
500 &options.password_authentication,
501 &options.batch_mode},
502 {"keyboard-interactive",
504 &options.kbd_interactive_authentication,
505 &options.batch_mode},
510 {NULL, NULL, NULL, NULL}
514 ssh_userauth2(const char *server_user, char *host)
520 if (options.challenge_reponse_authentication)
521 options.kbd_interactive_authentication = 1;
523 debug("send SSH2_MSG_SERVICE_REQUEST");
524 packet_start(SSH2_MSG_SERVICE_REQUEST);
525 packet_put_cstring("ssh-userauth");
528 type = packet_read(&plen);
529 if (type != SSH2_MSG_SERVICE_ACCEPT) {
530 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
532 if (packet_remaining() > 0) {
533 char *reply = packet_get_string(&plen);
534 debug("service_accept: %s", reply);
537 debug("buggy server: service_accept w/o service");
540 debug("got SSH2_MSG_SERVICE_ACCEPT");
542 /* setup authentication context */
543 authctxt.agent = ssh_get_authentication_connection();
544 authctxt.server_user = server_user;
545 authctxt.host = host;
546 authctxt.service = "ssh-connection"; /* service name */
547 authctxt.success = 0;
548 authctxt.method = authmethod_lookup("none");
549 if (authctxt.method == NULL)
550 fatal("ssh_userauth2: internal error: cannot send userauth none request");
553 /* initial userauth request */
554 userauth_none(&authctxt);
556 dispatch_init(&input_userauth_error);
557 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
558 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
559 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
560 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
562 if (authctxt.agent != NULL)
563 ssh_close_authentication_connection(authctxt.agent);
565 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
568 input_userauth_error(int type, int plen, void *ctxt)
570 fatal("input_userauth_error: bad message during authentication: "
574 input_userauth_banner(int type, int plen, void *ctxt)
577 debug3("input_userauth_banner");
578 msg = packet_get_string(NULL);
579 lang = packet_get_string(NULL);
580 fprintf(stderr, "%s", msg);
585 input_userauth_success(int type, int plen, void *ctxt)
587 Authctxt *authctxt = ctxt;
588 if (authctxt == NULL)
589 fatal("input_userauth_success: no authentication context");
590 authctxt->success = 1; /* break out */
593 input_userauth_failure(int type, int plen, void *ctxt)
595 Authmethod *method = NULL;
596 Authctxt *authctxt = ctxt;
597 char *authlist = NULL;
600 if (authctxt == NULL)
601 fatal("input_userauth_failure: no authentication context");
603 authlist = packet_get_string(NULL);
604 partial = packet_get_char();
608 log("Authenticated with partial success.");
609 debug("authentications that can continue: %s", authlist);
612 method = authmethod_get(authlist);
614 fatal("Permission denied (%s).", authlist);
615 authctxt->method = method;
616 if (method->userauth(authctxt) != 0) {
617 debug2("we sent a %s packet, wait for reply", method->name);
620 debug2("we did not send a packet, disable method");
621 method->enabled = NULL;
628 userauth_none(Authctxt *authctxt)
630 /* initial userauth request */
631 packet_start(SSH2_MSG_USERAUTH_REQUEST);
632 packet_put_cstring(authctxt->server_user);
633 packet_put_cstring(authctxt->service);
634 packet_put_cstring(authctxt->method->name);
641 userauth_passwd(Authctxt *authctxt)
643 static int attempt = 0;
647 if (attempt++ >= options.number_of_password_prompts)
651 error("Permission denied, please try again.");
653 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
654 authctxt->server_user, authctxt->host);
655 password = read_passphrase(prompt, 0);
656 packet_start(SSH2_MSG_USERAUTH_REQUEST);
657 packet_put_cstring(authctxt->server_user);
658 packet_put_cstring(authctxt->service);
659 packet_put_cstring(authctxt->method->name);
661 ssh_put_password(password);
662 memset(password, 0, strlen(password));
670 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
673 u_char *blob, *signature;
679 debug3("sign_and_send_pubkey");
680 if (key_to_blob(k, &blob, &bloblen) == 0) {
681 /* we cannot handle this key */
682 debug3("sign_and_send_pubkey: cannot handle key");
685 /* data to be signed */
687 if (datafellows & SSH_OLD_SESSIONID) {
688 buffer_append(&b, session_id2, session_id2_len);
689 skip = session_id2_len;
691 buffer_put_string(&b, session_id2, session_id2_len);
692 skip = buffer_len(&b);
694 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
695 buffer_put_cstring(&b, authctxt->server_user);
696 buffer_put_cstring(&b,
697 datafellows & SSH_BUG_PKSERVICE ?
700 if (datafellows & SSH_BUG_PKAUTH) {
701 buffer_put_char(&b, have_sig);
703 buffer_put_cstring(&b, authctxt->method->name);
704 buffer_put_char(&b, have_sig);
705 buffer_put_cstring(&b, key_ssh_name(k));
707 buffer_put_string(&b, blob, bloblen);
709 /* generate signature */
710 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
719 if (datafellows & SSH_BUG_PKSERVICE) {
721 buffer_append(&b, session_id2, session_id2_len);
722 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
723 buffer_put_cstring(&b, authctxt->server_user);
724 buffer_put_cstring(&b, authctxt->service);
725 buffer_put_cstring(&b, authctxt->method->name);
726 buffer_put_char(&b, have_sig);
727 if (!(datafellows & SSH_BUG_PKAUTH))
728 buffer_put_cstring(&b, key_ssh_name(k));
729 buffer_put_string(&b, blob, bloblen);
732 /* append signature */
733 buffer_put_string(&b, signature, slen);
736 /* skip session id and packet type */
737 if (buffer_len(&b) < skip + 1)
738 fatal("userauth_pubkey: internal error");
739 buffer_consume(&b, skip + 1);
741 /* put remaining data from buffer into packet */
742 packet_start(SSH2_MSG_USERAUTH_REQUEST);
743 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
754 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
755 u_char *data, int datalen)
757 return key_sign(key, sigp, lenp, data, datalen);
761 userauth_pubkey_identity(Authctxt *authctxt, char *filename)
764 int i, ret, try_next, success = 0;
769 if (stat(filename, &st) != 0) {
770 debug("key does not exist: %s", filename);
773 debug("try pubkey: %s", filename);
775 k = key_new(KEY_UNSPEC);
776 if (!load_private_key(filename, "", k, NULL)) {
777 if (options.batch_mode) {
781 snprintf(prompt, sizeof prompt,
782 "Enter passphrase for key '%.100s': ", filename);
783 for (i = 0; i < options.number_of_password_prompts; i++) {
784 passphrase = read_passphrase(prompt, 0);
785 if (strcmp(passphrase, "") != 0) {
786 success = load_private_key(filename, passphrase, k, NULL);
789 debug2("no passphrase given, try next key");
792 memset(passphrase, 0, strlen(passphrase));
794 if (success || try_next)
796 debug2("bad passphrase given, try again...");
803 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
809 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
810 u_char *data, int datalen)
812 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
816 userauth_pubkey_agent(Authctxt *authctxt)
818 static int called = 0;
824 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
825 debug2("userauth_pubkey_agent: no keys at all");
828 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
830 debug2("userauth_pubkey_agent: no more keys");
832 debug("userauth_pubkey_agent: trying agent key %s", comment);
834 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
838 debug2("userauth_pubkey_agent: no message sent");
843 userauth_pubkey(Authctxt *authctxt)
848 if (authctxt->agent != NULL) {
850 sent = userauth_pubkey_agent(authctxt);
851 } while(!sent && authctxt->agent->howmany > 0);
853 while (!sent && idx < options.num_identity_files) {
854 if (options.identity_files_type[idx] != KEY_RSA1)
855 sent = userauth_pubkey_identity(authctxt,
856 options.identity_files[idx]);
863 * Send userauth request message specifying keyboard-interactive method.
866 userauth_kbdint(Authctxt *authctxt)
868 static int attempt = 0;
870 if (attempt++ >= options.number_of_password_prompts)
873 debug2("userauth_kbdint");
874 packet_start(SSH2_MSG_USERAUTH_REQUEST);
875 packet_put_cstring(authctxt->server_user);
876 packet_put_cstring(authctxt->service);
877 packet_put_cstring(authctxt->method->name);
878 packet_put_cstring(""); /* lang */
879 packet_put_cstring(options.kbd_interactive_devices ?
880 options.kbd_interactive_devices : "");
884 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
889 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
892 input_userauth_info_req(int type, int plen, void *ctxt)
894 Authctxt *authctxt = ctxt;
895 char *name, *inst, *lang, *prompt, *response;
896 u_int num_prompts, i;
899 debug2("input_userauth_info_req");
901 if (authctxt == NULL)
902 fatal("input_userauth_info_req: no authentication context");
904 name = packet_get_string(NULL);
905 inst = packet_get_string(NULL);
906 lang = packet_get_string(NULL);
907 if (strlen(name) > 0)
909 if (strlen(inst) > 0)
915 num_prompts = packet_get_int();
917 * Begin to build info response packet based on prompts requested.
918 * We commit to providing the correct number of responses, so if
919 * further on we run into a problem that prevents this, we have to
920 * be sure and clean this up and send a correct error response.
922 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
923 packet_put_int(num_prompts);
925 for (i = 0; i < num_prompts; i++) {
926 prompt = packet_get_string(NULL);
927 echo = packet_get_char();
929 response = cli_prompt(prompt, echo);
931 ssh_put_password(response);
932 memset(response, 0, strlen(response));
936 packet_done(); /* done with parsing incoming message. */
942 /* find auth method */
946 static char *def_authlist = "publickey,password";
947 static char *authlist_current = NULL; /* clean copy used for comparison */
948 static char *authname_current = NULL; /* last used auth method */
949 static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
950 static char *authlist_state = NULL; /* state variable for strtok_r() */
953 * Before starting to use a new authentication method list sent by the
954 * server, reset internal variables. This should also be called when
955 * finished processing server list to free resources.
958 authmethod_clear(void)
960 if (authlist_current != NULL) {
961 xfree(authlist_current);
962 authlist_current = NULL;
964 if (authlist_working != NULL) {
965 xfree(authlist_working);
966 authlist_working = NULL;
968 if (authname_current != NULL) {
969 xfree(authname_current);
970 authname_current = NULL;
972 if (authlist_state != NULL)
973 authlist_state = NULL;
978 * given auth method name, if configurable options permit this method fill
979 * in auth_ident field and return true, otherwise return false.
982 authmethod_is_enabled(Authmethod *method)
986 /* return false if options indicate this method is disabled */
987 if (method->enabled == NULL || *method->enabled == 0)
989 /* return false if batch mode is enabled but method needs interactive mode */
990 if (method->batch_flag != NULL && *method->batch_flag != 0)
996 authmethod_lookup(const char *name)
998 Authmethod *method = NULL;
1000 for (method = authmethods; method->name != NULL; method++)
1001 if (strcmp(name, method->name) == 0)
1003 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1008 * Given the authentication method list sent by the server, return the
1009 * next method we should try. If the server initially sends a nil list,
1010 * use a built-in default list. If the server sends a nil list after
1011 * previously sending a valid list, continue using the list originally
1016 authmethod_get(char *authlist)
1018 char *name = NULL, *authname_old;
1019 Authmethod *method = NULL;
1021 /* Use a suitable default if we're passed a nil list. */
1022 if (authlist == NULL || strlen(authlist) == 0)
1023 authlist = def_authlist;
1025 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1026 /* start over if passed a different list */
1027 debug3("start over, passed a different list");
1029 authlist_current = xstrdup(authlist);
1030 authlist_working = xstrdup(authlist);
1031 name = strtok_r(authlist_working, DELIM, &authlist_state);
1034 * try to use previously used authentication method
1035 * or continue to use previously passed list
1037 name = (authname_current != NULL) ?
1038 authname_current : strtok_r(NULL, DELIM, &authlist_state);
1041 while (name != NULL) {
1042 debug3("authmethod_lookup %s", name);
1043 method = authmethod_lookup(name);
1044 if (method != NULL && authmethod_is_enabled(method)) {
1045 debug3("authmethod_is_enabled %s", name);
1048 name = strtok_r(NULL, DELIM, &authlist_state);
1052 authname_old = authname_current;
1053 if (method != NULL) {
1054 debug("next auth method to try is %s", name);
1055 authname_current = xstrdup(name);
1057 debug("no more auth methods to try");
1058 authname_current = NULL;
1061 if (authname_old != NULL)
1062 xfree(authname_old);