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