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