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.108 2002/11/21 22:45:31 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");
137 typedef struct Authctxt Authctxt;
138 typedef struct Authmethod Authmethod;
140 typedef int sign_cb_fn(
141 Authctxt *authctxt, Key *key,
142 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
145 const char *server_user;
146 const char *local_user;
154 sign_cb_fn *last_key_sign;
156 AuthenticationConnection *agent;
158 Sensitive *sensitive;
159 /* kbd-interactive */
163 char *name; /* string to compare against server's list */
164 int (*userauth)(Authctxt *authctxt);
165 int *enabled; /* flag in option struct that enables method */
166 int *batch_flag; /* flag in option struct that disables method */
169 void input_userauth_success(int, u_int32_t, void *);
170 void input_userauth_failure(int, u_int32_t, void *);
171 void input_userauth_banner(int, u_int32_t, void *);
172 void input_userauth_error(int, u_int32_t, void *);
173 void input_userauth_info_req(int, u_int32_t, void *);
174 void input_userauth_pk_ok(int, u_int32_t, void *);
175 void input_userauth_passwd_changereq(int, u_int32_t, void *);
177 int userauth_none(Authctxt *);
178 int userauth_pubkey(Authctxt *);
179 int userauth_passwd(Authctxt *);
180 int userauth_kbdint(Authctxt *);
181 int userauth_hostbased(Authctxt *);
183 void userauth(Authctxt *, char *);
185 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
186 static void clear_auth_state(Authctxt *);
188 static Authmethod *authmethod_get(char *authlist);
189 static Authmethod *authmethod_lookup(const char *name);
190 static char *authmethods_get(void);
192 Authmethod authmethods[] = {
195 &options.hostbased_authentication,
199 &options.pubkey_authentication,
201 {"keyboard-interactive",
203 &options.kbd_interactive_authentication,
204 &options.batch_mode},
207 &options.password_authentication,
208 &options.batch_mode},
213 {NULL, NULL, NULL, NULL}
217 ssh_userauth2(const char *local_user, const char *server_user, char *host,
218 Sensitive *sensitive)
223 if (options.challenge_response_authentication)
224 options.kbd_interactive_authentication = 1;
226 packet_start(SSH2_MSG_SERVICE_REQUEST);
227 packet_put_cstring("ssh-userauth");
229 debug("SSH2_MSG_SERVICE_REQUEST sent");
231 type = packet_read();
232 if (type != SSH2_MSG_SERVICE_ACCEPT)
233 fatal("Server denied authentication request: %d", type);
234 if (packet_remaining() > 0) {
235 char *reply = packet_get_string(NULL);
236 debug2("service_accept: %s", reply);
239 debug("buggy server: service_accept w/o service");
242 debug("SSH2_MSG_SERVICE_ACCEPT received");
244 if (options.preferred_authentications == NULL)
245 options.preferred_authentications = authmethods_get();
247 /* setup authentication context */
248 memset(&authctxt, 0, sizeof(authctxt));
249 authctxt.agent = ssh_get_authentication_connection();
250 authctxt.server_user = server_user;
251 authctxt.local_user = local_user;
252 authctxt.host = host;
253 authctxt.service = "ssh-connection"; /* service name */
254 authctxt.success = 0;
255 authctxt.method = authmethod_lookup("none");
256 authctxt.authlist = NULL;
257 authctxt.sensitive = sensitive;
258 authctxt.info_req_seen = 0;
259 if (authctxt.method == NULL)
260 fatal("ssh_userauth2: internal error: cannot send userauth none request");
262 /* initial userauth request */
263 userauth_none(&authctxt);
265 dispatch_init(&input_userauth_error);
266 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
267 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
268 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
269 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
271 if (authctxt.agent != NULL)
272 ssh_close_authentication_connection(authctxt.agent);
274 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
277 userauth(Authctxt *authctxt, char *authlist)
279 if (authlist == NULL) {
280 authlist = authctxt->authlist;
282 if (authctxt->authlist)
283 xfree(authctxt->authlist);
284 authctxt->authlist = authlist;
287 Authmethod *method = authmethod_get(authlist);
289 fatal("Permission denied (%s).", authlist);
290 authctxt->method = method;
291 if (method->userauth(authctxt) != 0) {
292 debug2("we sent a %s packet, wait for reply", method->name);
295 debug2("we did not send a packet, disable method");
296 method->enabled = NULL;
302 input_userauth_error(int type, u_int32_t seq, void *ctxt)
304 fatal("input_userauth_error: bad message during authentication: "
309 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
312 debug3("input_userauth_banner");
313 msg = packet_get_string(NULL);
314 lang = packet_get_string(NULL);
315 fprintf(stderr, "%s", msg);
321 input_userauth_success(int type, u_int32_t seq, void *ctxt)
323 Authctxt *authctxt = ctxt;
324 if (authctxt == NULL)
325 fatal("input_userauth_success: no authentication context");
326 if (authctxt->authlist)
327 xfree(authctxt->authlist);
328 clear_auth_state(authctxt);
329 authctxt->success = 1; /* break out */
333 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
335 Authctxt *authctxt = ctxt;
336 char *authlist = NULL;
339 if (authctxt == NULL)
340 fatal("input_userauth_failure: no authentication context");
342 authlist = packet_get_string(NULL);
343 partial = packet_get_char();
347 log("Authenticated with partial success.");
348 debug("authentications that can continue: %s", authlist);
350 clear_auth_state(authctxt);
351 userauth(authctxt, authlist);
354 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
356 Authctxt *authctxt = ctxt;
359 int pktype, sent = 0;
364 if (authctxt == NULL)
365 fatal("input_userauth_pk_ok: no authentication context");
366 if (datafellows & SSH_BUG_PKOK) {
367 /* this is similar to SSH_BUG_PKAUTH */
368 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
369 pkblob = packet_get_string(&blen);
371 buffer_append(&b, pkblob, blen);
372 pkalg = buffer_get_string(&b, &alen);
375 pkalg = packet_get_string(&alen);
376 pkblob = packet_get_string(&blen);
380 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
381 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
384 if (authctxt->last_key == NULL ||
385 authctxt->last_key_sign == NULL) {
386 debug("no last key or no sign cb");
389 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
390 debug("unknown pkalg %s", pkalg);
393 if ((key = key_from_blob(pkblob, blen)) == NULL) {
394 debug("no key from blob. pkalg %s", pkalg);
397 if (key->type != pktype) {
398 error("input_userauth_pk_ok: type mismatch "
399 "for decoded key (received %d, expected %d)",
403 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
404 debug2("input_userauth_pk_ok: fp %s", fp);
406 if (!key_equal(key, authctxt->last_key)) {
407 debug("key != last_key");
410 sent = sign_and_send_pubkey(authctxt, key,
411 authctxt->last_key_sign);
420 clear_auth_state(authctxt);
421 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
423 /* try another method if we did not send a packet */
425 userauth(authctxt, NULL);
430 userauth_none(Authctxt *authctxt)
432 /* initial userauth request */
433 packet_start(SSH2_MSG_USERAUTH_REQUEST);
434 packet_put_cstring(authctxt->server_user);
435 packet_put_cstring(authctxt->service);
436 packet_put_cstring(authctxt->method->name);
442 userauth_passwd(Authctxt *authctxt)
444 static int attempt = 0;
448 if (attempt++ >= options.number_of_password_prompts)
452 error("Permission denied, please try again.");
454 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
455 authctxt->server_user, authctxt->host);
456 password = read_passphrase(prompt, 0);
457 packet_start(SSH2_MSG_USERAUTH_REQUEST);
458 packet_put_cstring(authctxt->server_user);
459 packet_put_cstring(authctxt->service);
460 packet_put_cstring(authctxt->method->name);
462 packet_put_cstring(password);
463 memset(password, 0, strlen(password));
465 packet_add_padding(64);
468 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
469 &input_userauth_passwd_changereq);
474 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
477 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
479 Authctxt *authctxt = ctxt;
480 char *info, *lang, *password = NULL, *retype = NULL;
483 debug2("input_userauth_passwd_changereq");
485 if (authctxt == NULL)
486 fatal("input_userauth_passwd_changereq: "
487 "no authentication context");
489 info = packet_get_string(NULL);
490 lang = packet_get_string(NULL);
491 if (strlen(info) > 0)
495 packet_start(SSH2_MSG_USERAUTH_REQUEST);
496 packet_put_cstring(authctxt->server_user);
497 packet_put_cstring(authctxt->service);
498 packet_put_cstring(authctxt->method->name);
499 packet_put_char(1); /* additional info */
500 snprintf(prompt, sizeof(prompt),
501 "Enter %.30s@%.128s's old password: ",
502 authctxt->server_user, authctxt->host);
503 password = read_passphrase(prompt, 0);
504 packet_put_cstring(password);
505 memset(password, 0, strlen(password));
508 while (password == NULL) {
509 snprintf(prompt, sizeof(prompt),
510 "Enter %.30s@%.128s's new password: ",
511 authctxt->server_user, authctxt->host);
512 password = read_passphrase(prompt, RP_ALLOW_EOF);
513 if (password == NULL) {
517 snprintf(prompt, sizeof(prompt),
518 "Retype %.30s@%.128s's new password: ",
519 authctxt->server_user, authctxt->host);
520 retype = read_passphrase(prompt, 0);
521 if (strcmp(password, retype) != 0) {
522 memset(password, 0, strlen(password));
524 log("Mismatch; try again, EOF to quit.");
527 memset(retype, 0, strlen(retype));
530 packet_put_cstring(password);
531 memset(password, 0, strlen(password));
533 packet_add_padding(64);
536 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
537 &input_userauth_passwd_changereq);
541 clear_auth_state(Authctxt *authctxt)
543 /* XXX clear authentication state */
544 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
546 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
547 debug3("clear_auth_state: key_free %p", authctxt->last_key);
548 key_free(authctxt->last_key);
550 authctxt->last_key = NULL;
551 authctxt->last_key_hint = -2;
552 authctxt->last_key_sign = NULL;
556 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
559 u_char *blob, *signature;
565 debug3("sign_and_send_pubkey");
567 if (key_to_blob(k, &blob, &bloblen) == 0) {
568 /* we cannot handle this key */
569 debug3("sign_and_send_pubkey: cannot handle key");
572 /* data to be signed */
574 if (datafellows & SSH_OLD_SESSIONID) {
575 buffer_append(&b, session_id2, session_id2_len);
576 skip = session_id2_len;
578 buffer_put_string(&b, session_id2, session_id2_len);
579 skip = buffer_len(&b);
581 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
582 buffer_put_cstring(&b, authctxt->server_user);
583 buffer_put_cstring(&b,
584 datafellows & SSH_BUG_PKSERVICE ?
587 if (datafellows & SSH_BUG_PKAUTH) {
588 buffer_put_char(&b, have_sig);
590 buffer_put_cstring(&b, authctxt->method->name);
591 buffer_put_char(&b, have_sig);
592 buffer_put_cstring(&b, key_ssh_name(k));
594 buffer_put_string(&b, blob, bloblen);
596 /* generate signature */
597 ret = (*sign_callback)(authctxt, k, &signature, &slen,
598 buffer_ptr(&b), buffer_len(&b));
607 if (datafellows & SSH_BUG_PKSERVICE) {
609 buffer_append(&b, session_id2, session_id2_len);
610 skip = session_id2_len;
611 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
612 buffer_put_cstring(&b, authctxt->server_user);
613 buffer_put_cstring(&b, authctxt->service);
614 buffer_put_cstring(&b, authctxt->method->name);
615 buffer_put_char(&b, have_sig);
616 if (!(datafellows & SSH_BUG_PKAUTH))
617 buffer_put_cstring(&b, key_ssh_name(k));
618 buffer_put_string(&b, blob, bloblen);
622 /* append signature */
623 buffer_put_string(&b, signature, slen);
626 /* skip session id and packet type */
627 if (buffer_len(&b) < skip + 1)
628 fatal("userauth_pubkey: internal error");
629 buffer_consume(&b, skip + 1);
631 /* put remaining data from buffer into packet */
632 packet_start(SSH2_MSG_USERAUTH_REQUEST);
633 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
641 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
645 u_int bloblen, have_sig = 0;
647 debug3("send_pubkey_test");
649 if (key_to_blob(k, &blob, &bloblen) == 0) {
650 /* we cannot handle this key */
651 debug3("send_pubkey_test: cannot handle key");
654 /* register callback for USERAUTH_PK_OK message */
655 authctxt->last_key_sign = sign_callback;
656 authctxt->last_key_hint = hint;
657 authctxt->last_key = k;
658 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
660 packet_start(SSH2_MSG_USERAUTH_REQUEST);
661 packet_put_cstring(authctxt->server_user);
662 packet_put_cstring(authctxt->service);
663 packet_put_cstring(authctxt->method->name);
664 packet_put_char(have_sig);
665 if (!(datafellows & SSH_BUG_PKAUTH))
666 packet_put_cstring(key_ssh_name(k));
667 packet_put_string(blob, bloblen);
674 load_identity_file(char *filename)
677 char prompt[300], *passphrase;
681 if (stat(filename, &st) < 0) {
682 debug3("no such identity: %s", filename);
685 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
686 if (private == NULL) {
687 if (options.batch_mode)
689 snprintf(prompt, sizeof prompt,
690 "Enter passphrase for key '%.100s': ", filename);
691 for (i = 0; i < options.number_of_password_prompts; i++) {
692 passphrase = read_passphrase(prompt, 0);
693 if (strcmp(passphrase, "") != 0) {
694 private = key_load_private_type(KEY_UNSPEC, filename,
698 debug2("no passphrase given, try next key");
701 memset(passphrase, 0, strlen(passphrase));
703 if (private != NULL || quit)
705 debug2("bad passphrase given, try again...");
712 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
713 u_char *data, u_int datalen)
718 idx = authctxt->last_key_hint;
722 /* private key is stored in external hardware */
723 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
724 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
726 private = load_identity_file(options.identity_files[idx]);
729 ret = key_sign(private, sigp, lenp, data, datalen);
735 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
736 u_char *data, u_int datalen)
738 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
742 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
743 u_char *data, u_int datalen)
745 return key_sign(key, sigp, lenp, data, datalen);
749 userauth_pubkey_agent(Authctxt *authctxt)
751 static int called = 0;
757 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
758 debug2("userauth_pubkey_agent: no keys at all");
761 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
763 debug2("userauth_pubkey_agent: no more keys");
765 debug("userauth_pubkey_agent: testing agent key %s", comment);
767 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
772 debug2("userauth_pubkey_agent: no message sent");
777 userauth_pubkey(Authctxt *authctxt)
784 if (authctxt->agent != NULL) {
786 sent = userauth_pubkey_agent(authctxt);
787 } while (!sent && authctxt->agent->howmany > 0);
789 while (!sent && idx < options.num_identity_files) {
790 key = options.identity_keys[idx];
791 filename = options.identity_files[idx];
793 debug("try privkey: %s", filename);
794 key = load_identity_file(filename);
796 sent = sign_and_send_pubkey(authctxt, key,
800 } else if (key->type != KEY_RSA1) {
801 debug("try pubkey: %s", filename);
802 sent = send_pubkey_test(authctxt, key,
803 identity_sign_cb, idx);
811 * Send userauth request message specifying keyboard-interactive method.
814 userauth_kbdint(Authctxt *authctxt)
816 static int attempt = 0;
818 if (attempt++ >= options.number_of_password_prompts)
820 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
821 if (attempt > 1 && !authctxt->info_req_seen) {
822 debug3("userauth_kbdint: disable: no info_req_seen");
823 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
827 debug2("userauth_kbdint");
828 packet_start(SSH2_MSG_USERAUTH_REQUEST);
829 packet_put_cstring(authctxt->server_user);
830 packet_put_cstring(authctxt->service);
831 packet_put_cstring(authctxt->method->name);
832 packet_put_cstring(""); /* lang */
833 packet_put_cstring(options.kbd_interactive_devices ?
834 options.kbd_interactive_devices : "");
837 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
842 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
845 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
847 Authctxt *authctxt = ctxt;
848 char *name, *inst, *lang, *prompt, *response;
849 u_int num_prompts, i;
852 debug2("input_userauth_info_req");
854 if (authctxt == NULL)
855 fatal("input_userauth_info_req: no authentication context");
857 authctxt->info_req_seen = 1;
859 name = packet_get_string(NULL);
860 inst = packet_get_string(NULL);
861 lang = packet_get_string(NULL);
862 if (strlen(name) > 0)
864 if (strlen(inst) > 0)
870 num_prompts = packet_get_int();
872 * Begin to build info response packet based on prompts requested.
873 * We commit to providing the correct number of responses, so if
874 * further on we run into a problem that prevents this, we have to
875 * be sure and clean this up and send a correct error response.
877 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
878 packet_put_int(num_prompts);
880 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
881 for (i = 0; i < num_prompts; i++) {
882 prompt = packet_get_string(NULL);
883 echo = packet_get_char();
885 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
887 packet_put_cstring(response);
888 memset(response, 0, strlen(response));
892 packet_check_eom(); /* done with parsing incoming message. */
894 packet_add_padding(64);
899 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
900 u_char *data, u_int datalen)
905 int to[2], from[2], status, version = 2;
907 debug("ssh_keysign called");
909 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
910 error("ssh_keysign: no installed: %s", strerror(errno));
913 if (fflush(stdout) != 0)
914 error("ssh_keysign: fflush: %s", strerror(errno));
916 error("ssh_keysign: pipe: %s", strerror(errno));
919 if (pipe(from) < 0) {
920 error("ssh_keysign: pipe: %s", strerror(errno));
923 if ((pid = fork()) < 0) {
924 error("ssh_keysign: fork: %s", strerror(errno));
931 if (dup2(from[1], STDOUT_FILENO) < 0)
932 fatal("ssh_keysign: dup2: %s", strerror(errno));
934 if (dup2(to[0], STDIN_FILENO) < 0)
935 fatal("ssh_keysign: dup2: %s", strerror(errno));
938 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
939 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
946 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
947 buffer_put_string(&b, data, datalen);
948 ssh_msg_send(to[1], version, &b);
950 if (ssh_msg_recv(from[0], &b) < 0) {
951 error("ssh_keysign: no reply");
958 while (waitpid(pid, &status, 0) < 0)
962 if (buffer_get_char(&b) != version) {
963 error("ssh_keysign: bad version");
967 *sigp = buffer_get_string(&b, lenp);
974 userauth_hostbased(Authctxt *authctxt)
977 Sensitive *sensitive = authctxt->sensitive;
979 u_char *signature, *blob;
980 char *chost, *pkalg, *p;
983 int ok, i, len, found = 0;
985 /* check for a useful key */
986 for (i = 0; i < sensitive->nkeys; i++) {
987 private = sensitive->keys[i];
988 if (private && private->type != KEY_RSA1) {
990 /* we take and free the key */
991 sensitive->keys[i] = NULL;
996 debug("userauth_hostbased: no more client hostkeys");
999 if (key_to_blob(private, &blob, &blen) == 0) {
1003 /* figure out a name for the client host */
1004 p = get_local_name(packet_get_connection_in());
1006 error("userauth_hostbased: cannot get local ipaddr/name");
1010 len = strlen(p) + 2;
1011 chost = xmalloc(len);
1012 strlcpy(chost, p, len);
1013 strlcat(chost, ".", len);
1014 debug2("userauth_hostbased: chost %s", chost);
1016 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1018 pkalg = xstrdup(key_ssh_name(private));
1020 /* construct data */
1021 buffer_put_string(&b, session_id2, session_id2_len);
1022 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1023 buffer_put_cstring(&b, authctxt->server_user);
1024 buffer_put_cstring(&b, service);
1025 buffer_put_cstring(&b, authctxt->method->name);
1026 buffer_put_cstring(&b, pkalg);
1027 buffer_put_string(&b, blob, blen);
1028 buffer_put_cstring(&b, chost);
1029 buffer_put_cstring(&b, authctxt->local_user);
1033 if (sensitive->external_keysign)
1034 ok = ssh_keysign(private, &signature, &slen,
1035 buffer_ptr(&b), buffer_len(&b));
1037 ok = key_sign(private, &signature, &slen,
1038 buffer_ptr(&b), buffer_len(&b));
1042 error("key_sign failed");
1047 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1048 packet_put_cstring(authctxt->server_user);
1049 packet_put_cstring(authctxt->service);
1050 packet_put_cstring(authctxt->method->name);
1051 packet_put_cstring(pkalg);
1052 packet_put_string(blob, blen);
1053 packet_put_cstring(chost);
1054 packet_put_cstring(authctxt->local_user);
1055 packet_put_string(signature, slen);
1056 memset(signature, 's', slen);
1065 /* find auth method */
1068 * given auth method name, if configurable options permit this method fill
1069 * in auth_ident field and return true, otherwise return false.
1072 authmethod_is_enabled(Authmethod *method)
1076 /* return false if options indicate this method is disabled */
1077 if (method->enabled == NULL || *method->enabled == 0)
1079 /* return false if batch mode is enabled but method needs interactive mode */
1080 if (method->batch_flag != NULL && *method->batch_flag != 0)
1086 authmethod_lookup(const char *name)
1088 Authmethod *method = NULL;
1090 for (method = authmethods; method->name != NULL; method++)
1091 if (strcmp(name, method->name) == 0)
1093 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1097 /* XXX internal state */
1098 static Authmethod *current = NULL;
1099 static char *supported = NULL;
1100 static char *preferred = NULL;
1103 * Given the authentication method list sent by the server, return the
1104 * next method we should try. If the server initially sends a nil list,
1105 * use a built-in default list.
1108 authmethod_get(char *authlist)
1114 /* Use a suitable default if we're passed a nil list. */
1115 if (authlist == NULL || strlen(authlist) == 0)
1116 authlist = options.preferred_authentications;
1118 if (supported == NULL || strcmp(authlist, supported) != 0) {
1119 debug3("start over, passed a different list %s", authlist);
1120 if (supported != NULL)
1122 supported = xstrdup(authlist);
1123 preferred = options.preferred_authentications;
1124 debug3("preferred %s", preferred);
1126 } else if (current != NULL && authmethod_is_enabled(current))
1130 if ((name = match_list(preferred, supported, &next)) == NULL) {
1131 debug("no more auth methods to try");
1136 debug3("authmethod_lookup %s", name);
1137 debug3("remaining preferred: %s", preferred);
1138 if ((current = authmethod_lookup(name)) != NULL &&
1139 authmethod_is_enabled(current)) {
1140 debug3("authmethod_is_enabled %s", name);
1141 debug("next auth method to try is %s", name);
1148 authmethods_get(void)
1150 Authmethod *method = NULL;
1155 for (method = authmethods; method->name != NULL; method++) {
1156 if (authmethod_is_enabled(method)) {
1157 if (buffer_len(&b) > 0)
1158 buffer_append(&b, ",", 1);
1159 buffer_append(&b, method->name, strlen(method->name));
1162 buffer_append(&b, "\0", 1);
1163 list = xstrdup(buffer_ptr(&b));