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.97 2002/02/25 16:33:27 markus Exp $");
37 #include "myproposal.h"
38 #include "sshconnect.h"
54 extern char *client_version_string;
55 extern char *server_version_string;
56 extern Options options;
62 u_char *session_id2 = NULL;
63 int session_id2_len = 0;
66 struct sockaddr *xxx_hostaddr;
71 verify_host_key_callback(Key *hostkey)
73 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
74 fatal("Host key verification failed.");
79 ssh_kex2(char *host, struct sockaddr *hostaddr)
88 xxx_hostaddr = hostaddr;
91 /* Add the GSSAPI mechanisms currently supported on this client to
92 * the key exchange algorithm proposal */
93 orig = myproposal[PROPOSAL_KEX_ALGS];
94 gss = ssh_gssapi_mechanisms(0,host);
96 len = strlen(orig)+strlen(gss)+2;
97 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
98 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
102 if (options.ciphers == (char *)-1) {
103 log("No valid ciphers for protocol version 2 given, using defaults.");
104 options.ciphers = NULL;
106 if (options.ciphers != NULL) {
107 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
108 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
110 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
111 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
112 myproposal[PROPOSAL_ENC_ALGS_STOC] =
113 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
114 if (options.compression) {
115 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
116 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
118 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
119 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
121 if (options.macs != NULL) {
122 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
123 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
125 if (options.hostkeyalgorithms != NULL)
126 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
127 options.hostkeyalgorithms;
130 /* If we've got GSSAPI algorithms, then we also support the
131 * 'null' hostkey, as a last resort */
133 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
134 len = strlen(orig)+sizeof(",null");
135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
136 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
140 /* start key exchange */
141 kex = kex_setup(myproposal);
142 kex->client_version_string=client_version_string;
143 kex->server_version_string=server_version_string;
144 kex->verify_host_key=&verify_host_key_callback;
148 kex->options.gss_deleg_creds=options.gss_deleg_creds;
153 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
155 session_id2 = kex->session_id;
156 session_id2_len = kex->session_id_len;
159 /* send 1st encrypted/maced/compressed message */
160 packet_start(SSH2_MSG_IGNORE);
161 packet_put_cstring("markus");
165 debug("done: ssh_kex2.");
172 typedef struct Authctxt Authctxt;
173 typedef struct Authmethod Authmethod;
175 typedef int sign_cb_fn(
176 Authctxt *authctxt, Key *key,
177 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
180 const char *server_user;
181 const char *local_user;
189 sign_cb_fn *last_key_sign;
191 AuthenticationConnection *agent;
195 /* kbd-interactive */
201 char *name; /* string to compare against server's list */
202 int (*userauth)(Authctxt *authctxt);
203 int *enabled; /* flag in option struct that enables method */
204 int *batch_flag; /* flag in option struct that disables method */
207 void input_userauth_success(int, u_int32_t, void *);
208 void input_userauth_failure(int, u_int32_t, void *);
209 void input_userauth_banner(int, u_int32_t, void *);
210 void input_userauth_error(int, u_int32_t, void *);
211 void input_userauth_info_req(int, u_int32_t, void *);
212 void input_userauth_pk_ok(int, u_int32_t, void *);
214 int userauth_none(Authctxt *);
215 int userauth_pubkey(Authctxt *);
216 int userauth_passwd(Authctxt *);
217 int userauth_kbdint(Authctxt *);
218 int userauth_hostbased(Authctxt *);
221 int userauth_external(Authctxt *authctxt);
222 int userauth_gssapi(Authctxt *authctxt);
223 void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
224 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
225 void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
228 void userauth(Authctxt *, char *);
230 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
231 static void clear_auth_state(Authctxt *);
233 static Authmethod *authmethod_get(char *authlist);
234 static Authmethod *authmethod_lookup(const char *name);
235 static char *authmethods_get(void);
237 Authmethod authmethods[] = {
241 &options.gss_authentication,
245 &options.gss_authentication,
250 &options.hostbased_authentication,
254 &options.pubkey_authentication,
256 {"keyboard-interactive",
258 &options.kbd_interactive_authentication,
259 &options.batch_mode},
262 &options.password_authentication,
263 &options.batch_mode},
268 {NULL, NULL, NULL, NULL}
272 ssh_userauth2(const char *local_user, const char *server_user, char *host,
273 Key **keys, int nkeys)
278 if (options.challenge_response_authentication)
279 options.kbd_interactive_authentication = 1;
281 debug("send SSH2_MSG_SERVICE_REQUEST");
282 packet_start(SSH2_MSG_SERVICE_REQUEST);
283 packet_put_cstring("ssh-userauth");
286 type = packet_read();
287 if (type != SSH2_MSG_SERVICE_ACCEPT) {
288 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
290 if (packet_remaining() > 0) {
291 char *reply = packet_get_string(NULL);
292 debug("service_accept: %s", reply);
295 debug("buggy server: service_accept w/o service");
298 debug("got SSH2_MSG_SERVICE_ACCEPT");
300 if (options.preferred_authentications == NULL)
301 options.preferred_authentications = authmethods_get();
303 /* setup authentication context */
304 memset(&authctxt, 0, sizeof(authctxt));
305 authctxt.agent = ssh_get_authentication_connection();
306 authctxt.server_user = server_user;
307 authctxt.local_user = local_user;
308 authctxt.host = host;
309 authctxt.service = "ssh-connection"; /* service name */
310 authctxt.success = 0;
311 authctxt.method = authmethod_lookup("none");
312 authctxt.authlist = NULL;
313 authctxt.methoddata = NULL;
314 authctxt.keys = keys;
315 authctxt.nkeys = nkeys;
316 authctxt.info_req_seen = 0;
317 if (authctxt.method == NULL)
318 fatal("ssh_userauth2: internal error: cannot send userauth none request");
320 /* initial userauth request */
321 userauth_none(&authctxt);
323 dispatch_init(&input_userauth_error);
324 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
325 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
326 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
327 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
329 if (authctxt.agent != NULL)
330 ssh_close_authentication_connection(authctxt.agent);
332 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
335 userauth(Authctxt *authctxt, char *authlist)
337 if (authctxt->methoddata!=NULL) {
338 xfree(authctxt->methoddata);
339 authctxt->methoddata=NULL;
342 if (authlist == NULL) {
343 authlist = authctxt->authlist;
345 if (authctxt->authlist)
346 xfree(authctxt->authlist);
347 authctxt->authlist = authlist;
350 Authmethod *method = authmethod_get(authlist);
352 fatal("Permission denied (%s).", authlist);
353 authctxt->method = method;
354 if (method->userauth(authctxt) != 0) {
355 debug2("we sent a %s packet, wait for reply", method->name);
358 debug2("we did not send a packet, disable method");
359 method->enabled = NULL;
364 input_userauth_error(int type, u_int32_t seq, void *ctxt)
366 fatal("input_userauth_error: bad message during authentication: "
370 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
373 debug3("input_userauth_banner");
374 msg = packet_get_string(NULL);
375 lang = packet_get_string(NULL);
376 fprintf(stderr, "%s", msg);
381 input_userauth_success(int type, u_int32_t seq, void *ctxt)
383 Authctxt *authctxt = ctxt;
384 if (authctxt == NULL)
385 fatal("input_userauth_success: no authentication context");
386 if (authctxt->authlist)
387 xfree(authctxt->authlist);
388 if (authctxt->methoddata)
389 xfree(authctxt->methoddata);
390 clear_auth_state(authctxt);
391 authctxt->success = 1; /* break out */
394 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
396 Authctxt *authctxt = ctxt;
397 char *authlist = NULL;
400 if (authctxt == NULL)
401 fatal("input_userauth_failure: no authentication context");
403 authlist = packet_get_string(NULL);
404 partial = packet_get_char();
408 log("Authenticated with partial success.");
409 debug("authentications that can continue: %s", authlist);
411 clear_auth_state(authctxt);
412 userauth(authctxt, authlist);
415 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
417 Authctxt *authctxt = ctxt;
420 int pktype, sent = 0;
425 if (authctxt == NULL)
426 fatal("input_userauth_pk_ok: no authentication context");
427 if (datafellows & SSH_BUG_PKOK) {
428 /* this is similar to SSH_BUG_PKAUTH */
429 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
430 pkblob = packet_get_string(&blen);
432 buffer_append(&b, pkblob, blen);
433 pkalg = buffer_get_string(&b, &alen);
436 pkalg = packet_get_string(&alen);
437 pkblob = packet_get_string(&blen);
441 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
442 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
445 if (authctxt->last_key == NULL ||
446 authctxt->last_key_sign == NULL) {
447 debug("no last key or no sign cb");
450 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
451 debug("unknown pkalg %s", pkalg);
454 if ((key = key_from_blob(pkblob, blen)) == NULL) {
455 debug("no key from blob. pkalg %s", pkalg);
458 if (key->type != pktype) {
459 error("input_userauth_pk_ok: type mismatch "
460 "for decoded key (received %d, expected %d)",
464 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
465 debug2("input_userauth_pk_ok: fp %s", fp);
467 if (!key_equal(key, authctxt->last_key)) {
468 debug("key != last_key");
471 sent = sign_and_send_pubkey(authctxt, key,
472 authctxt->last_key_sign);
481 clear_auth_state(authctxt);
482 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
484 /* try another method if we did not send a packet*/
486 userauth(authctxt, NULL);
492 userauth_gssapi(Authctxt *authctxt)
498 /* For now, we only make one attempt at this. We could try offering
499 * the server different GSSAPI OIDs until we get bored, I suppose.
501 if (tries++>0) return 0;
503 if (datafellows & SSH_OLD_GSSAPI) return 0;
505 gssctxt=xmalloc(sizeof(Gssctxt));
507 /* Initialise as much of our context as we can, so failures can be
508 * trapped before sending any packets.
510 ssh_gssapi_build_ctx(gssctxt);
511 if (ssh_gssapi_import_name(gssctxt,authctxt->host)) {
514 authctxt->methoddata=(void *)gssctxt;
516 packet_start(SSH2_MSG_USERAUTH_REQUEST);
517 packet_put_cstring(authctxt->server_user);
518 packet_put_cstring(authctxt->service);
519 packet_put_cstring(authctxt->method->name);
521 /* FIXME: This assumes that our current GSSAPI implementation
522 * supports all of the mechanisms listed in supported_mechs.
523 * This may not be the case - we should use something along
524 * the lines of the code in gss_genr to remove the ones that
525 * aren't supported */
526 packet_put_int(GSS_LAST_ENTRY);
527 for (i=0;i<GSS_LAST_ENTRY;i++) {
528 packet_put_string(supported_mechs[i].oid.elements,
529 supported_mechs[i].oid.length);
534 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
535 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
541 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
543 Authctxt *authctxt = ctxt;
548 gss_buffer_desc send_tok;
550 if (authctxt == NULL)
551 fatal("input_gssapi_response: no authentication context");
552 gssctxt = authctxt->methoddata;
555 oidv=packet_get_string(&oidlen);
556 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
560 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
561 GSS_C_NO_BUFFER, &send_tok,
563 if (GSS_ERROR(status)) {
564 /* Start again with next method on list */
565 debug("Trying to start again");
566 userauth(authctxt,NULL);
570 /* We must have data to send */
571 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
572 packet_put_string(send_tok.value,send_tok.length);
575 gss_release_buffer(&ms, &send_tok);
579 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
581 Authctxt *authctxt = ctxt;
583 gss_buffer_desc send_tok,recv_tok;
586 if (authctxt == NULL)
587 fatal("input_gssapi_response: no authentication context");
588 gssctxt = authctxt->methoddata;
590 recv_tok.value=packet_get_string(&recv_tok.length);
592 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
593 &recv_tok, &send_tok, NULL);
597 if (GSS_ERROR(status)) {
598 /* Start again with the next method in the list */
599 userauth(authctxt,NULL);
603 if (send_tok.length>0) {
604 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
605 packet_put_string(send_tok.value,send_tok.length);
610 if (status == GSS_S_COMPLETE) {
611 /* If that succeeded, send a exchange complete message */
612 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
619 userauth_external(Authctxt *authctxt)
621 static int attempt =0;
626 debug2("userauth_external");
627 packet_start(SSH2_MSG_USERAUTH_REQUEST);
628 packet_put_cstring(authctxt->server_user);
629 packet_put_cstring(authctxt->service);
630 packet_put_cstring(authctxt->method->name);
638 userauth_none(Authctxt *authctxt)
640 /* initial userauth request */
641 packet_start(SSH2_MSG_USERAUTH_REQUEST);
642 packet_put_cstring(authctxt->server_user);
643 packet_put_cstring(authctxt->service);
644 packet_put_cstring(authctxt->method->name);
651 userauth_passwd(Authctxt *authctxt)
653 static int attempt = 0;
657 if (attempt++ >= options.number_of_password_prompts)
661 error("Permission denied, please try again.");
663 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
664 authctxt->server_user, authctxt->host);
665 password = read_passphrase(prompt, 0);
666 packet_start(SSH2_MSG_USERAUTH_REQUEST);
667 packet_put_cstring(authctxt->server_user);
668 packet_put_cstring(authctxt->service);
669 packet_put_cstring(authctxt->method->name);
671 packet_put_cstring(password);
672 memset(password, 0, strlen(password));
674 packet_add_padding(64);
680 clear_auth_state(Authctxt *authctxt)
682 /* XXX clear authentication state */
683 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
684 debug3("clear_auth_state: key_free %p", authctxt->last_key);
685 key_free(authctxt->last_key);
687 authctxt->last_key = NULL;
688 authctxt->last_key_hint = -2;
689 authctxt->last_key_sign = NULL;
693 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
696 u_char *blob, *signature;
702 debug3("sign_and_send_pubkey");
704 if (key_to_blob(k, &blob, &bloblen) == 0) {
705 /* we cannot handle this key */
706 debug3("sign_and_send_pubkey: cannot handle key");
709 /* data to be signed */
711 if (datafellows & SSH_OLD_SESSIONID) {
712 buffer_append(&b, session_id2, session_id2_len);
713 skip = session_id2_len;
715 buffer_put_string(&b, session_id2, session_id2_len);
716 skip = buffer_len(&b);
718 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
719 buffer_put_cstring(&b, authctxt->server_user);
720 buffer_put_cstring(&b,
721 datafellows & SSH_BUG_PKSERVICE ?
724 if (datafellows & SSH_BUG_PKAUTH) {
725 buffer_put_char(&b, have_sig);
727 buffer_put_cstring(&b, authctxt->method->name);
728 buffer_put_char(&b, have_sig);
729 buffer_put_cstring(&b, key_ssh_name(k));
731 buffer_put_string(&b, blob, bloblen);
733 /* generate signature */
734 ret = (*sign_callback)(authctxt, k, &signature, &slen,
735 buffer_ptr(&b), buffer_len(&b));
744 if (datafellows & SSH_BUG_PKSERVICE) {
746 buffer_append(&b, session_id2, session_id2_len);
747 skip = session_id2_len;
748 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
749 buffer_put_cstring(&b, authctxt->server_user);
750 buffer_put_cstring(&b, authctxt->service);
751 buffer_put_cstring(&b, authctxt->method->name);
752 buffer_put_char(&b, have_sig);
753 if (!(datafellows & SSH_BUG_PKAUTH))
754 buffer_put_cstring(&b, key_ssh_name(k));
755 buffer_put_string(&b, blob, bloblen);
759 /* append signature */
760 buffer_put_string(&b, signature, slen);
763 /* skip session id and packet type */
764 if (buffer_len(&b) < skip + 1)
765 fatal("userauth_pubkey: internal error");
766 buffer_consume(&b, skip + 1);
768 /* put remaining data from buffer into packet */
769 packet_start(SSH2_MSG_USERAUTH_REQUEST);
770 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
778 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
782 u_int bloblen, have_sig = 0;
784 debug3("send_pubkey_test");
786 if (key_to_blob(k, &blob, &bloblen) == 0) {
787 /* we cannot handle this key */
788 debug3("send_pubkey_test: cannot handle key");
791 /* register callback for USERAUTH_PK_OK message */
792 authctxt->last_key_sign = sign_callback;
793 authctxt->last_key_hint = hint;
794 authctxt->last_key = k;
795 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
797 packet_start(SSH2_MSG_USERAUTH_REQUEST);
798 packet_put_cstring(authctxt->server_user);
799 packet_put_cstring(authctxt->service);
800 packet_put_cstring(authctxt->method->name);
801 packet_put_char(have_sig);
802 if (!(datafellows & SSH_BUG_PKAUTH))
803 packet_put_cstring(key_ssh_name(k));
804 packet_put_string(blob, bloblen);
811 load_identity_file(char *filename)
814 char prompt[300], *passphrase;
818 if (stat(filename, &st) < 0) {
819 debug3("no such identity: %s", filename);
822 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
823 if (private == NULL) {
824 if (options.batch_mode)
826 snprintf(prompt, sizeof prompt,
827 "Enter passphrase for key '%.100s': ", filename);
828 for (i = 0; i < options.number_of_password_prompts; i++) {
829 passphrase = read_passphrase(prompt, 0);
830 if (strcmp(passphrase, "") != 0) {
831 private = key_load_private_type(KEY_UNSPEC, filename,
835 debug2("no passphrase given, try next key");
838 memset(passphrase, 0, strlen(passphrase));
840 if (private != NULL || quit)
842 debug2("bad passphrase given, try again...");
849 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
850 u_char *data, u_int datalen)
855 idx = authctxt->last_key_hint;
859 /* private key is stored in external hardware */
860 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
861 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
863 private = load_identity_file(options.identity_files[idx]);
866 ret = key_sign(private, sigp, lenp, data, datalen);
872 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
873 u_char *data, u_int datalen)
875 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
879 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
880 u_char *data, u_int datalen)
882 return key_sign(key, sigp, lenp, data, datalen);
886 userauth_pubkey_agent(Authctxt *authctxt)
888 static int called = 0;
894 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
895 debug2("userauth_pubkey_agent: no keys at all");
898 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
900 debug2("userauth_pubkey_agent: no more keys");
902 debug("userauth_pubkey_agent: testing agent key %s", comment);
904 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
909 debug2("userauth_pubkey_agent: no message sent");
914 userauth_pubkey(Authctxt *authctxt)
921 if (authctxt->agent != NULL) {
923 sent = userauth_pubkey_agent(authctxt);
924 } while (!sent && authctxt->agent->howmany > 0);
926 while (!sent && idx < options.num_identity_files) {
927 key = options.identity_keys[idx];
928 filename = options.identity_files[idx];
930 debug("try privkey: %s", filename);
931 key = load_identity_file(filename);
933 sent = sign_and_send_pubkey(authctxt, key,
937 } else if (key->type != KEY_RSA1) {
938 debug("try pubkey: %s", filename);
939 sent = send_pubkey_test(authctxt, key,
940 identity_sign_cb, idx);
948 * Send userauth request message specifying keyboard-interactive method.
951 userauth_kbdint(Authctxt *authctxt)
953 static int attempt = 0;
955 if (attempt++ >= options.number_of_password_prompts)
957 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
958 if (attempt > 1 && !authctxt->info_req_seen) {
959 debug3("userauth_kbdint: disable: no info_req_seen");
960 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
964 debug2("userauth_kbdint");
965 packet_start(SSH2_MSG_USERAUTH_REQUEST);
966 packet_put_cstring(authctxt->server_user);
967 packet_put_cstring(authctxt->service);
968 packet_put_cstring(authctxt->method->name);
969 packet_put_cstring(""); /* lang */
970 packet_put_cstring(options.kbd_interactive_devices ?
971 options.kbd_interactive_devices : "");
974 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
979 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
982 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
984 Authctxt *authctxt = ctxt;
985 char *name, *inst, *lang, *prompt, *response;
986 u_int num_prompts, i;
989 debug2("input_userauth_info_req");
991 if (authctxt == NULL)
992 fatal("input_userauth_info_req: no authentication context");
994 authctxt->info_req_seen = 1;
996 name = packet_get_string(NULL);
997 inst = packet_get_string(NULL);
998 lang = packet_get_string(NULL);
999 if (strlen(name) > 0)
1001 if (strlen(inst) > 0)
1007 num_prompts = packet_get_int();
1009 * Begin to build info response packet based on prompts requested.
1010 * We commit to providing the correct number of responses, so if
1011 * further on we run into a problem that prevents this, we have to
1012 * be sure and clean this up and send a correct error response.
1014 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1015 packet_put_int(num_prompts);
1017 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1018 for (i = 0; i < num_prompts; i++) {
1019 prompt = packet_get_string(NULL);
1020 echo = packet_get_char();
1022 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1024 packet_put_cstring(response);
1025 memset(response, 0, strlen(response));
1029 packet_check_eom(); /* done with parsing incoming message. */
1031 packet_add_padding(64);
1036 * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign
1037 * will be setuid-root and the sbit can be removed from /usr/bin/ssh.
1040 userauth_hostbased(Authctxt *authctxt)
1042 Key *private = NULL;
1044 u_char *signature, *blob;
1045 char *chost, *pkalg, *p;
1046 const char *service;
1048 int ok, i, len, found = 0;
1050 /* check for a useful key */
1051 for (i = 0; i < authctxt->nkeys; i++) {
1052 private = authctxt->keys[i];
1053 if (private && private->type != KEY_RSA1) {
1055 /* we take and free the key */
1056 authctxt->keys[i] = NULL;
1061 debug("userauth_hostbased: no more client hostkeys");
1064 if (key_to_blob(private, &blob, &blen) == 0) {
1068 /* figure out a name for the client host */
1069 p = get_local_name(packet_get_connection_in());
1071 error("userauth_hostbased: cannot get local ipaddr/name");
1075 len = strlen(p) + 2;
1076 chost = xmalloc(len);
1077 strlcpy(chost, p, len);
1078 strlcat(chost, ".", len);
1079 debug2("userauth_hostbased: chost %s", chost);
1081 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1083 pkalg = xstrdup(key_ssh_name(private));
1085 /* construct data */
1086 buffer_put_string(&b, session_id2, session_id2_len);
1087 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1088 buffer_put_cstring(&b, authctxt->server_user);
1089 buffer_put_cstring(&b, service);
1090 buffer_put_cstring(&b, authctxt->method->name);
1091 buffer_put_cstring(&b, pkalg);
1092 buffer_put_string(&b, blob, blen);
1093 buffer_put_cstring(&b, chost);
1094 buffer_put_cstring(&b, authctxt->local_user);
1098 ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
1102 error("key_sign failed");
1107 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1108 packet_put_cstring(authctxt->server_user);
1109 packet_put_cstring(authctxt->service);
1110 packet_put_cstring(authctxt->method->name);
1111 packet_put_cstring(pkalg);
1112 packet_put_string(blob, blen);
1113 packet_put_cstring(chost);
1114 packet_put_cstring(authctxt->local_user);
1115 packet_put_string(signature, slen);
1116 memset(signature, 's', slen);
1125 /* find auth method */
1128 * given auth method name, if configurable options permit this method fill
1129 * in auth_ident field and return true, otherwise return false.
1132 authmethod_is_enabled(Authmethod *method)
1136 /* return false if options indicate this method is disabled */
1137 if (method->enabled == NULL || *method->enabled == 0)
1139 /* return false if batch mode is enabled but method needs interactive mode */
1140 if (method->batch_flag != NULL && *method->batch_flag != 0)
1146 authmethod_lookup(const char *name)
1148 Authmethod *method = NULL;
1150 for (method = authmethods; method->name != NULL; method++)
1151 if (strcmp(name, method->name) == 0)
1153 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1157 /* XXX internal state */
1158 static Authmethod *current = NULL;
1159 static char *supported = NULL;
1160 static char *preferred = NULL;
1162 * Given the authentication method list sent by the server, return the
1163 * next method we should try. If the server initially sends a nil list,
1164 * use a built-in default list.
1167 authmethod_get(char *authlist)
1173 /* Use a suitable default if we're passed a nil list. */
1174 if (authlist == NULL || strlen(authlist) == 0)
1175 authlist = options.preferred_authentications;
1177 if (supported == NULL || strcmp(authlist, supported) != 0) {
1178 debug3("start over, passed a different list %s", authlist);
1179 if (supported != NULL)
1181 supported = xstrdup(authlist);
1182 preferred = options.preferred_authentications;
1183 debug3("preferred %s", preferred);
1185 } else if (current != NULL && authmethod_is_enabled(current))
1189 if ((name = match_list(preferred, supported, &next)) == NULL) {
1190 debug("no more auth methods to try");
1195 debug3("authmethod_lookup %s", name);
1196 debug3("remaining preferred: %s", preferred);
1197 if ((current = authmethod_lookup(name)) != NULL &&
1198 authmethod_is_enabled(current)) {
1199 debug3("authmethod_is_enabled %s", name);
1200 debug("next auth method to try is %s", name);
1207 authmethods_get(void)
1209 Authmethod *method = NULL;
1214 for (method = authmethods; method->name != NULL; method++) {
1215 if (authmethod_is_enabled(method)) {
1216 if (buffer_len(&b) > 0)
1217 buffer_append(&b, ",", 1);
1218 buffer_append(&b, method->name, strlen(method->name));
1221 buffer_append(&b, "\0", 1);
1222 list = xstrdup(buffer_ptr(&b));