blob: a796647a76434c0fb05618e0bd8f643c6bd18dcc [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Adds an identity to the authentication server, or removes an identity.
Damien Millerad833b32000-08-23 10:46:23 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Damien Millerad833b32000-08-23 10:46:23 +100013 * SSH2 implementation,
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Darren Tucker47eede72005-03-14 23:08:12 +110038RCSID("$OpenBSD: ssh-add.c,v 1.71 2005/03/10 22:01:06 deraadt Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100039
Damien Millerad833b32000-08-23 10:46:23 +100040#include <openssl/evp.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "rsa.h"
44#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100047#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000050#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Ben Lindstromddfb1e32001-08-06 22:06:35 +000052/* argv0 */
53extern char *__progname;
54
Damien Miller6e1057c2002-01-22 23:05:59 +110055/* Default files to add */
56static char *default_files[] = {
57 _PATH_SSH_CLIENT_ID_RSA,
58 _PATH_SSH_CLIENT_ID_DSA,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000059 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110060 NULL
61};
62
Ben Lindstrom61d328a2002-06-06 21:54:57 +000063/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000064static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110065
Damien Miller6c711792003-01-24 11:36:23 +110066/* User has to confirm key use */
67static int confirm = 0;
68
Ben Lindstromee617942001-04-10 02:45:32 +000069/* we keep a cache of one passphrases */
70static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000071static void
Ben Lindstromee617942001-04-10 02:45:32 +000072clear_pass(void)
73{
74 if (pass) {
75 memset(pass, 0, strlen(pass));
76 xfree(pass);
77 pass = NULL;
78 }
79}
80
Ben Lindstrom569f88d2001-10-03 17:43:01 +000081static int
Damien Miller01ab4a21999-10-28 15:23:30 +100082delete_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083{
Damien Millereba71ba2000-04-29 23:57:08 +100084 Key *public;
Ben Lindstromd5730a82001-04-08 18:04:36 +000085 char *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +000086 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Ben Lindstromd0fca422001-03-26 13:44:06 +000088 public = key_load_public(filename, &comment);
89 if (public == NULL) {
90 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000091 return -1;
Damien Miller95def091999-11-25 00:26:21 +110092 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +000093 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +110094 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000095 ret = 0;
96 } else
Damien Miller95def091999-11-25 00:26:21 +110097 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000098
Damien Millereba71ba2000-04-29 23:57:08 +100099 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100100 xfree(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100101
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000102 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103}
104
Damien Millerad833b32000-08-23 10:46:23 +1000105/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000106static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000107delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000109 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000110
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000111 if (ssh_remove_all_identities(ac, 1))
112 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000113 /* ignore error-code for ssh2 */
114 ssh_remove_all_identities(ac, 2);
115
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000116 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100117 fprintf(stderr, "All identities removed.\n");
118 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000119 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000120
121 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122}
123
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000124static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000125add_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Millerad833b32000-08-23 10:46:23 +1000127 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000128 Key *private;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000129 char *comment = NULL;
130 char msg[1024];
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000131 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Millerad833b32000-08-23 10:46:23 +1000133 if (stat(filename, &st) < 0) {
134 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000135 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000136 }
Damien Miller95def091999-11-25 00:26:21 +1100137 /* At first, try empty passphrase */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000138 private = key_load_private(filename, "", &comment);
139 if (comment == NULL)
140 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000141 /* try last */
142 if (private == NULL && pass != NULL)
143 private = key_load_private(filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000144 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000145 /* clear passphrase since it did not work */
146 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000147 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000148 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100149 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000150 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100151 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000152 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000153 xfree(comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000154 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100155 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000156 private = key_load_private(filename, pass, &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000157 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100158 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000159 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000160 snprintf(msg, sizeof msg,
161 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100162 }
163 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000164
Damien Millera8e06ce2003-11-21 23:48:55 +1100165 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
166 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000167 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000168 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000169 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000170 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000171 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100172 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100173 fprintf(stderr,
174 "The user has to confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000175 } else if (ssh_add_identity(ac, private, comment)) {
176 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
177 ret = 0;
178 } else {
Damien Miller95def091999-11-25 00:26:21 +1100179 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000180 }
181
Ben Lindstromd0fca422001-03-26 13:44:06 +0000182 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000183 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100184
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000185 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186}
187
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000188static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000189update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000190{
Ben Lindstromba72d302002-03-22 03:51:06 +0000191 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100192 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000193
194 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
195 if (pin == NULL)
196 return -1;
197
Damien Millerd94f20d2003-06-11 22:06:33 +1000198 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000199 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100200 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100201 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000202 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000203 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100204 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100205 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000206 }
Damien Miller00111382003-03-10 11:21:17 +1100207 xfree(pin);
208 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000209}
210
Damien Miller43cba342002-02-05 12:12:49 +1100211static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000212list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Millerad833b32000-08-23 10:46:23 +1000214 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000215 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000216 int had_identities = 0;
217 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Millerad833b32000-08-23 10:46:23 +1000219 for (version = 1; version <= 2; version++) {
220 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100221 key != NULL;
222 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000223 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000224 if (do_fp) {
225 fp = key_fingerprint(key, SSH_FP_MD5,
226 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100227 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000228 key_size(key), fp, comment, key_type(key));
229 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100230 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000231 if (!key_write(key, stdout))
232 fprintf(stderr, "key_write failed");
233 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100234 }
Damien Millerad833b32000-08-23 10:46:23 +1000235 key_free(key);
236 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100237 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100238 }
Damien Miller43cba342002-02-05 12:12:49 +1100239 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100240 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100241 return -1;
242 }
243 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244}
245
Damien Miller6e1057c2002-01-22 23:05:59 +1100246static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000247lock_agent(AuthenticationConnection *ac, int lock)
248{
249 char prompt[100], *p1, *p2;
250 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000251
Ben Lindstrom2f717042002-06-06 21:52:03 +0000252 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
253 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
254 if (lock) {
255 strlcpy(prompt, "Again: ", sizeof prompt);
256 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
257 if (strcmp(p1, p2) != 0) {
258 fprintf(stderr, "Passwords do not match.\n");
259 passok = 0;
260 }
261 memset(p2, 0, strlen(p2));
262 xfree(p2);
263 }
264 if (passok && ssh_lock_agent(ac, lock, p1)) {
265 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
266 ret = 0;
267 } else
268 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
269 memset(p1, 0, strlen(p1));
270 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000271 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000272}
273
274static int
Damien Miller6e1057c2002-01-22 23:05:59 +1100275do_file(AuthenticationConnection *ac, int deleting, char *file)
276{
277 if (deleting) {
278 if (delete_file(ac, file) == -1)
279 return -1;
280 } else {
281 if (add_file(ac, file) == -1)
282 return -1;
283 }
284 return 0;
285}
286
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000287static void
288usage(void)
289{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000290 fprintf(stderr, "Usage: %s [options]\n", __progname);
291 fprintf(stderr, "Options:\n");
292 fprintf(stderr, " -l List fingerprints of all identities.\n");
293 fprintf(stderr, " -L List public key parameters of all identities.\n");
294 fprintf(stderr, " -d Delete identity.\n");
295 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000296 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000297 fprintf(stderr, " -X Unlock agent.\n");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000298 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Damien Miller6c711792003-01-24 11:36:23 +1100299 fprintf(stderr, " -c Require confirmation to sign using identities\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000300#ifdef SMARTCARD
301 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
302 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
303#endif
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000304}
305
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306int
Damien Miller01ab4a21999-10-28 15:23:30 +1000307main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000309 extern char *optarg;
310 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100311 AuthenticationConnection *ac = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000312 char *sc_reader_id = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000313 int i, ch, deleting = 0, ret = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller59d3d5b2003-08-22 09:34:41 +1000315 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000316 init_rng();
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000317 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000318
Kevin Stevesef4eea92001-02-05 12:42:17 +0000319 SSLeay_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000320
Damien Miller95def091999-11-25 00:26:21 +1100321 /* At first, get a connection to the authentication agent. */
322 ac = ssh_get_authentication_connection();
323 if (ac == NULL) {
324 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100325 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326 }
Damien Miller6c711792003-01-24 11:36:23 +1100327 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000328 switch (ch) {
329 case 'l':
330 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100331 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
332 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000333 goto done;
334 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000335 case 'x':
336 case 'X':
337 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
338 ret = 1;
339 goto done;
340 break;
Damien Miller6c711792003-01-24 11:36:23 +1100341 case 'c':
342 confirm = 1;
343 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000344 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100345 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000346 break;
347 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000348 if (delete_all(ac) == -1)
349 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000350 goto done;
351 break;
352 case 's':
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000353 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000354 break;
355 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100356 deleting = 1;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000357 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000358 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000359 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000360 if ((lifetime = convtime(optarg)) == -1) {
361 fprintf(stderr, "Invalid lifetime\n");
362 ret = 1;
363 goto done;
364 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000365 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000366 default:
367 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000368 ret = 1;
369 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000370 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000372 argc -= optind;
373 argv += optind;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000374 if (sc_reader_id != NULL) {
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000375 if (update_card(ac, !deleting, sc_reader_id) == -1)
376 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000377 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000378 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000379 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100380 char buf[MAXPATHLEN];
381 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000382 struct stat st;
383 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100384
385 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100386 fprintf(stderr, "No user found with uid %u\n",
387 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000388 ret = 1;
389 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100390 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100391
Darren Tucker47eede72005-03-14 23:08:12 +1100392 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000393 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100394 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000395 if (stat(buf, &st) < 0)
396 continue;
Damien Miller6e1057c2002-01-22 23:05:59 +1100397 if (do_file(ac, deleting, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000398 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000399 else
400 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000401 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000402 if (count == 0)
403 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000404 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100405 for (i = 0; i < argc; i++) {
Damien Millera055c252002-01-22 23:07:01 +1100406 if (do_file(ac, deleting, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100407 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000408 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409 }
Ben Lindstromee617942001-04-10 02:45:32 +0000410 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000411
412done:
Damien Miller95def091999-11-25 00:26:21 +1100413 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000414 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415}