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