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