2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: sshconnect2.c,v 1.114 2003/04/01 10:22:21 markus Exp $");
37 #include "myproposal.h"
38 #include "sshconnect.h"
49 #include "pathnames.h"
56 extern char *client_version_string;
57 extern char *server_version_string;
58 extern Options options;
64 u_char *session_id2 = NULL;
65 int session_id2_len = 0;
68 struct sockaddr *xxx_hostaddr;
73 verify_host_key_callback(Key *hostkey)
75 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
76 fatal("Host key verification failed.");
81 ssh_kex2(char *host, struct sockaddr *hostaddr)
90 xxx_hostaddr = hostaddr;
93 /* Add the GSSAPI mechanisms currently supported on this client to
94 * the key exchange algorithm proposal */
95 orig = myproposal[PROPOSAL_KEX_ALGS];
96 gss = ssh_gssapi_client_mechanisms(host);
98 len = strlen(orig)+strlen(gss)+2;
99 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
100 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
104 if (options.ciphers == (char *)-1) {
105 log("No valid ciphers for protocol version 2 given, using defaults.");
106 options.ciphers = NULL;
108 if (options.ciphers != NULL) {
109 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
110 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
112 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
113 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
114 myproposal[PROPOSAL_ENC_ALGS_STOC] =
115 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
116 if (options.compression) {
117 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
118 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
120 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
121 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
123 if (options.macs != NULL) {
124 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
125 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
127 if (options.hostkeyalgorithms != NULL)
128 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
129 options.hostkeyalgorithms;
132 /* If we've got GSSAPI algorithms, then we also support the
133 * 'null' hostkey, as a last resort */
135 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
136 len = strlen(orig)+sizeof(",null");
137 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
138 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
141 /* start key exchange */
142 kex = kex_setup(myproposal);
143 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
144 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
146 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
148 kex->client_version_string=client_version_string;
149 kex->server_version_string=server_version_string;
150 kex->verify_host_key=&verify_host_key_callback;
153 kex->options.gss_deleg_creds=options.gss_deleg_creds;
157 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
159 session_id2 = kex->session_id;
160 session_id2_len = kex->session_id_len;
163 /* send 1st encrypted/maced/compressed message */
164 packet_start(SSH2_MSG_IGNORE);
165 packet_put_cstring("markus");
175 typedef struct Authctxt Authctxt;
176 typedef struct Authmethod Authmethod;
178 typedef int sign_cb_fn(
179 Authctxt *authctxt, Key *key,
180 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
183 const char *server_user;
184 const char *local_user;
192 sign_cb_fn *last_key_sign;
194 AuthenticationConnection *agent;
196 Sensitive *sensitive;
197 /* kbd-interactive */
203 char *name; /* string to compare against server's list */
204 int (*userauth)(Authctxt *authctxt);
205 int *enabled; /* flag in option struct that enables method */
206 int *batch_flag; /* flag in option struct that disables method */
209 void input_userauth_success(int, u_int32_t, void *);
210 void input_userauth_failure(int, u_int32_t, void *);
211 void input_userauth_banner(int, u_int32_t, void *);
212 void input_userauth_error(int, u_int32_t, void *);
213 void input_userauth_info_req(int, u_int32_t, void *);
214 void input_userauth_pk_ok(int, u_int32_t, void *);
215 void input_userauth_passwd_changereq(int, u_int32_t, void *);
217 int userauth_none(Authctxt *);
218 int userauth_pubkey(Authctxt *);
219 int userauth_passwd(Authctxt *);
220 int userauth_kbdint(Authctxt *);
221 int userauth_hostbased(Authctxt *);
224 int userauth_external(Authctxt *authctxt);
225 int userauth_gssapi(Authctxt *authctxt);
226 void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
227 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
228 void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
229 void input_gssapi_error(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);
569 mech++; /* Move along to next candidate */
575 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
577 Authctxt *authctxt = ctxt;
582 gss_buffer_desc send_tok;
584 if (authctxt == NULL)
585 fatal("input_gssapi_response: no authentication context");
586 gssctxt = authctxt->methoddata;
589 oidv=packet_get_string(&oidlen);
591 if (datafellows & SSH_BUG_GSSAPI_BER) {
592 if (!ssh_gssapi_check_oid(gssctxt,oidv,oidlen)) {
593 fatal("Server returned different OID than expected");
595 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
597 if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
598 debug("Badly encoded mechanism OID received");
599 clear_auth_state(authctxt);
600 userauth(authctxt,NULL);
603 if (!ssh_gssapi_check_oid(gssctxt,oidv+2,oidlen-2)) {
604 fatal("Server returned different OID than expected");
606 ssh_gssapi_set_oid_data(gssctxt,oidv+2,oidlen-2);
611 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
612 GSS_C_NO_BUFFER, &send_tok,
614 if (GSS_ERROR(status)) {
615 /* Start again with next method on list */
616 debug("Trying to start again");
617 clear_auth_state(authctxt);
618 userauth(authctxt,NULL);
622 /* We must have data to send */
623 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
624 packet_put_string(send_tok.value,send_tok.length);
627 gss_release_buffer(&ms, &send_tok);
631 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
633 Authctxt *authctxt = ctxt;
635 gss_buffer_desc send_tok,recv_tok;
638 if (authctxt == NULL)
639 fatal("input_gssapi_response: no authentication context");
640 gssctxt = authctxt->methoddata;
642 recv_tok.value=packet_get_string(&recv_tok.length);
644 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
645 &recv_tok, &send_tok, NULL);
649 if (GSS_ERROR(status)) {
650 /* Start again with the next method in the list */
651 clear_auth_state(authctxt);
652 userauth(authctxt,NULL);
656 if (send_tok.length>0) {
657 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
658 packet_put_string(send_tok.value,send_tok.length);
663 if (status == GSS_S_COMPLETE) {
664 /* If that succeeded, send a exchange complete message */
665 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
672 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
678 maj=packet_get_int();
679 min=packet_get_int();
680 msg=packet_get_string(NULL);
681 lang=packet_get_string(NULL);
685 fprintf(stderr, "Server GSSAPI Error:\n%s\n", msg);
691 userauth_external(Authctxt *authctxt)
693 static int attempt =0;
698 debug2("userauth_external");
699 packet_start(SSH2_MSG_USERAUTH_REQUEST);
700 packet_put_cstring(authctxt->server_user);
701 packet_put_cstring(authctxt->service);
702 packet_put_cstring(authctxt->method->name);
710 userauth_none(Authctxt *authctxt)
712 /* initial userauth request */
713 packet_start(SSH2_MSG_USERAUTH_REQUEST);
714 packet_put_cstring(authctxt->server_user);
715 packet_put_cstring(authctxt->service);
716 packet_put_cstring(authctxt->method->name);
723 userauth_passwd(Authctxt *authctxt)
725 static int attempt = 0;
729 if (attempt++ >= options.number_of_password_prompts)
733 error("Permission denied, please try again.");
735 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
736 authctxt->server_user, authctxt->host);
737 password = read_passphrase(prompt, 0);
738 packet_start(SSH2_MSG_USERAUTH_REQUEST);
739 packet_put_cstring(authctxt->server_user);
740 packet_put_cstring(authctxt->service);
741 packet_put_cstring(authctxt->method->name);
743 packet_put_cstring(password);
744 memset(password, 0, strlen(password));
746 packet_add_padding(64);
749 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
750 &input_userauth_passwd_changereq);
755 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
758 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
760 Authctxt *authctxt = ctxt;
761 char *info, *lang, *password = NULL, *retype = NULL;
764 debug2("input_userauth_passwd_changereq");
766 if (authctxt == NULL)
767 fatal("input_userauth_passwd_changereq: "
768 "no authentication context");
770 info = packet_get_string(NULL);
771 lang = packet_get_string(NULL);
772 if (strlen(info) > 0)
776 packet_start(SSH2_MSG_USERAUTH_REQUEST);
777 packet_put_cstring(authctxt->server_user);
778 packet_put_cstring(authctxt->service);
779 packet_put_cstring(authctxt->method->name);
780 packet_put_char(1); /* additional info */
781 snprintf(prompt, sizeof(prompt),
782 "Enter %.30s@%.128s's old password: ",
783 authctxt->server_user, authctxt->host);
784 password = read_passphrase(prompt, 0);
785 packet_put_cstring(password);
786 memset(password, 0, strlen(password));
789 while (password == NULL) {
790 snprintf(prompt, sizeof(prompt),
791 "Enter %.30s@%.128s's new password: ",
792 authctxt->server_user, authctxt->host);
793 password = read_passphrase(prompt, RP_ALLOW_EOF);
794 if (password == NULL) {
798 snprintf(prompt, sizeof(prompt),
799 "Retype %.30s@%.128s's new password: ",
800 authctxt->server_user, authctxt->host);
801 retype = read_passphrase(prompt, 0);
802 if (strcmp(password, retype) != 0) {
803 memset(password, 0, strlen(password));
805 log("Mismatch; try again, EOF to quit.");
808 memset(retype, 0, strlen(retype));
811 packet_put_cstring(password);
812 memset(password, 0, strlen(password));
814 packet_add_padding(64);
817 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
818 &input_userauth_passwd_changereq);
822 clear_auth_state(Authctxt *authctxt)
824 /* XXX clear authentication state */
825 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
827 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
828 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
829 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
832 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
833 debug3("clear_auth_state: key_free %p", authctxt->last_key);
834 key_free(authctxt->last_key);
836 authctxt->last_key = NULL;
837 authctxt->last_key_hint = -2;
838 authctxt->last_key_sign = NULL;
842 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
845 u_char *blob, *signature;
851 debug3("sign_and_send_pubkey");
853 if (key_to_blob(k, &blob, &bloblen) == 0) {
854 /* we cannot handle this key */
855 debug3("sign_and_send_pubkey: cannot handle key");
858 /* data to be signed */
860 if (datafellows & SSH_OLD_SESSIONID) {
861 buffer_append(&b, session_id2, session_id2_len);
862 skip = session_id2_len;
864 buffer_put_string(&b, session_id2, session_id2_len);
865 skip = buffer_len(&b);
867 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
868 buffer_put_cstring(&b, authctxt->server_user);
869 buffer_put_cstring(&b,
870 datafellows & SSH_BUG_PKSERVICE ?
873 if (datafellows & SSH_BUG_PKAUTH) {
874 buffer_put_char(&b, have_sig);
876 buffer_put_cstring(&b, authctxt->method->name);
877 buffer_put_char(&b, have_sig);
878 buffer_put_cstring(&b, key_ssh_name(k));
880 buffer_put_string(&b, blob, bloblen);
882 /* generate signature */
883 ret = (*sign_callback)(authctxt, k, &signature, &slen,
884 buffer_ptr(&b), buffer_len(&b));
893 if (datafellows & SSH_BUG_PKSERVICE) {
895 buffer_append(&b, session_id2, session_id2_len);
896 skip = session_id2_len;
897 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
898 buffer_put_cstring(&b, authctxt->server_user);
899 buffer_put_cstring(&b, authctxt->service);
900 buffer_put_cstring(&b, authctxt->method->name);
901 buffer_put_char(&b, have_sig);
902 if (!(datafellows & SSH_BUG_PKAUTH))
903 buffer_put_cstring(&b, key_ssh_name(k));
904 buffer_put_string(&b, blob, bloblen);
908 /* append signature */
909 buffer_put_string(&b, signature, slen);
912 /* skip session id and packet type */
913 if (buffer_len(&b) < skip + 1)
914 fatal("userauth_pubkey: internal error");
915 buffer_consume(&b, skip + 1);
917 /* put remaining data from buffer into packet */
918 packet_start(SSH2_MSG_USERAUTH_REQUEST);
919 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
927 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
931 u_int bloblen, have_sig = 0;
933 debug3("send_pubkey_test");
935 if (key_to_blob(k, &blob, &bloblen) == 0) {
936 /* we cannot handle this key */
937 debug3("send_pubkey_test: cannot handle key");
940 /* register callback for USERAUTH_PK_OK message */
941 authctxt->last_key_sign = sign_callback;
942 authctxt->last_key_hint = hint;
943 authctxt->last_key = k;
944 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
946 packet_start(SSH2_MSG_USERAUTH_REQUEST);
947 packet_put_cstring(authctxt->server_user);
948 packet_put_cstring(authctxt->service);
949 packet_put_cstring(authctxt->method->name);
950 packet_put_char(have_sig);
951 if (!(datafellows & SSH_BUG_PKAUTH))
952 packet_put_cstring(key_ssh_name(k));
953 packet_put_string(blob, bloblen);
960 load_identity_file(char *filename)
963 char prompt[300], *passphrase;
967 if (stat(filename, &st) < 0) {
968 debug3("no such identity: %s", filename);
971 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
972 if (private == NULL) {
973 if (options.batch_mode)
975 snprintf(prompt, sizeof prompt,
976 "Enter passphrase for key '%.100s': ", filename);
977 for (i = 0; i < options.number_of_password_prompts; i++) {
978 passphrase = read_passphrase(prompt, 0);
979 if (strcmp(passphrase, "") != 0) {
980 private = key_load_private_type(KEY_UNSPEC, filename,
984 debug2("no passphrase given, try next key");
987 memset(passphrase, 0, strlen(passphrase));
989 if (private != NULL || quit)
991 debug2("bad passphrase given, try again...");
998 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
999 u_char *data, u_int datalen)
1004 idx = authctxt->last_key_hint;
1008 /* private key is stored in external hardware */
1009 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1010 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
1012 private = load_identity_file(options.identity_files[idx]);
1013 if (private == NULL)
1015 ret = key_sign(private, sigp, lenp, data, datalen);
1021 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1022 u_char *data, u_int datalen)
1024 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1028 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1029 u_char *data, u_int datalen)
1031 return key_sign(key, sigp, lenp, data, datalen);
1035 userauth_pubkey_agent(Authctxt *authctxt)
1037 static int called = 0;
1043 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1044 debug2("userauth_pubkey_agent: no keys at all");
1047 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1049 debug2("userauth_pubkey_agent: no more keys");
1051 debug("Offering agent key: %s", comment);
1053 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1058 debug2("userauth_pubkey_agent: no message sent");
1063 userauth_pubkey(Authctxt *authctxt)
1070 if (authctxt->agent != NULL) {
1072 sent = userauth_pubkey_agent(authctxt);
1073 } while (!sent && authctxt->agent->howmany > 0);
1075 while (!sent && idx < options.num_identity_files) {
1076 key = options.identity_keys[idx];
1077 filename = options.identity_files[idx];
1079 debug("Trying private key: %s", filename);
1080 key = load_identity_file(filename);
1082 sent = sign_and_send_pubkey(authctxt, key,
1086 } else if (key->type != KEY_RSA1) {
1087 debug("Offering public key: %s", filename);
1088 sent = send_pubkey_test(authctxt, key,
1089 identity_sign_cb, idx);
1097 * Send userauth request message specifying keyboard-interactive method.
1100 userauth_kbdint(Authctxt *authctxt)
1102 static int attempt = 0;
1104 if (attempt++ >= options.number_of_password_prompts)
1106 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1107 if (attempt > 1 && !authctxt->info_req_seen) {
1108 debug3("userauth_kbdint: disable: no info_req_seen");
1109 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1113 debug2("userauth_kbdint");
1114 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1115 packet_put_cstring(authctxt->server_user);
1116 packet_put_cstring(authctxt->service);
1117 packet_put_cstring(authctxt->method->name);
1118 packet_put_cstring(""); /* lang */
1119 packet_put_cstring(options.kbd_interactive_devices ?
1120 options.kbd_interactive_devices : "");
1123 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1128 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1131 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1133 Authctxt *authctxt = ctxt;
1134 char *name, *inst, *lang, *prompt, *response;
1135 u_int num_prompts, i;
1138 debug2("input_userauth_info_req");
1140 if (authctxt == NULL)
1141 fatal("input_userauth_info_req: no authentication context");
1143 authctxt->info_req_seen = 1;
1145 name = packet_get_string(NULL);
1146 inst = packet_get_string(NULL);
1147 lang = packet_get_string(NULL);
1148 if (strlen(name) > 0)
1150 if (strlen(inst) > 0)
1156 num_prompts = packet_get_int();
1158 * Begin to build info response packet based on prompts requested.
1159 * We commit to providing the correct number of responses, so if
1160 * further on we run into a problem that prevents this, we have to
1161 * be sure and clean this up and send a correct error response.
1163 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1164 packet_put_int(num_prompts);
1166 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1167 for (i = 0; i < num_prompts; i++) {
1168 prompt = packet_get_string(NULL);
1169 echo = packet_get_char();
1171 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1173 packet_put_cstring(response);
1174 memset(response, 0, strlen(response));
1178 packet_check_eom(); /* done with parsing incoming message. */
1180 packet_add_padding(64);
1185 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1186 u_char *data, u_int datalen)
1191 int to[2], from[2], status, version = 2;
1193 debug2("ssh_keysign called");
1195 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1196 error("ssh_keysign: no installed: %s", strerror(errno));
1199 if (fflush(stdout) != 0)
1200 error("ssh_keysign: fflush: %s", strerror(errno));
1202 error("ssh_keysign: pipe: %s", strerror(errno));
1205 if (pipe(from) < 0) {
1206 error("ssh_keysign: pipe: %s", strerror(errno));
1209 if ((pid = fork()) < 0) {
1210 error("ssh_keysign: fork: %s", strerror(errno));
1217 if (dup2(from[1], STDOUT_FILENO) < 0)
1218 fatal("ssh_keysign: dup2: %s", strerror(errno));
1220 if (dup2(to[0], STDIN_FILENO) < 0)
1221 fatal("ssh_keysign: dup2: %s", strerror(errno));
1224 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1225 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1232 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1233 buffer_put_string(&b, data, datalen);
1234 ssh_msg_send(to[1], version, &b);
1236 if (ssh_msg_recv(from[0], &b) < 0) {
1237 error("ssh_keysign: no reply");
1244 while (waitpid(pid, &status, 0) < 0)
1248 if (buffer_get_char(&b) != version) {
1249 error("ssh_keysign: bad version");
1253 *sigp = buffer_get_string(&b, lenp);
1260 userauth_hostbased(Authctxt *authctxt)
1262 Key *private = NULL;
1263 Sensitive *sensitive = authctxt->sensitive;
1265 u_char *signature, *blob;
1266 char *chost, *pkalg, *p;
1267 const char *service;
1269 int ok, i, len, found = 0;
1271 /* check for a useful key */
1272 for (i = 0; i < sensitive->nkeys; i++) {
1273 private = sensitive->keys[i];
1274 if (private && private->type != KEY_RSA1) {
1276 /* we take and free the key */
1277 sensitive->keys[i] = NULL;
1282 debug("No more client hostkeys for hostbased authentication.");
1285 if (key_to_blob(private, &blob, &blen) == 0) {
1289 /* figure out a name for the client host */
1290 p = get_local_name(packet_get_connection_in());
1292 error("userauth_hostbased: cannot get local ipaddr/name");
1296 len = strlen(p) + 2;
1297 chost = xmalloc(len);
1298 strlcpy(chost, p, len);
1299 strlcat(chost, ".", len);
1300 debug2("userauth_hostbased: chost %s", chost);
1303 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1305 pkalg = xstrdup(key_ssh_name(private));
1307 /* construct data */
1308 buffer_put_string(&b, session_id2, session_id2_len);
1309 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1310 buffer_put_cstring(&b, authctxt->server_user);
1311 buffer_put_cstring(&b, service);
1312 buffer_put_cstring(&b, authctxt->method->name);
1313 buffer_put_cstring(&b, pkalg);
1314 buffer_put_string(&b, blob, blen);
1315 buffer_put_cstring(&b, chost);
1316 buffer_put_cstring(&b, authctxt->local_user);
1320 if (sensitive->external_keysign)
1321 ok = ssh_keysign(private, &signature, &slen,
1322 buffer_ptr(&b), buffer_len(&b));
1324 ok = key_sign(private, &signature, &slen,
1325 buffer_ptr(&b), buffer_len(&b));
1329 error("key_sign failed");
1334 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1335 packet_put_cstring(authctxt->server_user);
1336 packet_put_cstring(authctxt->service);
1337 packet_put_cstring(authctxt->method->name);
1338 packet_put_cstring(pkalg);
1339 packet_put_string(blob, blen);
1340 packet_put_cstring(chost);
1341 packet_put_cstring(authctxt->local_user);
1342 packet_put_string(signature, slen);
1343 memset(signature, 's', slen);
1352 /* find auth method */
1355 * given auth method name, if configurable options permit this method fill
1356 * in auth_ident field and return true, otherwise return false.
1359 authmethod_is_enabled(Authmethod *method)
1363 /* return false if options indicate this method is disabled */
1364 if (method->enabled == NULL || *method->enabled == 0)
1366 /* return false if batch mode is enabled but method needs interactive mode */
1367 if (method->batch_flag != NULL && *method->batch_flag != 0)
1373 authmethod_lookup(const char *name)
1375 Authmethod *method = NULL;
1377 for (method = authmethods; method->name != NULL; method++)
1378 if (strcmp(name, method->name) == 0)
1380 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1384 /* XXX internal state */
1385 static Authmethod *current = NULL;
1386 static char *supported = NULL;
1387 static char *preferred = NULL;
1390 * Given the authentication method list sent by the server, return the
1391 * next method we should try. If the server initially sends a nil list,
1392 * use a built-in default list.
1395 authmethod_get(char *authlist)
1400 /* Use a suitable default if we're passed a nil list. */
1401 if (authlist == NULL || strlen(authlist) == 0)
1402 authlist = options.preferred_authentications;
1404 if (supported == NULL || strcmp(authlist, supported) != 0) {
1405 debug3("start over, passed a different list %s", authlist);
1406 if (supported != NULL)
1408 supported = xstrdup(authlist);
1409 preferred = options.preferred_authentications;
1410 debug3("preferred %s", preferred);
1412 } else if (current != NULL && authmethod_is_enabled(current))
1416 if ((name = match_list(preferred, supported, &next)) == NULL) {
1417 debug("No more authentication methods to try.");
1422 debug3("authmethod_lookup %s", name);
1423 debug3("remaining preferred: %s", preferred);
1424 if ((current = authmethod_lookup(name)) != NULL &&
1425 authmethod_is_enabled(current)) {
1426 debug3("authmethod_is_enabled %s", name);
1427 debug("Next authentication method: %s", name);
1434 authmethods_get(void)
1436 Authmethod *method = NULL;
1441 for (method = authmethods; method->name != NULL; method++) {
1442 if (authmethod_is_enabled(method)) {
1443 if (buffer_len(&b) > 0)
1444 buffer_append(&b, ",", 1);
1445 buffer_append(&b, method->name, strlen(method->name));
1448 buffer_append(&b, "\0", 1);
1449 list = xstrdup(buffer_ptr(&b));