xref: /openssh-portable/ssh-add.c (revision 264a9662)
1 /* $OpenBSD: ssh-add.c,v 1.153 2020/02/18 08:58:33 dtucker 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 #include "ssh-sk.h"
70 
71 /* argv0 */
72 extern char *__progname;
73 
74 /* Default files to add */
75 static char *default_files[] = {
76 #ifdef WITH_OPENSSL
77 	_PATH_SSH_CLIENT_ID_RSA,
78 	_PATH_SSH_CLIENT_ID_DSA,
79 #ifdef OPENSSL_HAS_ECC
80 	_PATH_SSH_CLIENT_ID_ECDSA,
81 	_PATH_SSH_CLIENT_ID_ECDSA_SK,
82 #endif
83 #endif /* WITH_OPENSSL */
84 	_PATH_SSH_CLIENT_ID_ED25519,
85 	_PATH_SSH_CLIENT_ID_ED25519_SK,
86 	_PATH_SSH_CLIENT_ID_XMSS,
87 	NULL
88 };
89 
90 static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
91 
92 /* Default lifetime (0 == forever) */
93 static long lifetime = 0;
94 
95 /* User has to confirm key use */
96 static int confirm = 0;
97 
98 /* Maximum number of signatures (XMSS) */
99 static u_int maxsign = 0;
100 static u_int minleft = 0;
101 
102 /* we keep a cache of one passphrase */
103 static char *pass = NULL;
104 static void
105 clear_pass(void)
106 {
107 	if (pass) {
108 		explicit_bzero(pass, strlen(pass));
109 		free(pass);
110 		pass = NULL;
111 	}
112 }
113 
114 static int
115 delete_file(int agent_fd, const char *filename, int key_only, int qflag)
116 {
117 	struct sshkey *public, *cert = NULL;
118 	char *certpath = NULL, *comment = NULL;
119 	int r, ret = -1;
120 
121 	if ((r = sshkey_load_public(filename, &public,  &comment)) != 0) {
122 		printf("Bad key file %s: %s\n", filename, ssh_err(r));
123 		return -1;
124 	}
125 	if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
126 		if (!qflag) {
127 			fprintf(stderr, "Identity removed: %s (%s)\n",
128 			    filename, comment);
129 		}
130 		ret = 0;
131 	} else
132 		fprintf(stderr, "Could not remove identity \"%s\": %s\n",
133 		    filename, ssh_err(r));
134 
135 	if (key_only)
136 		goto out;
137 
138 	/* Now try to delete the corresponding certificate too */
139 	free(comment);
140 	comment = NULL;
141 	xasprintf(&certpath, "%s-cert.pub", filename);
142 	if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
143 		if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
144 			error("Failed to load certificate \"%s\": %s",
145 			    certpath, ssh_err(r));
146 		goto out;
147 	}
148 
149 	if (!sshkey_equal_public(cert, public))
150 		fatal("Certificate %s does not match private key %s",
151 		    certpath, filename);
152 
153 	if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
154 		if (!qflag) {
155 			fprintf(stderr, "Identity removed: %s (%s)\n",
156 			    certpath, comment);
157 		}
158 		ret = 0;
159 	} else
160 		fprintf(stderr, "Could not remove identity \"%s\": %s\n",
161 		    certpath, ssh_err(r));
162 
163  out:
164 	sshkey_free(cert);
165 	sshkey_free(public);
166 	free(certpath);
167 	free(comment);
168 
169 	return ret;
170 }
171 
172 /* Send a request to remove all identities. */
173 static int
174 delete_all(int agent_fd, int qflag)
175 {
176 	int ret = -1;
177 
178 	/*
179 	 * Since the agent might be forwarded, old or non-OpenSSH, when asked
180 	 * to remove all keys, attempt to remove both protocol v.1 and v.2
181 	 * keys.
182 	 */
183 	if (ssh_remove_all_identities(agent_fd, 2) == 0)
184 		ret = 0;
185 	/* ignore error-code for ssh1 */
186 	ssh_remove_all_identities(agent_fd, 1);
187 
188 	if (ret != 0)
189 		fprintf(stderr, "Failed to remove all identities.\n");
190 	else if (!qflag)
191 		fprintf(stderr, "All identities removed.\n");
192 
193 	return ret;
194 }
195 
196 static int
197 add_file(int agent_fd, const char *filename, int key_only, int qflag,
198     const char *skprovider)
199 {
200 	struct sshkey *private, *cert;
201 	char *comment = NULL;
202 	char msg[1024], *certpath = NULL;
203 	int r, fd, ret = -1;
204 	size_t i;
205 	u_int32_t left;
206 	struct sshbuf *keyblob;
207 	struct ssh_identitylist *idlist;
208 
209 	if (strcmp(filename, "-") == 0) {
210 		fd = STDIN_FILENO;
211 		filename = "(stdin)";
212 	} else if ((fd = open(filename, O_RDONLY)) == -1) {
213 		perror(filename);
214 		return -1;
215 	}
216 
217 	/*
218 	 * Since we'll try to load a keyfile multiple times, permission errors
219 	 * will occur multiple times, so check perms first and bail if wrong.
220 	 */
221 	if (fd != STDIN_FILENO) {
222 		if (sshkey_perm_ok(fd, filename) != 0) {
223 			close(fd);
224 			return -1;
225 		}
226 	}
227 	if ((r = sshbuf_load_fd(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_is_sk(private))
316 		skprovider = NULL; /* Don't send constraint for other keys */
317 	else if (skprovider == NULL) {
318 		fprintf(stderr, "Cannot load authenticator-hosted key %s "
319 		    "without 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 %ld 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 %ld 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, NULL)) != 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 load_resident_keys(int agent_fd, const char *skprovider, int qflag)
543 {
544 	struct sshkey **keys;
545 	size_t nkeys, i;
546 	int r, ok = 0;
547 	char *fp;
548 
549 	pass = read_passphrase("Enter PIN for authenticator: ", RP_ALLOW_STDIN);
550 	if ((r = sshsk_load_resident(skprovider, NULL, pass,
551 	    &keys, &nkeys)) != 0) {
552 		error("Unable to load resident keys: %s", ssh_err(r));
553 		return r;
554 	}
555 	for (i = 0; i < nkeys; i++) {
556 		if ((fp = sshkey_fingerprint(keys[i],
557 		    fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
558 			fatal("%s: sshkey_fingerprint failed", __func__);
559 		if ((r = ssh_add_identity_constrained(agent_fd, keys[i], "",
560 		    lifetime, confirm, maxsign, skprovider)) != 0) {
561 			error("Unable to add key %s %s",
562 			    sshkey_type(keys[i]), fp);
563 			free(fp);
564 			ok = r;
565 			continue;
566 		}
567 		if (ok == 0)
568 			ok = 1;
569 		if (!qflag) {
570 			fprintf(stderr, "Resident identity added: %s %s\n",
571 			    sshkey_type(keys[i]), fp);
572 			if (lifetime != 0) {
573 				fprintf(stderr,
574 				    "Lifetime set to %ld seconds\n", lifetime);
575 			}
576 			if (confirm != 0) {
577 				fprintf(stderr, "The user must confirm "
578 				    "each use of the key\n");
579 			}
580 		}
581 		free(fp);
582 		sshkey_free(keys[i]);
583 	}
584 	free(keys);
585 	if (nkeys == 0)
586 		return SSH_ERR_KEY_NOT_FOUND;
587 	return ok == 1 ? 0 : ok;
588 }
589 
590 static int
591 do_file(int agent_fd, int deleting, int key_only, char *file, int qflag,
592     const char *skprovider)
593 {
594 	if (deleting) {
595 		if (delete_file(agent_fd, file, key_only, qflag) == -1)
596 			return -1;
597 	} else {
598 		if (add_file(agent_fd, file, key_only, qflag, skprovider) == -1)
599 			return -1;
600 	}
601 	return 0;
602 }
603 
604 static void
605 usage(void)
606 {
607 	fprintf(stderr,
608 "usage: ssh-add [-cDdKkLlqvXx] [-E fingerprint_hash] [-S provider] [-t life]\n"
609 #ifdef WITH_XMSS
610 "               [-M maxsign] [-m minleft]\n"
611 #endif
612 "               [file ...]\n"
613 "       ssh-add -s pkcs11\n"
614 "       ssh-add -e pkcs11\n"
615 "       ssh-add -T pubkey ...\n"
616 	);
617 }
618 
619 int
620 main(int argc, char **argv)
621 {
622 	extern char *optarg;
623 	extern int optind;
624 	int agent_fd;
625 	char *pkcs11provider = NULL, *skprovider = NULL;
626 	int r, i, ch, deleting = 0, ret = 0, key_only = 0, do_download = 0;
627 	int xflag = 0, lflag = 0, Dflag = 0, qflag = 0, Tflag = 0;
628 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
629 	LogLevel log_level = SYSLOG_LEVEL_INFO;
630 
631 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
632 	sanitise_stdfd();
633 
634 	__progname = ssh_get_progname(argv[0]);
635 	seed_rng();
636 
637 	log_init(__progname, log_level, log_facility, 1);
638 
639 	setvbuf(stdout, NULL, _IOLBF, 0);
640 
641 	/* First, get a connection to the authentication agent. */
642 	switch (r = ssh_get_authentication_socket(&agent_fd)) {
643 	case 0:
644 		break;
645 	case SSH_ERR_AGENT_NOT_PRESENT:
646 		fprintf(stderr, "Could not open a connection to your "
647 		    "authentication agent.\n");
648 		exit(2);
649 	default:
650 		fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
651 		exit(2);
652 	}
653 
654 	skprovider = getenv("SSH_SK_PROVIDER");
655 
656 	while ((ch = getopt(argc, argv, "vkKlLcdDTxXE:e:M:m:qs:S:t:")) != -1) {
657 		switch (ch) {
658 		case 'v':
659 			if (log_level == SYSLOG_LEVEL_INFO)
660 				log_level = SYSLOG_LEVEL_DEBUG1;
661 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
662 				log_level++;
663 			break;
664 		case 'E':
665 			fingerprint_hash = ssh_digest_alg_by_name(optarg);
666 			if (fingerprint_hash == -1)
667 				fatal("Invalid hash algorithm \"%s\"", optarg);
668 			break;
669 		case 'k':
670 			key_only = 1;
671 			break;
672 		case 'K':
673 			do_download = 1;
674 			break;
675 		case 'l':
676 		case 'L':
677 			if (lflag != 0)
678 				fatal("-%c flag already specified", lflag);
679 			lflag = ch;
680 			break;
681 		case 'x':
682 		case 'X':
683 			if (xflag != 0)
684 				fatal("-%c flag already specified", xflag);
685 			xflag = ch;
686 			break;
687 		case 'c':
688 			confirm = 1;
689 			break;
690 		case 'm':
691 			minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL);
692 			if (minleft == 0) {
693 				usage();
694 				ret = 1;
695 				goto done;
696 			}
697 			break;
698 		case 'M':
699 			maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL);
700 			if (maxsign == 0) {
701 				usage();
702 				ret = 1;
703 				goto done;
704 			}
705 			break;
706 		case 'd':
707 			deleting = 1;
708 			break;
709 		case 'D':
710 			Dflag = 1;
711 			break;
712 		case 's':
713 			pkcs11provider = optarg;
714 			break;
715 		case 'S':
716 			skprovider = optarg;
717 			break;
718 		case 'e':
719 			deleting = 1;
720 			pkcs11provider = optarg;
721 			break;
722 		case 't':
723 			if ((lifetime = convtime(optarg)) == -1 ||
724 			    lifetime < 0 || lifetime > UINT32_MAX) {
725 				fprintf(stderr, "Invalid lifetime\n");
726 				ret = 1;
727 				goto done;
728 			}
729 			break;
730 		case 'q':
731 			qflag = 1;
732 			break;
733 		case 'T':
734 			Tflag = 1;
735 			break;
736 		default:
737 			usage();
738 			ret = 1;
739 			goto done;
740 		}
741 	}
742 	log_init(__progname, log_level, log_facility, 1);
743 
744 	if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
745 		fatal("Invalid combination of actions");
746 	else if (xflag) {
747 		if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
748 			ret = 1;
749 		goto done;
750 	} else if (lflag) {
751 		if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
752 			ret = 1;
753 		goto done;
754 	} else if (Dflag) {
755 		if (delete_all(agent_fd, qflag) == -1)
756 			ret = 1;
757 		goto done;
758 	}
759 
760 #ifdef ENABLE_SK_INTERNAL
761 	if (skprovider == NULL)
762 		skprovider = "internal";
763 #endif
764 
765 	argc -= optind;
766 	argv += optind;
767 	if (Tflag) {
768 		if (argc <= 0)
769 			fatal("no keys to test");
770 		for (r = i = 0; i < argc; i++)
771 			r |= test_key(agent_fd, argv[i]);
772 		ret = r == 0 ? 0 : 1;
773 		goto done;
774 	}
775 	if (pkcs11provider != NULL) {
776 		if (update_card(agent_fd, !deleting, pkcs11provider,
777 		    qflag) == -1)
778 			ret = 1;
779 		goto done;
780 	}
781 	if (do_download) {
782 		if (skprovider == NULL)
783 			fatal("Cannot download keys without provider");
784 		if (load_resident_keys(agent_fd, skprovider, qflag) != 0)
785 			ret = 1;
786 		goto done;
787 	}
788 	if (argc == 0) {
789 		char buf[PATH_MAX];
790 		struct passwd *pw;
791 		struct stat st;
792 		int count = 0;
793 
794 		if ((pw = getpwuid(getuid())) == NULL) {
795 			fprintf(stderr, "No user found with uid %u\n",
796 			    (u_int)getuid());
797 			ret = 1;
798 			goto done;
799 		}
800 
801 		for (i = 0; default_files[i]; i++) {
802 			snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
803 			    default_files[i]);
804 			if (stat(buf, &st) == -1)
805 				continue;
806 			if (do_file(agent_fd, deleting, key_only, buf,
807 			    qflag, skprovider) == -1)
808 				ret = 1;
809 			else
810 				count++;
811 		}
812 		if (count == 0)
813 			ret = 1;
814 	} else {
815 		for (i = 0; i < argc; i++) {
816 			if (do_file(agent_fd, deleting, key_only,
817 			    argv[i], qflag, skprovider) == -1)
818 				ret = 1;
819 		}
820 	}
821 done:
822 	clear_pass();
823 	ssh_close_authentication_socket(agent_fd);
824 	return ret;
825 }
826