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