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.105 2002/06/23 03:30:17 deraadt 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) {
94 /* Add the GSSAPI mechanisms currently supported on this client to
95 * the key exchange algorithm proposal */
96 orig = myproposal[PROPOSAL_KEX_ALGS];
97 gss = ssh_gssapi_mechanisms(0,host);
99 len = strlen(orig)+strlen(gss)+2;
100 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
101 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
106 if (options.ciphers == (char *)-1) {
107 log("No valid ciphers for protocol version 2 given, using defaults.");
108 options.ciphers = NULL;
110 if (options.ciphers != NULL) {
111 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
112 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
114 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
115 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
116 myproposal[PROPOSAL_ENC_ALGS_STOC] =
117 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
118 if (options.compression) {
119 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
120 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
122 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
123 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
125 if (options.macs != NULL) {
126 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
127 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
129 if (options.hostkeyalgorithms != NULL)
130 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
131 options.hostkeyalgorithms;
134 /* If we've got GSSAPI algorithms, then we also support the
135 * 'null' hostkey, as a last resort */
136 if (options.gss_keyex && gss) {
137 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
138 len = strlen(orig)+sizeof(",null");
139 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
140 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
144 /* start key exchange */
145 kex = kex_setup(myproposal);
146 kex->client_version_string=client_version_string;
147 kex->server_version_string=server_version_string;
148 kex->verify_host_key=&verify_host_key_callback;
152 kex->options.gss_deleg_creds=options.gss_deleg_creds;
157 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
159 session_id2 = kex->session_id;
160 session_id2_len = kex->session_id_len;
163 /* send 1st encrypted/maced/compressed message */
164 packet_start(SSH2_MSG_IGNORE);
165 packet_put_cstring("markus");
169 debug("done: ssh_kex2.");
176 typedef struct Authctxt Authctxt;
177 typedef struct Authmethod Authmethod;
179 typedef int sign_cb_fn(
180 Authctxt *authctxt, Key *key,
181 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
184 const char *server_user;
185 const char *local_user;
193 sign_cb_fn *last_key_sign;
195 AuthenticationConnection *agent;
197 Sensitive *sensitive;
198 /* kbd-interactive */
204 char *name; /* string to compare against server's list */
205 int (*userauth)(Authctxt *authctxt);
206 int *enabled; /* flag in option struct that enables method */
207 int *batch_flag; /* flag in option struct that disables method */
210 void input_userauth_success(int, u_int32_t, void *);
211 void input_userauth_failure(int, u_int32_t, void *);
212 void input_userauth_banner(int, u_int32_t, void *);
213 void input_userauth_error(int, u_int32_t, void *);
214 void input_userauth_info_req(int, u_int32_t, void *);
215 void input_userauth_pk_ok(int, u_int32_t, void *);
216 void input_userauth_passwd_changereq(int, u_int32_t, void *);
218 int userauth_none(Authctxt *);
219 int userauth_pubkey(Authctxt *);
220 int userauth_passwd(Authctxt *);
221 int userauth_kbdint(Authctxt *);
222 int userauth_hostbased(Authctxt *);
225 int userauth_external(Authctxt *authctxt);
226 int userauth_gssapi(Authctxt *authctxt);
227 void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
228 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
229 void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
232 void userauth(Authctxt *, char *);
234 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
235 static void clear_auth_state(Authctxt *);
237 static Authmethod *authmethod_get(char *authlist);
238 static Authmethod *authmethod_lookup(const char *name);
239 static char *authmethods_get(void);
241 Authmethod authmethods[] = {
245 &options.gss_authentication,
249 &options.gss_authentication,
254 &options.hostbased_authentication,
258 &options.pubkey_authentication,
260 {"keyboard-interactive",
262 &options.kbd_interactive_authentication,
263 &options.batch_mode},
266 &options.password_authentication,
267 &options.batch_mode},
272 {NULL, NULL, NULL, NULL}
276 ssh_userauth2(const char *local_user, const char *server_user, char *host,
277 Sensitive *sensitive)
282 if (options.challenge_response_authentication)
283 options.kbd_interactive_authentication = 1;
285 debug("send SSH2_MSG_SERVICE_REQUEST");
286 packet_start(SSH2_MSG_SERVICE_REQUEST);
287 packet_put_cstring("ssh-userauth");
290 type = packet_read();
291 if (type != SSH2_MSG_SERVICE_ACCEPT) {
292 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
294 if (packet_remaining() > 0) {
295 char *reply = packet_get_string(NULL);
296 debug("service_accept: %s", reply);
299 debug("buggy server: service_accept w/o service");
302 debug("got SSH2_MSG_SERVICE_ACCEPT");
304 if (options.preferred_authentications == NULL)
305 options.preferred_authentications = authmethods_get();
307 /* setup authentication context */
308 memset(&authctxt, 0, sizeof(authctxt));
309 authctxt.agent = ssh_get_authentication_connection();
310 authctxt.server_user = server_user;
311 authctxt.local_user = local_user;
312 authctxt.host = host;
313 authctxt.service = "ssh-connection"; /* service name */
314 authctxt.success = 0;
315 authctxt.method = authmethod_lookup("none");
316 authctxt.authlist = NULL;
317 authctxt.methoddata = NULL;
318 authctxt.sensitive = sensitive;
319 authctxt.info_req_seen = 0;
320 if (authctxt.method == NULL)
321 fatal("ssh_userauth2: internal error: cannot send userauth none request");
323 /* initial userauth request */
324 userauth_none(&authctxt);
326 dispatch_init(&input_userauth_error);
327 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
328 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
329 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
330 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
332 if (authctxt.agent != NULL)
333 ssh_close_authentication_connection(authctxt.agent);
335 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
338 userauth(Authctxt *authctxt, char *authlist)
340 if (authctxt->methoddata!=NULL) {
341 xfree(authctxt->methoddata);
342 authctxt->methoddata=NULL;
345 if (authlist == NULL) {
346 authlist = authctxt->authlist;
348 if (authctxt->authlist)
349 xfree(authctxt->authlist);
350 authctxt->authlist = authlist;
353 Authmethod *method = authmethod_get(authlist);
355 fatal("Permission denied (%s).", authlist);
356 authctxt->method = method;
357 if (method->userauth(authctxt) != 0) {
358 debug2("we sent a %s packet, wait for reply", method->name);
361 debug2("we did not send a packet, disable method");
362 method->enabled = NULL;
368 input_userauth_error(int type, u_int32_t seq, void *ctxt)
370 fatal("input_userauth_error: bad message during authentication: "
375 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
378 debug3("input_userauth_banner");
379 msg = packet_get_string(NULL);
380 lang = packet_get_string(NULL);
381 fprintf(stderr, "%s", msg);
387 input_userauth_success(int type, u_int32_t seq, void *ctxt)
389 Authctxt *authctxt = ctxt;
390 if (authctxt == NULL)
391 fatal("input_userauth_success: no authentication context");
392 if (authctxt->authlist)
393 xfree(authctxt->authlist);
394 if (authctxt->methoddata)
395 xfree(authctxt->methoddata);
396 clear_auth_state(authctxt);
397 authctxt->success = 1; /* break out */
401 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
403 Authctxt *authctxt = ctxt;
404 char *authlist = NULL;
407 if (authctxt == NULL)
408 fatal("input_userauth_failure: no authentication context");
410 authlist = packet_get_string(NULL);
411 partial = packet_get_char();
415 log("Authenticated with partial success.");
416 debug("authentications that can continue: %s", authlist);
418 clear_auth_state(authctxt);
419 userauth(authctxt, authlist);
422 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
424 Authctxt *authctxt = ctxt;
427 int pktype, sent = 0;
432 if (authctxt == NULL)
433 fatal("input_userauth_pk_ok: no authentication context");
434 if (datafellows & SSH_BUG_PKOK) {
435 /* this is similar to SSH_BUG_PKAUTH */
436 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
437 pkblob = packet_get_string(&blen);
439 buffer_append(&b, pkblob, blen);
440 pkalg = buffer_get_string(&b, &alen);
443 pkalg = packet_get_string(&alen);
444 pkblob = packet_get_string(&blen);
448 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
449 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
452 if (authctxt->last_key == NULL ||
453 authctxt->last_key_sign == NULL) {
454 debug("no last key or no sign cb");
457 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
458 debug("unknown pkalg %s", pkalg);
461 if ((key = key_from_blob(pkblob, blen)) == NULL) {
462 debug("no key from blob. pkalg %s", pkalg);
465 if (key->type != pktype) {
466 error("input_userauth_pk_ok: type mismatch "
467 "for decoded key (received %d, expected %d)",
471 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
472 debug2("input_userauth_pk_ok: fp %s", fp);
474 if (!key_equal(key, authctxt->last_key)) {
475 debug("key != last_key");
478 sent = sign_and_send_pubkey(authctxt, key,
479 authctxt->last_key_sign);
488 clear_auth_state(authctxt);
489 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
491 /* try another method if we did not send a packet*/
493 userauth(authctxt, NULL);
499 userauth_gssapi(Authctxt *authctxt)
504 if (datafellows & SSH_OLD_GSSAPI) return 0;
506 /* Try each mechanism in turn. Give up if we've tried all
507 supported mechanisms.
509 if (mech==GSS_LAST_ENTRY) return 0;
511 /* Initialise as much of our context as we can, so failures can be
512 * trapped before sending any packets.
514 ssh_gssapi_build_ctx(&gssctxt);
516 if (ssh_gssapi_import_name(gssctxt,authctxt->host)) {
520 authctxt->methoddata=(void *)gssctxt;
522 packet_start(SSH2_MSG_USERAUTH_REQUEST);
524 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
525 packet_put_cstring("");
528 packet_put_cstring(authctxt->server_user);
532 packet_put_cstring(authctxt->service);
533 packet_put_cstring(authctxt->method->name);
535 /* FIXME: This assumes that our current GSSAPI implementation
536 * supports all of the mechanisms listed in supported_mechs.
537 * This may not be the case - we should use something along
538 * the lines of the code in gss_genr to remove the ones that
539 * aren't supported */
541 /* Try one GSSAPI mechanism at a time. */
543 packet_put_string(supported_mechs[mech].oid.elements,
544 supported_mechs[mech].oid.length);
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
549 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
551 mech++; /* Move to next mechanism for next time. */
557 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
559 Authctxt *authctxt = ctxt;
564 gss_buffer_desc send_tok;
566 if (authctxt == NULL)
567 fatal("input_gssapi_response: no authentication context");
568 gssctxt = authctxt->methoddata;
571 oidv=packet_get_string(&oidlen);
572 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
576 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
577 GSS_C_NO_BUFFER, &send_tok,
579 if (GSS_ERROR(status)) {
580 /* Start again with next method on list */
581 debug("Trying to start again");
582 userauth(authctxt,NULL);
586 /* We must have data to send */
587 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
588 packet_put_string(send_tok.value,send_tok.length);
591 gss_release_buffer(&ms, &send_tok);
595 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
597 Authctxt *authctxt = ctxt;
599 gss_buffer_desc send_tok,recv_tok;
603 if (authctxt == NULL)
604 fatal("input_gssapi_token: no authentication context");
605 gssctxt = authctxt->methoddata;
607 recv_tok.value=packet_get_string(&slen);
608 recv_tok.length=slen; /* safe typecast */
610 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
611 &recv_tok, &send_tok, NULL);
615 if (GSS_ERROR(status)) {
616 /* Start again with the next method in the list */
617 userauth(authctxt,NULL);
621 if (send_tok.length>0) {
622 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
623 packet_put_string(send_tok.value,send_tok.length);
628 if (status == GSS_S_COMPLETE) {
629 /* If that succeeded, send a exchange complete message */
630 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
637 userauth_external(Authctxt *authctxt)
639 static int attempt =0;
644 debug2("userauth_external");
645 packet_start(SSH2_MSG_USERAUTH_REQUEST);
647 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
648 packet_put_cstring("");
651 packet_put_cstring(authctxt->server_user);
655 packet_put_cstring(authctxt->service);
656 packet_put_cstring(authctxt->method->name);
664 userauth_none(Authctxt *authctxt)
666 /* initial userauth request */
667 packet_start(SSH2_MSG_USERAUTH_REQUEST);
668 packet_put_cstring(authctxt->server_user);
669 packet_put_cstring(authctxt->service);
670 packet_put_cstring(authctxt->method->name);
677 userauth_passwd(Authctxt *authctxt)
679 static int attempt = 0;
683 if (attempt++ >= options.number_of_password_prompts)
687 error("Permission denied, please try again.");
689 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
690 authctxt->server_user, authctxt->host);
691 password = read_passphrase(prompt, 0);
692 packet_start(SSH2_MSG_USERAUTH_REQUEST);
693 packet_put_cstring(authctxt->server_user);
694 packet_put_cstring(authctxt->service);
695 packet_put_cstring(authctxt->method->name);
697 packet_put_cstring(password);
698 memset(password, 0, strlen(password));
700 packet_add_padding(64);
703 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
704 &input_userauth_passwd_changereq);
709 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
712 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
714 Authctxt *authctxt = ctxt;
715 char *info, *lang, *password = NULL, *retype = NULL;
718 debug2("input_userauth_passwd_changereq");
720 if (authctxt == NULL)
721 fatal("input_userauth_passwd_changereq: "
722 "no authentication context");
724 info = packet_get_string(NULL);
725 lang = packet_get_string(NULL);
726 if (strlen(info) > 0)
730 packet_start(SSH2_MSG_USERAUTH_REQUEST);
731 packet_put_cstring(authctxt->server_user);
732 packet_put_cstring(authctxt->service);
733 packet_put_cstring(authctxt->method->name);
734 packet_put_char(1); /* additional info */
735 snprintf(prompt, sizeof(prompt),
736 "Enter %.30s@%.128s's old password: ",
737 authctxt->server_user, authctxt->host);
738 password = read_passphrase(prompt, 0);
739 packet_put_cstring(password);
740 memset(password, 0, strlen(password));
743 while (password == NULL) {
744 snprintf(prompt, sizeof(prompt),
745 "Enter %.30s@%.128s's new password: ",
746 authctxt->server_user, authctxt->host);
747 password = read_passphrase(prompt, RP_ALLOW_EOF);
748 if (password == NULL) {
752 snprintf(prompt, sizeof(prompt),
753 "Retype %.30s@%.128s's new password: ",
754 authctxt->server_user, authctxt->host);
755 retype = read_passphrase(prompt, 0);
756 if (strcmp(password, retype) != 0) {
757 memset(password, 0, strlen(password));
759 log("Mismatch; try again, EOF to quit.");
762 memset(retype, 0, strlen(retype));
765 packet_put_cstring(password);
766 memset(password, 0, strlen(password));
768 packet_add_padding(64);
771 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
772 &input_userauth_passwd_changereq);
776 clear_auth_state(Authctxt *authctxt)
778 /* XXX clear authentication state */
779 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
781 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
782 debug3("clear_auth_state: key_free %p", authctxt->last_key);
783 key_free(authctxt->last_key);
785 authctxt->last_key = NULL;
786 authctxt->last_key_hint = -2;
787 authctxt->last_key_sign = NULL;
791 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
794 u_char *blob, *signature;
800 debug3("sign_and_send_pubkey");
802 if (key_to_blob(k, &blob, &bloblen) == 0) {
803 /* we cannot handle this key */
804 debug3("sign_and_send_pubkey: cannot handle key");
807 /* data to be signed */
809 if (datafellows & SSH_OLD_SESSIONID) {
810 buffer_append(&b, session_id2, session_id2_len);
811 skip = session_id2_len;
813 buffer_put_string(&b, session_id2, session_id2_len);
814 skip = buffer_len(&b);
816 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
817 buffer_put_cstring(&b, authctxt->server_user);
818 buffer_put_cstring(&b,
819 datafellows & SSH_BUG_PKSERVICE ?
822 if (datafellows & SSH_BUG_PKAUTH) {
823 buffer_put_char(&b, have_sig);
825 buffer_put_cstring(&b, authctxt->method->name);
826 buffer_put_char(&b, have_sig);
827 buffer_put_cstring(&b, key_ssh_name(k));
829 buffer_put_string(&b, blob, bloblen);
831 /* generate signature */
832 ret = (*sign_callback)(authctxt, k, &signature, &slen,
833 buffer_ptr(&b), buffer_len(&b));
842 if (datafellows & SSH_BUG_PKSERVICE) {
844 buffer_append(&b, session_id2, session_id2_len);
845 skip = session_id2_len;
846 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
847 buffer_put_cstring(&b, authctxt->server_user);
848 buffer_put_cstring(&b, authctxt->service);
849 buffer_put_cstring(&b, authctxt->method->name);
850 buffer_put_char(&b, have_sig);
851 if (!(datafellows & SSH_BUG_PKAUTH))
852 buffer_put_cstring(&b, key_ssh_name(k));
853 buffer_put_string(&b, blob, bloblen);
857 /* append signature */
858 buffer_put_string(&b, signature, slen);
861 /* skip session id and packet type */
862 if (buffer_len(&b) < skip + 1)
863 fatal("userauth_pubkey: internal error");
864 buffer_consume(&b, skip + 1);
866 /* put remaining data from buffer into packet */
867 packet_start(SSH2_MSG_USERAUTH_REQUEST);
868 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
876 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
880 u_int bloblen, have_sig = 0;
882 debug3("send_pubkey_test");
884 if (key_to_blob(k, &blob, &bloblen) == 0) {
885 /* we cannot handle this key */
886 debug3("send_pubkey_test: cannot handle key");
889 /* register callback for USERAUTH_PK_OK message */
890 authctxt->last_key_sign = sign_callback;
891 authctxt->last_key_hint = hint;
892 authctxt->last_key = k;
893 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
895 packet_start(SSH2_MSG_USERAUTH_REQUEST);
896 packet_put_cstring(authctxt->server_user);
897 packet_put_cstring(authctxt->service);
898 packet_put_cstring(authctxt->method->name);
899 packet_put_char(have_sig);
900 if (!(datafellows & SSH_BUG_PKAUTH))
901 packet_put_cstring(key_ssh_name(k));
902 packet_put_string(blob, bloblen);
909 load_identity_file(char *filename)
912 char prompt[300], *passphrase;
916 if (stat(filename, &st) < 0) {
917 debug3("no such identity: %s", filename);
920 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
921 if (private == NULL) {
922 if (options.batch_mode)
924 snprintf(prompt, sizeof prompt,
925 "Enter passphrase for key '%.100s': ", filename);
926 for (i = 0; i < options.number_of_password_prompts; i++) {
927 passphrase = read_passphrase(prompt, 0);
928 if (strcmp(passphrase, "") != 0) {
929 private = key_load_private_type(KEY_UNSPEC, filename,
933 debug2("no passphrase given, try next key");
936 memset(passphrase, 0, strlen(passphrase));
938 if (private != NULL || quit)
940 debug2("bad passphrase given, try again...");
947 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
948 u_char *data, u_int datalen)
953 idx = authctxt->last_key_hint;
957 /* private key is stored in external hardware */
958 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
959 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
961 private = load_identity_file(options.identity_files[idx]);
964 ret = key_sign(private, sigp, lenp, data, datalen);
970 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
971 u_char *data, u_int datalen)
973 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
977 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
978 u_char *data, u_int datalen)
980 return key_sign(key, sigp, lenp, data, datalen);
984 userauth_pubkey_agent(Authctxt *authctxt)
986 static int called = 0;
992 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
993 debug2("userauth_pubkey_agent: no keys at all");
996 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
998 debug2("userauth_pubkey_agent: no more keys");
1000 debug("userauth_pubkey_agent: testing agent key %s", comment);
1002 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1007 debug2("userauth_pubkey_agent: no message sent");
1012 userauth_pubkey(Authctxt *authctxt)
1019 if (authctxt->agent != NULL) {
1021 sent = userauth_pubkey_agent(authctxt);
1022 } while (!sent && authctxt->agent->howmany > 0);
1024 while (!sent && idx < options.num_identity_files) {
1025 key = options.identity_keys[idx];
1026 filename = options.identity_files[idx];
1028 debug("try privkey: %s", filename);
1029 key = load_identity_file(filename);
1031 sent = sign_and_send_pubkey(authctxt, key,
1035 } else if (key->type != KEY_RSA1) {
1036 debug("try pubkey: %s", filename);
1037 sent = send_pubkey_test(authctxt, key,
1038 identity_sign_cb, idx);
1046 * Send userauth request message specifying keyboard-interactive method.
1049 userauth_kbdint(Authctxt *authctxt)
1051 static int attempt = 0;
1053 if (attempt++ >= options.number_of_password_prompts)
1055 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1056 if (attempt > 1 && !authctxt->info_req_seen) {
1057 debug3("userauth_kbdint: disable: no info_req_seen");
1058 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1062 debug2("userauth_kbdint");
1063 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1064 packet_put_cstring(authctxt->server_user);
1065 packet_put_cstring(authctxt->service);
1066 packet_put_cstring(authctxt->method->name);
1067 packet_put_cstring(""); /* lang */
1068 packet_put_cstring(options.kbd_interactive_devices ?
1069 options.kbd_interactive_devices : "");
1072 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1077 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1080 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1082 Authctxt *authctxt = ctxt;
1083 char *name, *inst, *lang, *prompt, *response;
1084 u_int num_prompts, i;
1087 debug2("input_userauth_info_req");
1089 if (authctxt == NULL)
1090 fatal("input_userauth_info_req: no authentication context");
1092 authctxt->info_req_seen = 1;
1094 name = packet_get_string(NULL);
1095 inst = packet_get_string(NULL);
1096 lang = packet_get_string(NULL);
1097 if (strlen(name) > 0)
1099 if (strlen(inst) > 0)
1105 num_prompts = packet_get_int();
1107 * Begin to build info response packet based on prompts requested.
1108 * We commit to providing the correct number of responses, so if
1109 * further on we run into a problem that prevents this, we have to
1110 * be sure and clean this up and send a correct error response.
1112 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1113 packet_put_int(num_prompts);
1115 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1116 for (i = 0; i < num_prompts; i++) {
1117 prompt = packet_get_string(NULL);
1118 echo = packet_get_char();
1120 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1122 packet_put_cstring(response);
1123 memset(response, 0, strlen(response));
1127 packet_check_eom(); /* done with parsing incoming message. */
1129 packet_add_padding(64);
1134 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1135 u_char *data, u_int datalen)
1140 int to[2], from[2], status, version = 2;
1142 debug("ssh_keysign called");
1144 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1145 error("ssh_keysign: no installed: %s", strerror(errno));
1148 if (fflush(stdout) != 0)
1149 error("ssh_keysign: fflush: %s", strerror(errno));
1151 error("ssh_keysign: pipe: %s", strerror(errno));
1154 if (pipe(from) < 0) {
1155 error("ssh_keysign: pipe: %s", strerror(errno));
1158 if ((pid = fork()) < 0) {
1159 error("ssh_keysign: fork: %s", strerror(errno));
1166 if (dup2(from[1], STDOUT_FILENO) < 0)
1167 fatal("ssh_keysign: dup2: %s", strerror(errno));
1169 if (dup2(to[0], STDIN_FILENO) < 0)
1170 fatal("ssh_keysign: dup2: %s", strerror(errno));
1173 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1174 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1181 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1182 buffer_put_string(&b, data, datalen);
1183 msg_send(to[1], version, &b);
1185 if (msg_recv(from[0], &b) < 0) {
1186 error("ssh_keysign: no reply");
1193 while (waitpid(pid, &status, 0) < 0)
1197 if (buffer_get_char(&b) != version) {
1198 error("ssh_keysign: bad version");
1202 *sigp = buffer_get_string(&b, lenp);
1209 userauth_hostbased(Authctxt *authctxt)
1211 Key *private = NULL;
1212 Sensitive *sensitive = authctxt->sensitive;
1214 u_char *signature, *blob;
1215 char *chost, *pkalg, *p;
1216 const char *service;
1218 int ok, i, len, found = 0;
1220 /* check for a useful key */
1221 for (i = 0; i < sensitive->nkeys; i++) {
1222 private = sensitive->keys[i];
1223 if (private && private->type != KEY_RSA1) {
1225 /* we take and free the key */
1226 sensitive->keys[i] = NULL;
1231 debug("userauth_hostbased: no more client hostkeys");
1234 if (key_to_blob(private, &blob, &blen) == 0) {
1238 /* figure out a name for the client host */
1239 p = get_local_name(packet_get_connection_in());
1241 error("userauth_hostbased: cannot get local ipaddr/name");
1245 len = strlen(p) + 2;
1246 chost = xmalloc(len);
1247 strlcpy(chost, p, len);
1248 strlcat(chost, ".", len);
1249 debug2("userauth_hostbased: chost %s", chost);
1251 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1253 pkalg = xstrdup(key_ssh_name(private));
1255 /* construct data */
1256 buffer_put_string(&b, session_id2, session_id2_len);
1257 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1258 buffer_put_cstring(&b, authctxt->server_user);
1259 buffer_put_cstring(&b, service);
1260 buffer_put_cstring(&b, authctxt->method->name);
1261 buffer_put_cstring(&b, pkalg);
1262 buffer_put_string(&b, blob, blen);
1263 buffer_put_cstring(&b, chost);
1264 buffer_put_cstring(&b, authctxt->local_user);
1268 if (sensitive->external_keysign)
1269 ok = ssh_keysign(private, &signature, &slen,
1270 buffer_ptr(&b), buffer_len(&b));
1272 ok = key_sign(private, &signature, &slen,
1273 buffer_ptr(&b), buffer_len(&b));
1277 error("key_sign failed");
1282 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1283 packet_put_cstring(authctxt->server_user);
1284 packet_put_cstring(authctxt->service);
1285 packet_put_cstring(authctxt->method->name);
1286 packet_put_cstring(pkalg);
1287 packet_put_string(blob, blen);
1288 packet_put_cstring(chost);
1289 packet_put_cstring(authctxt->local_user);
1290 packet_put_string(signature, slen);
1291 memset(signature, 's', slen);
1300 /* find auth method */
1303 * given auth method name, if configurable options permit this method fill
1304 * in auth_ident field and return true, otherwise return false.
1307 authmethod_is_enabled(Authmethod *method)
1311 /* return false if options indicate this method is disabled */
1312 if (method->enabled == NULL || *method->enabled == 0)
1314 /* return false if batch mode is enabled but method needs interactive mode */
1315 if (method->batch_flag != NULL && *method->batch_flag != 0)
1321 authmethod_lookup(const char *name)
1323 Authmethod *method = NULL;
1325 for (method = authmethods; method->name != NULL; method++)
1326 if (strcmp(name, method->name) == 0)
1328 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1332 /* XXX internal state */
1333 static Authmethod *current = NULL;
1334 static char *supported = NULL;
1335 static char *preferred = NULL;
1338 * Given the authentication method list sent by the server, return the
1339 * next method we should try. If the server initially sends a nil list,
1340 * use a built-in default list.
1343 authmethod_get(char *authlist)
1349 /* Use a suitable default if we're passed a nil list. */
1350 if (authlist == NULL || strlen(authlist) == 0)
1351 authlist = options.preferred_authentications;
1353 if (supported == NULL || strcmp(authlist, supported) != 0) {
1354 debug3("start over, passed a different list %s", authlist);
1355 if (supported != NULL)
1357 supported = xstrdup(authlist);
1358 preferred = options.preferred_authentications;
1359 debug3("preferred %s", preferred);
1361 } else if (current != NULL && authmethod_is_enabled(current))
1365 if ((name = match_list(preferred, supported, &next)) == NULL) {
1366 debug("no more auth methods to try");
1371 debug3("authmethod_lookup %s", name);
1372 debug3("remaining preferred: %s", preferred);
1373 if ((current = authmethod_lookup(name)) != NULL &&
1374 authmethod_is_enabled(current)) {
1375 debug3("authmethod_is_enabled %s", name);
1376 debug("next auth method to try is %s", name);
1383 authmethods_get(void)
1385 Authmethod *method = NULL;
1390 for (method = authmethods; method->name != NULL; method++) {
1391 if (authmethod_is_enabled(method)) {
1392 if (buffer_len(&b) > 0)
1393 buffer_append(&b, ",", 1);
1394 buffer_append(&b, method->name, strlen(method->name));
1397 buffer_append(&b, "\0", 1);
1398 list = xstrdup(buffer_ptr(&b));