1296853Sdes/* $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 46295367Sdes#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> 54295367Sdes#include <limits.h> 55162856Sdes 56162856Sdes#include "xmalloc.h" 5776262Sgreen#include "ssh.h" 5857429Smarkm#include "rsa.h" 5976262Sgreen#include "log.h" 60295367Sdes#include "sshkey.h" 61295367Sdes#include "sshbuf.h" 6257429Smarkm#include "authfd.h" 6360573Skris#include "authfile.h" 6476262Sgreen#include "pathnames.h" 6598684Sdes#include "misc.h" 66295367Sdes#include "ssherr.h" 67295367Sdes#include "digest.h" 6857429Smarkm 6992559Sdes/* argv0 */ 7092559Sdesextern char *__progname; 7192559Sdes 7292559Sdes/* Default files to add */ 7392559Sdesstatic char *default_files[] = { 74295367Sdes#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 80295367Sdes#endif /* WITH_OPENSSL */ 81262566Sdes _PATH_SSH_CLIENT_ID_ED25519, 82295367Sdes#ifdef WITH_SSH1 8398684Sdes _PATH_SSH_CLIENT_IDENTITY, 84295367Sdes#endif 8592559Sdes NULL 8692559Sdes}; 8792559Sdes 88295367Sdesstatic int fingerprint_hash = SSH_FP_HASH_DEFAULT; 89295367Sdes 9098684Sdes/* Default lifetime (0 == forever) */ 9198684Sdesstatic int lifetime = 0; 9292559Sdes 93113911Sdes/* User has to confirm key use */ 94113911Sdesstatic int confirm = 0; 95113911Sdes 96296853Sdes/* we keep a cache of one passphrase */ 9776262Sgreenstatic char *pass = NULL; 9892559Sdesstatic void 9976262Sgreenclear_pass(void) 10076262Sgreen{ 10176262Sgreen if (pass) { 102264377Sdes explicit_bzero(pass, strlen(pass)); 103255767Sdes free(pass); 10476262Sgreen pass = NULL; 10576262Sgreen } 10676262Sgreen} 10776262Sgreen 10892559Sdesstatic int 109295367Sdesdelete_file(int agent_fd, const char *filename, int key_only) 11057429Smarkm{ 111295367Sdes struct sshkey *public, *cert = NULL; 112248619Sdes char *certpath = NULL, *comment = NULL; 113295367Sdes int r, ret = -1; 11457429Smarkm 115295367Sdes if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { 116295367Sdes printf("Bad key file %s: %s\n", filename, ssh_err(r)); 11792559Sdes return -1; 11857429Smarkm } 119295367Sdes if ((r = ssh_remove_identity(agent_fd, public)) == 0) { 12057429Smarkm fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); 12192559Sdes ret = 0; 12292559Sdes } else 123295367Sdes fprintf(stderr, "Could not remove identity \"%s\": %s\n", 124295367Sdes 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); 133295367Sdes if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { 134295367Sdes if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 135295367Sdes error("Failed to load certificate \"%s\": %s", 136295367Sdes certpath, ssh_err(r)); 137248619Sdes goto out; 138295367Sdes } 139295367Sdes 140295367Sdes if (!sshkey_equal_public(cert, public)) 141248619Sdes fatal("Certificate %s does not match private key %s", 142248619Sdes certpath, filename); 143248619Sdes 144295367Sdes 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 149295367Sdes fprintf(stderr, "Could not remove identity \"%s\": %s\n", 150295367Sdes certpath, ssh_err(r)); 151248619Sdes 152248619Sdes out: 153296853Sdes sshkey_free(cert); 154296853Sdes 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 163295367Sdesdelete_all(int agent_fd) 16457429Smarkm{ 16592559Sdes int ret = -1; 16665668Skris 167295367Sdes if (ssh_remove_all_identities(agent_fd, 2) == 0) 16892559Sdes ret = 0; 169295367Sdes /* ignore error-code for ssh1 */ 170295367Sdes 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 181295367Sdesadd_file(int agent_fd, const char *filename, int key_only) 18257429Smarkm{ 183295367Sdes struct sshkey *private, *cert; 18476262Sgreen char *comment = NULL; 185240075Sdes char msg[1024], *certpath = NULL; 186295367Sdes int r, fd, ret = -1; 187295367Sdes 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) { 202295367Sdes if (sshkey_perm_ok(fd, filename) != 0) { 203226046Sdes close(fd); 204226046Sdes return -1; 205226046Sdes } 206226046Sdes } 207295367Sdes if ((keyblob = sshbuf_new()) == NULL) 208295367Sdes fatal("%s: sshbuf_new failed", __func__); 209295367Sdes if ((r = sshkey_load_file(fd, keyblob)) != 0) { 210295367Sdes fprintf(stderr, "Error loading key \"%s\": %s\n", 211295367Sdes filename, ssh_err(r)); 212295367Sdes sshbuf_free(keyblob); 213226046Sdes close(fd); 214226046Sdes return -1; 215226046Sdes } 216162856Sdes close(fd); 217162856Sdes 21857429Smarkm /* At first, try empty passphrase */ 219296853Sdes if ((r = sshkey_parse_private_fileblob(keyblob, "", &private, 220296853Sdes &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 221295367Sdes fprintf(stderr, "Error loading key \"%s\": %s\n", 222295367Sdes filename, ssh_err(r)); 223295367Sdes goto fail_load; 224295367Sdes } 225295367Sdes /* try last */ 226295367Sdes if (private == NULL && pass != NULL) { 227296853Sdes if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private, 228296853Sdes &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 229295367Sdes fprintf(stderr, "Error loading key \"%s\": %s\n", 230295367Sdes filename, ssh_err(r)); 231295367Sdes goto fail_load; 232295367Sdes } 233295367Sdes } 23476262Sgreen if (private == NULL) { 23576262Sgreen /* clear passphrase since it did not work */ 23676262Sgreen clear_pass(); 237296853Sdes snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ", 238296853Sdes filename, confirm ? " (will confirm each use)" : ""); 23957429Smarkm for (;;) { 24092559Sdes pass = read_passphrase(msg, RP_ALLOW_STDIN); 241295367Sdes if (strcmp(pass, "") == 0) 242295367Sdes goto fail_load; 243295367Sdes if ((r = sshkey_parse_private_fileblob(keyblob, pass, 244296853Sdes &private, &comment)) == 0) 245295367Sdes break; 246295367Sdes else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 247295367Sdes fprintf(stderr, 248295367Sdes "Error loading key \"%s\": %s\n", 249295367Sdes filename, ssh_err(r)); 250295367Sdes fail_load: 25176262Sgreen clear_pass(); 252295367Sdes sshbuf_free(keyblob); 25392559Sdes return -1; 25457429Smarkm } 25576262Sgreen clear_pass(); 256124211Sdes snprintf(msg, sizeof msg, 257296853Sdes "Bad passphrase, try again for %s%s: ", filename, 258295367Sdes confirm ? " (will confirm each use)" : ""); 25957429Smarkm } 26057429Smarkm } 261296853Sdes if (comment == NULL || *comment == '\0') 262296853Sdes comment = xstrdup(filename); 263295367Sdes sshbuf_free(keyblob); 26498684Sdes 265295367Sdes if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 266295367Sdes 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 { 276295367Sdes fprintf(stderr, "Could not add identity \"%s\": %s\n", 277295367Sdes 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); 286295367Sdes if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { 287295367Sdes if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 288295367Sdes error("Failed to load certificate \"%s\": %s", 289295367Sdes certpath, ssh_err(r)); 290215116Sdes goto out; 291295367Sdes } 292215116Sdes 293295367Sdes if (!sshkey_equal_public(cert, private)) { 294215116Sdes error("Certificate %s does not match private key %s", 295215116Sdes certpath, filename); 296295367Sdes sshkey_free(cert); 297215116Sdes goto out; 298215116Sdes } 299204917Sdes 300215116Sdes /* Graft with private bits */ 301295367Sdes if ((r = sshkey_to_certified(private)) != 0) { 302295367Sdes error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); 303295367Sdes sshkey_free(cert); 304215116Sdes goto out; 305204917Sdes } 306295367Sdes if ((r = sshkey_cert_copy(cert, private)) != 0) { 307295367Sdes error("%s: key_cert_copy: %s", __func__, ssh_err(r)); 308295367Sdes sshkey_free(cert); 309295367Sdes goto out; 310295367Sdes } 311295367Sdes sshkey_free(cert); 312204917Sdes 313295367Sdes if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 314295367Sdes lifetime, confirm)) != 0) { 315295367Sdes error("Certificate %s (%s) add failed: %s", certpath, 316295367Sdes private->cert->key_id, ssh_err(r)); 317295367Sdes 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: 326295367Sdes free(certpath); 327255767Sdes free(comment); 328295367Sdes sshkey_free(private); 32992559Sdes 33092559Sdes return ret; 33157429Smarkm} 33257429Smarkm 33392559Sdesstatic int 334295367Sdesupdate_card(int agent_fd, int add, const char *id) 33592559Sdes{ 336262566Sdes char *pin = NULL; 337295367Sdes int r, ret = -1; 33898684Sdes 339262566Sdes if (add) { 340262566Sdes if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", 341262566Sdes RP_ALLOW_STDIN)) == NULL) 342262566Sdes return -1; 343262566Sdes } 34498684Sdes 345295367Sdes if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, 346295367Sdes lifetime, confirm)) == 0) { 34792559Sdes fprintf(stderr, "Card %s: %s\n", 34892559Sdes add ? "added" : "removed", id); 349113911Sdes ret = 0; 35092559Sdes } else { 351295367Sdes fprintf(stderr, "Could not %s card \"%s\": %s\n", 352295367Sdes add ? "add" : "remove", id, ssh_err(r)); 353113911Sdes ret = -1; 35492559Sdes } 355255767Sdes free(pin); 356113911Sdes return ret; 35792559Sdes} 35892559Sdes 35992559Sdesstatic int 360295367Sdeslist_identities(int agent_fd, int do_fp) 36157429Smarkm{ 362295367Sdes char *fp; 363295367Sdes int r, had_identities = 0; 364295367Sdes struct ssh_identitylist *idlist; 365295367Sdes size_t i; 366295367Sdes#ifdef WITH_SSH1 367295367Sdes int version = 1; 368295367Sdes#else 369295367Sdes int version = 2; 370295367Sdes#endif 37157429Smarkm 372295367Sdes for (; version <= 2; version++) { 373295367Sdes if ((r = ssh_fetch_identitylist(agent_fd, version, 374295367Sdes &idlist)) != 0) { 375295367Sdes if (r != SSH_ERR_AGENT_NO_IDENTITIES) 376295367Sdes fprintf(stderr, "error fetching identities for " 377295367Sdes "protocol %d: %s\n", version, ssh_err(r)); 378295367Sdes continue; 379295367Sdes } 380295367Sdes for (i = 0; i < idlist->nkeys; i++) { 38165668Skris had_identities = 1; 38276262Sgreen if (do_fp) { 383295367Sdes fp = sshkey_fingerprint(idlist->keys[i], 384295367Sdes fingerprint_hash, SSH_FP_DEFAULT); 385296853Sdes printf("%u %s %s (%s)\n", 386295367Sdes sshkey_size(idlist->keys[i]), 387295367Sdes fp == NULL ? "(null)" : fp, 388295367Sdes idlist->comments[i], 389295367Sdes sshkey_type(idlist->keys[i])); 390255767Sdes free(fp); 39157429Smarkm } else { 392295367Sdes if ((r = sshkey_write(idlist->keys[i], 393295367Sdes stdout)) != 0) { 394295367Sdes fprintf(stderr, "sshkey_write: %s\n", 395295367Sdes ssh_err(r)); 396295367Sdes continue; 397295367Sdes } 398295367Sdes fprintf(stdout, " %s\n", idlist->comments[i]); 39957429Smarkm } 40057429Smarkm } 401295367Sdes 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 411295367Sdeslock_agent(int agent_fd, int lock) 41298684Sdes{ 41398684Sdes char prompt[100], *p1, *p2; 414295367Sdes 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 } 425264377Sdes explicit_bzero(p2, strlen(p2)); 426255767Sdes free(p2); 42798684Sdes } 428295367Sdes if (passok) { 429295367Sdes if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { 430295367Sdes fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 431295367Sdes ret = 0; 432295367Sdes } else { 433295367Sdes fprintf(stderr, "Failed to %slock agent: %s\n", 434295367Sdes lock ? "" : "un", ssh_err(r)); 435295367Sdes } 436295367Sdes } 437264377Sdes explicit_bzero(p1, strlen(p1)); 438255767Sdes free(p1); 439106130Sdes return (ret); 44098684Sdes} 44198684Sdes 44298684Sdesstatic int 443295367Sdesdo_file(int agent_fd, int deleting, int key_only, char *file) 44492559Sdes{ 44592559Sdes if (deleting) { 446295367Sdes if (delete_file(agent_fd, file, key_only) == -1) 44792559Sdes return -1; 44892559Sdes } else { 449295367Sdes 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"); 461295367Sdes 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; 479295367Sdes int agent_fd; 480204917Sdes char *pkcs11provider = NULL; 481295367Sdes int r, i, ch, deleting = 0, ret = 0, key_only = 0; 482295367Sdes int xflag = 0, lflag = 0, Dflag = 0; 48357429Smarkm 484296853Sdes 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 491295367Sdes#ifdef WITH_OPENSSL 492221420Sdes OpenSSL_add_all_algorithms(); 493295367Sdes#endif 49457429Smarkm 495295367Sdes setvbuf(stdout, NULL, _IOLBF, 0); 496295367Sdes 497295367Sdes /* First, get a connection to the authentication agent. */ 498295367Sdes switch (r = ssh_get_authentication_socket(&agent_fd)) { 499295367Sdes case 0: 500295367Sdes break; 501295367Sdes case SSH_ERR_AGENT_NOT_PRESENT: 502295367Sdes fprintf(stderr, "Could not open a connection to your " 503295367Sdes "authentication agent.\n"); 50492559Sdes exit(2); 505295367Sdes default: 506295367Sdes fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); 507295367Sdes exit(2); 50857429Smarkm } 509295367Sdes 510295367Sdes while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) { 51192559Sdes switch (ch) { 512295367Sdes case 'E': 513295367Sdes fingerprint_hash = ssh_digest_alg_by_name(optarg); 514295367Sdes if (fingerprint_hash == -1) 515295367Sdes fatal("Invalid hash algorithm \"%s\"", optarg); 516295367Sdes break; 517240075Sdes case 'k': 518240075Sdes key_only = 1; 519240075Sdes break; 52092559Sdes case 'l': 52192559Sdes case 'L': 522295367Sdes if (lflag != 0) 523295367Sdes fatal("-%c flag already specified", lflag); 524295367Sdes lflag = ch; 525295367Sdes break; 52698684Sdes case 'x': 52798684Sdes case 'X': 528295367Sdes if (xflag != 0) 529295367Sdes fatal("-%c flag already specified", xflag); 530295367Sdes xflag = ch; 531295367Sdes break; 532113911Sdes case 'c': 533113911Sdes confirm = 1; 534113911Sdes break; 53592559Sdes case 'd': 53657429Smarkm deleting = 1; 53792559Sdes break; 53892559Sdes case 'D': 539295367Sdes Dflag = 1; 540295367Sdes 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 } 561295367Sdes 562295367Sdes if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) 563295367Sdes fatal("Invalid combination of actions"); 564295367Sdes else if (xflag) { 565295367Sdes if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) 566295367Sdes ret = 1; 567295367Sdes goto done; 568295367Sdes } else if (lflag) { 569295367Sdes if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) 570295367Sdes ret = 1; 571295367Sdes goto done; 572295367Sdes } else if (Dflag) { 573295367Sdes if (delete_all(agent_fd) == -1) 574295367Sdes ret = 1; 575295367Sdes goto done; 576295367Sdes } 577295367Sdes 57892559Sdes argc -= optind; 57992559Sdes argv += optind; 580204917Sdes if (pkcs11provider != NULL) { 581295367Sdes if (update_card(agent_fd, !deleting, pkcs11provider) == -1) 58292559Sdes ret = 1; 58392559Sdes goto done; 58492559Sdes } 58592559Sdes if (argc == 0) { 586295367Sdes 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; 603295367Sdes 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++) { 612295367Sdes if (do_file(agent_fd, deleting, key_only, 613295367Sdes argv[i]) == -1) 61492559Sdes ret = 1; 61592559Sdes } 61657429Smarkm } 61776262Sgreen clear_pass(); 61892559Sdes 61992559Sdesdone: 620295367Sdes ssh_close_authentication_socket(agent_fd); 62192559Sdes return ret; 62257429Smarkm} 623