]>
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" | |
67656ffc | 38 | RCSID("$OpenBSD: ssh-add.c,v 1.48 2001/12/21 10:06:43 djm 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" | |
8efc0c15 | 51 | |
f601d847 | 52 | #ifdef HAVE___PROGNAME |
53 | extern char *__progname; | |
260d427b | 54 | #else |
55 | char *__progname; | |
56 | #endif | |
f601d847 | 57 | |
33e766d2 | 58 | /* argv0 */ |
59 | extern char *__progname; | |
60 | ||
67656ffc | 61 | /* Default files to add */ |
62 | static char *default_files[] = { | |
63 | _PATH_SSH_CLIENT_ID_RSA, | |
64 | _PATH_SSH_CLIENT_ID_DSA, | |
65 | _PATH_SSH_CLIENT_IDENTITY, | |
66 | NULL | |
67 | }; | |
68 | ||
69 | ||
a8666d84 | 70 | /* we keep a cache of one passphrases */ |
71 | static char *pass = NULL; | |
396c147e | 72 | static void |
a8666d84 | 73 | clear_pass(void) |
74 | { | |
75 | if (pass) { | |
76 | memset(pass, 0, strlen(pass)); | |
77 | xfree(pass); | |
78 | pass = NULL; | |
79 | } | |
80 | } | |
81 | ||
e0543e42 | 82 | static int |
5068f573 | 83 | delete_file(AuthenticationConnection *ac, const char *filename) |
8efc0c15 | 84 | { |
a306f2dd | 85 | Key *public; |
d64050ef | 86 | char *comment = NULL; |
e0543e42 | 87 | int ret = -1; |
5260325f | 88 | |
aeaa3d9e | 89 | public = key_load_public(filename, &comment); |
90 | if (public == NULL) { | |
91 | printf("Bad key file %s\n", filename); | |
e0543e42 | 92 | return -1; |
5260325f | 93 | } |
e0543e42 | 94 | if (ssh_remove_identity(ac, public)) { |
5260325f | 95 | fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); |
e0543e42 | 96 | ret = 0; |
97 | } else | |
5260325f | 98 | fprintf(stderr, "Could not remove identity: %s\n", filename); |
e0543e42 | 99 | |
a306f2dd | 100 | key_free(public); |
5260325f | 101 | xfree(comment); |
184eed6a | 102 | |
e0543e42 | 103 | return ret; |
8efc0c15 | 104 | } |
105 | ||
2e73a022 | 106 | /* Send a request to remove all identities. */ |
e0543e42 | 107 | static int |
5068f573 | 108 | delete_all(AuthenticationConnection *ac) |
8efc0c15 | 109 | { |
e0543e42 | 110 | int ret = -1; |
2e73a022 | 111 | |
e0543e42 | 112 | if (ssh_remove_all_identities(ac, 1)) |
113 | ret = 0; | |
2e73a022 | 114 | /* ignore error-code for ssh2 */ |
115 | ssh_remove_all_identities(ac, 2); | |
116 | ||
e0543e42 | 117 | if (ret == 0) |
5260325f | 118 | fprintf(stderr, "All identities removed.\n"); |
119 | else | |
b5c334cc | 120 | fprintf(stderr, "Failed to remove all identities.\n"); |
e0543e42 | 121 | |
122 | return ret; | |
8efc0c15 | 123 | } |
124 | ||
e0543e42 | 125 | static int |
5068f573 | 126 | add_file(AuthenticationConnection *ac, const char *filename) |
8efc0c15 | 127 | { |
2e73a022 | 128 | struct stat st; |
a306f2dd | 129 | Key *private; |
561e5254 | 130 | char *comment = NULL; |
131 | char msg[1024]; | |
e0543e42 | 132 | int ret = -1; |
5260325f | 133 | |
2e73a022 | 134 | if (stat(filename, &st) < 0) { |
135 | perror(filename); | |
e0543e42 | 136 | return -1; |
2e73a022 | 137 | } |
5260325f | 138 | /* At first, try empty passphrase */ |
aeaa3d9e | 139 | private = key_load_private(filename, "", &comment); |
140 | if (comment == NULL) | |
141 | comment = xstrdup(filename); | |
a8666d84 | 142 | /* try last */ |
143 | if (private == NULL && pass != NULL) | |
144 | private = key_load_private(filename, pass, NULL); | |
aeaa3d9e | 145 | if (private == NULL) { |
a8666d84 | 146 | /* clear passphrase since it did not work */ |
147 | clear_pass(); | |
065604bb | 148 | snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ", |
561e5254 | 149 | comment); |
aa3378df | 150 | for (;;) { |
1843a425 | 151 | pass = read_passphrase(msg, RP_ALLOW_STDIN); |
5260325f | 152 | if (strcmp(pass, "") == 0) { |
eae942e2 | 153 | clear_pass(); |
aeaa3d9e | 154 | xfree(comment); |
e0543e42 | 155 | return -1; |
5260325f | 156 | } |
aeaa3d9e | 157 | private = key_load_private(filename, pass, &comment); |
aeaa3d9e | 158 | if (private != NULL) |
5260325f | 159 | break; |
a8666d84 | 160 | clear_pass(); |
065604bb | 161 | strlcpy(msg, "Bad passphrase, try again: ", sizeof msg); |
5260325f | 162 | } |
163 | } | |
e0543e42 | 164 | if (ssh_add_identity(ac, private, comment)) { |
aeaa3d9e | 165 | fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); |
e0543e42 | 166 | ret = 0; |
167 | } else | |
5260325f | 168 | fprintf(stderr, "Could not add identity: %s\n", filename); |
e0543e42 | 169 | |
aeaa3d9e | 170 | xfree(comment); |
a306f2dd | 171 | key_free(private); |
184eed6a | 172 | |
e0543e42 | 173 | return ret; |
8efc0c15 | 174 | } |
175 | ||
e0543e42 | 176 | static int |
9ff6f66f | 177 | update_card(AuthenticationConnection *ac, int add, const char *id) |
983def13 | 178 | { |
e0543e42 | 179 | if (ssh_update_card(ac, add, id)) { |
9ff6f66f | 180 | fprintf(stderr, "Card %s: %s\n", |
184eed6a | 181 | add ? "added" : "removed", id); |
e0543e42 | 182 | return 0; |
183 | } else { | |
9ff6f66f | 184 | fprintf(stderr, "Could not %s card: %s\n", |
184eed6a | 185 | add ? "add" : "remove", id); |
e0543e42 | 186 | return -1; |
187 | } | |
983def13 | 188 | } |
189 | ||
396c147e | 190 | static void |
22138a36 | 191 | list_identities(AuthenticationConnection *ac, int do_fp) |
8efc0c15 | 192 | { |
2e73a022 | 193 | Key *key; |
22138a36 | 194 | char *comment, *fp; |
2e73a022 | 195 | int had_identities = 0; |
196 | int version; | |
197 | ||
198 | for (version = 1; version <= 2; version++) { | |
199 | for (key = ssh_get_first_identity(ac, &comment, version); | |
184eed6a | 200 | key != NULL; |
201 | key = ssh_get_next_identity(ac, &comment, version)) { | |
2e73a022 | 202 | had_identities = 1; |
22138a36 | 203 | if (do_fp) { |
204 | fp = key_fingerprint(key, SSH_FP_MD5, | |
205 | SSH_FP_HEX); | |
fa08c86b | 206 | printf("%d %s %s (%s)\n", |
22138a36 | 207 | key_size(key), fp, comment, key_type(key)); |
208 | xfree(fp); | |
5260325f | 209 | } else { |
2e73a022 | 210 | if (!key_write(key, stdout)) |
211 | fprintf(stderr, "key_write failed"); | |
212 | fprintf(stdout, " %s\n", comment); | |
5260325f | 213 | } |
2e73a022 | 214 | key_free(key); |
215 | xfree(comment); | |
5260325f | 216 | } |
f095fcc7 | 217 | } |
5260325f | 218 | if (!had_identities) |
219 | printf("The agent has no identities.\n"); | |
8efc0c15 | 220 | } |
221 | ||
67656ffc | 222 | static int |
223 | do_file(AuthenticationConnection *ac, int deleting, char *file) | |
224 | { | |
225 | if (deleting) { | |
226 | if (delete_file(ac, file) == -1) | |
227 | return -1; | |
228 | } else { | |
229 | if (add_file(ac, file) == -1) | |
230 | return -1; | |
231 | } | |
232 | return 0; | |
233 | } | |
234 | ||
983def13 | 235 | static void |
236 | usage(void) | |
237 | { | |
33e766d2 | 238 | fprintf(stderr, "Usage: %s [options]\n", __progname); |
239 | fprintf(stderr, "Options:\n"); | |
240 | fprintf(stderr, " -l List fingerprints of all identities.\n"); | |
241 | fprintf(stderr, " -L List public key parameters of all identities.\n"); | |
242 | fprintf(stderr, " -d Delete identity.\n"); | |
243 | fprintf(stderr, " -D Delete all identities.\n"); | |
244 | #ifdef SMARTCARD | |
245 | fprintf(stderr, " -s reader Add key in smartcard reader.\n"); | |
246 | fprintf(stderr, " -e reader Remove key in smartcard reader.\n"); | |
247 | #endif | |
983def13 | 248 | } |
249 | ||
8efc0c15 | 250 | int |
5068f573 | 251 | main(int argc, char **argv) |
8efc0c15 | 252 | { |
34e02b83 | 253 | extern char *optarg; |
254 | extern int optind; | |
5260325f | 255 | AuthenticationConnection *ac = NULL; |
9ff6f66f | 256 | char *sc_reader_id = NULL; |
e0543e42 | 257 | int i, ch, deleting = 0, ret = 0; |
5260325f | 258 | |
260d427b | 259 | __progname = get_progname(argv[0]); |
264dce47 | 260 | init_rng(); |
044b0662 | 261 | seed_rng(); |
264dce47 | 262 | |
2b87da3b | 263 | SSLeay_add_all_algorithms(); |
2e73a022 | 264 | |
5260325f | 265 | /* At first, get a connection to the authentication agent. */ |
266 | ac = ssh_get_authentication_connection(); | |
267 | if (ac == NULL) { | |
268 | fprintf(stderr, "Could not open a connection to your authentication agent.\n"); | |
269 | exit(1); | |
8efc0c15 | 270 | } |
184eed6a | 271 | while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) { |
34e02b83 | 272 | switch (ch) { |
273 | case 'l': | |
274 | case 'L': | |
275 | list_identities(ac, ch == 'l' ? 1 : 0); | |
276 | goto done; | |
277 | break; | |
278 | case 'd': | |
5260325f | 279 | deleting = 1; |
34e02b83 | 280 | break; |
281 | case 'D': | |
e0543e42 | 282 | if (delete_all(ac) == -1) |
283 | ret = 1; | |
34e02b83 | 284 | goto done; |
285 | break; | |
286 | case 's': | |
9ff6f66f | 287 | sc_reader_id = optarg; |
34e02b83 | 288 | break; |
289 | case 'e': | |
184eed6a | 290 | deleting = 1; |
9ff6f66f | 291 | sc_reader_id = optarg; |
34e02b83 | 292 | break; |
293 | default: | |
294 | usage(); | |
e0543e42 | 295 | ret = 1; |
296 | goto done; | |
983def13 | 297 | } |
8efc0c15 | 298 | } |
34e02b83 | 299 | argc -= optind; |
300 | argv += optind; | |
9ff6f66f | 301 | if (sc_reader_id != NULL) { |
e0543e42 | 302 | if (update_card(ac, !deleting, sc_reader_id) == -1) |
303 | ret = 1; | |
34e02b83 | 304 | goto done; |
983def13 | 305 | } |
34e02b83 | 306 | if (argc == 0) { |
67656ffc | 307 | char buf[MAXPATHLEN]; |
308 | struct passwd *pw; | |
309 | ||
310 | if ((pw = getpwuid(getuid())) == NULL) { | |
14a9a859 | 311 | fprintf(stderr, "No user found with uid %u\n", |
312 | (u_int)getuid()); | |
e0543e42 | 313 | ret = 1; |
314 | goto done; | |
5260325f | 315 | } |
67656ffc | 316 | |
317 | for(i = 0; default_files[i]; i++) { | |
318 | snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, | |
319 | default_files[i]); | |
320 | if (do_file(ac, deleting, buf) == -1) | |
e0543e42 | 321 | ret = 1; |
322 | } | |
34e02b83 | 323 | } else { |
67656ffc | 324 | for(i = 0; i < argc; i++) { |
325 | if (do_file(ac, deleting, argv[1]) == -1) | |
326 | ret = 1; | |
34e02b83 | 327 | } |
8efc0c15 | 328 | } |
a8666d84 | 329 | clear_pass(); |
34e02b83 | 330 | |
331 | done: | |
5260325f | 332 | ssh_close_authentication_connection(ac); |
e0543e42 | 333 | return ret; |
8efc0c15 | 334 | } |