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.39 2001/01/22 23:06:40 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] = "zlib";
88 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
90 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
91 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
94 /* buffers with raw kexinit messages */
95 server_kexinit = xmalloc(sizeof(*server_kexinit));
96 buffer_init(server_kexinit);
97 client_kexinit = kex_init(myproposal);
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++)
105 /* server authentication and session key agreement */
106 switch(kex->kex_type) {
108 ssh_dh1_client(kex, host, hostaddr,
109 client_kexinit, server_kexinit);
112 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
116 fatal("Unsupported key exchange %d", kex->kex_type);
119 buffer_free(client_kexinit);
120 buffer_free(server_kexinit);
121 xfree(client_kexinit);
122 xfree(server_kexinit);
124 debug("Wait SSH2_MSG_NEWKEYS.");
125 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
127 debug("GOT SSH2_MSG_NEWKEYS.");
129 debug("send SSH2_MSG_NEWKEYS.");
130 packet_start(SSH2_MSG_NEWKEYS);
133 debug("done: send SSH2_MSG_NEWKEYS.");
136 /* send 1st encrypted/maced/compressed message */
137 packet_start(SSH2_MSG_IGNORE);
138 packet_put_cstring("markus");
142 debug("done: KEX2.");
145 /* diffie-hellman-group1-sha1 */
148 ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
149 Buffer *client_kexinit, Buffer *server_kexinit)
156 char *signature = NULL;
158 char *server_host_key_blob = NULL;
159 Key *server_host_key;
162 BIGNUM *dh_server_pub = 0;
163 BIGNUM *shared_secret = 0;
167 debug("Sending SSH2_MSG_KEXDH_INIT.");
168 /* generate and send 'e', client DH public key */
169 dh = dh_new_group1();
171 packet_start(SSH2_MSG_KEXDH_INIT);
172 packet_put_bignum2(dh->pub_key);
177 fprintf(stderr, "\np= ");
178 BN_print_fp(stderr, dh->p);
179 fprintf(stderr, "\ng= ");
180 BN_print_fp(stderr, dh->g);
181 fprintf(stderr, "\npub= ");
182 BN_print_fp(stderr, dh->pub_key);
183 fprintf(stderr, "\n");
184 DHparams_print_fp(stderr, dh);
187 debug("Wait SSH2_MSG_KEXDH_REPLY.");
189 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
191 debug("Got SSH2_MSG_KEXDH_REPLY.");
194 server_host_key_blob = packet_get_string(&sbloblen);
195 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
196 if (server_host_key == NULL)
197 fatal("cannot decode server_host_key_blob");
199 check_host_key(host, hostaddr, server_host_key,
200 options.user_hostfile2, options.system_hostfile2);
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);
209 fprintf(stderr, "\ndh_server_pub= ");
210 BN_print_fp(stderr, dh_server_pub);
211 fprintf(stderr, "\n");
212 debug("bits %d", BN_num_bits(dh_server_pub));
216 signature = packet_get_string(&slen);
219 if (!dh_pub_is_valid(dh, dh_server_pub))
220 packet_disconnect("bad server public DH value");
223 kbuf = xmalloc(klen);
224 kout = DH_compute_key(kbuf, dh_server_pub, dh);
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");
232 shared_secret = BN_new();
234 BN_bin2bn(kbuf, kout, shared_secret);
235 memset(kbuf, 0, klen);
238 /* calc and verify H */
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,
249 xfree(server_host_key_blob);
251 BN_free(dh_server_pub);
253 fprintf(stderr, "hash == ");
254 for (i = 0; i< 20; i++)
255 fprintf(stderr, "%02x", (hash[i])&0xff);
256 fprintf(stderr, "\n");
258 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
259 fatal("key_verify failed for server_host_key");
260 key_free(server_host_key);
263 kex_derive_keys(kex, hash, shared_secret);
264 BN_clear_free(shared_secret);
267 /* save session id */
268 session_id2_len = 20;
269 session_id2 = xmalloc(session_id2_len);
270 memcpy(session_id2, hash, session_id2_len);
273 /* diffie-hellman-group-exchange-sha1 */
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)))
282 dh_estimate(int bits)
286 return (512); /* O(2**63) */
288 return (1024); /* O(2**86) */
290 return (2048); /* O(2**116) */
291 return (4096); /* O(2**156) */
295 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
296 Buffer *client_kexinit, Buffer *server_kexinit)
303 char *signature = NULL;
305 char *server_host_key_blob = NULL;
306 Key *server_host_key;
309 BIGNUM *dh_server_pub = 0;
310 BIGNUM *shared_secret = 0;
311 BIGNUM *p = 0, *g = 0;
315 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
317 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
318 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
319 packet_put_int(nbits);
324 fprintf(stderr, "\nnbits = %d", nbits);
327 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
329 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
331 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
333 if ((p = BN_new()) == NULL)
335 packet_get_bignum2(p, &dlen);
336 if ((g = BN_new()) == NULL)
338 packet_get_bignum2(g, &dlen);
339 dh = dh_new_group(g, p);
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);
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);
361 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
363 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
365 debug("Got SSH2_MSG_KEXDH_REPLY.");
368 server_host_key_blob = packet_get_string(&sbloblen);
369 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
370 if (server_host_key == NULL)
371 fatal("cannot decode server_host_key_blob");
373 check_host_key(host, hostaddr, server_host_key,
374 options.user_hostfile2, options.system_hostfile2);
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);
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));
390 signature = packet_get_string(&slen);
393 if (!dh_pub_is_valid(dh, dh_server_pub))
394 packet_disconnect("bad server public DH value");
397 kbuf = xmalloc(klen);
398 kout = DH_compute_key(kbuf, dh_server_pub, dh);
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");
406 shared_secret = BN_new();
408 BN_bin2bn(kbuf, kout, shared_secret);
409 memset(kbuf, 0, klen);
412 /* calc and verify H */
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,
424 xfree(server_host_key_blob);
426 BN_free(dh_server_pub);
428 fprintf(stderr, "hash == ");
429 for (i = 0; i< 20; i++)
430 fprintf(stderr, "%02x", (hash[i])&0xff);
431 fprintf(stderr, "\n");
433 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
434 fatal("key_verify failed for server_host_key");
435 key_free(server_host_key);
438 kex_derive_keys(kex, hash, shared_secret);
439 BN_clear_free(shared_secret);
442 /* save session id */
443 session_id2_len = 20;
444 session_id2 = xmalloc(session_id2_len);
445 memcpy(session_id2, hash, session_id2_len);
452 typedef struct Authctxt Authctxt;
453 typedef struct Authmethod Authmethod;
455 typedef int sign_cb_fn(
456 Authctxt *authctxt, Key *key,
457 u_char **sigp, int *lenp, u_char *data, int datalen);
460 const char *server_user;
463 AuthenticationConnection *agent;
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 */
474 void input_userauth_success(int type, int plen, void *ctxt);
475 void input_userauth_failure(int type, int plen, void *ctxt);
476 void input_userauth_banner(int type, int plen, void *ctxt);
477 void input_userauth_error(int type, int plen, void *ctxt);
478 void input_userauth_info_req(int type, int plen, void *ctxt);
480 int userauth_none(Authctxt *authctxt);
481 int userauth_pubkey(Authctxt *authctxt);
482 int userauth_passwd(Authctxt *authctxt);
483 int userauth_kbdint(Authctxt *authctxt);
485 void authmethod_clear(void);
486 Authmethod *authmethod_get(char *authlist);
487 Authmethod *authmethod_lookup(const char *name);
489 Authmethod authmethods[] = {
492 &options.pubkey_authentication,
496 &options.password_authentication,
497 &options.batch_mode},
498 {"keyboard-interactive",
500 &options.kbd_interactive_authentication,
501 &options.batch_mode},
506 {NULL, NULL, NULL, NULL}
510 ssh_userauth2(const char *server_user, char *host)
516 if (options.challenge_reponse_authentication)
517 options.kbd_interactive_authentication = 1;
519 debug("send SSH2_MSG_SERVICE_REQUEST");
520 packet_start(SSH2_MSG_SERVICE_REQUEST);
521 packet_put_cstring("ssh-userauth");
524 type = packet_read(&plen);
525 if (type != SSH2_MSG_SERVICE_ACCEPT) {
526 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
528 if (packet_remaining() > 0) {
529 char *reply = packet_get_string(&plen);
530 debug("service_accept: %s", reply);
534 debug("buggy server: service_accept w/o service");
537 debug("got SSH2_MSG_SERVICE_ACCEPT");
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;
545 authctxt.method = authmethod_lookup("none");
546 if (authctxt.method == NULL)
547 fatal("ssh_userauth2: internal error: cannot send userauth none request");
550 /* initial userauth request */
551 userauth_none(&authctxt);
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);
556 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
557 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
559 if (authctxt.agent != NULL)
560 ssh_close_authentication_connection(authctxt.agent);
562 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
565 input_userauth_error(int type, int plen, void *ctxt)
567 fatal("input_userauth_error: bad message during authentication: "
571 input_userauth_banner(int type, int plen, void *ctxt)
574 debug3("input_userauth_banner");
575 msg = packet_get_string(NULL);
576 lang = packet_get_string(NULL);
577 fprintf(stderr, "%s", msg);
582 input_userauth_success(int type, int plen, void *ctxt)
584 Authctxt *authctxt = ctxt;
585 if (authctxt == NULL)
586 fatal("input_userauth_success: no authentication context");
587 authctxt->success = 1; /* break out */
590 input_userauth_failure(int type, int plen, void *ctxt)
592 Authmethod *method = NULL;
593 Authctxt *authctxt = ctxt;
594 char *authlist = NULL;
597 if (authctxt == NULL)
598 fatal("input_userauth_failure: no authentication context");
600 authlist = packet_get_string(NULL);
601 partial = packet_get_char();
605 debug("partial success");
606 debug("authentications that can continue: %s", authlist);
609 method = authmethod_get(authlist);
611 fatal("Unable to find an authentication method");
612 authctxt->method = method;
613 if (method->userauth(authctxt) != 0) {
614 debug2("we sent a %s packet, wait for reply", method->name);
617 debug2("we did not send a packet, disable method");
618 method->enabled = NULL;
625 userauth_none(Authctxt *authctxt)
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);
638 userauth_passwd(Authctxt *authctxt)
640 static int attempt = 0;
644 if (attempt++ >= options.number_of_password_prompts)
648 error("Permission denied, please try again.");
650 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
651 authctxt->server_user, authctxt->host);
652 password = read_passphrase(prompt, 0);
653 packet_start(SSH2_MSG_USERAUTH_REQUEST);
654 packet_put_cstring(authctxt->server_user);
655 packet_put_cstring(authctxt->service);
656 packet_put_cstring(authctxt->method->name);
658 packet_put_cstring(password);
659 memset(password, 0, strlen(password));
667 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
670 u_char *blob, *signature;
676 debug3("sign_and_send_pubkey");
677 if (key_to_blob(k, &blob, &bloblen) == 0) {
678 /* we cannot handle this key */
679 debug3("sign_and_send_pubkey: cannot handle key");
682 /* data to be signed */
684 if (datafellows & SSH_OLD_SESSIONID) {
685 buffer_append(&b, session_id2, session_id2_len);
686 skip = session_id2_len;
688 buffer_put_string(&b, session_id2, session_id2_len);
689 skip = buffer_len(&b);
691 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
692 buffer_put_cstring(&b, authctxt->server_user);
693 buffer_put_cstring(&b,
694 datafellows & SSH_BUG_PKSERVICE ?
697 if (datafellows & SSH_BUG_PKAUTH) {
698 buffer_put_char(&b, have_sig);
700 buffer_put_cstring(&b, authctxt->method->name);
701 buffer_put_char(&b, have_sig);
702 buffer_put_cstring(&b, key_ssh_name(k));
704 buffer_put_string(&b, blob, bloblen);
706 /* generate signature */
707 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
716 if (datafellows & SSH_BUG_PKSERVICE) {
718 buffer_append(&b, session_id2, session_id2_len);
719 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
720 buffer_put_cstring(&b, authctxt->server_user);
721 buffer_put_cstring(&b, authctxt->service);
722 buffer_put_cstring(&b, authctxt->method->name);
723 buffer_put_char(&b, have_sig);
724 if (!(datafellows & SSH_BUG_PKAUTH))
725 buffer_put_cstring(&b, key_ssh_name(k));
726 buffer_put_string(&b, blob, bloblen);
729 /* append signature */
730 buffer_put_string(&b, signature, slen);
733 /* skip session id and packet type */
734 if (buffer_len(&b) < skip + 1)
735 fatal("userauth_pubkey: internal error");
736 buffer_consume(&b, skip + 1);
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));
751 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
752 u_char *data, int datalen)
754 return key_sign(key, sigp, lenp, data, datalen);
758 userauth_pubkey_identity(Authctxt *authctxt, char *filename)
761 int i, ret, try_next;
764 if (stat(filename, &st) != 0) {
765 debug("key does not exist: %s", filename);
768 debug("try pubkey: %s", filename);
770 k = key_new(KEY_UNSPEC);
771 if (!load_private_key(filename, "", k, NULL)) {
775 snprintf(prompt, sizeof prompt,
776 "Enter passphrase for key '%.100s': ", filename);
777 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);
783 debug2("no passphrase given, try next key");
786 memset(passphrase, 0, strlen(passphrase));
788 if (success || try_next)
790 debug2("bad passphrase given, try again...");
797 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
803 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
804 u_char *data, int datalen)
806 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
810 userauth_pubkey_agent(Authctxt *authctxt)
812 static int called = 0;
818 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
819 debug2("userauth_pubkey_agent: no keys at all");
822 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
824 debug2("userauth_pubkey_agent: no more keys");
826 debug("userauth_pubkey_agent: trying agent key %s", comment);
828 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
832 debug2("userauth_pubkey_agent: no message sent");
837 userauth_pubkey(Authctxt *authctxt)
842 if (authctxt->agent != NULL) {
844 sent = userauth_pubkey_agent(authctxt);
845 } while(!sent && authctxt->agent->howmany > 0);
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]);
857 * Send userauth request message specifying keyboard-interactive method.
860 userauth_kbdint(Authctxt *authctxt)
862 static int attempt = 0;
864 if (attempt++ >= options.number_of_password_prompts)
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 : "");
878 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
883 * parse SSH2_MSG_USERAUTH_INFO_REQUEST, prompt user and send
884 * SSH2_MSG_USERAUTH_INFO_RESPONSE
887 input_userauth_info_req(int type, int plen, void *ctxt)
889 Authctxt *authctxt = ctxt;
894 char *response = NULL;
895 u_int num_prompts, i;
898 debug2("input_userauth_info_req");
900 if (authctxt == NULL)
901 fatal("input_userauth_info_req: no authentication context");
903 name = packet_get_string(NULL);
904 inst = packet_get_string(NULL);
905 lang = packet_get_string(NULL);
907 if (strlen(name) > 0)
911 if (strlen(inst) > 0)
914 xfree(lang); /* unused */
916 num_prompts = packet_get_int();
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.
923 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
924 packet_put_int(num_prompts);
926 for (i = 0; i < num_prompts; i++) {
927 prompt = packet_get_string(NULL);
928 echo = packet_get_char();
930 response = cli_prompt(prompt, echo);
932 packet_put_cstring(response);
933 memset(response, 0, strlen(response));
937 packet_done(); /* done with parsing incoming message. */
943 /* find auth method */
947 static char *def_authlist = "publickey,password";
948 static char *authlist_current = NULL; /* clean copy used for comparison */
949 static char *authname_current = NULL; /* last used auth method */
950 static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
951 static char *authlist_state = NULL; /* state variable for strtok_r() */
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.
959 authmethod_clear(void)
961 if (authlist_current != NULL) {
962 xfree(authlist_current);
963 authlist_current = NULL;
965 if (authlist_working != NULL) {
966 xfree(authlist_working);
967 authlist_working = NULL;
969 if (authname_current != NULL) {
970 xfree(authname_current);
971 authlist_state = NULL;
973 if (authlist_state != NULL)
974 authlist_state = NULL;
979 * given auth method name, if configurable options permit this method fill
980 * in auth_ident field and return true, otherwise return false.
983 authmethod_is_enabled(Authmethod *method)
987 /* return false if options indicate this method is disabled */
988 if (method->enabled == NULL || *method->enabled == 0)
990 /* return false if batch mode is enabled but method needs interactive mode */
991 if (method->batch_flag != NULL && *method->batch_flag != 0)
997 authmethod_lookup(const char *name)
999 Authmethod *method = NULL;
1001 for (method = authmethods; method->name != NULL; method++)
1002 if (strcmp(name, method->name) == 0)
1004 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
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
1017 authmethod_get(char *authlist)
1019 char *name = NULL, *authname_old;
1020 Authmethod *method = NULL;
1022 /* Use a suitable default if we're passed a nil list. */
1023 if (authlist == NULL || strlen(authlist) == 0)
1024 authlist = def_authlist;
1026 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1027 /* start over if passed a different list */
1028 debug3("start over, passed a different list");
1030 authlist_current = xstrdup(authlist);
1031 authlist_working = xstrdup(authlist);
1032 name = strtok_r(authlist_working, DELIM, &authlist_state);
1035 * try to use previously used authentication method
1036 * or continue to use previously passed list
1038 name = (authname_current != NULL) ?
1039 authname_current : strtok_r(NULL, DELIM, &authlist_state);
1042 while (name != NULL) {
1043 debug3("authmethod_lookup %s", name);
1044 method = authmethod_lookup(name);
1045 if (method != NULL && authmethod_is_enabled(method)) {
1046 debug3("authmethod_is_enabled %s", name);
1049 name = strtok_r(NULL, DELIM, &authlist_state);
1053 authname_old = authname_current;
1054 if (method != NULL) {
1055 debug("next auth method to try is %s", name);
1056 authname_current = xstrdup(name);
1058 debug("no more auth methods to try");
1059 authname_current = NULL;
1062 if (authname_old != NULL)
1063 xfree(authname_old);