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