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