blob: 8f369be4d08d18aa4ed27887b13cbcbd3512b612 [file] [log] [blame]
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001/* $OpenBSD: ssh-add.c,v 1.115 2014/12/21 22:27:56 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Adds an identity to the authentication server, or removes an identity.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Damien Millerad833b32000-08-23 10:46:23 +100014 * SSH2 implementation,
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110039
40#include <sys/types.h>
41#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100042#include <sys/param.h>
Damien Millereba71ba2000-04-29 23:57:08 +100043
Damien Millerad833b32000-08-23 10:46:23 +100044#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110045#include "openbsd-compat/openssl-compat.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Miller57cf6382006-07-10 21:13:46 +100047#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100048#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100049#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100050#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100051#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100053#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100054
Damien Millerd7834352006-08-05 12:39:39 +100055#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "rsa.h"
58#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100060#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100061#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100062#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000064#include "misc.h"
Damien Miller86687062014-07-02 15:28:02 +100065#include "ssherr.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000066#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Ben Lindstromddfb1e32001-08-06 22:06:35 +000068/* argv0 */
69extern char *__progname;
70
Damien Miller6e1057c2002-01-22 23:05:59 +110071/* Default files to add */
72static char *default_files[] = {
Damien Miller72ef7c12015-01-15 02:21:31 +110073#ifdef WITH_OPENSSL
Damien Miller6e1057c2002-01-22 23:05:59 +110074 _PATH_SSH_CLIENT_ID_RSA,
75 _PATH_SSH_CLIENT_ID_DSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110076#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100077 _PATH_SSH_CLIENT_ID_ECDSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110078#endif
Damien Miller72ef7c12015-01-15 02:21:31 +110079#endif /* WITH_OPENSSL */
Damien Miller059321d2013-12-18 17:49:48 +110080 _PATH_SSH_CLIENT_ID_ED25519,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000081 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110082 NULL
83};
84
djm@openbsd.org56d1c832014-12-21 22:27:55 +000085static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
86
Ben Lindstrom61d328a2002-06-06 21:54:57 +000087/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000088static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110089
Damien Miller6c711792003-01-24 11:36:23 +110090/* User has to confirm key use */
91static int confirm = 0;
92
Ben Lindstromee617942001-04-10 02:45:32 +000093/* we keep a cache of one passphrases */
94static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000095static void
Ben Lindstromee617942001-04-10 02:45:32 +000096clear_pass(void)
97{
98 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +110099 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000100 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +0000101 pass = NULL;
102 }
103}
104
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000105static int
Damien Miller33a81362012-12-03 09:50:24 +1100106delete_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107{
Damien Miller33a81362012-12-03 09:50:24 +1100108 Key *public = NULL, *cert = NULL;
109 char *certpath = NULL, *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000110 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Ben Lindstromd0fca422001-03-26 13:44:06 +0000112 public = key_load_public(filename, &comment);
113 if (public == NULL) {
114 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000115 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100116 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000117 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100118 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000119 ret = 0;
120 } else
Damien Miller95def091999-11-25 00:26:21 +1100121 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000122
Damien Miller33a81362012-12-03 09:50:24 +1100123 if (key_only)
124 goto out;
125
126 /* Now try to delete the corresponding certificate too */
127 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100128 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100129 xasprintf(&certpath, "%s-cert.pub", filename);
130 if ((cert = key_load_public(certpath, &comment)) == NULL)
131 goto out;
132 if (!key_equal_public(cert, public))
133 fatal("Certificate %s does not match private key %s",
134 certpath, filename);
135
136 if (ssh_remove_identity(ac, cert)) {
137 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
138 comment);
139 ret = 0;
140 } else
141 fprintf(stderr, "Could not remove identity: %s\n", certpath);
142
143 out:
144 if (cert != NULL)
145 key_free(cert);
146 if (public != NULL)
147 key_free(public);
148 free(certpath);
149 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100150
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000151 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152}
153
Damien Millerad833b32000-08-23 10:46:23 +1000154/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000155static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000156delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000158 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000159
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000160 if (ssh_remove_all_identities(ac, 1))
161 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000162 /* ignore error-code for ssh2 */
163 ssh_remove_all_identities(ac, 2);
164
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000165 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100166 fprintf(stderr, "All identities removed.\n");
167 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000168 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000169
170 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171}
172
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000173static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100174add_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175{
Damien Miller0a80ca12010-02-27 07:55:05 +1100176 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000177 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100178 char msg[1024], *certpath = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000179 int r, fd, perms_ok, ret = -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000180 Buffer keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181
Damien Miller2ce12ef2011-05-05 14:17:18 +1000182 if (strcmp(filename, "-") == 0) {
183 fd = STDIN_FILENO;
184 filename = "(stdin)";
185 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000186 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000187 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000188 }
Damien Miller8275fad2006-03-15 12:06:23 +1100189
190 /*
191 * Since we'll try to load a keyfile multiple times, permission errors
192 * will occur multiple times, so check perms first and bail if wrong.
193 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000194 if (fd != STDIN_FILENO) {
195 perms_ok = key_perm_ok(fd, filename);
196 if (!perms_ok) {
197 close(fd);
198 return -1;
199 }
200 }
201 buffer_init(&keyblob);
202 if (!key_load_file(fd, filename, &keyblob)) {
203 buffer_free(&keyblob);
204 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100205 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000206 }
207 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100208
Damien Miller95def091999-11-25 00:26:21 +1100209 /* At first, try empty passphrase */
Damien Millerec3d0e22014-07-02 15:30:00 +1000210 if ((r = sshkey_parse_private_fileblob(&keyblob, "", filename,
Damien Miller86687062014-07-02 15:28:02 +1000211 &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE)
Damien Millerec3d0e22014-07-02 15:30:00 +1000212 fatal("Cannot parse %s: %s", filename, ssh_err(r));
Ben Lindstromee617942001-04-10 02:45:32 +0000213 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000214 if (private == NULL && pass != NULL) {
Damien Millerec3d0e22014-07-02 15:30:00 +1000215 if ((r = sshkey_parse_private_fileblob(&keyblob, pass, filename,
Damien Miller86687062014-07-02 15:28:02 +1000216 &private, &comment)) != 0 &&
217 r != SSH_ERR_KEY_WRONG_PASSPHRASE)
218 fatal("Cannot parse %s: %s", filename, ssh_err(r));
219 }
Damien Miller1d0df322014-07-11 09:19:04 +1000220 if (comment == NULL)
221 comment = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000222 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000223 /* clear passphrase since it did not work */
224 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000225 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000226 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100227 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000228 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100229 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000230 clear_pass();
Darren Tuckera627d422013-06-02 07:31:17 +1000231 free(comment);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000232 buffer_free(&keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000233 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100234 }
Damien Miller86687062014-07-02 15:28:02 +1000235 if ((r = sshkey_parse_private_fileblob(&keyblob,
Damien Millerec3d0e22014-07-02 15:30:00 +1000236 pass, filename, &private, NULL)) != 0 &&
Damien Miller86687062014-07-02 15:28:02 +1000237 r != SSH_ERR_KEY_WRONG_PASSPHRASE)
238 fatal("Cannot parse %s: %s",
239 filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000240 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100241 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000242 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000243 snprintf(msg, sizeof msg,
244 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100245 }
246 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000247 buffer_free(&keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000248
Damien Millera8e06ce2003-11-21 23:48:55 +1100249 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
250 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000251 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000252 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000253 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000254 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000255 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100256 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100257 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000258 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000259 } else {
Damien Miller95def091999-11-25 00:26:21 +1100260 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000261 }
262
Damien Miller8f4279e2011-10-18 16:06:33 +1100263 /* Skip trying to load the cert if requested */
264 if (key_only)
265 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100266
267 /* Now try to add the certificate flavour too */
268 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000269 if ((cert = key_load_public(certpath, NULL)) == NULL)
270 goto out;
271
272 if (!key_equal_public(cert, private)) {
273 error("Certificate %s does not match private key %s",
274 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100275 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000276 goto out;
277 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100278
Damien Millerc6afb5f2010-05-21 14:56:47 +1000279 /* Graft with private bits */
280 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
281 error("%s: key_to_certified failed", __func__);
282 key_free(cert);
283 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100284 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000285 key_cert_copy(cert, private);
286 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100287
Damien Millerc6afb5f2010-05-21 14:56:47 +1000288 if (!ssh_add_identity_constrained(ac, private, comment,
289 lifetime, confirm)) {
290 error("Certificate %s (%s) add failed", certpath,
291 private->cert->key_id);
292 }
293 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
294 private->cert->key_id);
295 if (lifetime != 0)
296 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
297 if (confirm != 0)
298 fprintf(stderr, "The user must confirm each use of the key\n");
299 out:
Damien Miller8f4279e2011-10-18 16:06:33 +1100300 if (certpath != NULL)
Darren Tuckera627d422013-06-02 07:31:17 +1000301 free(certpath);
302 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000303 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100304
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000305 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000308static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000309update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000310{
Damien Milleref275ea2013-12-29 17:44:07 +1100311 char *pin = NULL;
Damien Miller00111382003-03-10 11:21:17 +1100312 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000313
Damien Milleref275ea2013-12-29 17:44:07 +1100314 if (add) {
315 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
316 RP_ALLOW_STDIN)) == NULL)
317 return -1;
318 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000319
Damien Milleref275ea2013-12-29 17:44:07 +1100320 if (ssh_update_card(ac, add, id, pin == NULL ? "" : pin,
321 lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000322 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100323 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100324 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000325 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000326 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100327 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100328 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000329 }
Darren Tuckera627d422013-06-02 07:31:17 +1000330 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100331 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000332}
333
Damien Miller43cba342002-02-05 12:12:49 +1100334static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000335list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336{
Damien Millerad833b32000-08-23 10:46:23 +1000337 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000338 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000339 int had_identities = 0;
340 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Millerad833b32000-08-23 10:46:23 +1000342 for (version = 1; version <= 2; version++) {
343 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100344 key != NULL;
345 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000346 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000347 if (do_fp) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000348 fp = key_fingerprint(key, fingerprint_hash,
349 SSH_FP_DEFAULT);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100350 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000351 key_size(key), fp, comment, key_type(key));
Darren Tuckera627d422013-06-02 07:31:17 +1000352 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100353 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000354 if (!key_write(key, stdout))
355 fprintf(stderr, "key_write failed");
356 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100357 }
Damien Millerad833b32000-08-23 10:46:23 +1000358 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000359 free(comment);
Damien Miller95def091999-11-25 00:26:21 +1100360 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100361 }
Damien Miller43cba342002-02-05 12:12:49 +1100362 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100363 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100364 return -1;
365 }
366 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367}
368
Damien Miller6e1057c2002-01-22 23:05:59 +1100369static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000370lock_agent(AuthenticationConnection *ac, int lock)
371{
372 char prompt[100], *p1, *p2;
373 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000374
Ben Lindstrom2f717042002-06-06 21:52:03 +0000375 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
376 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
377 if (lock) {
378 strlcpy(prompt, "Again: ", sizeof prompt);
379 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
380 if (strcmp(p1, p2) != 0) {
381 fprintf(stderr, "Passwords do not match.\n");
382 passok = 0;
383 }
Damien Millera5103f42014-02-04 11:20:14 +1100384 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000385 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000386 }
387 if (passok && ssh_lock_agent(ac, lock, p1)) {
388 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
389 ret = 0;
390 } else
391 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
Damien Millera5103f42014-02-04 11:20:14 +1100392 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000394 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000395}
396
397static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100398do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100399{
400 if (deleting) {
Damien Miller33a81362012-12-03 09:50:24 +1100401 if (delete_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100402 return -1;
403 } else {
Damien Miller8f4279e2011-10-18 16:06:33 +1100404 if (add_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100405 return -1;
406 }
407 return 0;
408}
409
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000410static void
411usage(void)
412{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000413 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000414 fprintf(stderr, "Options:\n");
415 fprintf(stderr, " -l List fingerprints of all identities.\n");
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000416 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000417 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100418 fprintf(stderr, " -k Load only keys and not certificates.\n");
419 fprintf(stderr, " -c Require confirmation to sign using identities\n");
420 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000421 fprintf(stderr, " -d Delete identity.\n");
422 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000423 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000424 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100425 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
426 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000427}
428
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429int
Damien Miller01ab4a21999-10-28 15:23:30 +1000430main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000432 extern char *optarg;
433 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100434 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100435 char *pkcs11provider = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100436 int i, ch, deleting = 0, ret = 0, key_only = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000437 int xflag = 0, lflag = 0, Dflag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438
Darren Tuckerce321d82005-10-03 18:11:24 +1000439 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
440 sanitise_stdfd();
441
Damien Miller59d3d5b2003-08-22 09:34:41 +1000442 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000443 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000444
Damien Miller72ef7c12015-01-15 02:21:31 +1100445#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000446 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +1100447#endif
Damien Millerad833b32000-08-23 10:46:23 +1000448
millert@openbsd.orgdb995f22014-11-26 18:34:51 +0000449 setvbuf(stdout, NULL, _IOLBF, 0);
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000450
Damien Miller95def091999-11-25 00:26:21 +1100451 /* At first, get a connection to the authentication agent. */
452 ac = ssh_get_authentication_connection();
453 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100454 fprintf(stderr,
455 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100456 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000458 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000459 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000460 case 'E':
461 fingerprint_hash = ssh_digest_alg_by_name(optarg);
462 if (fingerprint_hash == -1)
463 fatal("Invalid hash algorithm \"%s\"", optarg);
464 break;
Damien Miller8f4279e2011-10-18 16:06:33 +1100465 case 'k':
466 key_only = 1;
467 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000468 case 'l':
469 case 'L':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000470 if (lflag != 0)
471 fatal("-%c flag already specified", lflag);
472 lflag = ch;
473 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000474 case 'x':
475 case 'X':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000476 if (xflag != 0)
477 fatal("-%c flag already specified", xflag);
478 xflag = ch;
479 break;
Damien Miller6c711792003-01-24 11:36:23 +1100480 case 'c':
481 confirm = 1;
482 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000483 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100484 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000485 break;
486 case 'D':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000487 Dflag = 1;
488 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000489 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100490 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000491 break;
492 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100493 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100494 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000495 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000496 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000497 if ((lifetime = convtime(optarg)) == -1) {
498 fprintf(stderr, "Invalid lifetime\n");
499 ret = 1;
500 goto done;
501 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000502 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000503 default:
504 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000505 ret = 1;
506 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000507 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000509
510 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
511 fatal("Invalid combination of actions");
512 else if (xflag) {
513 if (lock_agent(ac, xflag == 'x' ? 1 : 0) == -1)
514 ret = 1;
515 goto done;
516 } else if (lflag) {
517 if (list_identities(ac, lflag == 'l' ? 1 : 0) == -1)
518 ret = 1;
519 goto done;
520 } else if (Dflag) {
521 if (delete_all(ac) == -1)
522 ret = 1;
523 goto done;
524 }
525
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000526 argc -= optind;
527 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100528 if (pkcs11provider != NULL) {
529 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000530 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000531 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000532 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000533 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100534 char buf[MAXPATHLEN];
535 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000536 struct stat st;
537 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100538
539 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100540 fprintf(stderr, "No user found with uid %u\n",
541 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000542 ret = 1;
543 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100544 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100545
Darren Tucker47eede72005-03-14 23:08:12 +1100546 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000547 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100548 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000549 if (stat(buf, &st) < 0)
550 continue;
Damien Miller8f4279e2011-10-18 16:06:33 +1100551 if (do_file(ac, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000552 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000553 else
554 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000555 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000556 if (count == 0)
557 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000558 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100559 for (i = 0; i < argc; i++) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100560 if (do_file(ac, deleting, key_only, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100561 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000562 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563 }
Ben Lindstromee617942001-04-10 02:45:32 +0000564 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000565
566done:
Damien Miller95def091999-11-25 00:26:21 +1100567 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000568 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569}