1296633Sdes/* $OpenBSD: ssh-add.c,v 1.128 2016/02/15 09:47:49 dtucker Exp $ */ 257429Smarkm/* 357429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi> 457429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 557429Smarkm * All rights reserved 657429Smarkm * Adds an identity to the authentication server, or removes an identity. 765668Skris * 865668Skris * As far as I am concerned, the code I have written for this software 965668Skris * can be used freely for any purpose. Any derived versions of this 1065668Skris * software must be clearly marked as such, and if the derived work is 1165668Skris * incompatible with the protocol description in the RFC file, it must be 1265668Skris * called by a name other than "ssh" or "Secure Shell". 1365668Skris * 1465668Skris * SSH2 implementation, 1592559Sdes * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 1665668Skris * 1765668Skris * Redistribution and use in source and binary forms, with or without 1865668Skris * modification, are permitted provided that the following conditions 1965668Skris * are met: 2065668Skris * 1. Redistributions of source code must retain the above copyright 2165668Skris * notice, this list of conditions and the following disclaimer. 2265668Skris * 2. Redistributions in binary form must reproduce the above copyright 2365668Skris * notice, this list of conditions and the following disclaimer in the 2465668Skris * documentation and/or other materials provided with the distribution. 2565668Skris * 2665668Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2765668Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2865668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2965668Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 3065668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 3165668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3265668Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3365668Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3465668Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3565668Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3657429Smarkm */ 3757429Smarkm 3857429Smarkm#include "includes.h" 3957429Smarkm 40162856Sdes#include <sys/types.h> 41162856Sdes#include <sys/stat.h> 42162856Sdes 4365668Skris#include <openssl/evp.h> 44181111Sdes#include "openbsd-compat/openssl-compat.h" 4560573Skris 46294332Sdes#include <errno.h> 47162856Sdes#include <fcntl.h> 48162856Sdes#include <pwd.h> 49162856Sdes#include <stdarg.h> 50162856Sdes#include <stdio.h> 51162856Sdes#include <stdlib.h> 52162856Sdes#include <string.h> 53162856Sdes#include <unistd.h> 54294332Sdes#include <limits.h> 55162856Sdes 56162856Sdes#include "xmalloc.h" 5776262Sgreen#include "ssh.h" 5857429Smarkm#include "rsa.h" 5976262Sgreen#include "log.h" 60294332Sdes#include "sshkey.h" 61294332Sdes#include "sshbuf.h" 6257429Smarkm#include "authfd.h" 6360573Skris#include "authfile.h" 6476262Sgreen#include "pathnames.h" 6598684Sdes#include "misc.h" 66294328Sdes#include "ssherr.h" 67294332Sdes#include "digest.h" 6857429Smarkm 6992559Sdes/* argv0 */ 7092559Sdesextern char *__progname; 7192559Sdes 7292559Sdes/* Default files to add */ 7392559Sdesstatic char *default_files[] = { 74294332Sdes#ifdef WITH_OPENSSL 7592559Sdes _PATH_SSH_CLIENT_ID_RSA, 7692559Sdes _PATH_SSH_CLIENT_ID_DSA, 77221420Sdes#ifdef OPENSSL_HAS_ECC 78221420Sdes _PATH_SSH_CLIENT_ID_ECDSA, 79221420Sdes#endif 80294332Sdes#endif /* WITH_OPENSSL */ 81261320Sdes _PATH_SSH_CLIENT_ID_ED25519, 82294336Sdes#ifdef WITH_SSH1 8398684Sdes _PATH_SSH_CLIENT_IDENTITY, 84294336Sdes#endif 8592559Sdes NULL 8692559Sdes}; 8792559Sdes 88294332Sdesstatic int fingerprint_hash = SSH_FP_HASH_DEFAULT; 89294332Sdes 9098684Sdes/* Default lifetime (0 == forever) */ 9198684Sdesstatic int lifetime = 0; 9292559Sdes 93113911Sdes/* User has to confirm key use */ 94113911Sdesstatic int confirm = 0; 95113911Sdes 96296633Sdes/* we keep a cache of one passphrase */ 9776262Sgreenstatic char *pass = NULL; 9892559Sdesstatic void 9976262Sgreenclear_pass(void) 10076262Sgreen{ 10176262Sgreen if (pass) { 102263712Sdes explicit_bzero(pass, strlen(pass)); 103255767Sdes free(pass); 10476262Sgreen pass = NULL; 10576262Sgreen } 10676262Sgreen} 10776262Sgreen 10892559Sdesstatic int 109294332Sdesdelete_file(int agent_fd, const char *filename, int key_only) 11057429Smarkm{ 111294332Sdes struct sshkey *public, *cert = NULL; 112248619Sdes char *certpath = NULL, *comment = NULL; 113294332Sdes int r, ret = -1; 11457429Smarkm 115294332Sdes if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { 116294332Sdes printf("Bad key file %s: %s\n", filename, ssh_err(r)); 11792559Sdes return -1; 11857429Smarkm } 119294332Sdes if ((r = ssh_remove_identity(agent_fd, public)) == 0) { 12057429Smarkm fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); 12192559Sdes ret = 0; 12292559Sdes } else 123294332Sdes fprintf(stderr, "Could not remove identity \"%s\": %s\n", 124294332Sdes filename, ssh_err(r)); 12592559Sdes 126248619Sdes if (key_only) 127248619Sdes goto out; 12892559Sdes 129248619Sdes /* Now try to delete the corresponding certificate too */ 130248619Sdes free(comment); 131248619Sdes comment = NULL; 132248619Sdes xasprintf(&certpath, "%s-cert.pub", filename); 133294332Sdes if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { 134294332Sdes if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 135294332Sdes error("Failed to load certificate \"%s\": %s", 136294332Sdes certpath, ssh_err(r)); 137248619Sdes goto out; 138294332Sdes } 139294332Sdes 140294332Sdes if (!sshkey_equal_public(cert, public)) 141248619Sdes fatal("Certificate %s does not match private key %s", 142248619Sdes certpath, filename); 143248619Sdes 144294332Sdes if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { 145248619Sdes fprintf(stderr, "Identity removed: %s (%s)\n", certpath, 146248619Sdes comment); 147248619Sdes ret = 0; 148248619Sdes } else 149294332Sdes fprintf(stderr, "Could not remove identity \"%s\": %s\n", 150294332Sdes certpath, ssh_err(r)); 151248619Sdes 152248619Sdes out: 153296633Sdes sshkey_free(cert); 154296633Sdes sshkey_free(public); 155248619Sdes free(certpath); 156248619Sdes free(comment); 157248619Sdes 15892559Sdes return ret; 15957429Smarkm} 16057429Smarkm 16165668Skris/* Send a request to remove all identities. */ 16292559Sdesstatic int 163294332Sdesdelete_all(int agent_fd) 16457429Smarkm{ 16592559Sdes int ret = -1; 16665668Skris 167294336Sdes if (ssh_remove_all_identities(agent_fd, 2) == 0) 16892559Sdes ret = 0; 169294336Sdes /* ignore error-code for ssh1 */ 170294336Sdes ssh_remove_all_identities(agent_fd, 1); 17165668Skris 17292559Sdes if (ret == 0) 17357429Smarkm fprintf(stderr, "All identities removed.\n"); 17457429Smarkm else 17576262Sgreen fprintf(stderr, "Failed to remove all identities.\n"); 17692559Sdes 17792559Sdes return ret; 17857429Smarkm} 17957429Smarkm 18092559Sdesstatic int 181294332Sdesadd_file(int agent_fd, const char *filename, int key_only) 18257429Smarkm{ 183294332Sdes struct sshkey *private, *cert; 18476262Sgreen char *comment = NULL; 185240075Sdes char msg[1024], *certpath = NULL; 186294332Sdes int r, fd, ret = -1; 187294332Sdes struct sshbuf *keyblob; 18857429Smarkm 189226046Sdes if (strcmp(filename, "-") == 0) { 190226046Sdes fd = STDIN_FILENO; 191226046Sdes filename = "(stdin)"; 192226046Sdes } else if ((fd = open(filename, O_RDONLY)) < 0) { 19365668Skris perror(filename); 19492559Sdes return -1; 19565668Skris } 196162856Sdes 197162856Sdes /* 198162856Sdes * Since we'll try to load a keyfile multiple times, permission errors 199162856Sdes * will occur multiple times, so check perms first and bail if wrong. 200162856Sdes */ 201226046Sdes if (fd != STDIN_FILENO) { 202294332Sdes if (sshkey_perm_ok(fd, filename) != 0) { 203226046Sdes close(fd); 204226046Sdes return -1; 205226046Sdes } 206226046Sdes } 207294332Sdes if ((keyblob = sshbuf_new()) == NULL) 208294332Sdes fatal("%s: sshbuf_new failed", __func__); 209294332Sdes if ((r = sshkey_load_file(fd, keyblob)) != 0) { 210294332Sdes fprintf(stderr, "Error loading key \"%s\": %s\n", 211294332Sdes filename, ssh_err(r)); 212294332Sdes sshbuf_free(keyblob); 213226046Sdes close(fd); 214226046Sdes return -1; 215226046Sdes } 216162856Sdes close(fd); 217162856Sdes 21857429Smarkm /* At first, try empty passphrase */ 219296633Sdes if ((r = sshkey_parse_private_fileblob(keyblob, "", &private, 220296633Sdes &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 221294332Sdes fprintf(stderr, "Error loading key \"%s\": %s\n", 222294332Sdes filename, ssh_err(r)); 223294332Sdes goto fail_load; 224294332Sdes } 225294328Sdes /* try last */ 226294328Sdes if (private == NULL && pass != NULL) { 227296633Sdes if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private, 228296633Sdes &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 229294332Sdes fprintf(stderr, "Error loading key \"%s\": %s\n", 230294332Sdes filename, ssh_err(r)); 231294332Sdes goto fail_load; 232294332Sdes } 233294328Sdes } 23476262Sgreen if (private == NULL) { 23576262Sgreen /* clear passphrase since it did not work */ 23676262Sgreen clear_pass(); 237296633Sdes snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ", 238296633Sdes filename, confirm ? " (will confirm each use)" : ""); 23957429Smarkm for (;;) { 24092559Sdes pass = read_passphrase(msg, RP_ALLOW_STDIN); 241294332Sdes if (strcmp(pass, "") == 0) 242294332Sdes goto fail_load; 243294332Sdes if ((r = sshkey_parse_private_fileblob(keyblob, pass, 244296633Sdes &private, &comment)) == 0) 245294332Sdes break; 246294332Sdes else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 247294332Sdes fprintf(stderr, 248294332Sdes "Error loading key \"%s\": %s\n", 249294332Sdes filename, ssh_err(r)); 250294332Sdes fail_load: 25176262Sgreen clear_pass(); 252294332Sdes sshbuf_free(keyblob); 25392559Sdes return -1; 25457429Smarkm } 25576262Sgreen clear_pass(); 256124211Sdes snprintf(msg, sizeof msg, 257296633Sdes "Bad passphrase, try again for %s%s: ", filename, 258294332Sdes confirm ? " (will confirm each use)" : ""); 25957429Smarkm } 26057429Smarkm } 261296633Sdes if (comment == NULL || *comment == '\0') 262296633Sdes comment = xstrdup(filename); 263294332Sdes sshbuf_free(keyblob); 26498684Sdes 265294332Sdes if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 266294332Sdes lifetime, confirm)) == 0) { 26776262Sgreen fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); 26892559Sdes ret = 0; 26998684Sdes if (lifetime != 0) 270113911Sdes fprintf(stderr, 27198684Sdes "Lifetime set to %d seconds\n", lifetime); 272126277Sdes if (confirm != 0) 273113911Sdes fprintf(stderr, 274215116Sdes "The user must confirm each use of the key\n"); 27598684Sdes } else { 276294332Sdes fprintf(stderr, "Could not add identity \"%s\": %s\n", 277294332Sdes filename, ssh_err(r)); 27898684Sdes } 27992559Sdes 280240075Sdes /* Skip trying to load the cert if requested */ 281240075Sdes if (key_only) 282240075Sdes goto out; 283204917Sdes 284204917Sdes /* Now try to add the certificate flavour too */ 285204917Sdes xasprintf(&certpath, "%s-cert.pub", filename); 286294332Sdes if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { 287294332Sdes if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 288294332Sdes error("Failed to load certificate \"%s\": %s", 289294332Sdes certpath, ssh_err(r)); 290215116Sdes goto out; 291294332Sdes } 292215116Sdes 293294332Sdes if (!sshkey_equal_public(cert, private)) { 294215116Sdes error("Certificate %s does not match private key %s", 295215116Sdes certpath, filename); 296294332Sdes sshkey_free(cert); 297215116Sdes goto out; 298215116Sdes } 299204917Sdes 300215116Sdes /* Graft with private bits */ 301294464Sdes if ((r = sshkey_to_certified(private)) != 0) { 302294332Sdes error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); 303294332Sdes sshkey_free(cert); 304215116Sdes goto out; 305204917Sdes } 306294332Sdes if ((r = sshkey_cert_copy(cert, private)) != 0) { 307294332Sdes error("%s: key_cert_copy: %s", __func__, ssh_err(r)); 308294332Sdes sshkey_free(cert); 309294332Sdes goto out; 310294332Sdes } 311294332Sdes sshkey_free(cert); 312204917Sdes 313294332Sdes if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 314294332Sdes lifetime, confirm)) != 0) { 315294332Sdes error("Certificate %s (%s) add failed: %s", certpath, 316294332Sdes private->cert->key_id, ssh_err(r)); 317294332Sdes goto out; 318215116Sdes } 319215116Sdes fprintf(stderr, "Certificate added: %s (%s)\n", certpath, 320215116Sdes private->cert->key_id); 321215116Sdes if (lifetime != 0) 322215116Sdes fprintf(stderr, "Lifetime set to %d seconds\n", lifetime); 323215116Sdes if (confirm != 0) 324215116Sdes fprintf(stderr, "The user must confirm each use of the key\n"); 325215116Sdes out: 326294332Sdes free(certpath); 327255767Sdes free(comment); 328294332Sdes sshkey_free(private); 32992559Sdes 33092559Sdes return ret; 33157429Smarkm} 33257429Smarkm 33392559Sdesstatic int 334294332Sdesupdate_card(int agent_fd, int add, const char *id) 33592559Sdes{ 336261320Sdes char *pin = NULL; 337294332Sdes int r, ret = -1; 33898684Sdes 339261320Sdes if (add) { 340261320Sdes if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", 341261320Sdes RP_ALLOW_STDIN)) == NULL) 342261320Sdes return -1; 343261320Sdes } 34498684Sdes 345294332Sdes if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, 346294332Sdes lifetime, confirm)) == 0) { 34792559Sdes fprintf(stderr, "Card %s: %s\n", 34892559Sdes add ? "added" : "removed", id); 349113911Sdes ret = 0; 35092559Sdes } else { 351294332Sdes fprintf(stderr, "Could not %s card \"%s\": %s\n", 352294332Sdes add ? "add" : "remove", id, ssh_err(r)); 353113911Sdes ret = -1; 35492559Sdes } 355255767Sdes free(pin); 356113911Sdes return ret; 35792559Sdes} 35892559Sdes 35992559Sdesstatic int 360294332Sdeslist_identities(int agent_fd, int do_fp) 36157429Smarkm{ 362294332Sdes char *fp; 363294336Sdes int r, had_identities = 0; 364294332Sdes struct ssh_identitylist *idlist; 365294332Sdes size_t i; 366294336Sdes#ifdef WITH_SSH1 367294336Sdes int version = 1; 368294336Sdes#else 369294336Sdes int version = 2; 370294336Sdes#endif 37157429Smarkm 372294336Sdes for (; version <= 2; version++) { 373294332Sdes if ((r = ssh_fetch_identitylist(agent_fd, version, 374294332Sdes &idlist)) != 0) { 375294332Sdes if (r != SSH_ERR_AGENT_NO_IDENTITIES) 376294332Sdes fprintf(stderr, "error fetching identities for " 377294332Sdes "protocol %d: %s\n", version, ssh_err(r)); 378294332Sdes continue; 379294332Sdes } 380294332Sdes for (i = 0; i < idlist->nkeys; i++) { 38165668Skris had_identities = 1; 38276262Sgreen if (do_fp) { 383294332Sdes fp = sshkey_fingerprint(idlist->keys[i], 384294332Sdes fingerprint_hash, SSH_FP_DEFAULT); 385296633Sdes printf("%u %s %s (%s)\n", 386294332Sdes sshkey_size(idlist->keys[i]), 387294332Sdes fp == NULL ? "(null)" : fp, 388294332Sdes idlist->comments[i], 389294332Sdes sshkey_type(idlist->keys[i])); 390255767Sdes free(fp); 39157429Smarkm } else { 392294332Sdes if ((r = sshkey_write(idlist->keys[i], 393294332Sdes stdout)) != 0) { 394294332Sdes fprintf(stderr, "sshkey_write: %s\n", 395294332Sdes ssh_err(r)); 396294332Sdes continue; 397294332Sdes } 398294332Sdes fprintf(stdout, " %s\n", idlist->comments[i]); 39957429Smarkm } 40057429Smarkm } 401294332Sdes ssh_free_identitylist(idlist); 40257429Smarkm } 40392559Sdes if (!had_identities) { 40457429Smarkm printf("The agent has no identities.\n"); 40592559Sdes return -1; 40692559Sdes } 40792559Sdes return 0; 40857429Smarkm} 40957429Smarkm 41092559Sdesstatic int 411294332Sdeslock_agent(int agent_fd, int lock) 41298684Sdes{ 41398684Sdes char prompt[100], *p1, *p2; 414294332Sdes int r, passok = 1, ret = -1; 41598684Sdes 41698684Sdes strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); 41798684Sdes p1 = read_passphrase(prompt, RP_ALLOW_STDIN); 41898684Sdes if (lock) { 41998684Sdes strlcpy(prompt, "Again: ", sizeof prompt); 42098684Sdes p2 = read_passphrase(prompt, RP_ALLOW_STDIN); 42198684Sdes if (strcmp(p1, p2) != 0) { 42298684Sdes fprintf(stderr, "Passwords do not match.\n"); 42398684Sdes passok = 0; 42498684Sdes } 425263712Sdes explicit_bzero(p2, strlen(p2)); 426255767Sdes free(p2); 42798684Sdes } 428294332Sdes if (passok) { 429294332Sdes if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { 430294332Sdes fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 431294332Sdes ret = 0; 432294332Sdes } else { 433294332Sdes fprintf(stderr, "Failed to %slock agent: %s\n", 434294332Sdes lock ? "" : "un", ssh_err(r)); 435294332Sdes } 436294332Sdes } 437263712Sdes explicit_bzero(p1, strlen(p1)); 438255767Sdes free(p1); 439106130Sdes return (ret); 44098684Sdes} 44198684Sdes 44298684Sdesstatic int 443294332Sdesdo_file(int agent_fd, int deleting, int key_only, char *file) 44492559Sdes{ 44592559Sdes if (deleting) { 446294332Sdes if (delete_file(agent_fd, file, key_only) == -1) 44792559Sdes return -1; 44892559Sdes } else { 449294332Sdes if (add_file(agent_fd, file, key_only) == -1) 45092559Sdes return -1; 45192559Sdes } 45292559Sdes return 0; 45392559Sdes} 45492559Sdes 45592559Sdesstatic void 45692559Sdesusage(void) 45792559Sdes{ 458181111Sdes fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); 45992559Sdes fprintf(stderr, "Options:\n"); 46092559Sdes fprintf(stderr, " -l List fingerprints of all identities.\n"); 461294332Sdes fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n"); 46292559Sdes fprintf(stderr, " -L List public key parameters of all identities.\n"); 463240075Sdes fprintf(stderr, " -k Load only keys and not certificates.\n"); 464240075Sdes fprintf(stderr, " -c Require confirmation to sign using identities\n"); 465240075Sdes fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); 46692559Sdes fprintf(stderr, " -d Delete identity.\n"); 46792559Sdes fprintf(stderr, " -D Delete all identities.\n"); 46898684Sdes fprintf(stderr, " -x Lock agent.\n"); 469106130Sdes fprintf(stderr, " -X Unlock agent.\n"); 470204917Sdes fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); 471204917Sdes fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); 47292559Sdes} 47392559Sdes 47457429Smarkmint 47557429Smarkmmain(int argc, char **argv) 47657429Smarkm{ 47792559Sdes extern char *optarg; 47892559Sdes extern int optind; 479294332Sdes int agent_fd; 480204917Sdes char *pkcs11provider = NULL; 481294332Sdes int r, i, ch, deleting = 0, ret = 0, key_only = 0; 482294332Sdes int xflag = 0, lflag = 0, Dflag = 0; 48357429Smarkm 484296633Sdes ssh_malloc_init(); /* must be called before any mallocs */ 485157019Sdes /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 486157019Sdes sanitise_stdfd(); 487157019Sdes 488124211Sdes __progname = ssh_get_progname(argv[0]); 48998941Sdes seed_rng(); 49098941Sdes 491294332Sdes#ifdef WITH_OPENSSL 492221420Sdes OpenSSL_add_all_algorithms(); 493294332Sdes#endif 49457429Smarkm 495294332Sdes setvbuf(stdout, NULL, _IOLBF, 0); 496294328Sdes 497294332Sdes /* First, get a connection to the authentication agent. */ 498294332Sdes switch (r = ssh_get_authentication_socket(&agent_fd)) { 499294332Sdes case 0: 500294332Sdes break; 501294332Sdes case SSH_ERR_AGENT_NOT_PRESENT: 502294332Sdes fprintf(stderr, "Could not open a connection to your " 503294332Sdes "authentication agent.\n"); 50492559Sdes exit(2); 505294332Sdes default: 506294332Sdes fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); 507294332Sdes exit(2); 50857429Smarkm } 509294332Sdes 510294332Sdes while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) { 51192559Sdes switch (ch) { 512294332Sdes case 'E': 513294332Sdes fingerprint_hash = ssh_digest_alg_by_name(optarg); 514294332Sdes if (fingerprint_hash == -1) 515294332Sdes fatal("Invalid hash algorithm \"%s\"", optarg); 516294332Sdes break; 517240075Sdes case 'k': 518240075Sdes key_only = 1; 519240075Sdes break; 52092559Sdes case 'l': 52192559Sdes case 'L': 522294332Sdes if (lflag != 0) 523294332Sdes fatal("-%c flag already specified", lflag); 524294332Sdes lflag = ch; 525294332Sdes break; 52698684Sdes case 'x': 52798684Sdes case 'X': 528294332Sdes if (xflag != 0) 529294332Sdes fatal("-%c flag already specified", xflag); 530294332Sdes xflag = ch; 531294332Sdes break; 532113911Sdes case 'c': 533113911Sdes confirm = 1; 534113911Sdes break; 53592559Sdes case 'd': 53657429Smarkm deleting = 1; 53792559Sdes break; 53892559Sdes case 'D': 539294332Sdes Dflag = 1; 540294332Sdes break; 54192559Sdes case 's': 542204917Sdes pkcs11provider = optarg; 54392559Sdes break; 54492559Sdes case 'e': 54592559Sdes deleting = 1; 546204917Sdes pkcs11provider = optarg; 54792559Sdes break; 54898684Sdes case 't': 54998684Sdes if ((lifetime = convtime(optarg)) == -1) { 55098684Sdes fprintf(stderr, "Invalid lifetime\n"); 55198684Sdes ret = 1; 55298684Sdes goto done; 55398684Sdes } 55498684Sdes break; 55592559Sdes default: 55692559Sdes usage(); 55792559Sdes ret = 1; 55892559Sdes goto done; 55957429Smarkm } 56057429Smarkm } 561294332Sdes 562294332Sdes if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) 563294332Sdes fatal("Invalid combination of actions"); 564294332Sdes else if (xflag) { 565294332Sdes if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) 566294332Sdes ret = 1; 567294332Sdes goto done; 568294332Sdes } else if (lflag) { 569294332Sdes if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) 570294332Sdes ret = 1; 571294332Sdes goto done; 572294332Sdes } else if (Dflag) { 573294332Sdes if (delete_all(agent_fd) == -1) 574294332Sdes ret = 1; 575294332Sdes goto done; 576294332Sdes } 577294332Sdes 57892559Sdes argc -= optind; 57992559Sdes argv += optind; 580204917Sdes if (pkcs11provider != NULL) { 581294332Sdes if (update_card(agent_fd, !deleting, pkcs11provider) == -1) 58292559Sdes ret = 1; 58392559Sdes goto done; 58492559Sdes } 58592559Sdes if (argc == 0) { 586294332Sdes char buf[PATH_MAX]; 58792559Sdes struct passwd *pw; 58898684Sdes struct stat st; 58998684Sdes int count = 0; 59092559Sdes 59192559Sdes if ((pw = getpwuid(getuid())) == NULL) { 59265668Skris fprintf(stderr, "No user found with uid %u\n", 59365668Skris (u_int)getuid()); 59492559Sdes ret = 1; 59592559Sdes goto done; 59657429Smarkm } 59792559Sdes 598147005Sdes for (i = 0; default_files[i]; i++) { 59998684Sdes snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, 60092559Sdes default_files[i]); 60198684Sdes if (stat(buf, &st) < 0) 60298684Sdes continue; 603294332Sdes if (do_file(agent_fd, deleting, key_only, buf) == -1) 60492559Sdes ret = 1; 60598684Sdes else 60698684Sdes count++; 60792559Sdes } 60898684Sdes if (count == 0) 60998684Sdes ret = 1; 61092559Sdes } else { 611147005Sdes for (i = 0; i < argc; i++) { 612294332Sdes if (do_file(agent_fd, deleting, key_only, 613294332Sdes argv[i]) == -1) 61492559Sdes ret = 1; 61592559Sdes } 61657429Smarkm } 61776262Sgreen clear_pass(); 61892559Sdes 61992559Sdesdone: 620294332Sdes ssh_close_authentication_socket(agent_fd); 62192559Sdes return ret; 62257429Smarkm} 623