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