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