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