ssh-add.c revision 113911
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 *                    All rights reserved
5 * Adds an identity to the authentication server, or removes an identity.
6 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose.  Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
13 * SSH2 implementation,
14 * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "includes.h"
38RCSID("$OpenBSD: ssh-add.c,v 1.66 2003/03/05 22:33:43 markus Exp $");
39RCSID("$FreeBSD: head/crypto/openssh/ssh-add.c 113911 2003-04-23 17:13:13Z des $");
40
41#include <openssl/evp.h>
42
43#include "ssh.h"
44#include "rsa.h"
45#include "log.h"
46#include "xmalloc.h"
47#include "key.h"
48#include "authfd.h"
49#include "authfile.h"
50#include "pathnames.h"
51#include "readpass.h"
52#include "misc.h"
53
54#ifdef HAVE___PROGNAME
55extern char *__progname;
56#else
57char *__progname;
58#endif
59
60/* argv0 */
61extern char *__progname;
62
63/* Default files to add */
64static char *default_files[] = {
65	_PATH_SSH_CLIENT_ID_RSA,
66	_PATH_SSH_CLIENT_ID_DSA,
67	_PATH_SSH_CLIENT_IDENTITY,
68	NULL
69};
70
71/* Default lifetime (0 == forever) */
72static int lifetime = 0;
73
74/* User has to confirm key use */
75static int confirm = 0;
76
77/* we keep a cache of one passphrases */
78static char *pass = NULL;
79static void
80clear_pass(void)
81{
82	if (pass) {
83		memset(pass, 0, strlen(pass));
84		xfree(pass);
85		pass = NULL;
86	}
87}
88
89static int
90delete_file(AuthenticationConnection *ac, const char *filename)
91{
92	Key *public;
93	char *comment = NULL;
94	int ret = -1;
95
96	public = key_load_public(filename, &comment);
97	if (public == NULL) {
98		printf("Bad key file %s\n", filename);
99		return -1;
100	}
101	if (ssh_remove_identity(ac, public)) {
102		fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
103		ret = 0;
104	} else
105		fprintf(stderr, "Could not remove identity: %s\n", filename);
106
107	key_free(public);
108	xfree(comment);
109
110	return ret;
111}
112
113/* Send a request to remove all identities. */
114static int
115delete_all(AuthenticationConnection *ac)
116{
117	int ret = -1;
118
119	if (ssh_remove_all_identities(ac, 1))
120		ret = 0;
121	/* ignore error-code for ssh2 */
122	ssh_remove_all_identities(ac, 2);
123
124	if (ret == 0)
125		fprintf(stderr, "All identities removed.\n");
126	else
127		fprintf(stderr, "Failed to remove all identities.\n");
128
129	return ret;
130}
131
132static int
133add_file(AuthenticationConnection *ac, const char *filename)
134{
135	struct stat st;
136	Key *private;
137	char *comment = NULL;
138	char msg[1024];
139	int ret = -1;
140
141	if (stat(filename, &st) < 0) {
142		perror(filename);
143		return -1;
144	}
145	/* At first, try empty passphrase */
146	private = key_load_private(filename, "", &comment);
147	if (comment == NULL)
148		comment = xstrdup(filename);
149	/* try last */
150	if (private == NULL && pass != NULL)
151		private = key_load_private(filename, pass, NULL);
152	if (private == NULL) {
153		/* clear passphrase since it did not work */
154		clear_pass();
155		snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
156		   comment);
157		for (;;) {
158			pass = read_passphrase(msg, RP_ALLOW_STDIN);
159			if (strcmp(pass, "") == 0) {
160				clear_pass();
161				xfree(comment);
162				return -1;
163			}
164			private = key_load_private(filename, pass, &comment);
165			if (private != NULL)
166				break;
167			clear_pass();
168			strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
169		}
170	}
171
172 	if (ssh_add_identity_constrained(ac, private, comment, lifetime,
173 	    confirm)) {
174		fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
175		ret = 0;
176		if (lifetime != 0)
177			fprintf(stderr,
178			    "Lifetime set to %d seconds\n", lifetime);
179 		if (confirm != 0)
180			fprintf(stderr,
181			    "The user has to confirm each use of the key\n");
182	} else if (ssh_add_identity(ac, private, comment)) {
183		fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
184		ret = 0;
185	} else {
186		fprintf(stderr, "Could not add identity: %s\n", filename);
187	}
188
189	xfree(comment);
190	key_free(private);
191
192	return ret;
193}
194
195static int
196update_card(AuthenticationConnection *ac, int add, const char *id)
197{
198	char *pin;
199	int ret = -1;
200
201	pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
202	if (pin == NULL)
203		return -1;
204
205	if (ssh_update_card(ac, add, id, pin)) {
206		fprintf(stderr, "Card %s: %s\n",
207		    add ? "added" : "removed", id);
208		ret = 0;
209	} else {
210		fprintf(stderr, "Could not %s card: %s\n",
211		    add ? "add" : "remove", id);
212		ret = -1;
213	}
214	xfree(pin);
215	return ret;
216}
217
218static int
219list_identities(AuthenticationConnection *ac, int do_fp)
220{
221	Key *key;
222	char *comment, *fp;
223	int had_identities = 0;
224	int version;
225
226	for (version = 1; version <= 2; version++) {
227		for (key = ssh_get_first_identity(ac, &comment, version);
228		    key != NULL;
229		    key = ssh_get_next_identity(ac, &comment, version)) {
230			had_identities = 1;
231			if (do_fp) {
232				fp = key_fingerprint(key, SSH_FP_MD5,
233				    SSH_FP_HEX);
234				printf("%d %s %s (%s)\n",
235				    key_size(key), fp, comment, key_type(key));
236				xfree(fp);
237			} else {
238				if (!key_write(key, stdout))
239					fprintf(stderr, "key_write failed");
240				fprintf(stdout, " %s\n", comment);
241			}
242			key_free(key);
243			xfree(comment);
244		}
245	}
246	if (!had_identities) {
247		printf("The agent has no identities.\n");
248		return -1;
249	}
250	return 0;
251}
252
253static int
254lock_agent(AuthenticationConnection *ac, int lock)
255{
256	char prompt[100], *p1, *p2;
257	int passok = 1, ret = -1;
258
259	strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
260	p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
261	if (lock) {
262		strlcpy(prompt, "Again: ", sizeof prompt);
263		p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
264		if (strcmp(p1, p2) != 0) {
265			fprintf(stderr, "Passwords do not match.\n");
266			passok = 0;
267		}
268		memset(p2, 0, strlen(p2));
269		xfree(p2);
270	}
271	if (passok && ssh_lock_agent(ac, lock, p1)) {
272		fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
273		ret = 0;
274	} else
275		fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
276	memset(p1, 0, strlen(p1));
277	xfree(p1);
278	return (ret);
279}
280
281static int
282do_file(AuthenticationConnection *ac, int deleting, char *file)
283{
284	if (deleting) {
285		if (delete_file(ac, file) == -1)
286			return -1;
287	} else {
288		if (add_file(ac, file) == -1)
289			return -1;
290	}
291	return 0;
292}
293
294static void
295usage(void)
296{
297	fprintf(stderr, "Usage: %s [options]\n", __progname);
298	fprintf(stderr, "Options:\n");
299	fprintf(stderr, "  -l          List fingerprints of all identities.\n");
300	fprintf(stderr, "  -L          List public key parameters of all identities.\n");
301	fprintf(stderr, "  -d          Delete identity.\n");
302	fprintf(stderr, "  -D          Delete all identities.\n");
303	fprintf(stderr, "  -x          Lock agent.\n");
304	fprintf(stderr, "  -X          Unlock agent.\n");
305	fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
306	fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
307#ifdef SMARTCARD
308	fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
309	fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
310#endif
311}
312
313int
314main(int argc, char **argv)
315{
316	extern char *optarg;
317	extern int optind;
318	AuthenticationConnection *ac = NULL;
319	char *sc_reader_id = NULL;
320	int i, ch, deleting = 0, ret = 0;
321
322	__progname = get_progname(argv[0]);
323	init_rng();
324	seed_rng();
325
326	SSLeay_add_all_algorithms();
327
328	/* At first, get a connection to the authentication agent. */
329	ac = ssh_get_authentication_connection();
330	if (ac == NULL) {
331		fprintf(stderr, "Could not open a connection to your authentication agent.\n");
332		exit(2);
333	}
334	while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
335		switch (ch) {
336		case 'l':
337		case 'L':
338			if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
339				ret = 1;
340			goto done;
341			break;
342		case 'x':
343		case 'X':
344			if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
345				ret = 1;
346			goto done;
347			break;
348		case 'c':
349			confirm = 1;
350			break;
351		case 'd':
352			deleting = 1;
353			break;
354		case 'D':
355			if (delete_all(ac) == -1)
356				ret = 1;
357			goto done;
358			break;
359		case 's':
360			sc_reader_id = optarg;
361			break;
362		case 'e':
363			deleting = 1;
364			sc_reader_id = optarg;
365			break;
366		case 't':
367			if ((lifetime = convtime(optarg)) == -1) {
368				fprintf(stderr, "Invalid lifetime\n");
369				ret = 1;
370				goto done;
371			}
372			break;
373		default:
374			usage();
375			ret = 1;
376			goto done;
377		}
378	}
379	argc -= optind;
380	argv += optind;
381	if (sc_reader_id != NULL) {
382		if (update_card(ac, !deleting, sc_reader_id) == -1)
383			ret = 1;
384		goto done;
385	}
386	if (argc == 0) {
387		char buf[MAXPATHLEN];
388		struct passwd *pw;
389		struct stat st;
390		int count = 0;
391
392		if ((pw = getpwuid(getuid())) == NULL) {
393			fprintf(stderr, "No user found with uid %u\n",
394			    (u_int)getuid());
395			ret = 1;
396			goto done;
397		}
398
399		for(i = 0; default_files[i]; i++) {
400			snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
401			    default_files[i]);
402			if (stat(buf, &st) < 0)
403				continue;
404			if (do_file(ac, deleting, buf) == -1)
405				ret = 1;
406			else
407				count++;
408		}
409		if (count == 0)
410			ret = 1;
411	} else {
412		for(i = 0; i < argc; i++) {
413			if (do_file(ac, deleting, argv[i]) == -1)
414				ret = 1;
415		}
416	}
417	clear_pass();
418
419done:
420	ssh_close_authentication_connection(ac);
421	return ret;
422}
423