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