1 /* $OpenBSD: ssh-add.c,v 1.150 2020/01/17 20:13:47 naddy Exp $ */ 2 /* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * Adds an identity to the authentication server, or removes an identity. 7 * 8 * As far as I am concerned, the code I have written for this software 9 * can be used freely for any purpose. Any derived versions of this 10 * software must be clearly marked as such, and if the derived work is 11 * incompatible with the protocol description in the RFC file, it must be 12 * called by a name other than "ssh" or "Secure Shell". 13 * 14 * SSH2 implementation, 15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 41 #ifdef WITH_OPENSSL 42 #include <openssl/evp.h> 43 #endif 44 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <pwd.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <stdarg.h> 52 #include <unistd.h> 53 #include <limits.h> 54 55 #include "xmalloc.h" 56 #include "ssh.h" 57 #include "log.h" 58 #include "sshkey.h" 59 #include "sshbuf.h" 60 #include "authfd.h" 61 #include "authfile.h" 62 #include "pathnames.h" 63 #include "misc.h" 64 #include "ssherr.h" 65 #include "digest.h" 66 #include "ssh-sk.h" 67 68 /* argv0 */ 69 extern char *__progname; 70 71 /* Default files to add */ 72 static char *default_files[] = { 73 _PATH_SSH_CLIENT_ID_RSA, 74 _PATH_SSH_CLIENT_ID_DSA, 75 _PATH_SSH_CLIENT_ID_ECDSA, 76 _PATH_SSH_CLIENT_ID_ECDSA_SK, 77 _PATH_SSH_CLIENT_ID_ED25519, 78 _PATH_SSH_CLIENT_ID_ED25519_SK, 79 _PATH_SSH_CLIENT_ID_XMSS, 80 NULL 81 }; 82 83 static int fingerprint_hash = SSH_FP_HASH_DEFAULT; 84 85 /* Default lifetime (0 == forever) */ 86 static int lifetime = 0; 87 88 /* User has to confirm key use */ 89 static int confirm = 0; 90 91 /* Maximum number of signatures (XMSS) */ 92 static u_int maxsign = 0; 93 static u_int minleft = 0; 94 95 /* we keep a cache of one passphrase */ 96 static char *pass = NULL; 97 static void 98 clear_pass(void) 99 { 100 if (pass) { 101 explicit_bzero(pass, strlen(pass)); 102 free(pass); 103 pass = NULL; 104 } 105 } 106 107 static int 108 delete_file(int agent_fd, const char *filename, int key_only, int qflag) 109 { 110 struct sshkey *public, *cert = NULL; 111 char *certpath = NULL, *comment = NULL; 112 int r, ret = -1; 113 114 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { 115 printf("Bad key file %s: %s\n", filename, ssh_err(r)); 116 return -1; 117 } 118 if ((r = ssh_remove_identity(agent_fd, public)) == 0) { 119 if (!qflag) { 120 fprintf(stderr, "Identity removed: %s (%s)\n", 121 filename, comment); 122 } 123 ret = 0; 124 } else 125 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 126 filename, ssh_err(r)); 127 128 if (key_only) 129 goto out; 130 131 /* Now try to delete the corresponding certificate too */ 132 free(comment); 133 comment = NULL; 134 xasprintf(&certpath, "%s-cert.pub", filename); 135 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { 136 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 137 error("Failed to load certificate \"%s\": %s", 138 certpath, ssh_err(r)); 139 goto out; 140 } 141 142 if (!sshkey_equal_public(cert, public)) 143 fatal("Certificate %s does not match private key %s", 144 certpath, filename); 145 146 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { 147 if (!qflag) { 148 fprintf(stderr, "Identity removed: %s (%s)\n", 149 certpath, comment); 150 } 151 ret = 0; 152 } else 153 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 154 certpath, ssh_err(r)); 155 156 out: 157 sshkey_free(cert); 158 sshkey_free(public); 159 free(certpath); 160 free(comment); 161 162 return ret; 163 } 164 165 /* Send a request to remove all identities. */ 166 static int 167 delete_all(int agent_fd, int qflag) 168 { 169 int ret = -1; 170 171 /* 172 * Since the agent might be forwarded, old or non-OpenSSH, when asked 173 * to remove all keys, attempt to remove both protocol v.1 and v.2 174 * keys. 175 */ 176 if (ssh_remove_all_identities(agent_fd, 2) == 0) 177 ret = 0; 178 /* ignore error-code for ssh1 */ 179 ssh_remove_all_identities(agent_fd, 1); 180 181 if (ret != 0) 182 fprintf(stderr, "Failed to remove all identities.\n"); 183 else if (!qflag) 184 fprintf(stderr, "All identities removed.\n"); 185 186 return ret; 187 } 188 189 static int 190 add_file(int agent_fd, const char *filename, int key_only, int qflag, 191 const char *skprovider) 192 { 193 struct sshkey *private, *cert; 194 char *comment = NULL; 195 char msg[1024], *certpath = NULL; 196 int r, fd, ret = -1; 197 size_t i; 198 u_int32_t left; 199 struct sshbuf *keyblob; 200 struct ssh_identitylist *idlist; 201 202 if (strcmp(filename, "-") == 0) { 203 fd = STDIN_FILENO; 204 filename = "(stdin)"; 205 } else if ((fd = open(filename, O_RDONLY)) == -1) { 206 perror(filename); 207 return -1; 208 } 209 210 /* 211 * Since we'll try to load a keyfile multiple times, permission errors 212 * will occur multiple times, so check perms first and bail if wrong. 213 */ 214 if (fd != STDIN_FILENO) { 215 if (sshkey_perm_ok(fd, filename) != 0) { 216 close(fd); 217 return -1; 218 } 219 } 220 if ((keyblob = sshbuf_new()) == NULL) 221 fatal("%s: sshbuf_new failed", __func__); 222 if ((r = sshkey_load_file(fd, keyblob)) != 0) { 223 fprintf(stderr, "Error loading key \"%s\": %s\n", 224 filename, ssh_err(r)); 225 sshbuf_free(keyblob); 226 close(fd); 227 return -1; 228 } 229 close(fd); 230 231 /* At first, try empty passphrase */ 232 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private, 233 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 234 fprintf(stderr, "Error loading key \"%s\": %s\n", 235 filename, ssh_err(r)); 236 goto fail_load; 237 } 238 /* try last */ 239 if (private == NULL && pass != NULL) { 240 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private, 241 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 242 fprintf(stderr, "Error loading key \"%s\": %s\n", 243 filename, ssh_err(r)); 244 goto fail_load; 245 } 246 } 247 if (private == NULL) { 248 /* clear passphrase since it did not work */ 249 clear_pass(); 250 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ", 251 filename, confirm ? " (will confirm each use)" : ""); 252 for (;;) { 253 pass = read_passphrase(msg, RP_ALLOW_STDIN); 254 if (strcmp(pass, "") == 0) 255 goto fail_load; 256 if ((r = sshkey_parse_private_fileblob(keyblob, pass, 257 &private, &comment)) == 0) 258 break; 259 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 260 fprintf(stderr, 261 "Error loading key \"%s\": %s\n", 262 filename, ssh_err(r)); 263 fail_load: 264 clear_pass(); 265 sshbuf_free(keyblob); 266 return -1; 267 } 268 clear_pass(); 269 snprintf(msg, sizeof msg, 270 "Bad passphrase, try again for %s%s: ", filename, 271 confirm ? " (will confirm each use)" : ""); 272 } 273 } 274 if (comment == NULL || *comment == '\0') 275 comment = xstrdup(filename); 276 sshbuf_free(keyblob); 277 278 /* For XMSS */ 279 if ((r = sshkey_set_filename(private, filename)) != 0) { 280 fprintf(stderr, "Could not add filename to private key: %s (%s)\n", 281 filename, comment); 282 goto out; 283 } 284 if (maxsign && minleft && 285 (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) { 286 for (i = 0; i < idlist->nkeys; i++) { 287 if (!sshkey_equal_public(idlist->keys[i], private)) 288 continue; 289 left = sshkey_signatures_left(idlist->keys[i]); 290 if (left < minleft) { 291 fprintf(stderr, 292 "Only %d signatures left.\n", left); 293 break; 294 } 295 fprintf(stderr, "Skipping update: "); 296 if (left == minleft) { 297 fprintf(stderr, 298 "required signatures left (%d).\n", left); 299 } else { 300 fprintf(stderr, 301 "more signatures left (%d) than" 302 " required (%d).\n", left, minleft); 303 } 304 ssh_free_identitylist(idlist); 305 goto out; 306 } 307 ssh_free_identitylist(idlist); 308 } 309 310 if (!sshkey_is_sk(private)) 311 skprovider = NULL; /* Don't send constraint for other keys */ 312 else if (skprovider == NULL) { 313 fprintf(stderr, "Cannot load security key %s without " 314 "provider\n", filename); 315 goto out; 316 } 317 318 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 319 lifetime, confirm, maxsign, skprovider)) == 0) { 320 ret = 0; 321 if (!qflag) { 322 fprintf(stderr, "Identity added: %s (%s)\n", 323 filename, comment); 324 if (lifetime != 0) { 325 fprintf(stderr, 326 "Lifetime set to %d seconds\n", lifetime); 327 } 328 if (confirm != 0) { 329 fprintf(stderr, "The user must confirm " 330 "each use of the key\n"); 331 } 332 } 333 } else { 334 fprintf(stderr, "Could not add identity \"%s\": %s\n", 335 filename, ssh_err(r)); 336 } 337 338 /* Skip trying to load the cert if requested */ 339 if (key_only) 340 goto out; 341 342 /* Now try to add the certificate flavour too */ 343 xasprintf(&certpath, "%s-cert.pub", filename); 344 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { 345 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 346 error("Failed to load certificate \"%s\": %s", 347 certpath, ssh_err(r)); 348 goto out; 349 } 350 351 if (!sshkey_equal_public(cert, private)) { 352 error("Certificate %s does not match private key %s", 353 certpath, filename); 354 sshkey_free(cert); 355 goto out; 356 } 357 358 /* Graft with private bits */ 359 if ((r = sshkey_to_certified(private)) != 0) { 360 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); 361 sshkey_free(cert); 362 goto out; 363 } 364 if ((r = sshkey_cert_copy(cert, private)) != 0) { 365 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r)); 366 sshkey_free(cert); 367 goto out; 368 } 369 sshkey_free(cert); 370 371 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 372 lifetime, confirm, maxsign, skprovider)) != 0) { 373 error("Certificate %s (%s) add failed: %s", certpath, 374 private->cert->key_id, ssh_err(r)); 375 goto out; 376 } 377 /* success */ 378 if (!qflag) { 379 fprintf(stderr, "Certificate added: %s (%s)\n", certpath, 380 private->cert->key_id); 381 if (lifetime != 0) { 382 fprintf(stderr, "Lifetime set to %d seconds\n", 383 lifetime); 384 } 385 if (confirm != 0) { 386 fprintf(stderr, "The user must confirm each use " 387 "of the key\n"); 388 } 389 } 390 391 out: 392 free(certpath); 393 free(comment); 394 sshkey_free(private); 395 396 return ret; 397 } 398 399 static int 400 update_card(int agent_fd, int add, const char *id, int qflag) 401 { 402 char *pin = NULL; 403 int r, ret = -1; 404 405 if (add) { 406 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", 407 RP_ALLOW_STDIN)) == NULL) 408 return -1; 409 } 410 411 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, 412 lifetime, confirm)) == 0) { 413 ret = 0; 414 if (!qflag) { 415 fprintf(stderr, "Card %s: %s\n", 416 add ? "added" : "removed", id); 417 } 418 } else { 419 fprintf(stderr, "Could not %s card \"%s\": %s\n", 420 add ? "add" : "remove", id, ssh_err(r)); 421 ret = -1; 422 } 423 free(pin); 424 return ret; 425 } 426 427 static int 428 test_key(int agent_fd, const char *filename) 429 { 430 struct sshkey *key = NULL; 431 u_char *sig = NULL; 432 size_t slen = 0; 433 int r, ret = -1; 434 char data[1024]; 435 436 if ((r = sshkey_load_public(filename, &key, NULL)) != 0) { 437 error("Couldn't read public key %s: %s", filename, ssh_err(r)); 438 return -1; 439 } 440 arc4random_buf(data, sizeof(data)); 441 if ((r = ssh_agent_sign(agent_fd, key, &sig, &slen, data, sizeof(data), 442 NULL, 0)) != 0) { 443 error("Agent signature failed for %s: %s", 444 filename, ssh_err(r)); 445 goto done; 446 } 447 if ((r = sshkey_verify(key, sig, slen, data, sizeof(data), 448 NULL, 0, NULL)) != 0) { 449 error("Signature verification failed for %s: %s", 450 filename, ssh_err(r)); 451 goto done; 452 } 453 /* success */ 454 ret = 0; 455 done: 456 free(sig); 457 sshkey_free(key); 458 return ret; 459 } 460 461 static int 462 list_identities(int agent_fd, int do_fp) 463 { 464 char *fp; 465 int r; 466 struct ssh_identitylist *idlist; 467 u_int32_t left; 468 size_t i; 469 470 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) { 471 if (r != SSH_ERR_AGENT_NO_IDENTITIES) 472 fprintf(stderr, "error fetching identities: %s\n", 473 ssh_err(r)); 474 else 475 printf("The agent has no identities.\n"); 476 return -1; 477 } 478 for (i = 0; i < idlist->nkeys; i++) { 479 if (do_fp) { 480 fp = sshkey_fingerprint(idlist->keys[i], 481 fingerprint_hash, SSH_FP_DEFAULT); 482 printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]), 483 fp == NULL ? "(null)" : fp, idlist->comments[i], 484 sshkey_type(idlist->keys[i])); 485 free(fp); 486 } else { 487 if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) { 488 fprintf(stderr, "sshkey_write: %s\n", 489 ssh_err(r)); 490 continue; 491 } 492 fprintf(stdout, " %s", idlist->comments[i]); 493 left = sshkey_signatures_left(idlist->keys[i]); 494 if (left > 0) 495 fprintf(stdout, 496 " [signatures left %d]", left); 497 fprintf(stdout, "\n"); 498 } 499 } 500 ssh_free_identitylist(idlist); 501 return 0; 502 } 503 504 static int 505 lock_agent(int agent_fd, int lock) 506 { 507 char prompt[100], *p1, *p2; 508 int r, passok = 1, ret = -1; 509 510 strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); 511 p1 = read_passphrase(prompt, RP_ALLOW_STDIN); 512 if (lock) { 513 strlcpy(prompt, "Again: ", sizeof prompt); 514 p2 = read_passphrase(prompt, RP_ALLOW_STDIN); 515 if (strcmp(p1, p2) != 0) { 516 fprintf(stderr, "Passwords do not match.\n"); 517 passok = 0; 518 } 519 explicit_bzero(p2, strlen(p2)); 520 free(p2); 521 } 522 if (passok) { 523 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { 524 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 525 ret = 0; 526 } else { 527 fprintf(stderr, "Failed to %slock agent: %s\n", 528 lock ? "" : "un", ssh_err(r)); 529 } 530 } 531 explicit_bzero(p1, strlen(p1)); 532 free(p1); 533 return (ret); 534 } 535 536 static int 537 load_resident_keys(int agent_fd, const char *skprovider, int qflag) 538 { 539 struct sshkey **keys; 540 size_t nkeys, i; 541 int r, ok = 0; 542 char *fp; 543 544 pass = read_passphrase("Enter PIN for security key: ", RP_ALLOW_STDIN); 545 if ((r = sshsk_load_resident(skprovider, NULL, pass, 546 &keys, &nkeys)) != 0) { 547 error("Unable to load resident keys: %s", ssh_err(r)); 548 return r; 549 } 550 for (i = 0; i < nkeys; i++) { 551 if ((fp = sshkey_fingerprint(keys[i], 552 fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 553 fatal("%s: sshkey_fingerprint failed", __func__); 554 if ((r = ssh_add_identity_constrained(agent_fd, keys[i], "", 555 lifetime, confirm, maxsign, skprovider)) != 0) { 556 error("Unable to add key %s %s", 557 sshkey_type(keys[i]), fp); 558 free(fp); 559 ok = r; 560 continue; 561 } 562 if (ok == 0) 563 ok = 1; 564 if (!qflag) { 565 fprintf(stderr, "Resident identity added: %s %s\n", 566 sshkey_type(keys[i]), fp); 567 if (lifetime != 0) { 568 fprintf(stderr, 569 "Lifetime set to %d seconds\n", lifetime); 570 } 571 if (confirm != 0) { 572 fprintf(stderr, "The user must confirm " 573 "each use of the key\n"); 574 } 575 } 576 free(fp); 577 sshkey_free(keys[i]); 578 } 579 free(keys); 580 if (nkeys == 0) 581 return SSH_ERR_KEY_NOT_FOUND; 582 return ok == 1 ? 0 : ok; 583 } 584 585 static int 586 do_file(int agent_fd, int deleting, int key_only, char *file, int qflag, 587 const char *skprovider) 588 { 589 if (deleting) { 590 if (delete_file(agent_fd, file, key_only, qflag) == -1) 591 return -1; 592 } else { 593 if (add_file(agent_fd, file, key_only, qflag, skprovider) == -1) 594 return -1; 595 } 596 return 0; 597 } 598 599 static void 600 usage(void) 601 { 602 fprintf(stderr, 603 "usage: ssh-add [-cDdKkLlqvXx] [-E fingerprint_hash] [-S provider] [-t life]\n" 604 #ifdef WITH_XMSS 605 " [-M maxsign] [-m minleft]\n" 606 #endif 607 " [file ...]\n" 608 " ssh-add -s pkcs11\n" 609 " ssh-add -e pkcs11\n" 610 " ssh-add -T pubkey ...\n" 611 ); 612 } 613 614 int 615 main(int argc, char **argv) 616 { 617 extern char *optarg; 618 extern int optind; 619 int agent_fd; 620 char *pkcs11provider = NULL, *skprovider = NULL; 621 int r, i, ch, deleting = 0, ret = 0, key_only = 0, do_download = 0; 622 int xflag = 0, lflag = 0, Dflag = 0, qflag = 0, Tflag = 0; 623 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; 624 LogLevel log_level = SYSLOG_LEVEL_INFO; 625 626 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 627 sanitise_stdfd(); 628 629 #ifdef WITH_OPENSSL 630 OpenSSL_add_all_algorithms(); 631 #endif 632 log_init(__progname, log_level, log_facility, 1); 633 634 setvbuf(stdout, NULL, _IOLBF, 0); 635 636 /* First, get a connection to the authentication agent. */ 637 switch (r = ssh_get_authentication_socket(&agent_fd)) { 638 case 0: 639 break; 640 case SSH_ERR_AGENT_NOT_PRESENT: 641 fprintf(stderr, "Could not open a connection to your " 642 "authentication agent.\n"); 643 exit(2); 644 default: 645 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); 646 exit(2); 647 } 648 649 skprovider = getenv("SSH_SK_PROVIDER"); 650 651 while ((ch = getopt(argc, argv, "vkKlLcdDTxXE:e:M:m:qs:S:t:")) != -1) { 652 switch (ch) { 653 case 'v': 654 if (log_level == SYSLOG_LEVEL_INFO) 655 log_level = SYSLOG_LEVEL_DEBUG1; 656 else if (log_level < SYSLOG_LEVEL_DEBUG3) 657 log_level++; 658 break; 659 case 'E': 660 fingerprint_hash = ssh_digest_alg_by_name(optarg); 661 if (fingerprint_hash == -1) 662 fatal("Invalid hash algorithm \"%s\"", optarg); 663 break; 664 case 'k': 665 key_only = 1; 666 break; 667 case 'K': 668 do_download = 1; 669 break; 670 case 'l': 671 case 'L': 672 if (lflag != 0) 673 fatal("-%c flag already specified", lflag); 674 lflag = ch; 675 break; 676 case 'x': 677 case 'X': 678 if (xflag != 0) 679 fatal("-%c flag already specified", xflag); 680 xflag = ch; 681 break; 682 case 'c': 683 confirm = 1; 684 break; 685 case 'm': 686 minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL); 687 if (minleft == 0) { 688 usage(); 689 ret = 1; 690 goto done; 691 } 692 break; 693 case 'M': 694 maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL); 695 if (maxsign == 0) { 696 usage(); 697 ret = 1; 698 goto done; 699 } 700 break; 701 case 'd': 702 deleting = 1; 703 break; 704 case 'D': 705 Dflag = 1; 706 break; 707 case 's': 708 pkcs11provider = optarg; 709 break; 710 case 'S': 711 skprovider = optarg; 712 break; 713 case 'e': 714 deleting = 1; 715 pkcs11provider = optarg; 716 break; 717 case 't': 718 if ((lifetime = convtime(optarg)) == -1) { 719 fprintf(stderr, "Invalid lifetime\n"); 720 ret = 1; 721 goto done; 722 } 723 break; 724 case 'q': 725 qflag = 1; 726 break; 727 case 'T': 728 Tflag = 1; 729 break; 730 default: 731 usage(); 732 ret = 1; 733 goto done; 734 } 735 } 736 log_init(__progname, log_level, log_facility, 1); 737 738 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) 739 fatal("Invalid combination of actions"); 740 else if (xflag) { 741 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) 742 ret = 1; 743 goto done; 744 } else if (lflag) { 745 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) 746 ret = 1; 747 goto done; 748 } else if (Dflag) { 749 if (delete_all(agent_fd, qflag) == -1) 750 ret = 1; 751 goto done; 752 } 753 754 if (skprovider == NULL) 755 skprovider = "internal"; 756 757 argc -= optind; 758 argv += optind; 759 if (Tflag) { 760 if (argc <= 0) 761 fatal("no keys to test"); 762 for (r = i = 0; i < argc; i++) 763 r |= test_key(agent_fd, argv[i]); 764 ret = r == 0 ? 0 : 1; 765 goto done; 766 } 767 if (pkcs11provider != NULL) { 768 if (update_card(agent_fd, !deleting, pkcs11provider, 769 qflag) == -1) 770 ret = 1; 771 goto done; 772 } 773 if (do_download) { 774 if (skprovider == NULL) 775 fatal("Cannot download keys without provider"); 776 if (load_resident_keys(agent_fd, skprovider, qflag) != 0) 777 ret = 1; 778 goto done; 779 } 780 if (argc == 0) { 781 char buf[PATH_MAX]; 782 struct passwd *pw; 783 struct stat st; 784 int count = 0; 785 786 if ((pw = getpwuid(getuid())) == NULL) { 787 fprintf(stderr, "No user found with uid %u\n", 788 (u_int)getuid()); 789 ret = 1; 790 goto done; 791 } 792 793 for (i = 0; default_files[i]; i++) { 794 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, 795 default_files[i]); 796 if (stat(buf, &st) == -1) 797 continue; 798 if (do_file(agent_fd, deleting, key_only, buf, 799 qflag, skprovider) == -1) 800 ret = 1; 801 else 802 count++; 803 } 804 if (count == 0) 805 ret = 1; 806 } else { 807 for (i = 0; i < argc; i++) { 808 if (do_file(agent_fd, deleting, key_only, 809 argv[i], qflag, skprovider) == -1) 810 ret = 1; 811 } 812 } 813 done: 814 clear_pass(); 815 ssh_close_authentication_socket(agent_fd); 816 return ret; 817 } 818