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