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