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 $");
28 RCSID("$DragonFly: src/crypto/openssh/Attic/sshconnect2.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
39 #include "myproposal.h"
40 #include "sshconnect.h"
51 #include "pathnames.h"
54 extern char *client_version_string;
55 extern char *server_version_string;
56 extern Options options;
62 u_char *session_id2 = NULL;
63 int session_id2_len = 0;
66 struct sockaddr *xxx_hostaddr;
71 verify_host_key_callback(Key *hostkey)
73 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
74 fatal("Host key verification failed.");
79 ssh_kex2(char *host, struct sockaddr *hostaddr)
84 xxx_hostaddr = hostaddr;
86 if (options.ciphers == (char *)-1) {
87 log("No valid ciphers for protocol version 2 given, using defaults.");
88 options.ciphers = NULL;
90 if (options.ciphers != NULL) {
91 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
92 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
94 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
95 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
96 myproposal[PROPOSAL_ENC_ALGS_STOC] =
97 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
98 if (options.compression) {
99 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
100 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
102 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
103 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
105 if (options.macs != NULL) {
106 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
107 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
109 if (options.hostkeyalgorithms != NULL)
110 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
111 options.hostkeyalgorithms;
113 /* start key exchange */
114 kex = kex_setup(myproposal);
115 kex->client_version_string=client_version_string;
116 kex->server_version_string=server_version_string;
117 kex->verify_host_key=&verify_host_key_callback;
121 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
123 session_id2 = kex->session_id;
124 session_id2_len = kex->session_id_len;
127 /* send 1st encrypted/maced/compressed message */
128 packet_start(SSH2_MSG_IGNORE);
129 packet_put_cstring("markus");
133 debug("done: ssh_kex2.");
140 typedef struct Authctxt Authctxt;
141 typedef struct Authmethod Authmethod;
143 typedef int sign_cb_fn(
144 Authctxt *authctxt, Key *key,
145 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
148 const char *server_user;
149 const char *local_user;
157 sign_cb_fn *last_key_sign;
159 AuthenticationConnection *agent;
161 Sensitive *sensitive;
162 /* kbd-interactive */
166 char *name; /* string to compare against server's list */
167 int (*userauth)(Authctxt *authctxt);
168 int *enabled; /* flag in option struct that enables method */
169 int *batch_flag; /* flag in option struct that disables method */
172 void input_userauth_success(int, u_int32_t, void *);
173 void input_userauth_failure(int, u_int32_t, void *);
174 void input_userauth_banner(int, u_int32_t, void *);
175 void input_userauth_error(int, u_int32_t, void *);
176 void input_userauth_info_req(int, u_int32_t, void *);
177 void input_userauth_pk_ok(int, u_int32_t, void *);
178 void input_userauth_passwd_changereq(int, u_int32_t, void *);
180 int userauth_none(Authctxt *);
181 int userauth_pubkey(Authctxt *);
182 int userauth_passwd(Authctxt *);
183 int userauth_kbdint(Authctxt *);
184 int userauth_hostbased(Authctxt *);
186 void userauth(Authctxt *, char *);
188 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
189 static void clear_auth_state(Authctxt *);
191 static Authmethod *authmethod_get(char *authlist);
192 static Authmethod *authmethod_lookup(const char *name);
193 static char *authmethods_get(void);
195 Authmethod authmethods[] = {
198 &options.hostbased_authentication,
202 &options.pubkey_authentication,
204 {"keyboard-interactive",
206 &options.kbd_interactive_authentication,
207 &options.batch_mode},
210 &options.password_authentication,
211 &options.batch_mode},
216 {NULL, NULL, NULL, NULL}
220 ssh_userauth2(const char *local_user, const char *server_user, char *host,
221 Sensitive *sensitive)
226 if (options.challenge_response_authentication)
227 options.kbd_interactive_authentication = 1;
229 debug("send SSH2_MSG_SERVICE_REQUEST");
230 packet_start(SSH2_MSG_SERVICE_REQUEST);
231 packet_put_cstring("ssh-userauth");
234 type = packet_read();
235 if (type != SSH2_MSG_SERVICE_ACCEPT) {
236 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
238 if (packet_remaining() > 0) {
239 char *reply = packet_get_string(NULL);
240 debug("service_accept: %s", reply);
243 debug("buggy server: service_accept w/o service");
246 debug("got SSH2_MSG_SERVICE_ACCEPT");
248 if (options.preferred_authentications == NULL)
249 options.preferred_authentications = authmethods_get();
251 /* setup authentication context */
252 memset(&authctxt, 0, sizeof(authctxt));
253 authctxt.agent = ssh_get_authentication_connection();
254 authctxt.server_user = server_user;
255 authctxt.local_user = local_user;
256 authctxt.host = host;
257 authctxt.service = "ssh-connection"; /* service name */
258 authctxt.success = 0;
259 authctxt.method = authmethod_lookup("none");
260 authctxt.authlist = NULL;
261 authctxt.sensitive = sensitive;
262 authctxt.info_req_seen = 0;
263 if (authctxt.method == NULL)
264 fatal("ssh_userauth2: internal error: cannot send userauth none request");
266 /* initial userauth request */
267 userauth_none(&authctxt);
269 dispatch_init(&input_userauth_error);
270 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
271 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
272 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
273 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
275 if (authctxt.agent != NULL)
276 ssh_close_authentication_connection(authctxt.agent);
278 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
281 userauth(Authctxt *authctxt, char *authlist)
283 if (authlist == NULL) {
284 authlist = authctxt->authlist;
286 if (authctxt->authlist)
287 xfree(authctxt->authlist);
288 authctxt->authlist = authlist;
291 Authmethod *method = authmethod_get(authlist);
293 fatal("Permission denied (%s).", authlist);
294 authctxt->method = method;
295 if (method->userauth(authctxt) != 0) {
296 debug2("we sent a %s packet, wait for reply", method->name);
299 debug2("we did not send a packet, disable method");
300 method->enabled = NULL;
306 input_userauth_error(int type, u_int32_t seq, void *ctxt)
308 fatal("input_userauth_error: bad message during authentication: "
313 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
316 debug3("input_userauth_banner");
317 msg = packet_get_string(NULL);
318 lang = packet_get_string(NULL);
319 fprintf(stderr, "%s", msg);
325 input_userauth_success(int type, u_int32_t seq, void *ctxt)
327 Authctxt *authctxt = ctxt;
328 if (authctxt == NULL)
329 fatal("input_userauth_success: no authentication context");
330 if (authctxt->authlist)
331 xfree(authctxt->authlist);
332 clear_auth_state(authctxt);
333 authctxt->success = 1; /* break out */
337 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
339 Authctxt *authctxt = ctxt;
340 char *authlist = NULL;
343 if (authctxt == NULL)
344 fatal("input_userauth_failure: no authentication context");
346 authlist = packet_get_string(NULL);
347 partial = packet_get_char();
351 log("Authenticated with partial success.");
352 debug("authentications that can continue: %s", authlist);
354 clear_auth_state(authctxt);
355 userauth(authctxt, authlist);
358 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
360 Authctxt *authctxt = ctxt;
363 int pktype, sent = 0;
368 if (authctxt == NULL)
369 fatal("input_userauth_pk_ok: no authentication context");
370 if (datafellows & SSH_BUG_PKOK) {
371 /* this is similar to SSH_BUG_PKAUTH */
372 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
373 pkblob = packet_get_string(&blen);
375 buffer_append(&b, pkblob, blen);
376 pkalg = buffer_get_string(&b, &alen);
379 pkalg = packet_get_string(&alen);
380 pkblob = packet_get_string(&blen);
384 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
385 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
388 if (authctxt->last_key == NULL ||
389 authctxt->last_key_sign == NULL) {
390 debug("no last key or no sign cb");
393 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
394 debug("unknown pkalg %s", pkalg);
397 if ((key = key_from_blob(pkblob, blen)) == NULL) {
398 debug("no key from blob. pkalg %s", pkalg);
401 if (key->type != pktype) {
402 error("input_userauth_pk_ok: type mismatch "
403 "for decoded key (received %d, expected %d)",
407 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
408 debug2("input_userauth_pk_ok: fp %s", fp);
410 if (!key_equal(key, authctxt->last_key)) {
411 debug("key != last_key");
414 sent = sign_and_send_pubkey(authctxt, key,
415 authctxt->last_key_sign);
424 clear_auth_state(authctxt);
425 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
427 /* try another method if we did not send a packet */
429 userauth(authctxt, NULL);
434 userauth_none(Authctxt *authctxt)
436 /* initial userauth request */
437 packet_start(SSH2_MSG_USERAUTH_REQUEST);
438 packet_put_cstring(authctxt->server_user);
439 packet_put_cstring(authctxt->service);
440 packet_put_cstring(authctxt->method->name);
446 userauth_passwd(Authctxt *authctxt)
448 static int attempt = 0;
452 if (attempt++ >= options.number_of_password_prompts)
456 error("Permission denied, please try again.");
458 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
459 authctxt->server_user, authctxt->host);
460 password = read_passphrase(prompt, 0);
461 packet_start(SSH2_MSG_USERAUTH_REQUEST);
462 packet_put_cstring(authctxt->server_user);
463 packet_put_cstring(authctxt->service);
464 packet_put_cstring(authctxt->method->name);
466 packet_put_cstring(password);
467 memset(password, 0, strlen(password));
469 packet_add_padding(64);
472 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
473 &input_userauth_passwd_changereq);
478 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
481 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
483 Authctxt *authctxt = ctxt;
484 char *info, *lang, *password = NULL, *retype = NULL;
487 debug2("input_userauth_passwd_changereq");
489 if (authctxt == NULL)
490 fatal("input_userauth_passwd_changereq: "
491 "no authentication context");
493 info = packet_get_string(NULL);
494 lang = packet_get_string(NULL);
495 if (strlen(info) > 0)
499 packet_start(SSH2_MSG_USERAUTH_REQUEST);
500 packet_put_cstring(authctxt->server_user);
501 packet_put_cstring(authctxt->service);
502 packet_put_cstring(authctxt->method->name);
503 packet_put_char(1); /* additional info */
504 snprintf(prompt, sizeof(prompt),
505 "Enter %.30s@%.128s's old password: ",
506 authctxt->server_user, authctxt->host);
507 password = read_passphrase(prompt, 0);
508 packet_put_cstring(password);
509 memset(password, 0, strlen(password));
512 while (password == NULL) {
513 snprintf(prompt, sizeof(prompt),
514 "Enter %.30s@%.128s's new password: ",
515 authctxt->server_user, authctxt->host);
516 password = read_passphrase(prompt, RP_ALLOW_EOF);
517 if (password == NULL) {
521 snprintf(prompt, sizeof(prompt),
522 "Retype %.30s@%.128s's new password: ",
523 authctxt->server_user, authctxt->host);
524 retype = read_passphrase(prompt, 0);
525 if (strcmp(password, retype) != 0) {
526 memset(password, 0, strlen(password));
528 log("Mismatch; try again, EOF to quit.");
531 memset(retype, 0, strlen(retype));
534 packet_put_cstring(password);
535 memset(password, 0, strlen(password));
537 packet_add_padding(64);
540 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
541 &input_userauth_passwd_changereq);
545 clear_auth_state(Authctxt *authctxt)
547 /* XXX clear authentication state */
548 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
550 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
551 debug3("clear_auth_state: key_free %p", authctxt->last_key);
552 key_free(authctxt->last_key);
554 authctxt->last_key = NULL;
555 authctxt->last_key_hint = -2;
556 authctxt->last_key_sign = NULL;
560 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
563 u_char *blob, *signature;
569 debug3("sign_and_send_pubkey");
571 if (key_to_blob(k, &blob, &bloblen) == 0) {
572 /* we cannot handle this key */
573 debug3("sign_and_send_pubkey: cannot handle key");
576 /* data to be signed */
578 if (datafellows & SSH_OLD_SESSIONID) {
579 buffer_append(&b, session_id2, session_id2_len);
580 skip = session_id2_len;
582 buffer_put_string(&b, session_id2, session_id2_len);
583 skip = buffer_len(&b);
585 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
586 buffer_put_cstring(&b, authctxt->server_user);
587 buffer_put_cstring(&b,
588 datafellows & SSH_BUG_PKSERVICE ?
591 if (datafellows & SSH_BUG_PKAUTH) {
592 buffer_put_char(&b, have_sig);
594 buffer_put_cstring(&b, authctxt->method->name);
595 buffer_put_char(&b, have_sig);
596 buffer_put_cstring(&b, key_ssh_name(k));
598 buffer_put_string(&b, blob, bloblen);
600 /* generate signature */
601 ret = (*sign_callback)(authctxt, k, &signature, &slen,
602 buffer_ptr(&b), buffer_len(&b));
611 if (datafellows & SSH_BUG_PKSERVICE) {
613 buffer_append(&b, session_id2, session_id2_len);
614 skip = session_id2_len;
615 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
616 buffer_put_cstring(&b, authctxt->server_user);
617 buffer_put_cstring(&b, authctxt->service);
618 buffer_put_cstring(&b, authctxt->method->name);
619 buffer_put_char(&b, have_sig);
620 if (!(datafellows & SSH_BUG_PKAUTH))
621 buffer_put_cstring(&b, key_ssh_name(k));
622 buffer_put_string(&b, blob, bloblen);
626 /* append signature */
627 buffer_put_string(&b, signature, slen);
630 /* skip session id and packet type */
631 if (buffer_len(&b) < skip + 1)
632 fatal("userauth_pubkey: internal error");
633 buffer_consume(&b, skip + 1);
635 /* put remaining data from buffer into packet */
636 packet_start(SSH2_MSG_USERAUTH_REQUEST);
637 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
645 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
649 u_int bloblen, have_sig = 0;
651 debug3("send_pubkey_test");
653 if (key_to_blob(k, &blob, &bloblen) == 0) {
654 /* we cannot handle this key */
655 debug3("send_pubkey_test: cannot handle key");
658 /* register callback for USERAUTH_PK_OK message */
659 authctxt->last_key_sign = sign_callback;
660 authctxt->last_key_hint = hint;
661 authctxt->last_key = k;
662 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
664 packet_start(SSH2_MSG_USERAUTH_REQUEST);
665 packet_put_cstring(authctxt->server_user);
666 packet_put_cstring(authctxt->service);
667 packet_put_cstring(authctxt->method->name);
668 packet_put_char(have_sig);
669 if (!(datafellows & SSH_BUG_PKAUTH))
670 packet_put_cstring(key_ssh_name(k));
671 packet_put_string(blob, bloblen);
678 load_identity_file(char *filename)
681 char prompt[300], *passphrase;
685 if (stat(filename, &st) < 0) {
686 debug3("no such identity: %s", filename);
689 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
690 if (private == NULL) {
691 if (options.batch_mode)
693 snprintf(prompt, sizeof prompt,
694 "Enter passphrase for key '%.100s': ", filename);
695 for (i = 0; i < options.number_of_password_prompts; i++) {
696 passphrase = read_passphrase(prompt, 0);
697 if (strcmp(passphrase, "") != 0) {
698 private = key_load_private_type(KEY_UNSPEC, filename,
702 debug2("no passphrase given, try next key");
705 memset(passphrase, 0, strlen(passphrase));
707 if (private != NULL || quit)
709 debug2("bad passphrase given, try again...");
716 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
717 u_char *data, u_int datalen)
722 idx = authctxt->last_key_hint;
726 /* private key is stored in external hardware */
727 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
728 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
730 private = load_identity_file(options.identity_files[idx]);
733 ret = key_sign(private, sigp, lenp, data, datalen);
739 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
740 u_char *data, u_int datalen)
742 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
746 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
747 u_char *data, u_int datalen)
749 return key_sign(key, sigp, lenp, data, datalen);
753 userauth_pubkey_agent(Authctxt *authctxt)
755 static int called = 0;
761 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
762 debug2("userauth_pubkey_agent: no keys at all");
765 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
767 debug2("userauth_pubkey_agent: no more keys");
769 debug("userauth_pubkey_agent: testing agent key %s", comment);
771 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
776 debug2("userauth_pubkey_agent: no message sent");
781 userauth_pubkey(Authctxt *authctxt)
788 if (authctxt->agent != NULL) {
790 sent = userauth_pubkey_agent(authctxt);
791 } while (!sent && authctxt->agent->howmany > 0);
793 while (!sent && idx < options.num_identity_files) {
794 key = options.identity_keys[idx];
795 filename = options.identity_files[idx];
797 debug("try privkey: %s", filename);
798 key = load_identity_file(filename);
800 sent = sign_and_send_pubkey(authctxt, key,
804 } else if (key->type != KEY_RSA1) {
805 debug("try pubkey: %s", filename);
806 sent = send_pubkey_test(authctxt, key,
807 identity_sign_cb, idx);
815 * Send userauth request message specifying keyboard-interactive method.
818 userauth_kbdint(Authctxt *authctxt)
820 static int attempt = 0;
822 if (attempt++ >= options.number_of_password_prompts)
824 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
825 if (attempt > 1 && !authctxt->info_req_seen) {
826 debug3("userauth_kbdint: disable: no info_req_seen");
827 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
831 debug2("userauth_kbdint");
832 packet_start(SSH2_MSG_USERAUTH_REQUEST);
833 packet_put_cstring(authctxt->server_user);
834 packet_put_cstring(authctxt->service);
835 packet_put_cstring(authctxt->method->name);
836 packet_put_cstring(""); /* lang */
837 packet_put_cstring(options.kbd_interactive_devices ?
838 options.kbd_interactive_devices : "");
841 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
846 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
849 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
851 Authctxt *authctxt = ctxt;
852 char *name, *inst, *lang, *prompt, *response;
853 u_int num_prompts, i;
856 debug2("input_userauth_info_req");
858 if (authctxt == NULL)
859 fatal("input_userauth_info_req: no authentication context");
861 authctxt->info_req_seen = 1;
863 name = packet_get_string(NULL);
864 inst = packet_get_string(NULL);
865 lang = packet_get_string(NULL);
866 if (strlen(name) > 0)
868 if (strlen(inst) > 0)
874 num_prompts = packet_get_int();
876 * Begin to build info response packet based on prompts requested.
877 * We commit to providing the correct number of responses, so if
878 * further on we run into a problem that prevents this, we have to
879 * be sure and clean this up and send a correct error response.
881 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
882 packet_put_int(num_prompts);
884 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
885 for (i = 0; i < num_prompts; i++) {
886 prompt = packet_get_string(NULL);
887 echo = packet_get_char();
889 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
891 packet_put_cstring(response);
892 memset(response, 0, strlen(response));
896 packet_check_eom(); /* done with parsing incoming message. */
898 packet_add_padding(64);
903 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
904 u_char *data, u_int datalen)
909 int to[2], from[2], status, version = 2;
911 debug("ssh_keysign called");
913 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
914 error("ssh_keysign: no installed: %s", strerror(errno));
917 if (fflush(stdout) != 0)
918 error("ssh_keysign: fflush: %s", strerror(errno));
920 error("ssh_keysign: pipe: %s", strerror(errno));
923 if (pipe(from) < 0) {
924 error("ssh_keysign: pipe: %s", strerror(errno));
927 if ((pid = fork()) < 0) {
928 error("ssh_keysign: fork: %s", strerror(errno));
935 if (dup2(from[1], STDOUT_FILENO) < 0)
936 fatal("ssh_keysign: dup2: %s", strerror(errno));
938 if (dup2(to[0], STDIN_FILENO) < 0)
939 fatal("ssh_keysign: dup2: %s", strerror(errno));
942 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
943 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
950 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
951 buffer_put_string(&b, data, datalen);
952 ssh_msg_send(to[1], version, &b);
954 if (ssh_msg_recv(from[0], &b) < 0) {
955 error("ssh_keysign: no reply");
962 while (waitpid(pid, &status, 0) < 0)
966 if (buffer_get_char(&b) != version) {
967 error("ssh_keysign: bad version");
971 *sigp = buffer_get_string(&b, lenp);
978 userauth_hostbased(Authctxt *authctxt)
981 Sensitive *sensitive = authctxt->sensitive;
983 u_char *signature, *blob;
984 char *chost, *pkalg, *p;
987 int ok, i, len, found = 0;
989 /* check for a useful key */
990 for (i = 0; i < sensitive->nkeys; i++) {
991 private = sensitive->keys[i];
992 if (private && private->type != KEY_RSA1) {
994 /* we take and free the key */
995 sensitive->keys[i] = NULL;
1000 debug("userauth_hostbased: no more client hostkeys");
1003 if (key_to_blob(private, &blob, &blen) == 0) {
1007 /* figure out a name for the client host */
1008 p = get_local_name(packet_get_connection_in());
1010 error("userauth_hostbased: cannot get local ipaddr/name");
1014 len = strlen(p) + 2;
1015 chost = xmalloc(len);
1016 strlcpy(chost, p, len);
1017 strlcat(chost, ".", len);
1018 debug2("userauth_hostbased: chost %s", chost);
1020 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1022 pkalg = xstrdup(key_ssh_name(private));
1024 /* construct data */
1025 buffer_put_string(&b, session_id2, session_id2_len);
1026 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1027 buffer_put_cstring(&b, authctxt->server_user);
1028 buffer_put_cstring(&b, service);
1029 buffer_put_cstring(&b, authctxt->method->name);
1030 buffer_put_cstring(&b, pkalg);
1031 buffer_put_string(&b, blob, blen);
1032 buffer_put_cstring(&b, chost);
1033 buffer_put_cstring(&b, authctxt->local_user);
1037 if (sensitive->external_keysign)
1038 ok = ssh_keysign(private, &signature, &slen,
1039 buffer_ptr(&b), buffer_len(&b));
1041 ok = key_sign(private, &signature, &slen,
1042 buffer_ptr(&b), buffer_len(&b));
1046 error("key_sign failed");
1051 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1052 packet_put_cstring(authctxt->server_user);
1053 packet_put_cstring(authctxt->service);
1054 packet_put_cstring(authctxt->method->name);
1055 packet_put_cstring(pkalg);
1056 packet_put_string(blob, blen);
1057 packet_put_cstring(chost);
1058 packet_put_cstring(authctxt->local_user);
1059 packet_put_string(signature, slen);
1060 memset(signature, 's', slen);
1069 /* find auth method */
1072 * given auth method name, if configurable options permit this method fill
1073 * in auth_ident field and return true, otherwise return false.
1076 authmethod_is_enabled(Authmethod *method)
1080 /* return false if options indicate this method is disabled */
1081 if (method->enabled == NULL || *method->enabled == 0)
1083 /* return false if batch mode is enabled but method needs interactive mode */
1084 if (method->batch_flag != NULL && *method->batch_flag != 0)
1090 authmethod_lookup(const char *name)
1092 Authmethod *method = NULL;
1094 for (method = authmethods; method->name != NULL; method++)
1095 if (strcmp(name, method->name) == 0)
1097 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1101 /* XXX internal state */
1102 static Authmethod *current = NULL;
1103 static char *supported = NULL;
1104 static char *preferred = NULL;
1107 * Given the authentication method list sent by the server, return the
1108 * next method we should try. If the server initially sends a nil list,
1109 * use a built-in default list.
1112 authmethod_get(char *authlist)
1118 /* Use a suitable default if we're passed a nil list. */
1119 if (authlist == NULL || strlen(authlist) == 0)
1120 authlist = options.preferred_authentications;
1122 if (supported == NULL || strcmp(authlist, supported) != 0) {
1123 debug3("start over, passed a different list %s", authlist);
1124 if (supported != NULL)
1126 supported = xstrdup(authlist);
1127 preferred = options.preferred_authentications;
1128 debug3("preferred %s", preferred);
1130 } else if (current != NULL && authmethod_is_enabled(current))
1134 if ((name = match_list(preferred, supported, &next)) == NULL) {
1135 debug("no more auth methods to try");
1140 debug3("authmethod_lookup %s", name);
1141 debug3("remaining preferred: %s", preferred);
1142 if ((current = authmethod_lookup(name)) != NULL &&
1143 authmethod_is_enabled(current)) {
1144 debug3("authmethod_is_enabled %s", name);
1145 debug("next auth method to try is %s", name);
1152 authmethods_get(void)
1154 Authmethod *method = NULL;
1159 for (method = authmethods; method->name != NULL; method++) {
1160 if (authmethod_is_enabled(method)) {
1161 if (buffer_len(&b) > 0)
1162 buffer_append(&b, ",", 1);
1163 buffer_append(&b, method->name, strlen(method->name));
1166 buffer_append(&b, "\0", 1);
1167 list = xstrdup(buffer_ptr(&b));