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