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