xref: /freebsd/crypto/openssh/ssh-add.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
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"
38 RCSID("$OpenBSD: ssh-add.c,v 1.61 2002/06/19 00:27:55 deraadt Exp $");
39 RCSID("$FreeBSD$");
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 /* argv0 */
55 extern char *__progname;
56 
57 /* Default files to add */
58 static char *default_files[] = {
59 	_PATH_SSH_CLIENT_ID_RSA,
60 	_PATH_SSH_CLIENT_ID_DSA,
61 	_PATH_SSH_CLIENT_IDENTITY,
62 	NULL
63 };
64 
65 /* Default lifetime (0 == forever) */
66 static int lifetime = 0;
67 
68 /* we keep a cache of one passphrases */
69 static char *pass = NULL;
70 static void
71 clear_pass(void)
72 {
73 	if (pass) {
74 		memset(pass, 0, strlen(pass));
75 		xfree(pass);
76 		pass = NULL;
77 	}
78 }
79 
80 static int
81 delete_file(AuthenticationConnection *ac, const char *filename)
82 {
83 	Key *public;
84 	char *comment = NULL;
85 	int ret = -1;
86 
87 	public = key_load_public(filename, &comment);
88 	if (public == NULL) {
89 		printf("Bad key file %s\n", filename);
90 		return -1;
91 	}
92 	if (ssh_remove_identity(ac, public)) {
93 		fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
94 		ret = 0;
95 	} else
96 		fprintf(stderr, "Could not remove identity: %s\n", filename);
97 
98 	key_free(public);
99 	xfree(comment);
100 
101 	return ret;
102 }
103 
104 /* Send a request to remove all identities. */
105 static int
106 delete_all(AuthenticationConnection *ac)
107 {
108 	int ret = -1;
109 
110 	if (ssh_remove_all_identities(ac, 1))
111 		ret = 0;
112 	/* ignore error-code for ssh2 */
113 	ssh_remove_all_identities(ac, 2);
114 
115 	if (ret == 0)
116 		fprintf(stderr, "All identities removed.\n");
117 	else
118 		fprintf(stderr, "Failed to remove all identities.\n");
119 
120 	return ret;
121 }
122 
123 static int
124 add_file(AuthenticationConnection *ac, const char *filename)
125 {
126 	struct stat st;
127 	Key *private;
128 	char *comment = NULL;
129 	char msg[1024];
130 	int ret = -1;
131 
132 	if (stat(filename, &st) < 0) {
133 		perror(filename);
134 		return -1;
135 	}
136 	/* At first, try empty passphrase */
137 	private = key_load_private(filename, "", &comment);
138 	if (comment == NULL)
139 		comment = xstrdup(filename);
140 	/* try last */
141 	if (private == NULL && pass != NULL)
142 		private = key_load_private(filename, pass, NULL);
143 	if (private == NULL) {
144 		/* clear passphrase since it did not work */
145 		clear_pass();
146 		snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
147 		   comment);
148 		for (;;) {
149 			pass = read_passphrase(msg, RP_ALLOW_STDIN);
150 			if (strcmp(pass, "") == 0) {
151 				clear_pass();
152 				xfree(comment);
153 				return -1;
154 			}
155 			private = key_load_private(filename, pass, &comment);
156 			if (private != NULL)
157 				break;
158 			clear_pass();
159 			strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
160 		}
161 	}
162 
163 	if (ssh_add_identity_constrained(ac, private, comment, lifetime)) {
164 		fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
165 		ret = 0;
166 		if (lifetime != 0)
167                         fprintf(stderr,
168 			    "Lifetime set to %d seconds\n", lifetime);
169 	} else if (ssh_add_identity(ac, private, comment)) {
170 		fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
171 		ret = 0;
172 	} else {
173 		fprintf(stderr, "Could not add identity: %s\n", filename);
174 	}
175 
176 	xfree(comment);
177 	key_free(private);
178 
179 	return ret;
180 }
181 
182 static int
183 update_card(AuthenticationConnection *ac, int add, const char *id)
184 {
185 	char *pin;
186 
187 	pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
188 	if (pin == NULL)
189 		return -1;
190 
191 	if (ssh_update_card(ac, add, id, pin)) {
192 		fprintf(stderr, "Card %s: %s\n",
193 		    add ? "added" : "removed", id);
194 		return 0;
195 	} else {
196 		fprintf(stderr, "Could not %s card: %s\n",
197 		    add ? "add" : "remove", id);
198 		return -1;
199 	}
200 }
201 
202 static int
203 list_identities(AuthenticationConnection *ac, int do_fp)
204 {
205 	Key *key;
206 	char *comment, *fp;
207 	int had_identities = 0;
208 	int version;
209 
210 	for (version = 1; version <= 2; version++) {
211 		for (key = ssh_get_first_identity(ac, &comment, version);
212 		    key != NULL;
213 		    key = ssh_get_next_identity(ac, &comment, version)) {
214 			had_identities = 1;
215 			if (do_fp) {
216 				fp = key_fingerprint(key, SSH_FP_MD5,
217 				    SSH_FP_HEX);
218 				printf("%d %s %s (%s)\n",
219 				    key_size(key), fp, comment, key_type(key));
220 				xfree(fp);
221 			} else {
222 				if (!key_write(key, stdout))
223 					fprintf(stderr, "key_write failed");
224 				fprintf(stdout, " %s\n", comment);
225 			}
226 			key_free(key);
227 			xfree(comment);
228 		}
229 	}
230 	if (!had_identities) {
231 		printf("The agent has no identities.\n");
232 		return -1;
233 	}
234 	return 0;
235 }
236 
237 static int
238 lock_agent(AuthenticationConnection *ac, int lock)
239 {
240 	char prompt[100], *p1, *p2;
241 	int passok = 1, ret = -1;
242 
243 	strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
244 	p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
245 	if (lock) {
246 		strlcpy(prompt, "Again: ", sizeof prompt);
247 		p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
248 		if (strcmp(p1, p2) != 0) {
249 			fprintf(stderr, "Passwords do not match.\n");
250 			passok = 0;
251 		}
252 		memset(p2, 0, strlen(p2));
253 		xfree(p2);
254 	}
255 	if (passok && ssh_lock_agent(ac, lock, p1)) {
256 		fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
257 		ret = 0;
258 	} else
259 		fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
260 	memset(p1, 0, strlen(p1));
261 	xfree(p1);
262 	return -1;
263 }
264 
265 static int
266 do_file(AuthenticationConnection *ac, int deleting, char *file)
267 {
268 	if (deleting) {
269 		if (delete_file(ac, file) == -1)
270 			return -1;
271 	} else {
272 		if (add_file(ac, file) == -1)
273 			return -1;
274 	}
275 	return 0;
276 }
277 
278 static void
279 usage(void)
280 {
281 	fprintf(stderr, "Usage: %s [options]\n", __progname);
282 	fprintf(stderr, "Options:\n");
283 	fprintf(stderr, "  -l          List fingerprints of all identities.\n");
284 	fprintf(stderr, "  -L          List public key parameters of all identities.\n");
285 	fprintf(stderr, "  -d          Delete identity.\n");
286 	fprintf(stderr, "  -D          Delete all identities.\n");
287 	fprintf(stderr, "  -x          Lock agent.\n");
288 	fprintf(stderr, "  -x          Unlock agent.\n");
289 	fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
290 #ifdef SMARTCARD
291 	fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
292 	fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
293 #endif
294 }
295 
296 int
297 main(int argc, char **argv)
298 {
299 	extern char *optarg;
300 	extern int optind;
301 	AuthenticationConnection *ac = NULL;
302 	char *sc_reader_id = NULL;
303 	int i, ch, deleting = 0, ret = 0;
304 
305 	SSLeay_add_all_algorithms();
306 
307 	/* At first, get a connection to the authentication agent. */
308 	ac = ssh_get_authentication_connection();
309 	if (ac == NULL) {
310 		fprintf(stderr, "Could not open a connection to your authentication agent.\n");
311 		exit(2);
312 	}
313 	while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
314 		switch (ch) {
315 		case 'l':
316 		case 'L':
317 			if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
318 				ret = 1;
319 			goto done;
320 			break;
321 		case 'x':
322 		case 'X':
323 			if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
324 				ret = 1;
325 			goto done;
326 			break;
327 		case 'd':
328 			deleting = 1;
329 			break;
330 		case 'D':
331 			if (delete_all(ac) == -1)
332 				ret = 1;
333 			goto done;
334 			break;
335 		case 's':
336 			sc_reader_id = optarg;
337 			break;
338 		case 'e':
339 			deleting = 1;
340 			sc_reader_id = optarg;
341 			break;
342 		case 't':
343 			if ((lifetime = convtime(optarg)) == -1) {
344 				fprintf(stderr, "Invalid lifetime\n");
345 				ret = 1;
346 				goto done;
347 			}
348 			break;
349 		default:
350 			usage();
351 			ret = 1;
352 			goto done;
353 		}
354 	}
355 	argc -= optind;
356 	argv += optind;
357 	if (sc_reader_id != NULL) {
358 		if (update_card(ac, !deleting, sc_reader_id) == -1)
359 			ret = 1;
360 		goto done;
361 	}
362 	if (argc == 0) {
363 		char buf[MAXPATHLEN];
364 		struct passwd *pw;
365 		struct stat st;
366 		int count = 0;
367 
368 		if ((pw = getpwuid(getuid())) == NULL) {
369 			fprintf(stderr, "No user found with uid %u\n",
370 			    (u_int)getuid());
371 			ret = 1;
372 			goto done;
373 		}
374 
375 		for(i = 0; default_files[i]; i++) {
376 			snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
377 			    default_files[i]);
378 			if (stat(buf, &st) < 0)
379 				continue;
380 			if (do_file(ac, deleting, buf) == -1)
381 				ret = 1;
382 			else
383 				count++;
384 		}
385 		if (count == 0)
386 			ret = 1;
387 	} else {
388 		for(i = 0; i < argc; i++) {
389 			if (do_file(ac, deleting, argv[i]) == -1)
390 				ret = 1;
391 		}
392 	}
393 	clear_pass();
394 
395 done:
396 	ssh_close_authentication_connection(ac);
397 	return ret;
398 }
399