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.142 2005/08/30 22:08:05 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)
87 char *orig=NULL, *gss=NULL;
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@openssh.com,zlib,none";
124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,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;
540 static u_int mech = 0;
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 */
577 authctxt->methoddata=(void *)gssctxt;
579 packet_start(SSH2_MSG_USERAUTH_REQUEST);
580 packet_put_cstring(authctxt->server_user);
581 packet_put_cstring(authctxt->service);
582 packet_put_cstring(authctxt->method->name);
586 packet_put_int((gss_supported->elements[mech].length) + 2);
587 packet_put_char(SSH_GSS_OIDTYPE);
588 packet_put_char(gss_supported->elements[mech].length);
589 packet_put_raw(gss_supported->elements[mech].elements,
590 gss_supported->elements[mech].length);
594 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
595 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
596 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
597 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
599 mech++; /* Move along to next candidate */
605 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
607 Authctxt *authctxt = ctxt;
608 Gssctxt *gssctxt = authctxt->methoddata;
609 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
610 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
611 gss_buffer_desc gssbuf;
612 OM_uint32 status, ms, flags;
615 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
616 recv_tok, &send_tok, &flags);
618 if (send_tok.length > 0) {
619 if (GSS_ERROR(status))
620 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
622 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
624 packet_put_string(send_tok.value, send_tok.length);
626 gss_release_buffer(&ms, &send_tok);
629 if (status == GSS_S_COMPLETE) {
630 /* send either complete or MIC, depending on mechanism */
631 if (strcmp(authctxt->method->name,"gssapi")==0 ||
632 (!(flags & GSS_C_INTEG_FLAG))) {
633 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
636 ssh_gssapi_buildmic(&b, authctxt->server_user,
637 authctxt->service, "gssapi-with-mic");
639 gssbuf.value = buffer_ptr(&b);
640 gssbuf.length = buffer_len(&b);
642 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
644 if (!GSS_ERROR(status)) {
645 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
646 packet_put_string(mic.value, mic.length);
652 gss_release_buffer(&ms, &mic);
660 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
662 Authctxt *authctxt = ctxt;
667 if (authctxt == NULL)
668 fatal("input_gssapi_response: no authentication context");
669 gssctxt = authctxt->methoddata;
672 oidv = packet_get_string(&oidlen);
675 oidv[0] != SSH_GSS_OIDTYPE ||
676 oidv[1] != oidlen - 2) {
678 debug("Badly encoded mechanism OID received");
679 userauth(authctxt, NULL);
683 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
684 fatal("Server returned different OID than expected");
690 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
691 /* Start again with next method on list */
692 debug("Trying to start again");
693 userauth(authctxt, NULL);
699 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
701 Authctxt *authctxt = ctxt;
702 gss_buffer_desc recv_tok;
706 if (authctxt == NULL)
707 fatal("input_gssapi_response: no authentication context");
709 recv_tok.value = packet_get_string(&slen);
710 recv_tok.length = slen; /* safe typecast */
714 status = process_gssapi_token(ctxt, &recv_tok);
716 xfree(recv_tok.value);
718 if (GSS_ERROR(status)) {
719 /* Start again with the next method in the list */
720 userauth(authctxt, NULL);
726 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
728 Authctxt *authctxt = ctxt;
730 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
731 gss_buffer_desc recv_tok;
732 OM_uint32 status, ms;
735 if (authctxt == NULL)
736 fatal("input_gssapi_response: no authentication context");
737 gssctxt = authctxt->methoddata;
739 recv_tok.value = packet_get_string(&len);
740 recv_tok.length = len;
744 /* Stick it into GSSAPI and see what it says */
745 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
746 &recv_tok, &send_tok, NULL);
748 xfree(recv_tok.value);
749 gss_release_buffer(&ms, &send_tok);
751 /* Server will be returning a failed packet after this one */
755 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
761 maj=packet_get_int();
762 min=packet_get_int();
763 msg=packet_get_string(NULL);
764 lang=packet_get_string(NULL);
768 debug("Server GSSAPI Error:\n%s\n", msg);
774 userauth_external(Authctxt *authctxt)
776 static int attempt = 0;
781 /* The client MUST NOT try this method if initial key exchange
782 was not performed using a GSSAPI-based key exchange
784 if (xxx_kex->kex_type != KEX_GSS_GRP1_SHA1) {
785 debug2("gsskex not performed, skipping external-keyx");
789 debug2("userauth_external");
790 packet_start(SSH2_MSG_USERAUTH_REQUEST);
792 if(options.implicit) {
793 packet_put_cstring("");
796 packet_put_cstring(authctxt->server_user);
800 packet_put_cstring(authctxt->service);
801 packet_put_cstring(authctxt->method->name);
809 userauth_none(Authctxt *authctxt)
811 /* initial userauth request */
812 packet_start(SSH2_MSG_USERAUTH_REQUEST);
813 packet_put_cstring(authctxt->server_user);
814 packet_put_cstring(authctxt->service);
815 packet_put_cstring(authctxt->method->name);
821 userauth_passwd(Authctxt *authctxt)
823 static int attempt = 0;
827 if (attempt++ >= options.number_of_password_prompts)
831 error("Permission denied, please try again.");
833 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
834 authctxt->server_user, authctxt->host);
835 password = read_passphrase(prompt, 0);
836 packet_start(SSH2_MSG_USERAUTH_REQUEST);
837 packet_put_cstring(authctxt->server_user);
838 packet_put_cstring(authctxt->service);
839 packet_put_cstring(authctxt->method->name);
841 packet_put_cstring(password);
842 memset(password, 0, strlen(password));
844 packet_add_padding(64);
847 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
848 &input_userauth_passwd_changereq);
853 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
856 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
858 Authctxt *authctxt = ctxt;
859 char *info, *lang, *password = NULL, *retype = NULL;
862 debug2("input_userauth_passwd_changereq");
864 if (authctxt == NULL)
865 fatal("input_userauth_passwd_changereq: "
866 "no authentication context");
868 info = packet_get_string(NULL);
869 lang = packet_get_string(NULL);
870 if (strlen(info) > 0)
874 packet_start(SSH2_MSG_USERAUTH_REQUEST);
875 packet_put_cstring(authctxt->server_user);
876 packet_put_cstring(authctxt->service);
877 packet_put_cstring(authctxt->method->name);
878 packet_put_char(1); /* additional info */
879 snprintf(prompt, sizeof(prompt),
880 "Enter %.30s@%.128s's old password: ",
881 authctxt->server_user, authctxt->host);
882 password = read_passphrase(prompt, 0);
883 packet_put_cstring(password);
884 memset(password, 0, strlen(password));
887 while (password == NULL) {
888 snprintf(prompt, sizeof(prompt),
889 "Enter %.30s@%.128s's new password: ",
890 authctxt->server_user, authctxt->host);
891 password = read_passphrase(prompt, RP_ALLOW_EOF);
892 if (password == NULL) {
896 snprintf(prompt, sizeof(prompt),
897 "Retype %.30s@%.128s's new password: ",
898 authctxt->server_user, authctxt->host);
899 retype = read_passphrase(prompt, 0);
900 if (strcmp(password, retype) != 0) {
901 memset(password, 0, strlen(password));
903 logit("Mismatch; try again, EOF to quit.");
906 memset(retype, 0, strlen(retype));
909 packet_put_cstring(password);
910 memset(password, 0, strlen(password));
912 packet_add_padding(64);
915 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
916 &input_userauth_passwd_changereq);
920 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
921 u_char *data, u_int datalen)
926 /* the agent supports this key */
928 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
931 * we have already loaded the private key or
932 * the private key is stored in external hardware
934 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
935 return (key_sign(id->key, sigp, lenp, data, datalen));
936 /* load the private key from the file */
937 if ((prv = load_identity_file(id->filename)) == NULL)
939 ret = key_sign(prv, sigp, lenp, data, datalen);
945 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
948 u_char *blob, *signature;
954 debug3("sign_and_send_pubkey");
956 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
957 /* we cannot handle this key */
958 debug3("sign_and_send_pubkey: cannot handle key");
961 /* data to be signed */
963 if (datafellows & SSH_OLD_SESSIONID) {
964 buffer_append(&b, session_id2, session_id2_len);
965 skip = session_id2_len;
967 buffer_put_string(&b, session_id2, session_id2_len);
968 skip = buffer_len(&b);
970 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
971 buffer_put_cstring(&b, authctxt->server_user);
972 buffer_put_cstring(&b,
973 datafellows & SSH_BUG_PKSERVICE ?
976 if (datafellows & SSH_BUG_PKAUTH) {
977 buffer_put_char(&b, have_sig);
979 buffer_put_cstring(&b, authctxt->method->name);
980 buffer_put_char(&b, have_sig);
981 buffer_put_cstring(&b, key_ssh_name(id->key));
983 buffer_put_string(&b, blob, bloblen);
985 /* generate signature */
986 ret = identity_sign(id, &signature, &slen,
987 buffer_ptr(&b), buffer_len(&b));
996 if (datafellows & SSH_BUG_PKSERVICE) {
998 buffer_append(&b, session_id2, session_id2_len);
999 skip = session_id2_len;
1000 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1001 buffer_put_cstring(&b, authctxt->server_user);
1002 buffer_put_cstring(&b, authctxt->service);
1003 buffer_put_cstring(&b, authctxt->method->name);
1004 buffer_put_char(&b, have_sig);
1005 if (!(datafellows & SSH_BUG_PKAUTH))
1006 buffer_put_cstring(&b, key_ssh_name(id->key));
1007 buffer_put_string(&b, blob, bloblen);
1011 /* append signature */
1012 buffer_put_string(&b, signature, slen);
1015 /* skip session id and packet type */
1016 if (buffer_len(&b) < skip + 1)
1017 fatal("userauth_pubkey: internal error");
1018 buffer_consume(&b, skip + 1);
1020 /* put remaining data from buffer into packet */
1021 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1022 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1030 send_pubkey_test(Authctxt *authctxt, Identity *id)
1033 u_int bloblen, have_sig = 0;
1035 debug3("send_pubkey_test");
1037 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1038 /* we cannot handle this key */
1039 debug3("send_pubkey_test: cannot handle key");
1042 /* register callback for USERAUTH_PK_OK message */
1043 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1045 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1046 packet_put_cstring(authctxt->server_user);
1047 packet_put_cstring(authctxt->service);
1048 packet_put_cstring(authctxt->method->name);
1049 packet_put_char(have_sig);
1050 if (!(datafellows & SSH_BUG_PKAUTH))
1051 packet_put_cstring(key_ssh_name(id->key));
1052 packet_put_string(blob, bloblen);
1059 load_identity_file(char *filename)
1062 char prompt[300], *passphrase;
1066 if (stat(filename, &st) < 0) {
1067 debug3("no such identity: %s", filename);
1070 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1071 if (private == NULL) {
1072 if (options.batch_mode)
1074 snprintf(prompt, sizeof prompt,
1075 "Enter passphrase for key '%.100s': ", filename);
1076 for (i = 0; i < options.number_of_password_prompts; i++) {
1077 passphrase = read_passphrase(prompt, 0);
1078 if (strcmp(passphrase, "") != 0) {
1079 private = key_load_private_type(KEY_UNSPEC, filename,
1083 debug2("no passphrase given, try next key");
1086 memset(passphrase, 0, strlen(passphrase));
1088 if (private != NULL || quit)
1090 debug2("bad passphrase given, try again...");
1097 * try keys in the following order:
1098 * 1. agent keys that are found in the config file
1099 * 2. other agent keys
1100 * 3. keys that are only listed in the config file
1103 pubkey_prepare(Authctxt *authctxt)
1106 Idlist agent, files, *preferred;
1108 AuthenticationConnection *ac;
1112 TAILQ_INIT(&agent); /* keys from the agent */
1113 TAILQ_INIT(&files); /* keys from the config file */
1114 preferred = &authctxt->keys;
1115 TAILQ_INIT(preferred); /* preferred order of keys */
1117 /* list of keys stored in the filesystem */
1118 for (i = 0; i < options.num_identity_files; i++) {
1119 key = options.identity_keys[i];
1120 if (key && key->type == KEY_RSA1)
1122 options.identity_keys[i] = NULL;
1123 id = xmalloc(sizeof(*id));
1124 memset(id, 0, sizeof(*id));
1126 id->filename = xstrdup(options.identity_files[i]);
1127 TAILQ_INSERT_TAIL(&files, id, next);
1129 /* list of keys supported by the agent */
1130 if ((ac = ssh_get_authentication_connection())) {
1131 for (key = ssh_get_first_identity(ac, &comment, 2);
1133 key = ssh_get_next_identity(ac, &comment, 2)) {
1135 TAILQ_FOREACH(id, &files, next) {
1136 /* agent keys from the config file are preferred */
1137 if (key_equal(key, id->key)) {
1140 TAILQ_REMOVE(&files, id, next);
1141 TAILQ_INSERT_TAIL(preferred, id, next);
1147 if (!found && !options.identities_only) {
1148 id = xmalloc(sizeof(*id));
1149 memset(id, 0, sizeof(*id));
1151 id->filename = comment;
1153 TAILQ_INSERT_TAIL(&agent, id, next);
1156 /* append remaining agent keys */
1157 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1158 TAILQ_REMOVE(&agent, id, next);
1159 TAILQ_INSERT_TAIL(preferred, id, next);
1161 authctxt->agent = ac;
1163 /* append remaining keys from the config file */
1164 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1165 TAILQ_REMOVE(&files, id, next);
1166 TAILQ_INSERT_TAIL(preferred, id, next);
1168 TAILQ_FOREACH(id, preferred, next) {
1169 debug2("key: %s (%p)", id->filename, id->key);
1174 pubkey_cleanup(Authctxt *authctxt)
1178 if (authctxt->agent != NULL)
1179 ssh_close_authentication_connection(authctxt->agent);
1180 for (id = TAILQ_FIRST(&authctxt->keys); id;
1181 id = TAILQ_FIRST(&authctxt->keys)) {
1182 TAILQ_REMOVE(&authctxt->keys, id, next);
1186 xfree(id->filename);
1192 userauth_pubkey(Authctxt *authctxt)
1197 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1200 /* move key to the end of the queue */
1201 TAILQ_REMOVE(&authctxt->keys, id, next);
1202 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1204 * send a test message if we have the public key. for
1205 * encrypted keys we cannot do this and have to load the
1206 * private key instead
1208 if (id->key && id->key->type != KEY_RSA1) {
1209 debug("Offering public key: %s", id->filename);
1210 sent = send_pubkey_test(authctxt, id);
1211 } else if (id->key == NULL) {
1212 debug("Trying private key: %s", id->filename);
1213 id->key = load_identity_file(id->filename);
1214 if (id->key != NULL) {
1216 sent = sign_and_send_pubkey(authctxt, id);
1228 * Send userauth request message specifying keyboard-interactive method.
1231 userauth_kbdint(Authctxt *authctxt)
1233 static int attempt = 0;
1235 if (attempt++ >= options.number_of_password_prompts)
1237 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1238 if (attempt > 1 && !authctxt->info_req_seen) {
1239 debug3("userauth_kbdint: disable: no info_req_seen");
1240 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1244 debug2("userauth_kbdint");
1245 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1246 packet_put_cstring(authctxt->server_user);
1247 packet_put_cstring(authctxt->service);
1248 packet_put_cstring(authctxt->method->name);
1249 packet_put_cstring(""); /* lang */
1250 packet_put_cstring(options.kbd_interactive_devices ?
1251 options.kbd_interactive_devices : "");
1254 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1259 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1262 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1264 Authctxt *authctxt = ctxt;
1265 char *name, *inst, *lang, *prompt, *response;
1266 u_int num_prompts, i;
1269 debug2("input_userauth_info_req");
1271 if (authctxt == NULL)
1272 fatal("input_userauth_info_req: no authentication context");
1274 authctxt->info_req_seen = 1;
1276 name = packet_get_string(NULL);
1277 inst = packet_get_string(NULL);
1278 lang = packet_get_string(NULL);
1279 if (strlen(name) > 0)
1281 if (strlen(inst) > 0)
1287 num_prompts = packet_get_int();
1289 * Begin to build info response packet based on prompts requested.
1290 * We commit to providing the correct number of responses, so if
1291 * further on we run into a problem that prevents this, we have to
1292 * be sure and clean this up and send a correct error response.
1294 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1295 packet_put_int(num_prompts);
1297 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1298 for (i = 0; i < num_prompts; i++) {
1299 prompt = packet_get_string(NULL);
1300 echo = packet_get_char();
1302 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1304 packet_put_cstring(response);
1305 memset(response, 0, strlen(response));
1309 packet_check_eom(); /* done with parsing incoming message. */
1311 packet_add_padding(64);
1316 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1317 u_char *data, u_int datalen)
1322 int to[2], from[2], status, version = 2;
1324 debug2("ssh_keysign called");
1326 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1327 error("ssh_keysign: no installed: %s", strerror(errno));
1330 if (fflush(stdout) != 0)
1331 error("ssh_keysign: fflush: %s", strerror(errno));
1333 error("ssh_keysign: pipe: %s", strerror(errno));
1336 if (pipe(from) < 0) {
1337 error("ssh_keysign: pipe: %s", strerror(errno));
1340 if ((pid = fork()) < 0) {
1341 error("ssh_keysign: fork: %s", strerror(errno));
1348 if (dup2(from[1], STDOUT_FILENO) < 0)
1349 fatal("ssh_keysign: dup2: %s", strerror(errno));
1351 if (dup2(to[0], STDIN_FILENO) < 0)
1352 fatal("ssh_keysign: dup2: %s", strerror(errno));
1355 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1356 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1363 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1364 buffer_put_string(&b, data, datalen);
1365 if (ssh_msg_send(to[1], version, &b) == -1)
1366 fatal("ssh_keysign: couldn't send request");
1368 if (ssh_msg_recv(from[0], &b) < 0) {
1369 error("ssh_keysign: no reply");
1376 while (waitpid(pid, &status, 0) < 0)
1380 if (buffer_get_char(&b) != version) {
1381 error("ssh_keysign: bad version");
1385 *sigp = buffer_get_string(&b, lenp);
1392 userauth_hostbased(Authctxt *authctxt)
1394 Key *private = NULL;
1395 Sensitive *sensitive = authctxt->sensitive;
1397 u_char *signature, *blob;
1398 char *chost, *pkalg, *p;
1399 const char *service;
1401 int ok, i, len, found = 0;
1403 /* check for a useful key */
1404 for (i = 0; i < sensitive->nkeys; i++) {
1405 private = sensitive->keys[i];
1406 if (private && private->type != KEY_RSA1) {
1408 /* we take and free the key */
1409 sensitive->keys[i] = NULL;
1414 debug("No more client hostkeys for hostbased authentication.");
1417 if (key_to_blob(private, &blob, &blen) == 0) {
1421 /* figure out a name for the client host */
1422 p = get_local_name(packet_get_connection_in());
1424 error("userauth_hostbased: cannot get local ipaddr/name");
1428 len = strlen(p) + 2;
1429 chost = xmalloc(len);
1430 strlcpy(chost, p, len);
1431 strlcat(chost, ".", len);
1432 debug2("userauth_hostbased: chost %s", chost);
1435 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1437 pkalg = xstrdup(key_ssh_name(private));
1439 /* construct data */
1440 buffer_put_string(&b, session_id2, session_id2_len);
1441 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1442 buffer_put_cstring(&b, authctxt->server_user);
1443 buffer_put_cstring(&b, service);
1444 buffer_put_cstring(&b, authctxt->method->name);
1445 buffer_put_cstring(&b, pkalg);
1446 buffer_put_string(&b, blob, blen);
1447 buffer_put_cstring(&b, chost);
1448 buffer_put_cstring(&b, authctxt->local_user);
1452 if (sensitive->external_keysign)
1453 ok = ssh_keysign(private, &signature, &slen,
1454 buffer_ptr(&b), buffer_len(&b));
1456 ok = key_sign(private, &signature, &slen,
1457 buffer_ptr(&b), buffer_len(&b));
1461 error("key_sign failed");
1466 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1467 packet_put_cstring(authctxt->server_user);
1468 packet_put_cstring(authctxt->service);
1469 packet_put_cstring(authctxt->method->name);
1470 packet_put_cstring(pkalg);
1471 packet_put_string(blob, blen);
1472 packet_put_cstring(chost);
1473 packet_put_cstring(authctxt->local_user);
1474 packet_put_string(signature, slen);
1475 memset(signature, 's', slen);
1484 /* find auth method */
1487 * given auth method name, if configurable options permit this method fill
1488 * in auth_ident field and return true, otherwise return false.
1491 authmethod_is_enabled(Authmethod *method)
1495 /* return false if options indicate this method is disabled */
1496 if (method->enabled == NULL || *method->enabled == 0)
1498 /* return false if batch mode is enabled but method needs interactive mode */
1499 if (method->batch_flag != NULL && *method->batch_flag != 0)
1505 authmethod_lookup(const char *name)
1507 Authmethod *method = NULL;
1509 for (method = authmethods; method->name != NULL; method++)
1510 if (strcmp(name, method->name) == 0)
1512 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1516 /* XXX internal state */
1517 static Authmethod *current = NULL;
1518 static char *supported = NULL;
1519 static char *preferred = NULL;
1522 * Given the authentication method list sent by the server, return the
1523 * next method we should try. If the server initially sends a nil list,
1524 * use a built-in default list.
1527 authmethod_get(char *authlist)
1532 /* Use a suitable default if we're passed a nil list. */
1533 if (authlist == NULL || strlen(authlist) == 0)
1534 authlist = options.preferred_authentications;
1536 if (supported == NULL || strcmp(authlist, supported) != 0) {
1537 debug3("start over, passed a different list %s", authlist);
1538 if (supported != NULL)
1540 supported = xstrdup(authlist);
1541 preferred = options.preferred_authentications;
1542 debug3("preferred %s", preferred);
1544 } else if (current != NULL && authmethod_is_enabled(current))
1548 if ((name = match_list(preferred, supported, &next)) == NULL) {
1549 debug("No more authentication methods to try.");
1554 debug3("authmethod_lookup %s", name);
1555 debug3("remaining preferred: %s", preferred);
1556 if ((current = authmethod_lookup(name)) != NULL &&
1557 authmethod_is_enabled(current)) {
1558 debug3("authmethod_is_enabled %s", name);
1559 debug("Next authentication method: %s", name);
1566 authmethods_get(void)
1568 Authmethod *method = NULL;
1573 for (method = authmethods; method->name != NULL; method++) {
1574 if (authmethod_is_enabled(method)) {
1575 if (buffer_len(&b) > 0)
1576 buffer_append(&b, ",", 1);
1577 buffer_append(&b, method->name, strlen(method->name));
1580 buffer_append(&b, "\0", 1);
1581 list = xstrdup(buffer_ptr(&b));