1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * The authentication agent program. 6 * 7 * As far as I am concerned, the code I have written for this software 8 * can be used freely for any purpose. Any derived versions of this 9 * software must be clearly marked as such, and if the derived work is 10 * incompatible with the protocol description in the RFC file, it must be 11 * called by a name other than "ssh" or "Secure Shell". 12 * 13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include "includes.h" 37 #include <sys/queue.h> 38 RCSID("$OpenBSD: ssh-agent.c,v 1.111 2003/06/12 19:12:03 markus Exp $"); 39 40 #include <openssl/evp.h> 41 #include <openssl/md5.h> 42 43 #include "ssh.h" 44 #include "rsa.h" 45 #include "buffer.h" 46 #include "bufaux.h" 47 #include "xmalloc.h" 48 #include "getput.h" 49 #include "key.h" 50 #include "authfd.h" 51 #include "compat.h" 52 #include "log.h" 53 #include "readpass.h" 54 #include "misc.h" 55 56 #ifdef SMARTCARD 57 #include "scard.h" 58 #endif 59 60 typedef enum { 61 AUTH_UNUSED, 62 AUTH_SOCKET, 63 AUTH_CONNECTION 64 } sock_type; 65 66 typedef struct { 67 int fd; 68 sock_type type; 69 Buffer input; 70 Buffer output; 71 Buffer request; 72 } SocketEntry; 73 74 u_int sockets_alloc = 0; 75 SocketEntry *sockets = NULL; 76 77 typedef struct identity { 78 TAILQ_ENTRY(identity) next; 79 Key *key; 80 char *comment; 81 u_int death; 82 u_int confirm; 83 } Identity; 84 85 typedef struct { 86 int nentries; 87 TAILQ_HEAD(idqueue, identity) idlist; 88 } Idtab; 89 90 /* private key table, one per protocol version */ 91 Idtab idtable[3]; 92 93 int max_fd = 0; 94 95 /* pid of shell == parent of agent */ 96 pid_t parent_pid = -1; 97 98 /* pathname and directory for AUTH_SOCKET */ 99 char socket_name[1024]; 100 char socket_dir[1024]; 101 102 /* locking */ 103 int locked = 0; 104 char *lock_passwd = NULL; 105 106 extern char *__progname; 107 108 /* Default lifetime (0 == forever) */ 109 static int lifetime = 0; 110 111 static void 112 close_socket(SocketEntry *e) 113 { 114 close(e->fd); 115 e->fd = -1; 116 e->type = AUTH_UNUSED; 117 buffer_free(&e->input); 118 buffer_free(&e->output); 119 buffer_free(&e->request); 120 } 121 122 static void 123 idtab_init(void) 124 { 125 int i; 126 127 for (i = 0; i <=2; i++) { 128 TAILQ_INIT(&idtable[i].idlist); 129 idtable[i].nentries = 0; 130 } 131 } 132 133 /* return private key table for requested protocol version */ 134 static Idtab * 135 idtab_lookup(int version) 136 { 137 if (version < 1 || version > 2) 138 fatal("internal error, bad protocol version %d", version); 139 return &idtable[version]; 140 } 141 142 static void 143 free_identity(Identity *id) 144 { 145 key_free(id->key); 146 xfree(id->comment); 147 xfree(id); 148 } 149 150 /* return matching private key for given public key */ 151 static Identity * 152 lookup_identity(Key *key, int version) 153 { 154 Identity *id; 155 156 Idtab *tab = idtab_lookup(version); 157 TAILQ_FOREACH(id, &tab->idlist, next) { 158 if (key_equal(key, id->key)) 159 return (id); 160 } 161 return (NULL); 162 } 163 164 /* Check confirmation of keysign request */ 165 static int 166 confirm_key(Identity *id) 167 { 168 char *p, prompt[1024]; 169 int ret = -1; 170 171 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); 172 snprintf(prompt, sizeof(prompt), "Allow use of key %s?\n" 173 "Key fingerprint %s.", id->comment, p); 174 xfree(p); 175 p = read_passphrase(prompt, RP_ALLOW_EOF); 176 if (p != NULL) { 177 /* 178 * Accept empty responses and responses consisting 179 * of the word "yes" as affirmative. 180 */ 181 if (*p == '\0' || *p == '\n' || strcasecmp(p, "yes") == 0) 182 ret = 0; 183 xfree(p); 184 } 185 return (ret); 186 } 187 188 /* send list of supported public keys to 'client' */ 189 static void 190 process_request_identities(SocketEntry *e, int version) 191 { 192 Idtab *tab = idtab_lookup(version); 193 Identity *id; 194 Buffer msg; 195 196 buffer_init(&msg); 197 buffer_put_char(&msg, (version == 1) ? 198 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 199 buffer_put_int(&msg, tab->nentries); 200 TAILQ_FOREACH(id, &tab->idlist, next) { 201 if (id->key->type == KEY_RSA1) { 202 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); 203 buffer_put_bignum(&msg, id->key->rsa->e); 204 buffer_put_bignum(&msg, id->key->rsa->n); 205 } else { 206 u_char *blob; 207 u_int blen; 208 key_to_blob(id->key, &blob, &blen); 209 buffer_put_string(&msg, blob, blen); 210 xfree(blob); 211 } 212 buffer_put_cstring(&msg, id->comment); 213 } 214 buffer_put_int(&e->output, buffer_len(&msg)); 215 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 216 buffer_free(&msg); 217 } 218 219 /* ssh1 only */ 220 static void 221 process_authentication_challenge1(SocketEntry *e) 222 { 223 u_char buf[32], mdbuf[16], session_id[16]; 224 u_int response_type; 225 BIGNUM *challenge; 226 Identity *id; 227 int i, len; 228 Buffer msg; 229 MD5_CTX md; 230 Key *key; 231 232 buffer_init(&msg); 233 key = key_new(KEY_RSA1); 234 if ((challenge = BN_new()) == NULL) 235 fatal("process_authentication_challenge1: BN_new failed"); 236 237 (void) buffer_get_int(&e->request); /* ignored */ 238 buffer_get_bignum(&e->request, key->rsa->e); 239 buffer_get_bignum(&e->request, key->rsa->n); 240 buffer_get_bignum(&e->request, challenge); 241 242 /* Only protocol 1.1 is supported */ 243 if (buffer_len(&e->request) == 0) 244 goto failure; 245 buffer_get(&e->request, session_id, 16); 246 response_type = buffer_get_int(&e->request); 247 if (response_type != 1) 248 goto failure; 249 250 id = lookup_identity(key, 1); 251 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) { 252 Key *private = id->key; 253 /* Decrypt the challenge using the private key. */ 254 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) 255 goto failure; 256 257 /* The response is MD5 of decrypted challenge plus session id. */ 258 len = BN_num_bytes(challenge); 259 if (len <= 0 || len > 32) { 260 logit("process_authentication_challenge: bad challenge length %d", len); 261 goto failure; 262 } 263 memset(buf, 0, 32); 264 BN_bn2bin(challenge, buf + 32 - len); 265 MD5_Init(&md); 266 MD5_Update(&md, buf, 32); 267 MD5_Update(&md, session_id, 16); 268 MD5_Final(mdbuf, &md); 269 270 /* Send the response. */ 271 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); 272 for (i = 0; i < 16; i++) 273 buffer_put_char(&msg, mdbuf[i]); 274 goto send; 275 } 276 277 failure: 278 /* Unknown identity or protocol error. Send failure. */ 279 buffer_put_char(&msg, SSH_AGENT_FAILURE); 280 send: 281 buffer_put_int(&e->output, buffer_len(&msg)); 282 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 283 key_free(key); 284 BN_clear_free(challenge); 285 buffer_free(&msg); 286 } 287 288 /* ssh2 only */ 289 static void 290 process_sign_request2(SocketEntry *e) 291 { 292 u_char *blob, *data, *signature = NULL; 293 u_int blen, dlen, slen = 0; 294 extern int datafellows; 295 int ok = -1, flags; 296 Buffer msg; 297 Key *key; 298 299 datafellows = 0; 300 301 blob = buffer_get_string(&e->request, &blen); 302 data = buffer_get_string(&e->request, &dlen); 303 304 flags = buffer_get_int(&e->request); 305 if (flags & SSH_AGENT_OLD_SIGNATURE) 306 datafellows = SSH_BUG_SIGBLOB; 307 308 key = key_from_blob(blob, blen); 309 if (key != NULL) { 310 Identity *id = lookup_identity(key, 2); 311 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) 312 ok = key_sign(id->key, &signature, &slen, data, dlen); 313 } 314 key_free(key); 315 buffer_init(&msg); 316 if (ok == 0) { 317 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); 318 buffer_put_string(&msg, signature, slen); 319 } else { 320 buffer_put_char(&msg, SSH_AGENT_FAILURE); 321 } 322 buffer_put_int(&e->output, buffer_len(&msg)); 323 buffer_append(&e->output, buffer_ptr(&msg), 324 buffer_len(&msg)); 325 buffer_free(&msg); 326 xfree(data); 327 xfree(blob); 328 if (signature != NULL) 329 xfree(signature); 330 } 331 332 /* shared */ 333 static void 334 process_remove_identity(SocketEntry *e, int version) 335 { 336 u_int blen, bits; 337 int success = 0; 338 Key *key = NULL; 339 u_char *blob; 340 341 switch (version) { 342 case 1: 343 key = key_new(KEY_RSA1); 344 bits = buffer_get_int(&e->request); 345 buffer_get_bignum(&e->request, key->rsa->e); 346 buffer_get_bignum(&e->request, key->rsa->n); 347 348 if (bits != key_size(key)) 349 logit("Warning: identity keysize mismatch: actual %u, announced %u", 350 key_size(key), bits); 351 break; 352 case 2: 353 blob = buffer_get_string(&e->request, &blen); 354 key = key_from_blob(blob, blen); 355 xfree(blob); 356 break; 357 } 358 if (key != NULL) { 359 Identity *id = lookup_identity(key, version); 360 if (id != NULL) { 361 /* 362 * We have this key. Free the old key. Since we 363 * don\'t want to leave empty slots in the middle of 364 * the array, we actually free the key there and move 365 * all the entries between the empty slot and the end 366 * of the array. 367 */ 368 Idtab *tab = idtab_lookup(version); 369 if (tab->nentries < 1) 370 fatal("process_remove_identity: " 371 "internal error: tab->nentries %d", 372 tab->nentries); 373 TAILQ_REMOVE(&tab->idlist, id, next); 374 free_identity(id); 375 tab->nentries--; 376 success = 1; 377 } 378 key_free(key); 379 } 380 buffer_put_int(&e->output, 1); 381 buffer_put_char(&e->output, 382 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 383 } 384 385 static void 386 process_remove_all_identities(SocketEntry *e, int version) 387 { 388 Idtab *tab = idtab_lookup(version); 389 Identity *id; 390 391 /* Loop over all identities and clear the keys. */ 392 for (id = TAILQ_FIRST(&tab->idlist); id; 393 id = TAILQ_FIRST(&tab->idlist)) { 394 TAILQ_REMOVE(&tab->idlist, id, next); 395 free_identity(id); 396 } 397 398 /* Mark that there are no identities. */ 399 tab->nentries = 0; 400 401 /* Send success. */ 402 buffer_put_int(&e->output, 1); 403 buffer_put_char(&e->output, SSH_AGENT_SUCCESS); 404 } 405 406 static void 407 reaper(void) 408 { 409 u_int now = time(NULL); 410 Identity *id, *nxt; 411 int version; 412 Idtab *tab; 413 414 for (version = 1; version < 3; version++) { 415 tab = idtab_lookup(version); 416 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { 417 nxt = TAILQ_NEXT(id, next); 418 if (id->death != 0 && now >= id->death) { 419 TAILQ_REMOVE(&tab->idlist, id, next); 420 free_identity(id); 421 tab->nentries--; 422 } 423 } 424 } 425 } 426 427 static void 428 process_add_identity(SocketEntry *e, int version) 429 { 430 Idtab *tab = idtab_lookup(version); 431 int type, success = 0, death = 0, confirm = 0; 432 char *type_name, *comment; 433 Key *k = NULL; 434 435 switch (version) { 436 case 1: 437 k = key_new_private(KEY_RSA1); 438 (void) buffer_get_int(&e->request); /* ignored */ 439 buffer_get_bignum(&e->request, k->rsa->n); 440 buffer_get_bignum(&e->request, k->rsa->e); 441 buffer_get_bignum(&e->request, k->rsa->d); 442 buffer_get_bignum(&e->request, k->rsa->iqmp); 443 444 /* SSH and SSL have p and q swapped */ 445 buffer_get_bignum(&e->request, k->rsa->q); /* p */ 446 buffer_get_bignum(&e->request, k->rsa->p); /* q */ 447 448 /* Generate additional parameters */ 449 rsa_generate_additional_parameters(k->rsa); 450 break; 451 case 2: 452 type_name = buffer_get_string(&e->request, NULL); 453 type = key_type_from_name(type_name); 454 xfree(type_name); 455 switch (type) { 456 case KEY_DSA: 457 k = key_new_private(type); 458 buffer_get_bignum2(&e->request, k->dsa->p); 459 buffer_get_bignum2(&e->request, k->dsa->q); 460 buffer_get_bignum2(&e->request, k->dsa->g); 461 buffer_get_bignum2(&e->request, k->dsa->pub_key); 462 buffer_get_bignum2(&e->request, k->dsa->priv_key); 463 break; 464 case KEY_RSA: 465 k = key_new_private(type); 466 buffer_get_bignum2(&e->request, k->rsa->n); 467 buffer_get_bignum2(&e->request, k->rsa->e); 468 buffer_get_bignum2(&e->request, k->rsa->d); 469 buffer_get_bignum2(&e->request, k->rsa->iqmp); 470 buffer_get_bignum2(&e->request, k->rsa->p); 471 buffer_get_bignum2(&e->request, k->rsa->q); 472 473 /* Generate additional parameters */ 474 rsa_generate_additional_parameters(k->rsa); 475 break; 476 default: 477 buffer_clear(&e->request); 478 goto send; 479 } 480 break; 481 } 482 /* enable blinding */ 483 switch (k->type) { 484 case KEY_RSA: 485 case KEY_RSA1: 486 if (RSA_blinding_on(k->rsa, NULL) != 1) { 487 error("process_add_identity: RSA_blinding_on failed"); 488 key_free(k); 489 goto send; 490 } 491 break; 492 } 493 comment = buffer_get_string(&e->request, NULL); 494 if (k == NULL) { 495 xfree(comment); 496 goto send; 497 } 498 success = 1; 499 while (buffer_len(&e->request)) { 500 switch (buffer_get_char(&e->request)) { 501 case SSH_AGENT_CONSTRAIN_LIFETIME: 502 death = time(NULL) + buffer_get_int(&e->request); 503 break; 504 case SSH_AGENT_CONSTRAIN_CONFIRM: 505 confirm = 1; 506 break; 507 default: 508 break; 509 } 510 } 511 if (lifetime && !death) 512 death = time(NULL) + lifetime; 513 if (lookup_identity(k, version) == NULL) { 514 Identity *id = xmalloc(sizeof(Identity)); 515 id->key = k; 516 id->comment = comment; 517 id->death = death; 518 id->confirm = confirm; 519 TAILQ_INSERT_TAIL(&tab->idlist, id, next); 520 /* Increment the number of identities. */ 521 tab->nentries++; 522 } else { 523 key_free(k); 524 xfree(comment); 525 } 526 send: 527 buffer_put_int(&e->output, 1); 528 buffer_put_char(&e->output, 529 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 530 } 531 532 /* XXX todo: encrypt sensitive data with passphrase */ 533 static void 534 process_lock_agent(SocketEntry *e, int lock) 535 { 536 int success = 0; 537 char *passwd; 538 539 passwd = buffer_get_string(&e->request, NULL); 540 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { 541 locked = 0; 542 memset(lock_passwd, 0, strlen(lock_passwd)); 543 xfree(lock_passwd); 544 lock_passwd = NULL; 545 success = 1; 546 } else if (!locked && lock) { 547 locked = 1; 548 lock_passwd = xstrdup(passwd); 549 success = 1; 550 } 551 memset(passwd, 0, strlen(passwd)); 552 xfree(passwd); 553 554 buffer_put_int(&e->output, 1); 555 buffer_put_char(&e->output, 556 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 557 } 558 559 static void 560 no_identities(SocketEntry *e, u_int type) 561 { 562 Buffer msg; 563 564 buffer_init(&msg); 565 buffer_put_char(&msg, 566 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? 567 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 568 buffer_put_int(&msg, 0); 569 buffer_put_int(&e->output, buffer_len(&msg)); 570 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 571 buffer_free(&msg); 572 } 573 574 #ifdef SMARTCARD 575 static void 576 process_add_smartcard_key (SocketEntry *e) 577 { 578 char *sc_reader_id = NULL, *pin; 579 int i, version, success = 0, death = 0, confirm = 0; 580 Key **keys, *k; 581 Identity *id; 582 Idtab *tab; 583 584 sc_reader_id = buffer_get_string(&e->request, NULL); 585 pin = buffer_get_string(&e->request, NULL); 586 587 while (buffer_len(&e->request)) { 588 switch (buffer_get_char(&e->request)) { 589 case SSH_AGENT_CONSTRAIN_LIFETIME: 590 death = time(NULL) + buffer_get_int(&e->request); 591 break; 592 case SSH_AGENT_CONSTRAIN_CONFIRM: 593 confirm = 1; 594 break; 595 default: 596 break; 597 } 598 } 599 if (lifetime && !death) 600 death = time(NULL) + lifetime; 601 602 keys = sc_get_keys(sc_reader_id, pin); 603 xfree(sc_reader_id); 604 xfree(pin); 605 606 if (keys == NULL || keys[0] == NULL) { 607 error("sc_get_keys failed"); 608 goto send; 609 } 610 for (i = 0; keys[i] != NULL; i++) { 611 k = keys[i]; 612 version = k->type == KEY_RSA1 ? 1 : 2; 613 tab = idtab_lookup(version); 614 if (lookup_identity(k, version) == NULL) { 615 id = xmalloc(sizeof(Identity)); 616 id->key = k; 617 id->comment = sc_get_key_label(k); 618 id->death = death; 619 id->confirm = confirm; 620 TAILQ_INSERT_TAIL(&tab->idlist, id, next); 621 tab->nentries++; 622 success = 1; 623 } else { 624 key_free(k); 625 } 626 keys[i] = NULL; 627 } 628 xfree(keys); 629 send: 630 buffer_put_int(&e->output, 1); 631 buffer_put_char(&e->output, 632 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 633 } 634 635 static void 636 process_remove_smartcard_key(SocketEntry *e) 637 { 638 char *sc_reader_id = NULL, *pin; 639 int i, version, success = 0; 640 Key **keys, *k = NULL; 641 Identity *id; 642 Idtab *tab; 643 644 sc_reader_id = buffer_get_string(&e->request, NULL); 645 pin = buffer_get_string(&e->request, NULL); 646 keys = sc_get_keys(sc_reader_id, pin); 647 xfree(sc_reader_id); 648 xfree(pin); 649 650 if (keys == NULL || keys[0] == NULL) { 651 error("sc_get_keys failed"); 652 goto send; 653 } 654 for (i = 0; keys[i] != NULL; i++) { 655 k = keys[i]; 656 version = k->type == KEY_RSA1 ? 1 : 2; 657 if ((id = lookup_identity(k, version)) != NULL) { 658 tab = idtab_lookup(version); 659 TAILQ_REMOVE(&tab->idlist, id, next); 660 tab->nentries--; 661 free_identity(id); 662 success = 1; 663 } 664 key_free(k); 665 keys[i] = NULL; 666 } 667 xfree(keys); 668 send: 669 buffer_put_int(&e->output, 1); 670 buffer_put_char(&e->output, 671 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 672 } 673 #endif /* SMARTCARD */ 674 675 /* dispatch incoming messages */ 676 677 static void 678 process_message(SocketEntry *e) 679 { 680 u_int msg_len, type; 681 u_char *cp; 682 683 /* kill dead keys */ 684 reaper(); 685 686 if (buffer_len(&e->input) < 5) 687 return; /* Incomplete message. */ 688 cp = buffer_ptr(&e->input); 689 msg_len = GET_32BIT(cp); 690 if (msg_len > 256 * 1024) { 691 close_socket(e); 692 return; 693 } 694 if (buffer_len(&e->input) < msg_len + 4) 695 return; 696 697 /* move the current input to e->request */ 698 buffer_consume(&e->input, 4); 699 buffer_clear(&e->request); 700 buffer_append(&e->request, buffer_ptr(&e->input), msg_len); 701 buffer_consume(&e->input, msg_len); 702 type = buffer_get_char(&e->request); 703 704 /* check wheter agent is locked */ 705 if (locked && type != SSH_AGENTC_UNLOCK) { 706 buffer_clear(&e->request); 707 switch (type) { 708 case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 709 case SSH2_AGENTC_REQUEST_IDENTITIES: 710 /* send empty lists */ 711 no_identities(e, type); 712 break; 713 default: 714 /* send a fail message for all other request types */ 715 buffer_put_int(&e->output, 1); 716 buffer_put_char(&e->output, SSH_AGENT_FAILURE); 717 } 718 return; 719 } 720 721 debug("type %d", type); 722 switch (type) { 723 case SSH_AGENTC_LOCK: 724 case SSH_AGENTC_UNLOCK: 725 process_lock_agent(e, type == SSH_AGENTC_LOCK); 726 break; 727 /* ssh1 */ 728 case SSH_AGENTC_RSA_CHALLENGE: 729 process_authentication_challenge1(e); 730 break; 731 case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 732 process_request_identities(e, 1); 733 break; 734 case SSH_AGENTC_ADD_RSA_IDENTITY: 735 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: 736 process_add_identity(e, 1); 737 break; 738 case SSH_AGENTC_REMOVE_RSA_IDENTITY: 739 process_remove_identity(e, 1); 740 break; 741 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: 742 process_remove_all_identities(e, 1); 743 break; 744 /* ssh2 */ 745 case SSH2_AGENTC_SIGN_REQUEST: 746 process_sign_request2(e); 747 break; 748 case SSH2_AGENTC_REQUEST_IDENTITIES: 749 process_request_identities(e, 2); 750 break; 751 case SSH2_AGENTC_ADD_IDENTITY: 752 case SSH2_AGENTC_ADD_ID_CONSTRAINED: 753 process_add_identity(e, 2); 754 break; 755 case SSH2_AGENTC_REMOVE_IDENTITY: 756 process_remove_identity(e, 2); 757 break; 758 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: 759 process_remove_all_identities(e, 2); 760 break; 761 #ifdef SMARTCARD 762 case SSH_AGENTC_ADD_SMARTCARD_KEY: 763 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED: 764 process_add_smartcard_key(e); 765 break; 766 case SSH_AGENTC_REMOVE_SMARTCARD_KEY: 767 process_remove_smartcard_key(e); 768 break; 769 #endif /* SMARTCARD */ 770 default: 771 /* Unknown message. Respond with failure. */ 772 error("Unknown message %d", type); 773 buffer_clear(&e->request); 774 buffer_put_int(&e->output, 1); 775 buffer_put_char(&e->output, SSH_AGENT_FAILURE); 776 break; 777 } 778 } 779 780 static void 781 new_socket(sock_type type, int fd) 782 { 783 u_int i, old_alloc; 784 785 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) 786 error("fcntl O_NONBLOCK: %s", strerror(errno)); 787 788 if (fd > max_fd) 789 max_fd = fd; 790 791 for (i = 0; i < sockets_alloc; i++) 792 if (sockets[i].type == AUTH_UNUSED) { 793 sockets[i].fd = fd; 794 sockets[i].type = type; 795 buffer_init(&sockets[i].input); 796 buffer_init(&sockets[i].output); 797 buffer_init(&sockets[i].request); 798 return; 799 } 800 old_alloc = sockets_alloc; 801 sockets_alloc += 10; 802 if (sockets) 803 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); 804 else 805 sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); 806 for (i = old_alloc; i < sockets_alloc; i++) 807 sockets[i].type = AUTH_UNUSED; 808 sockets[old_alloc].type = type; 809 sockets[old_alloc].fd = fd; 810 buffer_init(&sockets[old_alloc].input); 811 buffer_init(&sockets[old_alloc].output); 812 buffer_init(&sockets[old_alloc].request); 813 } 814 815 static int 816 prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) 817 { 818 u_int i, sz; 819 int n = 0; 820 821 for (i = 0; i < sockets_alloc; i++) { 822 switch (sockets[i].type) { 823 case AUTH_SOCKET: 824 case AUTH_CONNECTION: 825 n = MAX(n, sockets[i].fd); 826 break; 827 case AUTH_UNUSED: 828 break; 829 default: 830 fatal("Unknown socket type %d", sockets[i].type); 831 break; 832 } 833 } 834 835 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); 836 if (*fdrp == NULL || sz > *nallocp) { 837 if (*fdrp) 838 xfree(*fdrp); 839 if (*fdwp) 840 xfree(*fdwp); 841 *fdrp = xmalloc(sz); 842 *fdwp = xmalloc(sz); 843 *nallocp = sz; 844 } 845 if (n < *fdl) 846 debug("XXX shrink: %d < %d", n, *fdl); 847 *fdl = n; 848 memset(*fdrp, 0, sz); 849 memset(*fdwp, 0, sz); 850 851 for (i = 0; i < sockets_alloc; i++) { 852 switch (sockets[i].type) { 853 case AUTH_SOCKET: 854 case AUTH_CONNECTION: 855 FD_SET(sockets[i].fd, *fdrp); 856 if (buffer_len(&sockets[i].output) > 0) 857 FD_SET(sockets[i].fd, *fdwp); 858 break; 859 default: 860 break; 861 } 862 } 863 return (1); 864 } 865 866 static void 867 after_select(fd_set *readset, fd_set *writeset) 868 { 869 struct sockaddr_un sunaddr; 870 socklen_t slen; 871 char buf[1024]; 872 int len, sock; 873 u_int i; 874 uid_t euid; 875 gid_t egid; 876 877 for (i = 0; i < sockets_alloc; i++) 878 switch (sockets[i].type) { 879 case AUTH_UNUSED: 880 break; 881 case AUTH_SOCKET: 882 if (FD_ISSET(sockets[i].fd, readset)) { 883 slen = sizeof(sunaddr); 884 sock = accept(sockets[i].fd, 885 (struct sockaddr *) &sunaddr, &slen); 886 if (sock < 0) { 887 error("accept from AUTH_SOCKET: %s", 888 strerror(errno)); 889 break; 890 } 891 if (getpeereid(sock, &euid, &egid) < 0) { 892 error("getpeereid %d failed: %s", 893 sock, strerror(errno)); 894 close(sock); 895 break; 896 } 897 if ((euid != 0) && (getuid() != euid)) { 898 error("uid mismatch: " 899 "peer euid %u != uid %u", 900 (u_int) euid, (u_int) getuid()); 901 close(sock); 902 break; 903 } 904 new_socket(AUTH_CONNECTION, sock); 905 } 906 break; 907 case AUTH_CONNECTION: 908 if (buffer_len(&sockets[i].output) > 0 && 909 FD_ISSET(sockets[i].fd, writeset)) { 910 do { 911 len = write(sockets[i].fd, 912 buffer_ptr(&sockets[i].output), 913 buffer_len(&sockets[i].output)); 914 if (len == -1 && (errno == EAGAIN || 915 errno == EINTR)) 916 continue; 917 break; 918 } while (1); 919 if (len <= 0) { 920 close_socket(&sockets[i]); 921 break; 922 } 923 buffer_consume(&sockets[i].output, len); 924 } 925 if (FD_ISSET(sockets[i].fd, readset)) { 926 do { 927 len = read(sockets[i].fd, buf, sizeof(buf)); 928 if (len == -1 && (errno == EAGAIN || 929 errno == EINTR)) 930 continue; 931 break; 932 } while (1); 933 if (len <= 0) { 934 close_socket(&sockets[i]); 935 break; 936 } 937 buffer_append(&sockets[i].input, buf, len); 938 process_message(&sockets[i]); 939 } 940 break; 941 default: 942 fatal("Unknown type %d", sockets[i].type); 943 } 944 } 945 946 static void 947 cleanup_socket(void *p) 948 { 949 if (socket_name[0]) 950 unlink(socket_name); 951 if (socket_dir[0]) 952 rmdir(socket_dir); 953 } 954 955 static void 956 cleanup_exit(int i) 957 { 958 cleanup_socket(NULL); 959 exit(i); 960 } 961 962 static void 963 cleanup_handler(int sig) 964 { 965 cleanup_socket(NULL); 966 _exit(2); 967 } 968 969 static void 970 check_parent_exists(int sig) 971 { 972 int save_errno = errno; 973 974 if (parent_pid != -1 && kill(parent_pid, 0) < 0) { 975 /* printf("Parent has died - Authentication agent exiting.\n"); */ 976 cleanup_handler(sig); /* safe */ 977 } 978 signal(SIGALRM, check_parent_exists); 979 alarm(10); 980 errno = save_errno; 981 } 982 983 static void 984 usage(void) 985 { 986 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n", 987 __progname); 988 fprintf(stderr, "Options:\n"); 989 fprintf(stderr, " -c Generate C-shell commands on stdout.\n"); 990 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n"); 991 fprintf(stderr, " -k Kill the current agent.\n"); 992 fprintf(stderr, " -d Debug mode.\n"); 993 fprintf(stderr, " -a socket Bind agent socket to given name.\n"); 994 fprintf(stderr, " -t life Default identity lifetime (seconds).\n"); 995 exit(1); 996 } 997 998 int 999 main(int ac, char **av) 1000 { 1001 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; 1002 int sock, fd, ch, nalloc; 1003 char *shell, *format, *pidstr, *agentsocket = NULL; 1004 fd_set *readsetp = NULL, *writesetp = NULL; 1005 struct sockaddr_un sunaddr; 1006 struct rlimit rlim; 1007 extern int optind; 1008 extern char *optarg; 1009 pid_t pid; 1010 char pidstrbuf[1 + 3 * sizeof pid]; 1011 1012 /* drop */ 1013 setegid(getgid()); 1014 setgid(getgid()); 1015 1016 SSLeay_add_all_algorithms(); 1017 1018 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { 1019 switch (ch) { 1020 case 'c': 1021 if (s_flag) 1022 usage(); 1023 c_flag++; 1024 break; 1025 case 'k': 1026 k_flag++; 1027 break; 1028 case 's': 1029 if (c_flag) 1030 usage(); 1031 s_flag++; 1032 break; 1033 case 'd': 1034 if (d_flag) 1035 usage(); 1036 d_flag++; 1037 break; 1038 case 'a': 1039 agentsocket = optarg; 1040 break; 1041 case 't': 1042 if ((lifetime = convtime(optarg)) == -1) { 1043 fprintf(stderr, "Invalid lifetime\n"); 1044 usage(); 1045 } 1046 break; 1047 default: 1048 usage(); 1049 } 1050 } 1051 ac -= optind; 1052 av += optind; 1053 1054 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) 1055 usage(); 1056 1057 if (ac == 0 && !c_flag && !s_flag) { 1058 shell = getenv("SHELL"); 1059 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) 1060 c_flag = 1; 1061 } 1062 if (k_flag) { 1063 pidstr = getenv(SSH_AGENTPID_ENV_NAME); 1064 if (pidstr == NULL) { 1065 fprintf(stderr, "%s not set, cannot kill agent\n", 1066 SSH_AGENTPID_ENV_NAME); 1067 exit(1); 1068 } 1069 pid = atoi(pidstr); 1070 if (pid < 1) { 1071 fprintf(stderr, "%s=\"%s\", which is not a good PID\n", 1072 SSH_AGENTPID_ENV_NAME, pidstr); 1073 exit(1); 1074 } 1075 if (kill(pid, SIGTERM) == -1) { 1076 perror("kill"); 1077 exit(1); 1078 } 1079 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; 1080 printf(format, SSH_AUTHSOCKET_ENV_NAME); 1081 printf(format, SSH_AGENTPID_ENV_NAME); 1082 printf("echo Agent pid %ld killed;\n", (long)pid); 1083 exit(0); 1084 } 1085 parent_pid = getpid(); 1086 1087 if (agentsocket == NULL) { 1088 /* Create private directory for agent socket */ 1089 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); 1090 if (mkdtemp(socket_dir) == NULL) { 1091 perror("mkdtemp: private socket dir"); 1092 exit(1); 1093 } 1094 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, 1095 (long)parent_pid); 1096 } else { 1097 /* Try to use specified agent socket */ 1098 socket_dir[0] = '\0'; 1099 strlcpy(socket_name, agentsocket, sizeof socket_name); 1100 } 1101 1102 /* 1103 * Create socket early so it will exist before command gets run from 1104 * the parent. 1105 */ 1106 sock = socket(AF_UNIX, SOCK_STREAM, 0); 1107 if (sock < 0) { 1108 perror("socket"); 1109 cleanup_exit(1); 1110 } 1111 memset(&sunaddr, 0, sizeof(sunaddr)); 1112 sunaddr.sun_family = AF_UNIX; 1113 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); 1114 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { 1115 perror("bind"); 1116 cleanup_exit(1); 1117 } 1118 if (listen(sock, 128) < 0) { 1119 perror("listen"); 1120 cleanup_exit(1); 1121 } 1122 1123 /* 1124 * Fork, and have the parent execute the command, if any, or present 1125 * the socket data. The child continues as the authentication agent. 1126 */ 1127 if (d_flag) { 1128 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); 1129 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 1130 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 1131 SSH_AUTHSOCKET_ENV_NAME); 1132 printf("echo Agent pid %ld;\n", (long)parent_pid); 1133 goto skip; 1134 } 1135 pid = fork(); 1136 if (pid == -1) { 1137 perror("fork"); 1138 cleanup_exit(1); 1139 } 1140 if (pid != 0) { /* Parent - execute the given command. */ 1141 close(sock); 1142 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); 1143 if (ac == 0) { 1144 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 1145 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 1146 SSH_AUTHSOCKET_ENV_NAME); 1147 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, 1148 SSH_AGENTPID_ENV_NAME); 1149 printf("echo Agent pid %ld;\n", (long)pid); 1150 exit(0); 1151 } 1152 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || 1153 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { 1154 perror("setenv"); 1155 exit(1); 1156 } 1157 execvp(av[0], av); 1158 perror(av[0]); 1159 exit(1); 1160 } 1161 /* child */ 1162 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); 1163 1164 if (setsid() == -1) { 1165 error("setsid: %s", strerror(errno)); 1166 cleanup_exit(1); 1167 } 1168 1169 (void)chdir("/"); 1170 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 1171 /* XXX might close listen socket */ 1172 (void)dup2(fd, STDIN_FILENO); 1173 (void)dup2(fd, STDOUT_FILENO); 1174 (void)dup2(fd, STDERR_FILENO); 1175 if (fd > 2) 1176 close(fd); 1177 } 1178 1179 /* deny core dumps, since memory contains unencrypted private keys */ 1180 rlim.rlim_cur = rlim.rlim_max = 0; 1181 if (setrlimit(RLIMIT_CORE, &rlim) < 0) { 1182 error("setrlimit RLIMIT_CORE: %s", strerror(errno)); 1183 cleanup_exit(1); 1184 } 1185 1186 skip: 1187 fatal_add_cleanup(cleanup_socket, NULL); 1188 new_socket(AUTH_SOCKET, sock); 1189 if (ac > 0) { 1190 signal(SIGALRM, check_parent_exists); 1191 alarm(10); 1192 } 1193 idtab_init(); 1194 if (!d_flag) 1195 signal(SIGINT, SIG_IGN); 1196 signal(SIGPIPE, SIG_IGN); 1197 signal(SIGHUP, cleanup_handler); 1198 signal(SIGTERM, cleanup_handler); 1199 nalloc = 0; 1200 1201 while (1) { 1202 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); 1203 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { 1204 if (errno == EINTR) 1205 continue; 1206 fatal("select: %s", strerror(errno)); 1207 } 1208 after_select(readsetp, writesetp); 1209 } 1210 /* NOTREACHED */ 1211 } 1212