blob: 8bfc401e8dae3ea230dcae76e9997fe33c93a337 [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"
Damien Millerf17883e2006-03-15 11:45:54 +110038RCSID("$OpenBSD: ssh-add.c,v 1.75 2006/02/20 17:19:54 stevesk Exp $");
39
40#include <sys/types.h>
41#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100042
Damien Millerad833b32000-08-23 10:46:23 +100043#include <openssl/evp.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "rsa.h"
47#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100050#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100051#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000053#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Ben Lindstromddfb1e32001-08-06 22:06:35 +000055/* argv0 */
56extern char *__progname;
57
Damien Miller6e1057c2002-01-22 23:05:59 +110058/* Default files to add */
59static char *default_files[] = {
60 _PATH_SSH_CLIENT_ID_RSA,
61 _PATH_SSH_CLIENT_ID_DSA,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000062 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110063 NULL
64};
65
Ben Lindstrom61d328a2002-06-06 21:54:57 +000066/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000067static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110068
Damien Miller6c711792003-01-24 11:36:23 +110069/* User has to confirm key use */
70static int confirm = 0;
71
Ben Lindstromee617942001-04-10 02:45:32 +000072/* we keep a cache of one passphrases */
73static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000074static void
Ben Lindstromee617942001-04-10 02:45:32 +000075clear_pass(void)
76{
77 if (pass) {
78 memset(pass, 0, strlen(pass));
79 xfree(pass);
80 pass = NULL;
81 }
82}
83
Ben Lindstrom569f88d2001-10-03 17:43:01 +000084static int
Damien Miller01ab4a21999-10-28 15:23:30 +100085delete_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Damien Millereba71ba2000-04-29 23:57:08 +100087 Key *public;
Ben Lindstromd5730a82001-04-08 18:04:36 +000088 char *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +000089 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Ben Lindstromd0fca422001-03-26 13:44:06 +000091 public = key_load_public(filename, &comment);
92 if (public == NULL) {
93 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000094 return -1;
Damien Miller95def091999-11-25 00:26:21 +110095 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +000096 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +110097 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000098 ret = 0;
99 } else
Damien Miller95def091999-11-25 00:26:21 +1100100 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000101
Damien Millereba71ba2000-04-29 23:57:08 +1000102 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100103 xfree(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100104
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000105 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106}
107
Damien Millerad833b32000-08-23 10:46:23 +1000108/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000109static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000110delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000112 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000113
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000114 if (ssh_remove_all_identities(ac, 1))
115 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000116 /* ignore error-code for ssh2 */
117 ssh_remove_all_identities(ac, 2);
118
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000119 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100120 fprintf(stderr, "All identities removed.\n");
121 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000122 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000123
124 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125}
126
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000127static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000128add_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
Damien Millerad833b32000-08-23 10:46:23 +1000130 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000131 Key *private;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000132 char *comment = NULL;
133 char msg[1024];
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000134 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Millerad833b32000-08-23 10:46:23 +1000136 if (stat(filename, &st) < 0) {
137 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000138 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000139 }
Damien Miller95def091999-11-25 00:26:21 +1100140 /* At first, try empty passphrase */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000141 private = key_load_private(filename, "", &comment);
142 if (comment == NULL)
143 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000144 /* try last */
145 if (private == NULL && pass != NULL)
146 private = key_load_private(filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000147 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000148 /* clear passphrase since it did not work */
149 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000150 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000151 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100152 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000153 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100154 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000155 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000156 xfree(comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000157 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100158 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000159 private = key_load_private(filename, pass, &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000160 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100161 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000162 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000163 snprintf(msg, sizeof msg,
164 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100165 }
166 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000167
Damien Millera8e06ce2003-11-21 23:48:55 +1100168 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
169 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000170 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000171 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000172 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000173 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000174 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100175 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100176 fprintf(stderr,
177 "The user has to confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000178 } else if (ssh_add_identity(ac, private, comment)) {
179 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
180 ret = 0;
181 } else {
Damien Miller95def091999-11-25 00:26:21 +1100182 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000183 }
184
Ben Lindstromd0fca422001-03-26 13:44:06 +0000185 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000186 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100187
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000188 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189}
190
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000191static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000192update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000193{
Ben Lindstromba72d302002-03-22 03:51:06 +0000194 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100195 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000196
197 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
198 if (pin == NULL)
199 return -1;
200
Damien Millerd94f20d2003-06-11 22:06:33 +1000201 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000202 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100203 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100204 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000205 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000206 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100207 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100208 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000209 }
Damien Miller00111382003-03-10 11:21:17 +1100210 xfree(pin);
211 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000212}
213
Damien Miller43cba342002-02-05 12:12:49 +1100214static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000215list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216{
Damien Millerad833b32000-08-23 10:46:23 +1000217 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000218 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000219 int had_identities = 0;
220 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Millerad833b32000-08-23 10:46:23 +1000222 for (version = 1; version <= 2; version++) {
223 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100224 key != NULL;
225 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000226 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000227 if (do_fp) {
228 fp = key_fingerprint(key, SSH_FP_MD5,
229 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000231 key_size(key), fp, comment, key_type(key));
232 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100233 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000234 if (!key_write(key, stdout))
235 fprintf(stderr, "key_write failed");
236 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100237 }
Damien Millerad833b32000-08-23 10:46:23 +1000238 key_free(key);
239 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100240 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100241 }
Damien Miller43cba342002-02-05 12:12:49 +1100242 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100243 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100244 return -1;
245 }
246 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247}
248
Damien Miller6e1057c2002-01-22 23:05:59 +1100249static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000250lock_agent(AuthenticationConnection *ac, int lock)
251{
252 char prompt[100], *p1, *p2;
253 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000254
Ben Lindstrom2f717042002-06-06 21:52:03 +0000255 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
256 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
257 if (lock) {
258 strlcpy(prompt, "Again: ", sizeof prompt);
259 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
260 if (strcmp(p1, p2) != 0) {
261 fprintf(stderr, "Passwords do not match.\n");
262 passok = 0;
263 }
264 memset(p2, 0, strlen(p2));
265 xfree(p2);
266 }
267 if (passok && ssh_lock_agent(ac, lock, p1)) {
268 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
269 ret = 0;
270 } else
271 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
272 memset(p1, 0, strlen(p1));
273 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000274 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000275}
276
277static int
Damien Miller6e1057c2002-01-22 23:05:59 +1100278do_file(AuthenticationConnection *ac, int deleting, char *file)
279{
280 if (deleting) {
281 if (delete_file(ac, file) == -1)
282 return -1;
283 } else {
284 if (add_file(ac, file) == -1)
285 return -1;
286 }
287 return 0;
288}
289
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000290static void
291usage(void)
292{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000293 fprintf(stderr, "Usage: %s [options]\n", __progname);
294 fprintf(stderr, "Options:\n");
295 fprintf(stderr, " -l List fingerprints of all identities.\n");
296 fprintf(stderr, " -L List public key parameters of all identities.\n");
297 fprintf(stderr, " -d Delete identity.\n");
298 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000299 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000300 fprintf(stderr, " -X Unlock agent.\n");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000301 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Damien Miller6c711792003-01-24 11:36:23 +1100302 fprintf(stderr, " -c Require confirmation to sign using identities\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000303#ifdef SMARTCARD
304 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
305 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
306#endif
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000307}
308
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309int
Damien Miller01ab4a21999-10-28 15:23:30 +1000310main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000312 extern char *optarg;
313 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100314 AuthenticationConnection *ac = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000315 char *sc_reader_id = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000316 int i, ch, deleting = 0, ret = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Darren Tuckerce321d82005-10-03 18:11:24 +1000318 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
319 sanitise_stdfd();
320
Damien Miller59d3d5b2003-08-22 09:34:41 +1000321 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000322 init_rng();
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000323 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000324
Kevin Stevesef4eea92001-02-05 12:42:17 +0000325 SSLeay_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327 /* At first, get a connection to the authentication agent. */
328 ac = ssh_get_authentication_connection();
329 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100330 fprintf(stderr,
331 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100332 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333 }
Damien Miller6c711792003-01-24 11:36:23 +1100334 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000335 switch (ch) {
336 case 'l':
337 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100338 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
339 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000340 goto done;
341 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000342 case 'x':
343 case 'X':
344 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
345 ret = 1;
346 goto done;
347 break;
Damien Miller6c711792003-01-24 11:36:23 +1100348 case 'c':
349 confirm = 1;
350 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000351 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100352 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000353 break;
354 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000355 if (delete_all(ac) == -1)
356 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000357 goto done;
358 break;
359 case 's':
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000360 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000361 break;
362 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100363 deleting = 1;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000364 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000365 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000366 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000367 if ((lifetime = convtime(optarg)) == -1) {
368 fprintf(stderr, "Invalid lifetime\n");
369 ret = 1;
370 goto done;
371 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000372 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000373 default:
374 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000375 ret = 1;
376 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000377 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000379 argc -= optind;
380 argv += optind;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000381 if (sc_reader_id != NULL) {
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000382 if (update_card(ac, !deleting, sc_reader_id) == -1)
383 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000384 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000385 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000386 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100387 char buf[MAXPATHLEN];
388 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000389 struct stat st;
390 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100391
392 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100393 fprintf(stderr, "No user found with uid %u\n",
394 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000395 ret = 1;
396 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100397 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100398
Darren Tucker47eede72005-03-14 23:08:12 +1100399 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000400 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100401 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000402 if (stat(buf, &st) < 0)
403 continue;
Damien Miller6e1057c2002-01-22 23:05:59 +1100404 if (do_file(ac, deleting, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000405 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000406 else
407 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000408 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000409 if (count == 0)
410 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000411 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100412 for (i = 0; i < argc; i++) {
Damien Millera055c252002-01-22 23:07:01 +1100413 if (do_file(ac, deleting, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100414 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000415 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416 }
Ben Lindstromee617942001-04-10 02:45:32 +0000417 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000418
419done:
Damien Miller95def091999-11-25 00:26:21 +1100420 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000421 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422}