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.99 2002/03/26 15:58:46 markus Exp $");
37 #include "myproposal.h"
38 #include "sshconnect.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)
88 xxx_hostaddr = hostaddr;
91 /* Add the GSSAPI mechanisms currently supported on this client to
92 * the key exchange algorithm proposal */
93 orig = myproposal[PROPOSAL_KEX_ALGS];
94 gss = ssh_gssapi_mechanisms(0,host);
96 len = strlen(orig)+strlen(gss)+2;
97 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
98 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
102 if (options.ciphers == (char *)-1) {
103 log("No valid ciphers for protocol version 2 given, using defaults.");
104 options.ciphers = NULL;
106 if (options.ciphers != NULL) {
107 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
108 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
110 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
111 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
112 myproposal[PROPOSAL_ENC_ALGS_STOC] =
113 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
114 if (options.compression) {
115 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
116 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
118 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
119 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
121 if (options.macs != NULL) {
122 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
123 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
125 if (options.hostkeyalgorithms != NULL)
126 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
127 options.hostkeyalgorithms;
130 /* If we've got GSSAPI algorithms, then we also support the
131 * 'null' hostkey, as a last resort */
133 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
134 len = strlen(orig)+sizeof(",null");
135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
136 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
140 /* start key exchange */
141 kex = kex_setup(myproposal);
142 kex->client_version_string=client_version_string;
143 kex->server_version_string=server_version_string;
144 kex->verify_host_key=&verify_host_key_callback;
148 kex->options.gss_deleg_creds=options.gss_deleg_creds;
153 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
155 session_id2 = kex->session_id;
156 session_id2_len = kex->session_id_len;
159 /* send 1st encrypted/maced/compressed message */
160 packet_start(SSH2_MSG_IGNORE);
161 packet_put_cstring("markus");
165 debug("done: ssh_kex2.");
172 typedef struct Authctxt Authctxt;
173 typedef struct Authmethod Authmethod;
175 typedef int sign_cb_fn(
176 Authctxt *authctxt, Key *key,
177 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
180 const char *server_user;
181 const char *local_user;
189 sign_cb_fn *last_key_sign;
191 AuthenticationConnection *agent;
195 /* kbd-interactive */
201 char *name; /* string to compare against server's list */
202 int (*userauth)(Authctxt *authctxt);
203 int *enabled; /* flag in option struct that enables method */
204 int *batch_flag; /* flag in option struct that disables method */
207 void input_userauth_success(int, u_int32_t, void *);
208 void input_userauth_failure(int, u_int32_t, void *);
209 void input_userauth_banner(int, u_int32_t, void *);
210 void input_userauth_error(int, u_int32_t, void *);
211 void input_userauth_info_req(int, u_int32_t, void *);
212 void input_userauth_pk_ok(int, u_int32_t, void *);
213 void input_userauth_passwd_changereq(int, u_int32_t, void *);
215 int userauth_none(Authctxt *);
216 int userauth_pubkey(Authctxt *);
217 int userauth_passwd(Authctxt *);
218 int userauth_kbdint(Authctxt *);
219 int userauth_hostbased(Authctxt *);
222 int userauth_external(Authctxt *authctxt);
223 int userauth_gssapi(Authctxt *authctxt);
224 void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
225 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
226 void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
229 void userauth(Authctxt *, char *);
231 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
232 static void clear_auth_state(Authctxt *);
234 static Authmethod *authmethod_get(char *authlist);
235 static Authmethod *authmethod_lookup(const char *name);
236 static char *authmethods_get(void);
238 Authmethod authmethods[] = {
242 &options.gss_authentication,
246 &options.gss_authentication,
251 &options.hostbased_authentication,
255 &options.pubkey_authentication,
257 {"keyboard-interactive",
259 &options.kbd_interactive_authentication,
260 &options.batch_mode},
263 &options.password_authentication,
264 &options.batch_mode},
269 {NULL, NULL, NULL, NULL}
273 ssh_userauth2(const char *local_user, const char *server_user, char *host,
274 Key **keys, int nkeys)
279 if (options.challenge_response_authentication)
280 options.kbd_interactive_authentication = 1;
282 debug("send SSH2_MSG_SERVICE_REQUEST");
283 packet_start(SSH2_MSG_SERVICE_REQUEST);
284 packet_put_cstring("ssh-userauth");
287 type = packet_read();
288 if (type != SSH2_MSG_SERVICE_ACCEPT) {
289 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
291 if (packet_remaining() > 0) {
292 char *reply = packet_get_string(NULL);
293 debug("service_accept: %s", reply);
296 debug("buggy server: service_accept w/o service");
299 debug("got SSH2_MSG_SERVICE_ACCEPT");
301 if (options.preferred_authentications == NULL)
302 options.preferred_authentications = authmethods_get();
304 /* setup authentication context */
305 memset(&authctxt, 0, sizeof(authctxt));
306 authctxt.agent = ssh_get_authentication_connection();
307 authctxt.server_user = server_user;
308 authctxt.local_user = local_user;
309 authctxt.host = host;
310 authctxt.service = "ssh-connection"; /* service name */
311 authctxt.success = 0;
312 authctxt.method = authmethod_lookup("none");
313 authctxt.authlist = NULL;
314 authctxt.methoddata = NULL;
315 authctxt.keys = keys;
316 authctxt.nkeys = nkeys;
317 authctxt.info_req_seen = 0;
318 if (authctxt.method == NULL)
319 fatal("ssh_userauth2: internal error: cannot send userauth none request");
321 /* initial userauth request */
322 userauth_none(&authctxt);
324 dispatch_init(&input_userauth_error);
325 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
326 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
327 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
328 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
330 if (authctxt.agent != NULL)
331 ssh_close_authentication_connection(authctxt.agent);
333 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
336 userauth(Authctxt *authctxt, char *authlist)
338 if (authctxt->methoddata!=NULL) {
339 xfree(authctxt->methoddata);
340 authctxt->methoddata=NULL;
343 if (authlist == NULL) {
344 authlist = authctxt->authlist;
346 if (authctxt->authlist)
347 xfree(authctxt->authlist);
348 authctxt->authlist = authlist;
351 Authmethod *method = authmethod_get(authlist);
353 fatal("Permission denied (%s).", authlist);
354 authctxt->method = method;
355 if (method->userauth(authctxt) != 0) {
356 debug2("we sent a %s packet, wait for reply", method->name);
359 debug2("we did not send a packet, disable method");
360 method->enabled = NULL;
365 input_userauth_error(int type, u_int32_t seq, void *ctxt)
367 fatal("input_userauth_error: bad message during authentication: "
371 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
374 debug3("input_userauth_banner");
375 msg = packet_get_string(NULL);
376 lang = packet_get_string(NULL);
377 fprintf(stderr, "%s", msg);
382 input_userauth_success(int type, u_int32_t seq, void *ctxt)
384 Authctxt *authctxt = ctxt;
385 if (authctxt == NULL)
386 fatal("input_userauth_success: no authentication context");
387 if (authctxt->authlist)
388 xfree(authctxt->authlist);
389 if (authctxt->methoddata)
390 xfree(authctxt->methoddata);
391 clear_auth_state(authctxt);
392 authctxt->success = 1; /* break out */
395 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
397 Authctxt *authctxt = ctxt;
398 char *authlist = NULL;
401 if (authctxt == NULL)
402 fatal("input_userauth_failure: no authentication context");
404 authlist = packet_get_string(NULL);
405 partial = packet_get_char();
409 log("Authenticated with partial success.");
410 debug("authentications that can continue: %s", authlist);
412 clear_auth_state(authctxt);
413 userauth(authctxt, authlist);
416 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
418 Authctxt *authctxt = ctxt;
421 int pktype, sent = 0;
426 if (authctxt == NULL)
427 fatal("input_userauth_pk_ok: no authentication context");
428 if (datafellows & SSH_BUG_PKOK) {
429 /* this is similar to SSH_BUG_PKAUTH */
430 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
431 pkblob = packet_get_string(&blen);
433 buffer_append(&b, pkblob, blen);
434 pkalg = buffer_get_string(&b, &alen);
437 pkalg = packet_get_string(&alen);
438 pkblob = packet_get_string(&blen);
442 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
443 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
446 if (authctxt->last_key == NULL ||
447 authctxt->last_key_sign == NULL) {
448 debug("no last key or no sign cb");
451 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
452 debug("unknown pkalg %s", pkalg);
455 if ((key = key_from_blob(pkblob, blen)) == NULL) {
456 debug("no key from blob. pkalg %s", pkalg);
459 if (key->type != pktype) {
460 error("input_userauth_pk_ok: type mismatch "
461 "for decoded key (received %d, expected %d)",
465 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
466 debug2("input_userauth_pk_ok: fp %s", fp);
468 if (!key_equal(key, authctxt->last_key)) {
469 debug("key != last_key");
472 sent = sign_and_send_pubkey(authctxt, key,
473 authctxt->last_key_sign);
482 clear_auth_state(authctxt);
483 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
485 /* try another method if we did not send a packet*/
487 userauth(authctxt, NULL);
493 userauth_gssapi(Authctxt *authctxt)
499 /* For now, we only make one attempt at this. We could try offering
500 * the server different GSSAPI OIDs until we get bored, I suppose.
502 if (tries++>0) return 0;
504 if (datafellows & SSH_OLD_GSSAPI) return 0;
506 gssctxt=xmalloc(sizeof(Gssctxt));
508 /* Initialise as much of our context as we can, so failures can be
509 * trapped before sending any packets.
511 ssh_gssapi_build_ctx(gssctxt);
512 if (ssh_gssapi_import_name(gssctxt,authctxt->host)) {
515 authctxt->methoddata=(void *)gssctxt;
517 packet_start(SSH2_MSG_USERAUTH_REQUEST);
518 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
519 packet_put_cstring("");
521 packet_put_cstring(authctxt->server_user);
523 packet_put_cstring(authctxt->service);
524 packet_put_cstring(authctxt->method->name);
526 /* FIXME: This assumes that our current GSSAPI implementation
527 * supports all of the mechanisms listed in supported_mechs.
528 * This may not be the case - we should use something along
529 * the lines of the code in gss_genr to remove the ones that
530 * aren't supported */
531 packet_put_int(GSS_LAST_ENTRY);
532 for (i=0;i<GSS_LAST_ENTRY;i++) {
533 packet_put_string(supported_mechs[i].oid.elements,
534 supported_mechs[i].oid.length);
539 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
540 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
546 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
548 Authctxt *authctxt = ctxt;
553 gss_buffer_desc send_tok;
555 if (authctxt == NULL)
556 fatal("input_gssapi_response: no authentication context");
557 gssctxt = authctxt->methoddata;
560 oidv=packet_get_string(&oidlen);
561 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
565 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
566 GSS_C_NO_BUFFER, &send_tok,
568 if (GSS_ERROR(status)) {
569 /* Start again with next method on list */
570 debug("Trying to start again");
571 userauth(authctxt,NULL);
575 /* We must have data to send */
576 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
577 packet_put_string(send_tok.value,send_tok.length);
580 gss_release_buffer(&ms, &send_tok);
584 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
586 Authctxt *authctxt = ctxt;
588 gss_buffer_desc send_tok,recv_tok;
591 if (authctxt == NULL)
592 fatal("input_gssapi_response: no authentication context");
593 gssctxt = authctxt->methoddata;
595 recv_tok.value=packet_get_string(&recv_tok.length);
597 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
598 &recv_tok, &send_tok, NULL);
602 if (GSS_ERROR(status)) {
603 /* Start again with the next method in the list */
604 userauth(authctxt,NULL);
608 if (send_tok.length>0) {
609 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
610 packet_put_string(send_tok.value,send_tok.length);
615 if (status == GSS_S_COMPLETE) {
616 /* If that succeeded, send a exchange complete message */
617 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
624 userauth_external(Authctxt *authctxt)
626 static int attempt =0;
631 debug2("userauth_external");
632 packet_start(SSH2_MSG_USERAUTH_REQUEST);
633 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
634 packet_put_cstring("");
636 packet_put_cstring(authctxt->server_user);
638 packet_put_cstring(authctxt->service);
639 packet_put_cstring(authctxt->method->name);
647 userauth_none(Authctxt *authctxt)
649 /* initial userauth request */
650 packet_start(SSH2_MSG_USERAUTH_REQUEST);
651 packet_put_cstring(authctxt->server_user);
652 packet_put_cstring(authctxt->service);
653 packet_put_cstring(authctxt->method->name);
660 userauth_passwd(Authctxt *authctxt)
662 static int attempt = 0;
666 if (attempt++ >= options.number_of_password_prompts)
670 error("Permission denied, please try again.");
672 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
673 authctxt->server_user, authctxt->host);
674 password = read_passphrase(prompt, 0);
675 packet_start(SSH2_MSG_USERAUTH_REQUEST);
676 packet_put_cstring(authctxt->server_user);
677 packet_put_cstring(authctxt->service);
678 packet_put_cstring(authctxt->method->name);
680 packet_put_cstring(password);
681 memset(password, 0, strlen(password));
683 packet_add_padding(64);
686 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
687 &input_userauth_passwd_changereq);
692 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
695 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
697 Authctxt *authctxt = ctxt;
698 char *info, *lang, *password = NULL, *retype = NULL;
701 debug2("input_userauth_passwd_changereq");
703 if (authctxt == NULL)
704 fatal("input_userauth_passwd_changereq: "
705 "no authentication context");
707 info = packet_get_string(NULL);
708 lang = packet_get_string(NULL);
709 if (strlen(info) > 0)
713 packet_start(SSH2_MSG_USERAUTH_REQUEST);
714 packet_put_cstring(authctxt->server_user);
715 packet_put_cstring(authctxt->service);
716 packet_put_cstring(authctxt->method->name);
717 packet_put_char(1); /* additional info */
718 snprintf(prompt, sizeof(prompt),
719 "Enter %.30s@%.128s's old password: ",
720 authctxt->server_user, authctxt->host);
721 password = read_passphrase(prompt, 0);
722 packet_put_cstring(password);
723 memset(password, 0, strlen(password));
726 while (password == NULL) {
727 snprintf(prompt, sizeof(prompt),
728 "Enter %.30s@%.128s's new password: ",
729 authctxt->server_user, authctxt->host);
730 password = read_passphrase(prompt, RP_ALLOW_EOF);
731 if (password == NULL) {
735 snprintf(prompt, sizeof(prompt),
736 "Retype %.30s@%.128s's new password: ",
737 authctxt->server_user, authctxt->host);
738 retype = read_passphrase(prompt, 0);
739 if (strcmp(password, retype) != 0) {
740 memset(password, 0, strlen(password));
742 log("Mismatch; try again, EOF to quit.");
745 memset(retype, 0, strlen(retype));
748 packet_put_cstring(password);
749 memset(password, 0, strlen(password));
751 packet_add_padding(64);
754 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
755 &input_userauth_passwd_changereq);
759 clear_auth_state(Authctxt *authctxt)
761 /* XXX clear authentication state */
762 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
764 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
765 debug3("clear_auth_state: key_free %p", authctxt->last_key);
766 key_free(authctxt->last_key);
768 authctxt->last_key = NULL;
769 authctxt->last_key_hint = -2;
770 authctxt->last_key_sign = NULL;
774 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
777 u_char *blob, *signature;
783 debug3("sign_and_send_pubkey");
785 if (key_to_blob(k, &blob, &bloblen) == 0) {
786 /* we cannot handle this key */
787 debug3("sign_and_send_pubkey: cannot handle key");
790 /* data to be signed */
792 if (datafellows & SSH_OLD_SESSIONID) {
793 buffer_append(&b, session_id2, session_id2_len);
794 skip = session_id2_len;
796 buffer_put_string(&b, session_id2, session_id2_len);
797 skip = buffer_len(&b);
799 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
800 buffer_put_cstring(&b, authctxt->server_user);
801 buffer_put_cstring(&b,
802 datafellows & SSH_BUG_PKSERVICE ?
805 if (datafellows & SSH_BUG_PKAUTH) {
806 buffer_put_char(&b, have_sig);
808 buffer_put_cstring(&b, authctxt->method->name);
809 buffer_put_char(&b, have_sig);
810 buffer_put_cstring(&b, key_ssh_name(k));
812 buffer_put_string(&b, blob, bloblen);
814 /* generate signature */
815 ret = (*sign_callback)(authctxt, k, &signature, &slen,
816 buffer_ptr(&b), buffer_len(&b));
825 if (datafellows & SSH_BUG_PKSERVICE) {
827 buffer_append(&b, session_id2, session_id2_len);
828 skip = session_id2_len;
829 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
830 buffer_put_cstring(&b, authctxt->server_user);
831 buffer_put_cstring(&b, authctxt->service);
832 buffer_put_cstring(&b, authctxt->method->name);
833 buffer_put_char(&b, have_sig);
834 if (!(datafellows & SSH_BUG_PKAUTH))
835 buffer_put_cstring(&b, key_ssh_name(k));
836 buffer_put_string(&b, blob, bloblen);
840 /* append signature */
841 buffer_put_string(&b, signature, slen);
844 /* skip session id and packet type */
845 if (buffer_len(&b) < skip + 1)
846 fatal("userauth_pubkey: internal error");
847 buffer_consume(&b, skip + 1);
849 /* put remaining data from buffer into packet */
850 packet_start(SSH2_MSG_USERAUTH_REQUEST);
851 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
859 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
863 u_int bloblen, have_sig = 0;
865 debug3("send_pubkey_test");
867 if (key_to_blob(k, &blob, &bloblen) == 0) {
868 /* we cannot handle this key */
869 debug3("send_pubkey_test: cannot handle key");
872 /* register callback for USERAUTH_PK_OK message */
873 authctxt->last_key_sign = sign_callback;
874 authctxt->last_key_hint = hint;
875 authctxt->last_key = k;
876 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
878 packet_start(SSH2_MSG_USERAUTH_REQUEST);
879 packet_put_cstring(authctxt->server_user);
880 packet_put_cstring(authctxt->service);
881 packet_put_cstring(authctxt->method->name);
882 packet_put_char(have_sig);
883 if (!(datafellows & SSH_BUG_PKAUTH))
884 packet_put_cstring(key_ssh_name(k));
885 packet_put_string(blob, bloblen);
892 load_identity_file(char *filename)
895 char prompt[300], *passphrase;
899 if (stat(filename, &st) < 0) {
900 debug3("no such identity: %s", filename);
903 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
904 if (private == NULL) {
905 if (options.batch_mode)
907 snprintf(prompt, sizeof prompt,
908 "Enter passphrase for key '%.100s': ", filename);
909 for (i = 0; i < options.number_of_password_prompts; i++) {
910 passphrase = read_passphrase(prompt, 0);
911 if (strcmp(passphrase, "") != 0) {
912 private = key_load_private_type(KEY_UNSPEC, filename,
916 debug2("no passphrase given, try next key");
919 memset(passphrase, 0, strlen(passphrase));
921 if (private != NULL || quit)
923 debug2("bad passphrase given, try again...");
930 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
931 u_char *data, u_int datalen)
936 idx = authctxt->last_key_hint;
940 /* private key is stored in external hardware */
941 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
942 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
944 private = load_identity_file(options.identity_files[idx]);
947 ret = key_sign(private, sigp, lenp, data, datalen);
953 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
954 u_char *data, u_int datalen)
956 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
960 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
961 u_char *data, u_int datalen)
963 return key_sign(key, sigp, lenp, data, datalen);
967 userauth_pubkey_agent(Authctxt *authctxt)
969 static int called = 0;
975 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
976 debug2("userauth_pubkey_agent: no keys at all");
979 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
981 debug2("userauth_pubkey_agent: no more keys");
983 debug("userauth_pubkey_agent: testing agent key %s", comment);
985 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
990 debug2("userauth_pubkey_agent: no message sent");
995 userauth_pubkey(Authctxt *authctxt)
1002 if (authctxt->agent != NULL) {
1004 sent = userauth_pubkey_agent(authctxt);
1005 } while (!sent && authctxt->agent->howmany > 0);
1007 while (!sent && idx < options.num_identity_files) {
1008 key = options.identity_keys[idx];
1009 filename = options.identity_files[idx];
1011 debug("try privkey: %s", filename);
1012 key = load_identity_file(filename);
1014 sent = sign_and_send_pubkey(authctxt, key,
1018 } else if (key->type != KEY_RSA1) {
1019 debug("try pubkey: %s", filename);
1020 sent = send_pubkey_test(authctxt, key,
1021 identity_sign_cb, idx);
1029 * Send userauth request message specifying keyboard-interactive method.
1032 userauth_kbdint(Authctxt *authctxt)
1034 static int attempt = 0;
1036 if (attempt++ >= options.number_of_password_prompts)
1038 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1039 if (attempt > 1 && !authctxt->info_req_seen) {
1040 debug3("userauth_kbdint: disable: no info_req_seen");
1041 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1045 debug2("userauth_kbdint");
1046 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1047 packet_put_cstring(authctxt->server_user);
1048 packet_put_cstring(authctxt->service);
1049 packet_put_cstring(authctxt->method->name);
1050 packet_put_cstring(""); /* lang */
1051 packet_put_cstring(options.kbd_interactive_devices ?
1052 options.kbd_interactive_devices : "");
1055 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1060 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1063 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1065 Authctxt *authctxt = ctxt;
1066 char *name, *inst, *lang, *prompt, *response;
1067 u_int num_prompts, i;
1070 debug2("input_userauth_info_req");
1072 if (authctxt == NULL)
1073 fatal("input_userauth_info_req: no authentication context");
1075 authctxt->info_req_seen = 1;
1077 name = packet_get_string(NULL);
1078 inst = packet_get_string(NULL);
1079 lang = packet_get_string(NULL);
1080 if (strlen(name) > 0)
1082 if (strlen(inst) > 0)
1088 num_prompts = packet_get_int();
1090 * Begin to build info response packet based on prompts requested.
1091 * We commit to providing the correct number of responses, so if
1092 * further on we run into a problem that prevents this, we have to
1093 * be sure and clean this up and send a correct error response.
1095 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1096 packet_put_int(num_prompts);
1098 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1099 for (i = 0; i < num_prompts; i++) {
1100 prompt = packet_get_string(NULL);
1101 echo = packet_get_char();
1103 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1105 packet_put_cstring(response);
1106 memset(response, 0, strlen(response));
1110 packet_check_eom(); /* done with parsing incoming message. */
1112 packet_add_padding(64);
1117 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
1118 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
1121 userauth_hostbased(Authctxt *authctxt)
1123 Key *private = NULL;
1125 u_char *signature, *blob;
1126 char *chost, *pkalg, *p;
1127 const char *service;
1129 int ok, i, len, found = 0;
1131 /* check for a useful key */
1132 for (i = 0; i < authctxt->nkeys; i++) {
1133 private = authctxt->keys[i];
1134 if (private && private->type != KEY_RSA1) {
1136 /* we take and free the key */
1137 authctxt->keys[i] = NULL;
1142 debug("userauth_hostbased: no more client hostkeys");
1145 if (key_to_blob(private, &blob, &blen) == 0) {
1149 /* figure out a name for the client host */
1150 p = get_local_name(packet_get_connection_in());
1152 error("userauth_hostbased: cannot get local ipaddr/name");
1156 len = strlen(p) + 2;
1157 chost = xmalloc(len);
1158 strlcpy(chost, p, len);
1159 strlcat(chost, ".", len);
1160 debug2("userauth_hostbased: chost %s", chost);
1162 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1164 pkalg = xstrdup(key_ssh_name(private));
1166 /* construct data */
1167 buffer_put_string(&b, session_id2, session_id2_len);
1168 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1169 buffer_put_cstring(&b, authctxt->server_user);
1170 buffer_put_cstring(&b, service);
1171 buffer_put_cstring(&b, authctxt->method->name);
1172 buffer_put_cstring(&b, pkalg);
1173 buffer_put_string(&b, blob, blen);
1174 buffer_put_cstring(&b, chost);
1175 buffer_put_cstring(&b, authctxt->local_user);
1179 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
1183 error("key_sign failed");
1188 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1189 packet_put_cstring(authctxt->server_user);
1190 packet_put_cstring(authctxt->service);
1191 packet_put_cstring(authctxt->method->name);
1192 packet_put_cstring(pkalg);
1193 packet_put_string(blob, blen);
1194 packet_put_cstring(chost);
1195 packet_put_cstring(authctxt->local_user);
1196 packet_put_string(signature, slen);
1197 memset(signature, 's', slen);
1206 /* find auth method */
1209 * given auth method name, if configurable options permit this method fill
1210 * in auth_ident field and return true, otherwise return false.
1213 authmethod_is_enabled(Authmethod *method)
1217 /* return false if options indicate this method is disabled */
1218 if (method->enabled == NULL || *method->enabled == 0)
1220 /* return false if batch mode is enabled but method needs interactive mode */
1221 if (method->batch_flag != NULL && *method->batch_flag != 0)
1227 authmethod_lookup(const char *name)
1229 Authmethod *method = NULL;
1231 for (method = authmethods; method->name != NULL; method++)
1232 if (strcmp(name, method->name) == 0)
1234 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1238 /* XXX internal state */
1239 static Authmethod *current = NULL;
1240 static char *supported = NULL;
1241 static char *preferred = NULL;
1243 * Given the authentication method list sent by the server, return the
1244 * next method we should try. If the server initially sends a nil list,
1245 * use a built-in default list.
1248 authmethod_get(char *authlist)
1254 /* Use a suitable default if we're passed a nil list. */
1255 if (authlist == NULL || strlen(authlist) == 0)
1256 authlist = options.preferred_authentications;
1258 if (supported == NULL || strcmp(authlist, supported) != 0) {
1259 debug3("start over, passed a different list %s", authlist);
1260 if (supported != NULL)
1262 supported = xstrdup(authlist);
1263 preferred = options.preferred_authentications;
1264 debug3("preferred %s", preferred);
1266 } else if (current != NULL && authmethod_is_enabled(current))
1270 if ((name = match_list(preferred, supported, &next)) == NULL) {
1271 debug("no more auth methods to try");
1276 debug3("authmethod_lookup %s", name);
1277 debug3("remaining preferred: %s", preferred);
1278 if ((current = authmethod_lookup(name)) != NULL &&
1279 authmethod_is_enabled(current)) {
1280 debug3("authmethod_is_enabled %s", name);
1281 debug("next auth method to try is %s", name);
1288 authmethods_get(void)
1290 Authmethod *method = NULL;
1295 for (method = authmethods; method->name != NULL; method++) {
1296 if (authmethod_is_enabled(method)) {
1297 if (buffer_len(&b) > 0)
1298 buffer_append(&b, ",", 1);
1299 buffer_append(&b, method->name, strlen(method->name));
1302 buffer_append(&b, "\0", 1);
1303 list = xstrdup(buffer_ptr(&b));