2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Adds an identity to the authentication server, or removes an identity.
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".
13 * SSH2 implementation,
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
38 RCSID("$OpenBSD: ssh-add.c,v 1.46 2001/10/02 08:38:50 djm Exp $");
40 #include <openssl/evp.h>
49 #include "pathnames.h"
52 #ifdef HAVE___PROGNAME
53 extern char *__progname;
59 extern char *__progname;
61 /* we keep a cache of one passphrases */
62 static char *pass = NULL;
67 memset(pass, 0, strlen(pass));
74 delete_file(AuthenticationConnection *ac, const char *filename)
80 public = key_load_public(filename, &comment);
82 printf("Bad key file %s\n", filename);
85 if (ssh_remove_identity(ac, public)) {
86 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
89 fprintf(stderr, "Could not remove identity: %s\n", filename);
97 /* Send a request to remove all identities. */
99 delete_all(AuthenticationConnection *ac)
103 if (ssh_remove_all_identities(ac, 1))
105 /* ignore error-code for ssh2 */
106 ssh_remove_all_identities(ac, 2);
109 fprintf(stderr, "All identities removed.\n");
111 fprintf(stderr, "Failed to remove all identities.\n");
117 add_file(AuthenticationConnection *ac, const char *filename)
121 char *comment = NULL;
125 if (stat(filename, &st) < 0) {
129 /* At first, try empty passphrase */
130 private = key_load_private(filename, "", &comment);
132 comment = xstrdup(filename);
134 if (private == NULL && pass != NULL)
135 private = key_load_private(filename, pass, NULL);
136 if (private == NULL) {
137 /* clear passphrase since it did not work */
139 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
142 pass = read_passphrase(msg, RP_ALLOW_STDIN);
143 if (strcmp(pass, "") == 0) {
148 private = key_load_private(filename, pass, &comment);
152 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
155 if (ssh_add_identity(ac, private, comment)) {
156 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
159 fprintf(stderr, "Could not add identity: %s\n", filename);
168 update_card(AuthenticationConnection *ac, int add, const char *id)
170 if (ssh_update_card(ac, add, id)) {
171 fprintf(stderr, "Card %s: %s\n",
172 add ? "added" : "removed", id);
175 fprintf(stderr, "Could not %s card: %s\n",
176 add ? "add" : "remove", id);
182 list_identities(AuthenticationConnection *ac, int do_fp)
186 int had_identities = 0;
189 for (version = 1; version <= 2; version++) {
190 for (key = ssh_get_first_identity(ac, &comment, version);
192 key = ssh_get_next_identity(ac, &comment, version)) {
195 fp = key_fingerprint(key, SSH_FP_MD5,
197 printf("%d %s %s (%s)\n",
198 key_size(key), fp, comment, key_type(key));
201 if (!key_write(key, stdout))
202 fprintf(stderr, "key_write failed");
203 fprintf(stdout, " %s\n", comment);
210 printf("The agent has no identities.\n");
216 fprintf(stderr, "Usage: %s [options]\n", __progname);
217 fprintf(stderr, "Options:\n");
218 fprintf(stderr, " -l List fingerprints of all identities.\n");
219 fprintf(stderr, " -L List public key parameters of all identities.\n");
220 fprintf(stderr, " -d Delete identity.\n");
221 fprintf(stderr, " -D Delete all identities.\n");
223 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
224 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
229 main(int argc, char **argv)
233 AuthenticationConnection *ac = NULL;
236 char *sc_reader_id = NULL;
237 int i, ch, deleting = 0, ret = 0;
239 __progname = get_progname(argv[0]);
243 SSLeay_add_all_algorithms();
245 /* At first, get a connection to the authentication agent. */
246 ac = ssh_get_authentication_connection();
248 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
251 while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) {
255 list_identities(ac, ch == 'l' ? 1 : 0);
262 if (delete_all(ac) == -1)
267 sc_reader_id = optarg;
271 sc_reader_id = optarg;
281 if (sc_reader_id != NULL) {
282 if (update_card(ac, !deleting, sc_reader_id) == -1)
287 pw = getpwuid(getuid());
289 fprintf(stderr, "No user found with uid %u\n",
294 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
296 if (delete_file(ac, buf) == -1)
299 if (add_file(ac, buf) == -1)
303 for (i = 0; i < argc; i++) {
305 if (delete_file(ac, argv[i]) == -1)
308 if (add_file(ac, argv[i]) == -1)
316 ssh_close_authentication_connection(ac);