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