Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.76
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.76 ! dtucker 38: RCSID("$OpenBSD: ssh-add.c,v 1.75 2006/02/20 17:19:54 stevesk Exp $");
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.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.16 markus 130: Key *private;
1.36 markus 131: char *comment = NULL;
132: char msg[1024];
1.76 ! dtucker 133: int fd, perms_ok, ret = -1;
1.12 markus 134:
1.76 ! dtucker 135: if ((fd = open(filename, 0)) < 0) {
1.19 markus 136: perror(filename);
1.46 djm 137: return -1;
1.19 markus 138: }
1.76 ! dtucker 139:
! 140: /*
! 141: * Since we'll try to load a keyfile multiple times, permission errors
! 142: * will occur multiple times, so check perms first and bail if wrong.
! 143: */
! 144: perms_ok = key_perm_ok(fd, filename);
! 145: close(fd);
! 146: if (!perms_ok)
! 147: return -1;
! 148:
1.12 markus 149: /* At first, try empty passphrase */
1.31 markus 150: private = key_load_private(filename, "", &comment);
151: if (comment == NULL)
152: comment = xstrdup(filename);
1.33 markus 153: /* try last */
154: if (private == NULL && pass != NULL)
155: private = key_load_private(filename, pass, NULL);
1.31 markus 156: if (private == NULL) {
1.33 markus 157: /* clear passphrase since it did not work */
158: clear_pass();
1.37 markus 159: snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.72 djm 160: comment);
1.12 markus 161: for (;;) {
1.40 markus 162: pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.12 markus 163: if (strcmp(pass, "") == 0) {
1.35 markus 164: clear_pass();
1.31 markus 165: xfree(comment);
1.46 djm 166: return -1;
1.12 markus 167: }
1.31 markus 168: private = key_load_private(filename, pass, &comment);
169: if (private != NULL)
1.12 markus 170: break;
1.33 markus 171: clear_pass();
1.68 markus 172: snprintf(msg, sizeof msg,
173: "Bad passphrase, try again for %.200s: ", comment);
1.12 markus 174: }
175: }
1.60 markus 176:
1.69 djm 177: if (ssh_add_identity_constrained(ac, private, comment, lifetime,
178: confirm)) {
1.60 markus 179: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
180: ret = 0;
181: if (lifetime != 0)
1.64 deraadt 182: fprintf(stderr,
1.60 markus 183: "Lifetime set to %d seconds\n", lifetime);
1.69 djm 184: if (confirm != 0)
1.65 markus 185: fprintf(stderr,
186: "The user has to confirm each use of the key\n");
1.60 markus 187: } else if (ssh_add_identity(ac, private, comment)) {
1.31 markus 188: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.46 djm 189: ret = 0;
1.60 markus 190: } else {
1.12 markus 191: fprintf(stderr, "Could not add identity: %s\n", filename);
1.56 markus 192: }
193:
1.31 markus 194: xfree(comment);
1.16 markus 195: key_free(private);
1.47 deraadt 196:
1.46 djm 197: return ret;
1.1 deraadt 198: }
199:
1.46 djm 200: static int
1.44 markus 201: update_card(AuthenticationConnection *ac, int add, const char *id)
1.42 markus 202: {
1.53 rees 203: char *pin;
1.66 markus 204: int ret = -1;
1.53 rees 205:
206: pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
207: if (pin == NULL)
208: return -1;
209:
1.67 djm 210: if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
1.44 markus 211: fprintf(stderr, "Card %s: %s\n",
1.47 deraadt 212: add ? "added" : "removed", id);
1.66 markus 213: ret = 0;
1.46 djm 214: } else {
1.44 markus 215: fprintf(stderr, "Could not %s card: %s\n",
1.47 deraadt 216: add ? "add" : "remove", id);
1.66 markus 217: ret = -1;
1.46 djm 218: }
1.66 markus 219: xfree(pin);
220: return ret;
1.42 markus 221: }
222:
1.50 markus 223: static int
1.30 markus 224: list_identities(AuthenticationConnection *ac, int do_fp)
1.1 deraadt 225: {
1.19 markus 226: Key *key;
1.30 markus 227: char *comment, *fp;
1.19 markus 228: int had_identities = 0;
229: int version;
1.12 markus 230:
1.19 markus 231: for (version = 1; version <= 2; version++) {
232: for (key = ssh_get_first_identity(ac, &comment, version);
1.47 deraadt 233: key != NULL;
234: key = ssh_get_next_identity(ac, &comment, version)) {
1.19 markus 235: had_identities = 1;
1.30 markus 236: if (do_fp) {
237: fp = key_fingerprint(key, SSH_FP_MD5,
238: SSH_FP_HEX);
1.23 markus 239: printf("%d %s %s (%s)\n",
1.30 markus 240: key_size(key), fp, comment, key_type(key));
241: xfree(fp);
1.12 markus 242: } else {
1.19 markus 243: if (!key_write(key, stdout))
244: fprintf(stderr, "key_write failed");
245: fprintf(stdout, " %s\n", comment);
1.12 markus 246: }
1.19 markus 247: key_free(key);
248: xfree(comment);
1.12 markus 249: }
250: }
1.50 markus 251: if (!had_identities) {
1.12 markus 252: printf("The agent has no identities.\n");
1.50 markus 253: return -1;
254: }
255: return 0;
1.1 deraadt 256: }
257:
1.48 djm 258: static int
1.54 markus 259: lock_agent(AuthenticationConnection *ac, int lock)
260: {
261: char prompt[100], *p1, *p2;
262: int passok = 1, ret = -1;
1.61 deraadt 263:
1.54 markus 264: strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
265: p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
266: if (lock) {
267: strlcpy(prompt, "Again: ", sizeof prompt);
268: p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
269: if (strcmp(p1, p2) != 0) {
270: fprintf(stderr, "Passwords do not match.\n");
271: passok = 0;
272: }
273: memset(p2, 0, strlen(p2));
274: xfree(p2);
275: }
276: if (passok && ssh_lock_agent(ac, lock, p1)) {
277: fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
278: ret = 0;
279: } else
280: fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
281: memset(p1, 0, strlen(p1));
282: xfree(p1);
1.62 markus 283: return (ret);
1.54 markus 284: }
285:
286: static int
1.48 djm 287: do_file(AuthenticationConnection *ac, int deleting, char *file)
288: {
289: if (deleting) {
290: if (delete_file(ac, file) == -1)
291: return -1;
292: } else {
293: if (add_file(ac, file) == -1)
294: return -1;
295: }
296: return 0;
297: }
298:
1.42 markus 299: static void
300: usage(void)
301: {
1.45 jakob 302: fprintf(stderr, "Usage: %s [options]\n", __progname);
303: fprintf(stderr, "Options:\n");
304: fprintf(stderr, " -l List fingerprints of all identities.\n");
305: fprintf(stderr, " -L List public key parameters of all identities.\n");
306: fprintf(stderr, " -d Delete identity.\n");
307: fprintf(stderr, " -D Delete all identities.\n");
1.55 markus 308: fprintf(stderr, " -x Lock agent.\n");
1.63 markus 309: fprintf(stderr, " -X Unlock agent.\n");
1.56 markus 310: fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
1.65 markus 311: fprintf(stderr, " -c Require confirmation to sign using identities\n");
1.45 jakob 312: #ifdef SMARTCARD
313: fprintf(stderr, " -s reader Add key in smartcard reader.\n");
314: fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
315: #endif
1.42 markus 316: }
317:
1.2 provos 318: int
1.7 markus 319: main(int argc, char **argv)
1.1 deraadt 320: {
1.43 markus 321: extern char *optarg;
322: extern int optind;
1.12 markus 323: AuthenticationConnection *ac = NULL;
1.44 markus 324: char *sc_reader_id = NULL;
1.46 djm 325: int i, ch, deleting = 0, ret = 0;
1.73 djm 326:
327: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
328: sanitise_stdfd();
1.12 markus 329:
1.28 stevesk 330: SSLeay_add_all_algorithms();
1.19 markus 331:
1.12 markus 332: /* At first, get a connection to the authentication agent. */
333: ac = ssh_get_authentication_connection();
334: if (ac == NULL) {
1.74 deraadt 335: fprintf(stderr,
336: "Could not open a connection to your authentication agent.\n");
1.50 markus 337: exit(2);
1.12 markus 338: }
1.65 markus 339: while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
1.43 markus 340: switch (ch) {
341: case 'l':
342: case 'L':
1.50 markus 343: if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
1.54 markus 344: ret = 1;
345: goto done;
346: break;
347: case 'x':
348: case 'X':
349: if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
1.50 markus 350: ret = 1;
1.43 markus 351: goto done;
1.65 markus 352: break;
353: case 'c':
354: confirm = 1;
1.43 markus 355: break;
356: case 'd':
1.12 markus 357: deleting = 1;
1.43 markus 358: break;
359: case 'D':
1.46 djm 360: if (delete_all(ac) == -1)
361: ret = 1;
1.43 markus 362: goto done;
363: break;
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: }