Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.46
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.46 ! djm 38: RCSID("$OpenBSD: ssh-add.c,v 1.45 2001/08/03 10:31:30 jakob 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.1 deraadt 51:
1.45 jakob 52: /* argv0 */
53: extern char *__progname;
54:
1.33 markus 55: /* we keep a cache of one passphrases */
56: static char *pass = NULL;
1.39 itojun 57: static void
1.33 markus 58: clear_pass(void)
59: {
60: if (pass) {
61: memset(pass, 0, strlen(pass));
62: xfree(pass);
63: pass = NULL;
64: }
65: }
66:
1.46 ! djm 67: static int
1.7 markus 68: delete_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 69: {
1.16 markus 70: Key *public;
1.32 markus 71: char *comment = NULL;
1.46 ! djm 72: int ret = -1;
1.1 deraadt 73:
1.31 markus 74: public = key_load_public(filename, &comment);
75: if (public == NULL) {
76: printf("Bad key file %s\n", filename);
1.46 ! djm 77: return -1;
1.12 markus 78: }
1.46 ! djm 79: if (ssh_remove_identity(ac, public)) {
1.12 markus 80: fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
1.46 ! djm 81: ret = 0;
! 82: } else
1.12 markus 83: fprintf(stderr, "Could not remove identity: %s\n", filename);
1.46 ! djm 84:
1.16 markus 85: key_free(public);
1.12 markus 86: xfree(comment);
1.46 ! djm 87:
! 88: return ret;
1.1 deraadt 89: }
90:
1.19 markus 91: /* Send a request to remove all identities. */
1.46 ! djm 92: static int
1.7 markus 93: delete_all(AuthenticationConnection *ac)
1.1 deraadt 94: {
1.46 ! djm 95: int ret = -1;
1.19 markus 96:
1.46 ! djm 97: if (ssh_remove_all_identities(ac, 1))
! 98: ret = 0;
1.19 markus 99: /* ignore error-code for ssh2 */
100: ssh_remove_all_identities(ac, 2);
101:
1.46 ! djm 102: if (ret == 0)
1.12 markus 103: fprintf(stderr, "All identities removed.\n");
104: else
1.24 markus 105: fprintf(stderr, "Failed to remove all identities.\n");
1.46 ! djm 106:
! 107: return ret;
1.1 deraadt 108: }
109:
1.46 ! djm 110: static int
1.7 markus 111: add_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 112: {
1.19 markus 113: struct stat st;
1.16 markus 114: Key *private;
1.36 markus 115: char *comment = NULL;
116: char msg[1024];
1.46 ! djm 117: int ret = -1;
1.12 markus 118:
1.19 markus 119: if (stat(filename, &st) < 0) {
120: perror(filename);
1.46 ! djm 121: return -1;
1.19 markus 122: }
1.12 markus 123: /* At first, try empty passphrase */
1.31 markus 124: private = key_load_private(filename, "", &comment);
125: if (comment == NULL)
126: comment = xstrdup(filename);
1.33 markus 127: /* try last */
128: if (private == NULL && pass != NULL)
129: private = key_load_private(filename, pass, NULL);
1.31 markus 130: if (private == NULL) {
1.33 markus 131: /* clear passphrase since it did not work */
132: clear_pass();
1.37 markus 133: snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.36 markus 134: comment);
1.12 markus 135: for (;;) {
1.40 markus 136: pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.12 markus 137: if (strcmp(pass, "") == 0) {
1.35 markus 138: clear_pass();
1.31 markus 139: xfree(comment);
1.46 ! djm 140: return -1;
1.12 markus 141: }
1.31 markus 142: private = key_load_private(filename, pass, &comment);
143: if (private != NULL)
1.12 markus 144: break;
1.33 markus 145: clear_pass();
1.37 markus 146: strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
1.12 markus 147: }
148: }
1.46 ! djm 149: if (ssh_add_identity(ac, private, comment)) {
1.31 markus 150: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.46 ! djm 151: ret = 0;
! 152: } else
1.12 markus 153: fprintf(stderr, "Could not add identity: %s\n", filename);
1.46 ! djm 154:
1.31 markus 155: xfree(comment);
1.16 markus 156: key_free(private);
1.46 ! djm 157:
! 158: return ret;
1.1 deraadt 159: }
160:
1.46 ! djm 161: static int
1.44 markus 162: update_card(AuthenticationConnection *ac, int add, const char *id)
1.42 markus 163: {
1.46 ! djm 164: if (ssh_update_card(ac, add, id)) {
1.44 markus 165: fprintf(stderr, "Card %s: %s\n",
1.42 markus 166: add ? "added" : "removed", id);
1.46 ! djm 167: return 0;
! 168: } else {
1.44 markus 169: fprintf(stderr, "Could not %s card: %s\n",
1.42 markus 170: add ? "add" : "remove", id);
1.46 ! djm 171: return -1;
! 172: }
1.42 markus 173: }
174:
175: static void
1.30 markus 176: list_identities(AuthenticationConnection *ac, int do_fp)
1.1 deraadt 177: {
1.19 markus 178: Key *key;
1.30 markus 179: char *comment, *fp;
1.19 markus 180: int had_identities = 0;
181: int version;
1.12 markus 182:
1.19 markus 183: for (version = 1; version <= 2; version++) {
184: for (key = ssh_get_first_identity(ac, &comment, version);
185: key != NULL;
186: key = ssh_get_next_identity(ac, &comment, version)) {
187: had_identities = 1;
1.30 markus 188: if (do_fp) {
189: fp = key_fingerprint(key, SSH_FP_MD5,
190: SSH_FP_HEX);
1.23 markus 191: printf("%d %s %s (%s)\n",
1.30 markus 192: key_size(key), fp, comment, key_type(key));
193: xfree(fp);
1.12 markus 194: } else {
1.19 markus 195: if (!key_write(key, stdout))
196: fprintf(stderr, "key_write failed");
197: fprintf(stdout, " %s\n", comment);
1.12 markus 198: }
1.19 markus 199: key_free(key);
200: xfree(comment);
1.12 markus 201: }
202: }
203: if (!had_identities)
204: printf("The agent has no identities.\n");
1.1 deraadt 205: }
206:
1.42 markus 207: static void
208: usage(void)
209: {
1.45 jakob 210: fprintf(stderr, "Usage: %s [options]\n", __progname);
211: fprintf(stderr, "Options:\n");
212: fprintf(stderr, " -l List fingerprints of all identities.\n");
213: fprintf(stderr, " -L List public key parameters of all identities.\n");
214: fprintf(stderr, " -d Delete identity.\n");
215: fprintf(stderr, " -D Delete all identities.\n");
216: #ifdef SMARTCARD
217: fprintf(stderr, " -s reader Add key in smartcard reader.\n");
218: fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
219: #endif
1.42 markus 220: }
221:
1.2 provos 222: int
1.7 markus 223: main(int argc, char **argv)
1.1 deraadt 224: {
1.43 markus 225: extern char *optarg;
226: extern int optind;
1.12 markus 227: AuthenticationConnection *ac = NULL;
228: struct passwd *pw;
229: char buf[1024];
1.44 markus 230: char *sc_reader_id = NULL;
1.46 ! djm 231: int i, ch, deleting = 0, ret = 0;
1.12 markus 232:
1.28 stevesk 233: SSLeay_add_all_algorithms();
1.19 markus 234:
1.12 markus 235: /* At first, get a connection to the authentication agent. */
236: ac = ssh_get_authentication_connection();
237: if (ac == NULL) {
238: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
239: exit(1);
240: }
1.43 markus 241: while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) {
242: switch (ch) {
243: case 'l':
244: case 'L':
245: list_identities(ac, ch == 'l' ? 1 : 0);
246: goto done;
247: break;
248: case 'd':
1.12 markus 249: deleting = 1;
1.43 markus 250: break;
251: case 'D':
1.46 ! djm 252: if (delete_all(ac) == -1)
! 253: ret = 1;
1.43 markus 254: goto done;
255: break;
256: case 's':
1.44 markus 257: sc_reader_id = optarg;
1.43 markus 258: break;
259: case 'e':
1.42 markus 260: deleting = 1;
1.44 markus 261: sc_reader_id = optarg;
1.43 markus 262: break;
263: default:
264: usage();
1.46 ! djm 265: ret = 1;
! 266: goto done;
1.42 markus 267: }
268: }
1.43 markus 269: argc -= optind;
270: argv += optind;
1.44 markus 271: if (sc_reader_id != NULL) {
1.46 ! djm 272: if (update_card(ac, !deleting, sc_reader_id) == -1)
! 273: ret = 1;
1.43 markus 274: goto done;
1.12 markus 275: }
1.43 markus 276: if (argc == 0) {
1.12 markus 277: pw = getpwuid(getuid());
278: if (!pw) {
1.20 deraadt 279: fprintf(stderr, "No user found with uid %u\n",
280: (u_int)getuid());
1.46 ! djm 281: ret = 1;
! 282: goto done;
1.12 markus 283: }
1.25 markus 284: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.46 ! djm 285: if (deleting) {
! 286: if (delete_file(ac, buf) == -1)
! 287: ret = 1;
! 288: } else {
! 289: if (add_file(ac, buf) == -1)
! 290: ret = 1;
! 291: }
1.43 markus 292: } else {
293: for (i = 0; i < argc; i++) {
1.46 ! djm 294: if (deleting) {
! 295: if (delete_file(ac, argv[i]) == -1)
! 296: ret = 1;
! 297: } else {
! 298: if (add_file(ac, argv[i]) == -1)
! 299: ret = 1;
! 300: }
1.43 markus 301: }
1.12 markus 302: }
1.33 markus 303: clear_pass();
1.43 markus 304:
305: done:
1.12 markus 306: ssh_close_authentication_connection(ac);
1.46 ! djm 307: return ret;
1.1 deraadt 308: }