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.58 2002/06/15 00:01:36 markus Exp $");
40 #include <openssl/evp.h>
49 #include "pathnames.h"
53 #ifdef HAVE___PROGNAME
54 extern char *__progname;
60 extern char *__progname;
62 /* Default files to add */
63 static char *default_files[] = {
64 _PATH_SSH_CLIENT_ID_RSA,
65 _PATH_SSH_CLIENT_ID_DSA,
66 _PATH_SSH_CLIENT_IDENTITY,
70 /* Default lifetime (0 == forever) */
71 static int lifetime = 0;
73 /* we keep a cache of one passphrases */
74 static char *pass = NULL;
79 memset(pass, 0, strlen(pass));
86 delete_file(AuthenticationConnection *ac, const char *filename)
92 public = key_load_public(filename, &comment);
94 printf("Bad key file %s\n", filename);
97 if (ssh_remove_identity(ac, public)) {
98 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
101 fprintf(stderr, "Could not remove identity: %s\n", filename);
109 /* Send a request to remove all identities. */
111 delete_all(AuthenticationConnection *ac)
115 if (ssh_remove_all_identities(ac, 1))
117 /* ignore error-code for ssh2 */
118 ssh_remove_all_identities(ac, 2);
121 fprintf(stderr, "All identities removed.\n");
123 fprintf(stderr, "Failed to remove all identities.\n");
129 add_file(AuthenticationConnection *ac, const char *filename)
133 char *comment = NULL;
137 if (stat(filename, &st) < 0) {
141 /* At first, try empty passphrase */
142 private = key_load_private(filename, "", &comment);
144 comment = xstrdup(filename);
146 if (private == NULL && pass != NULL)
147 private = key_load_private(filename, pass, NULL);
148 if (private == NULL) {
149 /* clear passphrase since it did not work */
151 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
154 pass = read_passphrase(msg, RP_ALLOW_STDIN);
155 if (strcmp(pass, "") == 0) {
160 private = key_load_private(filename, pass, &comment);
164 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
167 if (ssh_add_identity(ac, private, comment)) {
168 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
171 fprintf(stderr, "Could not add identity: %s\n", filename);
173 if (ret == 0 && lifetime != 0) {
174 if (ssh_contrain_identity(ac, private, lifetime)) {
176 "Lifetime set to %d seconds for: %s (%s)\n",
177 lifetime, filename, comment);
180 "Could not set lifetime for identity: %s\n",
192 update_card(AuthenticationConnection *ac, int add, const char *id)
196 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
200 if (ssh_update_card(ac, add, id, pin)) {
201 fprintf(stderr, "Card %s: %s\n",
202 add ? "added" : "removed", id);
205 fprintf(stderr, "Could not %s card: %s\n",
206 add ? "add" : "remove", id);
212 list_identities(AuthenticationConnection *ac, int do_fp)
216 int had_identities = 0;
219 for (version = 1; version <= 2; version++) {
220 for (key = ssh_get_first_identity(ac, &comment, version);
222 key = ssh_get_next_identity(ac, &comment, version)) {
225 fp = key_fingerprint(key, SSH_FP_MD5,
227 printf("%d %s %s (%s)\n",
228 key_size(key), fp, comment, key_type(key));
231 if (!key_write(key, stdout))
232 fprintf(stderr, "key_write failed");
233 fprintf(stdout, " %s\n", comment);
239 if (!had_identities) {
240 printf("The agent has no identities.\n");
247 lock_agent(AuthenticationConnection *ac, int lock)
249 char prompt[100], *p1, *p2;
250 int passok = 1, ret = -1;
252 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
253 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
255 strlcpy(prompt, "Again: ", sizeof prompt);
256 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
257 if (strcmp(p1, p2) != 0) {
258 fprintf(stderr, "Passwords do not match.\n");
261 memset(p2, 0, strlen(p2));
264 if (passok && ssh_lock_agent(ac, lock, p1)) {
265 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
268 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
269 memset(p1, 0, strlen(p1));
275 do_file(AuthenticationConnection *ac, int deleting, char *file)
278 if (delete_file(ac, file) == -1)
281 if (add_file(ac, file) == -1)
290 fprintf(stderr, "Usage: %s [options]\n", __progname);
291 fprintf(stderr, "Options:\n");
292 fprintf(stderr, " -l List fingerprints of all identities.\n");
293 fprintf(stderr, " -L List public key parameters of all identities.\n");
294 fprintf(stderr, " -d Delete identity.\n");
295 fprintf(stderr, " -D Delete all identities.\n");
296 fprintf(stderr, " -x Lock agent.\n");
297 fprintf(stderr, " -x Unlock agent.\n");
298 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
300 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
301 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
306 main(int argc, char **argv)
310 AuthenticationConnection *ac = NULL;
311 char *sc_reader_id = NULL;
312 int i, ch, deleting = 0, ret = 0;
314 __progname = get_progname(argv[0]);
318 SSLeay_add_all_algorithms();
320 /* At first, get a connection to the authentication agent. */
321 ac = ssh_get_authentication_connection();
323 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
326 while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
330 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
336 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
344 if (delete_all(ac) == -1)
349 sc_reader_id = optarg;
353 sc_reader_id = optarg;
356 if ((lifetime = convtime(optarg)) == -1) {
357 fprintf(stderr, "Invalid lifetime\n");
370 if (sc_reader_id != NULL) {
371 if (update_card(ac, !deleting, sc_reader_id) == -1)
376 char buf[MAXPATHLEN];
381 if ((pw = getpwuid(getuid())) == NULL) {
382 fprintf(stderr, "No user found with uid %u\n",
388 for(i = 0; default_files[i]; i++) {
389 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
391 if (stat(buf, &st) < 0)
393 if (do_file(ac, deleting, buf) == -1)
401 for(i = 0; i < argc; i++) {
402 if (do_file(ac, deleting, argv[i]) == -1)
409 ssh_close_authentication_connection(ac);