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