Deleted Added
full compact
ssh-add.c (106130) ssh-add.c (113911)
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Adds an identity to the authentication server, or removes an identity.
6 *
7 * 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 *
13 * SSH2 implementation,
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
15 *
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.
35 */
36
37#include "includes.h"
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Adds an identity to the authentication server, or removes an identity.
6 *
7 * 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 *
13 * SSH2 implementation,
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
15 *
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.
35 */
36
37#include "includes.h"
38RCSID("$OpenBSD: ssh-add.c,v 1.63 2002/09/19 15:51:23 markus Exp $");
39RCSID("$FreeBSD: head/crypto/openssh/ssh-add.c 106130 2002-10-29 10:16:02Z des $");
38RCSID("$OpenBSD: ssh-add.c,v 1.66 2003/03/05 22:33:43 markus Exp $");
39RCSID("$FreeBSD: head/crypto/openssh/ssh-add.c 113911 2003-04-23 17:13:13Z des $");
40
41#include <openssl/evp.h>
42
43#include "ssh.h"
44#include "rsa.h"
45#include "log.h"
46#include "xmalloc.h"
47#include "key.h"
48#include "authfd.h"
49#include "authfile.h"
50#include "pathnames.h"
51#include "readpass.h"
52#include "misc.h"
53
54#ifdef HAVE___PROGNAME
55extern char *__progname;
56#else
57char *__progname;
58#endif
59
60/* argv0 */
61extern char *__progname;
62
63/* Default files to add */
64static char *default_files[] = {
65 _PATH_SSH_CLIENT_ID_RSA,
66 _PATH_SSH_CLIENT_ID_DSA,
67 _PATH_SSH_CLIENT_IDENTITY,
68 NULL
69};
70
71/* Default lifetime (0 == forever) */
72static int lifetime = 0;
73
40
41#include <openssl/evp.h>
42
43#include "ssh.h"
44#include "rsa.h"
45#include "log.h"
46#include "xmalloc.h"
47#include "key.h"
48#include "authfd.h"
49#include "authfile.h"
50#include "pathnames.h"
51#include "readpass.h"
52#include "misc.h"
53
54#ifdef HAVE___PROGNAME
55extern char *__progname;
56#else
57char *__progname;
58#endif
59
60/* argv0 */
61extern char *__progname;
62
63/* Default files to add */
64static char *default_files[] = {
65 _PATH_SSH_CLIENT_ID_RSA,
66 _PATH_SSH_CLIENT_ID_DSA,
67 _PATH_SSH_CLIENT_IDENTITY,
68 NULL
69};
70
71/* Default lifetime (0 == forever) */
72static int lifetime = 0;
73
74/* User has to confirm key use */
75static int confirm = 0;
76
74/* we keep a cache of one passphrases */
75static char *pass = NULL;
76static void
77clear_pass(void)
78{
79 if (pass) {
80 memset(pass, 0, strlen(pass));
81 xfree(pass);
82 pass = NULL;
83 }
84}
85
86static int
87delete_file(AuthenticationConnection *ac, const char *filename)
88{
89 Key *public;
90 char *comment = NULL;
91 int ret = -1;
92
93 public = key_load_public(filename, &comment);
94 if (public == NULL) {
95 printf("Bad key file %s\n", filename);
96 return -1;
97 }
98 if (ssh_remove_identity(ac, public)) {
99 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
100 ret = 0;
101 } else
102 fprintf(stderr, "Could not remove identity: %s\n", filename);
103
104 key_free(public);
105 xfree(comment);
106
107 return ret;
108}
109
110/* Send a request to remove all identities. */
111static int
112delete_all(AuthenticationConnection *ac)
113{
114 int ret = -1;
115
116 if (ssh_remove_all_identities(ac, 1))
117 ret = 0;
118 /* ignore error-code for ssh2 */
119 ssh_remove_all_identities(ac, 2);
120
121 if (ret == 0)
122 fprintf(stderr, "All identities removed.\n");
123 else
124 fprintf(stderr, "Failed to remove all identities.\n");
125
126 return ret;
127}
128
129static int
130add_file(AuthenticationConnection *ac, const char *filename)
131{
132 struct stat st;
133 Key *private;
134 char *comment = NULL;
135 char msg[1024];
136 int ret = -1;
137
138 if (stat(filename, &st) < 0) {
139 perror(filename);
140 return -1;
141 }
142 /* At first, try empty passphrase */
143 private = key_load_private(filename, "", &comment);
144 if (comment == NULL)
145 comment = xstrdup(filename);
146 /* try last */
147 if (private == NULL && pass != NULL)
148 private = key_load_private(filename, pass, NULL);
149 if (private == NULL) {
150 /* clear passphrase since it did not work */
151 clear_pass();
152 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
153 comment);
154 for (;;) {
155 pass = read_passphrase(msg, RP_ALLOW_STDIN);
156 if (strcmp(pass, "") == 0) {
157 clear_pass();
158 xfree(comment);
159 return -1;
160 }
161 private = key_load_private(filename, pass, &comment);
162 if (private != NULL)
163 break;
164 clear_pass();
165 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
166 }
167 }
168
77/* we keep a cache of one passphrases */
78static char *pass = NULL;
79static void
80clear_pass(void)
81{
82 if (pass) {
83 memset(pass, 0, strlen(pass));
84 xfree(pass);
85 pass = NULL;
86 }
87}
88
89static int
90delete_file(AuthenticationConnection *ac, const char *filename)
91{
92 Key *public;
93 char *comment = NULL;
94 int ret = -1;
95
96 public = key_load_public(filename, &comment);
97 if (public == NULL) {
98 printf("Bad key file %s\n", filename);
99 return -1;
100 }
101 if (ssh_remove_identity(ac, public)) {
102 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
103 ret = 0;
104 } else
105 fprintf(stderr, "Could not remove identity: %s\n", filename);
106
107 key_free(public);
108 xfree(comment);
109
110 return ret;
111}
112
113/* Send a request to remove all identities. */
114static int
115delete_all(AuthenticationConnection *ac)
116{
117 int ret = -1;
118
119 if (ssh_remove_all_identities(ac, 1))
120 ret = 0;
121 /* ignore error-code for ssh2 */
122 ssh_remove_all_identities(ac, 2);
123
124 if (ret == 0)
125 fprintf(stderr, "All identities removed.\n");
126 else
127 fprintf(stderr, "Failed to remove all identities.\n");
128
129 return ret;
130}
131
132static int
133add_file(AuthenticationConnection *ac, const char *filename)
134{
135 struct stat st;
136 Key *private;
137 char *comment = NULL;
138 char msg[1024];
139 int ret = -1;
140
141 if (stat(filename, &st) < 0) {
142 perror(filename);
143 return -1;
144 }
145 /* At first, try empty passphrase */
146 private = key_load_private(filename, "", &comment);
147 if (comment == NULL)
148 comment = xstrdup(filename);
149 /* try last */
150 if (private == NULL && pass != NULL)
151 private = key_load_private(filename, pass, NULL);
152 if (private == NULL) {
153 /* clear passphrase since it did not work */
154 clear_pass();
155 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
156 comment);
157 for (;;) {
158 pass = read_passphrase(msg, RP_ALLOW_STDIN);
159 if (strcmp(pass, "") == 0) {
160 clear_pass();
161 xfree(comment);
162 return -1;
163 }
164 private = key_load_private(filename, pass, &comment);
165 if (private != NULL)
166 break;
167 clear_pass();
168 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
169 }
170 }
171
169 if (ssh_add_identity_constrained(ac, private, comment, lifetime)) {
172 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
173 confirm)) {
170 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
171 ret = 0;
172 if (lifetime != 0)
174 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
175 ret = 0;
176 if (lifetime != 0)
173 fprintf(stderr,
177 fprintf(stderr,
174 "Lifetime set to %d seconds\n", lifetime);
178 "Lifetime set to %d seconds\n", lifetime);
179 if (confirm != 0)
180 fprintf(stderr,
181 "The user has to confirm each use of the key\n");
175 } else if (ssh_add_identity(ac, private, comment)) {
176 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
177 ret = 0;
178 } else {
179 fprintf(stderr, "Could not add identity: %s\n", filename);
180 }
181
182 xfree(comment);
183 key_free(private);
184
185 return ret;
186}
187
188static int
189update_card(AuthenticationConnection *ac, int add, const char *id)
190{
191 char *pin;
182 } else if (ssh_add_identity(ac, private, comment)) {
183 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
184 ret = 0;
185 } else {
186 fprintf(stderr, "Could not add identity: %s\n", filename);
187 }
188
189 xfree(comment);
190 key_free(private);
191
192 return ret;
193}
194
195static int
196update_card(AuthenticationConnection *ac, int add, const char *id)
197{
198 char *pin;
199 int ret = -1;
192
193 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
194 if (pin == NULL)
195 return -1;
196
197 if (ssh_update_card(ac, add, id, pin)) {
198 fprintf(stderr, "Card %s: %s\n",
199 add ? "added" : "removed", id);
200
201 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
202 if (pin == NULL)
203 return -1;
204
205 if (ssh_update_card(ac, add, id, pin)) {
206 fprintf(stderr, "Card %s: %s\n",
207 add ? "added" : "removed", id);
200 return 0;
208 ret = 0;
201 } else {
202 fprintf(stderr, "Could not %s card: %s\n",
203 add ? "add" : "remove", id);
209 } else {
210 fprintf(stderr, "Could not %s card: %s\n",
211 add ? "add" : "remove", id);
204 return -1;
212 ret = -1;
205 }
213 }
214 xfree(pin);
215 return ret;
206}
207
208static int
209list_identities(AuthenticationConnection *ac, int do_fp)
210{
211 Key *key;
212 char *comment, *fp;
213 int had_identities = 0;
214 int version;
215
216 for (version = 1; version <= 2; version++) {
217 for (key = ssh_get_first_identity(ac, &comment, version);
218 key != NULL;
219 key = ssh_get_next_identity(ac, &comment, version)) {
220 had_identities = 1;
221 if (do_fp) {
222 fp = key_fingerprint(key, SSH_FP_MD5,
223 SSH_FP_HEX);
224 printf("%d %s %s (%s)\n",
225 key_size(key), fp, comment, key_type(key));
226 xfree(fp);
227 } else {
228 if (!key_write(key, stdout))
229 fprintf(stderr, "key_write failed");
230 fprintf(stdout, " %s\n", comment);
231 }
232 key_free(key);
233 xfree(comment);
234 }
235 }
236 if (!had_identities) {
237 printf("The agent has no identities.\n");
238 return -1;
239 }
240 return 0;
241}
242
243static int
244lock_agent(AuthenticationConnection *ac, int lock)
245{
246 char prompt[100], *p1, *p2;
247 int passok = 1, ret = -1;
248
249 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
250 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
251 if (lock) {
252 strlcpy(prompt, "Again: ", sizeof prompt);
253 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
254 if (strcmp(p1, p2) != 0) {
255 fprintf(stderr, "Passwords do not match.\n");
256 passok = 0;
257 }
258 memset(p2, 0, strlen(p2));
259 xfree(p2);
260 }
261 if (passok && ssh_lock_agent(ac, lock, p1)) {
262 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
263 ret = 0;
264 } else
265 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
266 memset(p1, 0, strlen(p1));
267 xfree(p1);
268 return (ret);
269}
270
271static int
272do_file(AuthenticationConnection *ac, int deleting, char *file)
273{
274 if (deleting) {
275 if (delete_file(ac, file) == -1)
276 return -1;
277 } else {
278 if (add_file(ac, file) == -1)
279 return -1;
280 }
281 return 0;
282}
283
284static void
285usage(void)
286{
287 fprintf(stderr, "Usage: %s [options]\n", __progname);
288 fprintf(stderr, "Options:\n");
289 fprintf(stderr, " -l List fingerprints of all identities.\n");
290 fprintf(stderr, " -L List public key parameters of all identities.\n");
291 fprintf(stderr, " -d Delete identity.\n");
292 fprintf(stderr, " -D Delete all identities.\n");
293 fprintf(stderr, " -x Lock agent.\n");
294 fprintf(stderr, " -X Unlock agent.\n");
295 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
216}
217
218static int
219list_identities(AuthenticationConnection *ac, int do_fp)
220{
221 Key *key;
222 char *comment, *fp;
223 int had_identities = 0;
224 int version;
225
226 for (version = 1; version <= 2; version++) {
227 for (key = ssh_get_first_identity(ac, &comment, version);
228 key != NULL;
229 key = ssh_get_next_identity(ac, &comment, version)) {
230 had_identities = 1;
231 if (do_fp) {
232 fp = key_fingerprint(key, SSH_FP_MD5,
233 SSH_FP_HEX);
234 printf("%d %s %s (%s)\n",
235 key_size(key), fp, comment, key_type(key));
236 xfree(fp);
237 } else {
238 if (!key_write(key, stdout))
239 fprintf(stderr, "key_write failed");
240 fprintf(stdout, " %s\n", comment);
241 }
242 key_free(key);
243 xfree(comment);
244 }
245 }
246 if (!had_identities) {
247 printf("The agent has no identities.\n");
248 return -1;
249 }
250 return 0;
251}
252
253static int
254lock_agent(AuthenticationConnection *ac, int lock)
255{
256 char prompt[100], *p1, *p2;
257 int passok = 1, ret = -1;
258
259 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
260 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
261 if (lock) {
262 strlcpy(prompt, "Again: ", sizeof prompt);
263 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
264 if (strcmp(p1, p2) != 0) {
265 fprintf(stderr, "Passwords do not match.\n");
266 passok = 0;
267 }
268 memset(p2, 0, strlen(p2));
269 xfree(p2);
270 }
271 if (passok && ssh_lock_agent(ac, lock, p1)) {
272 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
273 ret = 0;
274 } else
275 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
276 memset(p1, 0, strlen(p1));
277 xfree(p1);
278 return (ret);
279}
280
281static int
282do_file(AuthenticationConnection *ac, int deleting, char *file)
283{
284 if (deleting) {
285 if (delete_file(ac, file) == -1)
286 return -1;
287 } else {
288 if (add_file(ac, file) == -1)
289 return -1;
290 }
291 return 0;
292}
293
294static void
295usage(void)
296{
297 fprintf(stderr, "Usage: %s [options]\n", __progname);
298 fprintf(stderr, "Options:\n");
299 fprintf(stderr, " -l List fingerprints of all identities.\n");
300 fprintf(stderr, " -L List public key parameters of all identities.\n");
301 fprintf(stderr, " -d Delete identity.\n");
302 fprintf(stderr, " -D Delete all identities.\n");
303 fprintf(stderr, " -x Lock agent.\n");
304 fprintf(stderr, " -X Unlock agent.\n");
305 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
306 fprintf(stderr, " -c Require confirmation to sign using identities\n");
296#ifdef SMARTCARD
297 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
298 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
299#endif
300}
301
302int
303main(int argc, char **argv)
304{
305 extern char *optarg;
306 extern int optind;
307 AuthenticationConnection *ac = NULL;
308 char *sc_reader_id = NULL;
309 int i, ch, deleting = 0, ret = 0;
310
311 __progname = get_progname(argv[0]);
312 init_rng();
313 seed_rng();
314
315 SSLeay_add_all_algorithms();
316
317 /* At first, get a connection to the authentication agent. */
318 ac = ssh_get_authentication_connection();
319 if (ac == NULL) {
320 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
321 exit(2);
322 }
307#ifdef SMARTCARD
308 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
309 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
310#endif
311}
312
313int
314main(int argc, char **argv)
315{
316 extern char *optarg;
317 extern int optind;
318 AuthenticationConnection *ac = NULL;
319 char *sc_reader_id = NULL;
320 int i, ch, deleting = 0, ret = 0;
321
322 __progname = get_progname(argv[0]);
323 init_rng();
324 seed_rng();
325
326 SSLeay_add_all_algorithms();
327
328 /* At first, get a connection to the authentication agent. */
329 ac = ssh_get_authentication_connection();
330 if (ac == NULL) {
331 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
332 exit(2);
333 }
323 while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
334 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
324 switch (ch) {
325 case 'l':
326 case 'L':
327 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
328 ret = 1;
329 goto done;
330 break;
331 case 'x':
332 case 'X':
333 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
334 ret = 1;
335 goto done;
336 break;
335 switch (ch) {
336 case 'l':
337 case 'L':
338 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
339 ret = 1;
340 goto done;
341 break;
342 case 'x':
343 case 'X':
344 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
345 ret = 1;
346 goto done;
347 break;
348 case 'c':
349 confirm = 1;
350 break;
337 case 'd':
338 deleting = 1;
339 break;
340 case 'D':
341 if (delete_all(ac) == -1)
342 ret = 1;
343 goto done;
344 break;
345 case 's':
346 sc_reader_id = optarg;
347 break;
348 case 'e':
349 deleting = 1;
350 sc_reader_id = optarg;
351 break;
352 case 't':
353 if ((lifetime = convtime(optarg)) == -1) {
354 fprintf(stderr, "Invalid lifetime\n");
355 ret = 1;
356 goto done;
357 }
358 break;
359 default:
360 usage();
361 ret = 1;
362 goto done;
363 }
364 }
365 argc -= optind;
366 argv += optind;
367 if (sc_reader_id != NULL) {
368 if (update_card(ac, !deleting, sc_reader_id) == -1)
369 ret = 1;
370 goto done;
371 }
372 if (argc == 0) {
373 char buf[MAXPATHLEN];
374 struct passwd *pw;
375 struct stat st;
376 int count = 0;
377
378 if ((pw = getpwuid(getuid())) == NULL) {
379 fprintf(stderr, "No user found with uid %u\n",
380 (u_int)getuid());
381 ret = 1;
382 goto done;
383 }
384
385 for(i = 0; default_files[i]; i++) {
386 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
387 default_files[i]);
388 if (stat(buf, &st) < 0)
389 continue;
390 if (do_file(ac, deleting, buf) == -1)
391 ret = 1;
392 else
393 count++;
394 }
395 if (count == 0)
396 ret = 1;
397 } else {
398 for(i = 0; i < argc; i++) {
399 if (do_file(ac, deleting, argv[i]) == -1)
400 ret = 1;
401 }
402 }
403 clear_pass();
404
405done:
406 ssh_close_authentication_connection(ac);
407 return ret;
408}
351 case 'd':
352 deleting = 1;
353 break;
354 case 'D':
355 if (delete_all(ac) == -1)
356 ret = 1;
357 goto done;
358 break;
359 case 's':
360 sc_reader_id = optarg;
361 break;
362 case 'e':
363 deleting = 1;
364 sc_reader_id = optarg;
365 break;
366 case 't':
367 if ((lifetime = convtime(optarg)) == -1) {
368 fprintf(stderr, "Invalid lifetime\n");
369 ret = 1;
370 goto done;
371 }
372 break;
373 default:
374 usage();
375 ret = 1;
376 goto done;
377 }
378 }
379 argc -= optind;
380 argv += optind;
381 if (sc_reader_id != NULL) {
382 if (update_card(ac, !deleting, sc_reader_id) == -1)
383 ret = 1;
384 goto done;
385 }
386 if (argc == 0) {
387 char buf[MAXPATHLEN];
388 struct passwd *pw;
389 struct stat st;
390 int count = 0;
391
392 if ((pw = getpwuid(getuid())) == NULL) {
393 fprintf(stderr, "No user found with uid %u\n",
394 (u_int)getuid());
395 ret = 1;
396 goto done;
397 }
398
399 for(i = 0; default_files[i]; i++) {
400 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
401 default_files[i]);
402 if (stat(buf, &st) < 0)
403 continue;
404 if (do_file(ac, deleting, buf) == -1)
405 ret = 1;
406 else
407 count++;
408 }
409 if (count == 0)
410 ret = 1;
411 } else {
412 for(i = 0; i < argc; i++) {
413 if (do_file(ac, deleting, argv[i]) == -1)
414 ret = 1;
415 }
416 }
417 clear_pass();
418
419done:
420 ssh_close_authentication_connection(ac);
421 return ret;
422}