1 /* $OpenBSD: sshconnect2.c,v 1.162 2006/08/30 00:06:51 dtucker Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/socket.h>
41 #include "openbsd-compat/sys-queue.h"
52 #include "myproposal.h"
53 #include "sshconnect.h"
64 #include "pathnames.h"
72 extern char *client_version_string;
73 extern char *server_version_string;
74 extern Options options;
77 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
78 /* if it is set then prevent the switch to the null cipher */
86 u_char *session_id2 = NULL;
87 u_int session_id2_len = 0;
90 struct sockaddr *xxx_hostaddr;
95 verify_host_key_callback(Key *hostkey)
97 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
98 fatal("Host key verification failed.");
103 ssh_kex2(char *host, struct sockaddr *hostaddr)
108 char *orig = NULL, *gss = NULL;
109 char *gss_host = NULL;
113 xxx_hostaddr = hostaddr;
116 if (options.gss_keyex) {
117 /* Add the GSSAPI mechanisms currently supported on this
118 * client to the key exchange algorithm proposal */
119 orig = myproposal[PROPOSAL_KEX_ALGS];
121 if (options.gss_trust_dns)
122 gss_host = (char *)get_canonical_hostname(1);
126 gss = ssh_gssapi_client_mechanisms(gss_host);
128 debug("Offering GSSAPI proposal: %s", gss);
129 xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
135 if (options.ciphers == (char *)-1) {
136 logit("No valid ciphers for protocol version 2 given, using defaults.");
137 options.ciphers = NULL;
139 if (options.ciphers != NULL) {
140 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
141 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
144 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
145 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
146 myproposal[PROPOSAL_ENC_ALGS_STOC] =
147 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
148 if (options.compression) {
149 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
150 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
152 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
153 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
155 if (options.macs != NULL) {
156 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
157 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
159 if (options.hostkeyalgorithms != NULL)
160 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
161 options.hostkeyalgorithms;
164 /* If we've got GSSAPI algorithms, then we also support the
165 * 'null' hostkey, as a last resort */
166 if (options.gss_keyex && gss) {
167 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
168 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
173 if (options.rekey_limit)
174 packet_set_rekey_limit(options.rekey_limit);
176 /* start key exchange */
177 kex = kex_setup(myproposal);
178 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
179 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
180 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
181 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
183 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
184 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
185 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
187 kex->client_version_string=client_version_string;
188 kex->server_version_string=server_version_string;
189 kex->verify_host_key=&verify_host_key_callback;
192 kex->gss_deleg_creds = options.gss_deleg_creds;
193 kex->gss_trust_dns = options.gss_trust_dns;
194 kex->gss_host = gss_host;
199 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
201 session_id2 = kex->session_id;
202 session_id2_len = kex->session_id_len;
205 /* send 1st encrypted/maced/compressed message */
206 packet_start(SSH2_MSG_IGNORE);
207 packet_put_cstring("markus");
217 typedef struct Authctxt Authctxt;
218 typedef struct Authmethod Authmethod;
219 typedef struct identity Identity;
220 typedef struct idlist Idlist;
223 TAILQ_ENTRY(identity) next;
224 AuthenticationConnection *ac; /* set if agent supports key */
225 Key *key; /* public/private key */
226 char *filename; /* comment for agent-only keys */
228 int isprivate; /* key points to the private key */
230 TAILQ_HEAD(idlist, identity);
233 const char *server_user;
234 const char *local_user;
242 AuthenticationConnection *agent;
244 Sensitive *sensitive;
245 /* kbd-interactive */
251 char *name; /* string to compare against server's list */
252 int (*userauth)(Authctxt *authctxt);
253 int *enabled; /* flag in option struct that enables method */
254 int *batch_flag; /* flag in option struct that disables method */
257 void input_userauth_success(int, u_int32_t, void *);
258 void input_userauth_failure(int, u_int32_t, void *);
259 void input_userauth_banner(int, u_int32_t, void *);
260 void input_userauth_error(int, u_int32_t, void *);
261 void input_userauth_info_req(int, u_int32_t, void *);
262 void input_userauth_pk_ok(int, u_int32_t, void *);
263 void input_userauth_passwd_changereq(int, u_int32_t, void *);
265 int userauth_none(Authctxt *);
266 int userauth_pubkey(Authctxt *);
267 int userauth_passwd(Authctxt *);
268 int userauth_kbdint(Authctxt *);
269 int userauth_hostbased(Authctxt *);
270 int userauth_kerberos(Authctxt *);
273 int userauth_gssapi(Authctxt *authctxt);
274 void input_gssapi_response(int type, u_int32_t, void *);
275 void input_gssapi_token(int type, u_int32_t, void *);
276 void input_gssapi_hash(int type, u_int32_t, void *);
277 void input_gssapi_error(int, u_int32_t, void *);
278 void input_gssapi_errtok(int, u_int32_t, void *);
279 int userauth_gsskeyex(Authctxt *authctxt);
282 void userauth(Authctxt *, char *);
284 static int sign_and_send_pubkey(Authctxt *, Identity *);
285 static void pubkey_prepare(Authctxt *);
286 static void pubkey_cleanup(Authctxt *);
287 static Key *load_identity_file(char *);
289 static Authmethod *authmethod_get(char *authlist);
290 static Authmethod *authmethod_lookup(const char *name);
291 static char *authmethods_get(void);
293 Authmethod authmethods[] = {
297 &options.gss_authentication,
301 &options.gss_authentication,
306 &options.hostbased_authentication,
310 &options.pubkey_authentication,
312 {"keyboard-interactive",
314 &options.kbd_interactive_authentication,
315 &options.batch_mode},
318 &options.password_authentication,
319 &options.batch_mode},
324 {NULL, NULL, NULL, NULL}
328 ssh_userauth2(const char *local_user, const char *server_user, char *host,
329 Sensitive *sensitive)
334 if (options.challenge_response_authentication)
335 options.kbd_interactive_authentication = 1;
337 packet_start(SSH2_MSG_SERVICE_REQUEST);
338 packet_put_cstring("ssh-userauth");
340 debug("SSH2_MSG_SERVICE_REQUEST sent");
342 type = packet_read();
343 if (type != SSH2_MSG_SERVICE_ACCEPT)
344 fatal("Server denied authentication request: %d", type);
345 if (packet_remaining() > 0) {
346 char *reply = packet_get_string(NULL);
347 debug2("service_accept: %s", reply);
350 debug2("buggy server: service_accept w/o service");
353 debug("SSH2_MSG_SERVICE_ACCEPT received");
355 if (options.preferred_authentications == NULL)
356 options.preferred_authentications = authmethods_get();
358 /* setup authentication context */
359 memset(&authctxt, 0, sizeof(authctxt));
360 pubkey_prepare(&authctxt);
361 authctxt.server_user = server_user;
362 authctxt.local_user = local_user;
363 authctxt.host = host;
364 authctxt.service = "ssh-connection"; /* service name */
365 authctxt.success = 0;
366 authctxt.method = authmethod_lookup("none");
367 authctxt.authlist = NULL;
368 authctxt.methoddata = NULL;
369 authctxt.sensitive = sensitive;
370 authctxt.info_req_seen = 0;
371 if (authctxt.method == NULL)
372 fatal("ssh_userauth2: internal error: cannot send userauth none request");
374 /* initial userauth request */
375 userauth_none(&authctxt);
377 dispatch_init(&input_userauth_error);
378 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
379 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
380 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
381 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
383 pubkey_cleanup(&authctxt);
384 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
386 /* if the user wants to use the none cipher do it */
387 /* post authentication and only if the right conditions are met */
388 /* both of the NONE commands must be true and there must be no */
390 if ((options.none_switch == 1) && (options.none_enabled == 1))
392 if (!tty_flag) /* no null on tty sessions */
394 debug("Requesting none rekeying...");
395 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
396 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
397 kex_prop2buf(&xxx_kex->my,myproposal);
398 packet_request_rekeying();
399 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
403 /* requested NONE cipher when in a tty */
404 debug("Cannot switch to NONE cipher with tty allocated");
405 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
408 debug("Authentication succeeded (%s).", authctxt.method->name);
412 userauth(Authctxt *authctxt, char *authlist)
414 if (authctxt->methoddata) {
415 xfree(authctxt->methoddata);
416 authctxt->methoddata = NULL;
418 if (authlist == NULL) {
419 authlist = authctxt->authlist;
421 if (authctxt->authlist)
422 xfree(authctxt->authlist);
423 authctxt->authlist = authlist;
426 Authmethod *method = authmethod_get(authlist);
428 fatal("Permission denied (%s).", authlist);
429 authctxt->method = method;
431 /* reset the per method handler */
432 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
433 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
435 /* and try new method */
436 if (method->userauth(authctxt) != 0) {
437 debug2("we sent a %s packet, wait for reply", method->name);
440 debug2("we did not send a packet, disable method");
441 method->enabled = NULL;
447 input_userauth_error(int type, u_int32_t seq, void *ctxt)
449 fatal("input_userauth_error: bad message during authentication: "
454 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
458 debug3("input_userauth_banner");
459 msg = packet_get_string(NULL);
460 lang = packet_get_string(NULL);
461 if (options.log_level >= SYSLOG_LEVEL_INFO)
462 fprintf(stderr, "%s", msg);
468 input_userauth_success(int type, u_int32_t seq, void *ctxt)
470 Authctxt *authctxt = ctxt;
471 if (authctxt == NULL)
472 fatal("input_userauth_success: no authentication context");
473 if (authctxt->authlist) {
474 xfree(authctxt->authlist);
475 authctxt->authlist = NULL;
477 if (authctxt->methoddata) {
478 xfree(authctxt->methoddata);
479 authctxt->methoddata = NULL;
481 authctxt->success = 1; /* break out */
485 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
487 Authctxt *authctxt = ctxt;
488 char *authlist = NULL;
491 if (authctxt == NULL)
492 fatal("input_userauth_failure: no authentication context");
494 authlist = packet_get_string(NULL);
495 partial = packet_get_char();
499 logit("Authenticated with partial success.");
500 debug("Authentications that can continue: %s", authlist);
502 userauth(authctxt, authlist);
505 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
507 Authctxt *authctxt = ctxt;
511 int pktype, sent = 0;
516 if (authctxt == NULL)
517 fatal("input_userauth_pk_ok: no authentication context");
518 if (datafellows & SSH_BUG_PKOK) {
519 /* this is similar to SSH_BUG_PKAUTH */
520 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
521 pkblob = packet_get_string(&blen);
523 buffer_append(&b, pkblob, blen);
524 pkalg = buffer_get_string(&b, &alen);
527 pkalg = packet_get_string(&alen);
528 pkblob = packet_get_string(&blen);
532 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
534 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
535 debug("unknown pkalg %s", pkalg);
538 if ((key = key_from_blob(pkblob, blen)) == NULL) {
539 debug("no key from blob. pkalg %s", pkalg);
542 if (key->type != pktype) {
543 error("input_userauth_pk_ok: type mismatch "
544 "for decoded key (received %d, expected %d)",
548 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
549 debug2("input_userauth_pk_ok: fp %s", fp);
553 * search keys in the reverse order, because last candidate has been
554 * moved to the end of the queue. this also avoids confusion by
557 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
558 if (key_equal(key, id->key)) {
559 sent = sign_and_send_pubkey(authctxt, id);
569 /* try another method if we did not send a packet */
571 userauth(authctxt, NULL);
576 userauth_gssapi(Authctxt *authctxt)
578 Gssctxt *gssctxt = NULL;
579 static gss_OID_set gss_supported = NULL;
580 static u_int mech = 0;
583 char *gss_host = NULL;
585 if (options.gss_trust_dns)
586 gss_host = (char *)get_canonical_hostname(1);
588 gss_host = (char *)authctxt->host;
590 /* Try one GSSAPI method at a time, rather than sending them all at
593 if (gss_supported == NULL)
594 gss_indicate_mechs(&min, &gss_supported);
596 /* Check to see if the mechanism is usable before we offer it */
597 while (mech < gss_supported->count && !ok) {
598 /* My DER encoding requires length<128 */
599 if (gss_supported->elements[mech].length < 128 &&
600 ssh_gssapi_check_mechanism(&gssctxt,
601 &gss_supported->elements[mech], gss_host)) {
602 ok = 1; /* Mechanism works */
611 authctxt->methoddata=(void *)gssctxt;
613 packet_start(SSH2_MSG_USERAUTH_REQUEST);
614 packet_put_cstring(authctxt->server_user);
615 packet_put_cstring(authctxt->service);
616 packet_put_cstring(authctxt->method->name);
620 packet_put_int((gss_supported->elements[mech].length) + 2);
621 packet_put_char(SSH_GSS_OIDTYPE);
622 packet_put_char(gss_supported->elements[mech].length);
623 packet_put_raw(gss_supported->elements[mech].elements,
624 gss_supported->elements[mech].length);
628 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
629 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
630 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
631 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
633 mech++; /* Move along to next candidate */
639 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
641 Authctxt *authctxt = ctxt;
642 Gssctxt *gssctxt = authctxt->methoddata;
643 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
644 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
645 gss_buffer_desc gssbuf;
646 OM_uint32 status, ms, flags;
649 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
650 recv_tok, &send_tok, &flags);
652 if (send_tok.length > 0) {
653 if (GSS_ERROR(status))
654 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
656 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
658 packet_put_string(send_tok.value, send_tok.length);
660 gss_release_buffer(&ms, &send_tok);
663 if (status == GSS_S_COMPLETE) {
664 /* send either complete or MIC, depending on mechanism */
665 if (!(flags & GSS_C_INTEG_FLAG)) {
666 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
669 ssh_gssapi_buildmic(&b, authctxt->server_user,
670 authctxt->service, "gssapi-with-mic");
672 gssbuf.value = buffer_ptr(&b);
673 gssbuf.length = buffer_len(&b);
675 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
677 if (!GSS_ERROR(status)) {
678 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
679 packet_put_string(mic.value, mic.length);
685 gss_release_buffer(&ms, &mic);
693 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
695 Authctxt *authctxt = ctxt;
700 if (authctxt == NULL)
701 fatal("input_gssapi_response: no authentication context");
702 gssctxt = authctxt->methoddata;
705 oidv = packet_get_string(&oidlen);
708 oidv[0] != SSH_GSS_OIDTYPE ||
709 oidv[1] != oidlen - 2) {
711 debug("Badly encoded mechanism OID received");
712 userauth(authctxt, NULL);
716 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
717 fatal("Server returned different OID than expected");
723 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
724 /* Start again with next method on list */
725 debug("Trying to start again");
726 userauth(authctxt, NULL);
732 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
734 Authctxt *authctxt = ctxt;
735 gss_buffer_desc recv_tok;
739 if (authctxt == NULL)
740 fatal("input_gssapi_response: no authentication context");
742 recv_tok.value = packet_get_string(&slen);
743 recv_tok.length = slen; /* safe typecast */
747 status = process_gssapi_token(ctxt, &recv_tok);
749 xfree(recv_tok.value);
751 if (GSS_ERROR(status)) {
752 /* Start again with the next method in the list */
753 userauth(authctxt, NULL);
759 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
761 Authctxt *authctxt = ctxt;
763 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
764 gss_buffer_desc recv_tok;
765 OM_uint32 status, ms;
768 if (authctxt == NULL)
769 fatal("input_gssapi_response: no authentication context");
770 gssctxt = authctxt->methoddata;
772 recv_tok.value = packet_get_string(&len);
773 recv_tok.length = len;
777 /* Stick it into GSSAPI and see what it says */
778 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
779 &recv_tok, &send_tok, NULL);
781 xfree(recv_tok.value);
782 gss_release_buffer(&ms, &send_tok);
784 /* Server will be returning a failed packet after this one */
788 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
794 maj=packet_get_int();
795 min=packet_get_int();
796 msg=packet_get_string(NULL);
797 lang=packet_get_string(NULL);
801 debug("Server GSSAPI Error:\n%s", msg);
807 userauth_gsskeyex(Authctxt *authctxt)
810 gss_buffer_desc gssbuf;
811 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
814 static int attempt = 0;
818 if (gss_kex_context == NULL) {
819 debug("No valid Key exchange context");
823 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
826 gssbuf.value = buffer_ptr(&b);
827 gssbuf.length = buffer_len(&b);
829 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
834 packet_start(SSH2_MSG_USERAUTH_REQUEST);
835 packet_put_cstring(authctxt->server_user);
836 packet_put_cstring(authctxt->service);
837 packet_put_cstring(authctxt->method->name);
838 packet_put_string(mic.value, mic.length);
842 gss_release_buffer(&ms, &mic);
850 userauth_none(Authctxt *authctxt)
852 /* initial userauth request */
853 packet_start(SSH2_MSG_USERAUTH_REQUEST);
854 packet_put_cstring(authctxt->server_user);
855 packet_put_cstring(authctxt->service);
856 packet_put_cstring(authctxt->method->name);
862 userauth_passwd(Authctxt *authctxt)
864 static int attempt = 0;
868 if (attempt++ >= options.number_of_password_prompts)
872 error("Permission denied, please try again.");
874 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
875 authctxt->server_user, authctxt->host);
876 password = read_passphrase(prompt, 0);
877 packet_start(SSH2_MSG_USERAUTH_REQUEST);
878 packet_put_cstring(authctxt->server_user);
879 packet_put_cstring(authctxt->service);
880 packet_put_cstring(authctxt->method->name);
882 packet_put_cstring(password);
883 memset(password, 0, strlen(password));
885 packet_add_padding(64);
888 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
889 &input_userauth_passwd_changereq);
894 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
897 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
899 Authctxt *authctxt = ctxt;
900 char *info, *lang, *password = NULL, *retype = NULL;
903 debug2("input_userauth_passwd_changereq");
905 if (authctxt == NULL)
906 fatal("input_userauth_passwd_changereq: "
907 "no authentication context");
909 info = packet_get_string(NULL);
910 lang = packet_get_string(NULL);
911 if (strlen(info) > 0)
915 packet_start(SSH2_MSG_USERAUTH_REQUEST);
916 packet_put_cstring(authctxt->server_user);
917 packet_put_cstring(authctxt->service);
918 packet_put_cstring(authctxt->method->name);
919 packet_put_char(1); /* additional info */
920 snprintf(prompt, sizeof(prompt),
921 "Enter %.30s@%.128s's old password: ",
922 authctxt->server_user, authctxt->host);
923 password = read_passphrase(prompt, 0);
924 packet_put_cstring(password);
925 memset(password, 0, strlen(password));
928 while (password == NULL) {
929 snprintf(prompt, sizeof(prompt),
930 "Enter %.30s@%.128s's new password: ",
931 authctxt->server_user, authctxt->host);
932 password = read_passphrase(prompt, RP_ALLOW_EOF);
933 if (password == NULL) {
937 snprintf(prompt, sizeof(prompt),
938 "Retype %.30s@%.128s's new password: ",
939 authctxt->server_user, authctxt->host);
940 retype = read_passphrase(prompt, 0);
941 if (strcmp(password, retype) != 0) {
942 memset(password, 0, strlen(password));
944 logit("Mismatch; try again, EOF to quit.");
947 memset(retype, 0, strlen(retype));
950 packet_put_cstring(password);
951 memset(password, 0, strlen(password));
953 packet_add_padding(64);
956 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
957 &input_userauth_passwd_changereq);
961 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
962 u_char *data, u_int datalen)
967 /* the agent supports this key */
969 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
972 * we have already loaded the private key or
973 * the private key is stored in external hardware
975 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
976 return (key_sign(id->key, sigp, lenp, data, datalen));
977 /* load the private key from the file */
978 if ((prv = load_identity_file(id->filename)) == NULL)
980 ret = key_sign(prv, sigp, lenp, data, datalen);
986 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
989 u_char *blob, *signature;
995 debug3("sign_and_send_pubkey");
997 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
998 /* we cannot handle this key */
999 debug3("sign_and_send_pubkey: cannot handle key");
1002 /* data to be signed */
1004 if (datafellows & SSH_OLD_SESSIONID) {
1005 buffer_append(&b, session_id2, session_id2_len);
1006 skip = session_id2_len;
1008 buffer_put_string(&b, session_id2, session_id2_len);
1009 skip = buffer_len(&b);
1011 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1012 buffer_put_cstring(&b, authctxt->server_user);
1013 buffer_put_cstring(&b,
1014 datafellows & SSH_BUG_PKSERVICE ?
1017 if (datafellows & SSH_BUG_PKAUTH) {
1018 buffer_put_char(&b, have_sig);
1020 buffer_put_cstring(&b, authctxt->method->name);
1021 buffer_put_char(&b, have_sig);
1022 buffer_put_cstring(&b, key_ssh_name(id->key));
1024 buffer_put_string(&b, blob, bloblen);
1026 /* generate signature */
1027 ret = identity_sign(id, &signature, &slen,
1028 buffer_ptr(&b), buffer_len(&b));
1037 if (datafellows & SSH_BUG_PKSERVICE) {
1039 buffer_append(&b, session_id2, session_id2_len);
1040 skip = session_id2_len;
1041 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1042 buffer_put_cstring(&b, authctxt->server_user);
1043 buffer_put_cstring(&b, authctxt->service);
1044 buffer_put_cstring(&b, authctxt->method->name);
1045 buffer_put_char(&b, have_sig);
1046 if (!(datafellows & SSH_BUG_PKAUTH))
1047 buffer_put_cstring(&b, key_ssh_name(id->key));
1048 buffer_put_string(&b, blob, bloblen);
1052 /* append signature */
1053 buffer_put_string(&b, signature, slen);
1056 /* skip session id and packet type */
1057 if (buffer_len(&b) < skip + 1)
1058 fatal("userauth_pubkey: internal error");
1059 buffer_consume(&b, skip + 1);
1061 /* put remaining data from buffer into packet */
1062 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1063 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1071 send_pubkey_test(Authctxt *authctxt, Identity *id)
1074 u_int bloblen, have_sig = 0;
1076 debug3("send_pubkey_test");
1078 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1079 /* we cannot handle this key */
1080 debug3("send_pubkey_test: cannot handle key");
1083 /* register callback for USERAUTH_PK_OK message */
1084 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1086 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1087 packet_put_cstring(authctxt->server_user);
1088 packet_put_cstring(authctxt->service);
1089 packet_put_cstring(authctxt->method->name);
1090 packet_put_char(have_sig);
1091 if (!(datafellows & SSH_BUG_PKAUTH))
1092 packet_put_cstring(key_ssh_name(id->key));
1093 packet_put_string(blob, bloblen);
1100 load_identity_file(char *filename)
1103 char prompt[300], *passphrase;
1104 int perm_ok, quit, i;
1107 if (stat(filename, &st) < 0) {
1108 debug3("no such identity: %s", filename);
1111 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1114 if (private == NULL) {
1115 if (options.batch_mode)
1117 snprintf(prompt, sizeof prompt,
1118 "Enter passphrase for key '%.100s': ", filename);
1119 for (i = 0; i < options.number_of_password_prompts; i++) {
1120 passphrase = read_passphrase(prompt, 0);
1121 if (strcmp(passphrase, "") != 0) {
1122 private = key_load_private_type(KEY_UNSPEC,
1123 filename, passphrase, NULL, NULL);
1126 debug2("no passphrase given, try next key");
1129 memset(passphrase, 0, strlen(passphrase));
1131 if (private != NULL || quit)
1133 debug2("bad passphrase given, try again...");
1140 * try keys in the following order:
1141 * 1. agent keys that are found in the config file
1142 * 2. other agent keys
1143 * 3. keys that are only listed in the config file
1146 pubkey_prepare(Authctxt *authctxt)
1149 Idlist agent, files, *preferred;
1151 AuthenticationConnection *ac;
1155 TAILQ_INIT(&agent); /* keys from the agent */
1156 TAILQ_INIT(&files); /* keys from the config file */
1157 preferred = &authctxt->keys;
1158 TAILQ_INIT(preferred); /* preferred order of keys */
1160 /* list of keys stored in the filesystem */
1161 for (i = 0; i < options.num_identity_files; i++) {
1162 key = options.identity_keys[i];
1163 if (key && key->type == KEY_RSA1)
1165 options.identity_keys[i] = NULL;
1166 id = xcalloc(1, sizeof(*id));
1168 id->filename = xstrdup(options.identity_files[i]);
1169 TAILQ_INSERT_TAIL(&files, id, next);
1171 /* list of keys supported by the agent */
1172 if ((ac = ssh_get_authentication_connection())) {
1173 for (key = ssh_get_first_identity(ac, &comment, 2);
1175 key = ssh_get_next_identity(ac, &comment, 2)) {
1177 TAILQ_FOREACH(id, &files, next) {
1178 /* agent keys from the config file are preferred */
1179 if (key_equal(key, id->key)) {
1182 TAILQ_REMOVE(&files, id, next);
1183 TAILQ_INSERT_TAIL(preferred, id, next);
1189 if (!found && !options.identities_only) {
1190 id = xcalloc(1, sizeof(*id));
1192 id->filename = comment;
1194 TAILQ_INSERT_TAIL(&agent, id, next);
1197 /* append remaining agent keys */
1198 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1199 TAILQ_REMOVE(&agent, id, next);
1200 TAILQ_INSERT_TAIL(preferred, id, next);
1202 authctxt->agent = ac;
1204 /* append remaining keys from the config file */
1205 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1206 TAILQ_REMOVE(&files, id, next);
1207 TAILQ_INSERT_TAIL(preferred, id, next);
1209 TAILQ_FOREACH(id, preferred, next) {
1210 debug2("key: %s (%p)", id->filename, id->key);
1215 pubkey_cleanup(Authctxt *authctxt)
1219 if (authctxt->agent != NULL)
1220 ssh_close_authentication_connection(authctxt->agent);
1221 for (id = TAILQ_FIRST(&authctxt->keys); id;
1222 id = TAILQ_FIRST(&authctxt->keys)) {
1223 TAILQ_REMOVE(&authctxt->keys, id, next);
1227 xfree(id->filename);
1233 userauth_pubkey(Authctxt *authctxt)
1238 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1241 /* move key to the end of the queue */
1242 TAILQ_REMOVE(&authctxt->keys, id, next);
1243 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1245 * send a test message if we have the public key. for
1246 * encrypted keys we cannot do this and have to load the
1247 * private key instead
1249 if (id->key && id->key->type != KEY_RSA1) {
1250 debug("Offering public key: %s", id->filename);
1251 sent = send_pubkey_test(authctxt, id);
1252 } else if (id->key == NULL) {
1253 debug("Trying private key: %s", id->filename);
1254 id->key = load_identity_file(id->filename);
1255 if (id->key != NULL) {
1257 sent = sign_and_send_pubkey(authctxt, id);
1269 * Send userauth request message specifying keyboard-interactive method.
1272 userauth_kbdint(Authctxt *authctxt)
1274 static int attempt = 0;
1276 if (attempt++ >= options.number_of_password_prompts)
1278 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1279 if (attempt > 1 && !authctxt->info_req_seen) {
1280 debug3("userauth_kbdint: disable: no info_req_seen");
1281 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1285 debug2("userauth_kbdint");
1286 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1287 packet_put_cstring(authctxt->server_user);
1288 packet_put_cstring(authctxt->service);
1289 packet_put_cstring(authctxt->method->name);
1290 packet_put_cstring(""); /* lang */
1291 packet_put_cstring(options.kbd_interactive_devices ?
1292 options.kbd_interactive_devices : "");
1295 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1300 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1303 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1305 Authctxt *authctxt = ctxt;
1306 char *name, *inst, *lang, *prompt, *response;
1307 u_int num_prompts, i;
1310 debug2("input_userauth_info_req");
1312 if (authctxt == NULL)
1313 fatal("input_userauth_info_req: no authentication context");
1315 authctxt->info_req_seen = 1;
1317 name = packet_get_string(NULL);
1318 inst = packet_get_string(NULL);
1319 lang = packet_get_string(NULL);
1320 if (strlen(name) > 0)
1322 if (strlen(inst) > 0)
1328 num_prompts = packet_get_int();
1330 * Begin to build info response packet based on prompts requested.
1331 * We commit to providing the correct number of responses, so if
1332 * further on we run into a problem that prevents this, we have to
1333 * be sure and clean this up and send a correct error response.
1335 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1336 packet_put_int(num_prompts);
1338 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1339 for (i = 0; i < num_prompts; i++) {
1340 prompt = packet_get_string(NULL);
1341 echo = packet_get_char();
1343 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1345 packet_put_cstring(response);
1346 memset(response, 0, strlen(response));
1350 packet_check_eom(); /* done with parsing incoming message. */
1352 packet_add_padding(64);
1357 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1358 u_char *data, u_int datalen)
1363 int to[2], from[2], status, version = 2;
1365 debug2("ssh_keysign called");
1367 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1368 error("ssh_keysign: no installed: %s", strerror(errno));
1371 if (fflush(stdout) != 0)
1372 error("ssh_keysign: fflush: %s", strerror(errno));
1374 error("ssh_keysign: pipe: %s", strerror(errno));
1377 if (pipe(from) < 0) {
1378 error("ssh_keysign: pipe: %s", strerror(errno));
1381 if ((pid = fork()) < 0) {
1382 error("ssh_keysign: fork: %s", strerror(errno));
1386 permanently_drop_suid(getuid());
1388 if (dup2(from[1], STDOUT_FILENO) < 0)
1389 fatal("ssh_keysign: dup2: %s", strerror(errno));
1391 if (dup2(to[0], STDIN_FILENO) < 0)
1392 fatal("ssh_keysign: dup2: %s", strerror(errno));
1395 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1396 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1403 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1404 buffer_put_string(&b, data, datalen);
1405 if (ssh_msg_send(to[1], version, &b) == -1)
1406 fatal("ssh_keysign: couldn't send request");
1408 if (ssh_msg_recv(from[0], &b) < 0) {
1409 error("ssh_keysign: no reply");
1416 while (waitpid(pid, &status, 0) < 0)
1420 if (buffer_get_char(&b) != version) {
1421 error("ssh_keysign: bad version");
1425 *sigp = buffer_get_string(&b, lenp);
1432 userauth_hostbased(Authctxt *authctxt)
1434 Key *private = NULL;
1435 Sensitive *sensitive = authctxt->sensitive;
1437 u_char *signature, *blob;
1438 char *chost, *pkalg, *p;
1439 const char *service;
1441 int ok, i, len, found = 0;
1443 /* check for a useful key */
1444 for (i = 0; i < sensitive->nkeys; i++) {
1445 private = sensitive->keys[i];
1446 if (private && private->type != KEY_RSA1) {
1448 /* we take and free the key */
1449 sensitive->keys[i] = NULL;
1454 debug("No more client hostkeys for hostbased authentication.");
1457 if (key_to_blob(private, &blob, &blen) == 0) {
1461 /* figure out a name for the client host */
1462 p = get_local_name(packet_get_connection_in());
1464 error("userauth_hostbased: cannot get local ipaddr/name");
1469 len = strlen(p) + 2;
1470 xasprintf(&chost, "%s.", p);
1471 debug2("userauth_hostbased: chost %s", chost);
1474 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1476 pkalg = xstrdup(key_ssh_name(private));
1478 /* construct data */
1479 buffer_put_string(&b, session_id2, session_id2_len);
1480 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1481 buffer_put_cstring(&b, authctxt->server_user);
1482 buffer_put_cstring(&b, service);
1483 buffer_put_cstring(&b, authctxt->method->name);
1484 buffer_put_cstring(&b, pkalg);
1485 buffer_put_string(&b, blob, blen);
1486 buffer_put_cstring(&b, chost);
1487 buffer_put_cstring(&b, authctxt->local_user);
1491 if (sensitive->external_keysign)
1492 ok = ssh_keysign(private, &signature, &slen,
1493 buffer_ptr(&b), buffer_len(&b));
1495 ok = key_sign(private, &signature, &slen,
1496 buffer_ptr(&b), buffer_len(&b));
1500 error("key_sign failed");
1506 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1507 packet_put_cstring(authctxt->server_user);
1508 packet_put_cstring(authctxt->service);
1509 packet_put_cstring(authctxt->method->name);
1510 packet_put_cstring(pkalg);
1511 packet_put_string(blob, blen);
1512 packet_put_cstring(chost);
1513 packet_put_cstring(authctxt->local_user);
1514 packet_put_string(signature, slen);
1515 memset(signature, 's', slen);
1525 /* find auth method */
1528 * given auth method name, if configurable options permit this method fill
1529 * in auth_ident field and return true, otherwise return false.
1532 authmethod_is_enabled(Authmethod *method)
1536 /* return false if options indicate this method is disabled */
1537 if (method->enabled == NULL || *method->enabled == 0)
1539 /* return false if batch mode is enabled but method needs interactive mode */
1540 if (method->batch_flag != NULL && *method->batch_flag != 0)
1546 authmethod_lookup(const char *name)
1548 Authmethod *method = NULL;
1550 for (method = authmethods; method->name != NULL; method++)
1551 if (strcmp(name, method->name) == 0)
1553 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1557 /* XXX internal state */
1558 static Authmethod *current = NULL;
1559 static char *supported = NULL;
1560 static char *preferred = NULL;
1563 * Given the authentication method list sent by the server, return the
1564 * next method we should try. If the server initially sends a nil list,
1565 * use a built-in default list.
1568 authmethod_get(char *authlist)
1573 /* Use a suitable default if we're passed a nil list. */
1574 if (authlist == NULL || strlen(authlist) == 0)
1575 authlist = options.preferred_authentications;
1577 if (supported == NULL || strcmp(authlist, supported) != 0) {
1578 debug3("start over, passed a different list %s", authlist);
1579 if (supported != NULL)
1581 supported = xstrdup(authlist);
1582 preferred = options.preferred_authentications;
1583 debug3("preferred %s", preferred);
1585 } else if (current != NULL && authmethod_is_enabled(current))
1589 if ((name = match_list(preferred, supported, &next)) == NULL) {
1590 debug("No more authentication methods to try.");
1595 debug3("authmethod_lookup %s", name);
1596 debug3("remaining preferred: %s", preferred);
1597 if ((current = authmethod_lookup(name)) != NULL &&
1598 authmethod_is_enabled(current)) {
1599 debug3("authmethod_is_enabled %s", name);
1600 debug("Next authentication method: %s", name);
1607 authmethods_get(void)
1609 Authmethod *method = NULL;
1614 for (method = authmethods; method->name != NULL; method++) {
1615 if (authmethod_is_enabled(method)) {
1616 if (buffer_len(&b) > 0)
1617 buffer_append(&b, ",", 1);
1618 buffer_append(&b, method->name, strlen(method->name));
1621 buffer_append(&b, "\0", 1);
1622 list = xstrdup(buffer_ptr(&b));