Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.43
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.43 ! markus 38: RCSID("$OpenBSD: ssh-add.c,v 1.42 2001/06/26 04:59:59 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: }
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.43 ! markus 208: int i, ch, deleting = 0, sc_reader_num = -1;
1.12 markus 209:
1.28 stevesk 210: SSLeay_add_all_algorithms();
1.19 markus 211:
1.12 markus 212: /* At first, get a connection to the authentication agent. */
213: ac = ssh_get_authentication_connection();
214: if (ac == NULL) {
215: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
216: exit(1);
217: }
1.43 ! markus 218: while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) {
! 219: switch (ch) {
! 220: case 'l':
! 221: case 'L':
! 222: list_identities(ac, ch == 'l' ? 1 : 0);
! 223: goto done;
! 224: break;
! 225: case 'd':
1.12 markus 226: deleting = 1;
1.43 ! markus 227: break;
! 228: case 'D':
1.12 markus 229: delete_all(ac);
1.43 ! markus 230: goto done;
! 231: break;
! 232: case 's':
! 233: sc_reader_num = atoi(optarg);
! 234: break;
! 235: case 'e':
1.42 markus 236: deleting = 1;
1.43 ! markus 237: sc_reader_num = atoi(optarg);
! 238: break;
! 239: default:
! 240: usage();
! 241: exit(1);
! 242: break;
1.42 markus 243: }
244: }
1.43 ! markus 245: argc -= optind;
! 246: argv += optind;
! 247: if (sc_reader_num != -1) {
1.42 markus 248: update_card(ac, !deleting, sc_reader_num);
1.43 ! markus 249: goto done;
1.12 markus 250: }
1.43 ! markus 251: if (argc == 0) {
1.12 markus 252: pw = getpwuid(getuid());
253: if (!pw) {
1.20 deraadt 254: fprintf(stderr, "No user found with uid %u\n",
255: (u_int)getuid());
1.12 markus 256: ssh_close_authentication_connection(ac);
257: exit(1);
258: }
1.25 markus 259: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.12 markus 260: if (deleting)
261: delete_file(ac, buf);
262: else
263: add_file(ac, buf);
1.43 ! markus 264: } else {
! 265: for (i = 0; i < argc; i++) {
! 266: if (deleting)
! 267: delete_file(ac, argv[i]);
! 268: else
! 269: add_file(ac, argv[i]);
! 270: }
1.12 markus 271: }
1.33 markus 272: clear_pass();
1.43 ! markus 273:
! 274: done:
1.12 markus 275: ssh_close_authentication_connection(ac);
276: exit(0);
1.1 deraadt 277: }