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