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