sshconnect2.c revision 264692
1/* $OpenBSD: sshconnect2.c,v 1.204 2014/02/02 03:44:32 djm Exp $ */ 2/* $FreeBSD: stable/10/crypto/openssh/sshconnect2.c 264692 2014-04-20 12:46:18Z des $ */ 3/* 4 * Copyright (c) 2000 Markus Friedl. All rights reserved. 5 * Copyright (c) 2008 Damien Miller. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "includes.h" 29 30#include <sys/types.h> 31#include <sys/socket.h> 32#include <sys/wait.h> 33#include <sys/stat.h> 34 35#include <errno.h> 36#include <fcntl.h> 37#include <netdb.h> 38#include <pwd.h> 39#include <signal.h> 40#include <stdarg.h> 41#include <stdio.h> 42#include <string.h> 43#include <unistd.h> 44#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) 45#include <vis.h> 46#endif 47 48#include "openbsd-compat/sys-queue.h" 49 50#include "xmalloc.h" 51#include "ssh.h" 52#include "ssh2.h" 53#include "buffer.h" 54#include "packet.h" 55#include "compat.h" 56#include "cipher.h" 57#include "key.h" 58#include "kex.h" 59#include "myproposal.h" 60#include "sshconnect.h" 61#include "authfile.h" 62#include "dh.h" 63#include "authfd.h" 64#include "log.h" 65#include "readconf.h" 66#include "misc.h" 67#include "match.h" 68#include "dispatch.h" 69#include "canohost.h" 70#include "msg.h" 71#include "pathnames.h" 72#include "uidswap.h" 73#include "hostfile.h" 74 75#ifdef GSSAPI 76#include "ssh-gss.h" 77#endif 78 79/* import */ 80extern char *client_version_string; 81extern char *server_version_string; 82extern Options options; 83#ifdef NONE_CIPHER_ENABLED 84extern Kex *xxx_kex; 85 86/* 87 * tty_flag is set in ssh.c so we can use it here. If set then prevent 88 * the switch to the null cipher. 89 */ 90 91extern int tty_flag; 92#endif 93 94/* 95 * SSH2 key exchange 96 */ 97 98u_char *session_id2 = NULL; 99u_int session_id2_len = 0; 100 101char *xxx_host; 102struct sockaddr *xxx_hostaddr; 103 104Kex *xxx_kex = NULL; 105 106static int 107verify_host_key_callback(Key *hostkey) 108{ 109 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) 110 fatal("Host key verification failed."); 111 return 0; 112} 113 114static char * 115order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) 116{ 117 char *oavail, *avail, *first, *last, *alg, *hostname, *ret; 118 size_t maxlen; 119 struct hostkeys *hostkeys; 120 int ktype; 121 u_int i; 122 123 /* Find all hostkeys for this hostname */ 124 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); 125 hostkeys = init_hostkeys(); 126 for (i = 0; i < options.num_user_hostfiles; i++) 127 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]); 128 for (i = 0; i < options.num_system_hostfiles; i++) 129 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]); 130 131 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG); 132 maxlen = strlen(avail) + 1; 133 first = xmalloc(maxlen); 134 last = xmalloc(maxlen); 135 *first = *last = '\0'; 136 137#define ALG_APPEND(to, from) \ 138 do { \ 139 if (*to != '\0') \ 140 strlcat(to, ",", maxlen); \ 141 strlcat(to, from, maxlen); \ 142 } while (0) 143 144 while ((alg = strsep(&avail, ",")) && *alg != '\0') { 145 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC) 146 fatal("%s: unknown alg %s", __func__, alg); 147 if (lookup_key_in_hostkeys_by_type(hostkeys, 148 key_type_plain(ktype), NULL)) 149 ALG_APPEND(first, alg); 150 else 151 ALG_APPEND(last, alg); 152 } 153#undef ALG_APPEND 154 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last); 155 if (*first != '\0') 156 debug3("%s: prefer hostkeyalgs: %s", __func__, first); 157 158 free(first); 159 free(last); 160 free(hostname); 161 free(oavail); 162 free_hostkeys(hostkeys); 163 164 return ret; 165} 166 167void 168ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) 169{ 170 Kex *kex; 171 172 xxx_host = host; 173 xxx_hostaddr = hostaddr; 174 175 if (options.ciphers == (char *)-1) { 176 logit("No valid ciphers for protocol version 2 given, using defaults."); 177 options.ciphers = NULL; 178 } 179 if (options.ciphers != NULL) { 180 myproposal[PROPOSAL_ENC_ALGS_CTOS] = 181 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; 182 } 183 myproposal[PROPOSAL_ENC_ALGS_CTOS] = 184 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); 185 myproposal[PROPOSAL_ENC_ALGS_STOC] = 186 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); 187 if (options.compression) { 188 myproposal[PROPOSAL_COMP_ALGS_CTOS] = 189 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; 190 } else { 191 myproposal[PROPOSAL_COMP_ALGS_CTOS] = 192 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; 193 } 194 if (options.macs != NULL) { 195 myproposal[PROPOSAL_MAC_ALGS_CTOS] = 196 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; 197 } 198 if (options.hostkeyalgorithms != NULL) 199 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 200 compat_pkalg_proposal(options.hostkeyalgorithms); 201 else { 202 /* Prefer algorithms that we already have keys for */ 203 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 204 compat_pkalg_proposal( 205 order_hostkeyalgs(host, hostaddr, port)); 206 } 207 if (options.kex_algorithms != NULL) 208 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms; 209 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal( 210 myproposal[PROPOSAL_KEX_ALGS]); 211 212 if (options.rekey_limit || options.rekey_interval) 213 packet_set_rekey_limits((u_int32_t)options.rekey_limit, 214 (time_t)options.rekey_interval); 215 216 /* start key exchange */ 217 kex = kex_setup(myproposal); 218 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; 219 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; 220 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 221 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 222 kex->kex[KEX_ECDH_SHA2] = kexecdh_client; 223 kex->kex[KEX_C25519_SHA256] = kexc25519_client; 224 kex->client_version_string=client_version_string; 225 kex->server_version_string=server_version_string; 226 kex->verify_host_key=&verify_host_key_callback; 227 228 xxx_kex = kex; 229 230 dispatch_run(DISPATCH_BLOCK, &kex->done, kex); 231 232 if (options.use_roaming && !kex->roaming) { 233 debug("Roaming not allowed by server"); 234 options.use_roaming = 0; 235 } 236 237 session_id2 = kex->session_id; 238 session_id2_len = kex->session_id_len; 239 240#ifdef DEBUG_KEXDH 241 /* send 1st encrypted/maced/compressed message */ 242 packet_start(SSH2_MSG_IGNORE); 243 packet_put_cstring("markus"); 244 packet_send(); 245 packet_write_wait(); 246#endif 247} 248 249/* 250 * Authenticate user 251 */ 252 253typedef struct Authctxt Authctxt; 254typedef struct Authmethod Authmethod; 255typedef struct identity Identity; 256typedef struct idlist Idlist; 257 258struct identity { 259 TAILQ_ENTRY(identity) next; 260 AuthenticationConnection *ac; /* set if agent supports key */ 261 Key *key; /* public/private key */ 262 char *filename; /* comment for agent-only keys */ 263 int tried; 264 int isprivate; /* key points to the private key */ 265 int userprovided; 266}; 267TAILQ_HEAD(idlist, identity); 268 269struct Authctxt { 270 const char *server_user; 271 const char *local_user; 272 const char *host; 273 const char *service; 274 Authmethod *method; 275 sig_atomic_t success; 276 char *authlist; 277 /* pubkey */ 278 Idlist keys; 279 AuthenticationConnection *agent; 280 /* hostbased */ 281 Sensitive *sensitive; 282 /* kbd-interactive */ 283 int info_req_seen; 284 /* generic */ 285 void *methoddata; 286}; 287struct Authmethod { 288 char *name; /* string to compare against server's list */ 289 int (*userauth)(Authctxt *authctxt); 290 void (*cleanup)(Authctxt *authctxt); 291 int *enabled; /* flag in option struct that enables method */ 292 int *batch_flag; /* flag in option struct that disables method */ 293}; 294 295void input_userauth_success(int, u_int32_t, void *); 296void input_userauth_success_unexpected(int, u_int32_t, void *); 297void input_userauth_failure(int, u_int32_t, void *); 298void input_userauth_banner(int, u_int32_t, void *); 299void input_userauth_error(int, u_int32_t, void *); 300void input_userauth_info_req(int, u_int32_t, void *); 301void input_userauth_pk_ok(int, u_int32_t, void *); 302void input_userauth_passwd_changereq(int, u_int32_t, void *); 303 304int userauth_none(Authctxt *); 305int userauth_pubkey(Authctxt *); 306int userauth_passwd(Authctxt *); 307int userauth_kbdint(Authctxt *); 308int userauth_hostbased(Authctxt *); 309 310#ifdef GSSAPI 311int userauth_gssapi(Authctxt *authctxt); 312void input_gssapi_response(int type, u_int32_t, void *); 313void input_gssapi_token(int type, u_int32_t, void *); 314void input_gssapi_hash(int type, u_int32_t, void *); 315void input_gssapi_error(int, u_int32_t, void *); 316void input_gssapi_errtok(int, u_int32_t, void *); 317#endif 318 319void userauth(Authctxt *, char *); 320 321static int sign_and_send_pubkey(Authctxt *, Identity *); 322static void pubkey_prepare(Authctxt *); 323static void pubkey_cleanup(Authctxt *); 324static Key *load_identity_file(char *, int); 325 326static Authmethod *authmethod_get(char *authlist); 327static Authmethod *authmethod_lookup(const char *name); 328static char *authmethods_get(void); 329 330Authmethod authmethods[] = { 331#ifdef GSSAPI 332 {"gssapi-with-mic", 333 userauth_gssapi, 334 NULL, 335 &options.gss_authentication, 336 NULL}, 337#endif 338 {"hostbased", 339 userauth_hostbased, 340 NULL, 341 &options.hostbased_authentication, 342 NULL}, 343 {"publickey", 344 userauth_pubkey, 345 NULL, 346 &options.pubkey_authentication, 347 NULL}, 348 {"keyboard-interactive", 349 userauth_kbdint, 350 NULL, 351 &options.kbd_interactive_authentication, 352 &options.batch_mode}, 353 {"password", 354 userauth_passwd, 355 NULL, 356 &options.password_authentication, 357 &options.batch_mode}, 358 {"none", 359 userauth_none, 360 NULL, 361 NULL, 362 NULL}, 363 {NULL, NULL, NULL, NULL, NULL} 364}; 365 366void 367ssh_userauth2(const char *local_user, const char *server_user, char *host, 368 Sensitive *sensitive) 369{ 370 Authctxt authctxt; 371 int type; 372 373 if (options.challenge_response_authentication) 374 options.kbd_interactive_authentication = 1; 375 376 packet_start(SSH2_MSG_SERVICE_REQUEST); 377 packet_put_cstring("ssh-userauth"); 378 packet_send(); 379 debug("SSH2_MSG_SERVICE_REQUEST sent"); 380 packet_write_wait(); 381 type = packet_read(); 382 if (type != SSH2_MSG_SERVICE_ACCEPT) 383 fatal("Server denied authentication request: %d", type); 384 if (packet_remaining() > 0) { 385 char *reply = packet_get_string(NULL); 386 debug2("service_accept: %s", reply); 387 free(reply); 388 } else { 389 debug2("buggy server: service_accept w/o service"); 390 } 391 packet_check_eom(); 392 debug("SSH2_MSG_SERVICE_ACCEPT received"); 393 394 if (options.preferred_authentications == NULL) 395 options.preferred_authentications = authmethods_get(); 396 397 /* setup authentication context */ 398 memset(&authctxt, 0, sizeof(authctxt)); 399 pubkey_prepare(&authctxt); 400 authctxt.server_user = server_user; 401 authctxt.local_user = local_user; 402 authctxt.host = host; 403 authctxt.service = "ssh-connection"; /* service name */ 404 authctxt.success = 0; 405 authctxt.method = authmethod_lookup("none"); 406 authctxt.authlist = NULL; 407 authctxt.methoddata = NULL; 408 authctxt.sensitive = sensitive; 409 authctxt.info_req_seen = 0; 410 if (authctxt.method == NULL) 411 fatal("ssh_userauth2: internal error: cannot send userauth none request"); 412 413 /* initial userauth request */ 414 userauth_none(&authctxt); 415 416 dispatch_init(&input_userauth_error); 417 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); 418 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); 419 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); 420 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ 421 422 pubkey_cleanup(&authctxt); 423 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); 424 425#ifdef NONE_CIPHER_ENABLED 426 /* 427 * If the user explicitly requests to use the none cipher enable it 428 * post authentication and only if the right conditions are met: both 429 * of the NONE switches must be true and there must be no tty allocated. 430 */ 431 if (options.none_switch == 1 && options.none_enabled == 1) { 432 if (!tty_flag) { 433 debug("Requesting none cipher re-keying..."); 434 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none"; 435 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none"; 436 kex_prop2buf(&xxx_kex->my, myproposal); 437 packet_request_rekeying(); 438 fprintf(stderr, "WARNING: enabled NONE cipher\n"); 439 } else { 440 /* Requested NONE cipher on an interactive session. */ 441 debug("Cannot switch to NONE cipher with tty " 442 "allocated"); 443 fprintf(stderr, "NONE cipher switch disabled given " 444 "a TTY is allocated\n"); 445 } 446 } 447#endif 448 debug("Authentication succeeded (%s).", authctxt.method->name); 449} 450 451void 452userauth(Authctxt *authctxt, char *authlist) 453{ 454 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 455 authctxt->method->cleanup(authctxt); 456 457 free(authctxt->methoddata); 458 authctxt->methoddata = NULL; 459 if (authlist == NULL) { 460 authlist = authctxt->authlist; 461 } else { 462 free(authctxt->authlist); 463 authctxt->authlist = authlist; 464 } 465 for (;;) { 466 Authmethod *method = authmethod_get(authlist); 467 if (method == NULL) 468 fatal("Permission denied (%s).", authlist); 469 authctxt->method = method; 470 471 /* reset the per method handler */ 472 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, 473 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); 474 475 /* and try new method */ 476 if (method->userauth(authctxt) != 0) { 477 debug2("we sent a %s packet, wait for reply", method->name); 478 break; 479 } else { 480 debug2("we did not send a packet, disable method"); 481 method->enabled = NULL; 482 } 483 } 484} 485 486/* ARGSUSED */ 487void 488input_userauth_error(int type, u_int32_t seq, void *ctxt) 489{ 490 fatal("input_userauth_error: bad message during authentication: " 491 "type %d", type); 492} 493 494/* ARGSUSED */ 495void 496input_userauth_banner(int type, u_int32_t seq, void *ctxt) 497{ 498 char *msg, *raw, *lang; 499 u_int len; 500 501 debug3("input_userauth_banner"); 502 raw = packet_get_string(&len); 503 lang = packet_get_string(NULL); 504 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { 505 if (len > 65536) 506 len = 65536; 507 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ 508 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH); 509 fprintf(stderr, "%s", msg); 510 free(msg); 511 } 512 free(raw); 513 free(lang); 514} 515 516/* ARGSUSED */ 517void 518input_userauth_success(int type, u_int32_t seq, void *ctxt) 519{ 520 Authctxt *authctxt = ctxt; 521 522 if (authctxt == NULL) 523 fatal("input_userauth_success: no authentication context"); 524 free(authctxt->authlist); 525 authctxt->authlist = NULL; 526 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 527 authctxt->method->cleanup(authctxt); 528 free(authctxt->methoddata); 529 authctxt->methoddata = NULL; 530 authctxt->success = 1; /* break out */ 531} 532 533void 534input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) 535{ 536 Authctxt *authctxt = ctxt; 537 538 if (authctxt == NULL) 539 fatal("%s: no authentication context", __func__); 540 541 fatal("Unexpected authentication success during %s.", 542 authctxt->method->name); 543} 544 545/* ARGSUSED */ 546void 547input_userauth_failure(int type, u_int32_t seq, void *ctxt) 548{ 549 Authctxt *authctxt = ctxt; 550 char *authlist = NULL; 551 int partial; 552 553 if (authctxt == NULL) 554 fatal("input_userauth_failure: no authentication context"); 555 556 authlist = packet_get_string(NULL); 557 partial = packet_get_char(); 558 packet_check_eom(); 559 560 if (partial != 0) { 561 logit("Authenticated with partial success."); 562 /* reset state */ 563 pubkey_cleanup(authctxt); 564 pubkey_prepare(authctxt); 565 } 566 debug("Authentications that can continue: %s", authlist); 567 568 userauth(authctxt, authlist); 569} 570 571/* ARGSUSED */ 572void 573input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) 574{ 575 Authctxt *authctxt = ctxt; 576 Key *key = NULL; 577 Identity *id = NULL; 578 Buffer b; 579 int pktype, sent = 0; 580 u_int alen, blen; 581 char *pkalg, *fp; 582 u_char *pkblob; 583 584 if (authctxt == NULL) 585 fatal("input_userauth_pk_ok: no authentication context"); 586 if (datafellows & SSH_BUG_PKOK) { 587 /* this is similar to SSH_BUG_PKAUTH */ 588 debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); 589 pkblob = packet_get_string(&blen); 590 buffer_init(&b); 591 buffer_append(&b, pkblob, blen); 592 pkalg = buffer_get_string(&b, &alen); 593 buffer_free(&b); 594 } else { 595 pkalg = packet_get_string(&alen); 596 pkblob = packet_get_string(&blen); 597 } 598 packet_check_eom(); 599 600 debug("Server accepts key: pkalg %s blen %u", pkalg, blen); 601 602 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { 603 debug("unknown pkalg %s", pkalg); 604 goto done; 605 } 606 if ((key = key_from_blob(pkblob, blen)) == NULL) { 607 debug("no key from blob. pkalg %s", pkalg); 608 goto done; 609 } 610 if (key->type != pktype) { 611 error("input_userauth_pk_ok: type mismatch " 612 "for decoded key (received %d, expected %d)", 613 key->type, pktype); 614 goto done; 615 } 616 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); 617 debug2("input_userauth_pk_ok: fp %s", fp); 618 free(fp); 619 620 /* 621 * search keys in the reverse order, because last candidate has been 622 * moved to the end of the queue. this also avoids confusion by 623 * duplicate keys 624 */ 625 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { 626 if (key_equal(key, id->key)) { 627 sent = sign_and_send_pubkey(authctxt, id); 628 break; 629 } 630 } 631done: 632 if (key != NULL) 633 key_free(key); 634 free(pkalg); 635 free(pkblob); 636 637 /* try another method if we did not send a packet */ 638 if (sent == 0) 639 userauth(authctxt, NULL); 640} 641 642#ifdef GSSAPI 643int 644userauth_gssapi(Authctxt *authctxt) 645{ 646 Gssctxt *gssctxt = NULL; 647 static gss_OID_set gss_supported = NULL; 648 static u_int mech = 0; 649 OM_uint32 min; 650 int ok = 0; 651 652 /* Try one GSSAPI method at a time, rather than sending them all at 653 * once. */ 654 655 if (gss_supported == NULL) 656 gss_indicate_mechs(&min, &gss_supported); 657 658 /* Check to see if the mechanism is usable before we offer it */ 659 while (mech < gss_supported->count && !ok) { 660 /* My DER encoding requires length<128 */ 661 if (gss_supported->elements[mech].length < 128 && 662 ssh_gssapi_check_mechanism(&gssctxt, 663 &gss_supported->elements[mech], authctxt->host)) { 664 ok = 1; /* Mechanism works */ 665 } else { 666 mech++; 667 } 668 } 669 670 if (!ok) 671 return 0; 672 673 authctxt->methoddata=(void *)gssctxt; 674 675 packet_start(SSH2_MSG_USERAUTH_REQUEST); 676 packet_put_cstring(authctxt->server_user); 677 packet_put_cstring(authctxt->service); 678 packet_put_cstring(authctxt->method->name); 679 680 packet_put_int(1); 681 682 packet_put_int((gss_supported->elements[mech].length) + 2); 683 packet_put_char(SSH_GSS_OIDTYPE); 684 packet_put_char(gss_supported->elements[mech].length); 685 packet_put_raw(gss_supported->elements[mech].elements, 686 gss_supported->elements[mech].length); 687 688 packet_send(); 689 690 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); 691 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); 692 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); 693 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); 694 695 mech++; /* Move along to next candidate */ 696 697 return 1; 698} 699 700static OM_uint32 701process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) 702{ 703 Authctxt *authctxt = ctxt; 704 Gssctxt *gssctxt = authctxt->methoddata; 705 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 706 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; 707 gss_buffer_desc gssbuf; 708 OM_uint32 status, ms, flags; 709 Buffer b; 710 711 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 712 recv_tok, &send_tok, &flags); 713 714 if (send_tok.length > 0) { 715 if (GSS_ERROR(status)) 716 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); 717 else 718 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); 719 720 packet_put_string(send_tok.value, send_tok.length); 721 packet_send(); 722 gss_release_buffer(&ms, &send_tok); 723 } 724 725 if (status == GSS_S_COMPLETE) { 726 /* send either complete or MIC, depending on mechanism */ 727 if (!(flags & GSS_C_INTEG_FLAG)) { 728 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); 729 packet_send(); 730 } else { 731 ssh_gssapi_buildmic(&b, authctxt->server_user, 732 authctxt->service, "gssapi-with-mic"); 733 734 gssbuf.value = buffer_ptr(&b); 735 gssbuf.length = buffer_len(&b); 736 737 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); 738 739 if (!GSS_ERROR(status)) { 740 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); 741 packet_put_string(mic.value, mic.length); 742 743 packet_send(); 744 } 745 746 buffer_free(&b); 747 gss_release_buffer(&ms, &mic); 748 } 749 } 750 751 return status; 752} 753 754/* ARGSUSED */ 755void 756input_gssapi_response(int type, u_int32_t plen, void *ctxt) 757{ 758 Authctxt *authctxt = ctxt; 759 Gssctxt *gssctxt; 760 int oidlen; 761 char *oidv; 762 763 if (authctxt == NULL) 764 fatal("input_gssapi_response: no authentication context"); 765 gssctxt = authctxt->methoddata; 766 767 /* Setup our OID */ 768 oidv = packet_get_string(&oidlen); 769 770 if (oidlen <= 2 || 771 oidv[0] != SSH_GSS_OIDTYPE || 772 oidv[1] != oidlen - 2) { 773 free(oidv); 774 debug("Badly encoded mechanism OID received"); 775 userauth(authctxt, NULL); 776 return; 777 } 778 779 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) 780 fatal("Server returned different OID than expected"); 781 782 packet_check_eom(); 783 784 free(oidv); 785 786 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { 787 /* Start again with next method on list */ 788 debug("Trying to start again"); 789 userauth(authctxt, NULL); 790 return; 791 } 792} 793 794/* ARGSUSED */ 795void 796input_gssapi_token(int type, u_int32_t plen, void *ctxt) 797{ 798 Authctxt *authctxt = ctxt; 799 gss_buffer_desc recv_tok; 800 OM_uint32 status; 801 u_int slen; 802 803 if (authctxt == NULL) 804 fatal("input_gssapi_response: no authentication context"); 805 806 recv_tok.value = packet_get_string(&slen); 807 recv_tok.length = slen; /* safe typecast */ 808 809 packet_check_eom(); 810 811 status = process_gssapi_token(ctxt, &recv_tok); 812 813 free(recv_tok.value); 814 815 if (GSS_ERROR(status)) { 816 /* Start again with the next method in the list */ 817 userauth(authctxt, NULL); 818 return; 819 } 820} 821 822/* ARGSUSED */ 823void 824input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) 825{ 826 Authctxt *authctxt = ctxt; 827 Gssctxt *gssctxt; 828 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 829 gss_buffer_desc recv_tok; 830 OM_uint32 ms; 831 u_int len; 832 833 if (authctxt == NULL) 834 fatal("input_gssapi_response: no authentication context"); 835 gssctxt = authctxt->methoddata; 836 837 recv_tok.value = packet_get_string(&len); 838 recv_tok.length = len; 839 840 packet_check_eom(); 841 842 /* Stick it into GSSAPI and see what it says */ 843 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 844 &recv_tok, &send_tok, NULL); 845 846 free(recv_tok.value); 847 gss_release_buffer(&ms, &send_tok); 848 849 /* Server will be returning a failed packet after this one */ 850} 851 852/* ARGSUSED */ 853void 854input_gssapi_error(int type, u_int32_t plen, void *ctxt) 855{ 856 char *msg; 857 char *lang; 858 859 /* maj */(void)packet_get_int(); 860 /* min */(void)packet_get_int(); 861 msg=packet_get_string(NULL); 862 lang=packet_get_string(NULL); 863 864 packet_check_eom(); 865 866 debug("Server GSSAPI Error:\n%s", msg); 867 free(msg); 868 free(lang); 869} 870#endif /* GSSAPI */ 871 872int 873userauth_none(Authctxt *authctxt) 874{ 875 /* initial userauth request */ 876 packet_start(SSH2_MSG_USERAUTH_REQUEST); 877 packet_put_cstring(authctxt->server_user); 878 packet_put_cstring(authctxt->service); 879 packet_put_cstring(authctxt->method->name); 880 packet_send(); 881 return 1; 882} 883 884int 885userauth_passwd(Authctxt *authctxt) 886{ 887 static int attempt = 0; 888 char prompt[150]; 889 char *password; 890 const char *host = options.host_key_alias ? options.host_key_alias : 891 authctxt->host; 892 893 if (attempt++ >= options.number_of_password_prompts) 894 return 0; 895 896 if (attempt != 1) 897 error("Permission denied, please try again."); 898 899 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", 900 authctxt->server_user, host); 901 password = read_passphrase(prompt, 0); 902 packet_start(SSH2_MSG_USERAUTH_REQUEST); 903 packet_put_cstring(authctxt->server_user); 904 packet_put_cstring(authctxt->service); 905 packet_put_cstring(authctxt->method->name); 906 packet_put_char(0); 907 packet_put_cstring(password); 908 explicit_bzero(password, strlen(password)); 909 free(password); 910 packet_add_padding(64); 911 packet_send(); 912 913 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 914 &input_userauth_passwd_changereq); 915 916 return 1; 917} 918 919/* 920 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST 921 */ 922/* ARGSUSED */ 923void 924input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) 925{ 926 Authctxt *authctxt = ctxt; 927 char *info, *lang, *password = NULL, *retype = NULL; 928 char prompt[150]; 929 const char *host = options.host_key_alias ? options.host_key_alias : 930 authctxt->host; 931 932 debug2("input_userauth_passwd_changereq"); 933 934 if (authctxt == NULL) 935 fatal("input_userauth_passwd_changereq: " 936 "no authentication context"); 937 938 info = packet_get_string(NULL); 939 lang = packet_get_string(NULL); 940 if (strlen(info) > 0) 941 logit("%s", info); 942 free(info); 943 free(lang); 944 packet_start(SSH2_MSG_USERAUTH_REQUEST); 945 packet_put_cstring(authctxt->server_user); 946 packet_put_cstring(authctxt->service); 947 packet_put_cstring(authctxt->method->name); 948 packet_put_char(1); /* additional info */ 949 snprintf(prompt, sizeof(prompt), 950 "Enter %.30s@%.128s's old password: ", 951 authctxt->server_user, host); 952 password = read_passphrase(prompt, 0); 953 packet_put_cstring(password); 954 explicit_bzero(password, strlen(password)); 955 free(password); 956 password = NULL; 957 while (password == NULL) { 958 snprintf(prompt, sizeof(prompt), 959 "Enter %.30s@%.128s's new password: ", 960 authctxt->server_user, host); 961 password = read_passphrase(prompt, RP_ALLOW_EOF); 962 if (password == NULL) { 963 /* bail out */ 964 return; 965 } 966 snprintf(prompt, sizeof(prompt), 967 "Retype %.30s@%.128s's new password: ", 968 authctxt->server_user, host); 969 retype = read_passphrase(prompt, 0); 970 if (strcmp(password, retype) != 0) { 971 explicit_bzero(password, strlen(password)); 972 free(password); 973 logit("Mismatch; try again, EOF to quit."); 974 password = NULL; 975 } 976 explicit_bzero(retype, strlen(retype)); 977 free(retype); 978 } 979 packet_put_cstring(password); 980 explicit_bzero(password, strlen(password)); 981 free(password); 982 packet_add_padding(64); 983 packet_send(); 984 985 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 986 &input_userauth_passwd_changereq); 987} 988 989static int 990identity_sign(Identity *id, u_char **sigp, u_int *lenp, 991 u_char *data, u_int datalen) 992{ 993 Key *prv; 994 int ret; 995 996 /* the agent supports this key */ 997 if (id->ac) 998 return (ssh_agent_sign(id->ac, id->key, sigp, lenp, 999 data, datalen)); 1000 /* 1001 * we have already loaded the private key or 1002 * the private key is stored in external hardware 1003 */ 1004 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT)) 1005 return (key_sign(id->key, sigp, lenp, data, datalen)); 1006 /* load the private key from the file */ 1007 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL) 1008 return (-1); 1009 ret = key_sign(prv, sigp, lenp, data, datalen); 1010 key_free(prv); 1011 return (ret); 1012} 1013 1014static int 1015sign_and_send_pubkey(Authctxt *authctxt, Identity *id) 1016{ 1017 Buffer b; 1018 u_char *blob, *signature; 1019 u_int bloblen, slen; 1020 u_int skip = 0; 1021 int ret = -1; 1022 int have_sig = 1; 1023 char *fp; 1024 1025 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); 1026 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp); 1027 free(fp); 1028 1029 if (key_to_blob(id->key, &blob, &bloblen) == 0) { 1030 /* we cannot handle this key */ 1031 debug3("sign_and_send_pubkey: cannot handle key"); 1032 return 0; 1033 } 1034 /* data to be signed */ 1035 buffer_init(&b); 1036 if (datafellows & SSH_OLD_SESSIONID) { 1037 buffer_append(&b, session_id2, session_id2_len); 1038 skip = session_id2_len; 1039 } else { 1040 buffer_put_string(&b, session_id2, session_id2_len); 1041 skip = buffer_len(&b); 1042 } 1043 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 1044 buffer_put_cstring(&b, authctxt->server_user); 1045 buffer_put_cstring(&b, 1046 datafellows & SSH_BUG_PKSERVICE ? 1047 "ssh-userauth" : 1048 authctxt->service); 1049 if (datafellows & SSH_BUG_PKAUTH) { 1050 buffer_put_char(&b, have_sig); 1051 } else { 1052 buffer_put_cstring(&b, authctxt->method->name); 1053 buffer_put_char(&b, have_sig); 1054 buffer_put_cstring(&b, key_ssh_name(id->key)); 1055 } 1056 buffer_put_string(&b, blob, bloblen); 1057 1058 /* generate signature */ 1059 ret = identity_sign(id, &signature, &slen, 1060 buffer_ptr(&b), buffer_len(&b)); 1061 if (ret == -1) { 1062 free(blob); 1063 buffer_free(&b); 1064 return 0; 1065 } 1066#ifdef DEBUG_PK 1067 buffer_dump(&b); 1068#endif 1069 if (datafellows & SSH_BUG_PKSERVICE) { 1070 buffer_clear(&b); 1071 buffer_append(&b, session_id2, session_id2_len); 1072 skip = session_id2_len; 1073 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 1074 buffer_put_cstring(&b, authctxt->server_user); 1075 buffer_put_cstring(&b, authctxt->service); 1076 buffer_put_cstring(&b, authctxt->method->name); 1077 buffer_put_char(&b, have_sig); 1078 if (!(datafellows & SSH_BUG_PKAUTH)) 1079 buffer_put_cstring(&b, key_ssh_name(id->key)); 1080 buffer_put_string(&b, blob, bloblen); 1081 } 1082 free(blob); 1083 1084 /* append signature */ 1085 buffer_put_string(&b, signature, slen); 1086 free(signature); 1087 1088 /* skip session id and packet type */ 1089 if (buffer_len(&b) < skip + 1) 1090 fatal("userauth_pubkey: internal error"); 1091 buffer_consume(&b, skip + 1); 1092 1093 /* put remaining data from buffer into packet */ 1094 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1095 packet_put_raw(buffer_ptr(&b), buffer_len(&b)); 1096 buffer_free(&b); 1097 packet_send(); 1098 1099 return 1; 1100} 1101 1102static int 1103send_pubkey_test(Authctxt *authctxt, Identity *id) 1104{ 1105 u_char *blob; 1106 u_int bloblen, have_sig = 0; 1107 1108 debug3("send_pubkey_test"); 1109 1110 if (key_to_blob(id->key, &blob, &bloblen) == 0) { 1111 /* we cannot handle this key */ 1112 debug3("send_pubkey_test: cannot handle key"); 1113 return 0; 1114 } 1115 /* register callback for USERAUTH_PK_OK message */ 1116 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); 1117 1118 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1119 packet_put_cstring(authctxt->server_user); 1120 packet_put_cstring(authctxt->service); 1121 packet_put_cstring(authctxt->method->name); 1122 packet_put_char(have_sig); 1123 if (!(datafellows & SSH_BUG_PKAUTH)) 1124 packet_put_cstring(key_ssh_name(id->key)); 1125 packet_put_string(blob, bloblen); 1126 free(blob); 1127 packet_send(); 1128 return 1; 1129} 1130 1131static Key * 1132load_identity_file(char *filename, int userprovided) 1133{ 1134 Key *private; 1135 char prompt[300], *passphrase; 1136 int perm_ok = 0, quit, i; 1137 struct stat st; 1138 1139 if (stat(filename, &st) < 0) { 1140 (userprovided ? logit : debug3)("no such identity: %s: %s", 1141 filename, strerror(errno)); 1142 return NULL; 1143 } 1144 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); 1145 if (!perm_ok) { 1146 if (private != NULL) 1147 key_free(private); 1148 return NULL; 1149 } 1150 if (private == NULL) { 1151 if (options.batch_mode) 1152 return NULL; 1153 snprintf(prompt, sizeof prompt, 1154 "Enter passphrase for key '%.100s': ", filename); 1155 for (i = 0; i < options.number_of_password_prompts; i++) { 1156 passphrase = read_passphrase(prompt, 0); 1157 if (strcmp(passphrase, "") != 0) { 1158 private = key_load_private_type(KEY_UNSPEC, 1159 filename, passphrase, NULL, NULL); 1160 quit = 0; 1161 } else { 1162 debug2("no passphrase given, try next key"); 1163 quit = 1; 1164 } 1165 explicit_bzero(passphrase, strlen(passphrase)); 1166 free(passphrase); 1167 if (private != NULL || quit) 1168 break; 1169 debug2("bad passphrase given, try again..."); 1170 } 1171 } 1172 return private; 1173} 1174 1175/* 1176 * try keys in the following order: 1177 * 1. agent keys that are found in the config file 1178 * 2. other agent keys 1179 * 3. keys that are only listed in the config file 1180 */ 1181static void 1182pubkey_prepare(Authctxt *authctxt) 1183{ 1184 Identity *id, *id2, *tmp; 1185 Idlist agent, files, *preferred; 1186 Key *key; 1187 AuthenticationConnection *ac; 1188 char *comment; 1189 int i, found; 1190 1191 TAILQ_INIT(&agent); /* keys from the agent */ 1192 TAILQ_INIT(&files); /* keys from the config file */ 1193 preferred = &authctxt->keys; 1194 TAILQ_INIT(preferred); /* preferred order of keys */ 1195 1196 /* list of keys stored in the filesystem and PKCS#11 */ 1197 for (i = 0; i < options.num_identity_files; i++) { 1198 key = options.identity_keys[i]; 1199 if (key && key->type == KEY_RSA1) 1200 continue; 1201 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER) 1202 continue; 1203 options.identity_keys[i] = NULL; 1204 id = xcalloc(1, sizeof(*id)); 1205 id->key = key; 1206 id->filename = xstrdup(options.identity_files[i]); 1207 id->userprovided = options.identity_file_userprovided[i]; 1208 TAILQ_INSERT_TAIL(&files, id, next); 1209 } 1210 /* Prefer PKCS11 keys that are explicitly listed */ 1211 TAILQ_FOREACH_SAFE(id, &files, next, tmp) { 1212 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0) 1213 continue; 1214 found = 0; 1215 TAILQ_FOREACH(id2, &files, next) { 1216 if (id2->key == NULL || 1217 (id2->key->flags & KEY_FLAG_EXT) != 0) 1218 continue; 1219 if (key_equal(id->key, id2->key)) { 1220 TAILQ_REMOVE(&files, id, next); 1221 TAILQ_INSERT_TAIL(preferred, id, next); 1222 found = 1; 1223 break; 1224 } 1225 } 1226 /* If IdentitiesOnly set and key not found then don't use it */ 1227 if (!found && options.identities_only) { 1228 TAILQ_REMOVE(&files, id, next); 1229 explicit_bzero(id, sizeof(*id)); 1230 free(id); 1231 } 1232 } 1233 /* list of keys supported by the agent */ 1234 if ((ac = ssh_get_authentication_connection())) { 1235 for (key = ssh_get_first_identity(ac, &comment, 2); 1236 key != NULL; 1237 key = ssh_get_next_identity(ac, &comment, 2)) { 1238 found = 0; 1239 TAILQ_FOREACH(id, &files, next) { 1240 /* agent keys from the config file are preferred */ 1241 if (key_equal(key, id->key)) { 1242 key_free(key); 1243 free(comment); 1244 TAILQ_REMOVE(&files, id, next); 1245 TAILQ_INSERT_TAIL(preferred, id, next); 1246 id->ac = ac; 1247 found = 1; 1248 break; 1249 } 1250 } 1251 if (!found && !options.identities_only) { 1252 id = xcalloc(1, sizeof(*id)); 1253 id->key = key; 1254 id->filename = comment; 1255 id->ac = ac; 1256 TAILQ_INSERT_TAIL(&agent, id, next); 1257 } 1258 } 1259 /* append remaining agent keys */ 1260 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { 1261 TAILQ_REMOVE(&agent, id, next); 1262 TAILQ_INSERT_TAIL(preferred, id, next); 1263 } 1264 authctxt->agent = ac; 1265 } 1266 /* append remaining keys from the config file */ 1267 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { 1268 TAILQ_REMOVE(&files, id, next); 1269 TAILQ_INSERT_TAIL(preferred, id, next); 1270 } 1271 TAILQ_FOREACH(id, preferred, next) { 1272 debug2("key: %s (%p),%s", id->filename, id->key, 1273 id->userprovided ? " explicit" : ""); 1274 } 1275} 1276 1277static void 1278pubkey_cleanup(Authctxt *authctxt) 1279{ 1280 Identity *id; 1281 1282 if (authctxt->agent != NULL) 1283 ssh_close_authentication_connection(authctxt->agent); 1284 for (id = TAILQ_FIRST(&authctxt->keys); id; 1285 id = TAILQ_FIRST(&authctxt->keys)) { 1286 TAILQ_REMOVE(&authctxt->keys, id, next); 1287 if (id->key) 1288 key_free(id->key); 1289 free(id->filename); 1290 free(id); 1291 } 1292} 1293 1294int 1295userauth_pubkey(Authctxt *authctxt) 1296{ 1297 Identity *id; 1298 int sent = 0; 1299 1300 while ((id = TAILQ_FIRST(&authctxt->keys))) { 1301 if (id->tried++) 1302 return (0); 1303 /* move key to the end of the queue */ 1304 TAILQ_REMOVE(&authctxt->keys, id, next); 1305 TAILQ_INSERT_TAIL(&authctxt->keys, id, next); 1306 /* 1307 * send a test message if we have the public key. for 1308 * encrypted keys we cannot do this and have to load the 1309 * private key instead 1310 */ 1311 if (id->key != NULL) { 1312 if (key_type_plain(id->key->type) == KEY_RSA && 1313 (datafellows & SSH_BUG_RSASIGMD5) != 0) { 1314 debug("Skipped %s key %s for RSA/MD5 server", 1315 key_type(id->key), id->filename); 1316 } else if (id->key->type != KEY_RSA1) { 1317 debug("Offering %s public key: %s", 1318 key_type(id->key), id->filename); 1319 sent = send_pubkey_test(authctxt, id); 1320 } 1321 } else { 1322 debug("Trying private key: %s", id->filename); 1323 id->key = load_identity_file(id->filename, 1324 id->userprovided); 1325 if (id->key != NULL) { 1326 id->isprivate = 1; 1327 if (key_type_plain(id->key->type) == KEY_RSA && 1328 (datafellows & SSH_BUG_RSASIGMD5) != 0) { 1329 debug("Skipped %s key %s for RSA/MD5 " 1330 "server", key_type(id->key), 1331 id->filename); 1332 } else { 1333 sent = sign_and_send_pubkey( 1334 authctxt, id); 1335 } 1336 key_free(id->key); 1337 id->key = NULL; 1338 } 1339 } 1340 if (sent) 1341 return (sent); 1342 } 1343 return (0); 1344} 1345 1346/* 1347 * Send userauth request message specifying keyboard-interactive method. 1348 */ 1349int 1350userauth_kbdint(Authctxt *authctxt) 1351{ 1352 static int attempt = 0; 1353 1354 if (attempt++ >= options.number_of_password_prompts) 1355 return 0; 1356 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ 1357 if (attempt > 1 && !authctxt->info_req_seen) { 1358 debug3("userauth_kbdint: disable: no info_req_seen"); 1359 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); 1360 return 0; 1361 } 1362 1363 debug2("userauth_kbdint"); 1364 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1365 packet_put_cstring(authctxt->server_user); 1366 packet_put_cstring(authctxt->service); 1367 packet_put_cstring(authctxt->method->name); 1368 packet_put_cstring(""); /* lang */ 1369 packet_put_cstring(options.kbd_interactive_devices ? 1370 options.kbd_interactive_devices : ""); 1371 packet_send(); 1372 1373 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); 1374 return 1; 1375} 1376 1377/* 1378 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE 1379 */ 1380void 1381input_userauth_info_req(int type, u_int32_t seq, void *ctxt) 1382{ 1383 Authctxt *authctxt = ctxt; 1384 char *name, *inst, *lang, *prompt, *response; 1385 u_int num_prompts, i; 1386 int echo = 0; 1387 1388 debug2("input_userauth_info_req"); 1389 1390 if (authctxt == NULL) 1391 fatal("input_userauth_info_req: no authentication context"); 1392 1393 authctxt->info_req_seen = 1; 1394 1395 name = packet_get_string(NULL); 1396 inst = packet_get_string(NULL); 1397 lang = packet_get_string(NULL); 1398 if (strlen(name) > 0) 1399 logit("%s", name); 1400 if (strlen(inst) > 0) 1401 logit("%s", inst); 1402 free(name); 1403 free(inst); 1404 free(lang); 1405 1406 num_prompts = packet_get_int(); 1407 /* 1408 * Begin to build info response packet based on prompts requested. 1409 * We commit to providing the correct number of responses, so if 1410 * further on we run into a problem that prevents this, we have to 1411 * be sure and clean this up and send a correct error response. 1412 */ 1413 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); 1414 packet_put_int(num_prompts); 1415 1416 debug2("input_userauth_info_req: num_prompts %d", num_prompts); 1417 for (i = 0; i < num_prompts; i++) { 1418 prompt = packet_get_string(NULL); 1419 echo = packet_get_char(); 1420 1421 response = read_passphrase(prompt, echo ? RP_ECHO : 0); 1422 1423 packet_put_cstring(response); 1424 explicit_bzero(response, strlen(response)); 1425 free(response); 1426 free(prompt); 1427 } 1428 packet_check_eom(); /* done with parsing incoming message. */ 1429 1430 packet_add_padding(64); 1431 packet_send(); 1432} 1433 1434static int 1435ssh_keysign(Key *key, u_char **sigp, u_int *lenp, 1436 u_char *data, u_int datalen) 1437{ 1438 Buffer b; 1439 struct stat st; 1440 pid_t pid; 1441 int to[2], from[2], status, version = 2; 1442 1443 debug2("ssh_keysign called"); 1444 1445 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { 1446 error("ssh_keysign: not installed: %s", strerror(errno)); 1447 return -1; 1448 } 1449 if (fflush(stdout) != 0) 1450 error("ssh_keysign: fflush: %s", strerror(errno)); 1451 if (pipe(to) < 0) { 1452 error("ssh_keysign: pipe: %s", strerror(errno)); 1453 return -1; 1454 } 1455 if (pipe(from) < 0) { 1456 error("ssh_keysign: pipe: %s", strerror(errno)); 1457 return -1; 1458 } 1459 if ((pid = fork()) < 0) { 1460 error("ssh_keysign: fork: %s", strerror(errno)); 1461 return -1; 1462 } 1463 if (pid == 0) { 1464 /* keep the socket on exec */ 1465 fcntl(packet_get_connection_in(), F_SETFD, 0); 1466 permanently_drop_suid(getuid()); 1467 close(from[0]); 1468 if (dup2(from[1], STDOUT_FILENO) < 0) 1469 fatal("ssh_keysign: dup2: %s", strerror(errno)); 1470 close(to[1]); 1471 if (dup2(to[0], STDIN_FILENO) < 0) 1472 fatal("ssh_keysign: dup2: %s", strerror(errno)); 1473 close(from[1]); 1474 close(to[0]); 1475 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); 1476 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, 1477 strerror(errno)); 1478 } 1479 close(from[1]); 1480 close(to[0]); 1481 1482 buffer_init(&b); 1483 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ 1484 buffer_put_string(&b, data, datalen); 1485 if (ssh_msg_send(to[1], version, &b) == -1) 1486 fatal("ssh_keysign: couldn't send request"); 1487 1488 if (ssh_msg_recv(from[0], &b) < 0) { 1489 error("ssh_keysign: no reply"); 1490 buffer_free(&b); 1491 return -1; 1492 } 1493 close(from[0]); 1494 close(to[1]); 1495 1496 while (waitpid(pid, &status, 0) < 0) 1497 if (errno != EINTR) 1498 break; 1499 1500 if (buffer_get_char(&b) != version) { 1501 error("ssh_keysign: bad version"); 1502 buffer_free(&b); 1503 return -1; 1504 } 1505 *sigp = buffer_get_string(&b, lenp); 1506 buffer_free(&b); 1507 1508 return 0; 1509} 1510 1511int 1512userauth_hostbased(Authctxt *authctxt) 1513{ 1514 Key *private = NULL; 1515 Sensitive *sensitive = authctxt->sensitive; 1516 Buffer b; 1517 u_char *signature, *blob; 1518 char *chost, *pkalg, *p; 1519 const char *service; 1520 u_int blen, slen; 1521 int ok, i, found = 0; 1522 1523 /* check for a useful key */ 1524 for (i = 0; i < sensitive->nkeys; i++) { 1525 private = sensitive->keys[i]; 1526 if (private && private->type != KEY_RSA1) { 1527 found = 1; 1528 /* we take and free the key */ 1529 sensitive->keys[i] = NULL; 1530 break; 1531 } 1532 } 1533 if (!found) { 1534 debug("No more client hostkeys for hostbased authentication."); 1535 return 0; 1536 } 1537 if (key_to_blob(private, &blob, &blen) == 0) { 1538 key_free(private); 1539 return 0; 1540 } 1541 /* figure out a name for the client host */ 1542 p = get_local_name(packet_get_connection_in()); 1543 if (p == NULL) { 1544 error("userauth_hostbased: cannot get local ipaddr/name"); 1545 key_free(private); 1546 free(blob); 1547 return 0; 1548 } 1549 xasprintf(&chost, "%s.", p); 1550 debug2("userauth_hostbased: chost %s", chost); 1551 free(p); 1552 1553 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : 1554 authctxt->service; 1555 pkalg = xstrdup(key_ssh_name(private)); 1556 buffer_init(&b); 1557 /* construct data */ 1558 buffer_put_string(&b, session_id2, session_id2_len); 1559 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 1560 buffer_put_cstring(&b, authctxt->server_user); 1561 buffer_put_cstring(&b, service); 1562 buffer_put_cstring(&b, authctxt->method->name); 1563 buffer_put_cstring(&b, pkalg); 1564 buffer_put_string(&b, blob, blen); 1565 buffer_put_cstring(&b, chost); 1566 buffer_put_cstring(&b, authctxt->local_user); 1567#ifdef DEBUG_PK 1568 buffer_dump(&b); 1569#endif 1570 if (sensitive->external_keysign) 1571 ok = ssh_keysign(private, &signature, &slen, 1572 buffer_ptr(&b), buffer_len(&b)); 1573 else 1574 ok = key_sign(private, &signature, &slen, 1575 buffer_ptr(&b), buffer_len(&b)); 1576 key_free(private); 1577 buffer_free(&b); 1578 if (ok != 0) { 1579 error("key_sign failed"); 1580 free(chost); 1581 free(pkalg); 1582 free(blob); 1583 return 0; 1584 } 1585 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1586 packet_put_cstring(authctxt->server_user); 1587 packet_put_cstring(authctxt->service); 1588 packet_put_cstring(authctxt->method->name); 1589 packet_put_cstring(pkalg); 1590 packet_put_string(blob, blen); 1591 packet_put_cstring(chost); 1592 packet_put_cstring(authctxt->local_user); 1593 packet_put_string(signature, slen); 1594 explicit_bzero(signature, slen); 1595 free(signature); 1596 free(chost); 1597 free(pkalg); 1598 free(blob); 1599 1600 packet_send(); 1601 return 1; 1602} 1603 1604/* find auth method */ 1605 1606/* 1607 * given auth method name, if configurable options permit this method fill 1608 * in auth_ident field and return true, otherwise return false. 1609 */ 1610static int 1611authmethod_is_enabled(Authmethod *method) 1612{ 1613 if (method == NULL) 1614 return 0; 1615 /* return false if options indicate this method is disabled */ 1616 if (method->enabled == NULL || *method->enabled == 0) 1617 return 0; 1618 /* return false if batch mode is enabled but method needs interactive mode */ 1619 if (method->batch_flag != NULL && *method->batch_flag != 0) 1620 return 0; 1621 return 1; 1622} 1623 1624static Authmethod * 1625authmethod_lookup(const char *name) 1626{ 1627 Authmethod *method = NULL; 1628 if (name != NULL) 1629 for (method = authmethods; method->name != NULL; method++) 1630 if (strcmp(name, method->name) == 0) 1631 return method; 1632 debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); 1633 return NULL; 1634} 1635 1636/* XXX internal state */ 1637static Authmethod *current = NULL; 1638static char *supported = NULL; 1639static char *preferred = NULL; 1640 1641/* 1642 * Given the authentication method list sent by the server, return the 1643 * next method we should try. If the server initially sends a nil list, 1644 * use a built-in default list. 1645 */ 1646static Authmethod * 1647authmethod_get(char *authlist) 1648{ 1649 char *name = NULL; 1650 u_int next; 1651 1652 /* Use a suitable default if we're passed a nil list. */ 1653 if (authlist == NULL || strlen(authlist) == 0) 1654 authlist = options.preferred_authentications; 1655 1656 if (supported == NULL || strcmp(authlist, supported) != 0) { 1657 debug3("start over, passed a different list %s", authlist); 1658 free(supported); 1659 supported = xstrdup(authlist); 1660 preferred = options.preferred_authentications; 1661 debug3("preferred %s", preferred); 1662 current = NULL; 1663 } else if (current != NULL && authmethod_is_enabled(current)) 1664 return current; 1665 1666 for (;;) { 1667 if ((name = match_list(preferred, supported, &next)) == NULL) { 1668 debug("No more authentication methods to try."); 1669 current = NULL; 1670 return NULL; 1671 } 1672 preferred += next; 1673 debug3("authmethod_lookup %s", name); 1674 debug3("remaining preferred: %s", preferred); 1675 if ((current = authmethod_lookup(name)) != NULL && 1676 authmethod_is_enabled(current)) { 1677 debug3("authmethod_is_enabled %s", name); 1678 debug("Next authentication method: %s", name); 1679 free(name); 1680 return current; 1681 } 1682 free(name); 1683 } 1684} 1685 1686static char * 1687authmethods_get(void) 1688{ 1689 Authmethod *method = NULL; 1690 Buffer b; 1691 char *list; 1692 1693 buffer_init(&b); 1694 for (method = authmethods; method->name != NULL; method++) { 1695 if (authmethod_is_enabled(method)) { 1696 if (buffer_len(&b) > 0) 1697 buffer_append(&b, ",", 1); 1698 buffer_append(&b, method->name, strlen(method->name)); 1699 } 1700 } 1701 buffer_append(&b, "\0", 1); 1702 list = xstrdup(buffer_ptr(&b)); 1703 buffer_free(&b); 1704 return list; 1705} 1706 1707