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.120 2003/06/24 08:23:46 markus Exp $");
31 #define krb5_get_err_text(context,code) error_message(code)
35 #include "openbsd-compat/sys-queue.h"
46 #include "myproposal.h"
47 #include "sshconnect.h"
58 #include "pathnames.h"
61 extern char *client_version_string;
62 extern char *server_version_string;
63 extern Options options;
69 u_char *session_id2 = NULL;
70 u_int session_id2_len = 0;
73 struct sockaddr *xxx_hostaddr;
78 verify_host_key_callback(Key *hostkey)
80 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
81 fatal("Host key verification failed.");
86 ssh_kex2(char *host, struct sockaddr *hostaddr)
91 xxx_hostaddr = hostaddr;
93 if (options.ciphers == (char *)-1) {
94 logit("No valid ciphers for protocol version 2 given, using defaults.");
95 options.ciphers = NULL;
97 if (options.ciphers != NULL) {
98 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
101 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
102 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
103 myproposal[PROPOSAL_ENC_ALGS_STOC] =
104 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
105 if (options.compression) {
106 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
107 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
109 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
110 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
112 if (options.macs != NULL) {
113 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
114 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
116 if (options.hostkeyalgorithms != NULL)
117 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
118 options.hostkeyalgorithms;
120 if (options.rekey_limit)
121 packet_set_rekey_limit(options.rekey_limit);
123 /* start key exchange */
124 kex = kex_setup(myproposal);
125 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
126 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
127 kex->client_version_string=client_version_string;
128 kex->server_version_string=server_version_string;
129 kex->verify_host_key=&verify_host_key_callback;
133 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
135 session_id2 = kex->session_id;
136 session_id2_len = kex->session_id_len;
139 /* send 1st encrypted/maced/compressed message */
140 packet_start(SSH2_MSG_IGNORE);
141 packet_put_cstring("markus");
151 typedef struct Authctxt Authctxt;
152 typedef struct Authmethod Authmethod;
153 typedef struct identity Identity;
154 typedef struct idlist Idlist;
157 TAILQ_ENTRY(identity) next;
158 AuthenticationConnection *ac; /* set if agent supports key */
159 Key *key; /* public/private key */
160 char *filename; /* comment for agent-only keys */
162 int isprivate; /* key points to the private key */
164 TAILQ_HEAD(idlist, identity);
167 const char *server_user;
168 const char *local_user;
176 AuthenticationConnection *agent;
178 Sensitive *sensitive;
179 /* kbd-interactive */
183 char *name; /* string to compare against server's list */
184 int (*userauth)(Authctxt *authctxt);
185 int *enabled; /* flag in option struct that enables method */
186 int *batch_flag; /* flag in option struct that disables method */
189 void input_userauth_success(int, u_int32_t, void *);
190 void input_userauth_failure(int, u_int32_t, void *);
191 void input_userauth_banner(int, u_int32_t, void *);
192 void input_userauth_error(int, u_int32_t, void *);
193 void input_userauth_info_req(int, u_int32_t, void *);
194 void input_userauth_pk_ok(int, u_int32_t, void *);
195 void input_userauth_passwd_changereq(int, u_int32_t, void *);
197 int userauth_none(Authctxt *);
198 int userauth_pubkey(Authctxt *);
199 int userauth_passwd(Authctxt *);
200 int userauth_kbdint(Authctxt *);
201 int userauth_hostbased(Authctxt *);
202 int userauth_kerberos(Authctxt *);
204 void userauth(Authctxt *, char *);
206 static int sign_and_send_pubkey(Authctxt *, Identity *);
207 static void pubkey_prepare(Authctxt *);
208 static void pubkey_cleanup(Authctxt *);
209 static Key *load_identity_file(char *);
211 static Authmethod *authmethod_get(char *authlist);
212 static Authmethod *authmethod_lookup(const char *name);
213 static char *authmethods_get(void);
215 Authmethod authmethods[] = {
218 &options.hostbased_authentication,
221 {"kerberos-2@ssh.com",
223 &options.kerberos_authentication,
228 &options.pubkey_authentication,
230 {"keyboard-interactive",
232 &options.kbd_interactive_authentication,
233 &options.batch_mode},
236 &options.password_authentication,
237 &options.batch_mode},
242 {NULL, NULL, NULL, NULL}
246 ssh_userauth2(const char *local_user, const char *server_user, char *host,
247 Sensitive *sensitive)
252 if (options.challenge_response_authentication)
253 options.kbd_interactive_authentication = 1;
255 packet_start(SSH2_MSG_SERVICE_REQUEST);
256 packet_put_cstring("ssh-userauth");
258 debug("SSH2_MSG_SERVICE_REQUEST sent");
260 type = packet_read();
261 if (type != SSH2_MSG_SERVICE_ACCEPT)
262 fatal("Server denied authentication request: %d", type);
263 if (packet_remaining() > 0) {
264 char *reply = packet_get_string(NULL);
265 debug2("service_accept: %s", reply);
268 debug2("buggy server: service_accept w/o service");
271 debug("SSH2_MSG_SERVICE_ACCEPT received");
273 if (options.preferred_authentications == NULL)
274 options.preferred_authentications = authmethods_get();
276 /* setup authentication context */
277 memset(&authctxt, 0, sizeof(authctxt));
278 pubkey_prepare(&authctxt);
279 authctxt.server_user = server_user;
280 authctxt.local_user = local_user;
281 authctxt.host = host;
282 authctxt.service = "ssh-connection"; /* service name */
283 authctxt.success = 0;
284 authctxt.method = authmethod_lookup("none");
285 authctxt.authlist = NULL;
286 authctxt.sensitive = sensitive;
287 authctxt.info_req_seen = 0;
288 if (authctxt.method == NULL)
289 fatal("ssh_userauth2: internal error: cannot send userauth none request");
291 /* initial userauth request */
292 userauth_none(&authctxt);
294 dispatch_init(&input_userauth_error);
295 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
296 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
297 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
298 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
300 pubkey_cleanup(&authctxt);
301 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
303 debug("Authentication succeeded (%s).", authctxt.method->name);
307 userauth(Authctxt *authctxt, char *authlist)
309 if (authlist == NULL) {
310 authlist = authctxt->authlist;
312 if (authctxt->authlist)
313 xfree(authctxt->authlist);
314 authctxt->authlist = authlist;
317 Authmethod *method = authmethod_get(authlist);
319 fatal("Permission denied (%s).", authlist);
320 authctxt->method = method;
322 /* reset the per method handler */
323 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
324 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
326 /* and try new method */
327 if (method->userauth(authctxt) != 0) {
328 debug2("we sent a %s packet, wait for reply", method->name);
331 debug2("we did not send a packet, disable method");
332 method->enabled = NULL;
338 input_userauth_error(int type, u_int32_t seq, void *ctxt)
340 fatal("input_userauth_error: bad message during authentication: "
345 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
348 debug3("input_userauth_banner");
349 msg = packet_get_string(NULL);
350 lang = packet_get_string(NULL);
351 fprintf(stderr, "%s", msg);
357 input_userauth_success(int type, u_int32_t seq, void *ctxt)
359 Authctxt *authctxt = ctxt;
360 if (authctxt == NULL)
361 fatal("input_userauth_success: no authentication context");
362 if (authctxt->authlist)
363 xfree(authctxt->authlist);
364 authctxt->success = 1; /* break out */
368 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
370 Authctxt *authctxt = ctxt;
371 char *authlist = NULL;
374 if (authctxt == NULL)
375 fatal("input_userauth_failure: no authentication context");
377 authlist = packet_get_string(NULL);
378 partial = packet_get_char();
382 logit("Authenticated with partial success.");
383 debug("Authentications that can continue: %s", authlist);
385 userauth(authctxt, authlist);
388 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
390 Authctxt *authctxt = ctxt;
394 int pktype, sent = 0;
399 if (authctxt == NULL)
400 fatal("input_userauth_pk_ok: no authentication context");
401 if (datafellows & SSH_BUG_PKOK) {
402 /* this is similar to SSH_BUG_PKAUTH */
403 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
404 pkblob = packet_get_string(&blen);
406 buffer_append(&b, pkblob, blen);
407 pkalg = buffer_get_string(&b, &alen);
410 pkalg = packet_get_string(&alen);
411 pkblob = packet_get_string(&blen);
415 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
417 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
418 debug("unknown pkalg %s", pkalg);
421 if ((key = key_from_blob(pkblob, blen)) == NULL) {
422 debug("no key from blob. pkalg %s", pkalg);
425 if (key->type != pktype) {
426 error("input_userauth_pk_ok: type mismatch "
427 "for decoded key (received %d, expected %d)",
431 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
432 debug2("input_userauth_pk_ok: fp %s", fp);
435 TAILQ_FOREACH(id, &authctxt->keys, next) {
436 if (key_equal(key, id->key)) {
437 sent = sign_and_send_pubkey(authctxt, id);
447 /* try another method if we did not send a packet */
449 userauth(authctxt, NULL);
453 userauth_none(Authctxt *authctxt)
455 /* initial userauth request */
456 packet_start(SSH2_MSG_USERAUTH_REQUEST);
457 packet_put_cstring(authctxt->server_user);
458 packet_put_cstring(authctxt->service);
459 packet_put_cstring(authctxt->method->name);
465 userauth_passwd(Authctxt *authctxt)
467 static int attempt = 0;
471 if (attempt++ >= options.number_of_password_prompts)
475 error("Permission denied, please try again.");
477 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
478 authctxt->server_user, authctxt->host);
479 password = read_passphrase(prompt, 0);
480 packet_start(SSH2_MSG_USERAUTH_REQUEST);
481 packet_put_cstring(authctxt->server_user);
482 packet_put_cstring(authctxt->service);
483 packet_put_cstring(authctxt->method->name);
485 packet_put_cstring(password);
486 memset(password, 0, strlen(password));
488 packet_add_padding(64);
491 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
492 &input_userauth_passwd_changereq);
497 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
500 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
502 Authctxt *authctxt = ctxt;
503 char *info, *lang, *password = NULL, *retype = NULL;
506 debug2("input_userauth_passwd_changereq");
508 if (authctxt == NULL)
509 fatal("input_userauth_passwd_changereq: "
510 "no authentication context");
512 info = packet_get_string(NULL);
513 lang = packet_get_string(NULL);
514 if (strlen(info) > 0)
518 packet_start(SSH2_MSG_USERAUTH_REQUEST);
519 packet_put_cstring(authctxt->server_user);
520 packet_put_cstring(authctxt->service);
521 packet_put_cstring(authctxt->method->name);
522 packet_put_char(1); /* additional info */
523 snprintf(prompt, sizeof(prompt),
524 "Enter %.30s@%.128s's old password: ",
525 authctxt->server_user, authctxt->host);
526 password = read_passphrase(prompt, 0);
527 packet_put_cstring(password);
528 memset(password, 0, strlen(password));
531 while (password == NULL) {
532 snprintf(prompt, sizeof(prompt),
533 "Enter %.30s@%.128s's new password: ",
534 authctxt->server_user, authctxt->host);
535 password = read_passphrase(prompt, RP_ALLOW_EOF);
536 if (password == NULL) {
540 snprintf(prompt, sizeof(prompt),
541 "Retype %.30s@%.128s's new password: ",
542 authctxt->server_user, authctxt->host);
543 retype = read_passphrase(prompt, 0);
544 if (strcmp(password, retype) != 0) {
545 memset(password, 0, strlen(password));
547 logit("Mismatch; try again, EOF to quit.");
550 memset(retype, 0, strlen(retype));
553 packet_put_cstring(password);
554 memset(password, 0, strlen(password));
556 packet_add_padding(64);
559 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
560 &input_userauth_passwd_changereq);
564 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
565 u_char *data, u_int datalen)
570 /* the agent supports this key */
572 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
575 * we have already loaded the private key or
576 * the private key is stored in external hardware
578 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
579 return (key_sign(id->key, sigp, lenp, data, datalen));
580 /* load the private key from the file */
581 if ((prv = load_identity_file(id->filename)) == NULL)
583 ret = key_sign(prv, sigp, lenp, data, datalen);
589 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
592 u_char *blob, *signature;
598 debug3("sign_and_send_pubkey");
600 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
601 /* we cannot handle this key */
602 debug3("sign_and_send_pubkey: cannot handle key");
605 /* data to be signed */
607 if (datafellows & SSH_OLD_SESSIONID) {
608 buffer_append(&b, session_id2, session_id2_len);
609 skip = session_id2_len;
611 buffer_put_string(&b, session_id2, session_id2_len);
612 skip = buffer_len(&b);
614 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
615 buffer_put_cstring(&b, authctxt->server_user);
616 buffer_put_cstring(&b,
617 datafellows & SSH_BUG_PKSERVICE ?
620 if (datafellows & SSH_BUG_PKAUTH) {
621 buffer_put_char(&b, have_sig);
623 buffer_put_cstring(&b, authctxt->method->name);
624 buffer_put_char(&b, have_sig);
625 buffer_put_cstring(&b, key_ssh_name(id->key));
627 buffer_put_string(&b, blob, bloblen);
629 /* generate signature */
630 ret = identity_sign(id, &signature, &slen,
631 buffer_ptr(&b), buffer_len(&b));
640 if (datafellows & SSH_BUG_PKSERVICE) {
642 buffer_append(&b, session_id2, session_id2_len);
643 skip = session_id2_len;
644 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
645 buffer_put_cstring(&b, authctxt->server_user);
646 buffer_put_cstring(&b, authctxt->service);
647 buffer_put_cstring(&b, authctxt->method->name);
648 buffer_put_char(&b, have_sig);
649 if (!(datafellows & SSH_BUG_PKAUTH))
650 buffer_put_cstring(&b, key_ssh_name(id->key));
651 buffer_put_string(&b, blob, bloblen);
655 /* append signature */
656 buffer_put_string(&b, signature, slen);
659 /* skip session id and packet type */
660 if (buffer_len(&b) < skip + 1)
661 fatal("userauth_pubkey: internal error");
662 buffer_consume(&b, skip + 1);
664 /* put remaining data from buffer into packet */
665 packet_start(SSH2_MSG_USERAUTH_REQUEST);
666 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
674 send_pubkey_test(Authctxt *authctxt, Identity *id)
677 u_int bloblen, have_sig = 0;
679 debug3("send_pubkey_test");
681 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
682 /* we cannot handle this key */
683 debug3("send_pubkey_test: cannot handle key");
686 /* register callback for USERAUTH_PK_OK message */
687 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
689 packet_start(SSH2_MSG_USERAUTH_REQUEST);
690 packet_put_cstring(authctxt->server_user);
691 packet_put_cstring(authctxt->service);
692 packet_put_cstring(authctxt->method->name);
693 packet_put_char(have_sig);
694 if (!(datafellows & SSH_BUG_PKAUTH))
695 packet_put_cstring(key_ssh_name(id->key));
696 packet_put_string(blob, bloblen);
703 load_identity_file(char *filename)
706 char prompt[300], *passphrase;
710 if (stat(filename, &st) < 0) {
711 debug3("no such identity: %s", filename);
714 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
715 if (private == NULL) {
716 if (options.batch_mode)
718 snprintf(prompt, sizeof prompt,
719 "Enter passphrase for key '%.100s': ", filename);
720 for (i = 0; i < options.number_of_password_prompts; i++) {
721 passphrase = read_passphrase(prompt, 0);
722 if (strcmp(passphrase, "") != 0) {
723 private = key_load_private_type(KEY_UNSPEC, filename,
727 debug2("no passphrase given, try next key");
730 memset(passphrase, 0, strlen(passphrase));
732 if (private != NULL || quit)
734 debug2("bad passphrase given, try again...");
741 * try keys in the following order:
742 * 1. agent keys that are found in the config file
743 * 2. other agent keys
744 * 3. keys that are only listed in the config file
747 pubkey_prepare(Authctxt *authctxt)
750 Idlist agent, files, *preferred;
752 AuthenticationConnection *ac;
756 TAILQ_INIT(&agent); /* keys from the agent */
757 TAILQ_INIT(&files); /* keys from the config file */
758 preferred = &authctxt->keys;
759 TAILQ_INIT(preferred); /* preferred order of keys */
761 /* list of keys stored in the filesystem */
762 for (i = 0; i < options.num_identity_files; i++) {
763 key = options.identity_keys[i];
764 if (key && key->type == KEY_RSA1)
766 options.identity_keys[i] = NULL;
767 id = xmalloc(sizeof(*id));
768 memset(id, 0, sizeof(*id));
770 id->filename = xstrdup(options.identity_files[i]);
771 TAILQ_INSERT_TAIL(&files, id, next);
773 /* list of keys supported by the agent */
774 if ((ac = ssh_get_authentication_connection())) {
775 for (key = ssh_get_first_identity(ac, &comment, 2);
777 key = ssh_get_next_identity(ac, &comment, 2)) {
779 TAILQ_FOREACH(id, &files, next) {
780 /* agent keys from the config file are preferred */
781 if (key_equal(key, id->key)) {
784 TAILQ_REMOVE(&files, id, next);
785 TAILQ_INSERT_TAIL(preferred, id, next);
792 id = xmalloc(sizeof(*id));
793 memset(id, 0, sizeof(*id));
795 id->filename = comment;
797 TAILQ_INSERT_TAIL(&agent, id, next);
800 /* append remaining agent keys */
801 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
802 TAILQ_REMOVE(&agent, id, next);
803 TAILQ_INSERT_TAIL(preferred, id, next);
805 authctxt->agent = ac;
807 /* append remaining keys from the config file */
808 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
809 TAILQ_REMOVE(&files, id, next);
810 TAILQ_INSERT_TAIL(preferred, id, next);
812 TAILQ_FOREACH(id, preferred, next) {
813 debug2("key: %s (%p)", id->filename, id->key);
818 pubkey_cleanup(Authctxt *authctxt)
822 if (authctxt->agent != NULL)
823 ssh_close_authentication_connection(authctxt->agent);
824 for (id = TAILQ_FIRST(&authctxt->keys); id;
825 id = TAILQ_FIRST(&authctxt->keys)) {
826 TAILQ_REMOVE(&authctxt->keys, id, next);
836 userauth_pubkey(Authctxt *authctxt)
841 while ((id = TAILQ_FIRST(&authctxt->keys))) {
844 TAILQ_REMOVE(&authctxt->keys, id, next);
845 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
847 * send a test message if we have the public key. for
848 * encrypted keys we cannot do this and have to load the
849 * private key instead
851 if (id->key && id->key->type != KEY_RSA1) {
852 debug("Offering public key: %s", id->filename);
853 sent = send_pubkey_test(authctxt, id);
854 } else if (id->key == NULL) {
855 debug("Trying private key: %s", id->filename);
856 id->key = load_identity_file(id->filename);
857 if (id->key != NULL) {
859 sent = sign_and_send_pubkey(authctxt, id);
871 * Send userauth request message specifying keyboard-interactive method.
874 userauth_kbdint(Authctxt *authctxt)
876 static int attempt = 0;
878 if (attempt++ >= options.number_of_password_prompts)
880 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
881 if (attempt > 1 && !authctxt->info_req_seen) {
882 debug3("userauth_kbdint: disable: no info_req_seen");
883 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
887 debug2("userauth_kbdint");
888 packet_start(SSH2_MSG_USERAUTH_REQUEST);
889 packet_put_cstring(authctxt->server_user);
890 packet_put_cstring(authctxt->service);
891 packet_put_cstring(authctxt->method->name);
892 packet_put_cstring(""); /* lang */
893 packet_put_cstring(options.kbd_interactive_devices ?
894 options.kbd_interactive_devices : "");
897 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
902 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
905 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
907 Authctxt *authctxt = ctxt;
908 char *name, *inst, *lang, *prompt, *response;
909 u_int num_prompts, i;
912 debug2("input_userauth_info_req");
914 if (authctxt == NULL)
915 fatal("input_userauth_info_req: no authentication context");
917 authctxt->info_req_seen = 1;
919 name = packet_get_string(NULL);
920 inst = packet_get_string(NULL);
921 lang = packet_get_string(NULL);
922 if (strlen(name) > 0)
924 if (strlen(inst) > 0)
930 num_prompts = packet_get_int();
932 * Begin to build info response packet based on prompts requested.
933 * We commit to providing the correct number of responses, so if
934 * further on we run into a problem that prevents this, we have to
935 * be sure and clean this up and send a correct error response.
937 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
938 packet_put_int(num_prompts);
940 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
941 for (i = 0; i < num_prompts; i++) {
942 prompt = packet_get_string(NULL);
943 echo = packet_get_char();
945 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
947 packet_put_cstring(response);
948 memset(response, 0, strlen(response));
952 packet_check_eom(); /* done with parsing incoming message. */
954 packet_add_padding(64);
959 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
960 u_char *data, u_int datalen)
965 int to[2], from[2], status, version = 2;
967 debug2("ssh_keysign called");
969 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
970 error("ssh_keysign: no installed: %s", strerror(errno));
973 if (fflush(stdout) != 0)
974 error("ssh_keysign: fflush: %s", strerror(errno));
976 error("ssh_keysign: pipe: %s", strerror(errno));
979 if (pipe(from) < 0) {
980 error("ssh_keysign: pipe: %s", strerror(errno));
983 if ((pid = fork()) < 0) {
984 error("ssh_keysign: fork: %s", strerror(errno));
991 if (dup2(from[1], STDOUT_FILENO) < 0)
992 fatal("ssh_keysign: dup2: %s", strerror(errno));
994 if (dup2(to[0], STDIN_FILENO) < 0)
995 fatal("ssh_keysign: dup2: %s", strerror(errno));
998 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
999 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1006 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1007 buffer_put_string(&b, data, datalen);
1008 ssh_msg_send(to[1], version, &b);
1010 if (ssh_msg_recv(from[0], &b) < 0) {
1011 error("ssh_keysign: no reply");
1018 while (waitpid(pid, &status, 0) < 0)
1022 if (buffer_get_char(&b) != version) {
1023 error("ssh_keysign: bad version");
1027 *sigp = buffer_get_string(&b, lenp);
1034 userauth_hostbased(Authctxt *authctxt)
1036 Key *private = NULL;
1037 Sensitive *sensitive = authctxt->sensitive;
1039 u_char *signature, *blob;
1040 char *chost, *pkalg, *p;
1041 const char *service;
1043 int ok, i, len, found = 0;
1045 /* check for a useful key */
1046 for (i = 0; i < sensitive->nkeys; i++) {
1047 private = sensitive->keys[i];
1048 if (private && private->type != KEY_RSA1) {
1050 /* we take and free the key */
1051 sensitive->keys[i] = NULL;
1056 debug("No more client hostkeys for hostbased authentication.");
1059 if (key_to_blob(private, &blob, &blen) == 0) {
1063 /* figure out a name for the client host */
1064 p = get_local_name(packet_get_connection_in());
1066 error("userauth_hostbased: cannot get local ipaddr/name");
1070 len = strlen(p) + 2;
1071 chost = xmalloc(len);
1072 strlcpy(chost, p, len);
1073 strlcat(chost, ".", len);
1074 debug2("userauth_hostbased: chost %s", chost);
1077 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1079 pkalg = xstrdup(key_ssh_name(private));
1081 /* construct data */
1082 buffer_put_string(&b, session_id2, session_id2_len);
1083 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1084 buffer_put_cstring(&b, authctxt->server_user);
1085 buffer_put_cstring(&b, service);
1086 buffer_put_cstring(&b, authctxt->method->name);
1087 buffer_put_cstring(&b, pkalg);
1088 buffer_put_string(&b, blob, blen);
1089 buffer_put_cstring(&b, chost);
1090 buffer_put_cstring(&b, authctxt->local_user);
1094 if (sensitive->external_keysign)
1095 ok = ssh_keysign(private, &signature, &slen,
1096 buffer_ptr(&b), buffer_len(&b));
1098 ok = key_sign(private, &signature, &slen,
1099 buffer_ptr(&b), buffer_len(&b));
1103 error("key_sign failed");
1108 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1109 packet_put_cstring(authctxt->server_user);
1110 packet_put_cstring(authctxt->service);
1111 packet_put_cstring(authctxt->method->name);
1112 packet_put_cstring(pkalg);
1113 packet_put_string(blob, blen);
1114 packet_put_cstring(chost);
1115 packet_put_cstring(authctxt->local_user);
1116 packet_put_string(signature, slen);
1117 memset(signature, 's', slen);
1128 ssh_krb5_helper(krb5_data *ap, krb5_context *context)
1130 krb5_context xcontext = NULL; /* XXX share with ssh1 */
1131 krb5_auth_context xauth_context = NULL;
1132 krb5_auth_context *auth_context;
1133 krb5_error_code problem;
1136 krb5_ccache ccache = NULL;
1137 const char *remotehost;
1140 memset(ap, 0, sizeof(*ap));
1142 context = &xcontext;
1143 auth_context = &xauth_context;
1145 problem = krb5_init_context(context);
1147 debug("Kerberos v5: krb5_init_context failed");
1152 tkfile = krb5_cc_default_name(*context);
1153 if (strncmp(tkfile, "FILE:", 5) == 0)
1156 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
1157 debug("Kerberos v5: could not get default ccache (permission denied).");
1162 problem = krb5_cc_default(*context, &ccache);
1164 debug("Kerberos v5: krb5_cc_default failed: %s",
1165 krb5_get_err_text(*context, problem));
1170 remotehost = get_canonical_hostname(1);
1172 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
1173 "host", remotehost, NULL, ccache, ap);
1175 debug("Kerberos v5: krb5_mk_req failed: %s",
1176 krb5_get_err_text(*context, problem));
1184 krb5_cc_close(*context, ccache);
1186 krb5_auth_con_free(*context, *auth_context);
1191 userauth_kerberos(Authctxt *authctxt)
1194 krb5_context *context;
1197 if (ssh_krb5_helper(&ap, context) == 0)
1200 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1201 packet_put_cstring(authctxt->server_user);
1202 packet_put_cstring(authctxt->service);
1203 packet_put_cstring(authctxt->method->name);
1204 packet_put_string(ap.data, ap.length);
1208 krb5_data_free(&ap);
1210 krb5_free_data_contents(*context, &ap);
1216 krb5_free_context(*context);
1221 /* find auth method */
1224 * given auth method name, if configurable options permit this method fill
1225 * in auth_ident field and return true, otherwise return false.
1228 authmethod_is_enabled(Authmethod *method)
1232 /* return false if options indicate this method is disabled */
1233 if (method->enabled == NULL || *method->enabled == 0)
1235 /* return false if batch mode is enabled but method needs interactive mode */
1236 if (method->batch_flag != NULL && *method->batch_flag != 0)
1242 authmethod_lookup(const char *name)
1244 Authmethod *method = NULL;
1246 for (method = authmethods; method->name != NULL; method++)
1247 if (strcmp(name, method->name) == 0)
1249 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1253 /* XXX internal state */
1254 static Authmethod *current = NULL;
1255 static char *supported = NULL;
1256 static char *preferred = NULL;
1259 * Given the authentication method list sent by the server, return the
1260 * next method we should try. If the server initially sends a nil list,
1261 * use a built-in default list.
1264 authmethod_get(char *authlist)
1269 /* Use a suitable default if we're passed a nil list. */
1270 if (authlist == NULL || strlen(authlist) == 0)
1271 authlist = options.preferred_authentications;
1273 if (supported == NULL || strcmp(authlist, supported) != 0) {
1274 debug3("start over, passed a different list %s", authlist);
1275 if (supported != NULL)
1277 supported = xstrdup(authlist);
1278 preferred = options.preferred_authentications;
1279 debug3("preferred %s", preferred);
1281 } else if (current != NULL && authmethod_is_enabled(current))
1285 if ((name = match_list(preferred, supported, &next)) == NULL) {
1286 debug("No more authentication methods to try.");
1291 debug3("authmethod_lookup %s", name);
1292 debug3("remaining preferred: %s", preferred);
1293 if ((current = authmethod_lookup(name)) != NULL &&
1294 authmethod_is_enabled(current)) {
1295 debug3("authmethod_is_enabled %s", name);
1296 debug("Next authentication method: %s", name);
1303 authmethods_get(void)
1305 Authmethod *method = NULL;
1310 for (method = authmethods; method->name != NULL; method++) {
1311 if (authmethod_is_enabled(method)) {
1312 if (buffer_len(&b) > 0)
1313 buffer_append(&b, ",", 1);
1314 buffer_append(&b, method->name, strlen(method->name));
1317 buffer_append(&b, "\0", 1);
1318 list = xstrdup(buffer_ptr(&b));