blob: 89658448210ebee59929f4e478670b06446c3b5c [file] [log] [blame]
Damien Miller059321d2013-12-18 17:49:48 +11001/* $OpenBSD: ssh-add.c,v 1.107 2013/12/15 18:17:26 pascal 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 Millerd4a8b7e1999-10-27 13:42:43 +100065
Ben Lindstromddfb1e32001-08-06 22:06:35 +000066/* argv0 */
67extern char *__progname;
68
Damien Miller6e1057c2002-01-22 23:05:59 +110069/* Default files to add */
70static char *default_files[] = {
71 _PATH_SSH_CLIENT_ID_RSA,
72 _PATH_SSH_CLIENT_ID_DSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110073#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100074 _PATH_SSH_CLIENT_ID_ECDSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110075#endif
Damien Miller059321d2013-12-18 17:49:48 +110076 _PATH_SSH_CLIENT_ID_ED25519,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000077 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110078 NULL
79};
80
Ben Lindstrom61d328a2002-06-06 21:54:57 +000081/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000082static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110083
Damien Miller6c711792003-01-24 11:36:23 +110084/* User has to confirm key use */
85static int confirm = 0;
86
Ben Lindstromee617942001-04-10 02:45:32 +000087/* we keep a cache of one passphrases */
88static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000089static void
Ben Lindstromee617942001-04-10 02:45:32 +000090clear_pass(void)
91{
92 if (pass) {
93 memset(pass, 0, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +100094 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +000095 pass = NULL;
96 }
97}
98
Ben Lindstrom569f88d2001-10-03 17:43:01 +000099static int
Damien Miller33a81362012-12-03 09:50:24 +1100100delete_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101{
Damien Miller33a81362012-12-03 09:50:24 +1100102 Key *public = NULL, *cert = NULL;
103 char *certpath = NULL, *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000104 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Ben Lindstromd0fca422001-03-26 13:44:06 +0000106 public = key_load_public(filename, &comment);
107 if (public == NULL) {
108 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000109 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100110 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000111 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100112 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000113 ret = 0;
114 } else
Damien Miller95def091999-11-25 00:26:21 +1100115 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000116
Damien Miller33a81362012-12-03 09:50:24 +1100117 if (key_only)
118 goto out;
119
120 /* Now try to delete the corresponding certificate too */
121 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100122 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100123 xasprintf(&certpath, "%s-cert.pub", filename);
124 if ((cert = key_load_public(certpath, &comment)) == NULL)
125 goto out;
126 if (!key_equal_public(cert, public))
127 fatal("Certificate %s does not match private key %s",
128 certpath, filename);
129
130 if (ssh_remove_identity(ac, cert)) {
131 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
132 comment);
133 ret = 0;
134 } else
135 fprintf(stderr, "Could not remove identity: %s\n", certpath);
136
137 out:
138 if (cert != NULL)
139 key_free(cert);
140 if (public != NULL)
141 key_free(public);
142 free(certpath);
143 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100144
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000145 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146}
147
Damien Millerad833b32000-08-23 10:46:23 +1000148/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000149static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000150delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000152 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000153
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000154 if (ssh_remove_all_identities(ac, 1))
155 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000156 /* ignore error-code for ssh2 */
157 ssh_remove_all_identities(ac, 2);
158
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000159 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100160 fprintf(stderr, "All identities removed.\n");
161 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000162 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000163
164 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000167static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100168add_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169{
Damien Miller0a80ca12010-02-27 07:55:05 +1100170 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000171 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100172 char msg[1024], *certpath = NULL;
Damien Miller8275fad2006-03-15 12:06:23 +1100173 int fd, perms_ok, ret = -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000174 Buffer keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller2ce12ef2011-05-05 14:17:18 +1000176 if (strcmp(filename, "-") == 0) {
177 fd = STDIN_FILENO;
178 filename = "(stdin)";
179 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000180 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000181 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000182 }
Damien Miller8275fad2006-03-15 12:06:23 +1100183
184 /*
185 * Since we'll try to load a keyfile multiple times, permission errors
186 * will occur multiple times, so check perms first and bail if wrong.
187 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000188 if (fd != STDIN_FILENO) {
189 perms_ok = key_perm_ok(fd, filename);
190 if (!perms_ok) {
191 close(fd);
192 return -1;
193 }
194 }
195 buffer_init(&keyblob);
196 if (!key_load_file(fd, filename, &keyblob)) {
197 buffer_free(&keyblob);
198 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100199 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000200 }
201 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100202
Damien Miller95def091999-11-25 00:26:21 +1100203 /* At first, try empty passphrase */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000204 private = key_parse_private(&keyblob, filename, "", &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000205 if (comment == NULL)
206 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000207 /* try last */
208 if (private == NULL && pass != NULL)
Damien Miller2ce12ef2011-05-05 14:17:18 +1000209 private = key_parse_private(&keyblob, filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000210 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000211 /* clear passphrase since it did not work */
212 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000213 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000214 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100215 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000216 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100217 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000218 clear_pass();
Darren Tuckera627d422013-06-02 07:31:17 +1000219 free(comment);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000220 buffer_free(&keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000221 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100222 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000223 private = key_parse_private(&keyblob, filename, pass,
224 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000225 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100226 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000227 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000228 snprintf(msg, sizeof msg,
229 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100230 }
231 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000232 buffer_free(&keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000233
Damien Millera8e06ce2003-11-21 23:48:55 +1100234 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
235 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000236 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000237 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000238 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000239 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000240 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100241 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100242 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000243 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000244 } else {
Damien Miller95def091999-11-25 00:26:21 +1100245 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000246 }
247
Damien Miller8f4279e2011-10-18 16:06:33 +1100248 /* Skip trying to load the cert if requested */
249 if (key_only)
250 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100251
252 /* Now try to add the certificate flavour too */
253 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000254 if ((cert = key_load_public(certpath, NULL)) == NULL)
255 goto out;
256
257 if (!key_equal_public(cert, private)) {
258 error("Certificate %s does not match private key %s",
259 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100260 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000261 goto out;
262 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100263
Damien Millerc6afb5f2010-05-21 14:56:47 +1000264 /* Graft with private bits */
265 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
266 error("%s: key_to_certified failed", __func__);
267 key_free(cert);
268 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100269 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000270 key_cert_copy(cert, private);
271 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100272
Damien Millerc6afb5f2010-05-21 14:56:47 +1000273 if (!ssh_add_identity_constrained(ac, private, comment,
274 lifetime, confirm)) {
275 error("Certificate %s (%s) add failed", certpath,
276 private->cert->key_id);
277 }
278 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
279 private->cert->key_id);
280 if (lifetime != 0)
281 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
282 if (confirm != 0)
283 fprintf(stderr, "The user must confirm each use of the key\n");
284 out:
Damien Miller8f4279e2011-10-18 16:06:33 +1100285 if (certpath != NULL)
Darren Tuckera627d422013-06-02 07:31:17 +1000286 free(certpath);
287 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000288 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100289
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000290 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291}
292
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000293static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000294update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000295{
Ben Lindstromba72d302002-03-22 03:51:06 +0000296 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100297 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000298
Damien Miller7ea845e2010-02-12 09:21:02 +1100299 pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN);
Ben Lindstromba72d302002-03-22 03:51:06 +0000300 if (pin == NULL)
301 return -1;
302
Damien Millerd94f20d2003-06-11 22:06:33 +1000303 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000304 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100305 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100306 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000307 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000308 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100309 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100310 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000311 }
Darren Tuckera627d422013-06-02 07:31:17 +1000312 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100313 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000314}
315
Damien Miller43cba342002-02-05 12:12:49 +1100316static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000317list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318{
Damien Millerad833b32000-08-23 10:46:23 +1000319 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000320 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000321 int had_identities = 0;
322 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Millerad833b32000-08-23 10:46:23 +1000324 for (version = 1; version <= 2; version++) {
325 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100326 key != NULL;
327 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000328 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000329 if (do_fp) {
330 fp = key_fingerprint(key, SSH_FP_MD5,
331 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100332 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000333 key_size(key), fp, comment, key_type(key));
Darren Tuckera627d422013-06-02 07:31:17 +1000334 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100335 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000336 if (!key_write(key, stdout))
337 fprintf(stderr, "key_write failed");
338 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100339 }
Damien Millerad833b32000-08-23 10:46:23 +1000340 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000341 free(comment);
Damien Miller95def091999-11-25 00:26:21 +1100342 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100343 }
Damien Miller43cba342002-02-05 12:12:49 +1100344 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100345 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100346 return -1;
347 }
348 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
Damien Miller6e1057c2002-01-22 23:05:59 +1100351static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000352lock_agent(AuthenticationConnection *ac, int lock)
353{
354 char prompt[100], *p1, *p2;
355 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000356
Ben Lindstrom2f717042002-06-06 21:52:03 +0000357 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
358 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
359 if (lock) {
360 strlcpy(prompt, "Again: ", sizeof prompt);
361 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
362 if (strcmp(p1, p2) != 0) {
363 fprintf(stderr, "Passwords do not match.\n");
364 passok = 0;
365 }
366 memset(p2, 0, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000367 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000368 }
369 if (passok && ssh_lock_agent(ac, lock, p1)) {
370 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
371 ret = 0;
372 } else
373 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
374 memset(p1, 0, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000375 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000376 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000377}
378
379static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100380do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100381{
382 if (deleting) {
Damien Miller33a81362012-12-03 09:50:24 +1100383 if (delete_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100384 return -1;
385 } else {
Damien Miller8f4279e2011-10-18 16:06:33 +1100386 if (add_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100387 return -1;
388 }
389 return 0;
390}
391
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000392static void
393usage(void)
394{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000395 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000396 fprintf(stderr, "Options:\n");
397 fprintf(stderr, " -l List fingerprints of all identities.\n");
398 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100399 fprintf(stderr, " -k Load only keys and not certificates.\n");
400 fprintf(stderr, " -c Require confirmation to sign using identities\n");
401 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000402 fprintf(stderr, " -d Delete identity.\n");
403 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000404 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000405 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100406 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
407 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000408}
409
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410int
Damien Miller01ab4a21999-10-28 15:23:30 +1000411main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000413 extern char *optarg;
414 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100415 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100416 char *pkcs11provider = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100417 int i, ch, deleting = 0, ret = 0, key_only = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Darren Tuckerce321d82005-10-03 18:11:24 +1000419 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
420 sanitise_stdfd();
421
Damien Miller59d3d5b2003-08-22 09:34:41 +1000422 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000423 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000424
Damien Miller4314c2b2010-09-10 11:12:09 +1000425 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000426
Damien Miller95def091999-11-25 00:26:21 +1100427 /* At first, get a connection to the authentication agent. */
428 ac = ssh_get_authentication_connection();
429 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100430 fprintf(stderr,
431 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100432 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433 }
Damien Miller8f4279e2011-10-18 16:06:33 +1100434 while ((ch = getopt(argc, argv, "klLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000435 switch (ch) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100436 case 'k':
437 key_only = 1;
438 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000439 case 'l':
440 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100441 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
442 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000443 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000444 case 'x':
445 case 'X':
446 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
447 ret = 1;
448 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100449 case 'c':
450 confirm = 1;
451 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000452 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100453 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000454 break;
455 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000456 if (delete_all(ac) == -1)
457 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000458 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000459 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100460 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000461 break;
462 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100463 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100464 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000465 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000466 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000467 if ((lifetime = convtime(optarg)) == -1) {
468 fprintf(stderr, "Invalid lifetime\n");
469 ret = 1;
470 goto done;
471 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000472 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000473 default:
474 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000475 ret = 1;
476 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000477 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000479 argc -= optind;
480 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100481 if (pkcs11provider != NULL) {
482 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000483 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000484 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000485 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000486 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100487 char buf[MAXPATHLEN];
488 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000489 struct stat st;
490 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100491
492 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100493 fprintf(stderr, "No user found with uid %u\n",
494 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000495 ret = 1;
496 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100497 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100498
Darren Tucker47eede72005-03-14 23:08:12 +1100499 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000500 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100501 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000502 if (stat(buf, &st) < 0)
503 continue;
Damien Miller8f4279e2011-10-18 16:06:33 +1100504 if (do_file(ac, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000505 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000506 else
507 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000508 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000509 if (count == 0)
510 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000511 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100512 for (i = 0; i < argc; i++) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100513 if (do_file(ac, deleting, key_only, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100514 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000515 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516 }
Ben Lindstromee617942001-04-10 02:45:32 +0000517 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000518
519done:
Damien Miller95def091999-11-25 00:26:21 +1100520 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000521 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522}