Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.121
1.121 ! markus 1: /* $OpenBSD: ssh-add.c,v 1.120 2015/02/21 21:46:57 halex Exp $ */
1.1 deraadt 2: /*
1.13 deraadt 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.
1.19 markus 7: *
1.22 deraadt 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: *
1.19 markus 14: * SSH2 implementation,
1.41 markus 15: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.22 deraadt 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.
1.13 deraadt 36: */
1.1 deraadt 37:
1.75 stevesk 38: #include <sys/types.h>
39: #include <sys/stat.h>
1.16 markus 40:
1.19 markus 41: #include <openssl/evp.h>
1.81 stevesk 42:
1.116 djm 43: #include <errno.h>
1.82 stevesk 44: #include <fcntl.h>
1.81 stevesk 45: #include <pwd.h>
1.88 stevesk 46: #include <stdio.h>
1.87 stevesk 47: #include <stdlib.h>
1.85 stevesk 48: #include <string.h>
1.84 stevesk 49: #include <unistd.h>
1.117 deraadt 50: #include <limits.h>
1.1 deraadt 51:
1.89 deraadt 52: #include "xmalloc.h"
1.27 markus 53: #include "ssh.h"
1.1 deraadt 54: #include "rsa.h"
1.27 markus 55: #include "log.h"
1.116 djm 56: #include "sshkey.h"
57: #include "sshbuf.h"
1.18 markus 58: #include "authfd.h"
1.16 markus 59: #include "authfile.h"
1.25 markus 60: #include "pathnames.h"
1.57 stevesk 61: #include "misc.h"
1.110 djm 62: #include "ssherr.h"
1.115 djm 63: #include "digest.h"
1.1 deraadt 64:
1.45 jakob 65: /* argv0 */
66: extern char *__progname;
67:
1.48 djm 68: /* Default files to add */
69: static char *default_files[] = {
70: _PATH_SSH_CLIENT_ID_RSA,
71: _PATH_SSH_CLIENT_ID_DSA,
1.99 djm 72: _PATH_SSH_CLIENT_ID_ECDSA,
1.107 pascal 73: _PATH_SSH_CLIENT_ID_ED25519,
1.51 markus 74: _PATH_SSH_CLIENT_IDENTITY,
1.48 djm 75: NULL
76: };
77:
1.115 djm 78: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
79:
1.56 markus 80: /* Default lifetime (0 == forever) */
1.57 stevesk 81: static int lifetime = 0;
1.48 djm 82:
1.65 markus 83: /* User has to confirm key use */
84: static int confirm = 0;
85:
1.33 markus 86: /* we keep a cache of one passphrases */
87: static char *pass = NULL;
1.39 itojun 88: static void
1.33 markus 89: clear_pass(void)
90: {
91: if (pass) {
1.109 djm 92: explicit_bzero(pass, strlen(pass));
1.106 djm 93: free(pass);
1.33 markus 94: pass = NULL;
95: }
96: }
97:
1.46 djm 98: static int
1.116 djm 99: delete_file(int agent_fd, const char *filename, int key_only)
1.1 deraadt 100: {
1.116 djm 101: struct sshkey *public, *cert = NULL;
1.104 djm 102: char *certpath = NULL, *comment = NULL;
1.116 djm 103: int r, ret = -1;
1.1 deraadt 104:
1.116 djm 105: if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
106: printf("Bad key file %s: %s\n", filename, ssh_err(r));
1.46 djm 107: return -1;
1.12 markus 108: }
1.116 djm 109: if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
1.12 markus 110: fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
1.46 djm 111: ret = 0;
112: } else
1.116 djm 113: fprintf(stderr, "Could not remove identity \"%s\": %s\n",
114: filename, ssh_err(r));
1.46 djm 115:
1.104 djm 116: if (key_only)
117: goto out;
118:
119: /* Now try to delete the corresponding certificate too */
120: free(comment);
1.105 markus 121: comment = NULL;
1.104 djm 122: xasprintf(&certpath, "%s-cert.pub", filename);
1.120 halex 123: if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
124: if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
125: error("Failed to load certificate \"%s\": %s",
126: certpath, ssh_err(r));
1.104 djm 127: goto out;
1.120 halex 128: }
129:
1.116 djm 130: if (!sshkey_equal_public(cert, public))
1.104 djm 131: fatal("Certificate %s does not match private key %s",
132: certpath, filename);
133:
1.120 halex 134: if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
1.104 djm 135: fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
136: comment);
137: ret = 0;
138: } else
1.120 halex 139: fprintf(stderr, "Could not remove identity \"%s\": %s\n",
140: certpath, ssh_err(r));
1.104 djm 141:
142: out:
143: if (cert != NULL)
1.116 djm 144: sshkey_free(cert);
1.104 djm 145: if (public != NULL)
1.116 djm 146: sshkey_free(public);
1.104 djm 147: free(certpath);
148: free(comment);
1.47 deraadt 149:
1.46 djm 150: return ret;
1.1 deraadt 151: }
152:
1.19 markus 153: /* Send a request to remove all identities. */
1.46 djm 154: static int
1.116 djm 155: delete_all(int agent_fd)
1.1 deraadt 156: {
1.46 djm 157: int ret = -1;
1.19 markus 158:
1.121 ! markus 159: if (ssh_remove_all_identities(agent_fd, 2) == 0)
1.46 djm 160: ret = 0;
1.121 ! markus 161: /* ignore error-code for ssh1 */
! 162: ssh_remove_all_identities(agent_fd, 1);
1.19 markus 163:
1.46 djm 164: if (ret == 0)
1.12 markus 165: fprintf(stderr, "All identities removed.\n");
166: else
1.24 markus 167: fprintf(stderr, "Failed to remove all identities.\n");
1.46 djm 168:
169: return ret;
1.1 deraadt 170: }
171:
1.46 djm 172: static int
1.116 djm 173: add_file(int agent_fd, const char *filename, int key_only)
1.1 deraadt 174: {
1.116 djm 175: struct sshkey *private, *cert;
1.36 markus 176: char *comment = NULL;
1.102 djm 177: char msg[1024], *certpath = NULL;
1.116 djm 178: int r, fd, ret = -1;
179: struct sshbuf *keyblob;
1.12 markus 180:
1.101 djm 181: if (strcmp(filename, "-") == 0) {
182: fd = STDIN_FILENO;
183: filename = "(stdin)";
184: } else if ((fd = open(filename, O_RDONLY)) < 0) {
1.19 markus 185: perror(filename);
1.46 djm 186: return -1;
1.19 markus 187: }
1.76 dtucker 188:
189: /*
190: * Since we'll try to load a keyfile multiple times, permission errors
191: * will occur multiple times, so check perms first and bail if wrong.
192: */
1.101 djm 193: if (fd != STDIN_FILENO) {
1.116 djm 194: if (sshkey_perm_ok(fd, filename) != 0) {
1.101 djm 195: close(fd);
196: return -1;
197: }
198: }
1.116 djm 199: if ((keyblob = sshbuf_new()) == NULL)
200: fatal("%s: sshbuf_new failed", __func__);
201: if ((r = sshkey_load_file(fd, keyblob)) != 0) {
202: fprintf(stderr, "Error loading key \"%s\": %s\n",
203: filename, ssh_err(r));
204: sshbuf_free(keyblob);
1.101 djm 205: close(fd);
206: return -1;
207: }
1.76 dtucker 208: close(fd);
209:
1.12 markus 210: /* At first, try empty passphrase */
1.116 djm 211: if ((r = sshkey_parse_private_fileblob(keyblob, "", filename,
212: &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
213: fprintf(stderr, "Error loading key \"%s\": %s\n",
214: filename, ssh_err(r));
215: goto fail_load;
216: }
1.33 markus 217: /* try last */
1.110 djm 218: if (private == NULL && pass != NULL) {
1.116 djm 219: if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename,
1.110 djm 220: &private, &comment)) != 0 &&
1.116 djm 221: r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
222: fprintf(stderr, "Error loading key \"%s\": %s\n",
223: filename, ssh_err(r));
224: goto fail_load;
225: }
1.110 djm 226: }
1.113 benno 227: if (comment == NULL)
228: comment = xstrdup(filename);
1.31 markus 229: if (private == NULL) {
1.33 markus 230: /* clear passphrase since it did not work */
231: clear_pass();
1.119 halex 232: snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ",
233: comment, confirm ? " (will confirm each use)" : "");
1.12 markus 234: for (;;) {
1.40 markus 235: pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.116 djm 236: if (strcmp(pass, "") == 0)
237: goto fail_load;
238: if ((r = sshkey_parse_private_fileblob(keyblob, pass,
239: filename, &private, NULL)) == 0)
240: break;
241: else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
242: fprintf(stderr,
243: "Error loading key \"%s\": %s\n",
244: filename, ssh_err(r));
245: fail_load:
1.35 markus 246: clear_pass();
1.106 djm 247: free(comment);
1.116 djm 248: sshbuf_free(keyblob);
1.46 djm 249: return -1;
1.12 markus 250: }
1.33 markus 251: clear_pass();
1.68 markus 252: snprintf(msg, sizeof msg,
1.119 halex 253: "Bad passphrase, try again for %.200s%s: ", comment,
254: confirm ? " (will confirm each use)" : "");
1.12 markus 255: }
256: }
1.116 djm 257: sshbuf_free(keyblob);
1.60 markus 258:
1.116 djm 259: if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
260: lifetime, confirm)) == 0) {
1.60 markus 261: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
262: ret = 0;
263: if (lifetime != 0)
1.64 deraadt 264: fprintf(stderr,
1.60 markus 265: "Lifetime set to %d seconds\n", lifetime);
1.69 djm 266: if (confirm != 0)
1.65 markus 267: fprintf(stderr,
1.96 djm 268: "The user must confirm each use of the key\n");
1.60 markus 269: } else {
1.116 djm 270: fprintf(stderr, "Could not add identity \"%s\": %s\n",
271: filename, ssh_err(r));
1.56 markus 272: }
273:
1.102 djm 274: /* Skip trying to load the cert if requested */
275: if (key_only)
276: goto out;
1.93 djm 277:
278: /* Now try to add the certificate flavour too */
279: xasprintf(&certpath, "%s-cert.pub", filename);
1.116 djm 280: if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
281: if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
282: error("Failed to load certificate \"%s\": %s",
283: certpath, ssh_err(r));
1.96 djm 284: goto out;
1.116 djm 285: }
1.96 djm 286:
1.116 djm 287: if (!sshkey_equal_public(cert, private)) {
1.96 djm 288: error("Certificate %s does not match private key %s",
289: certpath, filename);
1.116 djm 290: sshkey_free(cert);
1.96 djm 291: goto out;
292: }
1.93 djm 293:
1.96 djm 294: /* Graft with private bits */
1.116 djm 295: if ((r = sshkey_to_certified(private,
296: sshkey_cert_is_legacy(cert))) != 0) {
297: error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
298: sshkey_free(cert);
299: goto out;
300: }
301: if ((r = sshkey_cert_copy(cert, private)) != 0) {
302: error("%s: key_cert_copy: %s", __func__, ssh_err(r));
303: sshkey_free(cert);
1.96 djm 304: goto out;
1.94 otto 305: }
1.116 djm 306: sshkey_free(cert);
1.93 djm 307:
1.116 djm 308: if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
309: lifetime, confirm)) != 0) {
310: error("Certificate %s (%s) add failed: %s", certpath,
311: private->cert->key_id, ssh_err(r));
312: goto out;
1.96 djm 313: }
314: fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
315: private->cert->key_id);
316: if (lifetime != 0)
317: fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
318: if (confirm != 0)
319: fprintf(stderr, "The user must confirm each use of the key\n");
320: out:
1.116 djm 321: free(certpath);
1.106 djm 322: free(comment);
1.116 djm 323: sshkey_free(private);
1.47 deraadt 324:
1.46 djm 325: return ret;
1.1 deraadt 326: }
327:
1.46 djm 328: static int
1.116 djm 329: update_card(int agent_fd, int add, const char *id)
1.42 markus 330: {
1.108 djm 331: char *pin = NULL;
1.116 djm 332: int r, ret = -1;
1.53 rees 333:
1.108 djm 334: if (add) {
335: if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
336: RP_ALLOW_STDIN)) == NULL)
337: return -1;
338: }
1.53 rees 339:
1.116 djm 340: if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
341: lifetime, confirm)) == 0) {
1.44 markus 342: fprintf(stderr, "Card %s: %s\n",
1.47 deraadt 343: add ? "added" : "removed", id);
1.66 markus 344: ret = 0;
1.46 djm 345: } else {
1.116 djm 346: fprintf(stderr, "Could not %s card \"%s\": %s\n",
347: add ? "add" : "remove", id, ssh_err(r));
1.66 markus 348: ret = -1;
1.46 djm 349: }
1.106 djm 350: free(pin);
1.66 markus 351: return ret;
1.42 markus 352: }
353:
1.50 markus 354: static int
1.116 djm 355: list_identities(int agent_fd, int do_fp)
1.1 deraadt 356: {
1.116 djm 357: char *fp;
1.121 ! markus 358: int r, had_identities = 0;
1.116 djm 359: struct ssh_identitylist *idlist;
360: size_t i;
1.121 ! markus 361: #ifdef WITH_SSH1
! 362: int version = 1;
! 363: #else
! 364: int version = 2;
! 365: #endif
1.12 markus 366:
1.121 ! markus 367: for (; version <= 2; version++) {
1.116 djm 368: if ((r = ssh_fetch_identitylist(agent_fd, version,
369: &idlist)) != 0) {
370: if (r != SSH_ERR_AGENT_NO_IDENTITIES)
371: fprintf(stderr, "error fetching identities for "
372: "protocol %d: %s\n", version, ssh_err(r));
373: continue;
374: }
375: for (i = 0; i < idlist->nkeys; i++) {
1.19 markus 376: had_identities = 1;
1.30 markus 377: if (do_fp) {
1.116 djm 378: fp = sshkey_fingerprint(idlist->keys[i],
379: fingerprint_hash, SSH_FP_DEFAULT);
1.23 markus 380: printf("%d %s %s (%s)\n",
1.118 djm 381: sshkey_size(idlist->keys[i]),
382: fp == NULL ? "(null)" : fp,
1.116 djm 383: idlist->comments[i],
384: sshkey_type(idlist->keys[i]));
1.106 djm 385: free(fp);
1.12 markus 386: } else {
1.116 djm 387: if ((r = sshkey_write(idlist->keys[i],
388: stdout)) != 0) {
389: fprintf(stderr, "sshkey_write: %s\n",
390: ssh_err(r));
391: continue;
392: }
393: fprintf(stdout, " %s\n", idlist->comments[i]);
1.12 markus 394: }
395: }
1.116 djm 396: ssh_free_identitylist(idlist);
1.12 markus 397: }
1.50 markus 398: if (!had_identities) {
1.12 markus 399: printf("The agent has no identities.\n");
1.50 markus 400: return -1;
401: }
402: return 0;
1.1 deraadt 403: }
404:
1.48 djm 405: static int
1.116 djm 406: lock_agent(int agent_fd, int lock)
1.54 markus 407: {
408: char prompt[100], *p1, *p2;
1.116 djm 409: int r, passok = 1, ret = -1;
1.61 deraadt 410:
1.54 markus 411: strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
412: p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
413: if (lock) {
414: strlcpy(prompt, "Again: ", sizeof prompt);
415: p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
416: if (strcmp(p1, p2) != 0) {
417: fprintf(stderr, "Passwords do not match.\n");
418: passok = 0;
419: }
1.109 djm 420: explicit_bzero(p2, strlen(p2));
1.106 djm 421: free(p2);
1.54 markus 422: }
1.116 djm 423: if (passok) {
424: if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
425: fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
426: ret = 0;
427: } else {
428: fprintf(stderr, "Failed to %slock agent: %s\n",
429: lock ? "" : "un", ssh_err(r));
430: }
431: }
1.109 djm 432: explicit_bzero(p1, strlen(p1));
1.106 djm 433: free(p1);
1.62 markus 434: return (ret);
1.54 markus 435: }
436:
437: static int
1.116 djm 438: do_file(int agent_fd, int deleting, int key_only, char *file)
1.48 djm 439: {
440: if (deleting) {
1.116 djm 441: if (delete_file(agent_fd, file, key_only) == -1)
1.48 djm 442: return -1;
443: } else {
1.116 djm 444: if (add_file(agent_fd, file, key_only) == -1)
1.48 djm 445: return -1;
446: }
447: return 0;
448: }
449:
1.42 markus 450: static void
451: usage(void)
452: {
1.90 sobrado 453: fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
1.45 jakob 454: fprintf(stderr, "Options:\n");
455: fprintf(stderr, " -l List fingerprints of all identities.\n");
1.115 djm 456: fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
1.45 jakob 457: fprintf(stderr, " -L List public key parameters of all identities.\n");
1.103 djm 458: fprintf(stderr, " -k Load only keys and not certificates.\n");
459: fprintf(stderr, " -c Require confirmation to sign using identities\n");
460: fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
1.45 jakob 461: fprintf(stderr, " -d Delete identity.\n");
462: fprintf(stderr, " -D Delete all identities.\n");
1.55 markus 463: fprintf(stderr, " -x Lock agent.\n");
1.63 markus 464: fprintf(stderr, " -X Unlock agent.\n");
1.92 markus 465: fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
466: fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
1.42 markus 467: }
468:
1.2 provos 469: int
1.7 markus 470: main(int argc, char **argv)
1.1 deraadt 471: {
1.43 markus 472: extern char *optarg;
473: extern int optind;
1.116 djm 474: int agent_fd;
1.92 markus 475: char *pkcs11provider = NULL;
1.116 djm 476: int r, i, ch, deleting = 0, ret = 0, key_only = 0;
1.115 djm 477: int xflag = 0, lflag = 0, Dflag = 0;
1.73 djm 478:
479: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
480: sanitise_stdfd();
1.12 markus 481:
1.100 djm 482: OpenSSL_add_all_algorithms();
1.112 djm 483:
1.114 millert 484: setvbuf(stdout, NULL, _IOLBF, 0);
1.19 markus 485:
1.116 djm 486: /* First, get a connection to the authentication agent. */
487: switch (r = ssh_get_authentication_socket(&agent_fd)) {
488: case 0:
489: break;
490: case SSH_ERR_AGENT_NOT_PRESENT:
491: fprintf(stderr, "Could not open a connection to your "
492: "authentication agent.\n");
493: exit(2);
494: default:
495: fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
1.50 markus 496: exit(2);
1.12 markus 497: }
1.116 djm 498:
1.115 djm 499: while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
1.43 markus 500: switch (ch) {
1.115 djm 501: case 'E':
502: fingerprint_hash = ssh_digest_alg_by_name(optarg);
503: if (fingerprint_hash == -1)
504: fatal("Invalid hash algorithm \"%s\"", optarg);
505: break;
1.102 djm 506: case 'k':
507: key_only = 1;
508: break;
1.43 markus 509: case 'l':
510: case 'L':
1.115 djm 511: if (lflag != 0)
512: fatal("-%c flag already specified", lflag);
513: lflag = ch;
514: break;
1.54 markus 515: case 'x':
516: case 'X':
1.115 djm 517: if (xflag != 0)
518: fatal("-%c flag already specified", xflag);
519: xflag = ch;
520: break;
1.65 markus 521: case 'c':
522: confirm = 1;
1.43 markus 523: break;
524: case 'd':
1.12 markus 525: deleting = 1;
1.43 markus 526: break;
527: case 'D':
1.115 djm 528: Dflag = 1;
529: break;
1.43 markus 530: case 's':
1.92 markus 531: pkcs11provider = optarg;
1.43 markus 532: break;
533: case 'e':
1.47 deraadt 534: deleting = 1;
1.92 markus 535: pkcs11provider = optarg;
1.56 markus 536: break;
537: case 't':
1.57 stevesk 538: if ((lifetime = convtime(optarg)) == -1) {
539: fprintf(stderr, "Invalid lifetime\n");
540: ret = 1;
541: goto done;
542: }
1.43 markus 543: break;
544: default:
545: usage();
1.46 djm 546: ret = 1;
547: goto done;
1.42 markus 548: }
549: }
1.115 djm 550:
551: if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
552: fatal("Invalid combination of actions");
553: else if (xflag) {
1.116 djm 554: if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
1.115 djm 555: ret = 1;
556: goto done;
557: } else if (lflag) {
1.116 djm 558: if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
1.115 djm 559: ret = 1;
560: goto done;
561: } else if (Dflag) {
1.116 djm 562: if (delete_all(agent_fd) == -1)
1.115 djm 563: ret = 1;
564: goto done;
565: }
566:
1.43 markus 567: argc -= optind;
568: argv += optind;
1.92 markus 569: if (pkcs11provider != NULL) {
1.116 djm 570: if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
1.46 djm 571: ret = 1;
1.43 markus 572: goto done;
1.12 markus 573: }
1.43 markus 574: if (argc == 0) {
1.117 deraadt 575: char buf[PATH_MAX];
1.48 djm 576: struct passwd *pw;
1.52 markus 577: struct stat st;
578: int count = 0;
1.48 djm 579:
580: if ((pw = getpwuid(getuid())) == NULL) {
1.20 deraadt 581: fprintf(stderr, "No user found with uid %u\n",
582: (u_int)getuid());
1.46 djm 583: ret = 1;
584: goto done;
1.12 markus 585: }
1.48 djm 586:
1.71 deraadt 587: for (i = 0; default_files[i]; i++) {
1.51 markus 588: snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48 djm 589: default_files[i]);
1.52 markus 590: if (stat(buf, &st) < 0)
591: continue;
1.116 djm 592: if (do_file(agent_fd, deleting, key_only, buf) == -1)
1.46 djm 593: ret = 1;
1.52 markus 594: else
595: count++;
1.46 djm 596: }
1.52 markus 597: if (count == 0)
598: ret = 1;
1.43 markus 599: } else {
1.71 deraadt 600: for (i = 0; i < argc; i++) {
1.116 djm 601: if (do_file(agent_fd, deleting, key_only,
602: argv[i]) == -1)
1.48 djm 603: ret = 1;
1.43 markus 604: }
1.12 markus 605: }
1.33 markus 606: clear_pass();
1.43 markus 607:
608: done:
1.116 djm 609: ssh_close_authentication_socket(agent_fd);
1.46 djm 610: return ret;
1.1 deraadt 611: }