xref: /openssh-portable/ssh-add.c (revision b9dd14d3)
1 /* $OpenBSD: ssh-add.c,v 1.142 2019/10/31 21:19:15 djm Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Adds an identity to the authentication server, or removes an identity.
7  *
8  * 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  *
14  * SSH2 implementation,
15  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
16  *
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.
36  */
37 
38 #include "includes.h"
39 
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 
43 #ifdef WITH_OPENSSL
44 # include <openssl/evp.h>
45 # include "openbsd-compat/openssl-compat.h"
46 #endif
47 
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <pwd.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <limits.h>
57 
58 #include "xmalloc.h"
59 #include "ssh.h"
60 #include "log.h"
61 #include "sshkey.h"
62 #include "sshbuf.h"
63 #include "authfd.h"
64 #include "authfile.h"
65 #include "pathnames.h"
66 #include "misc.h"
67 #include "ssherr.h"
68 #include "digest.h"
69 
70 /* argv0 */
71 extern char *__progname;
72 
73 /* Default files to add */
74 static char *default_files[] = {
75 #ifdef WITH_OPENSSL
76 	_PATH_SSH_CLIENT_ID_RSA,
77 	_PATH_SSH_CLIENT_ID_DSA,
78 #ifdef OPENSSL_HAS_ECC
79 	_PATH_SSH_CLIENT_ID_ECDSA,
80 #endif
81 #endif /* WITH_OPENSSL */
82 	_PATH_SSH_CLIENT_ID_ED25519,
83 	_PATH_SSH_CLIENT_ID_XMSS,
84 	NULL
85 };
86 
87 static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
88 
89 /* Default lifetime (0 == forever) */
90 static int lifetime = 0;
91 
92 /* User has to confirm key use */
93 static int confirm = 0;
94 
95 /* Maximum number of signatures (XMSS) */
96 static u_int maxsign = 0;
97 static u_int minleft = 0;
98 
99 /* we keep a cache of one passphrase */
100 static char *pass = NULL;
101 static void
102 clear_pass(void)
103 {
104 	if (pass) {
105 		explicit_bzero(pass, strlen(pass));
106 		free(pass);
107 		pass = NULL;
108 	}
109 }
110 
111 static int
112 delete_file(int agent_fd, const char *filename, int key_only, int qflag)
113 {
114 	struct sshkey *public, *cert = NULL;
115 	char *certpath = NULL, *comment = NULL;
116 	int r, ret = -1;
117 
118 	if ((r = sshkey_load_public(filename, &public,  &comment)) != 0) {
119 		printf("Bad key file %s: %s\n", filename, ssh_err(r));
120 		return -1;
121 	}
122 	if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
123 		if (!qflag) {
124 			fprintf(stderr, "Identity removed: %s (%s)\n",
125 			    filename, comment);
126 		}
127 		ret = 0;
128 	} else
129 		fprintf(stderr, "Could not remove identity \"%s\": %s\n",
130 		    filename, ssh_err(r));
131 
132 	if (key_only)
133 		goto out;
134 
135 	/* Now try to delete the corresponding certificate too */
136 	free(comment);
137 	comment = NULL;
138 	xasprintf(&certpath, "%s-cert.pub", filename);
139 	if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
140 		if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
141 			error("Failed to load certificate \"%s\": %s",
142 			    certpath, ssh_err(r));
143 		goto out;
144 	}
145 
146 	if (!sshkey_equal_public(cert, public))
147 		fatal("Certificate %s does not match private key %s",
148 		    certpath, filename);
149 
150 	if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
151 		if (!qflag) {
152 			fprintf(stderr, "Identity removed: %s (%s)\n",
153 			    certpath, comment);
154 		}
155 		ret = 0;
156 	} else
157 		fprintf(stderr, "Could not remove identity \"%s\": %s\n",
158 		    certpath, ssh_err(r));
159 
160  out:
161 	sshkey_free(cert);
162 	sshkey_free(public);
163 	free(certpath);
164 	free(comment);
165 
166 	return ret;
167 }
168 
169 /* Send a request to remove all identities. */
170 static int
171 delete_all(int agent_fd, int qflag)
172 {
173 	int ret = -1;
174 
175 	/*
176 	 * Since the agent might be forwarded, old or non-OpenSSH, when asked
177 	 * to remove all keys, attempt to remove both protocol v.1 and v.2
178 	 * keys.
179 	 */
180 	if (ssh_remove_all_identities(agent_fd, 2) == 0)
181 		ret = 0;
182 	/* ignore error-code for ssh1 */
183 	ssh_remove_all_identities(agent_fd, 1);
184 
185 	if (ret != 0)
186 		fprintf(stderr, "Failed to remove all identities.\n");
187 	else if (!qflag)
188 		fprintf(stderr, "All identities removed.\n");
189 
190 	return ret;
191 }
192 
193 static int
194 add_file(int agent_fd, const char *filename, int key_only, int qflag)
195 {
196 	struct sshkey *private, *cert;
197 	char *comment = NULL;
198 	char msg[1024], *certpath = NULL;
199 	int r, fd, ret = -1;
200 	size_t i;
201 	u_int32_t left;
202 	struct sshbuf *keyblob;
203 	struct ssh_identitylist *idlist;
204 
205 	if (strcmp(filename, "-") == 0) {
206 		fd = STDIN_FILENO;
207 		filename = "(stdin)";
208 	} else if ((fd = open(filename, O_RDONLY)) == -1) {
209 		perror(filename);
210 		return -1;
211 	}
212 
213 	/*
214 	 * Since we'll try to load a keyfile multiple times, permission errors
215 	 * will occur multiple times, so check perms first and bail if wrong.
216 	 */
217 	if (fd != STDIN_FILENO) {
218 		if (sshkey_perm_ok(fd, filename) != 0) {
219 			close(fd);
220 			return -1;
221 		}
222 	}
223 	if ((keyblob = sshbuf_new()) == NULL)
224 		fatal("%s: sshbuf_new failed", __func__);
225 	if ((r = sshkey_load_file(fd, keyblob)) != 0) {
226 		fprintf(stderr, "Error loading key \"%s\": %s\n",
227 		    filename, ssh_err(r));
228 		sshbuf_free(keyblob);
229 		close(fd);
230 		return -1;
231 	}
232 	close(fd);
233 
234 	/* At first, try empty passphrase */
235 	if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
236 	    &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
237 		fprintf(stderr, "Error loading key \"%s\": %s\n",
238 		    filename, ssh_err(r));
239 		goto fail_load;
240 	}
241 	/* try last */
242 	if (private == NULL && pass != NULL) {
243 		if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
244 		    &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
245 			fprintf(stderr, "Error loading key \"%s\": %s\n",
246 			    filename, ssh_err(r));
247 			goto fail_load;
248 		}
249 	}
250 	if (private == NULL) {
251 		/* clear passphrase since it did not work */
252 		clear_pass();
253 		snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
254 		    filename, confirm ? " (will confirm each use)" : "");
255 		for (;;) {
256 			pass = read_passphrase(msg, RP_ALLOW_STDIN);
257 			if (strcmp(pass, "") == 0)
258 				goto fail_load;
259 			if ((r = sshkey_parse_private_fileblob(keyblob, pass,
260 			    &private, &comment)) == 0)
261 				break;
262 			else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
263 				fprintf(stderr,
264 				    "Error loading key \"%s\": %s\n",
265 				    filename, ssh_err(r));
266  fail_load:
267 				clear_pass();
268 				sshbuf_free(keyblob);
269 				return -1;
270 			}
271 			clear_pass();
272 			snprintf(msg, sizeof msg,
273 			    "Bad passphrase, try again for %s%s: ", filename,
274 			    confirm ? " (will confirm each use)" : "");
275 		}
276 	}
277 	if (comment == NULL || *comment == '\0')
278 		comment = xstrdup(filename);
279 	sshbuf_free(keyblob);
280 
281 	/* For XMSS */
282 	if ((r = sshkey_set_filename(private, filename)) != 0) {
283 		fprintf(stderr, "Could not add filename to private key: %s (%s)\n",
284 		    filename, comment);
285 		goto out;
286 	}
287 	if (maxsign && minleft &&
288 	    (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) {
289 		for (i = 0; i < idlist->nkeys; i++) {
290 			if (!sshkey_equal_public(idlist->keys[i], private))
291 				continue;
292 			left = sshkey_signatures_left(idlist->keys[i]);
293 			if (left < minleft) {
294 				fprintf(stderr,
295 				    "Only %d signatures left.\n", left);
296 				break;
297 			}
298 			fprintf(stderr, "Skipping update: ");
299 			if (left == minleft) {
300 				fprintf(stderr,
301 				   "required signatures left (%d).\n", left);
302 			} else {
303 				fprintf(stderr,
304 				   "more signatures left (%d) than"
305 				    " required (%d).\n", left, minleft);
306 			}
307 			ssh_free_identitylist(idlist);
308 			goto out;
309 		}
310 		ssh_free_identitylist(idlist);
311 	}
312 
313 	if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
314 	    lifetime, confirm, maxsign, NULL)) == 0) {
315 		ret = 0;
316 		if (!qflag) {
317 			fprintf(stderr, "Identity added: %s (%s)\n",
318 			    filename, comment);
319 			if (lifetime != 0) {
320 				fprintf(stderr,
321 				    "Lifetime set to %d seconds\n", lifetime);
322 			}
323 			if (confirm != 0) {
324 				fprintf(stderr, "The user must confirm "
325 				    "each use of the key\n");
326 			}
327 		}
328 	} else {
329 		fprintf(stderr, "Could not add identity \"%s\": %s\n",
330 		    filename, ssh_err(r));
331 	}
332 
333 	/* Skip trying to load the cert if requested */
334 	if (key_only)
335 		goto out;
336 
337 	/* Now try to add the certificate flavour too */
338 	xasprintf(&certpath, "%s-cert.pub", filename);
339 	if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
340 		if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
341 			error("Failed to load certificate \"%s\": %s",
342 			    certpath, ssh_err(r));
343 		goto out;
344 	}
345 
346 	if (!sshkey_equal_public(cert, private)) {
347 		error("Certificate %s does not match private key %s",
348 		    certpath, filename);
349 		sshkey_free(cert);
350 		goto out;
351 	}
352 
353 	/* Graft with private bits */
354 	if ((r = sshkey_to_certified(private)) != 0) {
355 		error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
356 		sshkey_free(cert);
357 		goto out;
358 	}
359 	if ((r = sshkey_cert_copy(cert, private)) != 0) {
360 		error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
361 		sshkey_free(cert);
362 		goto out;
363 	}
364 	sshkey_free(cert);
365 
366 	if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
367 	    lifetime, confirm, maxsign, NULL)) != 0) {
368 		error("Certificate %s (%s) add failed: %s", certpath,
369 		    private->cert->key_id, ssh_err(r));
370 		goto out;
371 	}
372 	/* success */
373 	if (!qflag) {
374 		fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
375 		    private->cert->key_id);
376 		if (lifetime != 0) {
377 			fprintf(stderr, "Lifetime set to %d seconds\n",
378 			    lifetime);
379 		}
380 		if (confirm != 0) {
381 			fprintf(stderr, "The user must confirm each use "
382 			    "of the key\n");
383 		}
384 	}
385 
386  out:
387 	free(certpath);
388 	free(comment);
389 	sshkey_free(private);
390 
391 	return ret;
392 }
393 
394 static int
395 update_card(int agent_fd, int add, const char *id, int qflag)
396 {
397 	char *pin = NULL;
398 	int r, ret = -1;
399 
400 	if (add) {
401 		if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
402 		    RP_ALLOW_STDIN)) == NULL)
403 			return -1;
404 	}
405 
406 	if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
407 	    lifetime, confirm)) == 0) {
408 		ret = 0;
409 		if (!qflag) {
410 			fprintf(stderr, "Card %s: %s\n",
411 			    add ? "added" : "removed", id);
412 		}
413 	} else {
414 		fprintf(stderr, "Could not %s card \"%s\": %s\n",
415 		    add ? "add" : "remove", id, ssh_err(r));
416 		ret = -1;
417 	}
418 	free(pin);
419 	return ret;
420 }
421 
422 static int
423 test_key(int agent_fd, const char *filename)
424 {
425 	struct sshkey *key = NULL;
426 	u_char *sig = NULL;
427 	size_t slen = 0;
428 	int r, ret = -1;
429 	char data[1024];
430 
431 	if ((r = sshkey_load_public(filename, &key, NULL)) != 0) {
432 		error("Couldn't read public key %s: %s", filename, ssh_err(r));
433 		return -1;
434 	}
435 	arc4random_buf(data, sizeof(data));
436 	if ((r = ssh_agent_sign(agent_fd, key, &sig, &slen, data, sizeof(data),
437 	    NULL, 0)) != 0) {
438 		error("Agent signature failed for %s: %s",
439 		    filename, ssh_err(r));
440 		goto done;
441 	}
442 	if ((r = sshkey_verify(key, sig, slen, data, sizeof(data),
443 	    NULL, 0)) != 0) {
444 		error("Signature verification failed for %s: %s",
445 		    filename, ssh_err(r));
446 		goto done;
447 	}
448 	/* success */
449 	ret = 0;
450  done:
451 	free(sig);
452 	sshkey_free(key);
453 	return ret;
454 }
455 
456 static int
457 list_identities(int agent_fd, int do_fp)
458 {
459 	char *fp;
460 	int r;
461 	struct ssh_identitylist *idlist;
462 	u_int32_t left;
463 	size_t i;
464 
465 	if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
466 		if (r != SSH_ERR_AGENT_NO_IDENTITIES)
467 			fprintf(stderr, "error fetching identities: %s\n",
468 			    ssh_err(r));
469 		else
470 			printf("The agent has no identities.\n");
471 		return -1;
472 	}
473 	for (i = 0; i < idlist->nkeys; i++) {
474 		if (do_fp) {
475 			fp = sshkey_fingerprint(idlist->keys[i],
476 			    fingerprint_hash, SSH_FP_DEFAULT);
477 			printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
478 			    fp == NULL ? "(null)" : fp, idlist->comments[i],
479 			    sshkey_type(idlist->keys[i]));
480 			free(fp);
481 		} else {
482 			if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
483 				fprintf(stderr, "sshkey_write: %s\n",
484 				    ssh_err(r));
485 				continue;
486 			}
487 			fprintf(stdout, " %s", idlist->comments[i]);
488 			left = sshkey_signatures_left(idlist->keys[i]);
489 			if (left > 0)
490 				fprintf(stdout,
491 				    " [signatures left %d]", left);
492 			fprintf(stdout, "\n");
493 		}
494 	}
495 	ssh_free_identitylist(idlist);
496 	return 0;
497 }
498 
499 static int
500 lock_agent(int agent_fd, int lock)
501 {
502 	char prompt[100], *p1, *p2;
503 	int r, passok = 1, ret = -1;
504 
505 	strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
506 	p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
507 	if (lock) {
508 		strlcpy(prompt, "Again: ", sizeof prompt);
509 		p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
510 		if (strcmp(p1, p2) != 0) {
511 			fprintf(stderr, "Passwords do not match.\n");
512 			passok = 0;
513 		}
514 		explicit_bzero(p2, strlen(p2));
515 		free(p2);
516 	}
517 	if (passok) {
518 		if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
519 			fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
520 			ret = 0;
521 		} else {
522 			fprintf(stderr, "Failed to %slock agent: %s\n",
523 			    lock ? "" : "un", ssh_err(r));
524 		}
525 	}
526 	explicit_bzero(p1, strlen(p1));
527 	free(p1);
528 	return (ret);
529 }
530 
531 static int
532 do_file(int agent_fd, int deleting, int key_only, char *file, int qflag)
533 {
534 	if (deleting) {
535 		if (delete_file(agent_fd, file, key_only, qflag) == -1)
536 			return -1;
537 	} else {
538 		if (add_file(agent_fd, file, key_only, qflag) == -1)
539 			return -1;
540 	}
541 	return 0;
542 }
543 
544 static void
545 usage(void)
546 {
547 	fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
548 	fprintf(stderr, "Options:\n");
549 	fprintf(stderr, "  -l          List fingerprints of all identities.\n");
550 	fprintf(stderr, "  -E hash     Specify hash algorithm used for fingerprints.\n");
551 	fprintf(stderr, "  -L          List public key parameters of all identities.\n");
552 	fprintf(stderr, "  -k          Load only keys and not certificates.\n");
553 	fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
554 	fprintf(stderr, "  -m minleft  Maxsign is only changed if less than minleft are left (for XMSS)\n");
555 	fprintf(stderr, "  -M maxsign  Maximum number of signatures allowed (for XMSS)\n");
556 	fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
557 	fprintf(stderr, "  -d          Delete identity.\n");
558 	fprintf(stderr, "  -D          Delete all identities.\n");
559 	fprintf(stderr, "  -x          Lock agent.\n");
560 	fprintf(stderr, "  -X          Unlock agent.\n");
561 	fprintf(stderr, "  -s pkcs11   Add keys from PKCS#11 provider.\n");
562 	fprintf(stderr, "  -e pkcs11   Remove keys provided by PKCS#11 provider.\n");
563 	fprintf(stderr, "  -T pubkey   Test if ssh-agent can access matching private key.\n");
564 	fprintf(stderr, "  -q          Be quiet after a successful operation.\n");
565 	fprintf(stderr, "  -v          Be more verbose.\n");
566 }
567 
568 int
569 main(int argc, char **argv)
570 {
571 	extern char *optarg;
572 	extern int optind;
573 	int agent_fd;
574 	char *pkcs11provider = NULL;
575 	int r, i, ch, deleting = 0, ret = 0, key_only = 0;
576 	int xflag = 0, lflag = 0, Dflag = 0, qflag = 0, Tflag = 0;
577 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
578 	LogLevel log_level = SYSLOG_LEVEL_INFO;
579 
580 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
581 	sanitise_stdfd();
582 
583 	__progname = ssh_get_progname(argv[0]);
584 	seed_rng();
585 
586 	log_init(__progname, log_level, log_facility, 1);
587 
588 	setvbuf(stdout, NULL, _IOLBF, 0);
589 
590 	/* First, get a connection to the authentication agent. */
591 	switch (r = ssh_get_authentication_socket(&agent_fd)) {
592 	case 0:
593 		break;
594 	case SSH_ERR_AGENT_NOT_PRESENT:
595 		fprintf(stderr, "Could not open a connection to your "
596 		    "authentication agent.\n");
597 		exit(2);
598 	default:
599 		fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
600 		exit(2);
601 	}
602 
603 	while ((ch = getopt(argc, argv, "vklLcdDTxXE:e:M:m:qs:t:")) != -1) {
604 		switch (ch) {
605 		case 'v':
606 			if (log_level == SYSLOG_LEVEL_INFO)
607 				log_level = SYSLOG_LEVEL_DEBUG1;
608 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
609 				log_level++;
610 			break;
611 		case 'E':
612 			fingerprint_hash = ssh_digest_alg_by_name(optarg);
613 			if (fingerprint_hash == -1)
614 				fatal("Invalid hash algorithm \"%s\"", optarg);
615 			break;
616 		case 'k':
617 			key_only = 1;
618 			break;
619 		case 'l':
620 		case 'L':
621 			if (lflag != 0)
622 				fatal("-%c flag already specified", lflag);
623 			lflag = ch;
624 			break;
625 		case 'x':
626 		case 'X':
627 			if (xflag != 0)
628 				fatal("-%c flag already specified", xflag);
629 			xflag = ch;
630 			break;
631 		case 'c':
632 			confirm = 1;
633 			break;
634 		case 'm':
635 			minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL);
636 			if (minleft == 0) {
637 				usage();
638 				ret = 1;
639 				goto done;
640 			}
641 			break;
642 		case 'M':
643 			maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL);
644 			if (maxsign == 0) {
645 				usage();
646 				ret = 1;
647 				goto done;
648 			}
649 			break;
650 		case 'd':
651 			deleting = 1;
652 			break;
653 		case 'D':
654 			Dflag = 1;
655 			break;
656 		case 's':
657 			pkcs11provider = optarg;
658 			break;
659 		case 'e':
660 			deleting = 1;
661 			pkcs11provider = optarg;
662 			break;
663 		case 't':
664 			if ((lifetime = convtime(optarg)) == -1) {
665 				fprintf(stderr, "Invalid lifetime\n");
666 				ret = 1;
667 				goto done;
668 			}
669 			break;
670 		case 'q':
671 			qflag = 1;
672 			break;
673 		case 'T':
674 			Tflag = 1;
675 			break;
676 		default:
677 			usage();
678 			ret = 1;
679 			goto done;
680 		}
681 	}
682 	log_init(__progname, log_level, log_facility, 1);
683 
684 	if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
685 		fatal("Invalid combination of actions");
686 	else if (xflag) {
687 		if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
688 			ret = 1;
689 		goto done;
690 	} else if (lflag) {
691 		if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
692 			ret = 1;
693 		goto done;
694 	} else if (Dflag) {
695 		if (delete_all(agent_fd, qflag) == -1)
696 			ret = 1;
697 		goto done;
698 	}
699 
700 	argc -= optind;
701 	argv += optind;
702 	if (Tflag) {
703 		if (argc <= 0)
704 			fatal("no keys to test");
705 		for (r = i = 0; i < argc; i++)
706 			r |= test_key(agent_fd, argv[i]);
707 		ret = r == 0 ? 0 : 1;
708 		goto done;
709 	}
710 	if (pkcs11provider != NULL) {
711 		if (update_card(agent_fd, !deleting, pkcs11provider,
712 		    qflag) == -1)
713 			ret = 1;
714 		goto done;
715 	}
716 	if (argc == 0) {
717 		char buf[PATH_MAX];
718 		struct passwd *pw;
719 		struct stat st;
720 		int count = 0;
721 
722 		if ((pw = getpwuid(getuid())) == NULL) {
723 			fprintf(stderr, "No user found with uid %u\n",
724 			    (u_int)getuid());
725 			ret = 1;
726 			goto done;
727 		}
728 
729 		for (i = 0; default_files[i]; i++) {
730 			snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
731 			    default_files[i]);
732 			if (stat(buf, &st) == -1)
733 				continue;
734 			if (do_file(agent_fd, deleting, key_only, buf,
735 			    qflag) == -1)
736 				ret = 1;
737 			else
738 				count++;
739 		}
740 		if (count == 0)
741 			ret = 1;
742 	} else {
743 		for (i = 0; i < argc; i++) {
744 			if (do_file(agent_fd, deleting, key_only,
745 			    argv[i], qflag) == -1)
746 				ret = 1;
747 		}
748 	}
749 	clear_pass();
750 
751 done:
752 	ssh_close_authentication_socket(agent_fd);
753 	return ret;
754 }
755