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