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