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.107 2002/07/01 19:48:46 markus Exp $");
27 RCSID("$FreeBSD: src/crypto/openssh/sshconnect2.c,v 1.1.1.2.2.7 2003/02/03 17:31:08 des Exp $");
38 #include "myproposal.h"
39 #include "sshconnect.h"
50 #include "pathnames.h"
53 extern char *client_version_string;
54 extern char *server_version_string;
55 extern Options options;
61 u_char *session_id2 = NULL;
62 int session_id2_len = 0;
65 struct sockaddr *xxx_hostaddr;
70 verify_host_key_callback(Key *hostkey)
72 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
73 fatal("Host key verification failed.");
78 ssh_kex2(char *host, struct sockaddr *hostaddr)
83 xxx_hostaddr = hostaddr;
85 if (options.ciphers == (char *)-1) {
86 log("No valid ciphers for protocol version 2 given, using defaults.");
87 options.ciphers = NULL;
89 if (options.ciphers != NULL) {
90 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
91 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
93 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
94 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
95 myproposal[PROPOSAL_ENC_ALGS_STOC] =
96 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
97 if (options.compression) {
98 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
99 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
101 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
102 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
104 if (options.macs != NULL) {
105 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
106 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
108 if (options.hostkeyalgorithms != NULL)
109 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
110 options.hostkeyalgorithms;
112 /* start key exchange */
113 kex = kex_setup(myproposal);
114 kex->client_version_string=client_version_string;
115 kex->server_version_string=server_version_string;
116 kex->verify_host_key=&verify_host_key_callback;
120 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
122 session_id2 = kex->session_id;
123 session_id2_len = kex->session_id_len;
126 /* send 1st encrypted/maced/compressed message */
127 packet_start(SSH2_MSG_IGNORE);
128 packet_put_cstring("markus");
132 debug("done: ssh_kex2.");
139 typedef struct Authctxt Authctxt;
140 typedef struct Authmethod Authmethod;
142 typedef int sign_cb_fn(
143 Authctxt *authctxt, Key *key,
144 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
147 const char *server_user;
148 const char *local_user;
156 sign_cb_fn *last_key_sign;
158 AuthenticationConnection *agent;
160 Sensitive *sensitive;
161 /* kbd-interactive */
165 char *name; /* string to compare against server's list */
166 int (*userauth)(Authctxt *authctxt);
167 int *enabled; /* flag in option struct that enables method */
168 int *batch_flag; /* flag in option struct that disables method */
171 void input_userauth_success(int, u_int32_t, void *);
172 void input_userauth_failure(int, u_int32_t, void *);
173 void input_userauth_banner(int, u_int32_t, void *);
174 void input_userauth_error(int, u_int32_t, void *);
175 void input_userauth_info_req(int, u_int32_t, void *);
176 void input_userauth_pk_ok(int, u_int32_t, void *);
177 void input_userauth_passwd_changereq(int, u_int32_t, void *);
179 int userauth_none(Authctxt *);
180 int userauth_pubkey(Authctxt *);
181 int userauth_passwd(Authctxt *);
182 int userauth_kbdint(Authctxt *);
183 int userauth_hostbased(Authctxt *);
185 void userauth(Authctxt *, char *);
187 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
188 static void clear_auth_state(Authctxt *);
190 static Authmethod *authmethod_get(char *authlist);
191 static Authmethod *authmethod_lookup(const char *name);
192 static char *authmethods_get(void);
194 Authmethod authmethods[] = {
197 &options.hostbased_authentication,
201 &options.pubkey_authentication,
203 {"keyboard-interactive",
205 &options.kbd_interactive_authentication,
206 &options.batch_mode},
209 &options.password_authentication,
210 &options.batch_mode},
215 {NULL, NULL, NULL, NULL}
219 ssh_userauth2(const char *local_user, const char *server_user, char *host,
220 Sensitive *sensitive)
225 if (options.challenge_response_authentication)
226 options.kbd_interactive_authentication = 1;
228 debug("send SSH2_MSG_SERVICE_REQUEST");
229 packet_start(SSH2_MSG_SERVICE_REQUEST);
230 packet_put_cstring("ssh-userauth");
233 type = packet_read();
234 if (type != SSH2_MSG_SERVICE_ACCEPT) {
235 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
237 if (packet_remaining() > 0) {
238 char *reply = packet_get_string(NULL);
239 debug("service_accept: %s", reply);
242 debug("buggy server: service_accept w/o service");
245 debug("got SSH2_MSG_SERVICE_ACCEPT");
247 if (options.preferred_authentications == NULL)
248 options.preferred_authentications = authmethods_get();
250 /* setup authentication context */
251 memset(&authctxt, 0, sizeof(authctxt));
252 authctxt.agent = ssh_get_authentication_connection();
253 authctxt.server_user = server_user;
254 authctxt.local_user = local_user;
255 authctxt.host = host;
256 authctxt.service = "ssh-connection"; /* service name */
257 authctxt.success = 0;
258 authctxt.method = authmethod_lookup("none");
259 authctxt.authlist = NULL;
260 authctxt.sensitive = sensitive;
261 authctxt.info_req_seen = 0;
262 if (authctxt.method == NULL)
263 fatal("ssh_userauth2: internal error: cannot send userauth none request");
265 /* initial userauth request */
266 userauth_none(&authctxt);
268 dispatch_init(&input_userauth_error);
269 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
270 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
271 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
272 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
274 if (authctxt.agent != NULL)
275 ssh_close_authentication_connection(authctxt.agent);
277 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
280 userauth(Authctxt *authctxt, char *authlist)
282 if (authlist == NULL) {
283 authlist = authctxt->authlist;
285 if (authctxt->authlist)
286 xfree(authctxt->authlist);
287 authctxt->authlist = authlist;
290 Authmethod *method = authmethod_get(authlist);
292 fatal("Permission denied (%s).", authlist);
293 authctxt->method = method;
294 if (method->userauth(authctxt) != 0) {
295 debug2("we sent a %s packet, wait for reply", method->name);
298 debug2("we did not send a packet, disable method");
299 method->enabled = NULL;
305 input_userauth_error(int type, u_int32_t seq, void *ctxt)
307 fatal("input_userauth_error: bad message during authentication: "
312 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
315 debug3("input_userauth_banner");
316 msg = packet_get_string(NULL);
317 lang = packet_get_string(NULL);
318 fprintf(stderr, "%s", msg);
324 input_userauth_success(int type, u_int32_t seq, void *ctxt)
326 Authctxt *authctxt = ctxt;
327 if (authctxt == NULL)
328 fatal("input_userauth_success: no authentication context");
329 if (authctxt->authlist)
330 xfree(authctxt->authlist);
331 clear_auth_state(authctxt);
332 authctxt->success = 1; /* break out */
336 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
338 Authctxt *authctxt = ctxt;
339 char *authlist = NULL;
342 if (authctxt == NULL)
343 fatal("input_userauth_failure: no authentication context");
345 authlist = packet_get_string(NULL);
346 partial = packet_get_char();
350 log("Authenticated with partial success.");
351 debug("authentications that can continue: %s", authlist);
353 clear_auth_state(authctxt);
354 userauth(authctxt, authlist);
357 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
359 Authctxt *authctxt = ctxt;
362 int pktype, sent = 0;
367 if (authctxt == NULL)
368 fatal("input_userauth_pk_ok: no authentication context");
369 if (datafellows & SSH_BUG_PKOK) {
370 /* this is similar to SSH_BUG_PKAUTH */
371 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
372 pkblob = packet_get_string(&blen);
374 buffer_append(&b, pkblob, blen);
375 pkalg = buffer_get_string(&b, &alen);
378 pkalg = packet_get_string(&alen);
379 pkblob = packet_get_string(&blen);
383 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
384 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
387 if (authctxt->last_key == NULL ||
388 authctxt->last_key_sign == NULL) {
389 debug("no last key or no sign cb");
392 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
393 debug("unknown pkalg %s", pkalg);
396 if ((key = key_from_blob(pkblob, blen)) == NULL) {
397 debug("no key from blob. pkalg %s", pkalg);
400 if (key->type != pktype) {
401 error("input_userauth_pk_ok: type mismatch "
402 "for decoded key (received %d, expected %d)",
406 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
407 debug2("input_userauth_pk_ok: fp %s", fp);
409 if (!key_equal(key, authctxt->last_key)) {
410 debug("key != last_key");
413 sent = sign_and_send_pubkey(authctxt, key,
414 authctxt->last_key_sign);
423 clear_auth_state(authctxt);
424 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
426 /* try another method if we did not send a packet */
428 userauth(authctxt, NULL);
433 userauth_none(Authctxt *authctxt)
435 /* initial userauth request */
436 packet_start(SSH2_MSG_USERAUTH_REQUEST);
437 packet_put_cstring(authctxt->server_user);
438 packet_put_cstring(authctxt->service);
439 packet_put_cstring(authctxt->method->name);
445 userauth_passwd(Authctxt *authctxt)
447 static int attempt = 0;
451 if (attempt++ >= options.number_of_password_prompts)
455 error("Permission denied, please try again.");
457 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
458 authctxt->server_user, authctxt->host);
459 password = read_passphrase(prompt, 0);
460 packet_start(SSH2_MSG_USERAUTH_REQUEST);
461 packet_put_cstring(authctxt->server_user);
462 packet_put_cstring(authctxt->service);
463 packet_put_cstring(authctxt->method->name);
465 packet_put_cstring(password);
466 memset(password, 0, strlen(password));
468 packet_add_padding(64);
471 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
472 &input_userauth_passwd_changereq);
477 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
480 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
482 Authctxt *authctxt = ctxt;
483 char *info, *lang, *password = NULL, *retype = NULL;
486 debug2("input_userauth_passwd_changereq");
488 if (authctxt == NULL)
489 fatal("input_userauth_passwd_changereq: "
490 "no authentication context");
492 info = packet_get_string(NULL);
493 lang = packet_get_string(NULL);
494 if (strlen(info) > 0)
498 packet_start(SSH2_MSG_USERAUTH_REQUEST);
499 packet_put_cstring(authctxt->server_user);
500 packet_put_cstring(authctxt->service);
501 packet_put_cstring(authctxt->method->name);
502 packet_put_char(1); /* additional info */
503 snprintf(prompt, sizeof(prompt),
504 "Enter %.30s@%.128s's old password: ",
505 authctxt->server_user, authctxt->host);
506 password = read_passphrase(prompt, 0);
507 packet_put_cstring(password);
508 memset(password, 0, strlen(password));
511 while (password == NULL) {
512 snprintf(prompt, sizeof(prompt),
513 "Enter %.30s@%.128s's new password: ",
514 authctxt->server_user, authctxt->host);
515 password = read_passphrase(prompt, RP_ALLOW_EOF);
516 if (password == NULL) {
520 snprintf(prompt, sizeof(prompt),
521 "Retype %.30s@%.128s's new password: ",
522 authctxt->server_user, authctxt->host);
523 retype = read_passphrase(prompt, 0);
524 if (strcmp(password, retype) != 0) {
525 memset(password, 0, strlen(password));
527 log("Mismatch; try again, EOF to quit.");
530 memset(retype, 0, strlen(retype));
533 packet_put_cstring(password);
534 memset(password, 0, strlen(password));
536 packet_add_padding(64);
539 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
540 &input_userauth_passwd_changereq);
544 clear_auth_state(Authctxt *authctxt)
546 /* XXX clear authentication state */
547 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
549 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
550 debug3("clear_auth_state: key_free %p", authctxt->last_key);
551 key_free(authctxt->last_key);
553 authctxt->last_key = NULL;
554 authctxt->last_key_hint = -2;
555 authctxt->last_key_sign = NULL;
559 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
562 u_char *blob, *signature;
568 debug3("sign_and_send_pubkey");
570 if (key_to_blob(k, &blob, &bloblen) == 0) {
571 /* we cannot handle this key */
572 debug3("sign_and_send_pubkey: cannot handle key");
575 /* data to be signed */
577 if (datafellows & SSH_OLD_SESSIONID) {
578 buffer_append(&b, session_id2, session_id2_len);
579 skip = session_id2_len;
581 buffer_put_string(&b, session_id2, session_id2_len);
582 skip = buffer_len(&b);
584 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
585 buffer_put_cstring(&b, authctxt->server_user);
586 buffer_put_cstring(&b,
587 datafellows & SSH_BUG_PKSERVICE ?
590 if (datafellows & SSH_BUG_PKAUTH) {
591 buffer_put_char(&b, have_sig);
593 buffer_put_cstring(&b, authctxt->method->name);
594 buffer_put_char(&b, have_sig);
595 buffer_put_cstring(&b, key_ssh_name(k));
597 buffer_put_string(&b, blob, bloblen);
599 /* generate signature */
600 ret = (*sign_callback)(authctxt, k, &signature, &slen,
601 buffer_ptr(&b), buffer_len(&b));
610 if (datafellows & SSH_BUG_PKSERVICE) {
612 buffer_append(&b, session_id2, session_id2_len);
613 skip = session_id2_len;
614 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
615 buffer_put_cstring(&b, authctxt->server_user);
616 buffer_put_cstring(&b, authctxt->service);
617 buffer_put_cstring(&b, authctxt->method->name);
618 buffer_put_char(&b, have_sig);
619 if (!(datafellows & SSH_BUG_PKAUTH))
620 buffer_put_cstring(&b, key_ssh_name(k));
621 buffer_put_string(&b, blob, bloblen);
625 /* append signature */
626 buffer_put_string(&b, signature, slen);
629 /* skip session id and packet type */
630 if (buffer_len(&b) < skip + 1)
631 fatal("userauth_pubkey: internal error");
632 buffer_consume(&b, skip + 1);
634 /* put remaining data from buffer into packet */
635 packet_start(SSH2_MSG_USERAUTH_REQUEST);
636 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
644 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
648 u_int bloblen, have_sig = 0;
650 debug3("send_pubkey_test");
652 if (key_to_blob(k, &blob, &bloblen) == 0) {
653 /* we cannot handle this key */
654 debug3("send_pubkey_test: cannot handle key");
657 /* register callback for USERAUTH_PK_OK message */
658 authctxt->last_key_sign = sign_callback;
659 authctxt->last_key_hint = hint;
660 authctxt->last_key = k;
661 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
663 packet_start(SSH2_MSG_USERAUTH_REQUEST);
664 packet_put_cstring(authctxt->server_user);
665 packet_put_cstring(authctxt->service);
666 packet_put_cstring(authctxt->method->name);
667 packet_put_char(have_sig);
668 if (!(datafellows & SSH_BUG_PKAUTH))
669 packet_put_cstring(key_ssh_name(k));
670 packet_put_string(blob, bloblen);
677 load_identity_file(char *filename)
680 char prompt[300], *passphrase;
684 if (stat(filename, &st) < 0) {
685 debug3("no such identity: %s", filename);
688 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
689 if (private == NULL) {
690 if (options.batch_mode)
692 snprintf(prompt, sizeof prompt,
693 "Enter passphrase for key '%.100s': ", filename);
694 for (i = 0; i < options.number_of_password_prompts; i++) {
695 passphrase = read_passphrase(prompt, 0);
696 if (strcmp(passphrase, "") != 0) {
697 private = key_load_private_type(KEY_UNSPEC, filename,
701 debug2("no passphrase given, try next key");
704 memset(passphrase, 0, strlen(passphrase));
706 if (private != NULL || quit)
708 debug2("bad passphrase given, try again...");
715 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
716 u_char *data, u_int datalen)
721 idx = authctxt->last_key_hint;
725 /* private key is stored in external hardware */
726 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
727 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
729 private = load_identity_file(options.identity_files[idx]);
732 ret = key_sign(private, sigp, lenp, data, datalen);
738 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
739 u_char *data, u_int datalen)
741 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
745 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
746 u_char *data, u_int datalen)
748 return key_sign(key, sigp, lenp, data, datalen);
752 userauth_pubkey_agent(Authctxt *authctxt)
754 static int called = 0;
760 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
761 debug2("userauth_pubkey_agent: no keys at all");
764 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
766 debug2("userauth_pubkey_agent: no more keys");
768 debug("userauth_pubkey_agent: testing agent key %s", comment);
770 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
775 debug2("userauth_pubkey_agent: no message sent");
780 userauth_pubkey(Authctxt *authctxt)
787 if (authctxt->agent != NULL) {
789 sent = userauth_pubkey_agent(authctxt);
790 } while (!sent && authctxt->agent->howmany > 0);
792 while (!sent && idx < options.num_identity_files) {
793 key = options.identity_keys[idx];
794 filename = options.identity_files[idx];
796 debug("try privkey: %s", filename);
797 key = load_identity_file(filename);
799 sent = sign_and_send_pubkey(authctxt, key,
803 } else if (key->type != KEY_RSA1) {
804 debug("try pubkey: %s", filename);
805 sent = send_pubkey_test(authctxt, key,
806 identity_sign_cb, idx);
814 * Send userauth request message specifying keyboard-interactive method.
817 userauth_kbdint(Authctxt *authctxt)
819 static int attempt = 0;
821 if (attempt++ >= options.number_of_password_prompts)
823 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
824 if (attempt > 1 && !authctxt->info_req_seen) {
825 debug3("userauth_kbdint: disable: no info_req_seen");
826 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
830 debug2("userauth_kbdint");
831 packet_start(SSH2_MSG_USERAUTH_REQUEST);
832 packet_put_cstring(authctxt->server_user);
833 packet_put_cstring(authctxt->service);
834 packet_put_cstring(authctxt->method->name);
835 packet_put_cstring(""); /* lang */
836 packet_put_cstring(options.kbd_interactive_devices ?
837 options.kbd_interactive_devices : "");
840 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
845 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
848 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
850 Authctxt *authctxt = ctxt;
851 char *name, *inst, *lang, *prompt, *response;
852 u_int num_prompts, i;
855 debug2("input_userauth_info_req");
857 if (authctxt == NULL)
858 fatal("input_userauth_info_req: no authentication context");
860 authctxt->info_req_seen = 1;
862 name = packet_get_string(NULL);
863 inst = packet_get_string(NULL);
864 lang = packet_get_string(NULL);
865 if (strlen(name) > 0)
867 if (strlen(inst) > 0)
873 num_prompts = packet_get_int();
875 * Begin to build info response packet based on prompts requested.
876 * We commit to providing the correct number of responses, so if
877 * further on we run into a problem that prevents this, we have to
878 * be sure and clean this up and send a correct error response.
880 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
881 packet_put_int(num_prompts);
883 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
884 for (i = 0; i < num_prompts; i++) {
885 prompt = packet_get_string(NULL);
886 echo = packet_get_char();
888 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
890 packet_put_cstring(response);
891 memset(response, 0, strlen(response));
895 packet_check_eom(); /* done with parsing incoming message. */
897 packet_add_padding(64);
902 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
903 u_char *data, u_int datalen)
908 int to[2], from[2], status, version = 2;
910 debug("ssh_keysign called");
912 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
913 error("ssh_keysign: no installed: %s", strerror(errno));
916 if (fflush(stdout) != 0)
917 error("ssh_keysign: fflush: %s", strerror(errno));
919 error("ssh_keysign: pipe: %s", strerror(errno));
922 if (pipe(from) < 0) {
923 error("ssh_keysign: pipe: %s", strerror(errno));
926 if ((pid = fork()) < 0) {
927 error("ssh_keysign: fork: %s", strerror(errno));
934 if (dup2(from[1], STDOUT_FILENO) < 0)
935 fatal("ssh_keysign: dup2: %s", strerror(errno));
937 if (dup2(to[0], STDIN_FILENO) < 0)
938 fatal("ssh_keysign: dup2: %s", strerror(errno));
941 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
942 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
949 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
950 buffer_put_string(&b, data, datalen);
951 ssh_msg_send(to[1], version, &b);
953 if (ssh_msg_recv(from[0], &b) < 0) {
954 error("ssh_keysign: no reply");
961 while (waitpid(pid, &status, 0) < 0)
965 if (buffer_get_char(&b) != version) {
966 error("ssh_keysign: bad version");
970 *sigp = buffer_get_string(&b, lenp);
977 userauth_hostbased(Authctxt *authctxt)
980 Sensitive *sensitive = authctxt->sensitive;
982 u_char *signature, *blob;
983 char *chost, *pkalg, *p;
986 int ok, i, len, found = 0;
988 /* check for a useful key */
989 for (i = 0; i < sensitive->nkeys; i++) {
990 private = sensitive->keys[i];
991 if (private && private->type != KEY_RSA1) {
993 /* we take and free the key */
994 sensitive->keys[i] = NULL;
999 debug("userauth_hostbased: no more client hostkeys");
1002 if (key_to_blob(private, &blob, &blen) == 0) {
1006 /* figure out a name for the client host */
1007 p = get_local_name(packet_get_connection_in());
1009 error("userauth_hostbased: cannot get local ipaddr/name");
1013 len = strlen(p) + 2;
1014 chost = xmalloc(len);
1015 strlcpy(chost, p, len);
1016 strlcat(chost, ".", len);
1017 debug2("userauth_hostbased: chost %s", chost);
1019 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1021 pkalg = xstrdup(key_ssh_name(private));
1023 /* construct data */
1024 buffer_put_string(&b, session_id2, session_id2_len);
1025 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1026 buffer_put_cstring(&b, authctxt->server_user);
1027 buffer_put_cstring(&b, service);
1028 buffer_put_cstring(&b, authctxt->method->name);
1029 buffer_put_cstring(&b, pkalg);
1030 buffer_put_string(&b, blob, blen);
1031 buffer_put_cstring(&b, chost);
1032 buffer_put_cstring(&b, authctxt->local_user);
1036 if (sensitive->external_keysign)
1037 ok = ssh_keysign(private, &signature, &slen,
1038 buffer_ptr(&b), buffer_len(&b));
1040 ok = key_sign(private, &signature, &slen,
1041 buffer_ptr(&b), buffer_len(&b));
1045 error("key_sign failed");
1050 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1051 packet_put_cstring(authctxt->server_user);
1052 packet_put_cstring(authctxt->service);
1053 packet_put_cstring(authctxt->method->name);
1054 packet_put_cstring(pkalg);
1055 packet_put_string(blob, blen);
1056 packet_put_cstring(chost);
1057 packet_put_cstring(authctxt->local_user);
1058 packet_put_string(signature, slen);
1059 memset(signature, 's', slen);
1068 /* find auth method */
1071 * given auth method name, if configurable options permit this method fill
1072 * in auth_ident field and return true, otherwise return false.
1075 authmethod_is_enabled(Authmethod *method)
1079 /* return false if options indicate this method is disabled */
1080 if (method->enabled == NULL || *method->enabled == 0)
1082 /* return false if batch mode is enabled but method needs interactive mode */
1083 if (method->batch_flag != NULL && *method->batch_flag != 0)
1089 authmethod_lookup(const char *name)
1091 Authmethod *method = NULL;
1093 for (method = authmethods; method->name != NULL; method++)
1094 if (strcmp(name, method->name) == 0)
1096 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1100 /* XXX internal state */
1101 static Authmethod *current = NULL;
1102 static char *supported = NULL;
1103 static char *preferred = NULL;
1106 * Given the authentication method list sent by the server, return the
1107 * next method we should try. If the server initially sends a nil list,
1108 * use a built-in default list.
1111 authmethod_get(char *authlist)
1117 /* Use a suitable default if we're passed a nil list. */
1118 if (authlist == NULL || strlen(authlist) == 0)
1119 authlist = options.preferred_authentications;
1121 if (supported == NULL || strcmp(authlist, supported) != 0) {
1122 debug3("start over, passed a different list %s", authlist);
1123 if (supported != NULL)
1125 supported = xstrdup(authlist);
1126 preferred = options.preferred_authentications;
1127 debug3("preferred %s", preferred);
1129 } else if (current != NULL && authmethod_is_enabled(current))
1133 if ((name = match_list(preferred, supported, &next)) == NULL) {
1134 debug("no more auth methods to try");
1139 debug3("authmethod_lookup %s", name);
1140 debug3("remaining preferred: %s", preferred);
1141 if ((current = authmethod_lookup(name)) != NULL &&
1142 authmethod_is_enabled(current)) {
1143 debug3("authmethod_is_enabled %s", name);
1144 debug("next auth method to try is %s", name);
1151 authmethods_get(void)
1153 Authmethod *method = NULL;
1158 for (method = authmethods; method->name != NULL; method++) {
1159 if (authmethod_is_enabled(method)) {
1160 if (buffer_len(&b) > 0)
1161 buffer_append(&b, ",", 1);
1162 buffer_append(&b, method->name, strlen(method->name));
1165 buffer_append(&b, "\0", 1);
1166 list = xstrdup(buffer_ptr(&b));