blob: f79ffb9156714d3f3a6c0f05c5ebd74bd574bfd5 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: ssh-add.c,v 1.85 2006/07/22 20:48:23 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Adds an identity to the authentication server, or removes an identity.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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 *
Damien Millerad833b32000-08-23 10:46:23 +100014 * SSH2 implementation,
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
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.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110039
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 Miller57cf6382006-07-10 21:13:46 +100045#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100046#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100048#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100049
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "rsa.h"
52#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100055#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100056#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000058#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Ben Lindstromddfb1e32001-08-06 22:06:35 +000060/* argv0 */
61extern char *__progname;
62
Damien Miller6e1057c2002-01-22 23:05:59 +110063/* Default files to add */
64static char *default_files[] = {
65 _PATH_SSH_CLIENT_ID_RSA,
66 _PATH_SSH_CLIENT_ID_DSA,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000067 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110068 NULL
69};
70
Ben Lindstrom61d328a2002-06-06 21:54:57 +000071/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000072static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110073
Damien Miller6c711792003-01-24 11:36:23 +110074/* User has to confirm key use */
75static int confirm = 0;
76
Ben Lindstromee617942001-04-10 02:45:32 +000077/* we keep a cache of one passphrases */
78static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000079static void
Ben Lindstromee617942001-04-10 02:45:32 +000080clear_pass(void)
81{
82 if (pass) {
83 memset(pass, 0, strlen(pass));
84 xfree(pass);
85 pass = NULL;
86 }
87}
88
Ben Lindstrom569f88d2001-10-03 17:43:01 +000089static int
Damien Miller01ab4a21999-10-28 15:23:30 +100090delete_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
Damien Millereba71ba2000-04-29 23:57:08 +100092 Key *public;
Ben Lindstromd5730a82001-04-08 18:04:36 +000093 char *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +000094 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Ben Lindstromd0fca422001-03-26 13:44:06 +000096 public = key_load_public(filename, &comment);
97 if (public == NULL) {
98 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +000099 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100100 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000101 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100102 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000103 ret = 0;
104 } else
Damien Miller95def091999-11-25 00:26:21 +1100105 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000106
Damien Millereba71ba2000-04-29 23:57:08 +1000107 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100108 xfree(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100109
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000110 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111}
112
Damien Millerad833b32000-08-23 10:46:23 +1000113/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000114static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000115delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000117 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000118
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000119 if (ssh_remove_all_identities(ac, 1))
120 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000121 /* ignore error-code for ssh2 */
122 ssh_remove_all_identities(ac, 2);
123
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000124 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100125 fprintf(stderr, "All identities removed.\n");
126 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000127 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000128
129 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130}
131
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000132static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000133add_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134{
Damien Millereba71ba2000-04-29 23:57:08 +1000135 Key *private;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000136 char *comment = NULL;
137 char msg[1024];
Damien Miller8275fad2006-03-15 12:06:23 +1100138 int fd, perms_ok, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller211838d2006-07-10 21:14:00 +1000140 if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000141 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000142 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000143 }
Damien Miller8275fad2006-03-15 12:06:23 +1100144
145 /*
146 * Since we'll try to load a keyfile multiple times, permission errors
147 * will occur multiple times, so check perms first and bail if wrong.
148 */
149 perms_ok = key_perm_ok(fd, filename);
150 close(fd);
151 if (!perms_ok)
152 return -1;
153
Damien Miller95def091999-11-25 00:26:21 +1100154 /* At first, try empty passphrase */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000155 private = key_load_private(filename, "", &comment);
156 if (comment == NULL)
157 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000158 /* try last */
159 if (private == NULL && pass != NULL)
160 private = key_load_private(filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000161 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000162 /* clear passphrase since it did not work */
163 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000164 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000165 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100166 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000167 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100168 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000169 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000170 xfree(comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000171 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100172 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000173 private = key_load_private(filename, pass, &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000174 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100175 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000176 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000177 snprintf(msg, sizeof msg,
178 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100179 }
180 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000181
Damien Millera8e06ce2003-11-21 23:48:55 +1100182 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
183 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000184 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000185 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000186 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000187 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000188 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100189 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100190 fprintf(stderr,
191 "The user has to confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000192 } else if (ssh_add_identity(ac, private, comment)) {
193 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
194 ret = 0;
195 } else {
Damien Miller95def091999-11-25 00:26:21 +1100196 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000197 }
198
Ben Lindstromd0fca422001-03-26 13:44:06 +0000199 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000200 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100201
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000202 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203}
204
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000205static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000206update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000207{
Ben Lindstromba72d302002-03-22 03:51:06 +0000208 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100209 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000210
211 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
212 if (pin == NULL)
213 return -1;
214
Damien Millerd94f20d2003-06-11 22:06:33 +1000215 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000216 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100217 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100218 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000219 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000220 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100221 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100222 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000223 }
Damien Miller00111382003-03-10 11:21:17 +1100224 xfree(pin);
225 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000226}
227
Damien Miller43cba342002-02-05 12:12:49 +1100228static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000229list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230{
Damien Millerad833b32000-08-23 10:46:23 +1000231 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000232 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000233 int had_identities = 0;
234 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Damien Millerad833b32000-08-23 10:46:23 +1000236 for (version = 1; version <= 2; version++) {
237 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238 key != NULL;
239 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000240 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000241 if (do_fp) {
242 fp = key_fingerprint(key, SSH_FP_MD5,
243 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100244 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000245 key_size(key), fp, comment, key_type(key));
246 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100247 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000248 if (!key_write(key, stdout))
249 fprintf(stderr, "key_write failed");
250 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100251 }
Damien Millerad833b32000-08-23 10:46:23 +1000252 key_free(key);
253 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100254 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100255 }
Damien Miller43cba342002-02-05 12:12:49 +1100256 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100257 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100258 return -1;
259 }
260 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261}
262
Damien Miller6e1057c2002-01-22 23:05:59 +1100263static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000264lock_agent(AuthenticationConnection *ac, int lock)
265{
266 char prompt[100], *p1, *p2;
267 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000268
Ben Lindstrom2f717042002-06-06 21:52:03 +0000269 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
270 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
271 if (lock) {
272 strlcpy(prompt, "Again: ", sizeof prompt);
273 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
274 if (strcmp(p1, p2) != 0) {
275 fprintf(stderr, "Passwords do not match.\n");
276 passok = 0;
277 }
278 memset(p2, 0, strlen(p2));
279 xfree(p2);
280 }
281 if (passok && ssh_lock_agent(ac, lock, p1)) {
282 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
283 ret = 0;
284 } else
285 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
286 memset(p1, 0, strlen(p1));
287 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000288 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000289}
290
291static int
Damien Miller6e1057c2002-01-22 23:05:59 +1100292do_file(AuthenticationConnection *ac, int deleting, char *file)
293{
294 if (deleting) {
295 if (delete_file(ac, file) == -1)
296 return -1;
297 } else {
298 if (add_file(ac, file) == -1)
299 return -1;
300 }
301 return 0;
302}
303
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000304static void
305usage(void)
306{
Damien Miller7b1e7572006-06-13 13:03:34 +1000307 fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000308 fprintf(stderr, "Options:\n");
309 fprintf(stderr, " -l List fingerprints of all identities.\n");
310 fprintf(stderr, " -L List public key parameters of all identities.\n");
311 fprintf(stderr, " -d Delete identity.\n");
312 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000313 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000314 fprintf(stderr, " -X Unlock agent.\n");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000315 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Damien Miller6c711792003-01-24 11:36:23 +1100316 fprintf(stderr, " -c Require confirmation to sign using identities\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000317#ifdef SMARTCARD
318 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
319 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
320#endif
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000321}
322
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323int
Damien Miller01ab4a21999-10-28 15:23:30 +1000324main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000326 extern char *optarg;
327 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100328 AuthenticationConnection *ac = NULL;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000329 char *sc_reader_id = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000330 int i, ch, deleting = 0, ret = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Darren Tuckerce321d82005-10-03 18:11:24 +1000332 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
333 sanitise_stdfd();
334
Damien Miller59d3d5b2003-08-22 09:34:41 +1000335 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000336 init_rng();
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000337 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000338
Kevin Stevesef4eea92001-02-05 12:42:17 +0000339 SSLeay_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000340
Damien Miller95def091999-11-25 00:26:21 +1100341 /* At first, get a connection to the authentication agent. */
342 ac = ssh_get_authentication_connection();
343 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100344 fprintf(stderr,
345 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100346 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347 }
Damien Miller6c711792003-01-24 11:36:23 +1100348 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000349 switch (ch) {
350 case 'l':
351 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100352 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
353 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000354 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000355 case 'x':
356 case 'X':
357 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
358 ret = 1;
359 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100360 case 'c':
361 confirm = 1;
362 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000363 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100364 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000365 break;
366 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000367 if (delete_all(ac) == -1)
368 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000369 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000370 case 's':
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000371 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000372 break;
373 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100374 deleting = 1;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000375 sc_reader_id = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000376 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000377 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000378 if ((lifetime = convtime(optarg)) == -1) {
379 fprintf(stderr, "Invalid lifetime\n");
380 ret = 1;
381 goto done;
382 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000383 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000384 default:
385 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000386 ret = 1;
387 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000388 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000390 argc -= optind;
391 argv += optind;
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000392 if (sc_reader_id != NULL) {
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000393 if (update_card(ac, !deleting, sc_reader_id) == -1)
394 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000395 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000396 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000397 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100398 char buf[MAXPATHLEN];
399 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000400 struct stat st;
401 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100402
403 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100404 fprintf(stderr, "No user found with uid %u\n",
405 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000406 ret = 1;
407 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100408 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100409
Darren Tucker47eede72005-03-14 23:08:12 +1100410 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000411 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100412 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000413 if (stat(buf, &st) < 0)
414 continue;
Damien Miller6e1057c2002-01-22 23:05:59 +1100415 if (do_file(ac, deleting, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000416 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000417 else
418 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000419 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000420 if (count == 0)
421 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000422 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100423 for (i = 0; i < argc; i++) {
Damien Millera055c252002-01-22 23:07:01 +1100424 if (do_file(ac, deleting, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100425 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000426 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427 }
Ben Lindstromee617942001-04-10 02:45:32 +0000428 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000429
430done:
Damien Miller95def091999-11-25 00:26:21 +1100431 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000432 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433}