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.138 2004/06/13 12:53:24 djm Exp $");
28 #include "openbsd-compat/sys-queue.h"
39 #include "myproposal.h"
40 #include "sshconnect.h"
51 #include "pathnames.h"
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
66 u_char *session_id2 = NULL;
67 u_int session_id2_len = 0;
70 struct sockaddr *xxx_hostaddr;
75 verify_host_key_callback(Key *hostkey)
77 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
78 fatal("Host key verification failed.");
83 ssh_kex2(char *host, struct sockaddr *hostaddr)
92 xxx_hostaddr = hostaddr;
95 if (options.gss_keyex) {
96 /* Add the GSSAPI mechanisms currently supported on this client to
97 * the key exchange algorithm proposal */
98 orig = myproposal[PROPOSAL_KEX_ALGS];
99 gss = ssh_gssapi_client_mechanisms((char *)get_canonical_hostname(1));
101 len = strlen(orig)+strlen(gss)+2;
102 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
103 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
108 if (options.ciphers == (char *)-1) {
109 logit("No valid ciphers for protocol version 2 given, using defaults.");
110 options.ciphers = NULL;
112 if (options.ciphers != NULL) {
113 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
114 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
116 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
118 myproposal[PROPOSAL_ENC_ALGS_STOC] =
119 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
120 if (options.compression) {
121 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
122 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
127 if (options.macs != NULL) {
128 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
129 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
131 if (options.hostkeyalgorithms != NULL)
132 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
133 options.hostkeyalgorithms;
136 /* If we've got GSSAPI algorithms, then we also support the
137 * 'null' hostkey, as a last resort */
138 if (options.gss_keyex && gss) {
139 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
140 len = strlen(orig)+sizeof(",null");
141 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
142 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
146 if (options.rekey_limit)
147 packet_set_rekey_limit(options.rekey_limit);
149 /* start key exchange */
150 kex = kex_setup(myproposal);
151 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
152 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
153 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
155 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
157 kex->client_version_string=client_version_string;
158 kex->server_version_string=server_version_string;
159 kex->verify_host_key=&verify_host_key_callback;
161 kex->options.gss_deleg_creds=options.gss_deleg_creds;
166 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
168 session_id2 = kex->session_id;
169 session_id2_len = kex->session_id_len;
172 /* send 1st encrypted/maced/compressed message */
173 packet_start(SSH2_MSG_IGNORE);
174 packet_put_cstring("markus");
184 typedef struct Authctxt Authctxt;
185 typedef struct Authmethod Authmethod;
186 typedef struct identity Identity;
187 typedef struct idlist Idlist;
190 TAILQ_ENTRY(identity) next;
191 AuthenticationConnection *ac; /* set if agent supports key */
192 Key *key; /* public/private key */
193 char *filename; /* comment for agent-only keys */
195 int isprivate; /* key points to the private key */
197 TAILQ_HEAD(idlist, identity);
200 const char *server_user;
201 const char *local_user;
209 AuthenticationConnection *agent;
211 Sensitive *sensitive;
212 /* kbd-interactive */
218 char *name; /* string to compare against server's list */
219 int (*userauth)(Authctxt *authctxt);
220 int *enabled; /* flag in option struct that enables method */
221 int *batch_flag; /* flag in option struct that disables method */
224 void input_userauth_success(int, u_int32_t, void *);
225 void input_userauth_failure(int, u_int32_t, void *);
226 void input_userauth_banner(int, u_int32_t, void *);
227 void input_userauth_error(int, u_int32_t, void *);
228 void input_userauth_info_req(int, u_int32_t, void *);
229 void input_userauth_pk_ok(int, u_int32_t, void *);
230 void input_userauth_passwd_changereq(int, u_int32_t, void *);
232 int userauth_none(Authctxt *);
233 int userauth_pubkey(Authctxt *);
234 int userauth_passwd(Authctxt *);
235 int userauth_kbdint(Authctxt *);
236 int userauth_hostbased(Authctxt *);
237 int userauth_kerberos(Authctxt *);
240 int userauth_gssapi(Authctxt *authctxt);
241 void input_gssapi_response(int type, u_int32_t, void *);
242 void input_gssapi_token(int type, u_int32_t, void *);
243 void input_gssapi_hash(int type, u_int32_t, void *);
244 void input_gssapi_error(int, u_int32_t, void *);
245 void input_gssapi_errtok(int, u_int32_t, void *);
249 int userauth_external(Authctxt *authctxt);
250 int userauth_gssapi(Authctxt *authctxt);
251 int userauth_gssapi_with_mic(Authctxt *authctxt);
252 int userauth_gssapi_without_mic(Authctxt *authctxt);
253 void input_gssapi_response(int type, u_int32_t, void *);
254 void input_gssapi_token(int type, u_int32_t, void *);
255 void input_gssapi_hash(int type, u_int32_t, void *);
256 void input_gssapi_error(int, u_int32_t, void *);
257 void input_gssapi_errtok(int, u_int32_t, void *);
260 void userauth(Authctxt *, char *);
262 static int sign_and_send_pubkey(Authctxt *, Identity *);
263 static void pubkey_prepare(Authctxt *);
264 static void pubkey_cleanup(Authctxt *);
265 static Key *load_identity_file(char *);
267 static Authmethod *authmethod_get(char *authlist);
268 static Authmethod *authmethod_lookup(const char *name);
269 static char *authmethods_get(void);
271 Authmethod authmethods[] = {
275 &options.gss_authentication,
279 &options.gss_authentication,
283 &options.gss_authentication,
288 &options.hostbased_authentication,
292 &options.pubkey_authentication,
294 {"keyboard-interactive",
296 &options.kbd_interactive_authentication,
297 &options.batch_mode},
300 &options.password_authentication,
301 &options.batch_mode},
306 {NULL, NULL, NULL, NULL}
310 ssh_userauth2(const char *local_user, const char *server_user, char *host,
311 Sensitive *sensitive)
316 if (options.challenge_response_authentication)
317 options.kbd_interactive_authentication = 1;
319 packet_start(SSH2_MSG_SERVICE_REQUEST);
320 packet_put_cstring("ssh-userauth");
322 debug("SSH2_MSG_SERVICE_REQUEST sent");
324 type = packet_read();
325 if (type != SSH2_MSG_SERVICE_ACCEPT)
326 fatal("Server denied authentication request: %d", type);
327 if (packet_remaining() > 0) {
328 char *reply = packet_get_string(NULL);
329 debug2("service_accept: %s", reply);
332 debug2("buggy server: service_accept w/o service");
335 debug("SSH2_MSG_SERVICE_ACCEPT received");
337 if (options.preferred_authentications == NULL)
338 options.preferred_authentications = authmethods_get();
340 /* setup authentication context */
341 memset(&authctxt, 0, sizeof(authctxt));
342 pubkey_prepare(&authctxt);
343 authctxt.server_user = server_user;
344 authctxt.local_user = local_user;
345 authctxt.host = host;
346 authctxt.service = "ssh-connection"; /* service name */
347 authctxt.success = 0;
348 authctxt.method = authmethod_lookup("none");
349 authctxt.authlist = NULL;
350 authctxt.methoddata = NULL;
351 authctxt.sensitive = sensitive;
352 authctxt.info_req_seen = 0;
353 if (authctxt.method == NULL)
354 fatal("ssh_userauth2: internal error: cannot send userauth none request");
356 /* initial userauth request */
357 userauth_none(&authctxt);
359 dispatch_init(&input_userauth_error);
360 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
361 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
362 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
363 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
365 pubkey_cleanup(&authctxt);
366 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
368 debug("Authentication succeeded (%s).", authctxt.method->name);
372 userauth(Authctxt *authctxt, char *authlist)
374 if (authctxt->methoddata) {
375 xfree(authctxt->methoddata);
376 authctxt->methoddata = NULL;
378 if (authlist == NULL) {
379 authlist = authctxt->authlist;
381 if (authctxt->authlist)
382 xfree(authctxt->authlist);
383 authctxt->authlist = authlist;
386 Authmethod *method = authmethod_get(authlist);
388 fatal("Permission denied (%s).", authlist);
389 authctxt->method = method;
391 /* reset the per method handler */
392 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
393 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
395 /* and try new method */
396 if (method->userauth(authctxt) != 0) {
397 debug2("we sent a %s packet, wait for reply", method->name);
400 debug2("we did not send a packet, disable method");
401 method->enabled = NULL;
407 input_userauth_error(int type, u_int32_t seq, void *ctxt)
409 fatal("input_userauth_error: bad message during authentication: "
414 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
418 debug3("input_userauth_banner");
419 msg = packet_get_string(NULL);
420 lang = packet_get_string(NULL);
421 if (options.log_level > SYSLOG_LEVEL_QUIET)
422 fprintf(stderr, "%s", msg);
428 input_userauth_success(int type, u_int32_t seq, void *ctxt)
430 Authctxt *authctxt = ctxt;
431 if (authctxt == NULL)
432 fatal("input_userauth_success: no authentication context");
433 if (authctxt->authlist) {
434 xfree(authctxt->authlist);
435 authctxt->authlist = NULL;
437 if (authctxt->methoddata) {
438 xfree(authctxt->methoddata);
439 authctxt->methoddata = NULL;
441 authctxt->success = 1; /* break out */
445 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
447 Authctxt *authctxt = ctxt;
448 char *authlist = NULL;
451 if (authctxt == NULL)
452 fatal("input_userauth_failure: no authentication context");
454 authlist = packet_get_string(NULL);
455 partial = packet_get_char();
459 logit("Authenticated with partial success.");
460 debug("Authentications that can continue: %s", authlist);
462 userauth(authctxt, authlist);
465 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
467 Authctxt *authctxt = ctxt;
471 int pktype, sent = 0;
476 if (authctxt == NULL)
477 fatal("input_userauth_pk_ok: no authentication context");
478 if (datafellows & SSH_BUG_PKOK) {
479 /* this is similar to SSH_BUG_PKAUTH */
480 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
481 pkblob = packet_get_string(&blen);
483 buffer_append(&b, pkblob, blen);
484 pkalg = buffer_get_string(&b, &alen);
487 pkalg = packet_get_string(&alen);
488 pkblob = packet_get_string(&blen);
492 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
494 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
495 debug("unknown pkalg %s", pkalg);
498 if ((key = key_from_blob(pkblob, blen)) == NULL) {
499 debug("no key from blob. pkalg %s", pkalg);
502 if (key->type != pktype) {
503 error("input_userauth_pk_ok: type mismatch "
504 "for decoded key (received %d, expected %d)",
508 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
509 debug2("input_userauth_pk_ok: fp %s", fp);
513 * search keys in the reverse order, because last candidate has been
514 * moved to the end of the queue. this also avoids confusion by
517 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
518 if (key_equal(key, id->key)) {
519 sent = sign_and_send_pubkey(authctxt, id);
529 /* try another method if we did not send a packet */
531 userauth(authctxt, NULL);
536 userauth_gssapi(Authctxt *authctxt)
538 Gssctxt *gssctxt = NULL;
539 static gss_OID_set gss_supported = NULL;
544 if (!options.gss_authentication) {
545 verbose("GSSAPI authentication disabled.");
549 /* Try one GSSAPI method at a time, rather than sending them all at
552 if (gss_supported == NULL)
553 gss_indicate_mechs(&min, &gss_supported);
555 /* Check to see if the mechanism is usable before we offer it */
556 while (mech < gss_supported->count && !ok) {
558 ssh_gssapi_delete_ctx(&gssctxt);
559 ssh_gssapi_build_ctx(&gssctxt);
560 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
562 /* My DER encoding requires length<128 */
563 if (gss_supported->elements[mech].length < 128 &&
564 ssh_gssapi_check_mechanism(&gss_supported->elements[mech],
565 get_canonical_hostname(1)) &&
566 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
567 get_canonical_hostname(1)))) {
568 ok = 1; /* Mechanism works */
576 authctxt->methoddata=(void *)gssctxt;
578 packet_start(SSH2_MSG_USERAUTH_REQUEST);
579 packet_put_cstring(authctxt->server_user);
580 packet_put_cstring(authctxt->service);
581 packet_put_cstring(authctxt->method->name);
585 /* Some servers encode the OID incorrectly (as we used to) */
586 if (datafellows & SSH_BUG_GSSAPI_BER) {
587 packet_put_string(gss_supported->elements[mech].elements,
588 gss_supported->elements[mech].length);
590 packet_put_int((gss_supported->elements[mech].length)+2);
591 packet_put_char(SSH_GSS_OIDTYPE);
592 packet_put_char(gss_supported->elements[mech].length);
593 packet_put_raw(gss_supported->elements[mech].elements,
594 gss_supported->elements[mech].length);
599 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
600 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
601 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
602 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
604 mech++; /* Move along to next candidate */
610 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
612 Authctxt *authctxt = ctxt;
613 Gssctxt *gssctxt = authctxt->methoddata;
614 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
615 gss_buffer_desc gssbuf, mic;
616 OM_uint32 status, ms, flags;
619 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
620 recv_tok, &send_tok, &flags);
622 if (send_tok.length > 0) {
623 if (GSS_ERROR(status))
624 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
626 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
628 packet_put_string(send_tok.value, send_tok.length);
630 gss_release_buffer(&ms, &send_tok);
633 if (status == GSS_S_COMPLETE) {
634 /* send either complete or MIC, depending on mechanism */
635 if (strcmp(authctxt->method->name,"gssapi")==0 ||
636 (!(flags & GSS_C_INTEG_FLAG))) {
637 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
640 ssh_gssapi_buildmic(&b, authctxt->server_user,
641 authctxt->service, "gssapi-with-mic");
643 gssbuf.value = buffer_ptr(&b);
644 gssbuf.length = buffer_len(&b);
646 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
648 if (!GSS_ERROR(status)) {
649 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
650 packet_put_string(mic.value, mic.length);
656 gss_release_buffer(&ms, &mic);
664 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
666 Authctxt *authctxt = ctxt;
671 if (authctxt == NULL)
672 fatal("input_gssapi_response: no authentication context");
673 gssctxt = authctxt->methoddata;
676 oidv = packet_get_string(&oidlen);
678 if (datafellows & SSH_BUG_GSSAPI_BER) {
679 if (!ssh_gssapi_check_oid(gssctxt, oidv, oidlen))
680 fatal("Server returned different OID than expected");
682 if(oidv[0] != SSH_GSS_OIDTYPE || oidv[1] != oidlen-2) {
683 debug("Badly encoded mechanism OID received");
684 userauth(authctxt, NULL);
688 if (!ssh_gssapi_check_oid(gssctxt, oidv+2, oidlen-2))
689 fatal("Server returned different OID than expected");
696 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
697 /* Start again with next method on list */
698 debug("Trying to start again");
699 userauth(authctxt, NULL);
705 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
707 Authctxt *authctxt = ctxt;
708 gss_buffer_desc recv_tok;
712 if (authctxt == NULL)
713 fatal("input_gssapi_response: no authentication context");
715 recv_tok.value = packet_get_string(&slen);
716 recv_tok.length = slen; /* safe typecast */
720 status = process_gssapi_token(ctxt, &recv_tok);
722 xfree(recv_tok.value);
724 if (GSS_ERROR(status)) {
725 /* Start again with the next method in the list */
726 userauth(authctxt, NULL);
732 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
734 Authctxt *authctxt = ctxt;
736 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
737 gss_buffer_desc recv_tok;
738 OM_uint32 status, ms;
741 if (authctxt == NULL)
742 fatal("input_gssapi_response: no authentication context");
743 gssctxt = authctxt->methoddata;
745 recv_tok.value = packet_get_string(&len);
746 recv_tok.length = len;
750 /* Stick it into GSSAPI and see what it says */
751 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
752 &recv_tok, &send_tok, NULL);
754 xfree(recv_tok.value);
755 gss_release_buffer(&ms, &send_tok);
757 /* Server will be returning a failed packet after this one */
761 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
767 maj=packet_get_int();
768 min=packet_get_int();
769 msg=packet_get_string(NULL);
770 lang=packet_get_string(NULL);
774 debug("Server GSSAPI Error:\n%s\n", msg);
780 userauth_external(Authctxt *authctxt)
782 static int attempt = 0;
787 /* The client MUST NOT try this method if initial key exchange
788 was not performed using a GSSAPI-based key exchange
790 if (xxx_kex->kex_type != KEX_GSS_GRP1_SHA1) {
791 debug2("gsskex not performed, skipping external-keyx");
795 debug2("userauth_external");
796 packet_start(SSH2_MSG_USERAUTH_REQUEST);
798 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
799 packet_put_cstring("");
802 packet_put_cstring(authctxt->server_user);
806 packet_put_cstring(authctxt->service);
807 packet_put_cstring(authctxt->method->name);
815 userauth_none(Authctxt *authctxt)
817 /* initial userauth request */
818 packet_start(SSH2_MSG_USERAUTH_REQUEST);
819 packet_put_cstring(authctxt->server_user);
820 packet_put_cstring(authctxt->service);
821 packet_put_cstring(authctxt->method->name);
827 userauth_passwd(Authctxt *authctxt)
829 static int attempt = 0;
833 if (attempt++ >= options.number_of_password_prompts)
837 error("Permission denied, please try again.");
839 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
840 authctxt->server_user, authctxt->host);
841 password = read_passphrase(prompt, 0);
842 packet_start(SSH2_MSG_USERAUTH_REQUEST);
843 packet_put_cstring(authctxt->server_user);
844 packet_put_cstring(authctxt->service);
845 packet_put_cstring(authctxt->method->name);
847 packet_put_cstring(password);
848 memset(password, 0, strlen(password));
850 packet_add_padding(64);
853 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
854 &input_userauth_passwd_changereq);
859 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
862 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
864 Authctxt *authctxt = ctxt;
865 char *info, *lang, *password = NULL, *retype = NULL;
868 debug2("input_userauth_passwd_changereq");
870 if (authctxt == NULL)
871 fatal("input_userauth_passwd_changereq: "
872 "no authentication context");
874 info = packet_get_string(NULL);
875 lang = packet_get_string(NULL);
876 if (strlen(info) > 0)
880 packet_start(SSH2_MSG_USERAUTH_REQUEST);
881 packet_put_cstring(authctxt->server_user);
882 packet_put_cstring(authctxt->service);
883 packet_put_cstring(authctxt->method->name);
884 packet_put_char(1); /* additional info */
885 snprintf(prompt, sizeof(prompt),
886 "Enter %.30s@%.128s's old password: ",
887 authctxt->server_user, authctxt->host);
888 password = read_passphrase(prompt, 0);
889 packet_put_cstring(password);
890 memset(password, 0, strlen(password));
893 while (password == NULL) {
894 snprintf(prompt, sizeof(prompt),
895 "Enter %.30s@%.128s's new password: ",
896 authctxt->server_user, authctxt->host);
897 password = read_passphrase(prompt, RP_ALLOW_EOF);
898 if (password == NULL) {
902 snprintf(prompt, sizeof(prompt),
903 "Retype %.30s@%.128s's new password: ",
904 authctxt->server_user, authctxt->host);
905 retype = read_passphrase(prompt, 0);
906 if (strcmp(password, retype) != 0) {
907 memset(password, 0, strlen(password));
909 logit("Mismatch; try again, EOF to quit.");
912 memset(retype, 0, strlen(retype));
915 packet_put_cstring(password);
916 memset(password, 0, strlen(password));
918 packet_add_padding(64);
921 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
922 &input_userauth_passwd_changereq);
926 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
927 u_char *data, u_int datalen)
932 /* the agent supports this key */
934 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
937 * we have already loaded the private key or
938 * the private key is stored in external hardware
940 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
941 return (key_sign(id->key, sigp, lenp, data, datalen));
942 /* load the private key from the file */
943 if ((prv = load_identity_file(id->filename)) == NULL)
945 ret = key_sign(prv, sigp, lenp, data, datalen);
951 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
954 u_char *blob, *signature;
960 debug3("sign_and_send_pubkey");
962 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
963 /* we cannot handle this key */
964 debug3("sign_and_send_pubkey: cannot handle key");
967 /* data to be signed */
969 if (datafellows & SSH_OLD_SESSIONID) {
970 buffer_append(&b, session_id2, session_id2_len);
971 skip = session_id2_len;
973 buffer_put_string(&b, session_id2, session_id2_len);
974 skip = buffer_len(&b);
976 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
977 buffer_put_cstring(&b, authctxt->server_user);
978 buffer_put_cstring(&b,
979 datafellows & SSH_BUG_PKSERVICE ?
982 if (datafellows & SSH_BUG_PKAUTH) {
983 buffer_put_char(&b, have_sig);
985 buffer_put_cstring(&b, authctxt->method->name);
986 buffer_put_char(&b, have_sig);
987 buffer_put_cstring(&b, key_ssh_name(id->key));
989 buffer_put_string(&b, blob, bloblen);
991 /* generate signature */
992 ret = identity_sign(id, &signature, &slen,
993 buffer_ptr(&b), buffer_len(&b));
1002 if (datafellows & SSH_BUG_PKSERVICE) {
1004 buffer_append(&b, session_id2, session_id2_len);
1005 skip = session_id2_len;
1006 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1007 buffer_put_cstring(&b, authctxt->server_user);
1008 buffer_put_cstring(&b, authctxt->service);
1009 buffer_put_cstring(&b, authctxt->method->name);
1010 buffer_put_char(&b, have_sig);
1011 if (!(datafellows & SSH_BUG_PKAUTH))
1012 buffer_put_cstring(&b, key_ssh_name(id->key));
1013 buffer_put_string(&b, blob, bloblen);
1017 /* append signature */
1018 buffer_put_string(&b, signature, slen);
1021 /* skip session id and packet type */
1022 if (buffer_len(&b) < skip + 1)
1023 fatal("userauth_pubkey: internal error");
1024 buffer_consume(&b, skip + 1);
1026 /* put remaining data from buffer into packet */
1027 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1028 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1036 send_pubkey_test(Authctxt *authctxt, Identity *id)
1039 u_int bloblen, have_sig = 0;
1041 debug3("send_pubkey_test");
1043 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1044 /* we cannot handle this key */
1045 debug3("send_pubkey_test: cannot handle key");
1048 /* register callback for USERAUTH_PK_OK message */
1049 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1051 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1052 packet_put_cstring(authctxt->server_user);
1053 packet_put_cstring(authctxt->service);
1054 packet_put_cstring(authctxt->method->name);
1055 packet_put_char(have_sig);
1056 if (!(datafellows & SSH_BUG_PKAUTH))
1057 packet_put_cstring(key_ssh_name(id->key));
1058 packet_put_string(blob, bloblen);
1065 load_identity_file(char *filename)
1068 char prompt[300], *passphrase;
1072 if (stat(filename, &st) < 0) {
1073 debug3("no such identity: %s", filename);
1076 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1077 if (private == NULL) {
1078 if (options.batch_mode)
1080 snprintf(prompt, sizeof prompt,
1081 "Enter passphrase for key '%.100s': ", filename);
1082 for (i = 0; i < options.number_of_password_prompts; i++) {
1083 passphrase = read_passphrase(prompt, 0);
1084 if (strcmp(passphrase, "") != 0) {
1085 private = key_load_private_type(KEY_UNSPEC, filename,
1089 debug2("no passphrase given, try next key");
1092 memset(passphrase, 0, strlen(passphrase));
1094 if (private != NULL || quit)
1096 debug2("bad passphrase given, try again...");
1103 * try keys in the following order:
1104 * 1. agent keys that are found in the config file
1105 * 2. other agent keys
1106 * 3. keys that are only listed in the config file
1109 pubkey_prepare(Authctxt *authctxt)
1112 Idlist agent, files, *preferred;
1114 AuthenticationConnection *ac;
1118 TAILQ_INIT(&agent); /* keys from the agent */
1119 TAILQ_INIT(&files); /* keys from the config file */
1120 preferred = &authctxt->keys;
1121 TAILQ_INIT(preferred); /* preferred order of keys */
1123 /* list of keys stored in the filesystem */
1124 for (i = 0; i < options.num_identity_files; i++) {
1125 key = options.identity_keys[i];
1126 if (key && key->type == KEY_RSA1)
1128 options.identity_keys[i] = NULL;
1129 id = xmalloc(sizeof(*id));
1130 memset(id, 0, sizeof(*id));
1132 id->filename = xstrdup(options.identity_files[i]);
1133 TAILQ_INSERT_TAIL(&files, id, next);
1135 /* list of keys supported by the agent */
1136 if ((ac = ssh_get_authentication_connection())) {
1137 for (key = ssh_get_first_identity(ac, &comment, 2);
1139 key = ssh_get_next_identity(ac, &comment, 2)) {
1141 TAILQ_FOREACH(id, &files, next) {
1142 /* agent keys from the config file are preferred */
1143 if (key_equal(key, id->key)) {
1146 TAILQ_REMOVE(&files, id, next);
1147 TAILQ_INSERT_TAIL(preferred, id, next);
1153 if (!found && !options.identities_only) {
1154 id = xmalloc(sizeof(*id));
1155 memset(id, 0, sizeof(*id));
1157 id->filename = comment;
1159 TAILQ_INSERT_TAIL(&agent, id, next);
1162 /* append remaining agent keys */
1163 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1164 TAILQ_REMOVE(&agent, id, next);
1165 TAILQ_INSERT_TAIL(preferred, id, next);
1167 authctxt->agent = ac;
1169 /* append remaining keys from the config file */
1170 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1171 TAILQ_REMOVE(&files, id, next);
1172 TAILQ_INSERT_TAIL(preferred, id, next);
1174 TAILQ_FOREACH(id, preferred, next) {
1175 debug2("key: %s (%p)", id->filename, id->key);
1180 pubkey_cleanup(Authctxt *authctxt)
1184 if (authctxt->agent != NULL)
1185 ssh_close_authentication_connection(authctxt->agent);
1186 for (id = TAILQ_FIRST(&authctxt->keys); id;
1187 id = TAILQ_FIRST(&authctxt->keys)) {
1188 TAILQ_REMOVE(&authctxt->keys, id, next);
1192 xfree(id->filename);
1198 userauth_pubkey(Authctxt *authctxt)
1203 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1206 /* move key to the end of the queue */
1207 TAILQ_REMOVE(&authctxt->keys, id, next);
1208 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1210 * send a test message if we have the public key. for
1211 * encrypted keys we cannot do this and have to load the
1212 * private key instead
1214 if (id->key && id->key->type != KEY_RSA1) {
1215 debug("Offering public key: %s", id->filename);
1216 sent = send_pubkey_test(authctxt, id);
1217 } else if (id->key == NULL) {
1218 debug("Trying private key: %s", id->filename);
1219 id->key = load_identity_file(id->filename);
1220 if (id->key != NULL) {
1222 sent = sign_and_send_pubkey(authctxt, id);
1234 * Send userauth request message specifying keyboard-interactive method.
1237 userauth_kbdint(Authctxt *authctxt)
1239 static int attempt = 0;
1241 if (attempt++ >= options.number_of_password_prompts)
1243 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1244 if (attempt > 1 && !authctxt->info_req_seen) {
1245 debug3("userauth_kbdint: disable: no info_req_seen");
1246 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1250 debug2("userauth_kbdint");
1251 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1252 packet_put_cstring(authctxt->server_user);
1253 packet_put_cstring(authctxt->service);
1254 packet_put_cstring(authctxt->method->name);
1255 packet_put_cstring(""); /* lang */
1256 packet_put_cstring(options.kbd_interactive_devices ?
1257 options.kbd_interactive_devices : "");
1260 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1265 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1268 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1270 Authctxt *authctxt = ctxt;
1271 char *name, *inst, *lang, *prompt, *response;
1272 u_int num_prompts, i;
1275 debug2("input_userauth_info_req");
1277 if (authctxt == NULL)
1278 fatal("input_userauth_info_req: no authentication context");
1280 authctxt->info_req_seen = 1;
1282 name = packet_get_string(NULL);
1283 inst = packet_get_string(NULL);
1284 lang = packet_get_string(NULL);
1285 if (strlen(name) > 0)
1287 if (strlen(inst) > 0)
1293 num_prompts = packet_get_int();
1295 * Begin to build info response packet based on prompts requested.
1296 * We commit to providing the correct number of responses, so if
1297 * further on we run into a problem that prevents this, we have to
1298 * be sure and clean this up and send a correct error response.
1300 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1301 packet_put_int(num_prompts);
1303 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1304 for (i = 0; i < num_prompts; i++) {
1305 prompt = packet_get_string(NULL);
1306 echo = packet_get_char();
1308 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1310 packet_put_cstring(response);
1311 memset(response, 0, strlen(response));
1315 packet_check_eom(); /* done with parsing incoming message. */
1317 packet_add_padding(64);
1322 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1323 u_char *data, u_int datalen)
1328 int to[2], from[2], status, version = 2;
1330 debug2("ssh_keysign called");
1332 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1333 error("ssh_keysign: no installed: %s", strerror(errno));
1336 if (fflush(stdout) != 0)
1337 error("ssh_keysign: fflush: %s", strerror(errno));
1339 error("ssh_keysign: pipe: %s", strerror(errno));
1342 if (pipe(from) < 0) {
1343 error("ssh_keysign: pipe: %s", strerror(errno));
1346 if ((pid = fork()) < 0) {
1347 error("ssh_keysign: fork: %s", strerror(errno));
1354 if (dup2(from[1], STDOUT_FILENO) < 0)
1355 fatal("ssh_keysign: dup2: %s", strerror(errno));
1357 if (dup2(to[0], STDIN_FILENO) < 0)
1358 fatal("ssh_keysign: dup2: %s", strerror(errno));
1361 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1362 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1369 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1370 buffer_put_string(&b, data, datalen);
1371 if (ssh_msg_send(to[1], version, &b) == -1)
1372 fatal("ssh_keysign: couldn't send request");
1374 if (ssh_msg_recv(from[0], &b) < 0) {
1375 error("ssh_keysign: no reply");
1382 while (waitpid(pid, &status, 0) < 0)
1386 if (buffer_get_char(&b) != version) {
1387 error("ssh_keysign: bad version");
1391 *sigp = buffer_get_string(&b, lenp);
1398 userauth_hostbased(Authctxt *authctxt)
1400 Key *private = NULL;
1401 Sensitive *sensitive = authctxt->sensitive;
1403 u_char *signature, *blob;
1404 char *chost, *pkalg, *p;
1405 const char *service;
1407 int ok, i, len, found = 0;
1409 /* check for a useful key */
1410 for (i = 0; i < sensitive->nkeys; i++) {
1411 private = sensitive->keys[i];
1412 if (private && private->type != KEY_RSA1) {
1414 /* we take and free the key */
1415 sensitive->keys[i] = NULL;
1420 debug("No more client hostkeys for hostbased authentication.");
1423 if (key_to_blob(private, &blob, &blen) == 0) {
1427 /* figure out a name for the client host */
1428 p = get_local_name(packet_get_connection_in());
1430 error("userauth_hostbased: cannot get local ipaddr/name");
1434 len = strlen(p) + 2;
1435 chost = xmalloc(len);
1436 strlcpy(chost, p, len);
1437 strlcat(chost, ".", len);
1438 debug2("userauth_hostbased: chost %s", chost);
1441 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1443 pkalg = xstrdup(key_ssh_name(private));
1445 /* construct data */
1446 buffer_put_string(&b, session_id2, session_id2_len);
1447 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1448 buffer_put_cstring(&b, authctxt->server_user);
1449 buffer_put_cstring(&b, service);
1450 buffer_put_cstring(&b, authctxt->method->name);
1451 buffer_put_cstring(&b, pkalg);
1452 buffer_put_string(&b, blob, blen);
1453 buffer_put_cstring(&b, chost);
1454 buffer_put_cstring(&b, authctxt->local_user);
1458 if (sensitive->external_keysign)
1459 ok = ssh_keysign(private, &signature, &slen,
1460 buffer_ptr(&b), buffer_len(&b));
1462 ok = key_sign(private, &signature, &slen,
1463 buffer_ptr(&b), buffer_len(&b));
1467 error("key_sign failed");
1472 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1473 packet_put_cstring(authctxt->server_user);
1474 packet_put_cstring(authctxt->service);
1475 packet_put_cstring(authctxt->method->name);
1476 packet_put_cstring(pkalg);
1477 packet_put_string(blob, blen);
1478 packet_put_cstring(chost);
1479 packet_put_cstring(authctxt->local_user);
1480 packet_put_string(signature, slen);
1481 memset(signature, 's', slen);
1490 /* find auth method */
1493 * given auth method name, if configurable options permit this method fill
1494 * in auth_ident field and return true, otherwise return false.
1497 authmethod_is_enabled(Authmethod *method)
1501 /* return false if options indicate this method is disabled */
1502 if (method->enabled == NULL || *method->enabled == 0)
1504 /* return false if batch mode is enabled but method needs interactive mode */
1505 if (method->batch_flag != NULL && *method->batch_flag != 0)
1511 authmethod_lookup(const char *name)
1513 Authmethod *method = NULL;
1515 for (method = authmethods; method->name != NULL; method++)
1516 if (strcmp(name, method->name) == 0)
1518 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1522 /* XXX internal state */
1523 static Authmethod *current = NULL;
1524 static char *supported = NULL;
1525 static char *preferred = NULL;
1528 * Given the authentication method list sent by the server, return the
1529 * next method we should try. If the server initially sends a nil list,
1530 * use a built-in default list.
1533 authmethod_get(char *authlist)
1538 /* Use a suitable default if we're passed a nil list. */
1539 if (authlist == NULL || strlen(authlist) == 0)
1540 authlist = options.preferred_authentications;
1542 if (supported == NULL || strcmp(authlist, supported) != 0) {
1543 debug3("start over, passed a different list %s", authlist);
1544 if (supported != NULL)
1546 supported = xstrdup(authlist);
1547 preferred = options.preferred_authentications;
1548 debug3("preferred %s", preferred);
1550 } else if (current != NULL && authmethod_is_enabled(current))
1554 if ((name = match_list(preferred, supported, &next)) == NULL) {
1555 debug("No more authentication methods to try.");
1560 debug3("authmethod_lookup %s", name);
1561 debug3("remaining preferred: %s", preferred);
1562 if ((current = authmethod_lookup(name)) != NULL &&
1563 authmethod_is_enabled(current)) {
1564 debug3("authmethod_is_enabled %s", name);
1565 debug("Next authentication method: %s", name);
1572 authmethods_get(void)
1574 Authmethod *method = NULL;
1579 for (method = authmethods; method->name != NULL; method++) {
1580 if (authmethod_is_enabled(method)) {
1581 if (buffer_len(&b) > 0)
1582 buffer_append(&b, ",", 1);
1583 buffer_append(&b, method->name, strlen(method->name));
1586 buffer_append(&b, "\0", 1);
1587 list = xstrdup(buffer_ptr(&b));