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 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_client_mechanisms(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);
148 /* start key exchange */
149 kex = kex_setup(myproposal);
150 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
151 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
153 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
155 kex->client_version_string=client_version_string;
156 kex->server_version_string=server_version_string;
157 kex->verify_host_key=&verify_host_key_callback;
160 kex->options.gss_deleg_creds=options.gss_deleg_creds;
164 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
166 session_id2 = kex->session_id;
167 session_id2_len = kex->session_id_len;
170 /* send 1st encrypted/maced/compressed message */
171 packet_start(SSH2_MSG_IGNORE);
172 packet_put_cstring("markus");
182 typedef struct Authctxt Authctxt;
183 typedef struct Authmethod Authmethod;
185 typedef int sign_cb_fn(
186 Authctxt *authctxt, Key *key,
187 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
190 const char *server_user;
191 const char *local_user;
199 sign_cb_fn *last_key_sign;
201 AuthenticationConnection *agent;
203 Sensitive *sensitive;
204 /* kbd-interactive */
210 char *name; /* string to compare against server's list */
211 int (*userauth)(Authctxt *authctxt);
212 int *enabled; /* flag in option struct that enables method */
213 int *batch_flag; /* flag in option struct that disables method */
216 void input_userauth_success(int, u_int32_t, void *);
217 void input_userauth_failure(int, u_int32_t, void *);
218 void input_userauth_banner(int, u_int32_t, void *);
219 void input_userauth_error(int, u_int32_t, void *);
220 void input_userauth_info_req(int, u_int32_t, void *);
221 void input_userauth_pk_ok(int, u_int32_t, void *);
222 void input_userauth_passwd_changereq(int, u_int32_t, void *);
224 int userauth_none(Authctxt *);
225 int userauth_pubkey(Authctxt *);
226 int userauth_passwd(Authctxt *);
227 int userauth_kbdint(Authctxt *);
228 int userauth_hostbased(Authctxt *);
231 int userauth_external(Authctxt *authctxt);
232 int userauth_gssapi(Authctxt *authctxt);
233 void input_gssapi_response(int type, u_int32_t, void *);
234 void input_gssapi_token(int type, u_int32_t, void *);
235 void input_gssapi_hash(int type, u_int32_t, void *);
236 void input_gssapi_error(int, u_int32_t, void *);
237 void input_gssapi_errtok(int, u_int32_t, void *);
240 void userauth(Authctxt *, char *);
242 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
243 static void clear_auth_state(Authctxt *);
245 static Authmethod *authmethod_get(char *authlist);
246 static Authmethod *authmethod_lookup(const char *name);
247 static char *authmethods_get(void);
249 Authmethod authmethods[] = {
253 &options.gss_authentication,
257 &options.gss_authentication,
262 &options.hostbased_authentication,
266 &options.pubkey_authentication,
268 {"keyboard-interactive",
270 &options.kbd_interactive_authentication,
271 &options.batch_mode},
274 &options.password_authentication,
275 &options.batch_mode},
280 {NULL, NULL, NULL, NULL}
284 ssh_userauth2(const char *local_user, const char *server_user, char *host,
285 Sensitive *sensitive)
290 if (options.challenge_response_authentication)
291 options.kbd_interactive_authentication = 1;
293 packet_start(SSH2_MSG_SERVICE_REQUEST);
294 packet_put_cstring("ssh-userauth");
296 debug("SSH2_MSG_SERVICE_REQUEST sent");
298 type = packet_read();
299 if (type != SSH2_MSG_SERVICE_ACCEPT)
300 fatal("Server denied authentication request: %d", type);
301 if (packet_remaining() > 0) {
302 char *reply = packet_get_string(NULL);
303 debug2("service_accept: %s", reply);
306 debug2("buggy server: service_accept w/o service");
309 debug("SSH2_MSG_SERVICE_ACCEPT received");
311 if (options.preferred_authentications == NULL)
312 options.preferred_authentications = authmethods_get();
314 /* setup authentication context */
315 memset(&authctxt, 0, sizeof(authctxt));
316 authctxt.agent = ssh_get_authentication_connection();
317 authctxt.server_user = server_user;
318 authctxt.local_user = local_user;
319 authctxt.host = host;
320 authctxt.service = "ssh-connection"; /* service name */
321 authctxt.success = 0;
322 authctxt.method = authmethod_lookup("none");
323 authctxt.authlist = NULL;
324 authctxt.methoddata = NULL;
325 authctxt.sensitive = sensitive;
326 authctxt.info_req_seen = 0;
327 if (authctxt.method == NULL)
328 fatal("ssh_userauth2: internal error: cannot send userauth none request");
330 /* initial userauth request */
331 userauth_none(&authctxt);
333 dispatch_init(&input_userauth_error);
334 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
335 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
336 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
337 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
339 if (authctxt.agent != NULL)
340 ssh_close_authentication_connection(authctxt.agent);
342 debug("Authentication succeeded (%s).", authctxt.method->name);
345 userauth(Authctxt *authctxt, char *authlist)
347 if (authctxt->methoddata!=NULL) {
348 xfree(authctxt->methoddata);
349 authctxt->methoddata=NULL;
352 if (authlist == NULL) {
353 authlist = authctxt->authlist;
355 if (authctxt->authlist)
356 xfree(authctxt->authlist);
357 authctxt->authlist = authlist;
360 Authmethod *method = authmethod_get(authlist);
362 fatal("Permission denied (%s).", authlist);
363 authctxt->method = method;
364 if (method->userauth(authctxt) != 0) {
365 debug2("we sent a %s packet, wait for reply", method->name);
368 debug2("we did not send a packet, disable method");
369 method->enabled = NULL;
375 input_userauth_error(int type, u_int32_t seq, void *ctxt)
377 fatal("input_userauth_error: bad message during authentication: "
382 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
385 debug3("input_userauth_banner");
386 msg = packet_get_string(NULL);
387 lang = packet_get_string(NULL);
388 fprintf(stderr, "%s", msg);
394 input_userauth_success(int type, u_int32_t seq, void *ctxt)
396 Authctxt *authctxt = ctxt;
397 if (authctxt == NULL)
398 fatal("input_userauth_success: no authentication context");
399 if (authctxt->authlist)
400 xfree(authctxt->authlist);
401 if (authctxt->methoddata)
402 xfree(authctxt->methoddata);
403 clear_auth_state(authctxt);
404 authctxt->success = 1; /* break out */
408 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
410 Authctxt *authctxt = ctxt;
411 char *authlist = NULL;
414 if (authctxt == NULL)
415 fatal("input_userauth_failure: no authentication context");
417 authlist = packet_get_string(NULL);
418 partial = packet_get_char();
422 log("Authenticated with partial success.");
423 debug("Authentications that can continue: %s", authlist);
425 clear_auth_state(authctxt);
426 userauth(authctxt, authlist);
429 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
431 Authctxt *authctxt = ctxt;
434 int pktype, sent = 0;
439 if (authctxt == NULL)
440 fatal("input_userauth_pk_ok: no authentication context");
441 if (datafellows & SSH_BUG_PKOK) {
442 /* this is similar to SSH_BUG_PKAUTH */
443 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
444 pkblob = packet_get_string(&blen);
446 buffer_append(&b, pkblob, blen);
447 pkalg = buffer_get_string(&b, &alen);
450 pkalg = packet_get_string(&alen);
451 pkblob = packet_get_string(&blen);
455 debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
456 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
459 if (authctxt->last_key == NULL ||
460 authctxt->last_key_sign == NULL) {
461 debug("no last key or no sign cb");
464 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
465 debug("unknown pkalg %s", pkalg);
468 if ((key = key_from_blob(pkblob, blen)) == NULL) {
469 debug("no key from blob. pkalg %s", pkalg);
472 if (key->type != pktype) {
473 error("input_userauth_pk_ok: type mismatch "
474 "for decoded key (received %d, expected %d)",
478 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
479 debug2("input_userauth_pk_ok: fp %s", fp);
481 if (!key_equal(key, authctxt->last_key)) {
482 debug("key != last_key");
485 sent = sign_and_send_pubkey(authctxt, key,
486 authctxt->last_key_sign);
495 clear_auth_state(authctxt);
496 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
498 /* try another method if we did not send a packet */
500 userauth(authctxt, NULL);
506 userauth_gssapi(Authctxt *authctxt)
508 Gssctxt *gssctxt = NULL;
509 static gss_OID_set supported = NULL;
514 /* Things work better if we send one mechanism at a time, rather
515 * than them all at once. This means that if we fail at some point
516 * in the middle of a negotiation, we can come back and try something
519 if (datafellows & SSH_OLD_GSSAPI) return 0;
521 /* Before we offer a mechanism, check that we can support it. Don't
522 * bother trying to get credentials - as the standard fallback will
523 * deal with that kind of failure.
526 if (supported==NULL) gss_indicate_mechs(&min, &supported);
528 while (mech<supported->count && !ok) {
529 if (gssctxt) ssh_gssapi_delete_ctx(&gssctxt);
530 ssh_gssapi_build_ctx(&gssctxt);
531 ssh_gssapi_set_oid(gssctxt,&supported->elements[mech]);
533 /* The DER encoding below only works for lengths<128,
536 if (supported->elements[mech].length<128 &&
537 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
539 ok = 1; /* Mechanism works */
547 authctxt->methoddata=(void *)gssctxt;
549 packet_start(SSH2_MSG_USERAUTH_REQUEST);
550 packet_put_cstring(authctxt->server_user);
551 packet_put_cstring(authctxt->service);
552 packet_put_cstring(authctxt->method->name);
556 /* The newest gsskeyex draft stipulates that OIDs should
557 * be DER encoded, so we need to add the object type and
558 * length information back on */
559 if (datafellows & SSH_BUG_GSSAPI_BER) {
560 packet_put_string(supported->elements[mech].elements,
561 supported->elements[mech].length);
563 packet_put_int((supported->elements[mech].length)+2);
564 packet_put_char(0x06);
565 packet_put_char(supported->elements[mech].length);
566 packet_put_raw(supported->elements[mech].elements,
567 supported->elements[mech].length);
573 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
574 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
575 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,&input_gssapi_error);
576 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,&input_gssapi_errtok);
578 mech++; /* Move along to next candidate */
584 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
586 Authctxt *authctxt = ctxt;
591 gss_buffer_desc send_tok;
593 if (authctxt == NULL)
594 fatal("input_gssapi_response: no authentication context");
595 gssctxt = authctxt->methoddata;
598 oidv=packet_get_string(&oidlen);
600 if (datafellows & SSH_BUG_GSSAPI_BER) {
601 if (!ssh_gssapi_check_oid(gssctxt,oidv,oidlen)) {
602 fatal("Server returned different OID than expected");
604 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
606 if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
607 debug("Badly encoded mechanism OID received");
608 clear_auth_state(authctxt);
609 userauth(authctxt,NULL);
612 if (!ssh_gssapi_check_oid(gssctxt,oidv+2,oidlen-2)) {
613 fatal("Server returned different OID than expected");
615 ssh_gssapi_set_oid_data(gssctxt,oidv+2,oidlen-2);
620 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
621 GSS_C_NO_BUFFER, &send_tok,
623 if (GSS_ERROR(status)) {
624 if (send_tok.length>0) {
625 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
626 packet_put_string(send_tok.value,send_tok.length);
630 /* Start again with next method on list */
631 debug("Trying to start again");
632 clear_auth_state(authctxt);
633 userauth(authctxt,NULL);
637 /* We must have data to send */
638 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
639 packet_put_string(send_tok.value,send_tok.length);
642 gss_release_buffer(&ms, &send_tok);
646 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
648 Authctxt *authctxt = ctxt;
650 gss_buffer_desc send_tok,recv_tok;
654 if (authctxt == NULL)
655 fatal("input_gssapi_token: no authentication context");
656 gssctxt = authctxt->methoddata;
658 recv_tok.value=packet_get_string(&slen);
659 recv_tok.length=slen; /* safe typecast */
661 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
662 &recv_tok, &send_tok, NULL);
666 if (GSS_ERROR(status)) {
667 if (send_tok.length>0) {
668 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
669 packet_put_string(send_tok.value,send_tok.length);
673 /* Start again with the next method in the list */
674 clear_auth_state(authctxt);
675 userauth(authctxt,NULL);
679 if (send_tok.length>0) {
680 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
681 packet_put_string(send_tok.value,send_tok.length);
686 if (status == GSS_S_COMPLETE) {
687 /* If that succeeded, send a exchange complete message */
688 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
695 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
697 Authctxt *authctxt = ctxt;
699 gss_buffer_desc send_tok,recv_tok;
702 if (authctxt == NULL)
703 fatal("input_gssapi_response: no authentication context");
704 gssctxt = authctxt->methoddata;
706 recv_tok.value=packet_get_string(&recv_tok.length);
708 /* Stick it into GSSAPI and see what it says */
709 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
710 &recv_tok, &send_tok, NULL);
714 /* We can't send a packet to the server */
716 /* The draft says that we should wait for the server to fail
717 * before starting the next authentication. So, we clear the
718 * state, but don't do anything else */
719 clear_auth_state(authctxt);
724 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
730 maj=packet_get_int();
731 min=packet_get_int();
732 msg=packet_get_string(NULL);
733 lang=packet_get_string(NULL);
737 fprintf(stderr, "Server GSSAPI Error:\n%s\n", msg);
743 userauth_external(Authctxt *authctxt)
745 static int attempt =0;
750 debug2("userauth_external");
751 packet_start(SSH2_MSG_USERAUTH_REQUEST);
753 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
754 packet_put_cstring("");
757 packet_put_cstring(authctxt->server_user);
761 packet_put_cstring(authctxt->service);
762 packet_put_cstring(authctxt->method->name);
770 userauth_none(Authctxt *authctxt)
772 /* initial userauth request */
773 packet_start(SSH2_MSG_USERAUTH_REQUEST);
774 packet_put_cstring(authctxt->server_user);
775 packet_put_cstring(authctxt->service);
776 packet_put_cstring(authctxt->method->name);
783 userauth_passwd(Authctxt *authctxt)
785 static int attempt = 0;
789 if (attempt++ >= options.number_of_password_prompts)
793 error("Permission denied, please try again.");
795 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
796 authctxt->server_user, authctxt->host);
797 password = read_passphrase(prompt, 0);
798 packet_start(SSH2_MSG_USERAUTH_REQUEST);
799 packet_put_cstring(authctxt->server_user);
800 packet_put_cstring(authctxt->service);
801 packet_put_cstring(authctxt->method->name);
803 packet_put_cstring(password);
804 memset(password, 0, strlen(password));
806 packet_add_padding(64);
809 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
810 &input_userauth_passwd_changereq);
815 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
818 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
820 Authctxt *authctxt = ctxt;
821 char *info, *lang, *password = NULL, *retype = NULL;
824 debug2("input_userauth_passwd_changereq");
826 if (authctxt == NULL)
827 fatal("input_userauth_passwd_changereq: "
828 "no authentication context");
830 info = packet_get_string(NULL);
831 lang = packet_get_string(NULL);
832 if (strlen(info) > 0)
836 packet_start(SSH2_MSG_USERAUTH_REQUEST);
837 packet_put_cstring(authctxt->server_user);
838 packet_put_cstring(authctxt->service);
839 packet_put_cstring(authctxt->method->name);
840 packet_put_char(1); /* additional info */
841 snprintf(prompt, sizeof(prompt),
842 "Enter %.30s@%.128s's old password: ",
843 authctxt->server_user, authctxt->host);
844 password = read_passphrase(prompt, 0);
845 packet_put_cstring(password);
846 memset(password, 0, strlen(password));
849 while (password == NULL) {
850 snprintf(prompt, sizeof(prompt),
851 "Enter %.30s@%.128s's new password: ",
852 authctxt->server_user, authctxt->host);
853 password = read_passphrase(prompt, RP_ALLOW_EOF);
854 if (password == NULL) {
858 snprintf(prompt, sizeof(prompt),
859 "Retype %.30s@%.128s's new password: ",
860 authctxt->server_user, authctxt->host);
861 retype = read_passphrase(prompt, 0);
862 if (strcmp(password, retype) != 0) {
863 memset(password, 0, strlen(password));
865 log("Mismatch; try again, EOF to quit.");
868 memset(retype, 0, strlen(retype));
871 packet_put_cstring(password);
872 memset(password, 0, strlen(password));
874 packet_add_padding(64);
877 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
878 &input_userauth_passwd_changereq);
882 clear_auth_state(Authctxt *authctxt)
884 /* XXX clear authentication state */
885 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
887 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
888 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
889 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
892 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
893 debug3("clear_auth_state: key_free %p", authctxt->last_key);
894 key_free(authctxt->last_key);
896 authctxt->last_key = NULL;
897 authctxt->last_key_hint = -2;
898 authctxt->last_key_sign = NULL;
902 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
905 u_char *blob, *signature;
911 debug3("sign_and_send_pubkey");
913 if (key_to_blob(k, &blob, &bloblen) == 0) {
914 /* we cannot handle this key */
915 debug3("sign_and_send_pubkey: cannot handle key");
918 /* data to be signed */
920 if (datafellows & SSH_OLD_SESSIONID) {
921 buffer_append(&b, session_id2, session_id2_len);
922 skip = session_id2_len;
924 buffer_put_string(&b, session_id2, session_id2_len);
925 skip = buffer_len(&b);
927 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
928 buffer_put_cstring(&b, authctxt->server_user);
929 buffer_put_cstring(&b,
930 datafellows & SSH_BUG_PKSERVICE ?
933 if (datafellows & SSH_BUG_PKAUTH) {
934 buffer_put_char(&b, have_sig);
936 buffer_put_cstring(&b, authctxt->method->name);
937 buffer_put_char(&b, have_sig);
938 buffer_put_cstring(&b, key_ssh_name(k));
940 buffer_put_string(&b, blob, bloblen);
942 /* generate signature */
943 ret = (*sign_callback)(authctxt, k, &signature, &slen,
944 buffer_ptr(&b), buffer_len(&b));
953 if (datafellows & SSH_BUG_PKSERVICE) {
955 buffer_append(&b, session_id2, session_id2_len);
956 skip = session_id2_len;
957 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
958 buffer_put_cstring(&b, authctxt->server_user);
959 buffer_put_cstring(&b, authctxt->service);
960 buffer_put_cstring(&b, authctxt->method->name);
961 buffer_put_char(&b, have_sig);
962 if (!(datafellows & SSH_BUG_PKAUTH))
963 buffer_put_cstring(&b, key_ssh_name(k));
964 buffer_put_string(&b, blob, bloblen);
968 /* append signature */
969 buffer_put_string(&b, signature, slen);
972 /* skip session id and packet type */
973 if (buffer_len(&b) < skip + 1)
974 fatal("userauth_pubkey: internal error");
975 buffer_consume(&b, skip + 1);
977 /* put remaining data from buffer into packet */
978 packet_start(SSH2_MSG_USERAUTH_REQUEST);
979 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
987 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
991 u_int bloblen, have_sig = 0;
993 debug3("send_pubkey_test");
995 if (key_to_blob(k, &blob, &bloblen) == 0) {
996 /* we cannot handle this key */
997 debug3("send_pubkey_test: cannot handle key");
1000 /* register callback for USERAUTH_PK_OK message */
1001 authctxt->last_key_sign = sign_callback;
1002 authctxt->last_key_hint = hint;
1003 authctxt->last_key = k;
1004 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1006 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1007 packet_put_cstring(authctxt->server_user);
1008 packet_put_cstring(authctxt->service);
1009 packet_put_cstring(authctxt->method->name);
1010 packet_put_char(have_sig);
1011 if (!(datafellows & SSH_BUG_PKAUTH))
1012 packet_put_cstring(key_ssh_name(k));
1013 packet_put_string(blob, bloblen);
1020 load_identity_file(char *filename)
1023 char prompt[300], *passphrase;
1027 if (stat(filename, &st) < 0) {
1028 debug3("no such identity: %s", filename);
1031 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1032 if (private == NULL) {
1033 if (options.batch_mode)
1035 snprintf(prompt, sizeof prompt,
1036 "Enter passphrase for key '%.100s': ", filename);
1037 for (i = 0; i < options.number_of_password_prompts; i++) {
1038 passphrase = read_passphrase(prompt, 0);
1039 if (strcmp(passphrase, "") != 0) {
1040 private = key_load_private_type(KEY_UNSPEC, filename,
1044 debug2("no passphrase given, try next key");
1047 memset(passphrase, 0, strlen(passphrase));
1049 if (private != NULL || quit)
1051 debug2("bad passphrase given, try again...");
1058 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1059 u_char *data, u_int datalen)
1064 idx = authctxt->last_key_hint;
1068 /* private key is stored in external hardware */
1069 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1070 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
1072 private = load_identity_file(options.identity_files[idx]);
1073 if (private == NULL)
1075 ret = key_sign(private, sigp, lenp, data, datalen);
1081 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1082 u_char *data, u_int datalen)
1084 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1088 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1089 u_char *data, u_int datalen)
1091 return key_sign(key, sigp, lenp, data, datalen);
1095 userauth_pubkey_agent(Authctxt *authctxt)
1097 static int called = 0;
1103 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1104 debug2("userauth_pubkey_agent: no keys at all");
1107 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1109 debug2("userauth_pubkey_agent: no more keys");
1111 debug("Offering agent key: %s", comment);
1113 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1118 debug2("userauth_pubkey_agent: no message sent");
1123 userauth_pubkey(Authctxt *authctxt)
1130 if (authctxt->agent != NULL) {
1132 sent = userauth_pubkey_agent(authctxt);
1133 } while (!sent && authctxt->agent->howmany > 0);
1135 while (!sent && idx < options.num_identity_files) {
1136 key = options.identity_keys[idx];
1137 filename = options.identity_files[idx];
1139 debug("Trying private key: %s", filename);
1140 key = load_identity_file(filename);
1142 sent = sign_and_send_pubkey(authctxt, key,
1146 } else if (key->type != KEY_RSA1) {
1147 debug("Offering public key: %s", filename);
1148 sent = send_pubkey_test(authctxt, key,
1149 identity_sign_cb, idx);
1157 * Send userauth request message specifying keyboard-interactive method.
1160 userauth_kbdint(Authctxt *authctxt)
1162 static int attempt = 0;
1164 if (attempt++ >= options.number_of_password_prompts)
1166 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1167 if (attempt > 1 && !authctxt->info_req_seen) {
1168 debug3("userauth_kbdint: disable: no info_req_seen");
1169 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1173 debug2("userauth_kbdint");
1174 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1175 packet_put_cstring(authctxt->server_user);
1176 packet_put_cstring(authctxt->service);
1177 packet_put_cstring(authctxt->method->name);
1178 packet_put_cstring(""); /* lang */
1179 packet_put_cstring(options.kbd_interactive_devices ?
1180 options.kbd_interactive_devices : "");
1183 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1188 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1191 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1193 Authctxt *authctxt = ctxt;
1194 char *name, *inst, *lang, *prompt, *response;
1195 u_int num_prompts, i;
1198 debug2("input_userauth_info_req");
1200 if (authctxt == NULL)
1201 fatal("input_userauth_info_req: no authentication context");
1203 authctxt->info_req_seen = 1;
1205 name = packet_get_string(NULL);
1206 inst = packet_get_string(NULL);
1207 lang = packet_get_string(NULL);
1208 if (strlen(name) > 0)
1210 if (strlen(inst) > 0)
1216 num_prompts = packet_get_int();
1218 * Begin to build info response packet based on prompts requested.
1219 * We commit to providing the correct number of responses, so if
1220 * further on we run into a problem that prevents this, we have to
1221 * be sure and clean this up and send a correct error response.
1223 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1224 packet_put_int(num_prompts);
1226 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1227 for (i = 0; i < num_prompts; i++) {
1228 prompt = packet_get_string(NULL);
1229 echo = packet_get_char();
1231 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1233 packet_put_cstring(response);
1234 memset(response, 0, strlen(response));
1238 packet_check_eom(); /* done with parsing incoming message. */
1240 packet_add_padding(64);
1245 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1246 u_char *data, u_int datalen)
1251 int to[2], from[2], status, version = 2;
1253 debug2("ssh_keysign called");
1255 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1256 error("ssh_keysign: no installed: %s", strerror(errno));
1259 if (fflush(stdout) != 0)
1260 error("ssh_keysign: fflush: %s", strerror(errno));
1262 error("ssh_keysign: pipe: %s", strerror(errno));
1265 if (pipe(from) < 0) {
1266 error("ssh_keysign: pipe: %s", strerror(errno));
1269 if ((pid = fork()) < 0) {
1270 error("ssh_keysign: fork: %s", strerror(errno));
1277 if (dup2(from[1], STDOUT_FILENO) < 0)
1278 fatal("ssh_keysign: dup2: %s", strerror(errno));
1280 if (dup2(to[0], STDIN_FILENO) < 0)
1281 fatal("ssh_keysign: dup2: %s", strerror(errno));
1284 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1285 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1292 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1293 buffer_put_string(&b, data, datalen);
1294 ssh_msg_send(to[1], version, &b);
1296 if (ssh_msg_recv(from[0], &b) < 0) {
1297 error("ssh_keysign: no reply");
1304 while (waitpid(pid, &status, 0) < 0)
1308 if (buffer_get_char(&b) != version) {
1309 error("ssh_keysign: bad version");
1313 *sigp = buffer_get_string(&b, lenp);
1320 userauth_hostbased(Authctxt *authctxt)
1322 Key *private = NULL;
1323 Sensitive *sensitive = authctxt->sensitive;
1325 u_char *signature, *blob;
1326 char *chost, *pkalg, *p;
1327 const char *service;
1329 int ok, i, len, found = 0;
1331 /* check for a useful key */
1332 for (i = 0; i < sensitive->nkeys; i++) {
1333 private = sensitive->keys[i];
1334 if (private && private->type != KEY_RSA1) {
1336 /* we take and free the key */
1337 sensitive->keys[i] = NULL;
1342 debug("No more client hostkeys for hostbased authentication.");
1345 if (key_to_blob(private, &blob, &blen) == 0) {
1349 /* figure out a name for the client host */
1350 p = get_local_name(packet_get_connection_in());
1352 error("userauth_hostbased: cannot get local ipaddr/name");
1356 len = strlen(p) + 2;
1357 chost = xmalloc(len);
1358 strlcpy(chost, p, len);
1359 strlcat(chost, ".", len);
1360 debug2("userauth_hostbased: chost %s", chost);
1363 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1365 pkalg = xstrdup(key_ssh_name(private));
1367 /* construct data */
1368 buffer_put_string(&b, session_id2, session_id2_len);
1369 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1370 buffer_put_cstring(&b, authctxt->server_user);
1371 buffer_put_cstring(&b, service);
1372 buffer_put_cstring(&b, authctxt->method->name);
1373 buffer_put_cstring(&b, pkalg);
1374 buffer_put_string(&b, blob, blen);
1375 buffer_put_cstring(&b, chost);
1376 buffer_put_cstring(&b, authctxt->local_user);
1380 if (sensitive->external_keysign)
1381 ok = ssh_keysign(private, &signature, &slen,
1382 buffer_ptr(&b), buffer_len(&b));
1384 ok = key_sign(private, &signature, &slen,
1385 buffer_ptr(&b), buffer_len(&b));
1389 error("key_sign failed");
1394 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1395 packet_put_cstring(authctxt->server_user);
1396 packet_put_cstring(authctxt->service);
1397 packet_put_cstring(authctxt->method->name);
1398 packet_put_cstring(pkalg);
1399 packet_put_string(blob, blen);
1400 packet_put_cstring(chost);
1401 packet_put_cstring(authctxt->local_user);
1402 packet_put_string(signature, slen);
1403 memset(signature, 's', slen);
1412 /* find auth method */
1415 * given auth method name, if configurable options permit this method fill
1416 * in auth_ident field and return true, otherwise return false.
1419 authmethod_is_enabled(Authmethod *method)
1423 /* return false if options indicate this method is disabled */
1424 if (method->enabled == NULL || *method->enabled == 0)
1426 /* return false if batch mode is enabled but method needs interactive mode */
1427 if (method->batch_flag != NULL && *method->batch_flag != 0)
1433 authmethod_lookup(const char *name)
1435 Authmethod *method = NULL;
1437 for (method = authmethods; method->name != NULL; method++)
1438 if (strcmp(name, method->name) == 0)
1440 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1444 /* XXX internal state */
1445 static Authmethod *current = NULL;
1446 static char *supported = NULL;
1447 static char *preferred = NULL;
1450 * Given the authentication method list sent by the server, return the
1451 * next method we should try. If the server initially sends a nil list,
1452 * use a built-in default list.
1455 authmethod_get(char *authlist)
1460 /* Use a suitable default if we're passed a nil list. */
1461 if (authlist == NULL || strlen(authlist) == 0)
1462 authlist = options.preferred_authentications;
1464 if (supported == NULL || strcmp(authlist, supported) != 0) {
1465 debug3("start over, passed a different list %s", authlist);
1466 if (supported != NULL)
1468 supported = xstrdup(authlist);
1469 preferred = options.preferred_authentications;
1470 debug3("preferred %s", preferred);
1472 } else if (current != NULL && authmethod_is_enabled(current))
1476 if ((name = match_list(preferred, supported, &next)) == NULL) {
1477 debug("No more authentication methods to try.");
1482 debug3("authmethod_lookup %s", name);
1483 debug3("remaining preferred: %s", preferred);
1484 if ((current = authmethod_lookup(name)) != NULL &&
1485 authmethod_is_enabled(current)) {
1486 debug3("authmethod_is_enabled %s", name);
1487 debug("Next authentication method: %s", name);
1494 authmethods_get(void)
1496 Authmethod *method = NULL;
1501 for (method = authmethods; method->name != NULL; method++) {
1502 if (authmethod_is_enabled(method)) {
1503 if (buffer_len(&b) > 0)
1504 buffer_append(&b, ",", 1);
1505 buffer_append(&b, method->name, strlen(method->name));
1508 buffer_append(&b, "\0", 1);
1509 list = xstrdup(buffer_ptr(&b));