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