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