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.115 2003/04/02 09:48:07 markus Exp $");
37 #include "myproposal.h"
38 #include "sshconnect.h"
49 #include "pathnames.h"
52 extern char *client_version_string;
53 extern char *server_version_string;
54 extern Options options;
60 u_char *session_id2 = NULL;
61 int session_id2_len = 0;
64 struct sockaddr *xxx_hostaddr;
69 verify_host_key_callback(Key *hostkey)
71 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
72 fatal("Host key verification failed.");
77 ssh_kex2(char *host, struct sockaddr *hostaddr)
82 xxx_hostaddr = hostaddr;
84 if (options.ciphers == (char *)-1) {
85 logit("No valid ciphers for protocol version 2 given, using defaults.");
86 options.ciphers = NULL;
88 if (options.ciphers != NULL) {
89 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
90 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
92 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
93 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
94 myproposal[PROPOSAL_ENC_ALGS_STOC] =
95 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
96 if (options.compression) {
97 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
98 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
100 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
101 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
103 if (options.macs != NULL) {
104 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
105 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
107 if (options.hostkeyalgorithms != NULL)
108 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
109 options.hostkeyalgorithms;
111 if (options.rekey_limit)
112 packet_set_rekey_limit(options.rekey_limit);
114 /* start key exchange */
115 kex = kex_setup(myproposal);
116 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
117 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
118 kex->client_version_string=client_version_string;
119 kex->server_version_string=server_version_string;
120 kex->verify_host_key=&verify_host_key_callback;
124 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
126 session_id2 = kex->session_id;
127 session_id2_len = kex->session_id_len;
130 /* send 1st encrypted/maced/compressed message */
131 packet_start(SSH2_MSG_IGNORE);
132 packet_put_cstring("markus");
142 typedef struct Authctxt Authctxt;
143 typedef struct Authmethod Authmethod;
145 typedef int sign_cb_fn(
146 Authctxt *authctxt, Key *key,
147 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
150 const char *server_user;
151 const char *local_user;
159 sign_cb_fn *last_key_sign;
161 AuthenticationConnection *agent;
163 Sensitive *sensitive;
164 /* kbd-interactive */
168 char *name; /* string to compare against server's list */
169 int (*userauth)(Authctxt *authctxt);
170 int *enabled; /* flag in option struct that enables method */
171 int *batch_flag; /* flag in option struct that disables method */
174 void input_userauth_success(int, u_int32_t, void *);
175 void input_userauth_failure(int, u_int32_t, void *);
176 void input_userauth_banner(int, u_int32_t, void *);
177 void input_userauth_error(int, u_int32_t, void *);
178 void input_userauth_info_req(int, u_int32_t, void *);
179 void input_userauth_pk_ok(int, u_int32_t, void *);
180 void input_userauth_passwd_changereq(int, u_int32_t, void *);
182 int userauth_none(Authctxt *);
183 int userauth_pubkey(Authctxt *);
184 int userauth_passwd(Authctxt *);
185 int userauth_kbdint(Authctxt *);
186 int userauth_hostbased(Authctxt *);
188 void userauth(Authctxt *, char *);
190 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
191 static void clear_auth_state(Authctxt *);
193 static Authmethod *authmethod_get(char *authlist);
194 static Authmethod *authmethod_lookup(const char *name);
195 static char *authmethods_get(void);
197 Authmethod authmethods[] = {
200 &options.hostbased_authentication,
204 &options.pubkey_authentication,
206 {"keyboard-interactive",
208 &options.kbd_interactive_authentication,
209 &options.batch_mode},
212 &options.password_authentication,
213 &options.batch_mode},
218 {NULL, NULL, NULL, NULL}
222 ssh_userauth2(const char *local_user, const char *server_user, char *host,
223 Sensitive *sensitive)
228 if (options.challenge_response_authentication)
229 options.kbd_interactive_authentication = 1;
231 packet_start(SSH2_MSG_SERVICE_REQUEST);
232 packet_put_cstring("ssh-userauth");
234 debug("SSH2_MSG_SERVICE_REQUEST sent");
236 type = packet_read();
237 if (type != SSH2_MSG_SERVICE_ACCEPT)
238 fatal("Server denied authentication request: %d", type);
239 if (packet_remaining() > 0) {
240 char *reply = packet_get_string(NULL);
241 debug2("service_accept: %s", reply);
244 debug2("buggy server: service_accept w/o service");
247 debug("SSH2_MSG_SERVICE_ACCEPT received");
249 if (options.preferred_authentications == NULL)
250 options.preferred_authentications = authmethods_get();
252 /* setup authentication context */
253 memset(&authctxt, 0, sizeof(authctxt));
254 authctxt.agent = ssh_get_authentication_connection();
255 authctxt.server_user = server_user;
256 authctxt.local_user = local_user;
257 authctxt.host = host;
258 authctxt.service = "ssh-connection"; /* service name */
259 authctxt.success = 0;
260 authctxt.method = authmethod_lookup("none");
261 authctxt.authlist = NULL;
262 authctxt.sensitive = sensitive;
263 authctxt.info_req_seen = 0;
264 if (authctxt.method == NULL)
265 fatal("ssh_userauth2: internal error: cannot send userauth none request");
267 /* initial userauth request */
268 userauth_none(&authctxt);
270 dispatch_init(&input_userauth_error);
271 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
272 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
273 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
274 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
276 if (authctxt.agent != NULL)
277 ssh_close_authentication_connection(authctxt.agent);
279 debug("Authentication succeeded (%s).", authctxt.method->name);
282 userauth(Authctxt *authctxt, char *authlist)
284 if (authlist == NULL) {
285 authlist = authctxt->authlist;
287 if (authctxt->authlist)
288 xfree(authctxt->authlist);
289 authctxt->authlist = authlist;
292 Authmethod *method = authmethod_get(authlist);
294 fatal("Permission denied (%s).", authlist);
295 authctxt->method = method;
296 if (method->userauth(authctxt) != 0) {
297 debug2("we sent a %s packet, wait for reply", method->name);
300 debug2("we did not send a packet, disable method");
301 method->enabled = NULL;
307 input_userauth_error(int type, u_int32_t seq, void *ctxt)
309 fatal("input_userauth_error: bad message during authentication: "
314 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
317 debug3("input_userauth_banner");
318 msg = packet_get_string(NULL);
319 lang = packet_get_string(NULL);
320 fprintf(stderr, "%s", msg);
326 input_userauth_success(int type, u_int32_t seq, void *ctxt)
328 Authctxt *authctxt = ctxt;
329 if (authctxt == NULL)
330 fatal("input_userauth_success: no authentication context");
331 if (authctxt->authlist)
332 xfree(authctxt->authlist);
333 clear_auth_state(authctxt);
334 authctxt->success = 1; /* break out */
338 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
340 Authctxt *authctxt = ctxt;
341 char *authlist = NULL;
344 if (authctxt == NULL)
345 fatal("input_userauth_failure: no authentication context");
347 authlist = packet_get_string(NULL);
348 partial = packet_get_char();
352 logit("Authenticated with partial success.");
353 debug("Authentications that can continue: %s", authlist);
355 clear_auth_state(authctxt);
356 userauth(authctxt, authlist);
359 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
361 Authctxt *authctxt = ctxt;
364 int pktype, sent = 0;
369 if (authctxt == NULL)
370 fatal("input_userauth_pk_ok: no authentication context");
371 if (datafellows & SSH_BUG_PKOK) {
372 /* this is similar to SSH_BUG_PKAUTH */
373 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
374 pkblob = packet_get_string(&blen);
376 buffer_append(&b, pkblob, blen);
377 pkalg = buffer_get_string(&b, &alen);
380 pkalg = packet_get_string(&alen);
381 pkblob = packet_get_string(&blen);
385 debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
386 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
389 if (authctxt->last_key == NULL ||
390 authctxt->last_key_sign == NULL) {
391 debug("no last key or no sign cb");
394 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
395 debug("unknown pkalg %s", pkalg);
398 if ((key = key_from_blob(pkblob, blen)) == NULL) {
399 debug("no key from blob. pkalg %s", pkalg);
402 if (key->type != pktype) {
403 error("input_userauth_pk_ok: type mismatch "
404 "for decoded key (received %d, expected %d)",
408 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
409 debug2("input_userauth_pk_ok: fp %s", fp);
411 if (!key_equal(key, authctxt->last_key)) {
412 debug("key != last_key");
415 sent = sign_and_send_pubkey(authctxt, key,
416 authctxt->last_key_sign);
425 clear_auth_state(authctxt);
426 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
428 /* try another method if we did not send a packet */
430 userauth(authctxt, NULL);
435 userauth_none(Authctxt *authctxt)
437 /* initial userauth request */
438 packet_start(SSH2_MSG_USERAUTH_REQUEST);
439 packet_put_cstring(authctxt->server_user);
440 packet_put_cstring(authctxt->service);
441 packet_put_cstring(authctxt->method->name);
447 userauth_passwd(Authctxt *authctxt)
449 static int attempt = 0;
453 if (attempt++ >= options.number_of_password_prompts)
457 error("Permission denied, please try again.");
459 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
460 authctxt->server_user, authctxt->host);
461 password = read_passphrase(prompt, 0);
462 packet_start(SSH2_MSG_USERAUTH_REQUEST);
463 packet_put_cstring(authctxt->server_user);
464 packet_put_cstring(authctxt->service);
465 packet_put_cstring(authctxt->method->name);
467 packet_put_cstring(password);
468 memset(password, 0, strlen(password));
470 packet_add_padding(64);
473 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
474 &input_userauth_passwd_changereq);
479 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
482 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
484 Authctxt *authctxt = ctxt;
485 char *info, *lang, *password = NULL, *retype = NULL;
488 debug2("input_userauth_passwd_changereq");
490 if (authctxt == NULL)
491 fatal("input_userauth_passwd_changereq: "
492 "no authentication context");
494 info = packet_get_string(NULL);
495 lang = packet_get_string(NULL);
496 if (strlen(info) > 0)
500 packet_start(SSH2_MSG_USERAUTH_REQUEST);
501 packet_put_cstring(authctxt->server_user);
502 packet_put_cstring(authctxt->service);
503 packet_put_cstring(authctxt->method->name);
504 packet_put_char(1); /* additional info */
505 snprintf(prompt, sizeof(prompt),
506 "Enter %.30s@%.128s's old password: ",
507 authctxt->server_user, authctxt->host);
508 password = read_passphrase(prompt, 0);
509 packet_put_cstring(password);
510 memset(password, 0, strlen(password));
513 while (password == NULL) {
514 snprintf(prompt, sizeof(prompt),
515 "Enter %.30s@%.128s's new password: ",
516 authctxt->server_user, authctxt->host);
517 password = read_passphrase(prompt, RP_ALLOW_EOF);
518 if (password == NULL) {
522 snprintf(prompt, sizeof(prompt),
523 "Retype %.30s@%.128s's new password: ",
524 authctxt->server_user, authctxt->host);
525 retype = read_passphrase(prompt, 0);
526 if (strcmp(password, retype) != 0) {
527 memset(password, 0, strlen(password));
529 logit("Mismatch; try again, EOF to quit.");
532 memset(retype, 0, strlen(retype));
535 packet_put_cstring(password);
536 memset(password, 0, strlen(password));
538 packet_add_padding(64);
541 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
542 &input_userauth_passwd_changereq);
546 clear_auth_state(Authctxt *authctxt)
548 /* XXX clear authentication state */
549 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
551 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
552 debug3("clear_auth_state: key_free %p", authctxt->last_key);
553 key_free(authctxt->last_key);
555 authctxt->last_key = NULL;
556 authctxt->last_key_hint = -2;
557 authctxt->last_key_sign = NULL;
561 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
564 u_char *blob, *signature;
570 debug3("sign_and_send_pubkey");
572 if (key_to_blob(k, &blob, &bloblen) == 0) {
573 /* we cannot handle this key */
574 debug3("sign_and_send_pubkey: cannot handle key");
577 /* data to be signed */
579 if (datafellows & SSH_OLD_SESSIONID) {
580 buffer_append(&b, session_id2, session_id2_len);
581 skip = session_id2_len;
583 buffer_put_string(&b, session_id2, session_id2_len);
584 skip = buffer_len(&b);
586 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
587 buffer_put_cstring(&b, authctxt->server_user);
588 buffer_put_cstring(&b,
589 datafellows & SSH_BUG_PKSERVICE ?
592 if (datafellows & SSH_BUG_PKAUTH) {
593 buffer_put_char(&b, have_sig);
595 buffer_put_cstring(&b, authctxt->method->name);
596 buffer_put_char(&b, have_sig);
597 buffer_put_cstring(&b, key_ssh_name(k));
599 buffer_put_string(&b, blob, bloblen);
601 /* generate signature */
602 ret = (*sign_callback)(authctxt, k, &signature, &slen,
603 buffer_ptr(&b), buffer_len(&b));
612 if (datafellows & SSH_BUG_PKSERVICE) {
614 buffer_append(&b, session_id2, session_id2_len);
615 skip = session_id2_len;
616 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
617 buffer_put_cstring(&b, authctxt->server_user);
618 buffer_put_cstring(&b, authctxt->service);
619 buffer_put_cstring(&b, authctxt->method->name);
620 buffer_put_char(&b, have_sig);
621 if (!(datafellows & SSH_BUG_PKAUTH))
622 buffer_put_cstring(&b, key_ssh_name(k));
623 buffer_put_string(&b, blob, bloblen);
627 /* append signature */
628 buffer_put_string(&b, signature, slen);
631 /* skip session id and packet type */
632 if (buffer_len(&b) < skip + 1)
633 fatal("userauth_pubkey: internal error");
634 buffer_consume(&b, skip + 1);
636 /* put remaining data from buffer into packet */
637 packet_start(SSH2_MSG_USERAUTH_REQUEST);
638 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
646 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
650 u_int bloblen, have_sig = 0;
652 debug3("send_pubkey_test");
654 if (key_to_blob(k, &blob, &bloblen) == 0) {
655 /* we cannot handle this key */
656 debug3("send_pubkey_test: cannot handle key");
659 /* register callback for USERAUTH_PK_OK message */
660 authctxt->last_key_sign = sign_callback;
661 authctxt->last_key_hint = hint;
662 authctxt->last_key = k;
663 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
665 packet_start(SSH2_MSG_USERAUTH_REQUEST);
666 packet_put_cstring(authctxt->server_user);
667 packet_put_cstring(authctxt->service);
668 packet_put_cstring(authctxt->method->name);
669 packet_put_char(have_sig);
670 if (!(datafellows & SSH_BUG_PKAUTH))
671 packet_put_cstring(key_ssh_name(k));
672 packet_put_string(blob, bloblen);
679 load_identity_file(char *filename)
682 char prompt[300], *passphrase;
686 if (stat(filename, &st) < 0) {
687 debug3("no such identity: %s", filename);
690 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
691 if (private == NULL) {
692 if (options.batch_mode)
694 snprintf(prompt, sizeof prompt,
695 "Enter passphrase for key '%.100s': ", filename);
696 for (i = 0; i < options.number_of_password_prompts; i++) {
697 passphrase = read_passphrase(prompt, 0);
698 if (strcmp(passphrase, "") != 0) {
699 private = key_load_private_type(KEY_UNSPEC, filename,
703 debug2("no passphrase given, try next key");
706 memset(passphrase, 0, strlen(passphrase));
708 if (private != NULL || quit)
710 debug2("bad passphrase given, try again...");
717 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
718 u_char *data, u_int datalen)
723 idx = authctxt->last_key_hint;
727 /* private key is stored in external hardware */
728 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
729 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
731 private = load_identity_file(options.identity_files[idx]);
734 ret = key_sign(private, sigp, lenp, data, datalen);
740 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
741 u_char *data, u_int datalen)
743 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
747 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
748 u_char *data, u_int datalen)
750 return key_sign(key, sigp, lenp, data, datalen);
754 userauth_pubkey_agent(Authctxt *authctxt)
756 static int called = 0;
762 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
763 debug2("userauth_pubkey_agent: no keys at all");
766 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
768 debug2("userauth_pubkey_agent: no more keys");
770 debug("Offering agent key: %s", comment);
772 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
777 debug2("userauth_pubkey_agent: no message sent");
782 userauth_pubkey(Authctxt *authctxt)
789 if (authctxt->agent != NULL) {
791 sent = userauth_pubkey_agent(authctxt);
792 } while (!sent && authctxt->agent->howmany > 0);
794 while (!sent && idx < options.num_identity_files) {
795 key = options.identity_keys[idx];
796 filename = options.identity_files[idx];
798 debug("Trying private key: %s", filename);
799 key = load_identity_file(filename);
801 sent = sign_and_send_pubkey(authctxt, key,
805 } else if (key->type != KEY_RSA1) {
806 debug("Offering public key: %s", filename);
807 sent = send_pubkey_test(authctxt, key,
808 identity_sign_cb, idx);
816 * Send userauth request message specifying keyboard-interactive method.
819 userauth_kbdint(Authctxt *authctxt)
821 static int attempt = 0;
823 if (attempt++ >= options.number_of_password_prompts)
825 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
826 if (attempt > 1 && !authctxt->info_req_seen) {
827 debug3("userauth_kbdint: disable: no info_req_seen");
828 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
832 debug2("userauth_kbdint");
833 packet_start(SSH2_MSG_USERAUTH_REQUEST);
834 packet_put_cstring(authctxt->server_user);
835 packet_put_cstring(authctxt->service);
836 packet_put_cstring(authctxt->method->name);
837 packet_put_cstring(""); /* lang */
838 packet_put_cstring(options.kbd_interactive_devices ?
839 options.kbd_interactive_devices : "");
842 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
847 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
850 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
852 Authctxt *authctxt = ctxt;
853 char *name, *inst, *lang, *prompt, *response;
854 u_int num_prompts, i;
857 debug2("input_userauth_info_req");
859 if (authctxt == NULL)
860 fatal("input_userauth_info_req: no authentication context");
862 authctxt->info_req_seen = 1;
864 name = packet_get_string(NULL);
865 inst = packet_get_string(NULL);
866 lang = packet_get_string(NULL);
867 if (strlen(name) > 0)
869 if (strlen(inst) > 0)
875 num_prompts = packet_get_int();
877 * Begin to build info response packet based on prompts requested.
878 * We commit to providing the correct number of responses, so if
879 * further on we run into a problem that prevents this, we have to
880 * be sure and clean this up and send a correct error response.
882 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
883 packet_put_int(num_prompts);
885 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
886 for (i = 0; i < num_prompts; i++) {
887 prompt = packet_get_string(NULL);
888 echo = packet_get_char();
890 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
892 packet_put_cstring(response);
893 memset(response, 0, strlen(response));
897 packet_check_eom(); /* done with parsing incoming message. */
899 packet_add_padding(64);
904 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
905 u_char *data, u_int datalen)
910 int to[2], from[2], status, version = 2;
912 debug2("ssh_keysign called");
914 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
915 error("ssh_keysign: no installed: %s", strerror(errno));
918 if (fflush(stdout) != 0)
919 error("ssh_keysign: fflush: %s", strerror(errno));
921 error("ssh_keysign: pipe: %s", strerror(errno));
924 if (pipe(from) < 0) {
925 error("ssh_keysign: pipe: %s", strerror(errno));
928 if ((pid = fork()) < 0) {
929 error("ssh_keysign: fork: %s", strerror(errno));
936 if (dup2(from[1], STDOUT_FILENO) < 0)
937 fatal("ssh_keysign: dup2: %s", strerror(errno));
939 if (dup2(to[0], STDIN_FILENO) < 0)
940 fatal("ssh_keysign: dup2: %s", strerror(errno));
943 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
944 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
951 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
952 buffer_put_string(&b, data, datalen);
953 ssh_msg_send(to[1], version, &b);
955 if (ssh_msg_recv(from[0], &b) < 0) {
956 error("ssh_keysign: no reply");
963 while (waitpid(pid, &status, 0) < 0)
967 if (buffer_get_char(&b) != version) {
968 error("ssh_keysign: bad version");
972 *sigp = buffer_get_string(&b, lenp);
979 userauth_hostbased(Authctxt *authctxt)
982 Sensitive *sensitive = authctxt->sensitive;
984 u_char *signature, *blob;
985 char *chost, *pkalg, *p;
988 int ok, i, len, found = 0;
990 /* check for a useful key */
991 for (i = 0; i < sensitive->nkeys; i++) {
992 private = sensitive->keys[i];
993 if (private && private->type != KEY_RSA1) {
995 /* we take and free the key */
996 sensitive->keys[i] = NULL;
1001 debug("No more client hostkeys for hostbased authentication.");
1004 if (key_to_blob(private, &blob, &blen) == 0) {
1008 /* figure out a name for the client host */
1009 p = get_local_name(packet_get_connection_in());
1011 error("userauth_hostbased: cannot get local ipaddr/name");
1015 len = strlen(p) + 2;
1016 chost = xmalloc(len);
1017 strlcpy(chost, p, len);
1018 strlcat(chost, ".", len);
1019 debug2("userauth_hostbased: chost %s", chost);
1022 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1024 pkalg = xstrdup(key_ssh_name(private));
1026 /* construct data */
1027 buffer_put_string(&b, session_id2, session_id2_len);
1028 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1029 buffer_put_cstring(&b, authctxt->server_user);
1030 buffer_put_cstring(&b, service);
1031 buffer_put_cstring(&b, authctxt->method->name);
1032 buffer_put_cstring(&b, pkalg);
1033 buffer_put_string(&b, blob, blen);
1034 buffer_put_cstring(&b, chost);
1035 buffer_put_cstring(&b, authctxt->local_user);
1039 if (sensitive->external_keysign)
1040 ok = ssh_keysign(private, &signature, &slen,
1041 buffer_ptr(&b), buffer_len(&b));
1043 ok = key_sign(private, &signature, &slen,
1044 buffer_ptr(&b), buffer_len(&b));
1048 error("key_sign failed");
1053 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1054 packet_put_cstring(authctxt->server_user);
1055 packet_put_cstring(authctxt->service);
1056 packet_put_cstring(authctxt->method->name);
1057 packet_put_cstring(pkalg);
1058 packet_put_string(blob, blen);
1059 packet_put_cstring(chost);
1060 packet_put_cstring(authctxt->local_user);
1061 packet_put_string(signature, slen);
1062 memset(signature, 's', slen);
1071 /* find auth method */
1074 * given auth method name, if configurable options permit this method fill
1075 * in auth_ident field and return true, otherwise return false.
1078 authmethod_is_enabled(Authmethod *method)
1082 /* return false if options indicate this method is disabled */
1083 if (method->enabled == NULL || *method->enabled == 0)
1085 /* return false if batch mode is enabled but method needs interactive mode */
1086 if (method->batch_flag != NULL && *method->batch_flag != 0)
1092 authmethod_lookup(const char *name)
1094 Authmethod *method = NULL;
1096 for (method = authmethods; method->name != NULL; method++)
1097 if (strcmp(name, method->name) == 0)
1099 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1103 /* XXX internal state */
1104 static Authmethod *current = NULL;
1105 static char *supported = NULL;
1106 static char *preferred = NULL;
1109 * Given the authentication method list sent by the server, return the
1110 * next method we should try. If the server initially sends a nil list,
1111 * use a built-in default list.
1114 authmethod_get(char *authlist)
1119 /* Use a suitable default if we're passed a nil list. */
1120 if (authlist == NULL || strlen(authlist) == 0)
1121 authlist = options.preferred_authentications;
1123 if (supported == NULL || strcmp(authlist, supported) != 0) {
1124 debug3("start over, passed a different list %s", authlist);
1125 if (supported != NULL)
1127 supported = xstrdup(authlist);
1128 preferred = options.preferred_authentications;
1129 debug3("preferred %s", preferred);
1131 } else if (current != NULL && authmethod_is_enabled(current))
1135 if ((name = match_list(preferred, supported, &next)) == NULL) {
1136 debug("No more authentication methods to try.");
1141 debug3("authmethod_lookup %s", name);
1142 debug3("remaining preferred: %s", preferred);
1143 if ((current = authmethod_lookup(name)) != NULL &&
1144 authmethod_is_enabled(current)) {
1145 debug3("authmethod_is_enabled %s", name);
1146 debug("Next authentication method: %s", name);
1153 authmethods_get(void)
1155 Authmethod *method = NULL;
1160 for (method = authmethods; method->name != NULL; method++) {
1161 if (authmethod_is_enabled(method)) {
1162 if (buffer_len(&b) > 0)
1163 buffer_append(&b, ",", 1);
1164 buffer_append(&b, method->name, strlen(method->name));
1167 buffer_append(&b, "\0", 1);
1168 list = xstrdup(buffer_ptr(&b));