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(get_canonical_hostname(1));
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;
152 kex->options.gss_deleg_creds=options.gss_deleg_creds;
156 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
158 session_id2 = kex->session_id;
159 session_id2_len = kex->session_id_len;
162 /* send 1st encrypted/maced/compressed message */
163 packet_start(SSH2_MSG_IGNORE);
164 packet_put_cstring("markus");
174 typedef struct Authctxt Authctxt;
175 typedef struct Authmethod Authmethod;
177 typedef int sign_cb_fn(
178 Authctxt *authctxt, Key *key,
179 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
182 const char *server_user;
183 const char *local_user;
191 sign_cb_fn *last_key_sign;
193 AuthenticationConnection *agent;
195 Sensitive *sensitive;
196 /* kbd-interactive */
202 char *name; /* string to compare against server's list */
203 int (*userauth)(Authctxt *authctxt);
204 int *enabled; /* flag in option struct that enables method */
205 int *batch_flag; /* flag in option struct that disables method */
208 void input_userauth_success(int, u_int32_t, void *);
209 void input_userauth_failure(int, u_int32_t, void *);
210 void input_userauth_banner(int, u_int32_t, void *);
211 void input_userauth_error(int, u_int32_t, void *);
212 void input_userauth_info_req(int, u_int32_t, void *);
213 void input_userauth_pk_ok(int, u_int32_t, void *);
214 void input_userauth_passwd_changereq(int, u_int32_t, void *);
216 int userauth_none(Authctxt *);
217 int userauth_pubkey(Authctxt *);
218 int userauth_passwd(Authctxt *);
219 int userauth_kbdint(Authctxt *);
220 int userauth_hostbased(Authctxt *);
223 int userauth_external(Authctxt *authctxt);
224 int userauth_gssapi(Authctxt *authctxt);
225 void input_gssapi_response(int type, u_int32_t, void *);
226 void input_gssapi_token(int type, u_int32_t, void *);
227 void input_gssapi_hash(int type, u_int32_t, void *);
228 void input_gssapi_error(int, u_int32_t, void *);
229 void input_gssapi_errtok(int, u_int32_t, void *);
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 packet_start(SSH2_MSG_SERVICE_REQUEST);
286 packet_put_cstring("ssh-userauth");
288 debug("SSH2_MSG_SERVICE_REQUEST sent");
290 type = packet_read();
291 if (type != SSH2_MSG_SERVICE_ACCEPT)
292 fatal("Server denied authentication request: %d", type);
293 if (packet_remaining() > 0) {
294 char *reply = packet_get_string(NULL);
295 debug2("service_accept: %s", reply);
298 debug2("buggy server: service_accept w/o service");
301 debug("SSH2_MSG_SERVICE_ACCEPT received");
303 if (options.preferred_authentications == NULL)
304 options.preferred_authentications = authmethods_get();
306 /* setup authentication context */
307 memset(&authctxt, 0, sizeof(authctxt));
308 authctxt.agent = ssh_get_authentication_connection();
309 authctxt.server_user = server_user;
310 authctxt.local_user = local_user;
311 authctxt.host = host;
312 authctxt.service = "ssh-connection"; /* service name */
313 authctxt.success = 0;
314 authctxt.method = authmethod_lookup("none");
315 authctxt.authlist = NULL;
316 authctxt.methoddata = NULL;
317 authctxt.sensitive = sensitive;
318 authctxt.info_req_seen = 0;
319 if (authctxt.method == NULL)
320 fatal("ssh_userauth2: internal error: cannot send userauth none request");
322 /* initial userauth request */
323 userauth_none(&authctxt);
325 dispatch_init(&input_userauth_error);
326 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
327 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
328 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
329 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
331 if (authctxt.agent != NULL)
332 ssh_close_authentication_connection(authctxt.agent);
334 debug("Authentication succeeded (%s).", authctxt.method->name);
337 userauth(Authctxt *authctxt, char *authlist)
339 if (authctxt->methoddata!=NULL) {
340 xfree(authctxt->methoddata);
341 authctxt->methoddata=NULL;
344 if (authlist == NULL) {
345 authlist = authctxt->authlist;
347 if (authctxt->authlist)
348 xfree(authctxt->authlist);
349 authctxt->authlist = authlist;
352 Authmethod *method = authmethod_get(authlist);
354 fatal("Permission denied (%s).", authlist);
355 authctxt->method = method;
356 if (method->userauth(authctxt) != 0) {
357 debug2("we sent a %s packet, wait for reply", method->name);
360 debug2("we did not send a packet, disable method");
361 method->enabled = NULL;
367 input_userauth_error(int type, u_int32_t seq, void *ctxt)
369 fatal("input_userauth_error: bad message during authentication: "
374 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
377 debug3("input_userauth_banner");
378 msg = packet_get_string(NULL);
379 lang = packet_get_string(NULL);
380 fprintf(stderr, "%s", msg);
386 input_userauth_success(int type, u_int32_t seq, void *ctxt)
388 Authctxt *authctxt = ctxt;
389 if (authctxt == NULL)
390 fatal("input_userauth_success: no authentication context");
391 if (authctxt->authlist)
392 xfree(authctxt->authlist);
393 if (authctxt->methoddata)
394 xfree(authctxt->methoddata);
395 clear_auth_state(authctxt);
396 authctxt->success = 1; /* break out */
400 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
402 Authctxt *authctxt = ctxt;
403 char *authlist = NULL;
406 if (authctxt == NULL)
407 fatal("input_userauth_failure: no authentication context");
409 authlist = packet_get_string(NULL);
410 partial = packet_get_char();
414 log("Authenticated with partial success.");
415 debug("Authentications that can continue: %s", authlist);
417 clear_auth_state(authctxt);
418 userauth(authctxt, authlist);
421 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
423 Authctxt *authctxt = ctxt;
426 int pktype, sent = 0;
431 if (authctxt == NULL)
432 fatal("input_userauth_pk_ok: no authentication context");
433 if (datafellows & SSH_BUG_PKOK) {
434 /* this is similar to SSH_BUG_PKAUTH */
435 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
436 pkblob = packet_get_string(&blen);
438 buffer_append(&b, pkblob, blen);
439 pkalg = buffer_get_string(&b, &alen);
442 pkalg = packet_get_string(&alen);
443 pkblob = packet_get_string(&blen);
447 debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
448 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
451 if (authctxt->last_key == NULL ||
452 authctxt->last_key_sign == NULL) {
453 debug("no last key or no sign cb");
456 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
457 debug("unknown pkalg %s", pkalg);
460 if ((key = key_from_blob(pkblob, blen)) == NULL) {
461 debug("no key from blob. pkalg %s", pkalg);
464 if (key->type != pktype) {
465 error("input_userauth_pk_ok: type mismatch "
466 "for decoded key (received %d, expected %d)",
470 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
471 debug2("input_userauth_pk_ok: fp %s", fp);
473 if (!key_equal(key, authctxt->last_key)) {
474 debug("key != last_key");
477 sent = sign_and_send_pubkey(authctxt, key,
478 authctxt->last_key_sign);
487 clear_auth_state(authctxt);
488 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
490 /* try another method if we did not send a packet */
492 userauth(authctxt, NULL);
498 userauth_gssapi(Authctxt *authctxt)
500 Gssctxt *gssctxt = NULL;
501 static gss_OID_set supported = NULL;
506 /* Things work better if we send one mechanism at a time, rather
507 * than them all at once. This means that if we fail at some point
508 * in the middle of a negotiation, we can come back and try something
511 if (datafellows & SSH_OLD_GSSAPI) return 0;
513 /* Before we offer a mechanism, check that we can support it. Don't
514 * bother trying to get credentials - as the standard fallback will
515 * deal with that kind of failure.
518 if (supported==NULL) gss_indicate_mechs(&min, &supported);
520 while (mech<supported->count && !ok) {
521 if (gssctxt) ssh_gssapi_delete_ctx(&gssctxt);
522 ssh_gssapi_build_ctx(&gssctxt);
523 ssh_gssapi_set_oid(gssctxt,&supported->elements[mech]);
525 /* The DER encoding below only works for lengths<128,
528 if (supported->elements[mech].length<128 &&
529 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
531 ok = 1; /* Mechanism works */
539 authctxt->methoddata=(void *)gssctxt;
541 packet_start(SSH2_MSG_USERAUTH_REQUEST);
542 packet_put_cstring(authctxt->server_user);
543 packet_put_cstring(authctxt->service);
544 packet_put_cstring(authctxt->method->name);
548 /* The newest gsskeyex draft stipulates that OIDs should
549 * be DER encoded, so we need to add the object type and
550 * length information back on */
551 if (datafellows & SSH_BUG_GSSAPI_BER) {
552 packet_put_string(supported->elements[mech].elements,
553 supported->elements[mech].length);
555 packet_put_int((supported->elements[mech].length)+2);
556 packet_put_char(0x06);
557 packet_put_char(supported->elements[mech].length);
558 packet_put_raw(supported->elements[mech].elements,
559 supported->elements[mech].length);
565 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
566 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
567 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,&input_gssapi_error);
568 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,&input_gssapi_errtok);
570 mech++; /* Move along to next candidate */
576 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
578 Authctxt *authctxt = ctxt;
583 gss_buffer_desc send_tok;
585 if (authctxt == NULL)
586 fatal("input_gssapi_response: no authentication context");
587 gssctxt = authctxt->methoddata;
590 oidv=packet_get_string(&oidlen);
592 if (datafellows & SSH_BUG_GSSAPI_BER) {
593 if (!ssh_gssapi_check_oid(gssctxt,oidv,oidlen)) {
594 fatal("Server returned different OID than expected");
596 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
598 if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
599 debug("Badly encoded mechanism OID received");
600 clear_auth_state(authctxt);
601 userauth(authctxt,NULL);
604 if (!ssh_gssapi_check_oid(gssctxt,oidv+2,oidlen-2)) {
605 fatal("Server returned different OID than expected");
607 ssh_gssapi_set_oid_data(gssctxt,oidv+2,oidlen-2);
612 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
613 GSS_C_NO_BUFFER, &send_tok,
615 if (GSS_ERROR(status)) {
616 if (send_tok.length>0) {
617 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
618 packet_put_string(send_tok.value,send_tok.length);
622 /* Start again with next method on list */
623 debug("Trying to start again");
624 clear_auth_state(authctxt);
625 userauth(authctxt,NULL);
629 /* We must have data to send */
630 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
631 packet_put_string(send_tok.value,send_tok.length);
634 gss_release_buffer(&ms, &send_tok);
638 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
640 Authctxt *authctxt = ctxt;
642 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(&slen);
651 recv_tok.length=slen; /* safe typecast */
653 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
654 &recv_tok, &send_tok, NULL);
658 if (GSS_ERROR(status)) {
659 if (send_tok.length>0) {
660 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
661 packet_put_string(send_tok.value,send_tok.length);
665 /* Start again with the next method in the list */
666 clear_auth_state(authctxt);
667 userauth(authctxt,NULL);
671 if (send_tok.length>0) {
672 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
673 packet_put_string(send_tok.value,send_tok.length);
678 if (status == GSS_S_COMPLETE) {
679 /* If that succeeded, send a exchange complete message */
680 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
687 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
689 Authctxt *authctxt = ctxt;
691 gss_buffer_desc send_tok,recv_tok;
694 if (authctxt == NULL)
695 fatal("input_gssapi_response: no authentication context");
696 gssctxt = authctxt->methoddata;
698 recv_tok.value=packet_get_string(&recv_tok.length);
700 /* Stick it into GSSAPI and see what it says */
701 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
702 &recv_tok, &send_tok, NULL);
706 /* We can't send a packet to the server */
708 /* The draft says that we should wait for the server to fail
709 * before starting the next authentication. So, we clear the
710 * state, but don't do anything else */
711 clear_auth_state(authctxt);
716 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
722 maj=packet_get_int();
723 min=packet_get_int();
724 msg=packet_get_string(NULL);
725 lang=packet_get_string(NULL);
729 fprintf(stderr, "Server GSSAPI Error:\n%s\n", msg);
735 userauth_external(Authctxt *authctxt)
737 static int attempt =0;
742 debug2("userauth_external");
743 packet_start(SSH2_MSG_USERAUTH_REQUEST);
744 packet_put_cstring(authctxt->server_user);
745 packet_put_cstring(authctxt->service);
746 packet_put_cstring(authctxt->method->name);
754 userauth_none(Authctxt *authctxt)
756 /* initial userauth request */
757 packet_start(SSH2_MSG_USERAUTH_REQUEST);
758 packet_put_cstring(authctxt->server_user);
759 packet_put_cstring(authctxt->service);
760 packet_put_cstring(authctxt->method->name);
767 userauth_passwd(Authctxt *authctxt)
769 static int attempt = 0;
773 if (attempt++ >= options.number_of_password_prompts)
777 error("Permission denied, please try again.");
779 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
780 authctxt->server_user, authctxt->host);
781 password = read_passphrase(prompt, 0);
782 packet_start(SSH2_MSG_USERAUTH_REQUEST);
783 packet_put_cstring(authctxt->server_user);
784 packet_put_cstring(authctxt->service);
785 packet_put_cstring(authctxt->method->name);
787 packet_put_cstring(password);
788 memset(password, 0, strlen(password));
790 packet_add_padding(64);
793 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
794 &input_userauth_passwd_changereq);
799 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
802 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
804 Authctxt *authctxt = ctxt;
805 char *info, *lang, *password = NULL, *retype = NULL;
808 debug2("input_userauth_passwd_changereq");
810 if (authctxt == NULL)
811 fatal("input_userauth_passwd_changereq: "
812 "no authentication context");
814 info = packet_get_string(NULL);
815 lang = packet_get_string(NULL);
816 if (strlen(info) > 0)
820 packet_start(SSH2_MSG_USERAUTH_REQUEST);
821 packet_put_cstring(authctxt->server_user);
822 packet_put_cstring(authctxt->service);
823 packet_put_cstring(authctxt->method->name);
824 packet_put_char(1); /* additional info */
825 snprintf(prompt, sizeof(prompt),
826 "Enter %.30s@%.128s's old password: ",
827 authctxt->server_user, authctxt->host);
828 password = read_passphrase(prompt, 0);
829 packet_put_cstring(password);
830 memset(password, 0, strlen(password));
833 while (password == NULL) {
834 snprintf(prompt, sizeof(prompt),
835 "Enter %.30s@%.128s's new password: ",
836 authctxt->server_user, authctxt->host);
837 password = read_passphrase(prompt, RP_ALLOW_EOF);
838 if (password == NULL) {
842 snprintf(prompt, sizeof(prompt),
843 "Retype %.30s@%.128s's new password: ",
844 authctxt->server_user, authctxt->host);
845 retype = read_passphrase(prompt, 0);
846 if (strcmp(password, retype) != 0) {
847 memset(password, 0, strlen(password));
849 log("Mismatch; try again, EOF to quit.");
852 memset(retype, 0, strlen(retype));
855 packet_put_cstring(password);
856 memset(password, 0, strlen(password));
858 packet_add_padding(64);
861 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
862 &input_userauth_passwd_changereq);
866 clear_auth_state(Authctxt *authctxt)
868 /* XXX clear authentication state */
869 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
871 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
872 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
873 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
876 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
877 debug3("clear_auth_state: key_free %p", authctxt->last_key);
878 key_free(authctxt->last_key);
880 authctxt->last_key = NULL;
881 authctxt->last_key_hint = -2;
882 authctxt->last_key_sign = NULL;
886 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
889 u_char *blob, *signature;
895 debug3("sign_and_send_pubkey");
897 if (key_to_blob(k, &blob, &bloblen) == 0) {
898 /* we cannot handle this key */
899 debug3("sign_and_send_pubkey: cannot handle key");
902 /* data to be signed */
904 if (datafellows & SSH_OLD_SESSIONID) {
905 buffer_append(&b, session_id2, session_id2_len);
906 skip = session_id2_len;
908 buffer_put_string(&b, session_id2, session_id2_len);
909 skip = buffer_len(&b);
911 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
912 buffer_put_cstring(&b, authctxt->server_user);
913 buffer_put_cstring(&b,
914 datafellows & SSH_BUG_PKSERVICE ?
917 if (datafellows & SSH_BUG_PKAUTH) {
918 buffer_put_char(&b, have_sig);
920 buffer_put_cstring(&b, authctxt->method->name);
921 buffer_put_char(&b, have_sig);
922 buffer_put_cstring(&b, key_ssh_name(k));
924 buffer_put_string(&b, blob, bloblen);
926 /* generate signature */
927 ret = (*sign_callback)(authctxt, k, &signature, &slen,
928 buffer_ptr(&b), buffer_len(&b));
937 if (datafellows & SSH_BUG_PKSERVICE) {
939 buffer_append(&b, session_id2, session_id2_len);
940 skip = session_id2_len;
941 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
942 buffer_put_cstring(&b, authctxt->server_user);
943 buffer_put_cstring(&b, authctxt->service);
944 buffer_put_cstring(&b, authctxt->method->name);
945 buffer_put_char(&b, have_sig);
946 if (!(datafellows & SSH_BUG_PKAUTH))
947 buffer_put_cstring(&b, key_ssh_name(k));
948 buffer_put_string(&b, blob, bloblen);
952 /* append signature */
953 buffer_put_string(&b, signature, slen);
956 /* skip session id and packet type */
957 if (buffer_len(&b) < skip + 1)
958 fatal("userauth_pubkey: internal error");
959 buffer_consume(&b, skip + 1);
961 /* put remaining data from buffer into packet */
962 packet_start(SSH2_MSG_USERAUTH_REQUEST);
963 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
971 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
975 u_int bloblen, have_sig = 0;
977 debug3("send_pubkey_test");
979 if (key_to_blob(k, &blob, &bloblen) == 0) {
980 /* we cannot handle this key */
981 debug3("send_pubkey_test: cannot handle key");
984 /* register callback for USERAUTH_PK_OK message */
985 authctxt->last_key_sign = sign_callback;
986 authctxt->last_key_hint = hint;
987 authctxt->last_key = k;
988 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
990 packet_start(SSH2_MSG_USERAUTH_REQUEST);
991 packet_put_cstring(authctxt->server_user);
992 packet_put_cstring(authctxt->service);
993 packet_put_cstring(authctxt->method->name);
994 packet_put_char(have_sig);
995 if (!(datafellows & SSH_BUG_PKAUTH))
996 packet_put_cstring(key_ssh_name(k));
997 packet_put_string(blob, bloblen);
1004 load_identity_file(char *filename)
1007 char prompt[300], *passphrase;
1011 if (stat(filename, &st) < 0) {
1012 debug3("no such identity: %s", filename);
1015 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1016 if (private == NULL) {
1017 if (options.batch_mode)
1019 snprintf(prompt, sizeof prompt,
1020 "Enter passphrase for key '%.100s': ", filename);
1021 for (i = 0; i < options.number_of_password_prompts; i++) {
1022 passphrase = read_passphrase(prompt, 0);
1023 if (strcmp(passphrase, "") != 0) {
1024 private = key_load_private_type(KEY_UNSPEC, filename,
1028 debug2("no passphrase given, try next key");
1031 memset(passphrase, 0, strlen(passphrase));
1033 if (private != NULL || quit)
1035 debug2("bad passphrase given, try again...");
1042 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1043 u_char *data, u_int datalen)
1048 idx = authctxt->last_key_hint;
1052 /* private key is stored in external hardware */
1053 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1054 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
1056 private = load_identity_file(options.identity_files[idx]);
1057 if (private == NULL)
1059 ret = key_sign(private, sigp, lenp, data, datalen);
1065 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1066 u_char *data, u_int datalen)
1068 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1072 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1073 u_char *data, u_int datalen)
1075 return key_sign(key, sigp, lenp, data, datalen);
1079 userauth_pubkey_agent(Authctxt *authctxt)
1081 static int called = 0;
1087 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1088 debug2("userauth_pubkey_agent: no keys at all");
1091 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1093 debug2("userauth_pubkey_agent: no more keys");
1095 debug("Offering agent key: %s", comment);
1097 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1102 debug2("userauth_pubkey_agent: no message sent");
1107 userauth_pubkey(Authctxt *authctxt)
1114 if (authctxt->agent != NULL) {
1116 sent = userauth_pubkey_agent(authctxt);
1117 } while (!sent && authctxt->agent->howmany > 0);
1119 while (!sent && idx < options.num_identity_files) {
1120 key = options.identity_keys[idx];
1121 filename = options.identity_files[idx];
1123 debug("Trying private key: %s", filename);
1124 key = load_identity_file(filename);
1126 sent = sign_and_send_pubkey(authctxt, key,
1130 } else if (key->type != KEY_RSA1) {
1131 debug("Offering public key: %s", filename);
1132 sent = send_pubkey_test(authctxt, key,
1133 identity_sign_cb, idx);
1141 * Send userauth request message specifying keyboard-interactive method.
1144 userauth_kbdint(Authctxt *authctxt)
1146 static int attempt = 0;
1148 if (attempt++ >= options.number_of_password_prompts)
1150 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1151 if (attempt > 1 && !authctxt->info_req_seen) {
1152 debug3("userauth_kbdint: disable: no info_req_seen");
1153 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1157 debug2("userauth_kbdint");
1158 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1159 packet_put_cstring(authctxt->server_user);
1160 packet_put_cstring(authctxt->service);
1161 packet_put_cstring(authctxt->method->name);
1162 packet_put_cstring(""); /* lang */
1163 packet_put_cstring(options.kbd_interactive_devices ?
1164 options.kbd_interactive_devices : "");
1167 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1172 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1175 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1177 Authctxt *authctxt = ctxt;
1178 char *name, *inst, *lang, *prompt, *response;
1179 u_int num_prompts, i;
1182 debug2("input_userauth_info_req");
1184 if (authctxt == NULL)
1185 fatal("input_userauth_info_req: no authentication context");
1187 authctxt->info_req_seen = 1;
1189 name = packet_get_string(NULL);
1190 inst = packet_get_string(NULL);
1191 lang = packet_get_string(NULL);
1192 if (strlen(name) > 0)
1194 if (strlen(inst) > 0)
1200 num_prompts = packet_get_int();
1202 * Begin to build info response packet based on prompts requested.
1203 * We commit to providing the correct number of responses, so if
1204 * further on we run into a problem that prevents this, we have to
1205 * be sure and clean this up and send a correct error response.
1207 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1208 packet_put_int(num_prompts);
1210 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1211 for (i = 0; i < num_prompts; i++) {
1212 prompt = packet_get_string(NULL);
1213 echo = packet_get_char();
1215 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1217 packet_put_cstring(response);
1218 memset(response, 0, strlen(response));
1222 packet_check_eom(); /* done with parsing incoming message. */
1224 packet_add_padding(64);
1229 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1230 u_char *data, u_int datalen)
1235 int to[2], from[2], status, version = 2;
1237 debug2("ssh_keysign called");
1239 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1240 error("ssh_keysign: no installed: %s", strerror(errno));
1243 if (fflush(stdout) != 0)
1244 error("ssh_keysign: fflush: %s", strerror(errno));
1246 error("ssh_keysign: pipe: %s", strerror(errno));
1249 if (pipe(from) < 0) {
1250 error("ssh_keysign: pipe: %s", strerror(errno));
1253 if ((pid = fork()) < 0) {
1254 error("ssh_keysign: fork: %s", strerror(errno));
1261 if (dup2(from[1], STDOUT_FILENO) < 0)
1262 fatal("ssh_keysign: dup2: %s", strerror(errno));
1264 if (dup2(to[0], STDIN_FILENO) < 0)
1265 fatal("ssh_keysign: dup2: %s", strerror(errno));
1268 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1269 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1276 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1277 buffer_put_string(&b, data, datalen);
1278 ssh_msg_send(to[1], version, &b);
1280 if (ssh_msg_recv(from[0], &b) < 0) {
1281 error("ssh_keysign: no reply");
1288 while (waitpid(pid, &status, 0) < 0)
1292 if (buffer_get_char(&b) != version) {
1293 error("ssh_keysign: bad version");
1297 *sigp = buffer_get_string(&b, lenp);
1304 userauth_hostbased(Authctxt *authctxt)
1306 Key *private = NULL;
1307 Sensitive *sensitive = authctxt->sensitive;
1309 u_char *signature, *blob;
1310 char *chost, *pkalg, *p;
1311 const char *service;
1313 int ok, i, len, found = 0;
1315 /* check for a useful key */
1316 for (i = 0; i < sensitive->nkeys; i++) {
1317 private = sensitive->keys[i];
1318 if (private && private->type != KEY_RSA1) {
1320 /* we take and free the key */
1321 sensitive->keys[i] = NULL;
1326 debug("No more client hostkeys for hostbased authentication.");
1329 if (key_to_blob(private, &blob, &blen) == 0) {
1333 /* figure out a name for the client host */
1334 p = get_local_name(packet_get_connection_in());
1336 error("userauth_hostbased: cannot get local ipaddr/name");
1340 len = strlen(p) + 2;
1341 chost = xmalloc(len);
1342 strlcpy(chost, p, len);
1343 strlcat(chost, ".", len);
1344 debug2("userauth_hostbased: chost %s", chost);
1347 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1349 pkalg = xstrdup(key_ssh_name(private));
1351 /* construct data */
1352 buffer_put_string(&b, session_id2, session_id2_len);
1353 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1354 buffer_put_cstring(&b, authctxt->server_user);
1355 buffer_put_cstring(&b, service);
1356 buffer_put_cstring(&b, authctxt->method->name);
1357 buffer_put_cstring(&b, pkalg);
1358 buffer_put_string(&b, blob, blen);
1359 buffer_put_cstring(&b, chost);
1360 buffer_put_cstring(&b, authctxt->local_user);
1364 if (sensitive->external_keysign)
1365 ok = ssh_keysign(private, &signature, &slen,
1366 buffer_ptr(&b), buffer_len(&b));
1368 ok = key_sign(private, &signature, &slen,
1369 buffer_ptr(&b), buffer_len(&b));
1373 error("key_sign failed");
1378 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1379 packet_put_cstring(authctxt->server_user);
1380 packet_put_cstring(authctxt->service);
1381 packet_put_cstring(authctxt->method->name);
1382 packet_put_cstring(pkalg);
1383 packet_put_string(blob, blen);
1384 packet_put_cstring(chost);
1385 packet_put_cstring(authctxt->local_user);
1386 packet_put_string(signature, slen);
1387 memset(signature, 's', slen);
1396 /* find auth method */
1399 * given auth method name, if configurable options permit this method fill
1400 * in auth_ident field and return true, otherwise return false.
1403 authmethod_is_enabled(Authmethod *method)
1407 /* return false if options indicate this method is disabled */
1408 if (method->enabled == NULL || *method->enabled == 0)
1410 /* return false if batch mode is enabled but method needs interactive mode */
1411 if (method->batch_flag != NULL && *method->batch_flag != 0)
1417 authmethod_lookup(const char *name)
1419 Authmethod *method = NULL;
1421 for (method = authmethods; method->name != NULL; method++)
1422 if (strcmp(name, method->name) == 0)
1424 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1428 /* XXX internal state */
1429 static Authmethod *current = NULL;
1430 static char *supported = NULL;
1431 static char *preferred = NULL;
1434 * Given the authentication method list sent by the server, return the
1435 * next method we should try. If the server initially sends a nil list,
1436 * use a built-in default list.
1439 authmethod_get(char *authlist)
1444 /* Use a suitable default if we're passed a nil list. */
1445 if (authlist == NULL || strlen(authlist) == 0)
1446 authlist = options.preferred_authentications;
1448 if (supported == NULL || strcmp(authlist, supported) != 0) {
1449 debug3("start over, passed a different list %s", authlist);
1450 if (supported != NULL)
1452 supported = xstrdup(authlist);
1453 preferred = options.preferred_authentications;
1454 debug3("preferred %s", preferred);
1456 } else if (current != NULL && authmethod_is_enabled(current))
1460 if ((name = match_list(preferred, supported, &next)) == NULL) {
1461 debug("No more authentication methods to try.");
1466 debug3("authmethod_lookup %s", name);
1467 debug3("remaining preferred: %s", preferred);
1468 if ((current = authmethod_lookup(name)) != NULL &&
1469 authmethod_is_enabled(current)) {
1470 debug3("authmethod_is_enabled %s", name);
1471 debug("Next authentication method: %s", name);
1478 authmethods_get(void)
1480 Authmethod *method = NULL;
1485 for (method = authmethods; method->name != NULL; method++) {
1486 if (authmethod_is_enabled(method)) {
1487 if (buffer_len(&b) > 0)
1488 buffer_append(&b, ",", 1);
1489 buffer_append(&b, method->name, strlen(method->name));
1492 buffer_append(&b, "\0", 1);
1493 list = xstrdup(buffer_ptr(&b));