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.33 2000/12/20 19:37:22 markus Exp $");
28 #include <openssl/bn.h>
29 #include <openssl/rsa.h>
30 #include <openssl/dsa.h>
31 #include <openssl/md5.h>
32 #include <openssl/dh.h>
33 #include <openssl/hmac.h>
46 #include "myproposal.h"
48 #include "sshconnect.h"
54 void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
55 void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
66 u_char *session_id2 = NULL;
67 int session_id2_len = 0;
70 ssh_kex2(char *host, struct sockaddr *hostaddr)
74 Buffer *client_kexinit, *server_kexinit;
75 char *sprop[PROPOSAL_MAX];
77 if (options.ciphers == (char *)-1) {
78 log("No valid ciphers for protocol version 2 given, using defaults.");
79 options.ciphers = NULL;
81 if (options.ciphers != NULL) {
82 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
83 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
85 if (options.compression) {
86 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib";
87 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
89 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
90 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
93 /* buffers with raw kexinit messages */
94 server_kexinit = xmalloc(sizeof(*server_kexinit));
95 buffer_init(server_kexinit);
96 client_kexinit = kex_init(myproposal);
98 /* algorithm negotiation */
99 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
100 kex = kex_choose_conf(myproposal, sprop, 0);
101 for (i = 0; i < PROPOSAL_MAX; i++)
104 /* server authentication and session key agreement */
105 switch(kex->kex_type) {
107 ssh_dh1_client(kex, host, hostaddr,
108 client_kexinit, server_kexinit);
111 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
115 fatal("Unsupported key exchange %d", kex->kex_type);
118 buffer_free(client_kexinit);
119 buffer_free(server_kexinit);
120 xfree(client_kexinit);
121 xfree(server_kexinit);
123 debug("Wait SSH2_MSG_NEWKEYS.");
124 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
126 debug("GOT SSH2_MSG_NEWKEYS.");
128 debug("send SSH2_MSG_NEWKEYS.");
129 packet_start(SSH2_MSG_NEWKEYS);
132 debug("done: send SSH2_MSG_NEWKEYS.");
135 /* send 1st encrypted/maced/compressed message */
136 packet_start(SSH2_MSG_IGNORE);
137 packet_put_cstring("markus");
141 debug("done: KEX2.");
144 /* diffie-hellman-group1-sha1 */
147 ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
148 Buffer *client_kexinit, Buffer *server_kexinit)
155 char *signature = NULL;
157 char *server_host_key_blob = NULL;
158 Key *server_host_key;
161 BIGNUM *dh_server_pub = 0;
162 BIGNUM *shared_secret = 0;
166 debug("Sending SSH2_MSG_KEXDH_INIT.");
167 /* generate and send 'e', client DH public key */
168 dh = dh_new_group1();
170 packet_start(SSH2_MSG_KEXDH_INIT);
171 packet_put_bignum2(dh->pub_key);
176 fprintf(stderr, "\np= ");
177 BN_print_fp(stderr, dh->p);
178 fprintf(stderr, "\ng= ");
179 BN_print_fp(stderr, dh->g);
180 fprintf(stderr, "\npub= ");
181 BN_print_fp(stderr, dh->pub_key);
182 fprintf(stderr, "\n");
183 DHparams_print_fp(stderr, dh);
186 debug("Wait SSH2_MSG_KEXDH_REPLY.");
188 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
190 debug("Got SSH2_MSG_KEXDH_REPLY.");
193 server_host_key_blob = packet_get_string(&sbloblen);
194 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
195 if (server_host_key == NULL)
196 fatal("cannot decode server_host_key_blob");
198 check_host_key(host, hostaddr, server_host_key,
199 options.user_hostfile2, options.system_hostfile2);
201 /* DH paramter f, server public DH key */
202 dh_server_pub = BN_new();
203 if (dh_server_pub == NULL)
204 fatal("dh_server_pub == NULL");
205 packet_get_bignum2(dh_server_pub, &dlen);
208 fprintf(stderr, "\ndh_server_pub= ");
209 BN_print_fp(stderr, dh_server_pub);
210 fprintf(stderr, "\n");
211 debug("bits %d", BN_num_bits(dh_server_pub));
215 signature = packet_get_string(&slen);
218 if (!dh_pub_is_valid(dh, dh_server_pub))
219 packet_disconnect("bad server public DH value");
222 kbuf = xmalloc(klen);
223 kout = DH_compute_key(kbuf, dh_server_pub, dh);
225 debug("shared secret: len %d/%d", klen, kout);
226 fprintf(stderr, "shared secret == ");
227 for (i = 0; i< kout; i++)
228 fprintf(stderr, "%02x", (kbuf[i])&0xff);
229 fprintf(stderr, "\n");
231 shared_secret = BN_new();
233 BN_bin2bn(kbuf, kout, shared_secret);
234 memset(kbuf, 0, klen);
237 /* calc and verify H */
239 client_version_string,
240 server_version_string,
241 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
242 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
243 server_host_key_blob, sbloblen,
248 xfree(server_host_key_blob);
251 fprintf(stderr, "hash == ");
252 for (i = 0; i< 20; i++)
253 fprintf(stderr, "%02x", (hash[i])&0xff);
254 fprintf(stderr, "\n");
256 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
257 fatal("key_verify failed for server_host_key");
258 key_free(server_host_key);
260 kex_derive_keys(kex, hash, shared_secret);
263 /* save session id */
264 session_id2_len = 20;
265 session_id2 = xmalloc(session_id2_len);
266 memcpy(session_id2, hash, session_id2_len);
269 /* diffie-hellman-group-exchange-sha1 */
272 * Estimates the group order for a Diffie-Hellman group that has an
273 * attack complexity approximately the same as O(2**bits). Estimate
274 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
278 dh_estimate(int bits)
282 return (512); /* O(2**63) */
284 return (1024); /* O(2**86) */
286 return (2048); /* O(2**116) */
287 return (4096); /* O(2**156) */
291 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
292 Buffer *client_kexinit, Buffer *server_kexinit)
299 char *signature = NULL;
301 char *server_host_key_blob = NULL;
302 Key *server_host_key;
305 BIGNUM *dh_server_pub = 0;
306 BIGNUM *shared_secret = 0;
307 BIGNUM *p = 0, *g = 0;
311 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
313 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
314 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
315 packet_put_int(nbits);
320 fprintf(stderr, "\nnbits = %d", nbits);
323 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
325 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
327 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
329 if ((p = BN_new()) == NULL)
331 packet_get_bignum2(p, &dlen);
332 if ((g = BN_new()) == NULL)
334 packet_get_bignum2(g, &dlen);
335 if ((dh = dh_new_group(g, p)) == NULL)
336 fatal("dh_new_group");
341 fprintf(stderr, "\np= ");
342 BN_print_fp(stderr, dh->p);
343 fprintf(stderr, "\ng= ");
344 BN_print_fp(stderr, dh->g);
345 fprintf(stderr, "\npub= ");
346 BN_print_fp(stderr, dh->pub_key);
347 fprintf(stderr, "\n");
348 DHparams_print_fp(stderr, dh);
351 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
352 /* generate and send 'e', client DH public key */
353 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
354 packet_put_bignum2(dh->pub_key);
358 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
360 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
362 debug("Got SSH2_MSG_KEXDH_REPLY.");
365 server_host_key_blob = packet_get_string(&sbloblen);
366 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
367 if (server_host_key == NULL)
368 fatal("cannot decode server_host_key_blob");
370 check_host_key(host, hostaddr, server_host_key,
371 options.user_hostfile2, options.system_hostfile2);
373 /* DH paramter f, server public DH key */
374 dh_server_pub = BN_new();
375 if (dh_server_pub == NULL)
376 fatal("dh_server_pub == NULL");
377 packet_get_bignum2(dh_server_pub, &dlen);
380 fprintf(stderr, "\ndh_server_pub= ");
381 BN_print_fp(stderr, dh_server_pub);
382 fprintf(stderr, "\n");
383 debug("bits %d", BN_num_bits(dh_server_pub));
387 signature = packet_get_string(&slen);
390 if (!dh_pub_is_valid(dh, dh_server_pub))
391 packet_disconnect("bad server public DH value");
394 kbuf = xmalloc(klen);
395 kout = DH_compute_key(kbuf, dh_server_pub, dh);
397 debug("shared secret: len %d/%d", klen, kout);
398 fprintf(stderr, "shared secret == ");
399 for (i = 0; i< kout; i++)
400 fprintf(stderr, "%02x", (kbuf[i])&0xff);
401 fprintf(stderr, "\n");
403 shared_secret = BN_new();
405 BN_bin2bn(kbuf, kout, shared_secret);
406 memset(kbuf, 0, klen);
409 /* calc and verify H */
411 client_version_string,
412 server_version_string,
413 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
414 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
415 server_host_key_blob, sbloblen,
421 xfree(server_host_key_blob);
424 fprintf(stderr, "hash == ");
425 for (i = 0; i< 20; i++)
426 fprintf(stderr, "%02x", (hash[i])&0xff);
427 fprintf(stderr, "\n");
429 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
430 fatal("key_verify failed for server_host_key");
431 key_free(server_host_key);
433 kex_derive_keys(kex, hash, shared_secret);
436 /* save session id */
437 session_id2_len = 20;
438 session_id2 = xmalloc(session_id2_len);
439 memcpy(session_id2, hash, session_id2_len);
446 typedef struct Authctxt Authctxt;
447 typedef struct Authmethod Authmethod;
449 typedef int sign_cb_fn(
450 Authctxt *authctxt, Key *key,
451 u_char **sigp, int *lenp, u_char *data, int datalen);
454 const char *server_user;
457 AuthenticationConnection *agent;
462 char *name; /* string to compare against server's list */
463 int (*userauth)(Authctxt *authctxt);
464 int *enabled; /* flag in option struct that enables method */
465 int *batch_flag; /* flag in option struct that disables method */
468 void input_userauth_success(int type, int plen, void *ctxt);
469 void input_userauth_failure(int type, int plen, void *ctxt);
470 void input_userauth_error(int type, int plen, void *ctxt);
471 void input_userauth_info_req(int type, int plen, void *ctxt);
473 int userauth_none(Authctxt *authctxt);
474 int userauth_pubkey(Authctxt *authctxt);
475 int userauth_passwd(Authctxt *authctxt);
476 int userauth_kbdint(Authctxt *authctxt);
478 void authmethod_clear(void);
479 Authmethod *authmethod_get(char *authlist);
480 Authmethod *authmethod_lookup(const char *name);
482 Authmethod authmethods[] = {
485 &options.pubkey_authentication,
489 &options.password_authentication,
490 &options.batch_mode},
491 {"keyboard-interactive",
493 &options.kbd_interactive_authentication,
494 &options.batch_mode},
499 {NULL, NULL, NULL, NULL}
503 ssh_userauth2(const char *server_user, char *host)
509 debug("send SSH2_MSG_SERVICE_REQUEST");
510 packet_start(SSH2_MSG_SERVICE_REQUEST);
511 packet_put_cstring("ssh-userauth");
514 type = packet_read(&plen);
515 if (type != SSH2_MSG_SERVICE_ACCEPT) {
516 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
518 if (packet_remaining() > 0) {
519 char *reply = packet_get_string(&plen);
520 debug("service_accept: %s", reply);
524 debug("buggy server: service_accept w/o service");
527 debug("got SSH2_MSG_SERVICE_ACCEPT");
529 /* setup authentication context */
530 authctxt.agent = ssh_get_authentication_connection();
531 authctxt.server_user = server_user;
532 authctxt.host = host;
533 authctxt.service = "ssh-connection"; /* service name */
534 authctxt.success = 0;
535 authctxt.method = authmethod_lookup("none");
536 if (authctxt.method == NULL)
537 fatal("ssh_userauth2: internal error: cannot send userauth none request");
540 /* initial userauth request */
541 userauth_none(&authctxt);
543 dispatch_init(&input_userauth_error);
544 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
545 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
546 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
548 if (authctxt.agent != NULL)
549 ssh_close_authentication_connection(authctxt.agent);
551 debug("ssh-userauth2 successfull: method %s", authctxt.method->name);
554 input_userauth_error(int type, int plen, void *ctxt)
556 fatal("input_userauth_error: bad message during authentication");
559 input_userauth_success(int type, int plen, void *ctxt)
561 Authctxt *authctxt = ctxt;
562 if (authctxt == NULL)
563 fatal("input_userauth_success: no authentication context");
564 authctxt->success = 1; /* break out */
567 input_userauth_failure(int type, int plen, void *ctxt)
569 Authmethod *method = NULL;
570 Authctxt *authctxt = ctxt;
571 char *authlist = NULL;
574 if (authctxt == NULL)
575 fatal("input_userauth_failure: no authentication context");
577 authlist = packet_get_string(NULL);
578 partial = packet_get_char();
582 debug("partial success");
583 debug("authentications that can continue: %s", authlist);
586 method = authmethod_get(authlist);
588 fatal("Unable to find an authentication method");
589 authctxt->method = method;
590 if (method->userauth(authctxt) != 0) {
591 debug2("we sent a %s packet, wait for reply", method->name);
594 debug2("we did not send a packet, disable method");
595 method->enabled = NULL;
602 userauth_none(Authctxt *authctxt)
604 /* initial userauth request */
605 packet_start(SSH2_MSG_USERAUTH_REQUEST);
606 packet_put_cstring(authctxt->server_user);
607 packet_put_cstring(authctxt->service);
608 packet_put_cstring(authctxt->method->name);
615 userauth_passwd(Authctxt *authctxt)
617 static int attempt = 0;
621 if (attempt++ >= options.number_of_password_prompts)
625 error("Permission denied, please try again.");
627 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
628 authctxt->server_user, authctxt->host);
629 password = read_passphrase(prompt, 0);
630 packet_start(SSH2_MSG_USERAUTH_REQUEST);
631 packet_put_cstring(authctxt->server_user);
632 packet_put_cstring(authctxt->service);
633 packet_put_cstring(authctxt->method->name);
635 packet_put_cstring(password);
636 memset(password, 0, strlen(password));
644 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
647 u_char *blob, *signature;
653 debug3("sign_and_send_pubkey");
654 if (key_to_blob(k, &blob, &bloblen) == 0) {
655 /* we cannot handle this key */
656 debug3("sign_and_send_pubkey: cannot handle key");
659 /* data to be signed */
661 if (datafellows & SSH_OLD_SESSIONID) {
662 buffer_append(&b, session_id2, session_id2_len);
663 skip = session_id2_len;
665 buffer_put_string(&b, session_id2, session_id2_len);
666 skip = buffer_len(&b);
668 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
669 buffer_put_cstring(&b, authctxt->server_user);
670 buffer_put_cstring(&b,
671 datafellows & SSH_BUG_PKSERVICE ?
674 if (datafellows & SSH_BUG_PKAUTH) {
675 buffer_put_char(&b, have_sig);
677 buffer_put_cstring(&b, authctxt->method->name);
678 buffer_put_char(&b, have_sig);
679 buffer_put_cstring(&b, key_ssh_name(k));
681 buffer_put_string(&b, blob, bloblen);
683 /* generate signature */
684 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
693 if (datafellows & SSH_BUG_PKSERVICE) {
695 buffer_append(&b, session_id2, session_id2_len);
696 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
697 buffer_put_cstring(&b, authctxt->server_user);
698 buffer_put_cstring(&b, authctxt->service);
699 buffer_put_cstring(&b, authctxt->method->name);
700 buffer_put_char(&b, have_sig);
701 if (!(datafellows & SSH_BUG_PKAUTH))
702 buffer_put_cstring(&b, key_ssh_name(k));
703 buffer_put_string(&b, blob, bloblen);
706 /* append signature */
707 buffer_put_string(&b, signature, slen);
710 /* skip session id and packet type */
711 if (buffer_len(&b) < skip + 1)
712 fatal("userauth_pubkey: internal error");
713 buffer_consume(&b, skip + 1);
715 /* put remaining data from buffer into packet */
716 packet_start(SSH2_MSG_USERAUTH_REQUEST);
717 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
728 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
729 u_char *data, int datalen)
731 return key_sign(key, sigp, lenp, data, datalen);
735 userauth_pubkey_identity(Authctxt *authctxt, char *filename)
738 int i, ret, try_next;
741 if (stat(filename, &st) != 0) {
742 debug("key does not exist: %s", filename);
745 debug("try pubkey: %s", filename);
747 k = key_new(KEY_UNSPEC);
748 if (!load_private_key(filename, "", k, NULL)) {
752 snprintf(prompt, sizeof prompt,
753 "Enter passphrase for key '%.100s': ", filename);
754 for (i = 0; i < options.number_of_password_prompts; i++) {
755 passphrase = read_passphrase(prompt, 0);
756 if (strcmp(passphrase, "") != 0) {
757 success = load_private_key(filename, passphrase, k, NULL);
760 debug2("no passphrase given, try next key");
763 memset(passphrase, 0, strlen(passphrase));
765 if (success || try_next)
767 debug2("bad passphrase given, try again...");
774 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
780 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
781 u_char *data, int datalen)
783 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
787 userauth_pubkey_agent(Authctxt *authctxt)
789 static int called = 0;
795 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
796 debug2("userauth_pubkey_agent: no keys at all");
799 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
801 debug2("userauth_pubkey_agent: no more keys");
803 debug("userauth_pubkey_agent: trying agent key %s", comment);
805 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
809 debug2("userauth_pubkey_agent: no message sent");
814 userauth_pubkey(Authctxt *authctxt)
819 if (authctxt->agent != NULL) {
821 sent = userauth_pubkey_agent(authctxt);
822 } while(!sent && authctxt->agent->howmany > 0);
824 while (!sent && idx < options.num_identity_files) {
825 if (options.identity_files_type[idx] != KEY_RSA1)
826 sent = userauth_pubkey_identity(authctxt,
827 options.identity_files[idx]);
834 * Send userauth request message specifying keyboard-interactive method.
837 userauth_kbdint(Authctxt *authctxt)
839 static int attempt = 0;
841 if (attempt++ >= options.number_of_password_prompts)
844 debug2("userauth_kbdint");
845 packet_start(SSH2_MSG_USERAUTH_REQUEST);
846 packet_put_cstring(authctxt->server_user);
847 packet_put_cstring(authctxt->service);
848 packet_put_cstring(authctxt->method->name);
849 packet_put_cstring(""); /* lang */
850 packet_put_cstring(options.kbd_interactive_devices ?
851 options.kbd_interactive_devices : "");
855 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
860 * parse SSH2_MSG_USERAUTH_INFO_REQUEST, prompt user and send
861 * SSH2_MSG_USERAUTH_INFO_RESPONSE
864 input_userauth_info_req(int type, int plen, void *ctxt)
866 Authctxt *authctxt = ctxt;
871 char *response = NULL;
872 u_int num_prompts, i;
875 debug2("input_userauth_info_req");
877 if (authctxt == NULL)
878 fatal("input_userauth_info_req: no authentication context");
880 name = packet_get_string(NULL);
881 inst = packet_get_string(NULL);
882 lang = packet_get_string(NULL);
884 if (strlen(name) > 0)
888 if (strlen(inst) > 0)
891 xfree(lang); /* unused */
893 num_prompts = packet_get_int();
895 * Begin to build info response packet based on prompts requested.
896 * We commit to providing the correct number of responses, so if
897 * further on we run into a problem that prevents this, we have to
898 * be sure and clean this up and send a correct error response.
900 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
901 packet_put_int(num_prompts);
903 for (i = 0; i < num_prompts; i++) {
904 prompt = packet_get_string(NULL);
905 echo = packet_get_char();
907 response = cli_prompt(prompt, echo);
909 packet_put_cstring(response);
910 memset(response, 0, strlen(response));
914 packet_done(); /* done with parsing incoming message. */
920 /* find auth method */
924 static char *def_authlist = "publickey,password";
925 static char *authlist_current = NULL; /* clean copy used for comparison */
926 static char *authname_current = NULL; /* last used auth method */
927 static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
928 static char *authlist_state = NULL; /* state variable for strtok_r() */
931 * Before starting to use a new authentication method list sent by the
932 * server, reset internal variables. This should also be called when
933 * finished processing server list to free resources.
936 authmethod_clear(void)
938 if (authlist_current != NULL) {
939 xfree(authlist_current);
940 authlist_current = NULL;
942 if (authlist_working != NULL) {
943 xfree(authlist_working);
944 authlist_working = NULL;
946 if (authname_current != NULL) {
947 xfree(authname_current);
948 authlist_state = NULL;
950 if (authlist_state != NULL)
951 authlist_state = NULL;
956 * given auth method name, if configurable options permit this method fill
957 * in auth_ident field and return true, otherwise return false.
960 authmethod_is_enabled(Authmethod *method)
964 /* return false if options indicate this method is disabled */
965 if (method->enabled == NULL || *method->enabled == 0)
967 /* return false if batch mode is enabled but method needs interactive mode */
968 if (method->batch_flag != NULL && *method->batch_flag != 0)
974 authmethod_lookup(const char *name)
976 Authmethod *method = NULL;
978 for (method = authmethods; method->name != NULL; method++)
979 if (strcmp(name, method->name) == 0)
981 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
986 * Given the authentication method list sent by the server, return the
987 * next method we should try. If the server initially sends a nil list,
988 * use a built-in default list. If the server sends a nil list after
989 * previously sending a valid list, continue using the list originally
994 authmethod_get(char *authlist)
996 char *name = NULL, *authname_old;
997 Authmethod *method = NULL;
999 /* Use a suitable default if we're passed a nil list. */
1000 if (authlist == NULL || strlen(authlist) == 0)
1001 authlist = def_authlist;
1003 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1004 /* start over if passed a different list */
1005 debug3("start over, passed a different list");
1007 authlist_current = xstrdup(authlist);
1008 authlist_working = xstrdup(authlist);
1009 name = strtok_r(authlist_working, DELIM, &authlist_state);
1012 * try to use previously used authentication method
1013 * or continue to use previously passed list
1015 name = (authname_current != NULL) ?
1016 authname_current : strtok_r(NULL, DELIM, &authlist_state);
1019 while (name != NULL) {
1020 debug3("authmethod_lookup %s", name);
1021 method = authmethod_lookup(name);
1022 if (method != NULL && authmethod_is_enabled(method)) {
1023 debug3("authmethod_is_enabled %s", name);
1026 name = strtok_r(NULL, DELIM, &authlist_state);
1030 authname_old = authname_current;
1031 if (method != NULL) {
1032 debug("next auth method to try is %s", name);
1033 authname_current = xstrdup(name);
1035 debug("no more auth methods to try");
1036 authname_current = NULL;
1039 if (authname_old != NULL)
1040 xfree(authname_old);