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