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