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 $");
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 log("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 /* start key exchange */
112 kex = kex_setup(myproposal);
113 kex->client_version_string=client_version_string;
114 kex->server_version_string=server_version_string;
115 kex->verify_host_key=&verify_host_key_callback;
119 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
121 session_id2 = kex->session_id;
122 session_id2_len = kex->session_id_len;
125 /* send 1st encrypted/maced/compressed message */
126 packet_start(SSH2_MSG_IGNORE);
127 packet_put_cstring("markus");
131 debug("done: ssh_kex2.");
138 typedef struct Authctxt Authctxt;
139 typedef struct Authmethod Authmethod;
141 typedef int sign_cb_fn(
142 Authctxt *authctxt, Key *key,
143 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
146 const char *server_user;
147 const char *local_user;
155 sign_cb_fn *last_key_sign;
157 AuthenticationConnection *agent;
159 Sensitive *sensitive;
160 /* kbd-interactive */
164 char *name; /* string to compare against server's list */
165 int (*userauth)(Authctxt *authctxt);
166 int *enabled; /* flag in option struct that enables method */
167 int *batch_flag; /* flag in option struct that disables method */
170 void input_userauth_success(int, u_int32_t, void *);
171 void input_userauth_failure(int, u_int32_t, void *);
172 void input_userauth_banner(int, u_int32_t, void *);
173 void input_userauth_error(int, u_int32_t, void *);
174 void input_userauth_info_req(int, u_int32_t, void *);
175 void input_userauth_pk_ok(int, u_int32_t, void *);
176 void input_userauth_passwd_changereq(int, u_int32_t, void *);
178 int userauth_none(Authctxt *);
179 int userauth_pubkey(Authctxt *);
180 int userauth_passwd(Authctxt *);
181 int userauth_kbdint(Authctxt *);
182 int userauth_hostbased(Authctxt *);
184 void userauth(Authctxt *, char *);
186 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
187 static void clear_auth_state(Authctxt *);
189 static Authmethod *authmethod_get(char *authlist);
190 static Authmethod *authmethod_lookup(const char *name);
191 static char *authmethods_get(void);
193 Authmethod authmethods[] = {
196 &options.hostbased_authentication,
200 &options.pubkey_authentication,
202 {"keyboard-interactive",
204 &options.kbd_interactive_authentication,
205 &options.batch_mode},
208 &options.password_authentication,
209 &options.batch_mode},
214 {NULL, NULL, NULL, NULL}
218 ssh_userauth2(const char *local_user, const char *server_user, char *host,
219 Sensitive *sensitive)
224 if (options.challenge_response_authentication)
225 options.kbd_interactive_authentication = 1;
227 debug("send SSH2_MSG_SERVICE_REQUEST");
228 packet_start(SSH2_MSG_SERVICE_REQUEST);
229 packet_put_cstring("ssh-userauth");
232 type = packet_read();
233 if (type != SSH2_MSG_SERVICE_ACCEPT) {
234 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
236 if (packet_remaining() > 0) {
237 char *reply = packet_get_string(NULL);
238 debug("service_accept: %s", reply);
241 debug("buggy server: service_accept w/o service");
244 debug("got SSH2_MSG_SERVICE_ACCEPT");
246 if (options.preferred_authentications == NULL)
247 options.preferred_authentications = authmethods_get();
249 /* setup authentication context */
250 memset(&authctxt, 0, sizeof(authctxt));
251 authctxt.agent = ssh_get_authentication_connection();
252 authctxt.server_user = server_user;
253 authctxt.local_user = local_user;
254 authctxt.host = host;
255 authctxt.service = "ssh-connection"; /* service name */
256 authctxt.success = 0;
257 authctxt.method = authmethod_lookup("none");
258 authctxt.authlist = NULL;
259 authctxt.sensitive = sensitive;
260 authctxt.info_req_seen = 0;
261 if (authctxt.method == NULL)
262 fatal("ssh_userauth2: internal error: cannot send userauth none request");
264 /* initial userauth request */
265 userauth_none(&authctxt);
267 dispatch_init(&input_userauth_error);
268 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
269 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
270 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
271 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
273 if (authctxt.agent != NULL)
274 ssh_close_authentication_connection(authctxt.agent);
276 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
279 userauth(Authctxt *authctxt, char *authlist)
281 if (authlist == NULL) {
282 authlist = authctxt->authlist;
284 if (authctxt->authlist)
285 xfree(authctxt->authlist);
286 authctxt->authlist = authlist;
289 Authmethod *method = authmethod_get(authlist);
291 fatal("Permission denied (%s).", authlist);
292 authctxt->method = method;
293 if (method->userauth(authctxt) != 0) {
294 debug2("we sent a %s packet, wait for reply", method->name);
297 debug2("we did not send a packet, disable method");
298 method->enabled = NULL;
304 input_userauth_error(int type, u_int32_t seq, void *ctxt)
306 fatal("input_userauth_error: bad message during authentication: "
311 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
314 debug3("input_userauth_banner");
315 msg = packet_get_string(NULL);
316 lang = packet_get_string(NULL);
317 fprintf(stderr, "%s", msg);
323 input_userauth_success(int type, u_int32_t seq, void *ctxt)
325 Authctxt *authctxt = ctxt;
326 if (authctxt == NULL)
327 fatal("input_userauth_success: no authentication context");
328 if (authctxt->authlist)
329 xfree(authctxt->authlist);
330 clear_auth_state(authctxt);
331 authctxt->success = 1; /* break out */
335 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
337 Authctxt *authctxt = ctxt;
338 char *authlist = NULL;
341 if (authctxt == NULL)
342 fatal("input_userauth_failure: no authentication context");
344 authlist = packet_get_string(NULL);
345 partial = packet_get_char();
349 log("Authenticated with partial success.");
350 debug("authentications that can continue: %s", authlist);
352 clear_auth_state(authctxt);
353 userauth(authctxt, authlist);
356 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
358 Authctxt *authctxt = ctxt;
361 int pktype, sent = 0;
366 if (authctxt == NULL)
367 fatal("input_userauth_pk_ok: no authentication context");
368 if (datafellows & SSH_BUG_PKOK) {
369 /* this is similar to SSH_BUG_PKAUTH */
370 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
371 pkblob = packet_get_string(&blen);
373 buffer_append(&b, pkblob, blen);
374 pkalg = buffer_get_string(&b, &alen);
377 pkalg = packet_get_string(&alen);
378 pkblob = packet_get_string(&blen);
382 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
383 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
386 if (authctxt->last_key == NULL ||
387 authctxt->last_key_sign == NULL) {
388 debug("no last key or no sign cb");
391 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
392 debug("unknown pkalg %s", pkalg);
395 if ((key = key_from_blob(pkblob, blen)) == NULL) {
396 debug("no key from blob. pkalg %s", pkalg);
399 if (key->type != pktype) {
400 error("input_userauth_pk_ok: type mismatch "
401 "for decoded key (received %d, expected %d)",
405 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
406 debug2("input_userauth_pk_ok: fp %s", fp);
408 if (!key_equal(key, authctxt->last_key)) {
409 debug("key != last_key");
412 sent = sign_and_send_pubkey(authctxt, key,
413 authctxt->last_key_sign);
422 clear_auth_state(authctxt);
423 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
425 /* try another method if we did not send a packet */
427 userauth(authctxt, NULL);
432 userauth_none(Authctxt *authctxt)
434 /* initial userauth request */
435 packet_start(SSH2_MSG_USERAUTH_REQUEST);
436 packet_put_cstring(authctxt->server_user);
437 packet_put_cstring(authctxt->service);
438 packet_put_cstring(authctxt->method->name);
444 userauth_passwd(Authctxt *authctxt)
446 static int attempt = 0;
450 if (attempt++ >= options.number_of_password_prompts)
454 error("Permission denied, please try again.");
456 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
457 authctxt->server_user, authctxt->host);
458 password = read_passphrase(prompt, 0);
459 packet_start(SSH2_MSG_USERAUTH_REQUEST);
460 packet_put_cstring(authctxt->server_user);
461 packet_put_cstring(authctxt->service);
462 packet_put_cstring(authctxt->method->name);
464 packet_put_cstring(password);
465 memset(password, 0, strlen(password));
467 packet_add_padding(64);
470 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
471 &input_userauth_passwd_changereq);
476 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
479 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
481 Authctxt *authctxt = ctxt;
482 char *info, *lang, *password = NULL, *retype = NULL;
485 debug2("input_userauth_passwd_changereq");
487 if (authctxt == NULL)
488 fatal("input_userauth_passwd_changereq: "
489 "no authentication context");
491 info = packet_get_string(NULL);
492 lang = packet_get_string(NULL);
493 if (strlen(info) > 0)
497 packet_start(SSH2_MSG_USERAUTH_REQUEST);
498 packet_put_cstring(authctxt->server_user);
499 packet_put_cstring(authctxt->service);
500 packet_put_cstring(authctxt->method->name);
501 packet_put_char(1); /* additional info */
502 snprintf(prompt, sizeof(prompt),
503 "Enter %.30s@%.128s's old password: ",
504 authctxt->server_user, authctxt->host);
505 password = read_passphrase(prompt, 0);
506 packet_put_cstring(password);
507 memset(password, 0, strlen(password));
510 while (password == NULL) {
511 snprintf(prompt, sizeof(prompt),
512 "Enter %.30s@%.128s's new password: ",
513 authctxt->server_user, authctxt->host);
514 password = read_passphrase(prompt, RP_ALLOW_EOF);
515 if (password == NULL) {
519 snprintf(prompt, sizeof(prompt),
520 "Retype %.30s@%.128s's new password: ",
521 authctxt->server_user, authctxt->host);
522 retype = read_passphrase(prompt, 0);
523 if (strcmp(password, retype) != 0) {
524 memset(password, 0, strlen(password));
526 log("Mismatch; try again, EOF to quit.");
529 memset(retype, 0, strlen(retype));
532 packet_put_cstring(password);
533 memset(password, 0, strlen(password));
535 packet_add_padding(64);
538 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
539 &input_userauth_passwd_changereq);
543 clear_auth_state(Authctxt *authctxt)
545 /* XXX clear authentication state */
546 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
548 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
549 debug3("clear_auth_state: key_free %p", authctxt->last_key);
550 key_free(authctxt->last_key);
552 authctxt->last_key = NULL;
553 authctxt->last_key_hint = -2;
554 authctxt->last_key_sign = NULL;
558 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
561 u_char *blob, *signature;
567 debug3("sign_and_send_pubkey");
569 if (key_to_blob(k, &blob, &bloblen) == 0) {
570 /* we cannot handle this key */
571 debug3("sign_and_send_pubkey: cannot handle key");
574 /* data to be signed */
576 if (datafellows & SSH_OLD_SESSIONID) {
577 buffer_append(&b, session_id2, session_id2_len);
578 skip = session_id2_len;
580 buffer_put_string(&b, session_id2, session_id2_len);
581 skip = buffer_len(&b);
583 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
584 buffer_put_cstring(&b, authctxt->server_user);
585 buffer_put_cstring(&b,
586 datafellows & SSH_BUG_PKSERVICE ?
589 if (datafellows & SSH_BUG_PKAUTH) {
590 buffer_put_char(&b, have_sig);
592 buffer_put_cstring(&b, authctxt->method->name);
593 buffer_put_char(&b, have_sig);
594 buffer_put_cstring(&b, key_ssh_name(k));
596 buffer_put_string(&b, blob, bloblen);
598 /* generate signature */
599 ret = (*sign_callback)(authctxt, k, &signature, &slen,
600 buffer_ptr(&b), buffer_len(&b));
609 if (datafellows & SSH_BUG_PKSERVICE) {
611 buffer_append(&b, session_id2, session_id2_len);
612 skip = session_id2_len;
613 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
614 buffer_put_cstring(&b, authctxt->server_user);
615 buffer_put_cstring(&b, authctxt->service);
616 buffer_put_cstring(&b, authctxt->method->name);
617 buffer_put_char(&b, have_sig);
618 if (!(datafellows & SSH_BUG_PKAUTH))
619 buffer_put_cstring(&b, key_ssh_name(k));
620 buffer_put_string(&b, blob, bloblen);
624 /* append signature */
625 buffer_put_string(&b, signature, slen);
628 /* skip session id and packet type */
629 if (buffer_len(&b) < skip + 1)
630 fatal("userauth_pubkey: internal error");
631 buffer_consume(&b, skip + 1);
633 /* put remaining data from buffer into packet */
634 packet_start(SSH2_MSG_USERAUTH_REQUEST);
635 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
643 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
647 u_int bloblen, have_sig = 0;
649 debug3("send_pubkey_test");
651 if (key_to_blob(k, &blob, &bloblen) == 0) {
652 /* we cannot handle this key */
653 debug3("send_pubkey_test: cannot handle key");
656 /* register callback for USERAUTH_PK_OK message */
657 authctxt->last_key_sign = sign_callback;
658 authctxt->last_key_hint = hint;
659 authctxt->last_key = k;
660 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
662 packet_start(SSH2_MSG_USERAUTH_REQUEST);
663 packet_put_cstring(authctxt->server_user);
664 packet_put_cstring(authctxt->service);
665 packet_put_cstring(authctxt->method->name);
666 packet_put_char(have_sig);
667 if (!(datafellows & SSH_BUG_PKAUTH))
668 packet_put_cstring(key_ssh_name(k));
669 packet_put_string(blob, bloblen);
676 load_identity_file(char *filename)
679 char prompt[300], *passphrase;
683 if (stat(filename, &st) < 0) {
684 debug3("no such identity: %s", filename);
687 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
688 if (private == NULL) {
689 if (options.batch_mode)
691 snprintf(prompt, sizeof prompt,
692 "Enter passphrase for key '%.100s': ", filename);
693 for (i = 0; i < options.number_of_password_prompts; i++) {
694 passphrase = read_passphrase(prompt, 0);
695 if (strcmp(passphrase, "") != 0) {
696 private = key_load_private_type(KEY_UNSPEC, filename,
700 debug2("no passphrase given, try next key");
703 memset(passphrase, 0, strlen(passphrase));
705 if (private != NULL || quit)
707 debug2("bad passphrase given, try again...");
714 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
715 u_char *data, u_int datalen)
720 idx = authctxt->last_key_hint;
724 /* private key is stored in external hardware */
725 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
726 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
728 private = load_identity_file(options.identity_files[idx]);
731 ret = key_sign(private, sigp, lenp, data, datalen);
737 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
738 u_char *data, u_int datalen)
740 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
744 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
745 u_char *data, u_int datalen)
747 return key_sign(key, sigp, lenp, data, datalen);
751 userauth_pubkey_agent(Authctxt *authctxt)
753 static int called = 0;
759 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
760 debug2("userauth_pubkey_agent: no keys at all");
763 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
765 debug2("userauth_pubkey_agent: no more keys");
767 debug("userauth_pubkey_agent: testing agent key %s", comment);
769 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
774 debug2("userauth_pubkey_agent: no message sent");
779 userauth_pubkey(Authctxt *authctxt)
786 if (authctxt->agent != NULL) {
788 sent = userauth_pubkey_agent(authctxt);
789 } while (!sent && authctxt->agent->howmany > 0);
791 while (!sent && idx < options.num_identity_files) {
792 key = options.identity_keys[idx];
793 filename = options.identity_files[idx];
795 debug("try privkey: %s", filename);
796 key = load_identity_file(filename);
798 sent = sign_and_send_pubkey(authctxt, key,
802 } else if (key->type != KEY_RSA1) {
803 debug("try pubkey: %s", filename);
804 sent = send_pubkey_test(authctxt, key,
805 identity_sign_cb, idx);
813 * Send userauth request message specifying keyboard-interactive method.
816 userauth_kbdint(Authctxt *authctxt)
818 static int attempt = 0;
820 if (attempt++ >= options.number_of_password_prompts)
822 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
823 if (attempt > 1 && !authctxt->info_req_seen) {
824 debug3("userauth_kbdint: disable: no info_req_seen");
825 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
829 debug2("userauth_kbdint");
830 packet_start(SSH2_MSG_USERAUTH_REQUEST);
831 packet_put_cstring(authctxt->server_user);
832 packet_put_cstring(authctxt->service);
833 packet_put_cstring(authctxt->method->name);
834 packet_put_cstring(""); /* lang */
835 packet_put_cstring(options.kbd_interactive_devices ?
836 options.kbd_interactive_devices : "");
839 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
844 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
847 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
849 Authctxt *authctxt = ctxt;
850 char *name, *inst, *lang, *prompt, *response;
851 u_int num_prompts, i;
854 debug2("input_userauth_info_req");
856 if (authctxt == NULL)
857 fatal("input_userauth_info_req: no authentication context");
859 authctxt->info_req_seen = 1;
861 name = packet_get_string(NULL);
862 inst = packet_get_string(NULL);
863 lang = packet_get_string(NULL);
864 if (strlen(name) > 0)
866 if (strlen(inst) > 0)
872 num_prompts = packet_get_int();
874 * Begin to build info response packet based on prompts requested.
875 * We commit to providing the correct number of responses, so if
876 * further on we run into a problem that prevents this, we have to
877 * be sure and clean this up and send a correct error response.
879 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
880 packet_put_int(num_prompts);
882 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
883 for (i = 0; i < num_prompts; i++) {
884 prompt = packet_get_string(NULL);
885 echo = packet_get_char();
887 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
889 packet_put_cstring(response);
890 memset(response, 0, strlen(response));
894 packet_check_eom(); /* done with parsing incoming message. */
896 packet_add_padding(64);
901 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
902 u_char *data, u_int datalen)
907 int to[2], from[2], status, version = 2;
909 debug("ssh_keysign called");
911 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
912 error("ssh_keysign: no installed: %s", strerror(errno));
915 if (fflush(stdout) != 0)
916 error("ssh_keysign: fflush: %s", strerror(errno));
918 error("ssh_keysign: pipe: %s", strerror(errno));
921 if (pipe(from) < 0) {
922 error("ssh_keysign: pipe: %s", strerror(errno));
925 if ((pid = fork()) < 0) {
926 error("ssh_keysign: fork: %s", strerror(errno));
933 if (dup2(from[1], STDOUT_FILENO) < 0)
934 fatal("ssh_keysign: dup2: %s", strerror(errno));
936 if (dup2(to[0], STDIN_FILENO) < 0)
937 fatal("ssh_keysign: dup2: %s", strerror(errno));
940 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
941 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
948 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
949 buffer_put_string(&b, data, datalen);
950 ssh_msg_send(to[1], version, &b);
952 if (ssh_msg_recv(from[0], &b) < 0) {
953 error("ssh_keysign: no reply");
960 while (waitpid(pid, &status, 0) < 0)
964 if (buffer_get_char(&b) != version) {
965 error("ssh_keysign: bad version");
969 *sigp = buffer_get_string(&b, lenp);
976 userauth_hostbased(Authctxt *authctxt)
979 Sensitive *sensitive = authctxt->sensitive;
981 u_char *signature, *blob;
982 char *chost, *pkalg, *p;
985 int ok, i, len, found = 0;
987 /* check for a useful key */
988 for (i = 0; i < sensitive->nkeys; i++) {
989 private = sensitive->keys[i];
990 if (private && private->type != KEY_RSA1) {
992 /* we take and free the key */
993 sensitive->keys[i] = NULL;
998 debug("userauth_hostbased: no more client hostkeys");
1001 if (key_to_blob(private, &blob, &blen) == 0) {
1005 /* figure out a name for the client host */
1006 p = get_local_name(packet_get_connection_in());
1008 error("userauth_hostbased: cannot get local ipaddr/name");
1012 len = strlen(p) + 2;
1013 chost = xmalloc(len);
1014 strlcpy(chost, p, len);
1015 strlcat(chost, ".", len);
1016 debug2("userauth_hostbased: chost %s", chost);
1018 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1020 pkalg = xstrdup(key_ssh_name(private));
1022 /* construct data */
1023 buffer_put_string(&b, session_id2, session_id2_len);
1024 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1025 buffer_put_cstring(&b, authctxt->server_user);
1026 buffer_put_cstring(&b, service);
1027 buffer_put_cstring(&b, authctxt->method->name);
1028 buffer_put_cstring(&b, pkalg);
1029 buffer_put_string(&b, blob, blen);
1030 buffer_put_cstring(&b, chost);
1031 buffer_put_cstring(&b, authctxt->local_user);
1035 if (sensitive->external_keysign)
1036 ok = ssh_keysign(private, &signature, &slen,
1037 buffer_ptr(&b), buffer_len(&b));
1039 ok = key_sign(private, &signature, &slen,
1040 buffer_ptr(&b), buffer_len(&b));
1044 error("key_sign failed");
1049 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1050 packet_put_cstring(authctxt->server_user);
1051 packet_put_cstring(authctxt->service);
1052 packet_put_cstring(authctxt->method->name);
1053 packet_put_cstring(pkalg);
1054 packet_put_string(blob, blen);
1055 packet_put_cstring(chost);
1056 packet_put_cstring(authctxt->local_user);
1057 packet_put_string(signature, slen);
1058 memset(signature, 's', slen);
1067 /* find auth method */
1070 * given auth method name, if configurable options permit this method fill
1071 * in auth_ident field and return true, otherwise return false.
1074 authmethod_is_enabled(Authmethod *method)
1078 /* return false if options indicate this method is disabled */
1079 if (method->enabled == NULL || *method->enabled == 0)
1081 /* return false if batch mode is enabled but method needs interactive mode */
1082 if (method->batch_flag != NULL && *method->batch_flag != 0)
1088 authmethod_lookup(const char *name)
1090 Authmethod *method = NULL;
1092 for (method = authmethods; method->name != NULL; method++)
1093 if (strcmp(name, method->name) == 0)
1095 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1099 /* XXX internal state */
1100 static Authmethod *current = NULL;
1101 static char *supported = NULL;
1102 static char *preferred = NULL;
1105 * Given the authentication method list sent by the server, return the
1106 * next method we should try. If the server initially sends a nil list,
1107 * use a built-in default list.
1110 authmethod_get(char *authlist)
1116 /* Use a suitable default if we're passed a nil list. */
1117 if (authlist == NULL || strlen(authlist) == 0)
1118 authlist = options.preferred_authentications;
1120 if (supported == NULL || strcmp(authlist, supported) != 0) {
1121 debug3("start over, passed a different list %s", authlist);
1122 if (supported != NULL)
1124 supported = xstrdup(authlist);
1125 preferred = options.preferred_authentications;
1126 debug3("preferred %s", preferred);
1128 } else if (current != NULL && authmethod_is_enabled(current))
1132 if ((name = match_list(preferred, supported, &next)) == NULL) {
1133 debug("no more auth methods to try");
1138 debug3("authmethod_lookup %s", name);
1139 debug3("remaining preferred: %s", preferred);
1140 if ((current = authmethod_lookup(name)) != NULL &&
1141 authmethod_is_enabled(current)) {
1142 debug3("authmethod_is_enabled %s", name);
1143 debug("next auth method to try is %s", name);
1150 authmethods_get(void)
1152 Authmethod *method = NULL;
1157 for (method = authmethods; method->name != NULL; method++) {
1158 if (authmethod_is_enabled(method)) {
1159 if (buffer_len(&b) > 0)
1160 buffer_append(&b, ",", 1);
1161 buffer_append(&b, method->name, strlen(method->name));
1164 buffer_append(&b, "\0", 1);
1165 list = xstrdup(buffer_ptr(&b));