1 /* $NetBSD: sshconnect2.c,v 1.22 2015/08/13 10:33:21 christos Exp $ */ 2 /* $OpenBSD: sshconnect2.c,v 1.226 2015/07/30 00:01:34 djm Exp $ */ 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 __RCSID("$NetBSD: sshconnect2.c,v 1.22 2015/08/13 10:33:21 christos Exp $"); 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 #include <sys/wait.h> 33 #include <sys/queue.h> 34 #include <sys/stat.h> 35 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <netdb.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <signal.h> 42 #include <pwd.h> 43 #include <unistd.h> 44 #include <vis.h> 45 46 #ifdef KRB5 47 #include <krb5.h> 48 #endif 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 "misc.h" 66 #include "readconf.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 #include "ssherr.h" 75 76 #ifdef GSSAPI 77 #include "ssh-gss.h" 78 #endif 79 80 /* import */ 81 extern char *client_version_string; 82 extern char *server_version_string; 83 extern Options options; 84 85 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */ 86 /* if it is set then prevent the switch to the null cipher */ 87 88 extern int tty_flag; 89 90 /* 91 * SSH2 key exchange 92 */ 93 94 u_char *session_id2 = NULL; 95 u_int session_id2_len = 0; 96 97 char *xxx_host; 98 struct sockaddr *xxx_hostaddr; 99 100 static int 101 verify_host_key_callback(Key *hostkey, struct ssh *ssh) 102 { 103 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) 104 fatal("Host key verification failed."); 105 return 0; 106 } 107 108 static char * 109 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) 110 { 111 char *oavail, *avail, *first, *last, *alg, *hostname, *ret; 112 size_t maxlen; 113 struct hostkeys *hostkeys; 114 int ktype; 115 u_int i; 116 117 /* Find all hostkeys for this hostname */ 118 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); 119 hostkeys = init_hostkeys(); 120 for (i = 0; i < options.num_user_hostfiles; i++) 121 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]); 122 for (i = 0; i < options.num_system_hostfiles; i++) 123 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]); 124 125 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG); 126 maxlen = strlen(avail) + 1; 127 first = xmalloc(maxlen); 128 last = xmalloc(maxlen); 129 *first = *last = '\0'; 130 131 #define ALG_APPEND(to, from) \ 132 do { \ 133 if (*to != '\0') \ 134 strlcat(to, ",", maxlen); \ 135 strlcat(to, from, maxlen); \ 136 } while (0) 137 138 while ((alg = strsep(&avail, ",")) && *alg != '\0') { 139 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) 140 fatal("%s: unknown alg %s", __func__, alg); 141 if (lookup_key_in_hostkeys_by_type(hostkeys, 142 sshkey_type_plain(ktype), NULL)) 143 ALG_APPEND(first, alg); 144 else 145 ALG_APPEND(last, alg); 146 } 147 #undef ALG_APPEND 148 xasprintf(&ret, "%s%s%s", first, 149 (*first == '\0' || *last == '\0') ? "" : ",", last); 150 if (*first != '\0') 151 debug3("%s: prefer hostkeyalgs: %s", __func__, first); 152 153 free(first); 154 free(last); 155 free(hostname); 156 free(oavail); 157 free_hostkeys(hostkeys); 158 159 return ret; 160 } 161 162 void 163 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) 164 { 165 const char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; 166 struct kex *kex; 167 int r; 168 169 xxx_host = host; 170 xxx_hostaddr = hostaddr; 171 172 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal( 173 options.kex_algorithms); 174 myproposal[PROPOSAL_ENC_ALGS_CTOS] = 175 compat_cipher_proposal(options.ciphers); 176 myproposal[PROPOSAL_ENC_ALGS_STOC] = 177 compat_cipher_proposal(options.ciphers); 178 if (options.compression) { 179 myproposal[PROPOSAL_COMP_ALGS_CTOS] = 180 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; 181 } else { 182 myproposal[PROPOSAL_COMP_ALGS_CTOS] = 183 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; 184 } 185 myproposal[PROPOSAL_MAC_ALGS_CTOS] = 186 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; 187 if (options.hostkeyalgorithms != NULL) { 188 if (kex_assemble_names(KEX_DEFAULT_PK_ALG, 189 &options.hostkeyalgorithms) != 0) 190 fatal("%s: kex_assemble_namelist", __func__); 191 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 192 compat_pkalg_proposal(options.hostkeyalgorithms); 193 } else { 194 /* Enforce default */ 195 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG); 196 /* Prefer algorithms that we already have keys for */ 197 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 198 compat_pkalg_proposal( 199 order_hostkeyalgs(host, hostaddr, port)); 200 } 201 202 if (options.rekey_limit || options.rekey_interval) 203 packet_set_rekey_limits((u_int32_t)options.rekey_limit, 204 (time_t)options.rekey_interval); 205 206 /* start key exchange */ 207 if ((r = kex_setup(active_state, myproposal)) != 0) 208 fatal("kex_setup: %s", ssh_err(r)); 209 kex = active_state->kex; 210 #ifdef WITH_OPENSSL 211 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; 212 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; 213 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 214 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 215 kex->kex[KEX_ECDH_SHA2] = kexecdh_client; 216 #endif 217 kex->kex[KEX_C25519_SHA256] = kexc25519_client; 218 kex->client_version_string=client_version_string; 219 kex->server_version_string=server_version_string; 220 kex->verify_host_key=&verify_host_key_callback; 221 222 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state); 223 224 if (options.use_roaming && !kex->roaming) { 225 debug("Roaming not allowed by server"); 226 options.use_roaming = 0; 227 } 228 229 session_id2 = kex->session_id; 230 session_id2_len = kex->session_id_len; 231 232 #ifdef DEBUG_KEXDH 233 /* send 1st encrypted/maced/compressed message */ 234 packet_start(SSH2_MSG_IGNORE); 235 packet_put_cstring("markus"); 236 packet_send(); 237 packet_write_wait(); 238 #endif 239 } 240 241 /* 242 * Authenticate user 243 */ 244 245 typedef struct cauthctxt Authctxt; 246 typedef struct cauthmethod Authmethod; 247 typedef struct identity Identity; 248 typedef struct idlist Idlist; 249 250 struct identity { 251 TAILQ_ENTRY(identity) next; 252 int agent_fd; /* >=0 if agent supports key */ 253 struct sshkey *key; /* public/private key */ 254 char *filename; /* comment for agent-only keys */ 255 int tried; 256 int isprivate; /* key points to the private key */ 257 int userprovided; 258 }; 259 TAILQ_HEAD(idlist, identity); 260 261 struct cauthctxt { 262 const char *server_user; 263 const char *local_user; 264 const char *host; 265 const char *service; 266 struct cauthmethod *method; 267 sig_atomic_t success; 268 char *authlist; 269 int attempt; 270 /* pubkey */ 271 struct idlist keys; 272 int agent_fd; 273 /* hostbased */ 274 Sensitive *sensitive; 275 char *oktypes, *ktypes; 276 const char *active_ktype; 277 /* kbd-interactive */ 278 int info_req_seen; 279 /* generic */ 280 void *methoddata; 281 }; 282 283 struct cauthmethod { 284 const char *name; /* string to compare against server's list */ 285 int (*userauth)(Authctxt *authctxt); 286 void (*cleanup)(Authctxt *authctxt); 287 int *enabled; /* flag in option struct that enables method */ 288 int *batch_flag; /* flag in option struct that disables method */ 289 }; 290 291 int input_userauth_success(int, u_int32_t, void *); 292 int input_userauth_success_unexpected(int, u_int32_t, void *); 293 int input_userauth_failure(int, u_int32_t, void *); 294 int input_userauth_banner(int, u_int32_t, void *); 295 int input_userauth_error(int, u_int32_t, void *); 296 int input_userauth_info_req(int, u_int32_t, void *); 297 int input_userauth_pk_ok(int, u_int32_t, void *); 298 int input_userauth_passwd_changereq(int, u_int32_t, void *); 299 300 int userauth_none(Authctxt *); 301 int userauth_pubkey(Authctxt *); 302 int userauth_passwd(Authctxt *); 303 int userauth_kbdint(Authctxt *); 304 int userauth_hostbased(Authctxt *); 305 int userauth_kerberos(Authctxt *); 306 307 #ifdef GSSAPI 308 int userauth_gssapi(Authctxt *authctxt); 309 int input_gssapi_response(int type, u_int32_t, void *); 310 int input_gssapi_token(int type, u_int32_t, void *); 311 int input_gssapi_hash(int type, u_int32_t, void *); 312 int input_gssapi_error(int, u_int32_t, void *); 313 int input_gssapi_errtok(int, u_int32_t, void *); 314 #endif 315 316 void userauth(Authctxt *, char *); 317 318 static int sign_and_send_pubkey(Authctxt *, Identity *); 319 static void pubkey_prepare(Authctxt *); 320 static void pubkey_cleanup(Authctxt *); 321 static Key *load_identity_file(char *, int); 322 323 static Authmethod *authmethod_get(char *authlist); 324 static Authmethod *authmethod_lookup(const char *name); 325 static char *authmethods_get(void); 326 327 Authmethod authmethods[] = { 328 #ifdef GSSAPI 329 {"gssapi-with-mic", 330 userauth_gssapi, 331 NULL, 332 &options.gss_authentication, 333 NULL}, 334 #endif 335 {"hostbased", 336 userauth_hostbased, 337 NULL, 338 &options.hostbased_authentication, 339 NULL}, 340 #if KRB5 341 {"kerberos-2@ssh.com", 342 userauth_kerberos, 343 NULL, 344 &options.kerberos_authentication, 345 NULL}, 346 #endif 347 {"publickey", 348 userauth_pubkey, 349 NULL, 350 &options.pubkey_authentication, 351 NULL}, 352 {"keyboard-interactive", 353 userauth_kbdint, 354 NULL, 355 &options.kbd_interactive_authentication, 356 &options.batch_mode}, 357 {"password", 358 userauth_passwd, 359 NULL, 360 &options.password_authentication, 361 &options.batch_mode}, 362 {"none", 363 userauth_none, 364 NULL, 365 NULL, 366 NULL}, 367 {NULL, NULL, NULL, NULL, NULL} 368 }; 369 370 void 371 ssh_userauth2(const char *local_user, const char *server_user, char *host, 372 Sensitive *sensitive) 373 { 374 Authctxt authctxt; 375 int type; 376 377 if (options.challenge_response_authentication) 378 options.kbd_interactive_authentication = 1; 379 380 packet_start(SSH2_MSG_SERVICE_REQUEST); 381 packet_put_cstring("ssh-userauth"); 382 packet_send(); 383 debug("SSH2_MSG_SERVICE_REQUEST sent"); 384 packet_write_wait(); 385 type = packet_read(); 386 if (type != SSH2_MSG_SERVICE_ACCEPT) 387 fatal("Server denied authentication request: %d", type); 388 if (packet_remaining() > 0) { 389 char *reply = packet_get_string(NULL); 390 debug2("service_accept: %s", reply); 391 free(reply); 392 } else { 393 debug2("buggy server: service_accept w/o service"); 394 } 395 packet_check_eom(); 396 debug("SSH2_MSG_SERVICE_ACCEPT received"); 397 398 if (options.preferred_authentications == NULL) 399 options.preferred_authentications = authmethods_get(); 400 401 /* setup authentication context */ 402 memset(&authctxt, 0, sizeof(authctxt)); 403 pubkey_prepare(&authctxt); 404 authctxt.server_user = server_user; 405 authctxt.local_user = local_user; 406 authctxt.host = host; 407 authctxt.service = "ssh-connection"; /* service name */ 408 authctxt.success = 0; 409 authctxt.method = authmethod_lookup("none"); 410 authctxt.authlist = NULL; 411 authctxt.methoddata = NULL; 412 authctxt.sensitive = sensitive; 413 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL; 414 authctxt.info_req_seen = 0; 415 authctxt.agent_fd = -1; 416 if (authctxt.method == NULL) 417 fatal("ssh_userauth2: internal error: cannot send userauth none request"); 418 419 /* initial userauth request */ 420 userauth_none(&authctxt); 421 422 dispatch_init(&input_userauth_error); 423 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); 424 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); 425 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); 426 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ 427 428 pubkey_cleanup(&authctxt); 429 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); 430 431 /* if the user wants to use the none cipher do it */ 432 /* post authentication and only if the right conditions are met */ 433 /* both of the NONE commands must be true and there must be no */ 434 /* tty allocated */ 435 if ((options.none_switch == 1) && (options.none_enabled == 1)) 436 { 437 if (!tty_flag) /* no null on tty sessions */ 438 { 439 const char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; 440 debug("Requesting none rekeying..."); 441 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none"; 442 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none"; 443 kex_prop2buf(active_state->kex->my, myproposal); 444 ssh_packet_request_rekeying(); 445 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n"); 446 } 447 else 448 { 449 /* requested NONE cipher when in a tty */ 450 debug("Cannot switch to NONE cipher with tty allocated"); 451 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n"); 452 } 453 } 454 debug("Authentication succeeded (%s).", authctxt.method->name); 455 } 456 457 void 458 userauth(Authctxt *authctxt, char *authlist) 459 { 460 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 461 authctxt->method->cleanup(authctxt); 462 463 free(authctxt->methoddata); 464 authctxt->methoddata = NULL; 465 if (authlist == NULL) { 466 authlist = authctxt->authlist; 467 } else { 468 free(authctxt->authlist); 469 authctxt->authlist = authlist; 470 } 471 for (;;) { 472 Authmethod *method = authmethod_get(authlist); 473 if (method == NULL) 474 fatal("Permission denied (%s).", authlist); 475 authctxt->method = method; 476 477 /* reset the per method handler */ 478 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, 479 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); 480 481 /* and try new method */ 482 if (method->userauth(authctxt) != 0) { 483 debug2("we sent a %s packet, wait for reply", method->name); 484 break; 485 } else { 486 debug2("we did not send a packet, disable method"); 487 method->enabled = NULL; 488 } 489 } 490 } 491 492 /* ARGSUSED */ 493 int 494 input_userauth_error(int type, u_int32_t seq, void *ctxt) 495 { 496 fatal("input_userauth_error: bad message during authentication: " 497 "type %d", type); 498 return 0; 499 } 500 501 /* ARGSUSED */ 502 int 503 input_userauth_banner(int type, u_int32_t seq, void *ctxt) 504 { 505 char *msg, *raw, *lang; 506 u_int len; 507 508 debug3("input_userauth_banner"); 509 raw = packet_get_string(&len); 510 lang = packet_get_string(NULL); 511 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { 512 if (len > 65536) 513 len = 65536; 514 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ 515 strvisx(msg, raw, len, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH); 516 fprintf(stderr, "%s", msg); 517 free(msg); 518 } 519 free(raw); 520 free(lang); 521 return 0; 522 } 523 524 /* ARGSUSED */ 525 int 526 input_userauth_success(int type, u_int32_t seq, void *ctxt) 527 { 528 Authctxt *authctxt = ctxt; 529 530 if (authctxt == NULL) 531 fatal("input_userauth_success: no authentication context"); 532 free(authctxt->authlist); 533 authctxt->authlist = NULL; 534 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 535 authctxt->method->cleanup(authctxt); 536 free(authctxt->methoddata); 537 authctxt->methoddata = NULL; 538 authctxt->success = 1; /* break out */ 539 return 0; 540 } 541 542 int 543 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) 544 { 545 Authctxt *authctxt = ctxt; 546 547 if (authctxt == NULL) 548 fatal("%s: no authentication context", __func__); 549 550 fatal("Unexpected authentication success during %s.", 551 authctxt->method->name); 552 return 0; 553 } 554 555 /* ARGSUSED */ 556 int 557 input_userauth_failure(int type, u_int32_t seq, void *ctxt) 558 { 559 Authctxt *authctxt = ctxt; 560 char *authlist = NULL; 561 int partial; 562 563 if (authctxt == NULL) 564 fatal("input_userauth_failure: no authentication context"); 565 566 authlist = packet_get_string(NULL); 567 partial = packet_get_char(); 568 packet_check_eom(); 569 570 if (partial != 0) { 571 logit("Authenticated with partial success."); 572 /* reset state */ 573 pubkey_cleanup(authctxt); 574 pubkey_prepare(authctxt); 575 } 576 debug("Authentications that can continue: %s", authlist); 577 578 userauth(authctxt, authlist); 579 return 0; 580 } 581 582 /* ARGSUSED */ 583 int 584 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) 585 { 586 Authctxt *authctxt = ctxt; 587 Key *key = NULL; 588 Identity *id = NULL; 589 Buffer b; 590 int pktype, sent = 0; 591 u_int alen, blen; 592 char *pkalg, *fp; 593 u_char *pkblob; 594 595 if (authctxt == NULL) 596 fatal("input_userauth_pk_ok: no authentication context"); 597 if (datafellows & SSH_BUG_PKOK) { 598 /* this is similar to SSH_BUG_PKAUTH */ 599 debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); 600 pkblob = packet_get_string(&blen); 601 buffer_init(&b); 602 buffer_append(&b, pkblob, blen); 603 pkalg = buffer_get_string(&b, &alen); 604 buffer_free(&b); 605 } else { 606 pkalg = packet_get_string(&alen); 607 pkblob = packet_get_string(&blen); 608 } 609 packet_check_eom(); 610 611 debug("Server accepts key: pkalg %s blen %u", pkalg, blen); 612 613 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { 614 debug("unknown pkalg %s", pkalg); 615 goto done; 616 } 617 if ((key = key_from_blob(pkblob, blen)) == NULL) { 618 debug("no key from blob. pkalg %s", pkalg); 619 goto done; 620 } 621 if (key->type != pktype) { 622 error("input_userauth_pk_ok: type mismatch " 623 "for decoded key (received %d, expected %d)", 624 key->type, pktype); 625 goto done; 626 } 627 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, 628 SSH_FP_DEFAULT)) == NULL) 629 goto done; 630 debug2("input_userauth_pk_ok: fp %s", fp); 631 free(fp); 632 633 /* 634 * search keys in the reverse order, because last candidate has been 635 * moved to the end of the queue. this also avoids confusion by 636 * duplicate keys 637 */ 638 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { 639 if (key_equal(key, id->key)) { 640 sent = sign_and_send_pubkey(authctxt, id); 641 break; 642 } 643 } 644 done: 645 if (key != NULL) 646 key_free(key); 647 free(pkalg); 648 free(pkblob); 649 650 /* try another method if we did not send a packet */ 651 if (sent == 0) 652 userauth(authctxt, NULL); 653 return 0; 654 } 655 656 #ifdef GSSAPI 657 int 658 userauth_gssapi(Authctxt *authctxt) 659 { 660 Gssctxt *gssctxt = NULL; 661 static gss_OID_set gss_supported = NULL; 662 static u_int mech = 0; 663 OM_uint32 min; 664 int ok = 0; 665 666 /* Try one GSSAPI method at a time, rather than sending them all at 667 * once. */ 668 669 if (gss_supported == NULL) 670 gss_indicate_mechs(&min, &gss_supported); 671 672 /* Check to see if the mechanism is usable before we offer it */ 673 while (mech < gss_supported->count && !ok) { 674 /* My DER encoding requires length<128 */ 675 if (gss_supported->elements[mech].length < 128 && 676 ssh_gssapi_check_mechanism(&gssctxt, 677 &gss_supported->elements[mech], authctxt->host)) { 678 ok = 1; /* Mechanism works */ 679 } else { 680 mech++; 681 } 682 } 683 684 if (!ok) { 685 ssh_gssapi_delete_ctx(&gssctxt); 686 return 0; 687 } 688 689 authctxt->methoddata=(void *)gssctxt; 690 691 packet_start(SSH2_MSG_USERAUTH_REQUEST); 692 packet_put_cstring(authctxt->server_user); 693 packet_put_cstring(authctxt->service); 694 packet_put_cstring(authctxt->method->name); 695 696 packet_put_int(1); 697 698 packet_put_int((gss_supported->elements[mech].length) + 2); 699 packet_put_char(SSH_GSS_OIDTYPE); 700 packet_put_char(gss_supported->elements[mech].length); 701 packet_put_raw(gss_supported->elements[mech].elements, 702 gss_supported->elements[mech].length); 703 704 packet_send(); 705 706 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); 707 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); 708 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); 709 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); 710 711 mech++; /* Move along to next candidate */ 712 713 return 1; 714 } 715 716 static OM_uint32 717 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) 718 { 719 Authctxt *authctxt = ctxt; 720 Gssctxt *gssctxt = authctxt->methoddata; 721 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 722 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; 723 gss_buffer_desc gssbuf; 724 OM_uint32 status, ms, flags; 725 Buffer b; 726 727 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 728 recv_tok, &send_tok, &flags); 729 730 if (send_tok.length > 0) { 731 if (GSS_ERROR(status)) 732 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); 733 else 734 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); 735 736 packet_put_string(send_tok.value, send_tok.length); 737 packet_send(); 738 gss_release_buffer(&ms, &send_tok); 739 } 740 741 if (status == GSS_S_COMPLETE) { 742 /* send either complete or MIC, depending on mechanism */ 743 if (!(flags & GSS_C_INTEG_FLAG)) { 744 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); 745 packet_send(); 746 } else { 747 ssh_gssapi_buildmic(&b, authctxt->server_user, 748 authctxt->service, "gssapi-with-mic"); 749 750 gssbuf.value = buffer_ptr(&b); 751 gssbuf.length = buffer_len(&b); 752 753 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); 754 755 if (!GSS_ERROR(status)) { 756 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); 757 packet_put_string(mic.value, mic.length); 758 759 packet_send(); 760 } 761 762 buffer_free(&b); 763 gss_release_buffer(&ms, &mic); 764 } 765 } 766 767 return status; 768 } 769 770 /* ARGSUSED */ 771 int 772 input_gssapi_response(int type, u_int32_t plen, void *ctxt) 773 { 774 Authctxt *authctxt = ctxt; 775 Gssctxt *gssctxt; 776 int oidlen; 777 char *oidv; 778 779 if (authctxt == NULL) 780 fatal("input_gssapi_response: no authentication context"); 781 gssctxt = authctxt->methoddata; 782 783 /* Setup our OID */ 784 oidv = packet_get_string(&oidlen); 785 786 if (oidlen <= 2 || 787 oidv[0] != SSH_GSS_OIDTYPE || 788 oidv[1] != oidlen - 2) { 789 free(oidv); 790 debug("Badly encoded mechanism OID received"); 791 userauth(authctxt, NULL); 792 return 0; 793 } 794 795 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) 796 fatal("Server returned different OID than expected"); 797 798 packet_check_eom(); 799 800 free(oidv); 801 802 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { 803 /* Start again with next method on list */ 804 debug("Trying to start again"); 805 userauth(authctxt, NULL); 806 return 0; 807 } 808 return 0; 809 } 810 811 /* ARGSUSED */ 812 int 813 input_gssapi_token(int type, u_int32_t plen, void *ctxt) 814 { 815 Authctxt *authctxt = ctxt; 816 gss_buffer_desc recv_tok; 817 OM_uint32 status; 818 u_int slen; 819 820 if (authctxt == NULL) 821 fatal("input_gssapi_response: no authentication context"); 822 823 recv_tok.value = packet_get_string(&slen); 824 recv_tok.length = slen; /* safe typecast */ 825 826 packet_check_eom(); 827 828 status = process_gssapi_token(ctxt, &recv_tok); 829 830 free(recv_tok.value); 831 832 if (GSS_ERROR(status)) { 833 /* Start again with the next method in the list */ 834 userauth(authctxt, NULL); 835 return 0; 836 } 837 return 0; 838 } 839 840 /* ARGSUSED */ 841 int 842 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) 843 { 844 Authctxt *authctxt = ctxt; 845 Gssctxt *gssctxt; 846 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 847 gss_buffer_desc recv_tok; 848 OM_uint32 ms; 849 u_int len; 850 851 if (authctxt == NULL) 852 fatal("input_gssapi_response: no authentication context"); 853 gssctxt = authctxt->methoddata; 854 855 recv_tok.value = packet_get_string(&len); 856 recv_tok.length = len; 857 858 packet_check_eom(); 859 860 /* Stick it into GSSAPI and see what it says */ 861 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 862 &recv_tok, &send_tok, NULL); 863 864 free(recv_tok.value); 865 gss_release_buffer(&ms, &send_tok); 866 867 /* Server will be returning a failed packet after this one */ 868 return 0; 869 } 870 871 /* ARGSUSED */ 872 int 873 input_gssapi_error(int type, u_int32_t plen, void *ctxt) 874 { 875 char *msg; 876 char *lang; 877 878 /* maj */(void)packet_get_int(); 879 /* min */(void)packet_get_int(); 880 msg=packet_get_string(NULL); 881 lang=packet_get_string(NULL); 882 883 packet_check_eom(); 884 885 debug("Server GSSAPI Error:\n%s", msg); 886 free(msg); 887 free(lang); 888 return 0; 889 } 890 #endif /* GSSAPI */ 891 892 int 893 userauth_none(Authctxt *authctxt) 894 { 895 /* initial userauth request */ 896 packet_start(SSH2_MSG_USERAUTH_REQUEST); 897 packet_put_cstring(authctxt->server_user); 898 packet_put_cstring(authctxt->service); 899 packet_put_cstring(authctxt->method->name); 900 packet_send(); 901 return 1; 902 } 903 904 int 905 userauth_passwd(Authctxt *authctxt) 906 { 907 static int attempt = 0; 908 char prompt[150]; 909 char *password; 910 const char *host = options.host_key_alias ? options.host_key_alias : 911 authctxt->host; 912 913 if (attempt++ >= options.number_of_password_prompts) 914 return 0; 915 916 if (attempt != 1) 917 error("Permission denied, please try again."); 918 919 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", 920 authctxt->server_user, host); 921 password = read_passphrase(prompt, 0); 922 packet_start(SSH2_MSG_USERAUTH_REQUEST); 923 packet_put_cstring(authctxt->server_user); 924 packet_put_cstring(authctxt->service); 925 packet_put_cstring(authctxt->method->name); 926 packet_put_char(0); 927 packet_put_cstring(password); 928 explicit_bzero(password, strlen(password)); 929 free(password); 930 packet_add_padding(64); 931 packet_send(); 932 933 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 934 &input_userauth_passwd_changereq); 935 936 return 1; 937 } 938 939 /* 940 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST 941 */ 942 /* ARGSUSED */ 943 int 944 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) 945 { 946 Authctxt *authctxt = ctxt; 947 char *info, *lang, *password = NULL, *retype = NULL; 948 char prompt[150]; 949 const char *host = options.host_key_alias ? options.host_key_alias : 950 authctxt->host; 951 952 debug2("input_userauth_passwd_changereq"); 953 954 if (authctxt == NULL) 955 fatal("input_userauth_passwd_changereq: " 956 "no authentication context"); 957 958 info = packet_get_string(NULL); 959 lang = packet_get_string(NULL); 960 if (strlen(info) > 0) 961 logit("%s", info); 962 free(info); 963 free(lang); 964 packet_start(SSH2_MSG_USERAUTH_REQUEST); 965 packet_put_cstring(authctxt->server_user); 966 packet_put_cstring(authctxt->service); 967 packet_put_cstring(authctxt->method->name); 968 packet_put_char(1); /* additional info */ 969 snprintf(prompt, sizeof(prompt), 970 "Enter %.30s@%.128s's old password: ", 971 authctxt->server_user, host); 972 password = read_passphrase(prompt, 0); 973 packet_put_cstring(password); 974 explicit_bzero(password, strlen(password)); 975 free(password); 976 password = NULL; 977 while (password == NULL) { 978 snprintf(prompt, sizeof(prompt), 979 "Enter %.30s@%.128s's new password: ", 980 authctxt->server_user, host); 981 password = read_passphrase(prompt, RP_ALLOW_EOF); 982 if (password == NULL) { 983 /* bail out */ 984 return 0; 985 } 986 snprintf(prompt, sizeof(prompt), 987 "Retype %.30s@%.128s's new password: ", 988 authctxt->server_user, host); 989 retype = read_passphrase(prompt, 0); 990 if (strcmp(password, retype) != 0) { 991 explicit_bzero(password, strlen(password)); 992 free(password); 993 logit("Mismatch; try again, EOF to quit."); 994 password = NULL; 995 } 996 explicit_bzero(retype, strlen(retype)); 997 free(retype); 998 } 999 packet_put_cstring(password); 1000 explicit_bzero(password, strlen(password)); 1001 free(password); 1002 packet_add_padding(64); 1003 packet_send(); 1004 1005 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 1006 &input_userauth_passwd_changereq); 1007 return 0; 1008 } 1009 1010 static int 1011 identity_sign(struct identity *id, u_char **sigp, size_t *lenp, 1012 const u_char *data, size_t datalen, u_int compat) 1013 { 1014 Key *prv; 1015 int ret; 1016 1017 /* the agent supports this key */ 1018 if (id->agent_fd) 1019 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, 1020 data, datalen, compat); 1021 1022 /* 1023 * we have already loaded the private key or 1024 * the private key is stored in external hardware 1025 */ 1026 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT)) 1027 return (sshkey_sign(id->key, sigp, lenp, data, datalen, 1028 compat)); 1029 /* load the private key from the file */ 1030 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL) 1031 return (-1); /* XXX return decent error code */ 1032 ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat); 1033 sshkey_free(prv); 1034 return (ret); 1035 } 1036 1037 static int 1038 sign_and_send_pubkey(Authctxt *authctxt, Identity *id) 1039 { 1040 Buffer b; 1041 u_char *blob, *signature; 1042 u_int bloblen; 1043 size_t slen; 1044 u_int skip = 0; 1045 int ret = -1; 1046 int have_sig = 1; 1047 char *fp; 1048 1049 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash, 1050 SSH_FP_DEFAULT)) == NULL) 1051 return 0; 1052 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp); 1053 free(fp); 1054 1055 if (key_to_blob(id->key, &blob, &bloblen) == 0) { 1056 /* we cannot handle this key */ 1057 debug3("sign_and_send_pubkey: cannot handle key"); 1058 return 0; 1059 } 1060 /* data to be signed */ 1061 buffer_init(&b); 1062 if (datafellows & SSH_OLD_SESSIONID) { 1063 buffer_append(&b, session_id2, session_id2_len); 1064 skip = session_id2_len; 1065 } else { 1066 buffer_put_string(&b, session_id2, session_id2_len); 1067 skip = buffer_len(&b); 1068 } 1069 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 1070 buffer_put_cstring(&b, authctxt->server_user); 1071 buffer_put_cstring(&b, 1072 datafellows & SSH_BUG_PKSERVICE ? 1073 "ssh-userauth" : 1074 authctxt->service); 1075 if (datafellows & SSH_BUG_PKAUTH) { 1076 buffer_put_char(&b, have_sig); 1077 } else { 1078 buffer_put_cstring(&b, authctxt->method->name); 1079 buffer_put_char(&b, have_sig); 1080 buffer_put_cstring(&b, key_ssh_name(id->key)); 1081 } 1082 buffer_put_string(&b, blob, bloblen); 1083 1084 /* generate signature */ 1085 ret = identity_sign(id, &signature, &slen, 1086 buffer_ptr(&b), buffer_len(&b), datafellows); 1087 if (ret != 0) { 1088 free(blob); 1089 buffer_free(&b); 1090 return 0; 1091 } 1092 #ifdef DEBUG_PK 1093 buffer_dump(&b); 1094 #endif 1095 if (datafellows & SSH_BUG_PKSERVICE) { 1096 buffer_clear(&b); 1097 buffer_append(&b, session_id2, session_id2_len); 1098 skip = session_id2_len; 1099 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 1100 buffer_put_cstring(&b, authctxt->server_user); 1101 buffer_put_cstring(&b, authctxt->service); 1102 buffer_put_cstring(&b, authctxt->method->name); 1103 buffer_put_char(&b, have_sig); 1104 if (!(datafellows & SSH_BUG_PKAUTH)) 1105 buffer_put_cstring(&b, key_ssh_name(id->key)); 1106 buffer_put_string(&b, blob, bloblen); 1107 } 1108 free(blob); 1109 1110 /* append signature */ 1111 buffer_put_string(&b, signature, slen); 1112 free(signature); 1113 1114 /* skip session id and packet type */ 1115 if (buffer_len(&b) < skip + 1) 1116 fatal("userauth_pubkey: internal error"); 1117 buffer_consume(&b, skip + 1); 1118 1119 /* put remaining data from buffer into packet */ 1120 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1121 packet_put_raw(buffer_ptr(&b), buffer_len(&b)); 1122 buffer_free(&b); 1123 packet_send(); 1124 1125 return 1; 1126 } 1127 1128 static int 1129 send_pubkey_test(Authctxt *authctxt, Identity *id) 1130 { 1131 u_char *blob; 1132 u_int bloblen, have_sig = 0; 1133 1134 debug3("send_pubkey_test"); 1135 1136 if (key_to_blob(id->key, &blob, &bloblen) == 0) { 1137 /* we cannot handle this key */ 1138 debug3("send_pubkey_test: cannot handle key"); 1139 return 0; 1140 } 1141 /* register callback for USERAUTH_PK_OK message */ 1142 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); 1143 1144 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1145 packet_put_cstring(authctxt->server_user); 1146 packet_put_cstring(authctxt->service); 1147 packet_put_cstring(authctxt->method->name); 1148 packet_put_char(have_sig); 1149 if (!(datafellows & SSH_BUG_PKAUTH)) 1150 packet_put_cstring(key_ssh_name(id->key)); 1151 packet_put_string(blob, bloblen); 1152 free(blob); 1153 packet_send(); 1154 return 1; 1155 } 1156 1157 static Key * 1158 load_identity_file(char *filename, int userprovided) 1159 { 1160 Key *private; 1161 char prompt[300], *passphrase; 1162 int r, perm_ok = 0, quit = 0, i; 1163 struct stat st; 1164 1165 if (stat(filename, &st) < 0) { 1166 (userprovided ? logit : debug3)("no such identity: %s: %s", 1167 filename, strerror(errno)); 1168 return NULL; 1169 } 1170 snprintf(prompt, sizeof prompt, 1171 "Enter passphrase for key '%.100s': ", filename); 1172 for (i = 0; i <= options.number_of_password_prompts; i++) { 1173 if (i == 0) 1174 passphrase = xstrdup(""); 1175 else { 1176 passphrase = read_passphrase(prompt, 0); 1177 if (*passphrase == '\0') { 1178 debug2("no passphrase given, try next key"); 1179 free(passphrase); 1180 break; 1181 } 1182 } 1183 switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename, 1184 passphrase, &private, NULL, &perm_ok))) { 1185 case 0: 1186 break; 1187 case SSH_ERR_KEY_WRONG_PASSPHRASE: 1188 if (options.batch_mode) { 1189 quit = 1; 1190 break; 1191 } 1192 if (i != 0) 1193 debug2("bad passphrase given, try again..."); 1194 break; 1195 case SSH_ERR_SYSTEM_ERROR: 1196 if (errno == ENOENT) { 1197 debug2("Load key \"%s\": %s", 1198 filename, ssh_err(r)); 1199 quit = 1; 1200 break; 1201 } 1202 /* FALLTHROUGH */ 1203 default: 1204 error("Load key \"%s\": %s", filename, ssh_err(r)); 1205 quit = 1; 1206 break; 1207 } 1208 if (i > 0) { 1209 explicit_bzero(passphrase, strlen(passphrase)); 1210 free(passphrase); 1211 } 1212 if (private != NULL || quit) 1213 break; 1214 } 1215 return private; 1216 } 1217 1218 /* 1219 * try keys in the following order: 1220 * 1. agent keys that are found in the config file 1221 * 2. other agent keys 1222 * 3. keys that are only listed in the config file 1223 */ 1224 static void 1225 pubkey_prepare(Authctxt *authctxt) 1226 { 1227 struct identity *id, *id2, *tmp; 1228 struct idlist agent, files, *preferred; 1229 struct sshkey *key; 1230 int agent_fd, i, r, found; 1231 size_t j; 1232 struct ssh_identitylist *idlist; 1233 1234 TAILQ_INIT(&agent); /* keys from the agent */ 1235 TAILQ_INIT(&files); /* keys from the config file */ 1236 preferred = &authctxt->keys; 1237 TAILQ_INIT(preferred); /* preferred order of keys */ 1238 1239 /* list of keys stored in the filesystem and PKCS#11 */ 1240 for (i = 0; i < options.num_identity_files; i++) { 1241 key = options.identity_keys[i]; 1242 if (key && key->type == KEY_RSA1) 1243 continue; 1244 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER) 1245 continue; 1246 options.identity_keys[i] = NULL; 1247 id = xcalloc(1, sizeof(*id)); 1248 id->key = key; 1249 id->filename = xstrdup(options.identity_files[i]); 1250 id->userprovided = options.identity_file_userprovided[i]; 1251 TAILQ_INSERT_TAIL(&files, id, next); 1252 } 1253 /* Prefer PKCS11 keys that are explicitly listed */ 1254 TAILQ_FOREACH_SAFE(id, &files, next, tmp) { 1255 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0) 1256 continue; 1257 found = 0; 1258 TAILQ_FOREACH(id2, &files, next) { 1259 if (id2->key == NULL || 1260 (id2->key->flags & SSHKEY_FLAG_EXT) == 0) 1261 continue; 1262 if (sshkey_equal(id->key, id2->key)) { 1263 TAILQ_REMOVE(&files, id, next); 1264 TAILQ_INSERT_TAIL(preferred, id, next); 1265 found = 1; 1266 break; 1267 } 1268 } 1269 /* If IdentitiesOnly set and key not found then don't use it */ 1270 if (!found && options.identities_only) { 1271 TAILQ_REMOVE(&files, id, next); 1272 explicit_bzero(id, sizeof(*id)); 1273 free(id); 1274 } 1275 } 1276 /* list of keys supported by the agent */ 1277 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { 1278 if (r != SSH_ERR_AGENT_NOT_PRESENT) 1279 debug("%s: ssh_get_authentication_socket: %s", 1280 __func__, ssh_err(r)); 1281 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) { 1282 if (r != SSH_ERR_AGENT_NO_IDENTITIES) 1283 debug("%s: ssh_fetch_identitylist: %s", 1284 __func__, ssh_err(r)); 1285 } else { 1286 for (j = 0; j < idlist->nkeys; j++) { 1287 found = 0; 1288 TAILQ_FOREACH(id, &files, next) { 1289 /* 1290 * agent keys from the config file are 1291 * preferred 1292 */ 1293 if (sshkey_equal(idlist->keys[j], id->key)) { 1294 TAILQ_REMOVE(&files, id, next); 1295 TAILQ_INSERT_TAIL(preferred, id, next); 1296 id->agent_fd = agent_fd; 1297 found = 1; 1298 break; 1299 } 1300 } 1301 if (!found && !options.identities_only) { 1302 id = xcalloc(1, sizeof(*id)); 1303 /* XXX "steals" key/comment from idlist */ 1304 id->key = idlist->keys[j]; 1305 id->filename = idlist->comments[j]; 1306 idlist->keys[j] = NULL; 1307 idlist->comments[j] = NULL; 1308 id->agent_fd = agent_fd; 1309 TAILQ_INSERT_TAIL(&agent, id, next); 1310 } 1311 } 1312 ssh_free_identitylist(idlist); 1313 /* append remaining agent keys */ 1314 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { 1315 TAILQ_REMOVE(&agent, id, next); 1316 TAILQ_INSERT_TAIL(preferred, id, next); 1317 } 1318 authctxt->agent_fd = agent_fd; 1319 } 1320 /* append remaining keys from the config file */ 1321 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { 1322 TAILQ_REMOVE(&files, id, next); 1323 TAILQ_INSERT_TAIL(preferred, id, next); 1324 } 1325 TAILQ_FOREACH(id, preferred, next) { 1326 debug2("key: %s (%p),%s", id->filename, id->key, 1327 id->userprovided ? " explicit" : ""); 1328 } 1329 } 1330 1331 static void 1332 pubkey_cleanup(Authctxt *authctxt) 1333 { 1334 Identity *id; 1335 1336 if (authctxt->agent_fd != -1) 1337 ssh_close_authentication_socket(authctxt->agent_fd); 1338 for (id = TAILQ_FIRST(&authctxt->keys); id; 1339 id = TAILQ_FIRST(&authctxt->keys)) { 1340 TAILQ_REMOVE(&authctxt->keys, id, next); 1341 if (id->key) 1342 sshkey_free(id->key); 1343 free(id->filename); 1344 free(id); 1345 } 1346 } 1347 1348 static int 1349 try_identity(Identity *id) 1350 { 1351 if (!id->key) 1352 return (0); 1353 if (match_pattern_list(sshkey_ssh_name(id->key), 1354 options.pubkey_key_types, 0) != 1) { 1355 debug("Skipping %s key %s for not in PubkeyAcceptedKeyTypes", 1356 sshkey_ssh_name(id->key), id->filename); 1357 return (0); 1358 } 1359 if (key_type_plain(id->key->type) == KEY_RSA && 1360 (datafellows & SSH_BUG_RSASIGMD5) != 0) { 1361 debug("Skipped %s key %s for RSA/MD5 server", 1362 key_type(id->key), id->filename); 1363 return (0); 1364 } 1365 return (id->key->type != KEY_RSA1); 1366 } 1367 1368 int 1369 userauth_pubkey(Authctxt *authctxt) 1370 { 1371 Identity *id; 1372 int sent = 0; 1373 1374 while ((id = TAILQ_FIRST(&authctxt->keys))) { 1375 if (id->tried++) 1376 return (0); 1377 /* move key to the end of the queue */ 1378 TAILQ_REMOVE(&authctxt->keys, id, next); 1379 TAILQ_INSERT_TAIL(&authctxt->keys, id, next); 1380 /* 1381 * send a test message if we have the public key. for 1382 * encrypted keys we cannot do this and have to load the 1383 * private key instead 1384 */ 1385 if (id->key != NULL) { 1386 if (try_identity(id)) { 1387 debug("Offering %s public key: %s", 1388 key_type(id->key), id->filename); 1389 sent = send_pubkey_test(authctxt, id); 1390 } 1391 } else { 1392 debug("Trying private key: %s", id->filename); 1393 id->key = load_identity_file(id->filename, 1394 id->userprovided); 1395 if (id->key != NULL) { 1396 if (try_identity(id)) { 1397 id->isprivate = 1; 1398 sent = sign_and_send_pubkey( 1399 authctxt, id); 1400 } 1401 key_free(id->key); 1402 id->key = NULL; 1403 } 1404 } 1405 if (sent) 1406 return (sent); 1407 } 1408 return (0); 1409 } 1410 1411 /* 1412 * Send userauth request message specifying keyboard-interactive method. 1413 */ 1414 int 1415 userauth_kbdint(Authctxt *authctxt) 1416 { 1417 static int attempt = 0; 1418 1419 if (attempt++ >= options.number_of_password_prompts) 1420 return 0; 1421 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ 1422 if (attempt > 1 && !authctxt->info_req_seen) { 1423 debug3("userauth_kbdint: disable: no info_req_seen"); 1424 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); 1425 return 0; 1426 } 1427 1428 debug2("userauth_kbdint"); 1429 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1430 packet_put_cstring(authctxt->server_user); 1431 packet_put_cstring(authctxt->service); 1432 packet_put_cstring(authctxt->method->name); 1433 packet_put_cstring(""); /* lang */ 1434 packet_put_cstring(options.kbd_interactive_devices ? 1435 options.kbd_interactive_devices : ""); 1436 packet_send(); 1437 1438 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); 1439 return 1; 1440 } 1441 1442 /* 1443 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE 1444 */ 1445 int 1446 input_userauth_info_req(int type, u_int32_t seq, void *ctxt) 1447 { 1448 Authctxt *authctxt = ctxt; 1449 char *name, *inst, *lang, *prompt, *response; 1450 u_int num_prompts, i; 1451 int echo = 0; 1452 1453 debug2("input_userauth_info_req"); 1454 1455 if (authctxt == NULL) 1456 fatal("input_userauth_info_req: no authentication context"); 1457 1458 authctxt->info_req_seen = 1; 1459 1460 name = packet_get_string(NULL); 1461 inst = packet_get_string(NULL); 1462 lang = packet_get_string(NULL); 1463 if (strlen(name) > 0) 1464 logit("%s", name); 1465 if (strlen(inst) > 0) 1466 logit("%s", inst); 1467 free(name); 1468 free(inst); 1469 free(lang); 1470 1471 num_prompts = packet_get_int(); 1472 /* 1473 * Begin to build info response packet based on prompts requested. 1474 * We commit to providing the correct number of responses, so if 1475 * further on we run into a problem that prevents this, we have to 1476 * be sure and clean this up and send a correct error response. 1477 */ 1478 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); 1479 packet_put_int(num_prompts); 1480 1481 debug2("input_userauth_info_req: num_prompts %d", num_prompts); 1482 for (i = 0; i < num_prompts; i++) { 1483 prompt = packet_get_string(NULL); 1484 echo = packet_get_char(); 1485 1486 response = read_passphrase(prompt, echo ? RP_ECHO : 0); 1487 1488 packet_put_cstring(response); 1489 explicit_bzero(response, strlen(response)); 1490 free(response); 1491 free(prompt); 1492 } 1493 packet_check_eom(); /* done with parsing incoming message. */ 1494 1495 packet_add_padding(64); 1496 packet_send(); 1497 return 0; 1498 } 1499 1500 static int 1501 ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp, 1502 const u_char *data, size_t datalen) 1503 { 1504 struct sshbuf *b; 1505 struct stat st; 1506 pid_t pid; 1507 int i, r, to[2], from[2], status, sock = packet_get_connection_in(); 1508 u_char rversion = 0, version = 2; 1509 void (*osigchld)(int); 1510 1511 *sigp = NULL; 1512 *lenp = 0; 1513 1514 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { 1515 error("%s: not installed: %s", __func__, strerror(errno)); 1516 return -1; 1517 } 1518 if (fflush(stdout) != 0) { 1519 error("%s: fflush: %s", __func__, strerror(errno)); 1520 return -1; 1521 } 1522 if (pipe(to) < 0) { 1523 error("%s: pipe: %s", __func__, strerror(errno)); 1524 return -1; 1525 } 1526 if (pipe(from) < 0) { 1527 error("%s: pipe: %s", __func__, strerror(errno)); 1528 return -1; 1529 } 1530 if ((pid = fork()) < 0) { 1531 error("%s: fork: %s", __func__, strerror(errno)); 1532 return -1; 1533 } 1534 osigchld = signal(SIGCHLD, SIG_DFL); 1535 if (pid == 0) { 1536 /* keep the socket on exec */ 1537 fcntl(sock, F_SETFD, 0); 1538 permanently_drop_suid(getuid()); 1539 close(from[0]); 1540 if (dup2(from[1], STDOUT_FILENO) < 0) 1541 fatal("%s: dup2: %s", __func__, strerror(errno)); 1542 close(to[1]); 1543 if (dup2(to[0], STDIN_FILENO) < 0) 1544 fatal("%s: dup2: %s", __func__, strerror(errno)); 1545 close(from[1]); 1546 close(to[0]); 1547 /* Close everything but stdio and the socket */ 1548 for (i = STDERR_FILENO + 1; i < sock; i++) 1549 close(i); 1550 if (closefrom(sock + 1) < 0) 1551 fatal("%s: closefrom: %s", __func__, strerror(errno)); 1552 debug3("%s: [child] pid=%ld, exec %s", 1553 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN); 1554 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); 1555 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN, 1556 strerror(errno)); 1557 } 1558 close(from[1]); 1559 close(to[0]); 1560 1561 if ((b = sshbuf_new()) == NULL) 1562 fatal("%s: sshbuf_new failed", __func__); 1563 /* send # of sock, data to be signed */ 1564 if ((r = sshbuf_put_u32(b, sock) != 0) || 1565 (r = sshbuf_put_string(b, data, datalen)) != 0) 1566 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1567 if (ssh_msg_send(to[1], version, b) == -1) 1568 fatal("%s: couldn't send request", __func__); 1569 sshbuf_reset(b); 1570 r = ssh_msg_recv(from[0], b); 1571 close(from[0]); 1572 close(to[1]); 1573 if (r < 0) { 1574 error("%s: no reply", __func__); 1575 goto fail; 1576 } 1577 1578 errno = 0; 1579 while (waitpid(pid, &status, 0) < 0) { 1580 if (errno != EINTR) { 1581 error("%s: waitpid %ld: %s", 1582 __func__, (long)pid, strerror(errno)); 1583 goto fail; 1584 } 1585 } 1586 if (!WIFEXITED(status)) { 1587 error("%s: exited abnormally", __func__); 1588 goto fail; 1589 } 1590 if (WEXITSTATUS(status) != 0) { 1591 error("%s: exited with status %d", 1592 __func__, WEXITSTATUS(status)); 1593 goto fail; 1594 } 1595 if ((r = sshbuf_get_u8(b, &rversion)) != 0) { 1596 error("%s: buffer error: %s", __func__, ssh_err(r)); 1597 goto fail; 1598 } 1599 if (rversion != version) { 1600 error("%s: bad version", __func__); 1601 goto fail; 1602 } 1603 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) { 1604 error("%s: buffer error: %s", __func__, ssh_err(r)); 1605 fail: 1606 signal(SIGCHLD, osigchld); 1607 sshbuf_free(b); 1608 return -1; 1609 } 1610 signal(SIGCHLD, osigchld); 1611 sshbuf_free(b); 1612 1613 return 0; 1614 } 1615 1616 int 1617 userauth_hostbased(Authctxt *authctxt) 1618 { 1619 struct ssh *ssh = active_state; 1620 struct sshkey *private = NULL; 1621 struct sshbuf *b = NULL; 1622 const char *service; 1623 u_char *sig = NULL, *keyblob = NULL; 1624 char *fp = NULL, *chost = NULL, *lname = NULL; 1625 size_t siglen = 0, keylen = 0; 1626 int i, r, success = 0; 1627 1628 if (authctxt->ktypes == NULL) { 1629 authctxt->oktypes = xstrdup(options.hostbased_key_types); 1630 authctxt->ktypes = authctxt->oktypes; 1631 } 1632 1633 /* 1634 * Work through each listed type pattern in HostbasedKeyTypes, 1635 * trying each hostkey that matches the type in turn. 1636 */ 1637 for (;;) { 1638 if (authctxt->active_ktype == NULL) 1639 authctxt->active_ktype = strsep(&authctxt->ktypes, ","); 1640 if (authctxt->active_ktype == NULL || 1641 *authctxt->active_ktype == '\0') 1642 break; 1643 debug3("%s: trying key type %s", __func__, 1644 authctxt->active_ktype); 1645 1646 /* check for a useful key */ 1647 private = NULL; 1648 for (i = 0; i < authctxt->sensitive->nkeys; i++) { 1649 if (authctxt->sensitive->keys[i] == NULL || 1650 authctxt->sensitive->keys[i]->type == KEY_RSA1 || 1651 authctxt->sensitive->keys[i]->type == KEY_UNSPEC) 1652 continue; 1653 if (match_pattern_list( 1654 sshkey_ssh_name(authctxt->sensitive->keys[i]), 1655 authctxt->active_ktype, 0) != 1) 1656 continue; 1657 /* we take and free the key */ 1658 private = authctxt->sensitive->keys[i]; 1659 authctxt->sensitive->keys[i] = NULL; 1660 break; 1661 } 1662 /* Found one */ 1663 if (private != NULL) 1664 break; 1665 /* No more keys of this type; advance */ 1666 authctxt->active_ktype = NULL; 1667 } 1668 if (private == NULL) { 1669 free(authctxt->oktypes); 1670 authctxt->oktypes = authctxt->ktypes = NULL; 1671 authctxt->active_ktype = NULL; 1672 debug("No more client hostkeys for hostbased authentication."); 1673 goto out; 1674 } 1675 1676 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash, 1677 SSH_FP_DEFAULT)) == NULL) { 1678 error("%s: sshkey_fingerprint failed", __func__); 1679 goto out; 1680 } 1681 debug("%s: trying hostkey %s %s", 1682 __func__, sshkey_ssh_name(private), fp); 1683 1684 /* figure out a name for the client host */ 1685 if ((lname = get_local_name(packet_get_connection_in())) == NULL) { 1686 error("%s: cannot get local ipaddr/name", __func__); 1687 goto out; 1688 } 1689 1690 /* XXX sshbuf_put_stringf? */ 1691 xasprintf(&chost, "%s.", lname); 1692 debug2("%s: chost %s", __func__, chost); 1693 1694 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : 1695 authctxt->service; 1696 1697 /* construct data */ 1698 if ((b = sshbuf_new()) == NULL) { 1699 error("%s: sshbuf_new failed", __func__); 1700 goto out; 1701 } 1702 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) { 1703 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); 1704 goto out; 1705 } 1706 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || 1707 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1708 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || 1709 (r = sshbuf_put_cstring(b, service)) != 0 || 1710 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || 1711 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 || 1712 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 || 1713 (r = sshbuf_put_cstring(b, chost)) != 0 || 1714 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) { 1715 error("%s: buffer error: %s", __func__, ssh_err(r)); 1716 goto out; 1717 } 1718 1719 #ifdef DEBUG_PK 1720 sshbuf_dump(b, stderr); 1721 #endif 1722 if (authctxt->sensitive->external_keysign) 1723 r = ssh_keysign(private, &sig, &siglen, 1724 sshbuf_ptr(b), sshbuf_len(b)); 1725 else if ((r = sshkey_sign(private, &sig, &siglen, 1726 sshbuf_ptr(b), sshbuf_len(b), datafellows)) != 0) 1727 debug("%s: sshkey_sign: %s", __func__, ssh_err(r)); 1728 if (r != 0) { 1729 error("sign using hostkey %s %s failed", 1730 sshkey_ssh_name(private), fp); 1731 goto out; 1732 } 1733 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1734 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1735 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1736 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1737 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 || 1738 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 || 1739 (r = sshpkt_put_cstring(ssh, chost)) != 0 || 1740 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 || 1741 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 || 1742 (r = sshpkt_send(ssh)) != 0) { 1743 error("%s: packet error: %s", __func__, ssh_err(r)); 1744 goto out; 1745 } 1746 success = 1; 1747 1748 out: 1749 if (sig != NULL) { 1750 explicit_bzero(sig, siglen); 1751 free(sig); 1752 } 1753 free(keyblob); 1754 free(lname); 1755 free(fp); 1756 free(chost); 1757 sshkey_free(private); 1758 sshbuf_free(b); 1759 1760 return success; 1761 } 1762 1763 #if KRB5 1764 static int 1765 ssh_krb5_helper(krb5_data *ap) 1766 { 1767 krb5_context xcontext = NULL; /* XXX share with ssh1 */ 1768 krb5_auth_context xauth_context = NULL; 1769 1770 krb5_context *context; 1771 krb5_auth_context *auth_context; 1772 krb5_error_code problem; 1773 const char *tkfile; 1774 struct stat buf; 1775 krb5_ccache ccache = NULL; 1776 const char *remotehost; 1777 int ret; 1778 const char *errtxt; 1779 1780 memset(ap, 0, sizeof(*ap)); 1781 1782 context = &xcontext; 1783 auth_context = &xauth_context; 1784 1785 problem = krb5_init_context(context); 1786 if (problem) { 1787 debug("Kerberos v5: krb5_init_context failed"); 1788 ret = 0; 1789 goto out; 1790 } 1791 1792 tkfile = krb5_cc_default_name(*context); 1793 if (strncmp(tkfile, "FILE:", 5) == 0) 1794 tkfile += 5; 1795 1796 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { 1797 debug("Kerberos v5: could not get default ccache (permission denied)."); 1798 ret = 0; 1799 goto out; 1800 } 1801 1802 problem = krb5_cc_default(*context, &ccache); 1803 if (problem) { 1804 errtxt = krb5_get_error_message(*context, problem); 1805 if (errtxt != NULL) { 1806 debug("Kerberos v5: krb5_cc_default failed: %s", 1807 errtxt); 1808 krb5_free_error_message(*context, errtxt); 1809 } else 1810 debug("Kerberos v5: krb5_cc_default failed: %d", 1811 problem); 1812 ret = 0; 1813 goto out; 1814 } 1815 1816 remotehost = get_canonical_hostname(1); 1817 1818 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, 1819 "host", remotehost, NULL, ccache, ap); 1820 if (problem) { 1821 errtxt = krb5_get_error_message(*context, problem); 1822 if (errtxt != NULL) { 1823 debug("Kerberos v5: krb5_mk_req failed: %s", errtxt); 1824 krb5_free_error_message(*context, errtxt); 1825 } else 1826 debug("Kerberos v5: krb5_mk_req failed: %d", problem); 1827 ret = 0; 1828 goto out; 1829 } 1830 ret = 1; 1831 1832 out: 1833 if (ccache != NULL) 1834 krb5_cc_close(*context, ccache); 1835 if (*auth_context) 1836 krb5_auth_con_free(*context, *auth_context); 1837 if (*context) 1838 krb5_free_context(*context); 1839 return (ret); 1840 } 1841 1842 int 1843 userauth_kerberos(Authctxt *authctxt) 1844 { 1845 krb5_data ap; 1846 1847 if (ssh_krb5_helper(&ap) == 0) 1848 return (0); 1849 1850 packet_start(SSH2_MSG_USERAUTH_REQUEST); 1851 packet_put_cstring(authctxt->server_user); 1852 packet_put_cstring(authctxt->service); 1853 packet_put_cstring(authctxt->method->name); 1854 packet_put_string(ap.data, ap.length); 1855 packet_send(); 1856 1857 krb5_data_free(&ap); 1858 return (1); 1859 } 1860 #endif 1861 1862 /* find auth method */ 1863 1864 /* 1865 * given auth method name, if configurable options permit this method fill 1866 * in auth_ident field and return true, otherwise return false. 1867 */ 1868 static int 1869 authmethod_is_enabled(Authmethod *method) 1870 { 1871 if (method == NULL) 1872 return 0; 1873 /* return false if options indicate this method is disabled */ 1874 if (method->enabled == NULL || *method->enabled == 0) 1875 return 0; 1876 /* return false if batch mode is enabled but method needs interactive mode */ 1877 if (method->batch_flag != NULL && *method->batch_flag != 0) 1878 return 0; 1879 return 1; 1880 } 1881 1882 static Authmethod * 1883 authmethod_lookup(const char *name) 1884 { 1885 Authmethod *method = NULL; 1886 if (name != NULL) 1887 for (method = authmethods; method->name != NULL; method++) 1888 if (strcmp(name, method->name) == 0) 1889 return method; 1890 debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); 1891 return NULL; 1892 } 1893 1894 /* XXX internal state */ 1895 static Authmethod *current = NULL; 1896 static char *supported = NULL; 1897 static char *preferred = NULL; 1898 1899 /* 1900 * Given the authentication method list sent by the server, return the 1901 * next method we should try. If the server initially sends a nil list, 1902 * use a built-in default list. 1903 */ 1904 static Authmethod * 1905 authmethod_get(char *authlist) 1906 { 1907 char *name = NULL; 1908 u_int next; 1909 1910 /* Use a suitable default if we're passed a nil list. */ 1911 if (authlist == NULL || strlen(authlist) == 0) 1912 authlist = options.preferred_authentications; 1913 1914 if (supported == NULL || strcmp(authlist, supported) != 0) { 1915 debug3("start over, passed a different list %s", authlist); 1916 free(supported); 1917 supported = xstrdup(authlist); 1918 preferred = options.preferred_authentications; 1919 debug3("preferred %s", preferred); 1920 current = NULL; 1921 } else if (current != NULL && authmethod_is_enabled(current)) 1922 return current; 1923 1924 for (;;) { 1925 if ((name = match_list(preferred, supported, &next)) == NULL) { 1926 debug("No more authentication methods to try."); 1927 current = NULL; 1928 return NULL; 1929 } 1930 preferred += next; 1931 debug3("authmethod_lookup %s", name); 1932 debug3("remaining preferred: %s", preferred); 1933 if ((current = authmethod_lookup(name)) != NULL && 1934 authmethod_is_enabled(current)) { 1935 debug3("authmethod_is_enabled %s", name); 1936 debug("Next authentication method: %s", name); 1937 free(name); 1938 return current; 1939 } 1940 free(name); 1941 } 1942 } 1943 1944 static char * 1945 authmethods_get(void) 1946 { 1947 Authmethod *method = NULL; 1948 Buffer b; 1949 char *list; 1950 1951 buffer_init(&b); 1952 for (method = authmethods; method->name != NULL; method++) { 1953 if (authmethod_is_enabled(method)) { 1954 if (buffer_len(&b) > 0) 1955 buffer_append(&b, ",", 1); 1956 buffer_append(&b, method->name, strlen(method->name)); 1957 } 1958 } 1959 buffer_append(&b, "\0", 1); 1960 list = xstrdup(buffer_ptr(&b)); 1961 buffer_free(&b); 1962 return list; 1963 } 1964 1965