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