blob: 4c5cc57277769b6914790d49b7e9ff112622137c [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 +110038
39#include <sys/types.h>
40#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100041
Damien Millerad833b32000-08-23 10:46:23 +100042#include <openssl/evp.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "rsa.h"
46#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100049#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000052#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Ben Lindstromddfb1e32001-08-06 22:06:35 +000054/* argv0 */
55extern char *__progname;
56
Damien Miller6e1057c2002-01-22 23:05:59 +110057/* Default files to add */
58static char *default_files[] = {
59 _PATH_SSH_CLIENT_ID_RSA,
60 _PATH_SSH_CLIENT_ID_DSA,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000061 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110062 NULL
63};
64
Ben Lindstrom61d328a2002-06-06 21:54:57 +000065/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000066static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110067
Damien Miller6c711792003-01-24 11:36:23 +110068/* User has to confirm key use */
69static int confirm = 0;
70
Ben Lindstromee617942001-04-10 02:45:32 +000071/* we keep a cache of one passphrases */
72static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000073static void
Ben Lindstromee617942001-04-10 02:45:32 +000074clear_pass(void)
75{
76 if (pass) {
77 memset(pass, 0, strlen(pass));
78 xfree(pass);
79 pass = NULL;
80 }
81}
82
Ben Lindstrom569f88d2001-10-03 17:43:01 +000083static int
Damien Miller01ab4a21999-10-28 15:23:30 +100084delete_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085{
Damien Millereba71ba2000-04-29 23:57:08 +100086 Key *public;
Ben Lindstromd5730a82001-04-08 18:04:36 +000087 char *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +000088 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Ben Lindstromd0fca422001-03-26 13:44:06 +000090 public = key_load_public(filename, &comment);
91 if (public == NULL) {
92 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000093 return -1;
Damien Miller95def091999-11-25 00:26:21 +110094 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +000095 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +110096 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000097 ret = 0;
98 } else
Damien Miller95def091999-11-25 00:26:21 +110099 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000100
Damien Millereba71ba2000-04-29 23:57:08 +1000101 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100102 xfree(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100103
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000104 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105}
106
Damien Millerad833b32000-08-23 10:46:23 +1000107/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000108static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000109delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000111 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000112
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000113 if (ssh_remove_all_identities(ac, 1))
114 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000115 /* ignore error-code for ssh2 */
116 ssh_remove_all_identities(ac, 2);
117
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000118 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100119 fprintf(stderr, "All identities removed.\n");
120 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000121 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000122
123 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124}
125
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000126static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000127add_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128{
Damien Millereba71ba2000-04-29 23:57:08 +1000129 Key *private;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000130 char *comment = NULL;
131 char msg[1024];
Damien Miller8275fad2006-03-15 12:06:23 +1100132 int fd, perms_ok, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller8275fad2006-03-15 12:06:23 +1100134 if ((fd = open(filename, 0)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000135 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000136 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000137 }
Damien Miller8275fad2006-03-15 12:06:23 +1100138
139 /*
140 * Since we'll try to load a keyfile multiple times, permission errors
141 * will occur multiple times, so check perms first and bail if wrong.
142 */
143 perms_ok = key_perm_ok(fd, filename);
144 close(fd);
145 if (!perms_ok)
146 return -1;
147
Damien Miller95def091999-11-25 00:26:21 +1100148 /* At first, try empty passphrase */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000149 private = key_load_private(filename, "", &comment);
150 if (comment == NULL)
151 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000152 /* try last */
153 if (private == NULL && pass != NULL)
154 private = key_load_private(filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000155 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000156 /* clear passphrase since it did not work */
157 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000158 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000159 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100160 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000161 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100162 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000163 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000164 xfree(comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000165 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100166 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000167 private = key_load_private(filename, pass, &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000168 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100169 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000170 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000171 snprintf(msg, sizeof msg,
172 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100173 }
174 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000175
Damien Millera8e06ce2003-11-21 23:48:55 +1100176 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
177 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000178 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000179 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000180 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000181 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000182 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100183 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100184 fprintf(stderr,
185 "The user has to confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000186 } else if (ssh_add_identity(ac, private, comment)) {
187 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
188 ret = 0;
189 } else {
Damien Miller95def091999-11-25 00:26:21 +1100190 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000191 }
192
Ben Lindstromd0fca422001-03-26 13:44:06 +0000193 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000194 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100195
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000196 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197}
198
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000199static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000200update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000201{
Ben Lindstromba72d302002-03-22 03:51:06 +0000202 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100203 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000204
205 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
206 if (pin == NULL)
207 return -1;
208
Damien Millerd94f20d2003-06-11 22:06:33 +1000209 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000210 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100211 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100212 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000213 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000214 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100215 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100216 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000217 }
Damien Miller00111382003-03-10 11:21:17 +1100218 xfree(pin);
219 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000220}
221
Damien Miller43cba342002-02-05 12:12:49 +1100222static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000223list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224{
Damien Millerad833b32000-08-23 10:46:23 +1000225 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000226 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000227 int had_identities = 0;
228 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Damien Millerad833b32000-08-23 10:46:23 +1000230 for (version = 1; version <= 2; version++) {
231 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100232 key != NULL;
233 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000234 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000235 if (do_fp) {
236 fp = key_fingerprint(key, SSH_FP_MD5,
237 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100238 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000239 key_size(key), fp, comment, key_type(key));
240 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100241 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000242 if (!key_write(key, stdout))
243 fprintf(stderr, "key_write failed");
244 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100245 }
Damien Millerad833b32000-08-23 10:46:23 +1000246 key_free(key);
247 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100248 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100249 }
Damien Miller43cba342002-02-05 12:12:49 +1100250 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100251 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100252 return -1;
253 }
254 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255}
256
Damien Miller6e1057c2002-01-22 23:05:59 +1100257static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000258lock_agent(AuthenticationConnection *ac, int lock)
259{
260 char prompt[100], *p1, *p2;
261 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000262
Ben Lindstrom2f717042002-06-06 21:52:03 +0000263 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
264 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
265 if (lock) {
266 strlcpy(prompt, "Again: ", sizeof prompt);
267 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
268 if (strcmp(p1, p2) != 0) {
269 fprintf(stderr, "Passwords do not match.\n");
270 passok = 0;
271 }
272 memset(p2, 0, strlen(p2));
273 xfree(p2);
274 }
275 if (passok && ssh_lock_agent(ac, lock, p1)) {
276 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
277 ret = 0;
278 } else
279 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
280 memset(p1, 0, strlen(p1));
281 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000282 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000283}
284
285static int
Damien Miller6e1057c2002-01-22 23:05:59 +1100286do_file(AuthenticationConnection *ac, int deleting, char *file)
287{
288 if (deleting) {
289 if (delete_file(ac, file) == -1)
290 return -1;
291 } else {
292 if (add_file(ac, file) == -1)
293 return -1;
294 }
295 return 0;
296}
297
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000298static void
299usage(void)
300{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000301 fprintf(stderr, "Usage: %s [options]\n", __progname);
302 fprintf(stderr, "Options:\n");
303 fprintf(stderr, " -l List fingerprints of all identities.\n");
304 fprintf(stderr, " -L List public key parameters of all identities.\n");
305 fprintf(stderr, " -d Delete identity.\n");
306 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000307 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000308 fprintf(stderr, " -X Unlock agent.\n");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000309 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Damien Miller6c711792003-01-24 11:36:23 +1100310 fprintf(stderr, " -c Require confirmation to sign using identities\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000311#ifdef SMARTCARD
312 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
313 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
314#endif
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000315}
316
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317int
Damien Miller01ab4a21999-10-28 15:23:30 +1000318main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000320 extern char *optarg;
321 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100322 AuthenticationConnection *ac = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000323 char *sc_reader_id = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000324 int i, ch, deleting = 0, ret = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Darren Tuckerce321d82005-10-03 18:11:24 +1000326 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
327 sanitise_stdfd();
328
Damien Miller59d3d5b2003-08-22 09:34:41 +1000329 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000330 init_rng();
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000331 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000332
Kevin Stevesef4eea92001-02-05 12:42:17 +0000333 SSLeay_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000334
Damien Miller95def091999-11-25 00:26:21 +1100335 /* At first, get a connection to the authentication agent. */
336 ac = ssh_get_authentication_connection();
337 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100338 fprintf(stderr,
339 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100340 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341 }
Damien Miller6c711792003-01-24 11:36:23 +1100342 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000343 switch (ch) {
344 case 'l':
345 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100346 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
347 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000348 goto done;
349 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000350 case 'x':
351 case 'X':
352 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
353 ret = 1;
354 goto done;
355 break;
Damien Miller6c711792003-01-24 11:36:23 +1100356 case 'c':
357 confirm = 1;
358 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000359 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100360 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000361 break;
362 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000363 if (delete_all(ac) == -1)
364 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000365 goto done;
366 break;
367 case 's':
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000368 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000369 break;
370 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100371 deleting = 1;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000372 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000373 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000374 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000375 if ((lifetime = convtime(optarg)) == -1) {
376 fprintf(stderr, "Invalid lifetime\n");
377 ret = 1;
378 goto done;
379 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000380 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000381 default:
382 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000383 ret = 1;
384 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000385 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000387 argc -= optind;
388 argv += optind;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000389 if (sc_reader_id != NULL) {
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000390 if (update_card(ac, !deleting, sc_reader_id) == -1)
391 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000392 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000393 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000394 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100395 char buf[MAXPATHLEN];
396 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000397 struct stat st;
398 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100399
400 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100401 fprintf(stderr, "No user found with uid %u\n",
402 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000403 ret = 1;
404 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100405 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100406
Darren Tucker47eede72005-03-14 23:08:12 +1100407 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000408 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100409 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000410 if (stat(buf, &st) < 0)
411 continue;
Damien Miller6e1057c2002-01-22 23:05:59 +1100412 if (do_file(ac, deleting, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000413 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000414 else
415 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000416 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000417 if (count == 0)
418 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000419 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100420 for (i = 0; i < argc; i++) {
Damien Millera055c252002-01-22 23:07:01 +1100421 if (do_file(ac, deleting, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100422 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000423 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424 }
Ben Lindstromee617942001-04-10 02:45:32 +0000425 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000426
427done:
Damien Miller95def091999-11-25 00:26:21 +1100428 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000429 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430}