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