1 /* $OpenBSD: ssh-add.c,v 1.138 2019/01/21 12:53:35 djm 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 "includes.h" 39 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 43 #include <openssl/evp.h> 44 #include "openbsd-compat/openssl-compat.h" 45 46 #include <errno.h> 47 #include <fcntl.h> 48 #include <pwd.h> 49 #include <stdarg.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <limits.h> 55 56 #include "xmalloc.h" 57 #include "ssh.h" 58 #include "log.h" 59 #include "sshkey.h" 60 #include "sshbuf.h" 61 #include "authfd.h" 62 #include "authfile.h" 63 #include "pathnames.h" 64 #include "misc.h" 65 #include "ssherr.h" 66 #include "digest.h" 67 68 /* argv0 */ 69 extern char *__progname; 70 71 /* Default files to add */ 72 static char *default_files[] = { 73 #ifdef WITH_OPENSSL 74 _PATH_SSH_CLIENT_ID_RSA, 75 _PATH_SSH_CLIENT_ID_DSA, 76 #ifdef OPENSSL_HAS_ECC 77 _PATH_SSH_CLIENT_ID_ECDSA, 78 #endif 79 #endif /* WITH_OPENSSL */ 80 _PATH_SSH_CLIENT_ID_ED25519, 81 _PATH_SSH_CLIENT_ID_XMSS, 82 NULL 83 }; 84 85 static int fingerprint_hash = SSH_FP_HASH_DEFAULT; 86 87 /* Default lifetime (0 == forever) */ 88 static int lifetime = 0; 89 90 /* User has to confirm key use */ 91 static int confirm = 0; 92 93 /* Maximum number of signatures (XMSS) */ 94 static u_int maxsign = 0; 95 static u_int minleft = 0; 96 97 /* we keep a cache of one passphrase */ 98 static char *pass = NULL; 99 static void 100 clear_pass(void) 101 { 102 if (pass) { 103 explicit_bzero(pass, strlen(pass)); 104 free(pass); 105 pass = NULL; 106 } 107 } 108 109 static int 110 delete_file(int agent_fd, const char *filename, int key_only, int qflag) 111 { 112 struct sshkey *public, *cert = NULL; 113 char *certpath = NULL, *comment = NULL; 114 int r, ret = -1; 115 116 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { 117 printf("Bad key file %s: %s\n", filename, ssh_err(r)); 118 return -1; 119 } 120 if ((r = ssh_remove_identity(agent_fd, public)) == 0) { 121 if (!qflag) { 122 fprintf(stderr, "Identity removed: %s (%s)\n", 123 filename, comment); 124 } 125 ret = 0; 126 } else 127 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 128 filename, ssh_err(r)); 129 130 if (key_only) 131 goto out; 132 133 /* Now try to delete the corresponding certificate too */ 134 free(comment); 135 comment = NULL; 136 xasprintf(&certpath, "%s-cert.pub", filename); 137 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { 138 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 139 error("Failed to load certificate \"%s\": %s", 140 certpath, ssh_err(r)); 141 goto out; 142 } 143 144 if (!sshkey_equal_public(cert, public)) 145 fatal("Certificate %s does not match private key %s", 146 certpath, filename); 147 148 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { 149 if (!qflag) { 150 fprintf(stderr, "Identity removed: %s (%s)\n", 151 certpath, comment); 152 } 153 ret = 0; 154 } else 155 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 156 certpath, ssh_err(r)); 157 158 out: 159 sshkey_free(cert); 160 sshkey_free(public); 161 free(certpath); 162 free(comment); 163 164 return ret; 165 } 166 167 /* Send a request to remove all identities. */ 168 static int 169 delete_all(int agent_fd, int qflag) 170 { 171 int ret = -1; 172 173 /* 174 * Since the agent might be forwarded, old or non-OpenSSH, when asked 175 * to remove all keys, attempt to remove both protocol v.1 and v.2 176 * keys. 177 */ 178 if (ssh_remove_all_identities(agent_fd, 2) == 0) 179 ret = 0; 180 /* ignore error-code for ssh1 */ 181 ssh_remove_all_identities(agent_fd, 1); 182 183 if (ret != 0) 184 fprintf(stderr, "Failed to remove all identities.\n"); 185 else if (!qflag) 186 fprintf(stderr, "All identities removed.\n"); 187 188 return ret; 189 } 190 191 static int 192 add_file(int agent_fd, const char *filename, int key_only, int qflag) 193 { 194 struct sshkey *private, *cert; 195 char *comment = NULL; 196 char msg[1024], *certpath = NULL; 197 int r, fd, ret = -1; 198 size_t i; 199 u_int32_t left; 200 struct sshbuf *keyblob; 201 struct ssh_identitylist *idlist; 202 203 if (strcmp(filename, "-") == 0) { 204 fd = STDIN_FILENO; 205 filename = "(stdin)"; 206 } else if ((fd = open(filename, O_RDONLY)) < 0) { 207 perror(filename); 208 return -1; 209 } 210 211 /* 212 * Since we'll try to load a keyfile multiple times, permission errors 213 * will occur multiple times, so check perms first and bail if wrong. 214 */ 215 if (fd != STDIN_FILENO) { 216 if (sshkey_perm_ok(fd, filename) != 0) { 217 close(fd); 218 return -1; 219 } 220 } 221 if ((keyblob = sshbuf_new()) == NULL) 222 fatal("%s: sshbuf_new failed", __func__); 223 if ((r = sshkey_load_file(fd, keyblob)) != 0) { 224 fprintf(stderr, "Error loading key \"%s\": %s\n", 225 filename, ssh_err(r)); 226 sshbuf_free(keyblob); 227 close(fd); 228 return -1; 229 } 230 close(fd); 231 232 /* At first, try empty passphrase */ 233 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private, 234 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 235 fprintf(stderr, "Error loading key \"%s\": %s\n", 236 filename, ssh_err(r)); 237 goto fail_load; 238 } 239 /* try last */ 240 if (private == NULL && pass != NULL) { 241 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private, 242 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 243 fprintf(stderr, "Error loading key \"%s\": %s\n", 244 filename, ssh_err(r)); 245 goto fail_load; 246 } 247 } 248 if (private == NULL) { 249 /* clear passphrase since it did not work */ 250 clear_pass(); 251 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ", 252 filename, confirm ? " (will confirm each use)" : ""); 253 for (;;) { 254 pass = read_passphrase(msg, RP_ALLOW_STDIN); 255 if (strcmp(pass, "") == 0) 256 goto fail_load; 257 if ((r = sshkey_parse_private_fileblob(keyblob, pass, 258 &private, &comment)) == 0) 259 break; 260 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 261 fprintf(stderr, 262 "Error loading key \"%s\": %s\n", 263 filename, ssh_err(r)); 264 fail_load: 265 clear_pass(); 266 sshbuf_free(keyblob); 267 return -1; 268 } 269 clear_pass(); 270 snprintf(msg, sizeof msg, 271 "Bad passphrase, try again for %s%s: ", filename, 272 confirm ? " (will confirm each use)" : ""); 273 } 274 } 275 if (comment == NULL || *comment == '\0') 276 comment = xstrdup(filename); 277 sshbuf_free(keyblob); 278 279 /* For XMSS */ 280 if ((r = sshkey_set_filename(private, filename)) != 0) { 281 fprintf(stderr, "Could not add filename to private key: %s (%s)\n", 282 filename, comment); 283 goto out; 284 } 285 if (maxsign && minleft && 286 (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) { 287 for (i = 0; i < idlist->nkeys; i++) { 288 if (!sshkey_equal_public(idlist->keys[i], private)) 289 continue; 290 left = sshkey_signatures_left(idlist->keys[i]); 291 if (left < minleft) { 292 fprintf(stderr, 293 "Only %d signatures left.\n", left); 294 break; 295 } 296 fprintf(stderr, "Skipping update: "); 297 if (left == minleft) { 298 fprintf(stderr, 299 "required signatures left (%d).\n", left); 300 } else { 301 fprintf(stderr, 302 "more signatures left (%d) than" 303 " required (%d).\n", left, minleft); 304 } 305 ssh_free_identitylist(idlist); 306 goto out; 307 } 308 ssh_free_identitylist(idlist); 309 } 310 311 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 312 lifetime, confirm, maxsign)) == 0) { 313 ret = 0; 314 if (!qflag) { 315 fprintf(stderr, "Identity added: %s (%s)\n", 316 filename, comment); 317 if (lifetime != 0) { 318 fprintf(stderr, 319 "Lifetime set to %d seconds\n", lifetime); 320 } 321 if (confirm != 0) { 322 fprintf(stderr, "The user must confirm " 323 "each use of the key\n"); 324 } 325 } 326 } else { 327 fprintf(stderr, "Could not add identity \"%s\": %s\n", 328 filename, ssh_err(r)); 329 } 330 331 /* Skip trying to load the cert if requested */ 332 if (key_only) 333 goto out; 334 335 /* Now try to add the certificate flavour too */ 336 xasprintf(&certpath, "%s-cert.pub", filename); 337 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { 338 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 339 error("Failed to load certificate \"%s\": %s", 340 certpath, ssh_err(r)); 341 goto out; 342 } 343 344 if (!sshkey_equal_public(cert, private)) { 345 error("Certificate %s does not match private key %s", 346 certpath, filename); 347 sshkey_free(cert); 348 goto out; 349 } 350 351 /* Graft with private bits */ 352 if ((r = sshkey_to_certified(private)) != 0) { 353 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); 354 sshkey_free(cert); 355 goto out; 356 } 357 if ((r = sshkey_cert_copy(cert, private)) != 0) { 358 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r)); 359 sshkey_free(cert); 360 goto out; 361 } 362 sshkey_free(cert); 363 364 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 365 lifetime, confirm, maxsign)) != 0) { 366 error("Certificate %s (%s) add failed: %s", certpath, 367 private->cert->key_id, ssh_err(r)); 368 goto out; 369 } 370 /* success */ 371 if (!qflag) { 372 fprintf(stderr, "Certificate added: %s (%s)\n", certpath, 373 private->cert->key_id); 374 if (lifetime != 0) { 375 fprintf(stderr, "Lifetime set to %d seconds\n", 376 lifetime); 377 } 378 if (confirm != 0) { 379 fprintf(stderr, "The user must confirm each use " 380 "of the key\n"); 381 } 382 } 383 384 out: 385 free(certpath); 386 free(comment); 387 sshkey_free(private); 388 389 return ret; 390 } 391 392 static int 393 update_card(int agent_fd, int add, const char *id, int qflag) 394 { 395 char *pin = NULL; 396 int r, ret = -1; 397 398 if (add) { 399 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", 400 RP_ALLOW_STDIN)) == NULL) 401 return -1; 402 } 403 404 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, 405 lifetime, confirm)) == 0) { 406 ret = 0; 407 if (!qflag) { 408 fprintf(stderr, "Card %s: %s\n", 409 add ? "added" : "removed", id); 410 } 411 } else { 412 fprintf(stderr, "Could not %s card \"%s\": %s\n", 413 add ? "add" : "remove", id, ssh_err(r)); 414 ret = -1; 415 } 416 free(pin); 417 return ret; 418 } 419 420 static int 421 test_key(int agent_fd, const char *filename) 422 { 423 struct sshkey *key = NULL; 424 u_char *sig = NULL; 425 size_t slen = 0; 426 int r, ret = -1; 427 char data[1024]; 428 429 if ((r = sshkey_load_public(filename, &key, NULL)) != 0) { 430 error("Couldn't read public key %s: %s", filename, ssh_err(r)); 431 return -1; 432 } 433 arc4random_buf(data, sizeof(data)); 434 if ((r = ssh_agent_sign(agent_fd, key, &sig, &slen, data, sizeof(data), 435 NULL, 0)) != 0) { 436 error("Agent signature failed for %s: %s", 437 filename, ssh_err(r)); 438 goto done; 439 } 440 if ((r = sshkey_verify(key, sig, slen, data, sizeof(data), 441 NULL, 0)) != 0) { 442 error("Signature verification failed for %s: %s", 443 filename, ssh_err(r)); 444 goto done; 445 } 446 /* success */ 447 ret = 0; 448 done: 449 free(sig); 450 sshkey_free(key); 451 return ret; 452 } 453 454 static int 455 list_identities(int agent_fd, int do_fp) 456 { 457 char *fp; 458 int r; 459 struct ssh_identitylist *idlist; 460 u_int32_t left; 461 size_t i; 462 463 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) { 464 if (r != SSH_ERR_AGENT_NO_IDENTITIES) 465 fprintf(stderr, "error fetching identities: %s\n", 466 ssh_err(r)); 467 else 468 printf("The agent has no identities.\n"); 469 return -1; 470 } 471 for (i = 0; i < idlist->nkeys; i++) { 472 if (do_fp) { 473 fp = sshkey_fingerprint(idlist->keys[i], 474 fingerprint_hash, SSH_FP_DEFAULT); 475 printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]), 476 fp == NULL ? "(null)" : fp, idlist->comments[i], 477 sshkey_type(idlist->keys[i])); 478 free(fp); 479 } else { 480 if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) { 481 fprintf(stderr, "sshkey_write: %s\n", 482 ssh_err(r)); 483 continue; 484 } 485 fprintf(stdout, " %s", idlist->comments[i]); 486 left = sshkey_signatures_left(idlist->keys[i]); 487 if (left > 0) 488 fprintf(stdout, 489 " [signatures left %d]", left); 490 fprintf(stdout, "\n"); 491 } 492 } 493 ssh_free_identitylist(idlist); 494 return 0; 495 } 496 497 static int 498 lock_agent(int agent_fd, int lock) 499 { 500 char prompt[100], *p1, *p2; 501 int r, passok = 1, ret = -1; 502 503 strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); 504 p1 = read_passphrase(prompt, RP_ALLOW_STDIN); 505 if (lock) { 506 strlcpy(prompt, "Again: ", sizeof prompt); 507 p2 = read_passphrase(prompt, RP_ALLOW_STDIN); 508 if (strcmp(p1, p2) != 0) { 509 fprintf(stderr, "Passwords do not match.\n"); 510 passok = 0; 511 } 512 explicit_bzero(p2, strlen(p2)); 513 free(p2); 514 } 515 if (passok) { 516 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { 517 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 518 ret = 0; 519 } else { 520 fprintf(stderr, "Failed to %slock agent: %s\n", 521 lock ? "" : "un", ssh_err(r)); 522 } 523 } 524 explicit_bzero(p1, strlen(p1)); 525 free(p1); 526 return (ret); 527 } 528 529 static int 530 do_file(int agent_fd, int deleting, int key_only, char *file, int qflag) 531 { 532 if (deleting) { 533 if (delete_file(agent_fd, file, key_only, qflag) == -1) 534 return -1; 535 } else { 536 if (add_file(agent_fd, file, key_only, qflag) == -1) 537 return -1; 538 } 539 return 0; 540 } 541 542 static void 543 usage(void) 544 { 545 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); 546 fprintf(stderr, "Options:\n"); 547 fprintf(stderr, " -l List fingerprints of all identities.\n"); 548 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n"); 549 fprintf(stderr, " -L List public key parameters of all identities.\n"); 550 fprintf(stderr, " -k Load only keys and not certificates.\n"); 551 fprintf(stderr, " -c Require confirmation to sign using identities\n"); 552 fprintf(stderr, " -m minleft Maxsign is only changed if less than minleft are left (for XMSS)\n"); 553 fprintf(stderr, " -M maxsign Maximum number of signatures allowed (for XMSS)\n"); 554 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); 555 fprintf(stderr, " -d Delete identity.\n"); 556 fprintf(stderr, " -D Delete all identities.\n"); 557 fprintf(stderr, " -x Lock agent.\n"); 558 fprintf(stderr, " -X Unlock agent.\n"); 559 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); 560 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); 561 fprintf(stderr, " -T pubkey Test if ssh-agent can access matching private key.\n"); 562 fprintf(stderr, " -q Be quiet after a successful operation.\n"); 563 fprintf(stderr, " -v Be more verbose.\n"); 564 } 565 566 int 567 main(int argc, char **argv) 568 { 569 extern char *optarg; 570 extern int optind; 571 int agent_fd; 572 char *pkcs11provider = NULL; 573 int r, i, ch, deleting = 0, ret = 0, key_only = 0; 574 int xflag = 0, lflag = 0, Dflag = 0, qflag = 0, Tflag = 0; 575 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; 576 LogLevel log_level = SYSLOG_LEVEL_INFO; 577 578 ssh_malloc_init(); /* must be called before any mallocs */ 579 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 580 sanitise_stdfd(); 581 582 __progname = ssh_get_progname(argv[0]); 583 seed_rng(); 584 585 log_init(__progname, log_level, log_facility, 1); 586 587 setvbuf(stdout, NULL, _IOLBF, 0); 588 589 /* First, get a connection to the authentication agent. */ 590 switch (r = ssh_get_authentication_socket(&agent_fd)) { 591 case 0: 592 break; 593 case SSH_ERR_AGENT_NOT_PRESENT: 594 fprintf(stderr, "Could not open a connection to your " 595 "authentication agent.\n"); 596 exit(2); 597 default: 598 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); 599 exit(2); 600 } 601 602 while ((ch = getopt(argc, argv, "vklLcdDTxXE:e:M:m:qs:t:")) != -1) { 603 switch (ch) { 604 case 'v': 605 if (log_level == SYSLOG_LEVEL_INFO) 606 log_level = SYSLOG_LEVEL_DEBUG1; 607 else if (log_level < SYSLOG_LEVEL_DEBUG3) 608 log_level++; 609 break; 610 case 'E': 611 fingerprint_hash = ssh_digest_alg_by_name(optarg); 612 if (fingerprint_hash == -1) 613 fatal("Invalid hash algorithm \"%s\"", optarg); 614 break; 615 case 'k': 616 key_only = 1; 617 break; 618 case 'l': 619 case 'L': 620 if (lflag != 0) 621 fatal("-%c flag already specified", lflag); 622 lflag = ch; 623 break; 624 case 'x': 625 case 'X': 626 if (xflag != 0) 627 fatal("-%c flag already specified", xflag); 628 xflag = ch; 629 break; 630 case 'c': 631 confirm = 1; 632 break; 633 case 'm': 634 minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL); 635 if (minleft == 0) { 636 usage(); 637 ret = 1; 638 goto done; 639 } 640 break; 641 case 'M': 642 maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL); 643 if (maxsign == 0) { 644 usage(); 645 ret = 1; 646 goto done; 647 } 648 break; 649 case 'd': 650 deleting = 1; 651 break; 652 case 'D': 653 Dflag = 1; 654 break; 655 case 's': 656 pkcs11provider = optarg; 657 break; 658 case 'e': 659 deleting = 1; 660 pkcs11provider = optarg; 661 break; 662 case 't': 663 if ((lifetime = convtime(optarg)) == -1) { 664 fprintf(stderr, "Invalid lifetime\n"); 665 ret = 1; 666 goto done; 667 } 668 break; 669 case 'q': 670 qflag = 1; 671 break; 672 case 'T': 673 Tflag = 1; 674 break; 675 default: 676 usage(); 677 ret = 1; 678 goto done; 679 } 680 } 681 log_init(__progname, log_level, log_facility, 1); 682 683 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) 684 fatal("Invalid combination of actions"); 685 else if (xflag) { 686 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) 687 ret = 1; 688 goto done; 689 } else if (lflag) { 690 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) 691 ret = 1; 692 goto done; 693 } else if (Dflag) { 694 if (delete_all(agent_fd, qflag) == -1) 695 ret = 1; 696 goto done; 697 } 698 699 argc -= optind; 700 argv += optind; 701 if (Tflag) { 702 if (argc <= 0) 703 fatal("no keys to test"); 704 for (r = i = 0; i < argc; i++) 705 r |= test_key(agent_fd, argv[i]); 706 ret = r == 0 ? 0 : 1; 707 goto done; 708 } 709 if (pkcs11provider != NULL) { 710 if (update_card(agent_fd, !deleting, pkcs11provider, 711 qflag) == -1) 712 ret = 1; 713 goto done; 714 } 715 if (argc == 0) { 716 char buf[PATH_MAX]; 717 struct passwd *pw; 718 struct stat st; 719 int count = 0; 720 721 if ((pw = getpwuid(getuid())) == NULL) { 722 fprintf(stderr, "No user found with uid %u\n", 723 (u_int)getuid()); 724 ret = 1; 725 goto done; 726 } 727 728 for (i = 0; default_files[i]; i++) { 729 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, 730 default_files[i]); 731 if (stat(buf, &st) < 0) 732 continue; 733 if (do_file(agent_fd, deleting, key_only, buf, 734 qflag) == -1) 735 ret = 1; 736 else 737 count++; 738 } 739 if (count == 0) 740 ret = 1; 741 } else { 742 for (i = 0; i < argc; i++) { 743 if (do_file(agent_fd, deleting, key_only, 744 argv[i], qflag) == -1) 745 ret = 1; 746 } 747 } 748 clear_pass(); 749 750 done: 751 ssh_close_authentication_socket(agent_fd); 752 return ret; 753 } 754