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