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.114 2003/04/01 10:22:21 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 /* Add the GSSAPI mechanisms currently supported on this client to
94 * the key exchange algorithm proposal */
95 orig = myproposal[PROPOSAL_KEX_ALGS];
96 gss = ssh_gssapi_client_mechanisms(host);
98 len = strlen(orig)+strlen(gss)+2;
99 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
100 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
104 if (options.ciphers == (char *)-1) {
105 log("No valid ciphers for protocol version 2 given, using defaults.");
106 options.ciphers = NULL;
108 if (options.ciphers != NULL) {
109 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
110 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
112 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
113 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
114 myproposal[PROPOSAL_ENC_ALGS_STOC] =
115 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
116 if (options.compression) {
117 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
118 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
120 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
121 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
123 if (options.macs != NULL) {
124 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
125 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
127 if (options.hostkeyalgorithms != NULL)
128 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
129 options.hostkeyalgorithms;
132 /* If we've got GSSAPI algorithms, then we also support the
133 * 'null' hostkey, as a last resort */
135 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
136 len = strlen(orig)+sizeof(",null");
137 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
138 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
141 /* start key exchange */
142 kex = kex_setup(myproposal);
143 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
144 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
146 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
148 kex->client_version_string=client_version_string;
149 kex->server_version_string=server_version_string;
150 kex->verify_host_key=&verify_host_key_callback;
153 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");
175 typedef struct Authctxt Authctxt;
176 typedef struct Authmethod Authmethod;
178 typedef int sign_cb_fn(
179 Authctxt *authctxt, Key *key,
180 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
183 const char *server_user;
184 const char *local_user;
192 sign_cb_fn *last_key_sign;
194 AuthenticationConnection *agent;
196 Sensitive *sensitive;
197 /* kbd-interactive */
203 char *name; /* string to compare against server's list */
204 int (*userauth)(Authctxt *authctxt);
205 int *enabled; /* flag in option struct that enables method */
206 int *batch_flag; /* flag in option struct that disables method */
209 void input_userauth_success(int, u_int32_t, void *);
210 void input_userauth_failure(int, u_int32_t, void *);
211 void input_userauth_banner(int, u_int32_t, void *);
212 void input_userauth_error(int, u_int32_t, void *);
213 void input_userauth_info_req(int, u_int32_t, void *);
214 void input_userauth_pk_ok(int, u_int32_t, void *);
215 void input_userauth_passwd_changereq(int, u_int32_t, void *);
217 int userauth_none(Authctxt *);
218 int userauth_pubkey(Authctxt *);
219 int userauth_passwd(Authctxt *);
220 int userauth_kbdint(Authctxt *);
221 int userauth_hostbased(Authctxt *);
224 int userauth_external(Authctxt *authctxt);
225 int userauth_gssapi(Authctxt *authctxt);
226 void input_gssapi_response(int type, u_int32_t, void *);
227 void input_gssapi_token(int type, u_int32_t, void *);
228 void input_gssapi_hash(int type, u_int32_t, void *);
229 void input_gssapi_error(int, u_int32_t, void *);
230 void input_gssapi_errtok(int, u_int32_t, void *);
233 void userauth(Authctxt *, char *);
235 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
236 static void clear_auth_state(Authctxt *);
238 static Authmethod *authmethod_get(char *authlist);
239 static Authmethod *authmethod_lookup(const char *name);
240 static char *authmethods_get(void);
242 Authmethod authmethods[] = {
246 &options.gss_authentication,
250 &options.gss_authentication,
255 &options.hostbased_authentication,
259 &options.pubkey_authentication,
261 {"keyboard-interactive",
263 &options.kbd_interactive_authentication,
264 &options.batch_mode},
267 &options.password_authentication,
268 &options.batch_mode},
273 {NULL, NULL, NULL, NULL}
277 ssh_userauth2(const char *local_user, const char *server_user, char *host,
278 Sensitive *sensitive)
283 if (options.challenge_response_authentication)
284 options.kbd_interactive_authentication = 1;
286 packet_start(SSH2_MSG_SERVICE_REQUEST);
287 packet_put_cstring("ssh-userauth");
289 debug("SSH2_MSG_SERVICE_REQUEST sent");
291 type = packet_read();
292 if (type != SSH2_MSG_SERVICE_ACCEPT)
293 fatal("Server denied authentication request: %d", type);
294 if (packet_remaining() > 0) {
295 char *reply = packet_get_string(NULL);
296 debug2("service_accept: %s", reply);
299 debug2("buggy server: service_accept w/o service");
302 debug("SSH2_MSG_SERVICE_ACCEPT received");
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("Authentication succeeded (%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("Server accepts key: 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)
501 Gssctxt *gssctxt = NULL;
502 static gss_OID_set supported = NULL;
507 /* Things work better if we send one mechanism at a time, rather
508 * than them all at once. This means that if we fail at some point
509 * in the middle of a negotiation, we can come back and try something
512 if (datafellows & SSH_OLD_GSSAPI) return 0;
514 /* Before we offer a mechanism, check that we can support it. Don't
515 * bother trying to get credentials - as the standard fallback will
516 * deal with that kind of failure.
519 if (supported==NULL) gss_indicate_mechs(&min, &supported);
521 while (mech<supported->count && !ok) {
522 if (gssctxt) ssh_gssapi_delete_ctx(&gssctxt);
523 ssh_gssapi_build_ctx(&gssctxt);
524 ssh_gssapi_set_oid(gssctxt,&supported->elements[mech]);
526 /* The DER encoding below only works for lengths<128,
529 if (supported->elements[mech].length<128 &&
530 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
532 ok = 1; /* Mechanism works */
540 authctxt->methoddata=(void *)gssctxt;
542 packet_start(SSH2_MSG_USERAUTH_REQUEST);
543 packet_put_cstring(authctxt->server_user);
544 packet_put_cstring(authctxt->service);
545 packet_put_cstring(authctxt->method->name);
549 /* The newest gsskeyex draft stipulates that OIDs should
550 * be DER encoded, so we need to add the object type and
551 * length information back on */
552 if (datafellows & SSH_BUG_GSSAPI_BER) {
553 packet_put_string(supported->elements[mech].elements,
554 supported->elements[mech].length);
556 packet_put_int((supported->elements[mech].length)+2);
557 packet_put_char(0x06);
558 packet_put_char(supported->elements[mech].length);
559 packet_put_raw(supported->elements[mech].elements,
560 supported->elements[mech].length);
566 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
567 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
568 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,&input_gssapi_error);
569 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,&input_gssapi_errtok);
571 mech++; /* Move along to next candidate */
577 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
579 Authctxt *authctxt = ctxt;
584 gss_buffer_desc send_tok;
586 if (authctxt == NULL)
587 fatal("input_gssapi_response: no authentication context");
588 gssctxt = authctxt->methoddata;
591 oidv=packet_get_string(&oidlen);
593 if (datafellows & SSH_BUG_GSSAPI_BER) {
594 if (!ssh_gssapi_check_oid(gssctxt,oidv,oidlen)) {
595 fatal("Server returned different OID than expected");
597 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
599 if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
600 debug("Badly encoded mechanism OID received");
601 clear_auth_state(authctxt);
602 userauth(authctxt,NULL);
605 if (!ssh_gssapi_check_oid(gssctxt,oidv+2,oidlen-2)) {
606 fatal("Server returned different OID than expected");
608 ssh_gssapi_set_oid_data(gssctxt,oidv+2,oidlen-2);
613 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
614 GSS_C_NO_BUFFER, &send_tok,
616 if (GSS_ERROR(status)) {
617 if (send_tok.length>0) {
618 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
619 packet_put_string(send_tok.value,send_tok.length);
623 /* Start again with next method on list */
624 debug("Trying to start again");
625 clear_auth_state(authctxt);
626 userauth(authctxt,NULL);
630 /* We must have data to send */
631 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
632 packet_put_string(send_tok.value,send_tok.length);
635 gss_release_buffer(&ms, &send_tok);
639 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
641 Authctxt *authctxt = ctxt;
643 gss_buffer_desc send_tok,recv_tok;
646 if (authctxt == NULL)
647 fatal("input_gssapi_response: no authentication context");
648 gssctxt = authctxt->methoddata;
650 recv_tok.value=packet_get_string(&recv_tok.length);
652 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
653 &recv_tok, &send_tok, NULL);
657 if (GSS_ERROR(status)) {
658 if (send_tok.length>0) {
659 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
660 packet_put_string(send_tok.value,send_tok.length);
664 /* Start again with the next method in the list */
665 clear_auth_state(authctxt);
666 userauth(authctxt,NULL);
670 if (send_tok.length>0) {
671 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
672 packet_put_string(send_tok.value,send_tok.length);
677 if (status == GSS_S_COMPLETE) {
678 /* If that succeeded, send a exchange complete message */
679 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
686 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
688 Authctxt *authctxt = ctxt;
690 gss_buffer_desc send_tok,recv_tok;
693 if (authctxt == NULL)
694 fatal("input_gssapi_response: no authentication context");
695 gssctxt = authctxt->methoddata;
697 recv_tok.value=packet_get_string(&recv_tok.length);
699 /* Stick it into GSSAPI and see what it says */
700 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
701 &recv_tok, &send_tok, NULL);
705 /* We can't send a packet to the server */
707 /* The draft says that we should wait for the server to fail
708 * before starting the next authentication. So, we clear the
709 * state, but don't do anything else */
710 clear_auth_state(authctxt);
715 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
721 maj=packet_get_int();
722 min=packet_get_int();
723 msg=packet_get_string(NULL);
724 lang=packet_get_string(NULL);
728 fprintf(stderr, "Server GSSAPI Error:\n%s\n", msg);
734 userauth_external(Authctxt *authctxt)
736 static int attempt =0;
741 debug2("userauth_external");
742 packet_start(SSH2_MSG_USERAUTH_REQUEST);
743 packet_put_cstring(authctxt->server_user);
744 packet_put_cstring(authctxt->service);
745 packet_put_cstring(authctxt->method->name);
753 userauth_none(Authctxt *authctxt)
755 /* initial userauth request */
756 packet_start(SSH2_MSG_USERAUTH_REQUEST);
757 packet_put_cstring(authctxt->server_user);
758 packet_put_cstring(authctxt->service);
759 packet_put_cstring(authctxt->method->name);
766 userauth_passwd(Authctxt *authctxt)
768 static int attempt = 0;
772 if (attempt++ >= options.number_of_password_prompts)
776 error("Permission denied, please try again.");
778 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
779 authctxt->server_user, authctxt->host);
780 password = read_passphrase(prompt, 0);
781 packet_start(SSH2_MSG_USERAUTH_REQUEST);
782 packet_put_cstring(authctxt->server_user);
783 packet_put_cstring(authctxt->service);
784 packet_put_cstring(authctxt->method->name);
786 packet_put_cstring(password);
787 memset(password, 0, strlen(password));
789 packet_add_padding(64);
792 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
793 &input_userauth_passwd_changereq);
798 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
801 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
803 Authctxt *authctxt = ctxt;
804 char *info, *lang, *password = NULL, *retype = NULL;
807 debug2("input_userauth_passwd_changereq");
809 if (authctxt == NULL)
810 fatal("input_userauth_passwd_changereq: "
811 "no authentication context");
813 info = packet_get_string(NULL);
814 lang = packet_get_string(NULL);
815 if (strlen(info) > 0)
819 packet_start(SSH2_MSG_USERAUTH_REQUEST);
820 packet_put_cstring(authctxt->server_user);
821 packet_put_cstring(authctxt->service);
822 packet_put_cstring(authctxt->method->name);
823 packet_put_char(1); /* additional info */
824 snprintf(prompt, sizeof(prompt),
825 "Enter %.30s@%.128s's old password: ",
826 authctxt->server_user, authctxt->host);
827 password = read_passphrase(prompt, 0);
828 packet_put_cstring(password);
829 memset(password, 0, strlen(password));
832 while (password == NULL) {
833 snprintf(prompt, sizeof(prompt),
834 "Enter %.30s@%.128s's new password: ",
835 authctxt->server_user, authctxt->host);
836 password = read_passphrase(prompt, RP_ALLOW_EOF);
837 if (password == NULL) {
841 snprintf(prompt, sizeof(prompt),
842 "Retype %.30s@%.128s's new password: ",
843 authctxt->server_user, authctxt->host);
844 retype = read_passphrase(prompt, 0);
845 if (strcmp(password, retype) != 0) {
846 memset(password, 0, strlen(password));
848 log("Mismatch; try again, EOF to quit.");
851 memset(retype, 0, strlen(retype));
854 packet_put_cstring(password);
855 memset(password, 0, strlen(password));
857 packet_add_padding(64);
860 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
861 &input_userauth_passwd_changereq);
865 clear_auth_state(Authctxt *authctxt)
867 /* XXX clear authentication state */
868 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
870 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
871 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
872 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
875 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
876 debug3("clear_auth_state: key_free %p", authctxt->last_key);
877 key_free(authctxt->last_key);
879 authctxt->last_key = NULL;
880 authctxt->last_key_hint = -2;
881 authctxt->last_key_sign = NULL;
885 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
888 u_char *blob, *signature;
894 debug3("sign_and_send_pubkey");
896 if (key_to_blob(k, &blob, &bloblen) == 0) {
897 /* we cannot handle this key */
898 debug3("sign_and_send_pubkey: cannot handle key");
901 /* data to be signed */
903 if (datafellows & SSH_OLD_SESSIONID) {
904 buffer_append(&b, session_id2, session_id2_len);
905 skip = session_id2_len;
907 buffer_put_string(&b, session_id2, session_id2_len);
908 skip = buffer_len(&b);
910 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
911 buffer_put_cstring(&b, authctxt->server_user);
912 buffer_put_cstring(&b,
913 datafellows & SSH_BUG_PKSERVICE ?
916 if (datafellows & SSH_BUG_PKAUTH) {
917 buffer_put_char(&b, have_sig);
919 buffer_put_cstring(&b, authctxt->method->name);
920 buffer_put_char(&b, have_sig);
921 buffer_put_cstring(&b, key_ssh_name(k));
923 buffer_put_string(&b, blob, bloblen);
925 /* generate signature */
926 ret = (*sign_callback)(authctxt, k, &signature, &slen,
927 buffer_ptr(&b), buffer_len(&b));
936 if (datafellows & SSH_BUG_PKSERVICE) {
938 buffer_append(&b, session_id2, session_id2_len);
939 skip = session_id2_len;
940 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
941 buffer_put_cstring(&b, authctxt->server_user);
942 buffer_put_cstring(&b, authctxt->service);
943 buffer_put_cstring(&b, authctxt->method->name);
944 buffer_put_char(&b, have_sig);
945 if (!(datafellows & SSH_BUG_PKAUTH))
946 buffer_put_cstring(&b, key_ssh_name(k));
947 buffer_put_string(&b, blob, bloblen);
951 /* append signature */
952 buffer_put_string(&b, signature, slen);
955 /* skip session id and packet type */
956 if (buffer_len(&b) < skip + 1)
957 fatal("userauth_pubkey: internal error");
958 buffer_consume(&b, skip + 1);
960 /* put remaining data from buffer into packet */
961 packet_start(SSH2_MSG_USERAUTH_REQUEST);
962 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
970 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
974 u_int bloblen, have_sig = 0;
976 debug3("send_pubkey_test");
978 if (key_to_blob(k, &blob, &bloblen) == 0) {
979 /* we cannot handle this key */
980 debug3("send_pubkey_test: cannot handle key");
983 /* register callback for USERAUTH_PK_OK message */
984 authctxt->last_key_sign = sign_callback;
985 authctxt->last_key_hint = hint;
986 authctxt->last_key = k;
987 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
989 packet_start(SSH2_MSG_USERAUTH_REQUEST);
990 packet_put_cstring(authctxt->server_user);
991 packet_put_cstring(authctxt->service);
992 packet_put_cstring(authctxt->method->name);
993 packet_put_char(have_sig);
994 if (!(datafellows & SSH_BUG_PKAUTH))
995 packet_put_cstring(key_ssh_name(k));
996 packet_put_string(blob, bloblen);
1003 load_identity_file(char *filename)
1006 char prompt[300], *passphrase;
1010 if (stat(filename, &st) < 0) {
1011 debug3("no such identity: %s", filename);
1014 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1015 if (private == NULL) {
1016 if (options.batch_mode)
1018 snprintf(prompt, sizeof prompt,
1019 "Enter passphrase for key '%.100s': ", filename);
1020 for (i = 0; i < options.number_of_password_prompts; i++) {
1021 passphrase = read_passphrase(prompt, 0);
1022 if (strcmp(passphrase, "") != 0) {
1023 private = key_load_private_type(KEY_UNSPEC, filename,
1027 debug2("no passphrase given, try next key");
1030 memset(passphrase, 0, strlen(passphrase));
1032 if (private != NULL || quit)
1034 debug2("bad passphrase given, try again...");
1041 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1042 u_char *data, u_int datalen)
1047 idx = authctxt->last_key_hint;
1051 /* private key is stored in external hardware */
1052 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1053 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
1055 private = load_identity_file(options.identity_files[idx]);
1056 if (private == NULL)
1058 ret = key_sign(private, sigp, lenp, data, datalen);
1064 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1065 u_char *data, u_int datalen)
1067 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1071 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1072 u_char *data, u_int datalen)
1074 return key_sign(key, sigp, lenp, data, datalen);
1078 userauth_pubkey_agent(Authctxt *authctxt)
1080 static int called = 0;
1086 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1087 debug2("userauth_pubkey_agent: no keys at all");
1090 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1092 debug2("userauth_pubkey_agent: no more keys");
1094 debug("Offering agent key: %s", comment);
1096 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1101 debug2("userauth_pubkey_agent: no message sent");
1106 userauth_pubkey(Authctxt *authctxt)
1113 if (authctxt->agent != NULL) {
1115 sent = userauth_pubkey_agent(authctxt);
1116 } while (!sent && authctxt->agent->howmany > 0);
1118 while (!sent && idx < options.num_identity_files) {
1119 key = options.identity_keys[idx];
1120 filename = options.identity_files[idx];
1122 debug("Trying private key: %s", filename);
1123 key = load_identity_file(filename);
1125 sent = sign_and_send_pubkey(authctxt, key,
1129 } else if (key->type != KEY_RSA1) {
1130 debug("Offering public key: %s", filename);
1131 sent = send_pubkey_test(authctxt, key,
1132 identity_sign_cb, idx);
1140 * Send userauth request message specifying keyboard-interactive method.
1143 userauth_kbdint(Authctxt *authctxt)
1145 static int attempt = 0;
1147 if (attempt++ >= options.number_of_password_prompts)
1149 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1150 if (attempt > 1 && !authctxt->info_req_seen) {
1151 debug3("userauth_kbdint: disable: no info_req_seen");
1152 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1156 debug2("userauth_kbdint");
1157 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1158 packet_put_cstring(authctxt->server_user);
1159 packet_put_cstring(authctxt->service);
1160 packet_put_cstring(authctxt->method->name);
1161 packet_put_cstring(""); /* lang */
1162 packet_put_cstring(options.kbd_interactive_devices ?
1163 options.kbd_interactive_devices : "");
1166 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1171 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1174 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1176 Authctxt *authctxt = ctxt;
1177 char *name, *inst, *lang, *prompt, *response;
1178 u_int num_prompts, i;
1181 debug2("input_userauth_info_req");
1183 if (authctxt == NULL)
1184 fatal("input_userauth_info_req: no authentication context");
1186 authctxt->info_req_seen = 1;
1188 name = packet_get_string(NULL);
1189 inst = packet_get_string(NULL);
1190 lang = packet_get_string(NULL);
1191 if (strlen(name) > 0)
1193 if (strlen(inst) > 0)
1199 num_prompts = packet_get_int();
1201 * Begin to build info response packet based on prompts requested.
1202 * We commit to providing the correct number of responses, so if
1203 * further on we run into a problem that prevents this, we have to
1204 * be sure and clean this up and send a correct error response.
1206 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1207 packet_put_int(num_prompts);
1209 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1210 for (i = 0; i < num_prompts; i++) {
1211 prompt = packet_get_string(NULL);
1212 echo = packet_get_char();
1214 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1216 packet_put_cstring(response);
1217 memset(response, 0, strlen(response));
1221 packet_check_eom(); /* done with parsing incoming message. */
1223 packet_add_padding(64);
1228 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1229 u_char *data, u_int datalen)
1234 int to[2], from[2], status, version = 2;
1236 debug2("ssh_keysign called");
1238 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1239 error("ssh_keysign: no installed: %s", strerror(errno));
1242 if (fflush(stdout) != 0)
1243 error("ssh_keysign: fflush: %s", strerror(errno));
1245 error("ssh_keysign: pipe: %s", strerror(errno));
1248 if (pipe(from) < 0) {
1249 error("ssh_keysign: pipe: %s", strerror(errno));
1252 if ((pid = fork()) < 0) {
1253 error("ssh_keysign: fork: %s", strerror(errno));
1260 if (dup2(from[1], STDOUT_FILENO) < 0)
1261 fatal("ssh_keysign: dup2: %s", strerror(errno));
1263 if (dup2(to[0], STDIN_FILENO) < 0)
1264 fatal("ssh_keysign: dup2: %s", strerror(errno));
1267 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1268 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1275 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1276 buffer_put_string(&b, data, datalen);
1277 ssh_msg_send(to[1], version, &b);
1279 if (ssh_msg_recv(from[0], &b) < 0) {
1280 error("ssh_keysign: no reply");
1287 while (waitpid(pid, &status, 0) < 0)
1291 if (buffer_get_char(&b) != version) {
1292 error("ssh_keysign: bad version");
1296 *sigp = buffer_get_string(&b, lenp);
1303 userauth_hostbased(Authctxt *authctxt)
1305 Key *private = NULL;
1306 Sensitive *sensitive = authctxt->sensitive;
1308 u_char *signature, *blob;
1309 char *chost, *pkalg, *p;
1310 const char *service;
1312 int ok, i, len, found = 0;
1314 /* check for a useful key */
1315 for (i = 0; i < sensitive->nkeys; i++) {
1316 private = sensitive->keys[i];
1317 if (private && private->type != KEY_RSA1) {
1319 /* we take and free the key */
1320 sensitive->keys[i] = NULL;
1325 debug("No more client hostkeys for hostbased authentication.");
1328 if (key_to_blob(private, &blob, &blen) == 0) {
1332 /* figure out a name for the client host */
1333 p = get_local_name(packet_get_connection_in());
1335 error("userauth_hostbased: cannot get local ipaddr/name");
1339 len = strlen(p) + 2;
1340 chost = xmalloc(len);
1341 strlcpy(chost, p, len);
1342 strlcat(chost, ".", len);
1343 debug2("userauth_hostbased: chost %s", chost);
1346 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1348 pkalg = xstrdup(key_ssh_name(private));
1350 /* construct data */
1351 buffer_put_string(&b, session_id2, session_id2_len);
1352 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1353 buffer_put_cstring(&b, authctxt->server_user);
1354 buffer_put_cstring(&b, service);
1355 buffer_put_cstring(&b, authctxt->method->name);
1356 buffer_put_cstring(&b, pkalg);
1357 buffer_put_string(&b, blob, blen);
1358 buffer_put_cstring(&b, chost);
1359 buffer_put_cstring(&b, authctxt->local_user);
1363 if (sensitive->external_keysign)
1364 ok = ssh_keysign(private, &signature, &slen,
1365 buffer_ptr(&b), buffer_len(&b));
1367 ok = key_sign(private, &signature, &slen,
1368 buffer_ptr(&b), buffer_len(&b));
1372 error("key_sign failed");
1377 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1378 packet_put_cstring(authctxt->server_user);
1379 packet_put_cstring(authctxt->service);
1380 packet_put_cstring(authctxt->method->name);
1381 packet_put_cstring(pkalg);
1382 packet_put_string(blob, blen);
1383 packet_put_cstring(chost);
1384 packet_put_cstring(authctxt->local_user);
1385 packet_put_string(signature, slen);
1386 memset(signature, 's', slen);
1395 /* find auth method */
1398 * given auth method name, if configurable options permit this method fill
1399 * in auth_ident field and return true, otherwise return false.
1402 authmethod_is_enabled(Authmethod *method)
1406 /* return false if options indicate this method is disabled */
1407 if (method->enabled == NULL || *method->enabled == 0)
1409 /* return false if batch mode is enabled but method needs interactive mode */
1410 if (method->batch_flag != NULL && *method->batch_flag != 0)
1416 authmethod_lookup(const char *name)
1418 Authmethod *method = NULL;
1420 for (method = authmethods; method->name != NULL; method++)
1421 if (strcmp(name, method->name) == 0)
1423 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1427 /* XXX internal state */
1428 static Authmethod *current = NULL;
1429 static char *supported = NULL;
1430 static char *preferred = NULL;
1433 * Given the authentication method list sent by the server, return the
1434 * next method we should try. If the server initially sends a nil list,
1435 * use a built-in default list.
1438 authmethod_get(char *authlist)
1443 /* Use a suitable default if we're passed a nil list. */
1444 if (authlist == NULL || strlen(authlist) == 0)
1445 authlist = options.preferred_authentications;
1447 if (supported == NULL || strcmp(authlist, supported) != 0) {
1448 debug3("start over, passed a different list %s", authlist);
1449 if (supported != NULL)
1451 supported = xstrdup(authlist);
1452 preferred = options.preferred_authentications;
1453 debug3("preferred %s", preferred);
1455 } else if (current != NULL && authmethod_is_enabled(current))
1459 if ((name = match_list(preferred, supported, &next)) == NULL) {
1460 debug("No more authentication methods to try.");
1465 debug3("authmethod_lookup %s", name);
1466 debug3("remaining preferred: %s", preferred);
1467 if ((current = authmethod_lookup(name)) != NULL &&
1468 authmethod_is_enabled(current)) {
1469 debug3("authmethod_is_enabled %s", name);
1470 debug("Next authentication method: %s", name);
1477 authmethods_get(void)
1479 Authmethod *method = NULL;
1484 for (method = authmethods; method->name != NULL; method++) {
1485 if (authmethod_is_enabled(method)) {
1486 if (buffer_len(&b) > 0)
1487 buffer_append(&b, ",", 1);
1488 buffer_append(&b, method->name, strlen(method->name));
1491 buffer_append(&b, "\0", 1);
1492 list = xstrdup(buffer_ptr(&b));