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;
80 u_char *session_id2 = NULL;
81 u_int session_id2_len = 0;
84 struct sockaddr *xxx_hostaddr;
89 verify_host_key_callback(Key *hostkey)
91 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
92 fatal("Host key verification failed.");
97 ssh_kex2(char *host, struct sockaddr *hostaddr)
102 char *orig = NULL, *gss = NULL;
103 char *gss_host = NULL;
107 xxx_hostaddr = hostaddr;
110 if (options.gss_keyex) {
111 /* Add the GSSAPI mechanisms currently supported on this
112 * client to the key exchange algorithm proposal */
113 orig = myproposal[PROPOSAL_KEX_ALGS];
115 if (options.gss_trust_dns)
116 gss_host = (char *)get_canonical_hostname(1);
120 gss = ssh_gssapi_client_mechanisms(gss_host);
122 debug("Offering GSSAPI proposal: %s", gss);
123 xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
129 if (options.ciphers == (char *)-1) {
130 logit("No valid ciphers for protocol version 2 given, using defaults.");
131 options.ciphers = NULL;
133 if (options.ciphers != NULL) {
134 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
135 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
137 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
138 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
139 myproposal[PROPOSAL_ENC_ALGS_STOC] =
140 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
141 if (options.compression) {
142 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
143 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
145 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
146 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
148 if (options.macs != NULL) {
149 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
150 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
152 if (options.hostkeyalgorithms != NULL)
153 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
154 options.hostkeyalgorithms;
157 /* If we've got GSSAPI algorithms, then we also support the
158 * 'null' hostkey, as a last resort */
159 if (options.gss_keyex && gss) {
160 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
161 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
166 if (options.rekey_limit)
167 packet_set_rekey_limit(options.rekey_limit);
169 /* start key exchange */
170 kex = kex_setup(myproposal);
171 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
172 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
173 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
174 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
176 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
177 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
178 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
180 kex->client_version_string=client_version_string;
181 kex->server_version_string=server_version_string;
182 kex->verify_host_key=&verify_host_key_callback;
185 kex->gss_deleg_creds = options.gss_deleg_creds;
186 kex->gss_trust_dns = options.gss_trust_dns;
187 kex->gss_host = gss_host;
192 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
194 session_id2 = kex->session_id;
195 session_id2_len = kex->session_id_len;
198 /* send 1st encrypted/maced/compressed message */
199 packet_start(SSH2_MSG_IGNORE);
200 packet_put_cstring("markus");
210 typedef struct Authctxt Authctxt;
211 typedef struct Authmethod Authmethod;
212 typedef struct identity Identity;
213 typedef struct idlist Idlist;
216 TAILQ_ENTRY(identity) next;
217 AuthenticationConnection *ac; /* set if agent supports key */
218 Key *key; /* public/private key */
219 char *filename; /* comment for agent-only keys */
221 int isprivate; /* key points to the private key */
223 TAILQ_HEAD(idlist, identity);
226 const char *server_user;
227 const char *local_user;
235 AuthenticationConnection *agent;
237 Sensitive *sensitive;
238 /* kbd-interactive */
244 char *name; /* string to compare against server's list */
245 int (*userauth)(Authctxt *authctxt);
246 int *enabled; /* flag in option struct that enables method */
247 int *batch_flag; /* flag in option struct that disables method */
250 void input_userauth_success(int, u_int32_t, void *);
251 void input_userauth_failure(int, u_int32_t, void *);
252 void input_userauth_banner(int, u_int32_t, void *);
253 void input_userauth_error(int, u_int32_t, void *);
254 void input_userauth_info_req(int, u_int32_t, void *);
255 void input_userauth_pk_ok(int, u_int32_t, void *);
256 void input_userauth_passwd_changereq(int, u_int32_t, void *);
258 int userauth_none(Authctxt *);
259 int userauth_pubkey(Authctxt *);
260 int userauth_passwd(Authctxt *);
261 int userauth_kbdint(Authctxt *);
262 int userauth_hostbased(Authctxt *);
263 int userauth_kerberos(Authctxt *);
266 int userauth_gssapi(Authctxt *authctxt);
267 void input_gssapi_response(int type, u_int32_t, void *);
268 void input_gssapi_token(int type, u_int32_t, void *);
269 void input_gssapi_hash(int type, u_int32_t, void *);
270 void input_gssapi_error(int, u_int32_t, void *);
271 void input_gssapi_errtok(int, u_int32_t, void *);
272 int userauth_gsskeyex(Authctxt *authctxt);
275 void userauth(Authctxt *, char *);
277 static int sign_and_send_pubkey(Authctxt *, Identity *);
278 static void pubkey_prepare(Authctxt *);
279 static void pubkey_cleanup(Authctxt *);
280 static Key *load_identity_file(char *);
282 static Authmethod *authmethod_get(char *authlist);
283 static Authmethod *authmethod_lookup(const char *name);
284 static char *authmethods_get(void);
286 Authmethod authmethods[] = {
290 &options.gss_authentication,
294 &options.gss_authentication,
299 &options.hostbased_authentication,
303 &options.pubkey_authentication,
305 {"keyboard-interactive",
307 &options.kbd_interactive_authentication,
308 &options.batch_mode},
311 &options.password_authentication,
312 &options.batch_mode},
317 {NULL, NULL, NULL, NULL}
321 ssh_userauth2(const char *local_user, const char *server_user, char *host,
322 Sensitive *sensitive)
327 if (options.challenge_response_authentication)
328 options.kbd_interactive_authentication = 1;
330 packet_start(SSH2_MSG_SERVICE_REQUEST);
331 packet_put_cstring("ssh-userauth");
333 debug("SSH2_MSG_SERVICE_REQUEST sent");
335 type = packet_read();
336 if (type != SSH2_MSG_SERVICE_ACCEPT)
337 fatal("Server denied authentication request: %d", type);
338 if (packet_remaining() > 0) {
339 char *reply = packet_get_string(NULL);
340 debug2("service_accept: %s", reply);
343 debug2("buggy server: service_accept w/o service");
346 debug("SSH2_MSG_SERVICE_ACCEPT received");
348 if (options.preferred_authentications == NULL)
349 options.preferred_authentications = authmethods_get();
351 /* setup authentication context */
352 memset(&authctxt, 0, sizeof(authctxt));
353 pubkey_prepare(&authctxt);
354 authctxt.server_user = server_user;
355 authctxt.local_user = local_user;
356 authctxt.host = host;
357 authctxt.service = "ssh-connection"; /* service name */
358 authctxt.success = 0;
359 authctxt.method = authmethod_lookup("none");
360 authctxt.authlist = NULL;
361 authctxt.methoddata = NULL;
362 authctxt.sensitive = sensitive;
363 authctxt.info_req_seen = 0;
364 if (authctxt.method == NULL)
365 fatal("ssh_userauth2: internal error: cannot send userauth none request");
367 /* initial userauth request */
368 userauth_none(&authctxt);
370 dispatch_init(&input_userauth_error);
371 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
372 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
373 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
374 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
376 pubkey_cleanup(&authctxt);
377 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
379 debug("Authentication succeeded (%s).", authctxt.method->name);
383 userauth(Authctxt *authctxt, char *authlist)
385 if (authctxt->methoddata) {
386 xfree(authctxt->methoddata);
387 authctxt->methoddata = NULL;
389 if (authlist == NULL) {
390 authlist = authctxt->authlist;
392 if (authctxt->authlist)
393 xfree(authctxt->authlist);
394 authctxt->authlist = authlist;
397 Authmethod *method = authmethod_get(authlist);
399 fatal("Permission denied (%s).", authlist);
400 authctxt->method = method;
402 /* reset the per method handler */
403 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
404 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
406 /* and try new method */
407 if (method->userauth(authctxt) != 0) {
408 debug2("we sent a %s packet, wait for reply", method->name);
411 debug2("we did not send a packet, disable method");
412 method->enabled = NULL;
418 input_userauth_error(int type, u_int32_t seq, void *ctxt)
420 fatal("input_userauth_error: bad message during authentication: "
425 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
429 debug3("input_userauth_banner");
430 msg = packet_get_string(NULL);
431 lang = packet_get_string(NULL);
432 if (options.log_level >= SYSLOG_LEVEL_INFO)
433 fprintf(stderr, "%s", msg);
439 input_userauth_success(int type, u_int32_t seq, void *ctxt)
441 Authctxt *authctxt = ctxt;
442 if (authctxt == NULL)
443 fatal("input_userauth_success: no authentication context");
444 if (authctxt->authlist) {
445 xfree(authctxt->authlist);
446 authctxt->authlist = NULL;
448 if (authctxt->methoddata) {
449 xfree(authctxt->methoddata);
450 authctxt->methoddata = NULL;
452 authctxt->success = 1; /* break out */
456 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
458 Authctxt *authctxt = ctxt;
459 char *authlist = NULL;
462 if (authctxt == NULL)
463 fatal("input_userauth_failure: no authentication context");
465 authlist = packet_get_string(NULL);
466 partial = packet_get_char();
470 logit("Authenticated with partial success.");
471 debug("Authentications that can continue: %s", authlist);
473 userauth(authctxt, authlist);
476 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
478 Authctxt *authctxt = ctxt;
482 int pktype, sent = 0;
487 if (authctxt == NULL)
488 fatal("input_userauth_pk_ok: no authentication context");
489 if (datafellows & SSH_BUG_PKOK) {
490 /* this is similar to SSH_BUG_PKAUTH */
491 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
492 pkblob = packet_get_string(&blen);
494 buffer_append(&b, pkblob, blen);
495 pkalg = buffer_get_string(&b, &alen);
498 pkalg = packet_get_string(&alen);
499 pkblob = packet_get_string(&blen);
503 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
505 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
506 debug("unknown pkalg %s", pkalg);
509 if ((key = key_from_blob(pkblob, blen)) == NULL) {
510 debug("no key from blob. pkalg %s", pkalg);
513 if (key->type != pktype) {
514 error("input_userauth_pk_ok: type mismatch "
515 "for decoded key (received %d, expected %d)",
519 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
520 debug2("input_userauth_pk_ok: fp %s", fp);
524 * search keys in the reverse order, because last candidate has been
525 * moved to the end of the queue. this also avoids confusion by
528 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
529 if (key_equal(key, id->key)) {
530 sent = sign_and_send_pubkey(authctxt, id);
540 /* try another method if we did not send a packet */
542 userauth(authctxt, NULL);
547 userauth_gssapi(Authctxt *authctxt)
549 Gssctxt *gssctxt = NULL;
550 static gss_OID_set gss_supported = NULL;
551 static u_int mech = 0;
554 char *gss_host = NULL;
556 if (options.gss_trust_dns)
557 gss_host = (char *)get_canonical_hostname(1);
559 gss_host = (char *)authctxt->host;
561 /* Try one GSSAPI method at a time, rather than sending them all at
564 if (gss_supported == NULL)
565 gss_indicate_mechs(&min, &gss_supported);
567 /* Check to see if the mechanism is usable before we offer it */
568 while (mech < gss_supported->count && !ok) {
569 /* My DER encoding requires length<128 */
570 if (gss_supported->elements[mech].length < 128 &&
571 ssh_gssapi_check_mechanism(&gssctxt,
572 &gss_supported->elements[mech], gss_host)) {
573 ok = 1; /* Mechanism works */
582 authctxt->methoddata=(void *)gssctxt;
584 packet_start(SSH2_MSG_USERAUTH_REQUEST);
585 packet_put_cstring(authctxt->server_user);
586 packet_put_cstring(authctxt->service);
587 packet_put_cstring(authctxt->method->name);
591 packet_put_int((gss_supported->elements[mech].length) + 2);
592 packet_put_char(SSH_GSS_OIDTYPE);
593 packet_put_char(gss_supported->elements[mech].length);
594 packet_put_raw(gss_supported->elements[mech].elements,
595 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 mic = GSS_C_EMPTY_BUFFER;
616 gss_buffer_desc gssbuf;
617 OM_uint32 status, ms, flags;
620 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
621 recv_tok, &send_tok, &flags);
623 if (send_tok.length > 0) {
624 if (GSS_ERROR(status))
625 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
627 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
629 packet_put_string(send_tok.value, send_tok.length);
631 gss_release_buffer(&ms, &send_tok);
634 if (status == GSS_S_COMPLETE) {
635 /* send either complete or MIC, depending on mechanism */
636 if (!(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);
679 oidv[0] != SSH_GSS_OIDTYPE ||
680 oidv[1] != oidlen - 2) {
682 debug("Badly encoded mechanism OID received");
683 userauth(authctxt, NULL);
687 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
688 fatal("Server returned different OID than expected");
694 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
695 /* Start again with next method on list */
696 debug("Trying to start again");
697 userauth(authctxt, NULL);
703 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
705 Authctxt *authctxt = ctxt;
706 gss_buffer_desc recv_tok;
710 if (authctxt == NULL)
711 fatal("input_gssapi_response: no authentication context");
713 recv_tok.value = packet_get_string(&slen);
714 recv_tok.length = slen; /* safe typecast */
718 status = process_gssapi_token(ctxt, &recv_tok);
720 xfree(recv_tok.value);
722 if (GSS_ERROR(status)) {
723 /* Start again with the next method in the list */
724 userauth(authctxt, NULL);
730 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
732 Authctxt *authctxt = ctxt;
734 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
735 gss_buffer_desc recv_tok;
736 OM_uint32 status, ms;
739 if (authctxt == NULL)
740 fatal("input_gssapi_response: no authentication context");
741 gssctxt = authctxt->methoddata;
743 recv_tok.value = packet_get_string(&len);
744 recv_tok.length = len;
748 /* Stick it into GSSAPI and see what it says */
749 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
750 &recv_tok, &send_tok, NULL);
752 xfree(recv_tok.value);
753 gss_release_buffer(&ms, &send_tok);
755 /* Server will be returning a failed packet after this one */
759 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
765 maj=packet_get_int();
766 min=packet_get_int();
767 msg=packet_get_string(NULL);
768 lang=packet_get_string(NULL);
772 debug("Server GSSAPI Error:\n%s", msg);
778 userauth_gsskeyex(Authctxt *authctxt)
781 gss_buffer_desc gssbuf;
782 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
785 static int attempt = 0;
789 if (gss_kex_context == NULL) {
790 debug("No valid Key exchange context");
794 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
797 gssbuf.value = buffer_ptr(&b);
798 gssbuf.length = buffer_len(&b);
800 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
805 packet_start(SSH2_MSG_USERAUTH_REQUEST);
806 packet_put_cstring(authctxt->server_user);
807 packet_put_cstring(authctxt->service);
808 packet_put_cstring(authctxt->method->name);
809 packet_put_string(mic.value, mic.length);
813 gss_release_buffer(&ms, &mic);
821 userauth_none(Authctxt *authctxt)
823 /* initial userauth request */
824 packet_start(SSH2_MSG_USERAUTH_REQUEST);
825 packet_put_cstring(authctxt->server_user);
826 packet_put_cstring(authctxt->service);
827 packet_put_cstring(authctxt->method->name);
833 userauth_passwd(Authctxt *authctxt)
835 static int attempt = 0;
839 if (attempt++ >= options.number_of_password_prompts)
843 error("Permission denied, please try again.");
845 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
846 authctxt->server_user, authctxt->host);
847 password = read_passphrase(prompt, 0);
848 packet_start(SSH2_MSG_USERAUTH_REQUEST);
849 packet_put_cstring(authctxt->server_user);
850 packet_put_cstring(authctxt->service);
851 packet_put_cstring(authctxt->method->name);
853 packet_put_cstring(password);
854 memset(password, 0, strlen(password));
856 packet_add_padding(64);
859 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
860 &input_userauth_passwd_changereq);
865 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
868 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
870 Authctxt *authctxt = ctxt;
871 char *info, *lang, *password = NULL, *retype = NULL;
874 debug2("input_userauth_passwd_changereq");
876 if (authctxt == NULL)
877 fatal("input_userauth_passwd_changereq: "
878 "no authentication context");
880 info = packet_get_string(NULL);
881 lang = packet_get_string(NULL);
882 if (strlen(info) > 0)
886 packet_start(SSH2_MSG_USERAUTH_REQUEST);
887 packet_put_cstring(authctxt->server_user);
888 packet_put_cstring(authctxt->service);
889 packet_put_cstring(authctxt->method->name);
890 packet_put_char(1); /* additional info */
891 snprintf(prompt, sizeof(prompt),
892 "Enter %.30s@%.128s's old password: ",
893 authctxt->server_user, authctxt->host);
894 password = read_passphrase(prompt, 0);
895 packet_put_cstring(password);
896 memset(password, 0, strlen(password));
899 while (password == NULL) {
900 snprintf(prompt, sizeof(prompt),
901 "Enter %.30s@%.128s's new password: ",
902 authctxt->server_user, authctxt->host);
903 password = read_passphrase(prompt, RP_ALLOW_EOF);
904 if (password == NULL) {
908 snprintf(prompt, sizeof(prompt),
909 "Retype %.30s@%.128s's new password: ",
910 authctxt->server_user, authctxt->host);
911 retype = read_passphrase(prompt, 0);
912 if (strcmp(password, retype) != 0) {
913 memset(password, 0, strlen(password));
915 logit("Mismatch; try again, EOF to quit.");
918 memset(retype, 0, strlen(retype));
921 packet_put_cstring(password);
922 memset(password, 0, strlen(password));
924 packet_add_padding(64);
927 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
928 &input_userauth_passwd_changereq);
932 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
933 u_char *data, u_int datalen)
938 /* the agent supports this key */
940 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
943 * we have already loaded the private key or
944 * the private key is stored in external hardware
946 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
947 return (key_sign(id->key, sigp, lenp, data, datalen));
948 /* load the private key from the file */
949 if ((prv = load_identity_file(id->filename)) == NULL)
951 ret = key_sign(prv, sigp, lenp, data, datalen);
957 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
960 u_char *blob, *signature;
966 debug3("sign_and_send_pubkey");
968 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
969 /* we cannot handle this key */
970 debug3("sign_and_send_pubkey: cannot handle key");
973 /* data to be signed */
975 if (datafellows & SSH_OLD_SESSIONID) {
976 buffer_append(&b, session_id2, session_id2_len);
977 skip = session_id2_len;
979 buffer_put_string(&b, session_id2, session_id2_len);
980 skip = buffer_len(&b);
982 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
983 buffer_put_cstring(&b, authctxt->server_user);
984 buffer_put_cstring(&b,
985 datafellows & SSH_BUG_PKSERVICE ?
988 if (datafellows & SSH_BUG_PKAUTH) {
989 buffer_put_char(&b, have_sig);
991 buffer_put_cstring(&b, authctxt->method->name);
992 buffer_put_char(&b, have_sig);
993 buffer_put_cstring(&b, key_ssh_name(id->key));
995 buffer_put_string(&b, blob, bloblen);
997 /* generate signature */
998 ret = identity_sign(id, &signature, &slen,
999 buffer_ptr(&b), buffer_len(&b));
1008 if (datafellows & SSH_BUG_PKSERVICE) {
1010 buffer_append(&b, session_id2, session_id2_len);
1011 skip = session_id2_len;
1012 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1013 buffer_put_cstring(&b, authctxt->server_user);
1014 buffer_put_cstring(&b, authctxt->service);
1015 buffer_put_cstring(&b, authctxt->method->name);
1016 buffer_put_char(&b, have_sig);
1017 if (!(datafellows & SSH_BUG_PKAUTH))
1018 buffer_put_cstring(&b, key_ssh_name(id->key));
1019 buffer_put_string(&b, blob, bloblen);
1023 /* append signature */
1024 buffer_put_string(&b, signature, slen);
1027 /* skip session id and packet type */
1028 if (buffer_len(&b) < skip + 1)
1029 fatal("userauth_pubkey: internal error");
1030 buffer_consume(&b, skip + 1);
1032 /* put remaining data from buffer into packet */
1033 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1034 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1042 send_pubkey_test(Authctxt *authctxt, Identity *id)
1045 u_int bloblen, have_sig = 0;
1047 debug3("send_pubkey_test");
1049 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1050 /* we cannot handle this key */
1051 debug3("send_pubkey_test: cannot handle key");
1054 /* register callback for USERAUTH_PK_OK message */
1055 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1057 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1058 packet_put_cstring(authctxt->server_user);
1059 packet_put_cstring(authctxt->service);
1060 packet_put_cstring(authctxt->method->name);
1061 packet_put_char(have_sig);
1062 if (!(datafellows & SSH_BUG_PKAUTH))
1063 packet_put_cstring(key_ssh_name(id->key));
1064 packet_put_string(blob, bloblen);
1071 load_identity_file(char *filename)
1074 char prompt[300], *passphrase;
1075 int perm_ok, quit, i;
1078 if (stat(filename, &st) < 0) {
1079 debug3("no such identity: %s", filename);
1082 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1085 if (private == NULL) {
1086 if (options.batch_mode)
1088 snprintf(prompt, sizeof prompt,
1089 "Enter passphrase for key '%.100s': ", filename);
1090 for (i = 0; i < options.number_of_password_prompts; i++) {
1091 passphrase = read_passphrase(prompt, 0);
1092 if (strcmp(passphrase, "") != 0) {
1093 private = key_load_private_type(KEY_UNSPEC,
1094 filename, passphrase, NULL, NULL);
1097 debug2("no passphrase given, try next key");
1100 memset(passphrase, 0, strlen(passphrase));
1102 if (private != NULL || quit)
1104 debug2("bad passphrase given, try again...");
1111 * try keys in the following order:
1112 * 1. agent keys that are found in the config file
1113 * 2. other agent keys
1114 * 3. keys that are only listed in the config file
1117 pubkey_prepare(Authctxt *authctxt)
1120 Idlist agent, files, *preferred;
1122 AuthenticationConnection *ac;
1126 TAILQ_INIT(&agent); /* keys from the agent */
1127 TAILQ_INIT(&files); /* keys from the config file */
1128 preferred = &authctxt->keys;
1129 TAILQ_INIT(preferred); /* preferred order of keys */
1131 /* list of keys stored in the filesystem */
1132 for (i = 0; i < options.num_identity_files; i++) {
1133 key = options.identity_keys[i];
1134 if (key && key->type == KEY_RSA1)
1136 options.identity_keys[i] = NULL;
1137 id = xcalloc(1, sizeof(*id));
1139 id->filename = xstrdup(options.identity_files[i]);
1140 TAILQ_INSERT_TAIL(&files, id, next);
1142 /* list of keys supported by the agent */
1143 if ((ac = ssh_get_authentication_connection())) {
1144 for (key = ssh_get_first_identity(ac, &comment, 2);
1146 key = ssh_get_next_identity(ac, &comment, 2)) {
1148 TAILQ_FOREACH(id, &files, next) {
1149 /* agent keys from the config file are preferred */
1150 if (key_equal(key, id->key)) {
1153 TAILQ_REMOVE(&files, id, next);
1154 TAILQ_INSERT_TAIL(preferred, id, next);
1160 if (!found && !options.identities_only) {
1161 id = xcalloc(1, sizeof(*id));
1163 id->filename = comment;
1165 TAILQ_INSERT_TAIL(&agent, id, next);
1168 /* append remaining agent keys */
1169 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1170 TAILQ_REMOVE(&agent, id, next);
1171 TAILQ_INSERT_TAIL(preferred, id, next);
1173 authctxt->agent = ac;
1175 /* append remaining keys from the config file */
1176 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1177 TAILQ_REMOVE(&files, id, next);
1178 TAILQ_INSERT_TAIL(preferred, id, next);
1180 TAILQ_FOREACH(id, preferred, next) {
1181 debug2("key: %s (%p)", id->filename, id->key);
1186 pubkey_cleanup(Authctxt *authctxt)
1190 if (authctxt->agent != NULL)
1191 ssh_close_authentication_connection(authctxt->agent);
1192 for (id = TAILQ_FIRST(&authctxt->keys); id;
1193 id = TAILQ_FIRST(&authctxt->keys)) {
1194 TAILQ_REMOVE(&authctxt->keys, id, next);
1198 xfree(id->filename);
1204 userauth_pubkey(Authctxt *authctxt)
1209 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1212 /* move key to the end of the queue */
1213 TAILQ_REMOVE(&authctxt->keys, id, next);
1214 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1216 * send a test message if we have the public key. for
1217 * encrypted keys we cannot do this and have to load the
1218 * private key instead
1220 if (id->key && id->key->type != KEY_RSA1) {
1221 debug("Offering public key: %s", id->filename);
1222 sent = send_pubkey_test(authctxt, id);
1223 } else if (id->key == NULL) {
1224 debug("Trying private key: %s", id->filename);
1225 id->key = load_identity_file(id->filename);
1226 if (id->key != NULL) {
1228 sent = sign_and_send_pubkey(authctxt, id);
1240 * Send userauth request message specifying keyboard-interactive method.
1243 userauth_kbdint(Authctxt *authctxt)
1245 static int attempt = 0;
1247 if (attempt++ >= options.number_of_password_prompts)
1249 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1250 if (attempt > 1 && !authctxt->info_req_seen) {
1251 debug3("userauth_kbdint: disable: no info_req_seen");
1252 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1256 debug2("userauth_kbdint");
1257 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1258 packet_put_cstring(authctxt->server_user);
1259 packet_put_cstring(authctxt->service);
1260 packet_put_cstring(authctxt->method->name);
1261 packet_put_cstring(""); /* lang */
1262 packet_put_cstring(options.kbd_interactive_devices ?
1263 options.kbd_interactive_devices : "");
1266 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1271 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1274 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1276 Authctxt *authctxt = ctxt;
1277 char *name, *inst, *lang, *prompt, *response;
1278 u_int num_prompts, i;
1281 debug2("input_userauth_info_req");
1283 if (authctxt == NULL)
1284 fatal("input_userauth_info_req: no authentication context");
1286 authctxt->info_req_seen = 1;
1288 name = packet_get_string(NULL);
1289 inst = packet_get_string(NULL);
1290 lang = packet_get_string(NULL);
1291 if (strlen(name) > 0)
1293 if (strlen(inst) > 0)
1299 num_prompts = packet_get_int();
1301 * Begin to build info response packet based on prompts requested.
1302 * We commit to providing the correct number of responses, so if
1303 * further on we run into a problem that prevents this, we have to
1304 * be sure and clean this up and send a correct error response.
1306 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1307 packet_put_int(num_prompts);
1309 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1310 for (i = 0; i < num_prompts; i++) {
1311 prompt = packet_get_string(NULL);
1312 echo = packet_get_char();
1314 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1316 packet_put_cstring(response);
1317 memset(response, 0, strlen(response));
1321 packet_check_eom(); /* done with parsing incoming message. */
1323 packet_add_padding(64);
1328 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1329 u_char *data, u_int datalen)
1334 int to[2], from[2], status, version = 2;
1336 debug2("ssh_keysign called");
1338 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1339 error("ssh_keysign: no installed: %s", strerror(errno));
1342 if (fflush(stdout) != 0)
1343 error("ssh_keysign: fflush: %s", strerror(errno));
1345 error("ssh_keysign: pipe: %s", strerror(errno));
1348 if (pipe(from) < 0) {
1349 error("ssh_keysign: pipe: %s", strerror(errno));
1352 if ((pid = fork()) < 0) {
1353 error("ssh_keysign: fork: %s", strerror(errno));
1357 permanently_drop_suid(getuid());
1359 if (dup2(from[1], STDOUT_FILENO) < 0)
1360 fatal("ssh_keysign: dup2: %s", strerror(errno));
1362 if (dup2(to[0], STDIN_FILENO) < 0)
1363 fatal("ssh_keysign: dup2: %s", strerror(errno));
1366 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1367 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1374 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1375 buffer_put_string(&b, data, datalen);
1376 if (ssh_msg_send(to[1], version, &b) == -1)
1377 fatal("ssh_keysign: couldn't send request");
1379 if (ssh_msg_recv(from[0], &b) < 0) {
1380 error("ssh_keysign: no reply");
1387 while (waitpid(pid, &status, 0) < 0)
1391 if (buffer_get_char(&b) != version) {
1392 error("ssh_keysign: bad version");
1396 *sigp = buffer_get_string(&b, lenp);
1403 userauth_hostbased(Authctxt *authctxt)
1405 Key *private = NULL;
1406 Sensitive *sensitive = authctxt->sensitive;
1408 u_char *signature, *blob;
1409 char *chost, *pkalg, *p;
1410 const char *service;
1412 int ok, i, len, found = 0;
1414 /* check for a useful key */
1415 for (i = 0; i < sensitive->nkeys; i++) {
1416 private = sensitive->keys[i];
1417 if (private && private->type != KEY_RSA1) {
1419 /* we take and free the key */
1420 sensitive->keys[i] = NULL;
1425 debug("No more client hostkeys for hostbased authentication.");
1428 if (key_to_blob(private, &blob, &blen) == 0) {
1432 /* figure out a name for the client host */
1433 p = get_local_name(packet_get_connection_in());
1435 error("userauth_hostbased: cannot get local ipaddr/name");
1440 len = strlen(p) + 2;
1441 xasprintf(&chost, "%s.", p);
1442 debug2("userauth_hostbased: chost %s", chost);
1445 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1447 pkalg = xstrdup(key_ssh_name(private));
1449 /* construct data */
1450 buffer_put_string(&b, session_id2, session_id2_len);
1451 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1452 buffer_put_cstring(&b, authctxt->server_user);
1453 buffer_put_cstring(&b, service);
1454 buffer_put_cstring(&b, authctxt->method->name);
1455 buffer_put_cstring(&b, pkalg);
1456 buffer_put_string(&b, blob, blen);
1457 buffer_put_cstring(&b, chost);
1458 buffer_put_cstring(&b, authctxt->local_user);
1462 if (sensitive->external_keysign)
1463 ok = ssh_keysign(private, &signature, &slen,
1464 buffer_ptr(&b), buffer_len(&b));
1466 ok = key_sign(private, &signature, &slen,
1467 buffer_ptr(&b), buffer_len(&b));
1471 error("key_sign failed");
1477 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1478 packet_put_cstring(authctxt->server_user);
1479 packet_put_cstring(authctxt->service);
1480 packet_put_cstring(authctxt->method->name);
1481 packet_put_cstring(pkalg);
1482 packet_put_string(blob, blen);
1483 packet_put_cstring(chost);
1484 packet_put_cstring(authctxt->local_user);
1485 packet_put_string(signature, slen);
1486 memset(signature, 's', slen);
1496 /* find auth method */
1499 * given auth method name, if configurable options permit this method fill
1500 * in auth_ident field and return true, otherwise return false.
1503 authmethod_is_enabled(Authmethod *method)
1507 /* return false if options indicate this method is disabled */
1508 if (method->enabled == NULL || *method->enabled == 0)
1510 /* return false if batch mode is enabled but method needs interactive mode */
1511 if (method->batch_flag != NULL && *method->batch_flag != 0)
1517 authmethod_lookup(const char *name)
1519 Authmethod *method = NULL;
1521 for (method = authmethods; method->name != NULL; method++)
1522 if (strcmp(name, method->name) == 0)
1524 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1528 /* XXX internal state */
1529 static Authmethod *current = NULL;
1530 static char *supported = NULL;
1531 static char *preferred = NULL;
1534 * Given the authentication method list sent by the server, return the
1535 * next method we should try. If the server initially sends a nil list,
1536 * use a built-in default list.
1539 authmethod_get(char *authlist)
1544 /* Use a suitable default if we're passed a nil list. */
1545 if (authlist == NULL || strlen(authlist) == 0)
1546 authlist = options.preferred_authentications;
1548 if (supported == NULL || strcmp(authlist, supported) != 0) {
1549 debug3("start over, passed a different list %s", authlist);
1550 if (supported != NULL)
1552 supported = xstrdup(authlist);
1553 preferred = options.preferred_authentications;
1554 debug3("preferred %s", preferred);
1556 } else if (current != NULL && authmethod_is_enabled(current))
1560 if ((name = match_list(preferred, supported, &next)) == NULL) {
1561 debug("No more authentication methods to try.");
1566 debug3("authmethod_lookup %s", name);
1567 debug3("remaining preferred: %s", preferred);
1568 if ((current = authmethod_lookup(name)) != NULL &&
1569 authmethod_is_enabled(current)) {
1570 debug3("authmethod_is_enabled %s", name);
1571 debug("Next authentication method: %s", name);
1578 authmethods_get(void)
1580 Authmethod *method = NULL;
1585 for (method = authmethods; method->name != NULL; method++) {
1586 if (authmethod_is_enabled(method)) {
1587 if (buffer_len(&b) > 0)
1588 buffer_append(&b, ",", 1);
1589 buffer_append(&b, method->name, strlen(method->name));
1592 buffer_append(&b, "\0", 1);
1593 list = xstrdup(buffer_ptr(&b));