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