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 $");
37 #include "myproposal.h"
38 #include "sshconnect.h"
49 #include "pathnames.h"
56 extern char *client_version_string;
57 extern char *server_version_string;
58 extern Options options;
64 u_char *session_id2 = NULL;
65 int session_id2_len = 0;
68 struct sockaddr *xxx_hostaddr;
73 verify_host_key_callback(Key *hostkey)
75 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
76 fatal("Host key verification failed.");
81 ssh_kex2(char *host, struct sockaddr *hostaddr)
90 xxx_hostaddr = hostaddr;
93 if (options.gss_keyex) {
95 /* Add the GSSAPI mechanisms currently supported on this client to
96 * the key exchange algorithm proposal */
97 orig = myproposal[PROPOSAL_KEX_ALGS];
98 canonhost = xstrdup(get_canonical_hostname(1));
99 resolve_localhost(&canonhost);
100 gss = ssh_gssapi_mechanisms(0,canonhost);
104 len = strlen(orig)+strlen(gss)+2;
105 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
106 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
111 if (options.ciphers == (char *)-1) {
112 log("No valid ciphers for protocol version 2 given, using defaults.");
113 options.ciphers = NULL;
115 if (options.ciphers != NULL) {
116 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
119 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
120 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
121 myproposal[PROPOSAL_ENC_ALGS_STOC] =
122 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
123 if (options.compression) {
124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
127 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
128 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
130 if (options.macs != NULL) {
131 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
132 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
134 if (options.hostkeyalgorithms != NULL)
135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
136 options.hostkeyalgorithms;
139 /* If we've got GSSAPI algorithms, then we also support the
140 * 'null' hostkey, as a last resort */
141 if (options.gss_keyex && gss) {
142 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
143 len = strlen(orig)+sizeof(",null");
144 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
145 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
149 /* start key exchange */
150 kex = kex_setup(myproposal);
151 kex->client_version_string=client_version_string;
152 kex->server_version_string=server_version_string;
153 kex->verify_host_key=&verify_host_key_callback;
157 kex->options.gss_deleg_creds=options.gss_deleg_creds;
162 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
164 session_id2 = kex->session_id;
165 session_id2_len = kex->session_id_len;
168 /* send 1st encrypted/maced/compressed message */
169 packet_start(SSH2_MSG_IGNORE);
170 packet_put_cstring("markus");
174 debug("done: ssh_kex2.");
181 typedef struct Authctxt Authctxt;
182 typedef struct Authmethod Authmethod;
184 typedef int sign_cb_fn(
185 Authctxt *authctxt, Key *key,
186 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
189 const char *server_user;
190 const char *local_user;
198 sign_cb_fn *last_key_sign;
200 AuthenticationConnection *agent;
202 Sensitive *sensitive;
203 /* kbd-interactive */
209 char *name; /* string to compare against server's list */
210 int (*userauth)(Authctxt *authctxt);
211 int *enabled; /* flag in option struct that enables method */
212 int *batch_flag; /* flag in option struct that disables method */
215 void input_userauth_success(int, u_int32_t, void *);
216 void input_userauth_failure(int, u_int32_t, void *);
217 void input_userauth_banner(int, u_int32_t, void *);
218 void input_userauth_error(int, u_int32_t, void *);
219 void input_userauth_info_req(int, u_int32_t, void *);
220 void input_userauth_pk_ok(int, u_int32_t, void *);
221 void input_userauth_passwd_changereq(int, u_int32_t, void *);
223 int userauth_none(Authctxt *);
224 int userauth_pubkey(Authctxt *);
225 int userauth_passwd(Authctxt *);
226 int userauth_kbdint(Authctxt *);
227 int userauth_hostbased(Authctxt *);
230 int userauth_external(Authctxt *authctxt);
231 int userauth_gssapi(Authctxt *authctxt);
232 void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
233 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
234 void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
237 void userauth(Authctxt *, char *);
239 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
240 static void clear_auth_state(Authctxt *);
242 static Authmethod *authmethod_get(char *authlist);
243 static Authmethod *authmethod_lookup(const char *name);
244 static char *authmethods_get(void);
246 Authmethod authmethods[] = {
250 &options.gss_authentication,
254 &options.gss_authentication,
259 &options.hostbased_authentication,
263 &options.pubkey_authentication,
265 {"keyboard-interactive",
267 &options.kbd_interactive_authentication,
268 &options.batch_mode},
271 &options.password_authentication,
272 &options.batch_mode},
277 {NULL, NULL, NULL, NULL}
281 ssh_userauth2(const char *local_user, const char *server_user, char *host,
282 Sensitive *sensitive)
287 if (options.challenge_response_authentication)
288 options.kbd_interactive_authentication = 1;
290 debug("send SSH2_MSG_SERVICE_REQUEST");
291 packet_start(SSH2_MSG_SERVICE_REQUEST);
292 packet_put_cstring("ssh-userauth");
295 type = packet_read();
296 if (type != SSH2_MSG_SERVICE_ACCEPT) {
297 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
299 if (packet_remaining() > 0) {
300 char *reply = packet_get_string(NULL);
301 debug("service_accept: %s", reply);
304 debug("buggy server: service_accept w/o service");
307 debug("got SSH2_MSG_SERVICE_ACCEPT");
309 if (options.preferred_authentications == NULL)
310 options.preferred_authentications = authmethods_get();
312 /* setup authentication context */
313 memset(&authctxt, 0, sizeof(authctxt));
314 authctxt.agent = ssh_get_authentication_connection();
315 authctxt.server_user = server_user;
316 authctxt.local_user = local_user;
317 authctxt.host = host;
318 authctxt.service = "ssh-connection"; /* service name */
319 authctxt.success = 0;
320 authctxt.method = authmethod_lookup("none");
321 authctxt.authlist = NULL;
322 authctxt.methoddata = NULL;
323 authctxt.sensitive = sensitive;
324 authctxt.info_req_seen = 0;
325 if (authctxt.method == NULL)
326 fatal("ssh_userauth2: internal error: cannot send userauth none request");
328 /* initial userauth request */
329 userauth_none(&authctxt);
331 dispatch_init(&input_userauth_error);
332 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
333 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
334 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
335 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
337 if (authctxt.agent != NULL)
338 ssh_close_authentication_connection(authctxt.agent);
340 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
343 userauth(Authctxt *authctxt, char *authlist)
345 if (authctxt->methoddata!=NULL) {
346 xfree(authctxt->methoddata);
347 authctxt->methoddata=NULL;
350 if (authlist == NULL) {
351 authlist = authctxt->authlist;
353 if (authctxt->authlist)
354 xfree(authctxt->authlist);
355 authctxt->authlist = authlist;
358 Authmethod *method = authmethod_get(authlist);
360 fatal("Permission denied (%s).", authlist);
361 authctxt->method = method;
362 if (method->userauth(authctxt) != 0) {
363 debug2("we sent a %s packet, wait for reply", method->name);
366 debug2("we did not send a packet, disable method");
367 method->enabled = NULL;
373 input_userauth_error(int type, u_int32_t seq, void *ctxt)
375 fatal("input_userauth_error: bad message during authentication: "
380 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
383 debug3("input_userauth_banner");
384 msg = packet_get_string(NULL);
385 lang = packet_get_string(NULL);
386 fprintf(stderr, "%s", msg);
392 input_userauth_success(int type, u_int32_t seq, void *ctxt)
394 Authctxt *authctxt = ctxt;
395 if (authctxt == NULL)
396 fatal("input_userauth_success: no authentication context");
397 if (authctxt->authlist)
398 xfree(authctxt->authlist);
399 if (authctxt->methoddata)
400 xfree(authctxt->methoddata);
401 clear_auth_state(authctxt);
402 authctxt->success = 1; /* break out */
406 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
408 Authctxt *authctxt = ctxt;
409 char *authlist = NULL;
412 if (authctxt == NULL)
413 fatal("input_userauth_failure: no authentication context");
415 authlist = packet_get_string(NULL);
416 partial = packet_get_char();
420 log("Authenticated with partial success.");
421 debug("authentications that can continue: %s", authlist);
423 clear_auth_state(authctxt);
424 userauth(authctxt, authlist);
427 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
429 Authctxt *authctxt = ctxt;
432 int pktype, sent = 0;
437 if (authctxt == NULL)
438 fatal("input_userauth_pk_ok: no authentication context");
439 if (datafellows & SSH_BUG_PKOK) {
440 /* this is similar to SSH_BUG_PKAUTH */
441 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
442 pkblob = packet_get_string(&blen);
444 buffer_append(&b, pkblob, blen);
445 pkalg = buffer_get_string(&b, &alen);
448 pkalg = packet_get_string(&alen);
449 pkblob = packet_get_string(&blen);
453 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
454 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
457 if (authctxt->last_key == NULL ||
458 authctxt->last_key_sign == NULL) {
459 debug("no last key or no sign cb");
462 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
463 debug("unknown pkalg %s", pkalg);
466 if ((key = key_from_blob(pkblob, blen)) == NULL) {
467 debug("no key from blob. pkalg %s", pkalg);
470 if (key->type != pktype) {
471 error("input_userauth_pk_ok: type mismatch "
472 "for decoded key (received %d, expected %d)",
476 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
477 debug2("input_userauth_pk_ok: fp %s", fp);
479 if (!key_equal(key, authctxt->last_key)) {
480 debug("key != last_key");
483 sent = sign_and_send_pubkey(authctxt, key,
484 authctxt->last_key_sign);
493 clear_auth_state(authctxt);
494 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
496 /* try another method if we did not send a packet */
498 userauth(authctxt, NULL);
504 userauth_gssapi(Authctxt *authctxt)
509 if (datafellows & SSH_OLD_GSSAPI) return 0;
511 /* Try each mechanism in turn. Give up if we've tried all
512 supported mechanisms.
514 if (mech==GSS_LAST_ENTRY) return 0;
516 /* Initialise as much of our context as we can, so failures can be
517 * trapped before sending any packets.
519 ssh_gssapi_build_ctx(&gssctxt);
521 if (ssh_gssapi_import_name(gssctxt,get_canonical_hostname(1))) {
525 authctxt->methoddata=(void *)gssctxt;
527 packet_start(SSH2_MSG_USERAUTH_REQUEST);
529 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
530 packet_put_cstring("");
533 packet_put_cstring(authctxt->server_user);
537 packet_put_cstring(authctxt->service);
538 packet_put_cstring(authctxt->method->name);
540 /* FIXME: This assumes that our current GSSAPI implementation
541 * supports all of the mechanisms listed in supported_mechs.
542 * This may not be the case - we should use something along
543 * the lines of the code in gss_genr to remove the ones that
544 * aren't supported */
546 /* Try one GSSAPI mechanism at a time. */
548 packet_put_string(supported_mechs[mech].oid.elements,
549 supported_mechs[mech].oid.length);
553 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
554 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
556 mech++; /* Move to next mechanism for next time. */
562 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
564 Authctxt *authctxt = ctxt;
569 gss_buffer_desc send_tok;
571 if (authctxt == NULL)
572 fatal("input_gssapi_response: no authentication context");
573 gssctxt = authctxt->methoddata;
576 oidv=packet_get_string(&oidlen);
577 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
581 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
582 GSS_C_NO_BUFFER, &send_tok,
584 if (GSS_ERROR(status)) {
585 /* Start again with next method on list */
586 debug("Trying to start again");
587 userauth(authctxt,NULL);
591 /* We must have data to send */
592 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
593 packet_put_string(send_tok.value,send_tok.length);
596 gss_release_buffer(&ms, &send_tok);
600 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
602 Authctxt *authctxt = ctxt;
604 gss_buffer_desc send_tok,recv_tok;
608 if (authctxt == NULL)
609 fatal("input_gssapi_token: no authentication context");
610 gssctxt = authctxt->methoddata;
612 recv_tok.value=packet_get_string(&slen);
613 recv_tok.length=slen; /* safe typecast */
615 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
616 &recv_tok, &send_tok, NULL);
620 if (GSS_ERROR(status)) {
621 /* Start again with the next method in the list */
622 userauth(authctxt,NULL);
626 if (send_tok.length>0) {
627 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
628 packet_put_string(send_tok.value,send_tok.length);
633 if (status == GSS_S_COMPLETE) {
634 /* If that succeeded, send a exchange complete message */
635 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
642 userauth_external(Authctxt *authctxt)
644 static int attempt =0;
649 debug2("userauth_external");
650 packet_start(SSH2_MSG_USERAUTH_REQUEST);
652 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
653 packet_put_cstring("");
656 packet_put_cstring(authctxt->server_user);
660 packet_put_cstring(authctxt->service);
661 packet_put_cstring(authctxt->method->name);
669 userauth_none(Authctxt *authctxt)
671 /* initial userauth request */
672 packet_start(SSH2_MSG_USERAUTH_REQUEST);
673 packet_put_cstring(authctxt->server_user);
674 packet_put_cstring(authctxt->service);
675 packet_put_cstring(authctxt->method->name);
682 userauth_passwd(Authctxt *authctxt)
684 static int attempt = 0;
688 if (attempt++ >= options.number_of_password_prompts)
692 error("Permission denied, please try again.");
694 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
695 authctxt->server_user, authctxt->host);
696 password = read_passphrase(prompt, 0);
697 packet_start(SSH2_MSG_USERAUTH_REQUEST);
698 packet_put_cstring(authctxt->server_user);
699 packet_put_cstring(authctxt->service);
700 packet_put_cstring(authctxt->method->name);
702 packet_put_cstring(password);
703 memset(password, 0, strlen(password));
705 packet_add_padding(64);
708 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
709 &input_userauth_passwd_changereq);
714 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
717 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
719 Authctxt *authctxt = ctxt;
720 char *info, *lang, *password = NULL, *retype = NULL;
723 debug2("input_userauth_passwd_changereq");
725 if (authctxt == NULL)
726 fatal("input_userauth_passwd_changereq: "
727 "no authentication context");
729 info = packet_get_string(NULL);
730 lang = packet_get_string(NULL);
731 if (strlen(info) > 0)
735 packet_start(SSH2_MSG_USERAUTH_REQUEST);
736 packet_put_cstring(authctxt->server_user);
737 packet_put_cstring(authctxt->service);
738 packet_put_cstring(authctxt->method->name);
739 packet_put_char(1); /* additional info */
740 snprintf(prompt, sizeof(prompt),
741 "Enter %.30s@%.128s's old password: ",
742 authctxt->server_user, authctxt->host);
743 password = read_passphrase(prompt, 0);
744 packet_put_cstring(password);
745 memset(password, 0, strlen(password));
748 while (password == NULL) {
749 snprintf(prompt, sizeof(prompt),
750 "Enter %.30s@%.128s's new password: ",
751 authctxt->server_user, authctxt->host);
752 password = read_passphrase(prompt, RP_ALLOW_EOF);
753 if (password == NULL) {
757 snprintf(prompt, sizeof(prompt),
758 "Retype %.30s@%.128s's new password: ",
759 authctxt->server_user, authctxt->host);
760 retype = read_passphrase(prompt, 0);
761 if (strcmp(password, retype) != 0) {
762 memset(password, 0, strlen(password));
764 log("Mismatch; try again, EOF to quit.");
767 memset(retype, 0, strlen(retype));
770 packet_put_cstring(password);
771 memset(password, 0, strlen(password));
773 packet_add_padding(64);
776 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
777 &input_userauth_passwd_changereq);
781 clear_auth_state(Authctxt *authctxt)
783 /* XXX clear authentication state */
784 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
786 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
787 debug3("clear_auth_state: key_free %p", authctxt->last_key);
788 key_free(authctxt->last_key);
790 authctxt->last_key = NULL;
791 authctxt->last_key_hint = -2;
792 authctxt->last_key_sign = NULL;
796 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
799 u_char *blob, *signature;
805 debug3("sign_and_send_pubkey");
807 if (key_to_blob(k, &blob, &bloblen) == 0) {
808 /* we cannot handle this key */
809 debug3("sign_and_send_pubkey: cannot handle key");
812 /* data to be signed */
814 if (datafellows & SSH_OLD_SESSIONID) {
815 buffer_append(&b, session_id2, session_id2_len);
816 skip = session_id2_len;
818 buffer_put_string(&b, session_id2, session_id2_len);
819 skip = buffer_len(&b);
821 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
822 buffer_put_cstring(&b, authctxt->server_user);
823 buffer_put_cstring(&b,
824 datafellows & SSH_BUG_PKSERVICE ?
827 if (datafellows & SSH_BUG_PKAUTH) {
828 buffer_put_char(&b, have_sig);
830 buffer_put_cstring(&b, authctxt->method->name);
831 buffer_put_char(&b, have_sig);
832 buffer_put_cstring(&b, key_ssh_name(k));
834 buffer_put_string(&b, blob, bloblen);
836 /* generate signature */
837 ret = (*sign_callback)(authctxt, k, &signature, &slen,
838 buffer_ptr(&b), buffer_len(&b));
847 if (datafellows & SSH_BUG_PKSERVICE) {
849 buffer_append(&b, session_id2, session_id2_len);
850 skip = session_id2_len;
851 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
852 buffer_put_cstring(&b, authctxt->server_user);
853 buffer_put_cstring(&b, authctxt->service);
854 buffer_put_cstring(&b, authctxt->method->name);
855 buffer_put_char(&b, have_sig);
856 if (!(datafellows & SSH_BUG_PKAUTH))
857 buffer_put_cstring(&b, key_ssh_name(k));
858 buffer_put_string(&b, blob, bloblen);
862 /* append signature */
863 buffer_put_string(&b, signature, slen);
866 /* skip session id and packet type */
867 if (buffer_len(&b) < skip + 1)
868 fatal("userauth_pubkey: internal error");
869 buffer_consume(&b, skip + 1);
871 /* put remaining data from buffer into packet */
872 packet_start(SSH2_MSG_USERAUTH_REQUEST);
873 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
881 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
885 u_int bloblen, have_sig = 0;
887 debug3("send_pubkey_test");
889 if (key_to_blob(k, &blob, &bloblen) == 0) {
890 /* we cannot handle this key */
891 debug3("send_pubkey_test: cannot handle key");
894 /* register callback for USERAUTH_PK_OK message */
895 authctxt->last_key_sign = sign_callback;
896 authctxt->last_key_hint = hint;
897 authctxt->last_key = k;
898 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
900 packet_start(SSH2_MSG_USERAUTH_REQUEST);
901 packet_put_cstring(authctxt->server_user);
902 packet_put_cstring(authctxt->service);
903 packet_put_cstring(authctxt->method->name);
904 packet_put_char(have_sig);
905 if (!(datafellows & SSH_BUG_PKAUTH))
906 packet_put_cstring(key_ssh_name(k));
907 packet_put_string(blob, bloblen);
914 load_identity_file(char *filename)
917 char prompt[300], *passphrase;
921 if (stat(filename, &st) < 0) {
922 debug3("no such identity: %s", filename);
925 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
926 if (private == NULL) {
927 if (options.batch_mode)
929 snprintf(prompt, sizeof prompt,
930 "Enter passphrase for key '%.100s': ", filename);
931 for (i = 0; i < options.number_of_password_prompts; i++) {
932 passphrase = read_passphrase(prompt, 0);
933 if (strcmp(passphrase, "") != 0) {
934 private = key_load_private_type(KEY_UNSPEC, filename,
938 debug2("no passphrase given, try next key");
941 memset(passphrase, 0, strlen(passphrase));
943 if (private != NULL || quit)
945 debug2("bad passphrase given, try again...");
952 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
953 u_char *data, u_int datalen)
958 idx = authctxt->last_key_hint;
962 /* private key is stored in external hardware */
963 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
964 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
966 private = load_identity_file(options.identity_files[idx]);
969 ret = key_sign(private, sigp, lenp, data, datalen);
975 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
976 u_char *data, u_int datalen)
978 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
982 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
983 u_char *data, u_int datalen)
985 return key_sign(key, sigp, lenp, data, datalen);
989 userauth_pubkey_agent(Authctxt *authctxt)
991 static int called = 0;
997 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
998 debug2("userauth_pubkey_agent: no keys at all");
1001 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1003 debug2("userauth_pubkey_agent: no more keys");
1005 debug("userauth_pubkey_agent: testing agent key %s", comment);
1007 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1012 debug2("userauth_pubkey_agent: no message sent");
1017 userauth_pubkey(Authctxt *authctxt)
1024 if (authctxt->agent != NULL) {
1026 sent = userauth_pubkey_agent(authctxt);
1027 } while (!sent && authctxt->agent->howmany > 0);
1029 while (!sent && idx < options.num_identity_files) {
1030 key = options.identity_keys[idx];
1031 filename = options.identity_files[idx];
1033 debug("try privkey: %s", filename);
1034 key = load_identity_file(filename);
1036 sent = sign_and_send_pubkey(authctxt, key,
1040 } else if (key->type != KEY_RSA1) {
1041 debug("try pubkey: %s", filename);
1042 sent = send_pubkey_test(authctxt, key,
1043 identity_sign_cb, idx);
1051 * Send userauth request message specifying keyboard-interactive method.
1054 userauth_kbdint(Authctxt *authctxt)
1056 static int attempt = 0;
1058 if (attempt++ >= options.number_of_password_prompts)
1060 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1061 if (attempt > 1 && !authctxt->info_req_seen) {
1062 debug3("userauth_kbdint: disable: no info_req_seen");
1063 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1067 debug2("userauth_kbdint");
1068 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1069 packet_put_cstring(authctxt->server_user);
1070 packet_put_cstring(authctxt->service);
1071 packet_put_cstring(authctxt->method->name);
1072 packet_put_cstring(""); /* lang */
1073 packet_put_cstring(options.kbd_interactive_devices ?
1074 options.kbd_interactive_devices : "");
1077 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1082 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1085 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1087 Authctxt *authctxt = ctxt;
1088 char *name, *inst, *lang, *prompt, *response;
1089 u_int num_prompts, i;
1092 debug2("input_userauth_info_req");
1094 if (authctxt == NULL)
1095 fatal("input_userauth_info_req: no authentication context");
1097 authctxt->info_req_seen = 1;
1099 name = packet_get_string(NULL);
1100 inst = packet_get_string(NULL);
1101 lang = packet_get_string(NULL);
1102 if (strlen(name) > 0)
1104 if (strlen(inst) > 0)
1110 num_prompts = packet_get_int();
1112 * Begin to build info response packet based on prompts requested.
1113 * We commit to providing the correct number of responses, so if
1114 * further on we run into a problem that prevents this, we have to
1115 * be sure and clean this up and send a correct error response.
1117 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1118 packet_put_int(num_prompts);
1120 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1121 for (i = 0; i < num_prompts; i++) {
1122 prompt = packet_get_string(NULL);
1123 echo = packet_get_char();
1125 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1127 packet_put_cstring(response);
1128 memset(response, 0, strlen(response));
1132 packet_check_eom(); /* done with parsing incoming message. */
1134 packet_add_padding(64);
1139 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1140 u_char *data, u_int datalen)
1145 int to[2], from[2], status, version = 2;
1147 debug("ssh_keysign called");
1149 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1150 error("ssh_keysign: no installed: %s", strerror(errno));
1153 if (fflush(stdout) != 0)
1154 error("ssh_keysign: fflush: %s", strerror(errno));
1156 error("ssh_keysign: pipe: %s", strerror(errno));
1159 if (pipe(from) < 0) {
1160 error("ssh_keysign: pipe: %s", strerror(errno));
1163 if ((pid = fork()) < 0) {
1164 error("ssh_keysign: fork: %s", strerror(errno));
1171 if (dup2(from[1], STDOUT_FILENO) < 0)
1172 fatal("ssh_keysign: dup2: %s", strerror(errno));
1174 if (dup2(to[0], STDIN_FILENO) < 0)
1175 fatal("ssh_keysign: dup2: %s", strerror(errno));
1178 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1179 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1186 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1187 buffer_put_string(&b, data, datalen);
1188 ssh_msg_send(to[1], version, &b);
1190 if (ssh_msg_recv(from[0], &b) < 0) {
1191 error("ssh_keysign: no reply");
1198 while (waitpid(pid, &status, 0) < 0)
1202 if (buffer_get_char(&b) != version) {
1203 error("ssh_keysign: bad version");
1207 *sigp = buffer_get_string(&b, lenp);
1214 userauth_hostbased(Authctxt *authctxt)
1216 Key *private = NULL;
1217 Sensitive *sensitive = authctxt->sensitive;
1219 u_char *signature, *blob;
1220 char *chost, *pkalg, *p;
1221 const char *service;
1223 int ok, i, len, found = 0;
1225 /* check for a useful key */
1226 for (i = 0; i < sensitive->nkeys; i++) {
1227 private = sensitive->keys[i];
1228 if (private && private->type != KEY_RSA1) {
1230 /* we take and free the key */
1231 sensitive->keys[i] = NULL;
1236 debug("userauth_hostbased: no more client hostkeys");
1239 if (key_to_blob(private, &blob, &blen) == 0) {
1243 /* figure out a name for the client host */
1244 p = get_local_name(packet_get_connection_in());
1246 error("userauth_hostbased: cannot get local ipaddr/name");
1250 len = strlen(p) + 2;
1251 chost = xmalloc(len);
1252 strlcpy(chost, p, len);
1253 strlcat(chost, ".", len);
1254 debug2("userauth_hostbased: chost %s", chost);
1256 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1258 pkalg = xstrdup(key_ssh_name(private));
1260 /* construct data */
1261 buffer_put_string(&b, session_id2, session_id2_len);
1262 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1263 buffer_put_cstring(&b, authctxt->server_user);
1264 buffer_put_cstring(&b, service);
1265 buffer_put_cstring(&b, authctxt->method->name);
1266 buffer_put_cstring(&b, pkalg);
1267 buffer_put_string(&b, blob, blen);
1268 buffer_put_cstring(&b, chost);
1269 buffer_put_cstring(&b, authctxt->local_user);
1273 if (sensitive->external_keysign)
1274 ok = ssh_keysign(private, &signature, &slen,
1275 buffer_ptr(&b), buffer_len(&b));
1277 ok = key_sign(private, &signature, &slen,
1278 buffer_ptr(&b), buffer_len(&b));
1282 error("key_sign failed");
1287 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1288 packet_put_cstring(authctxt->server_user);
1289 packet_put_cstring(authctxt->service);
1290 packet_put_cstring(authctxt->method->name);
1291 packet_put_cstring(pkalg);
1292 packet_put_string(blob, blen);
1293 packet_put_cstring(chost);
1294 packet_put_cstring(authctxt->local_user);
1295 packet_put_string(signature, slen);
1296 memset(signature, 's', slen);
1305 /* find auth method */
1308 * given auth method name, if configurable options permit this method fill
1309 * in auth_ident field and return true, otherwise return false.
1312 authmethod_is_enabled(Authmethod *method)
1316 /* return false if options indicate this method is disabled */
1317 if (method->enabled == NULL || *method->enabled == 0)
1319 /* return false if batch mode is enabled but method needs interactive mode */
1320 if (method->batch_flag != NULL && *method->batch_flag != 0)
1326 authmethod_lookup(const char *name)
1328 Authmethod *method = NULL;
1330 for (method = authmethods; method->name != NULL; method++)
1331 if (strcmp(name, method->name) == 0)
1333 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1337 /* XXX internal state */
1338 static Authmethod *current = NULL;
1339 static char *supported = NULL;
1340 static char *preferred = NULL;
1343 * Given the authentication method list sent by the server, return the
1344 * next method we should try. If the server initially sends a nil list,
1345 * use a built-in default list.
1348 authmethod_get(char *authlist)
1354 /* Use a suitable default if we're passed a nil list. */
1355 if (authlist == NULL || strlen(authlist) == 0)
1356 authlist = options.preferred_authentications;
1358 if (supported == NULL || strcmp(authlist, supported) != 0) {
1359 debug3("start over, passed a different list %s", authlist);
1360 if (supported != NULL)
1362 supported = xstrdup(authlist);
1363 preferred = options.preferred_authentications;
1364 debug3("preferred %s", preferred);
1366 } else if (current != NULL && authmethod_is_enabled(current))
1370 if ((name = match_list(preferred, supported, &next)) == NULL) {
1371 debug("no more auth methods to try");
1376 debug3("authmethod_lookup %s", name);
1377 debug3("remaining preferred: %s", preferred);
1378 if ((current = authmethod_lookup(name)) != NULL &&
1379 authmethod_is_enabled(current)) {
1380 debug3("authmethod_is_enabled %s", name);
1381 debug("next auth method to try is %s", name);
1388 authmethods_get(void)
1390 Authmethod *method = NULL;
1395 for (method = authmethods; method->name != NULL; method++) {
1396 if (authmethod_is_enabled(method)) {
1397 if (buffer_len(&b) > 0)
1398 buffer_append(&b, ",", 1);
1399 buffer_append(&b, method->name, strlen(method->name));
1402 buffer_append(&b, "\0", 1);
1403 list = xstrdup(buffer_ptr(&b));