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.117 2003/05/12 16:55:37 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;
144 typedef struct identity Identity;
145 typedef struct idlist Idlist;
148 TAILQ_ENTRY(identity) next;
149 AuthenticationConnection *ac; /* set if agent supports key */
150 Key *key; /* public/private key */
151 char *filename; /* comment for agent-only keys */
153 int isprivate; /* key points to the private key */
155 TAILQ_HEAD(idlist, identity);
158 const char *server_user;
159 const char *local_user;
167 AuthenticationConnection *agent;
169 Sensitive *sensitive;
170 /* kbd-interactive */
174 char *name; /* string to compare against server's list */
175 int (*userauth)(Authctxt *authctxt);
176 int *enabled; /* flag in option struct that enables method */
177 int *batch_flag; /* flag in option struct that disables method */
180 void input_userauth_success(int, u_int32_t, void *);
181 void input_userauth_failure(int, u_int32_t, void *);
182 void input_userauth_banner(int, u_int32_t, void *);
183 void input_userauth_error(int, u_int32_t, void *);
184 void input_userauth_info_req(int, u_int32_t, void *);
185 void input_userauth_pk_ok(int, u_int32_t, void *);
186 void input_userauth_passwd_changereq(int, u_int32_t, void *);
188 int userauth_none(Authctxt *);
189 int userauth_pubkey(Authctxt *);
190 int userauth_passwd(Authctxt *);
191 int userauth_kbdint(Authctxt *);
192 int userauth_hostbased(Authctxt *);
194 void userauth(Authctxt *, char *);
196 static int sign_and_send_pubkey(Authctxt *, Identity *);
197 static void clear_auth_state(Authctxt *);
198 static void pubkey_prepare(Authctxt *);
199 static void pubkey_cleanup(Authctxt *);
200 static Key *load_identity_file(char *);
202 static Authmethod *authmethod_get(char *authlist);
203 static Authmethod *authmethod_lookup(const char *name);
204 static char *authmethods_get(void);
206 Authmethod authmethods[] = {
209 &options.hostbased_authentication,
213 &options.pubkey_authentication,
215 {"keyboard-interactive",
217 &options.kbd_interactive_authentication,
218 &options.batch_mode},
221 &options.password_authentication,
222 &options.batch_mode},
227 {NULL, NULL, NULL, NULL}
231 ssh_userauth2(const char *local_user, const char *server_user, char *host,
232 Sensitive *sensitive)
237 if (options.challenge_response_authentication)
238 options.kbd_interactive_authentication = 1;
240 packet_start(SSH2_MSG_SERVICE_REQUEST);
241 packet_put_cstring("ssh-userauth");
243 debug("SSH2_MSG_SERVICE_REQUEST sent");
245 type = packet_read();
246 if (type != SSH2_MSG_SERVICE_ACCEPT)
247 fatal("Server denied authentication request: %d", type);
248 if (packet_remaining() > 0) {
249 char *reply = packet_get_string(NULL);
250 debug2("service_accept: %s", reply);
253 debug2("buggy server: service_accept w/o service");
256 debug("SSH2_MSG_SERVICE_ACCEPT received");
258 if (options.preferred_authentications == NULL)
259 options.preferred_authentications = authmethods_get();
261 /* setup authentication context */
262 memset(&authctxt, 0, sizeof(authctxt));
263 pubkey_prepare(&authctxt);
264 authctxt.server_user = server_user;
265 authctxt.local_user = local_user;
266 authctxt.host = host;
267 authctxt.service = "ssh-connection"; /* service name */
268 authctxt.success = 0;
269 authctxt.method = authmethod_lookup("none");
270 authctxt.authlist = NULL;
271 authctxt.sensitive = sensitive;
272 authctxt.info_req_seen = 0;
273 if (authctxt.method == NULL)
274 fatal("ssh_userauth2: internal error: cannot send userauth none request");
276 /* initial userauth request */
277 userauth_none(&authctxt);
279 dispatch_init(&input_userauth_error);
280 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
281 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
282 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
283 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
285 pubkey_cleanup(&authctxt);
286 debug("Authentication succeeded (%s).", authctxt.method->name);
289 userauth(Authctxt *authctxt, char *authlist)
291 if (authlist == NULL) {
292 authlist = authctxt->authlist;
294 if (authctxt->authlist)
295 xfree(authctxt->authlist);
296 authctxt->authlist = authlist;
299 Authmethod *method = authmethod_get(authlist);
301 fatal("Permission denied (%s).", authlist);
302 authctxt->method = method;
303 if (method->userauth(authctxt) != 0) {
304 debug2("we sent a %s packet, wait for reply", method->name);
307 debug2("we did not send a packet, disable method");
308 method->enabled = NULL;
314 input_userauth_error(int type, u_int32_t seq, void *ctxt)
316 fatal("input_userauth_error: bad message during authentication: "
321 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
324 debug3("input_userauth_banner");
325 msg = packet_get_string(NULL);
326 lang = packet_get_string(NULL);
327 fprintf(stderr, "%s", msg);
333 input_userauth_success(int type, u_int32_t seq, void *ctxt)
335 Authctxt *authctxt = ctxt;
336 if (authctxt == NULL)
337 fatal("input_userauth_success: no authentication context");
338 if (authctxt->authlist)
339 xfree(authctxt->authlist);
340 clear_auth_state(authctxt);
341 authctxt->success = 1; /* break out */
345 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
347 Authctxt *authctxt = ctxt;
348 char *authlist = NULL;
351 if (authctxt == NULL)
352 fatal("input_userauth_failure: no authentication context");
354 authlist = packet_get_string(NULL);
355 partial = packet_get_char();
359 logit("Authenticated with partial success.");
360 debug("Authentications that can continue: %s", authlist);
362 clear_auth_state(authctxt);
363 userauth(authctxt, authlist);
366 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
368 Authctxt *authctxt = ctxt;
372 int pktype, sent = 0;
377 if (authctxt == NULL)
378 fatal("input_userauth_pk_ok: no authentication context");
379 if (datafellows & SSH_BUG_PKOK) {
380 /* this is similar to SSH_BUG_PKAUTH */
381 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
382 pkblob = packet_get_string(&blen);
384 buffer_append(&b, pkblob, blen);
385 pkalg = buffer_get_string(&b, &alen);
388 pkalg = packet_get_string(&alen);
389 pkblob = packet_get_string(&blen);
393 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
395 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
396 debug("unknown pkalg %s", pkalg);
399 if ((key = key_from_blob(pkblob, blen)) == NULL) {
400 debug("no key from blob. pkalg %s", pkalg);
403 if (key->type != pktype) {
404 error("input_userauth_pk_ok: type mismatch "
405 "for decoded key (received %d, expected %d)",
409 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
410 debug2("input_userauth_pk_ok: fp %s", fp);
413 TAILQ_FOREACH(id, &authctxt->keys, next) {
414 if (key_equal(key, id->key)) {
415 sent = sign_and_send_pubkey(authctxt, id);
426 clear_auth_state(authctxt);
427 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
429 /* try another method if we did not send a packet */
431 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);
553 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
554 u_char *data, u_int datalen)
559 /* the agent supports this key */
561 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
564 * we have already loaded the private key or
565 * the private key is stored in external hardware
567 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
568 return (key_sign(id->key, sigp, lenp, data, datalen));
569 /* load the private key from the file */
570 if ((prv = load_identity_file(id->filename)) == NULL)
572 ret = key_sign(prv, sigp, lenp, data, datalen);
578 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
581 u_char *blob, *signature;
587 debug3("sign_and_send_pubkey");
589 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
590 /* we cannot handle this key */
591 debug3("sign_and_send_pubkey: cannot handle key");
594 /* data to be signed */
596 if (datafellows & SSH_OLD_SESSIONID) {
597 buffer_append(&b, session_id2, session_id2_len);
598 skip = session_id2_len;
600 buffer_put_string(&b, session_id2, session_id2_len);
601 skip = buffer_len(&b);
603 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
604 buffer_put_cstring(&b, authctxt->server_user);
605 buffer_put_cstring(&b,
606 datafellows & SSH_BUG_PKSERVICE ?
609 if (datafellows & SSH_BUG_PKAUTH) {
610 buffer_put_char(&b, have_sig);
612 buffer_put_cstring(&b, authctxt->method->name);
613 buffer_put_char(&b, have_sig);
614 buffer_put_cstring(&b, key_ssh_name(id->key));
616 buffer_put_string(&b, blob, bloblen);
618 /* generate signature */
619 ret = identity_sign(id, &signature, &slen,
620 buffer_ptr(&b), buffer_len(&b));
629 if (datafellows & SSH_BUG_PKSERVICE) {
631 buffer_append(&b, session_id2, session_id2_len);
632 skip = session_id2_len;
633 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
634 buffer_put_cstring(&b, authctxt->server_user);
635 buffer_put_cstring(&b, authctxt->service);
636 buffer_put_cstring(&b, authctxt->method->name);
637 buffer_put_char(&b, have_sig);
638 if (!(datafellows & SSH_BUG_PKAUTH))
639 buffer_put_cstring(&b, key_ssh_name(id->key));
640 buffer_put_string(&b, blob, bloblen);
644 /* append signature */
645 buffer_put_string(&b, signature, slen);
648 /* skip session id and packet type */
649 if (buffer_len(&b) < skip + 1)
650 fatal("userauth_pubkey: internal error");
651 buffer_consume(&b, skip + 1);
653 /* put remaining data from buffer into packet */
654 packet_start(SSH2_MSG_USERAUTH_REQUEST);
655 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
663 send_pubkey_test(Authctxt *authctxt, Identity *id)
666 u_int bloblen, have_sig = 0;
668 debug3("send_pubkey_test");
670 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
671 /* we cannot handle this key */
672 debug3("send_pubkey_test: cannot handle key");
675 /* register callback for USERAUTH_PK_OK message */
676 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
678 packet_start(SSH2_MSG_USERAUTH_REQUEST);
679 packet_put_cstring(authctxt->server_user);
680 packet_put_cstring(authctxt->service);
681 packet_put_cstring(authctxt->method->name);
682 packet_put_char(have_sig);
683 if (!(datafellows & SSH_BUG_PKAUTH))
684 packet_put_cstring(key_ssh_name(id->key));
685 packet_put_string(blob, bloblen);
692 load_identity_file(char *filename)
695 char prompt[300], *passphrase;
699 if (stat(filename, &st) < 0) {
700 debug3("no such identity: %s", filename);
703 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
704 if (private == NULL) {
705 if (options.batch_mode)
707 snprintf(prompt, sizeof prompt,
708 "Enter passphrase for key '%.100s': ", filename);
709 for (i = 0; i < options.number_of_password_prompts; i++) {
710 passphrase = read_passphrase(prompt, 0);
711 if (strcmp(passphrase, "") != 0) {
712 private = key_load_private_type(KEY_UNSPEC, filename,
716 debug2("no passphrase given, try next key");
719 memset(passphrase, 0, strlen(passphrase));
721 if (private != NULL || quit)
723 debug2("bad passphrase given, try again...");
730 * try keys in the following order:
731 * 1. agent keys that are found in the config file
732 * 2. other agent keys
733 * 3. keys that are only listed in the config file
736 pubkey_prepare(Authctxt *authctxt)
739 Idlist agent, files, *preferred;
741 AuthenticationConnection *ac;
745 TAILQ_INIT(&agent); /* keys from the agent */
746 TAILQ_INIT(&files); /* keys from the config file */
747 preferred = &authctxt->keys;
748 TAILQ_INIT(preferred); /* preferred order of keys */
750 /* list of keys stored in the filesystem */
751 for (i = 0; i < options.num_identity_files; i++) {
752 key = options.identity_keys[i];
753 if (key && key->type == KEY_RSA1)
755 options.identity_keys[i] = NULL;
756 id = xmalloc(sizeof(*id));
757 memset(id, 0, sizeof(*id));
759 id->filename = xstrdup(options.identity_files[i]);
760 TAILQ_INSERT_TAIL(&files, id, next);
762 /* list of keys supported by the agent */
763 if ((ac = ssh_get_authentication_connection())) {
764 for (key = ssh_get_first_identity(ac, &comment, 2);
766 key = ssh_get_next_identity(ac, &comment, 2)) {
768 TAILQ_FOREACH(id, &files, next) {
769 /* agent keys from the config file are preferred */
770 if (key_equal(key, id->key)) {
773 TAILQ_REMOVE(&files, id, next);
774 TAILQ_INSERT_TAIL(preferred, id, next);
781 id = xmalloc(sizeof(*id));
782 memset(id, 0, sizeof(*id));
784 id->filename = comment;
786 TAILQ_INSERT_TAIL(&agent, id, next);
789 /* append remaining agent keys */
790 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
791 TAILQ_REMOVE(&agent, id, next);
792 TAILQ_INSERT_TAIL(preferred, id, next);
794 authctxt->agent = ac;
796 /* append remaining keys from the config file */
797 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
798 TAILQ_REMOVE(&files, id, next);
799 TAILQ_INSERT_TAIL(preferred, id, next);
801 TAILQ_FOREACH(id, preferred, next) {
802 debug2("key: %s (%p)", id->filename, id->key);
807 pubkey_cleanup(Authctxt *authctxt)
811 if (authctxt->agent != NULL)
812 ssh_close_authentication_connection(authctxt->agent);
813 for (id = TAILQ_FIRST(&authctxt->keys); id;
814 id = TAILQ_FIRST(&authctxt->keys)) {
815 TAILQ_REMOVE(&authctxt->keys, id, next);
825 userauth_pubkey(Authctxt *authctxt)
830 while ((id = TAILQ_FIRST(&authctxt->keys))) {
833 TAILQ_REMOVE(&authctxt->keys, id, next);
834 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
836 * send a test message if we have the public key. for
837 * encrypted keys we cannot do this and have to load the
838 * private key instead
840 if (id->key && id->key->type != KEY_RSA1) {
841 debug("Offering public key: %s", id->filename);
842 sent = send_pubkey_test(authctxt, id);
843 } else if (id->key == NULL) {
844 debug("Trying private key: %s", id->filename);
845 id->key = load_identity_file(id->filename);
846 if (id->key != NULL) {
848 sent = sign_and_send_pubkey(authctxt, id);
860 * Send userauth request message specifying keyboard-interactive method.
863 userauth_kbdint(Authctxt *authctxt)
865 static int attempt = 0;
867 if (attempt++ >= options.number_of_password_prompts)
869 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
870 if (attempt > 1 && !authctxt->info_req_seen) {
871 debug3("userauth_kbdint: disable: no info_req_seen");
872 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
876 debug2("userauth_kbdint");
877 packet_start(SSH2_MSG_USERAUTH_REQUEST);
878 packet_put_cstring(authctxt->server_user);
879 packet_put_cstring(authctxt->service);
880 packet_put_cstring(authctxt->method->name);
881 packet_put_cstring(""); /* lang */
882 packet_put_cstring(options.kbd_interactive_devices ?
883 options.kbd_interactive_devices : "");
886 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
891 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
894 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
896 Authctxt *authctxt = ctxt;
897 char *name, *inst, *lang, *prompt, *response;
898 u_int num_prompts, i;
901 debug2("input_userauth_info_req");
903 if (authctxt == NULL)
904 fatal("input_userauth_info_req: no authentication context");
906 authctxt->info_req_seen = 1;
908 name = packet_get_string(NULL);
909 inst = packet_get_string(NULL);
910 lang = packet_get_string(NULL);
911 if (strlen(name) > 0)
913 if (strlen(inst) > 0)
919 num_prompts = packet_get_int();
921 * Begin to build info response packet based on prompts requested.
922 * We commit to providing the correct number of responses, so if
923 * further on we run into a problem that prevents this, we have to
924 * be sure and clean this up and send a correct error response.
926 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
927 packet_put_int(num_prompts);
929 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
930 for (i = 0; i < num_prompts; i++) {
931 prompt = packet_get_string(NULL);
932 echo = packet_get_char();
934 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
936 packet_put_cstring(response);
937 memset(response, 0, strlen(response));
941 packet_check_eom(); /* done with parsing incoming message. */
943 packet_add_padding(64);
948 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
949 u_char *data, u_int datalen)
954 int to[2], from[2], status, version = 2;
956 debug2("ssh_keysign called");
958 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
959 error("ssh_keysign: no installed: %s", strerror(errno));
962 if (fflush(stdout) != 0)
963 error("ssh_keysign: fflush: %s", strerror(errno));
965 error("ssh_keysign: pipe: %s", strerror(errno));
968 if (pipe(from) < 0) {
969 error("ssh_keysign: pipe: %s", strerror(errno));
972 if ((pid = fork()) < 0) {
973 error("ssh_keysign: fork: %s", strerror(errno));
980 if (dup2(from[1], STDOUT_FILENO) < 0)
981 fatal("ssh_keysign: dup2: %s", strerror(errno));
983 if (dup2(to[0], STDIN_FILENO) < 0)
984 fatal("ssh_keysign: dup2: %s", strerror(errno));
987 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
988 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
995 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
996 buffer_put_string(&b, data, datalen);
997 ssh_msg_send(to[1], version, &b);
999 if (ssh_msg_recv(from[0], &b) < 0) {
1000 error("ssh_keysign: no reply");
1007 while (waitpid(pid, &status, 0) < 0)
1011 if (buffer_get_char(&b) != version) {
1012 error("ssh_keysign: bad version");
1016 *sigp = buffer_get_string(&b, lenp);
1023 userauth_hostbased(Authctxt *authctxt)
1025 Key *private = NULL;
1026 Sensitive *sensitive = authctxt->sensitive;
1028 u_char *signature, *blob;
1029 char *chost, *pkalg, *p;
1030 const char *service;
1032 int ok, i, len, found = 0;
1034 /* check for a useful key */
1035 for (i = 0; i < sensitive->nkeys; i++) {
1036 private = sensitive->keys[i];
1037 if (private && private->type != KEY_RSA1) {
1039 /* we take and free the key */
1040 sensitive->keys[i] = NULL;
1045 debug("No more client hostkeys for hostbased authentication.");
1048 if (key_to_blob(private, &blob, &blen) == 0) {
1052 /* figure out a name for the client host */
1053 p = get_local_name(packet_get_connection_in());
1055 error("userauth_hostbased: cannot get local ipaddr/name");
1059 len = strlen(p) + 2;
1060 chost = xmalloc(len);
1061 strlcpy(chost, p, len);
1062 strlcat(chost, ".", len);
1063 debug2("userauth_hostbased: chost %s", chost);
1066 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1068 pkalg = xstrdup(key_ssh_name(private));
1070 /* construct data */
1071 buffer_put_string(&b, session_id2, session_id2_len);
1072 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1073 buffer_put_cstring(&b, authctxt->server_user);
1074 buffer_put_cstring(&b, service);
1075 buffer_put_cstring(&b, authctxt->method->name);
1076 buffer_put_cstring(&b, pkalg);
1077 buffer_put_string(&b, blob, blen);
1078 buffer_put_cstring(&b, chost);
1079 buffer_put_cstring(&b, authctxt->local_user);
1083 if (sensitive->external_keysign)
1084 ok = ssh_keysign(private, &signature, &slen,
1085 buffer_ptr(&b), buffer_len(&b));
1087 ok = key_sign(private, &signature, &slen,
1088 buffer_ptr(&b), buffer_len(&b));
1092 error("key_sign failed");
1097 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1098 packet_put_cstring(authctxt->server_user);
1099 packet_put_cstring(authctxt->service);
1100 packet_put_cstring(authctxt->method->name);
1101 packet_put_cstring(pkalg);
1102 packet_put_string(blob, blen);
1103 packet_put_cstring(chost);
1104 packet_put_cstring(authctxt->local_user);
1105 packet_put_string(signature, slen);
1106 memset(signature, 's', slen);
1115 /* find auth method */
1118 * given auth method name, if configurable options permit this method fill
1119 * in auth_ident field and return true, otherwise return false.
1122 authmethod_is_enabled(Authmethod *method)
1126 /* return false if options indicate this method is disabled */
1127 if (method->enabled == NULL || *method->enabled == 0)
1129 /* return false if batch mode is enabled but method needs interactive mode */
1130 if (method->batch_flag != NULL && *method->batch_flag != 0)
1136 authmethod_lookup(const char *name)
1138 Authmethod *method = NULL;
1140 for (method = authmethods; method->name != NULL; method++)
1141 if (strcmp(name, method->name) == 0)
1143 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1147 /* XXX internal state */
1148 static Authmethod *current = NULL;
1149 static char *supported = NULL;
1150 static char *preferred = NULL;
1153 * Given the authentication method list sent by the server, return the
1154 * next method we should try. If the server initially sends a nil list,
1155 * use a built-in default list.
1158 authmethod_get(char *authlist)
1163 /* Use a suitable default if we're passed a nil list. */
1164 if (authlist == NULL || strlen(authlist) == 0)
1165 authlist = options.preferred_authentications;
1167 if (supported == NULL || strcmp(authlist, supported) != 0) {
1168 debug3("start over, passed a different list %s", authlist);
1169 if (supported != NULL)
1171 supported = xstrdup(authlist);
1172 preferred = options.preferred_authentications;
1173 debug3("preferred %s", preferred);
1175 } else if (current != NULL && authmethod_is_enabled(current))
1179 if ((name = match_list(preferred, supported, &next)) == NULL) {
1180 debug("No more authentication methods to try.");
1185 debug3("authmethod_lookup %s", name);
1186 debug3("remaining preferred: %s", preferred);
1187 if ((current = authmethod_lookup(name)) != NULL &&
1188 authmethod_is_enabled(current)) {
1189 debug3("authmethod_is_enabled %s", name);
1190 debug("Next authentication method: %s", name);
1197 authmethods_get(void)
1199 Authmethod *method = NULL;
1204 for (method = authmethods; method->name != NULL; method++) {
1205 if (authmethod_is_enabled(method)) {
1206 if (buffer_len(&b) > 0)
1207 buffer_append(&b, ",", 1);
1208 buffer_append(&b, method->name, strlen(method->name));
1211 buffer_append(&b, "\0", 1);
1212 list = xstrdup(buffer_ptr(&b));