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