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