Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.42
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.42 ! markus 38: RCSID("$OpenBSD: ssh-add.c,v 1.41 2001/06/25 08:25:40 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.42 ! markus 147: update_card(AuthenticationConnection *ac, int add, int id)
! 148: {
! 149: if (ssh_update_card(ac, add, id))
! 150: fprintf(stderr, "Card %s: %d\n",
! 151: add ? "added" : "removed", id);
! 152: else
! 153: fprintf(stderr, "Could not %s card: %d\n",
! 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: exit (1);
! 199: }
! 200:
1.2 provos 201: int
1.7 markus 202: main(int argc, char **argv)
1.1 deraadt 203: {
1.12 markus 204: AuthenticationConnection *ac = NULL;
205: struct passwd *pw;
206: char buf[1024];
207: int no_files = 1;
208: int i;
209: int deleting = 0;
1.42 ! markus 210: int sc_mode = 0;
! 211: int sc_reader_num = 0;
1.12 markus 212:
1.28 stevesk 213: SSLeay_add_all_algorithms();
1.19 markus 214:
1.12 markus 215: /* At first, get a connection to the authentication agent. */
216: ac = ssh_get_authentication_connection();
217: if (ac == NULL) {
218: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
219: exit(1);
220: }
221: for (i = 1; i < argc; i++) {
222: if ((strcmp(argv[i], "-l") == 0) ||
223: (strcmp(argv[i], "-L") == 0)) {
224: list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
225: /* Don't default-add/delete if -l. */
226: no_files = 0;
227: continue;
228: }
229: if (strcmp(argv[i], "-d") == 0) {
230: deleting = 1;
231: continue;
232: }
233: if (strcmp(argv[i], "-D") == 0) {
234: delete_all(ac);
235: no_files = 0;
236: continue;
237: }
1.42 ! markus 238: if (strcmp(argv[i], "-s") == 0) {
! 239: sc_mode = 1;
! 240: deleting = 0;
! 241: i++;
! 242: if (i >= argc)
! 243: usage();
! 244: sc_reader_num = atoi(argv[i]);
! 245: continue;
! 246: }
! 247: if (strcmp(argv[i], "-e") == 0) {
! 248: sc_mode = 1;
! 249: deleting = 1;
! 250: i++;
! 251: if (i >= argc)
! 252: usage();
! 253: sc_reader_num = atoi(argv[i]);
! 254: continue;
! 255: }
! 256: if (sc_mode == 1)
! 257: update_card(ac, !deleting, sc_reader_num);
1.12 markus 258: no_files = 0;
259: if (deleting)
260: delete_file(ac, argv[i]);
261: else
262: add_file(ac, argv[i]);
1.42 ! markus 263: }
! 264: if (sc_mode == 1) {
! 265: update_card(ac, !deleting, sc_reader_num);
! 266: ssh_close_authentication_connection(ac);
! 267: exit(0);
1.12 markus 268: }
269: if (no_files) {
270: pw = getpwuid(getuid());
271: if (!pw) {
1.20 deraadt 272: fprintf(stderr, "No user found with uid %u\n",
273: (u_int)getuid());
1.12 markus 274: ssh_close_authentication_connection(ac);
275: exit(1);
276: }
1.25 markus 277: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.12 markus 278: if (deleting)
279: delete_file(ac, buf);
280: else
281: add_file(ac, buf);
282: }
1.33 markus 283: clear_pass();
1.12 markus 284: ssh_close_authentication_connection(ac);
285: exit(0);
1.1 deraadt 286: }