xref: /titanic_50/usr/src/cmd/ssh/ssh-add/ssh-add.c (revision ea01bd62c06264135e67699c4e213c6de9313abe)
17c478bd9Sstevel@tonic-gate /*
2*ea01bd62SHuie-Ying Lee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
87c478bd9Sstevel@tonic-gate  *                    All rights reserved
97c478bd9Sstevel@tonic-gate  * Adds an identity to the authentication server, or removes an identity.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
127c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
137c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
147c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
157c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * SSH2 implementation,
187c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
217c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
227c478bd9Sstevel@tonic-gate  * are met:
237c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
247c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
257c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
267c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
277c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
307c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
317c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
327c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
337c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
347c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
357c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
367c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
377c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
387c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "includes.h"
427c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: ssh-add.c,v 1.63 2002/09/19 15:51:23 markus Exp $");
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <openssl/evp.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include "ssh.h"
477c478bd9Sstevel@tonic-gate #include "rsa.h"
487c478bd9Sstevel@tonic-gate #include "log.h"
497c478bd9Sstevel@tonic-gate #include "xmalloc.h"
507c478bd9Sstevel@tonic-gate #include "key.h"
517c478bd9Sstevel@tonic-gate #include "authfd.h"
527c478bd9Sstevel@tonic-gate #include "authfile.h"
537c478bd9Sstevel@tonic-gate #include "pathnames.h"
547c478bd9Sstevel@tonic-gate #include "readpass.h"
557c478bd9Sstevel@tonic-gate #include "misc.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
587c478bd9Sstevel@tonic-gate extern char *__progname;
597c478bd9Sstevel@tonic-gate #else
607c478bd9Sstevel@tonic-gate char *__progname;
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* argv0 */
647c478bd9Sstevel@tonic-gate extern char *__progname;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Default files to add */
677c478bd9Sstevel@tonic-gate static char *default_files[] = {
687c478bd9Sstevel@tonic-gate 	_PATH_SSH_CLIENT_ID_RSA,
697c478bd9Sstevel@tonic-gate 	_PATH_SSH_CLIENT_ID_DSA,
707c478bd9Sstevel@tonic-gate 	_PATH_SSH_CLIENT_IDENTITY,
717c478bd9Sstevel@tonic-gate 	NULL
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* Default lifetime (0 == forever) */
757c478bd9Sstevel@tonic-gate static int lifetime = 0;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* we keep a cache of one passphrases */
787c478bd9Sstevel@tonic-gate static char *pass = NULL;
797c478bd9Sstevel@tonic-gate static void
clear_pass(void)807c478bd9Sstevel@tonic-gate clear_pass(void)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	if (pass) {
837c478bd9Sstevel@tonic-gate 		memset(pass, 0, strlen(pass));
847c478bd9Sstevel@tonic-gate 		xfree(pass);
857c478bd9Sstevel@tonic-gate 		pass = NULL;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static int
delete_file(AuthenticationConnection * ac,const char * filename)907c478bd9Sstevel@tonic-gate delete_file(AuthenticationConnection *ac, const char *filename)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	Key *public;
937c478bd9Sstevel@tonic-gate 	char *comment = NULL;
947c478bd9Sstevel@tonic-gate 	int ret = -1;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	public = key_load_public(filename, &comment);
977c478bd9Sstevel@tonic-gate 	if (public == NULL) {
987c478bd9Sstevel@tonic-gate 		printf(gettext("Bad key file %s\n"), filename);
997c478bd9Sstevel@tonic-gate 		return -1;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	if (ssh_remove_identity(ac, public)) {
1027c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Identity removed: %s (%s)\n"),
1037c478bd9Sstevel@tonic-gate 			filename, comment);
1047c478bd9Sstevel@tonic-gate 		ret = 0;
1057c478bd9Sstevel@tonic-gate 	} else
1067c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not remove identity: %s\n"),
1077c478bd9Sstevel@tonic-gate 			filename);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	key_free(public);
1107c478bd9Sstevel@tonic-gate 	xfree(comment);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	return ret;
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* Send a request to remove all identities. */
1167c478bd9Sstevel@tonic-gate static int
delete_all(AuthenticationConnection * ac)1177c478bd9Sstevel@tonic-gate delete_all(AuthenticationConnection *ac)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	int ret = -1;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (ssh_remove_all_identities(ac, 1))
1227c478bd9Sstevel@tonic-gate 		ret = 0;
1237c478bd9Sstevel@tonic-gate 	/* ignore error-code for ssh2 */
1247c478bd9Sstevel@tonic-gate 	ssh_remove_all_identities(ac, 2);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (ret == 0)
1277c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("All identities removed.\n"));
1287c478bd9Sstevel@tonic-gate 	else
1297c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Failed to remove all identities.\n"));
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	return ret;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static int
add_file(AuthenticationConnection * ac,const char * filename)1357c478bd9Sstevel@tonic-gate add_file(AuthenticationConnection *ac, const char *filename)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	struct stat st;
1387c478bd9Sstevel@tonic-gate 	Key *private;
1397c478bd9Sstevel@tonic-gate 	char *comment = NULL;
1407c478bd9Sstevel@tonic-gate 	char msg[1024];
1417c478bd9Sstevel@tonic-gate 	int ret = -1;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (stat(filename, &st) < 0) {
1447c478bd9Sstevel@tonic-gate 		perror(filename);
1457c478bd9Sstevel@tonic-gate 		return -1;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	/* At first, try empty passphrase */
1487c478bd9Sstevel@tonic-gate 	private = key_load_private(filename, "", &comment);
1497c478bd9Sstevel@tonic-gate 	if (comment == NULL)
1507c478bd9Sstevel@tonic-gate 		comment = xstrdup(filename);
1517c478bd9Sstevel@tonic-gate 	/* try last */
1527c478bd9Sstevel@tonic-gate 	if (private == NULL && pass != NULL)
1537c478bd9Sstevel@tonic-gate 		private = key_load_private(filename, pass, NULL);
1547c478bd9Sstevel@tonic-gate 	if (private == NULL) {
1557c478bd9Sstevel@tonic-gate 		/* clear passphrase since it did not work */
1567c478bd9Sstevel@tonic-gate 		clear_pass();
1577c478bd9Sstevel@tonic-gate 		snprintf(msg, sizeof msg,
1587c478bd9Sstevel@tonic-gate 			 gettext("Enter passphrase for %.200s: "), comment);
1597c478bd9Sstevel@tonic-gate 		for (;;) {
1607c478bd9Sstevel@tonic-gate 			pass = read_passphrase(msg, RP_ALLOW_STDIN);
1617c478bd9Sstevel@tonic-gate 			if (strcmp(pass, "") == 0) {
1627c478bd9Sstevel@tonic-gate 				clear_pass();
1637c478bd9Sstevel@tonic-gate 				xfree(comment);
1647c478bd9Sstevel@tonic-gate 				return -1;
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 			private = key_load_private(filename, pass, &comment);
1677c478bd9Sstevel@tonic-gate 			if (private != NULL)
1687c478bd9Sstevel@tonic-gate 				break;
1697c478bd9Sstevel@tonic-gate 			clear_pass();
1707c478bd9Sstevel@tonic-gate 			strlcpy(msg, gettext("Bad passphrase, try again: "),
1717c478bd9Sstevel@tonic-gate 				sizeof msg);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (ssh_add_identity_constrained(ac, private, comment, lifetime)) {
1767c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Identity added: %s (%s)\n"),
1777c478bd9Sstevel@tonic-gate 			filename, comment);
1787c478bd9Sstevel@tonic-gate 		ret = 0;
1797c478bd9Sstevel@tonic-gate 		if (lifetime != 0)
1807c478bd9Sstevel@tonic-gate                         fprintf(stderr,
1817c478bd9Sstevel@tonic-gate 			    gettext("Lifetime set to %d seconds\n"), lifetime);
1827c478bd9Sstevel@tonic-gate 	} else if (ssh_add_identity(ac, private, comment)) {
1837c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Identity added: %s (%s)\n"),
1847c478bd9Sstevel@tonic-gate 			filename, comment);
1857c478bd9Sstevel@tonic-gate 		ret = 0;
1867c478bd9Sstevel@tonic-gate 	} else {
1877c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not add identity: %s\n"),
1887c478bd9Sstevel@tonic-gate 			filename);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	xfree(comment);
1927c478bd9Sstevel@tonic-gate 	key_free(private);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return ret;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static int
list_identities(AuthenticationConnection * ac,int do_fp)1987c478bd9Sstevel@tonic-gate list_identities(AuthenticationConnection *ac, int do_fp)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	Key *key;
2017c478bd9Sstevel@tonic-gate 	char *comment, *fp;
2027c478bd9Sstevel@tonic-gate 	int had_identities = 0;
2037c478bd9Sstevel@tonic-gate 	int version;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	for (version = 1; version <= 2; version++) {
2067c478bd9Sstevel@tonic-gate 		for (key = ssh_get_first_identity(ac, &comment, version);
2077c478bd9Sstevel@tonic-gate 		    key != NULL;
2087c478bd9Sstevel@tonic-gate 		    key = ssh_get_next_identity(ac, &comment, version)) {
2097c478bd9Sstevel@tonic-gate 			had_identities = 1;
2107c478bd9Sstevel@tonic-gate 			if (do_fp) {
2117c478bd9Sstevel@tonic-gate 				fp = key_fingerprint(key, SSH_FP_MD5,
2127c478bd9Sstevel@tonic-gate 				    SSH_FP_HEX);
2137c478bd9Sstevel@tonic-gate 				printf("%d %s %s (%s)\n",
2147c478bd9Sstevel@tonic-gate 				    key_size(key), fp, comment, key_type(key));
2157c478bd9Sstevel@tonic-gate 				xfree(fp);
2167c478bd9Sstevel@tonic-gate 			} else {
2177c478bd9Sstevel@tonic-gate 				if (!key_write(key, stdout))
2187c478bd9Sstevel@tonic-gate 					fprintf(stderr,
2197c478bd9Sstevel@tonic-gate 						gettext("key_write failed"));
2207c478bd9Sstevel@tonic-gate 				fprintf(stdout, " %s\n", comment);
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			key_free(key);
2237c478bd9Sstevel@tonic-gate 			xfree(comment);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	if (!had_identities) {
2277c478bd9Sstevel@tonic-gate 		printf(gettext("The agent has no identities.\n"));
2287c478bd9Sstevel@tonic-gate 		return -1;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	return 0;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static int
lock_agent(AuthenticationConnection * ac,int lock)2347c478bd9Sstevel@tonic-gate lock_agent(AuthenticationConnection *ac, int lock)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	char prompt[100], *p1, *p2;
2377c478bd9Sstevel@tonic-gate 	int passok = 1, ret = -1;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
2407c478bd9Sstevel@tonic-gate 	p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
2417c478bd9Sstevel@tonic-gate 	if (lock) {
2427c478bd9Sstevel@tonic-gate 		strlcpy(prompt, "Again: ", sizeof prompt);
2437c478bd9Sstevel@tonic-gate 		p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
2447c478bd9Sstevel@tonic-gate 		if (strcmp(p1, p2) != 0) {
2457c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Passwords do not match.\n"));
2467c478bd9Sstevel@tonic-gate 			passok = 0;
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 		memset(p2, 0, strlen(p2));
2497c478bd9Sstevel@tonic-gate 		xfree(p2);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	if (passok && ssh_lock_agent(ac, lock, p1)) {
2527c478bd9Sstevel@tonic-gate 		if (lock)
2537c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Agent locked.\n"));
2547c478bd9Sstevel@tonic-gate 		else
2557c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Agent unlocked.\n"));
2567c478bd9Sstevel@tonic-gate 		ret = 0;
2577c478bd9Sstevel@tonic-gate 	} else {
2587c478bd9Sstevel@tonic-gate 		if (lock)
2597c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Failed to lock agent.\n"));
2607c478bd9Sstevel@tonic-gate 		else
2617c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Failed to unlock agent.\n"));
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	memset(p1, 0, strlen(p1));
2647c478bd9Sstevel@tonic-gate 	xfree(p1);
2657c478bd9Sstevel@tonic-gate 	return (ret);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static int
do_file(AuthenticationConnection * ac,int deleting,char * file)2697c478bd9Sstevel@tonic-gate do_file(AuthenticationConnection *ac, int deleting, char *file)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	if (deleting) {
2727c478bd9Sstevel@tonic-gate 		if (delete_file(ac, file) == -1)
2737c478bd9Sstevel@tonic-gate 			return -1;
2747c478bd9Sstevel@tonic-gate 	} else {
2757c478bd9Sstevel@tonic-gate 		if (add_file(ac, file) == -1)
2767c478bd9Sstevel@tonic-gate 			return -1;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	return 0;
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static void
usage(void)2827c478bd9Sstevel@tonic-gate usage(void)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	fprintf(stderr,
2857c478bd9Sstevel@tonic-gate 		gettext( "Usage: %s [options]\n"
2867c478bd9Sstevel@tonic-gate 		"Options:\n"
2877c478bd9Sstevel@tonic-gate 		"  -l          List fingerprints of all identities.\n"
2887c478bd9Sstevel@tonic-gate 		"  -L          List public key parameters of all identities.\n"
2897c478bd9Sstevel@tonic-gate 		"  -d          Delete identity.\n"
2907c478bd9Sstevel@tonic-gate 		"  -D          Delete all identities.\n"
2917c478bd9Sstevel@tonic-gate 		"  -x          Lock agent.\n"
2927c478bd9Sstevel@tonic-gate 		"  -X          Unlock agent.\n"
2937c478bd9Sstevel@tonic-gate 		"  -t life     Set lifetime (seconds) when adding identities.\n"
2947c478bd9Sstevel@tonic-gate 		), __progname);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)2987c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	extern char *optarg;
3017c478bd9Sstevel@tonic-gate 	extern int optind;
3027c478bd9Sstevel@tonic-gate 	AuthenticationConnection *ac = NULL;
3037c478bd9Sstevel@tonic-gate 	int i, ch, deleting = 0, ret = 0;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	__progname = get_progname(argv[0]);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	(void) g11n_setlocale(LC_ALL, "");
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	init_rng();
3107c478bd9Sstevel@tonic-gate 	seed_rng();
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	SSLeay_add_all_algorithms();
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* At first, get a connection to the authentication agent. */
3157c478bd9Sstevel@tonic-gate 	ac = ssh_get_authentication_connection();
3167c478bd9Sstevel@tonic-gate 	if (ac == NULL) {
3177c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Could not open a connection "
3187c478bd9Sstevel@tonic-gate 			    "to your authentication agent.\n"));
3197c478bd9Sstevel@tonic-gate 		exit(2);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
3227c478bd9Sstevel@tonic-gate 		switch (ch) {
3237c478bd9Sstevel@tonic-gate 		case 'l':
3247c478bd9Sstevel@tonic-gate 		case 'L':
3257c478bd9Sstevel@tonic-gate 			if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
3267c478bd9Sstevel@tonic-gate 				ret = 1;
3277c478bd9Sstevel@tonic-gate 			goto done;
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 		case 'x':
3307c478bd9Sstevel@tonic-gate 		case 'X':
3317c478bd9Sstevel@tonic-gate 			if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
3327c478bd9Sstevel@tonic-gate 				ret = 1;
3337c478bd9Sstevel@tonic-gate 			goto done;
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 		case 'd':
3367c478bd9Sstevel@tonic-gate 			deleting = 1;
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		case 'D':
3397c478bd9Sstevel@tonic-gate 			if (delete_all(ac) == -1)
3407c478bd9Sstevel@tonic-gate 				ret = 1;
3417c478bd9Sstevel@tonic-gate 			goto done;
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 		case 't':
3447c478bd9Sstevel@tonic-gate 			if ((lifetime = convtime(optarg)) == -1) {
3457c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Invalid lifetime\n"));
3467c478bd9Sstevel@tonic-gate 				ret = 1;
3477c478bd9Sstevel@tonic-gate 				goto done;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 			break;
3507c478bd9Sstevel@tonic-gate 		default:
3517c478bd9Sstevel@tonic-gate 			usage();
3527c478bd9Sstevel@tonic-gate 			ret = 1;
3537c478bd9Sstevel@tonic-gate 			goto done;
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	argc -= optind;
3577c478bd9Sstevel@tonic-gate 	argv += optind;
3587c478bd9Sstevel@tonic-gate 	if (argc == 0) {
3597c478bd9Sstevel@tonic-gate 		char buf[MAXPATHLEN];
3607c478bd9Sstevel@tonic-gate 		struct passwd *pw;
3617c478bd9Sstevel@tonic-gate 		struct stat st;
3627c478bd9Sstevel@tonic-gate 		int count = 0;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(getuid())) == NULL) {
3657c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("No user found with uid %u\n"),
3667c478bd9Sstevel@tonic-gate 			    (u_int)getuid());
3677c478bd9Sstevel@tonic-gate 			ret = 1;
3687c478bd9Sstevel@tonic-gate 			goto done;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		for(i = 0; default_files[i]; i++) {
3727c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
3737c478bd9Sstevel@tonic-gate 			    default_files[i]);
3747c478bd9Sstevel@tonic-gate 			if (stat(buf, &st) < 0)
3757c478bd9Sstevel@tonic-gate 				continue;
3767c478bd9Sstevel@tonic-gate 			if (do_file(ac, deleting, buf) == -1)
3777c478bd9Sstevel@tonic-gate 				ret = 1;
3787c478bd9Sstevel@tonic-gate 			else
3797c478bd9Sstevel@tonic-gate 				count++;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		if (count == 0)
3827c478bd9Sstevel@tonic-gate 			ret = 1;
3837c478bd9Sstevel@tonic-gate 	} else {
3847c478bd9Sstevel@tonic-gate 		for(i = 0; i < argc; i++) {
3857c478bd9Sstevel@tonic-gate 			if (do_file(ac, deleting, argv[i]) == -1)
3867c478bd9Sstevel@tonic-gate 				ret = 1;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	clear_pass();
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate done:
3927c478bd9Sstevel@tonic-gate 	ssh_close_authentication_connection(ac);
3937c478bd9Sstevel@tonic-gate 	return ret;
3947c478bd9Sstevel@tonic-gate }
395