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