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