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