blob: 78a3359ade41ef5bb3026d0086dd03edd7de3cc0 [file] [log] [blame]
Damien Miller1d0df322014-07-11 09:19:04 +10001/* $OpenBSD: ssh-add.c,v 1.113 2014/07/09 14:15:56 benno 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"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Ben Lindstromddfb1e32001-08-06 22:06:35 +000067/* argv0 */
68extern char *__progname;
69
Damien Miller6e1057c2002-01-22 23:05:59 +110070/* Default files to add */
71static char *default_files[] = {
72 _PATH_SSH_CLIENT_ID_RSA,
73 _PATH_SSH_CLIENT_ID_DSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110074#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100075 _PATH_SSH_CLIENT_ID_ECDSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110076#endif
Damien Miller059321d2013-12-18 17:49:48 +110077 _PATH_SSH_CLIENT_ID_ED25519,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000078 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110079 NULL
80};
81
Ben Lindstrom61d328a2002-06-06 21:54:57 +000082/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000083static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110084
Damien Miller6c711792003-01-24 11:36:23 +110085/* User has to confirm key use */
86static int confirm = 0;
87
Ben Lindstromee617942001-04-10 02:45:32 +000088/* we keep a cache of one passphrases */
89static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000090static void
Ben Lindstromee617942001-04-10 02:45:32 +000091clear_pass(void)
92{
93 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +110094 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +100095 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +000096 pass = NULL;
97 }
98}
99
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000100static int
Damien Miller33a81362012-12-03 09:50:24 +1100101delete_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102{
Damien Miller33a81362012-12-03 09:50:24 +1100103 Key *public = NULL, *cert = NULL;
104 char *certpath = NULL, *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000105 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Ben Lindstromd0fca422001-03-26 13:44:06 +0000107 public = key_load_public(filename, &comment);
108 if (public == NULL) {
109 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000110 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100111 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000112 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100113 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000114 ret = 0;
115 } else
Damien Miller95def091999-11-25 00:26:21 +1100116 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000117
Damien Miller33a81362012-12-03 09:50:24 +1100118 if (key_only)
119 goto out;
120
121 /* Now try to delete the corresponding certificate too */
122 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100123 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100124 xasprintf(&certpath, "%s-cert.pub", filename);
125 if ((cert = key_load_public(certpath, &comment)) == NULL)
126 goto out;
127 if (!key_equal_public(cert, public))
128 fatal("Certificate %s does not match private key %s",
129 certpath, filename);
130
131 if (ssh_remove_identity(ac, cert)) {
132 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
133 comment);
134 ret = 0;
135 } else
136 fprintf(stderr, "Could not remove identity: %s\n", certpath);
137
138 out:
139 if (cert != NULL)
140 key_free(cert);
141 if (public != NULL)
142 key_free(public);
143 free(certpath);
144 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100145
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000146 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147}
148
Damien Millerad833b32000-08-23 10:46:23 +1000149/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000150static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000151delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000153 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000154
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000155 if (ssh_remove_all_identities(ac, 1))
156 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000157 /* ignore error-code for ssh2 */
158 ssh_remove_all_identities(ac, 2);
159
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000160 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100161 fprintf(stderr, "All identities removed.\n");
162 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000163 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000164
165 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000168static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100169add_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Miller0a80ca12010-02-27 07:55:05 +1100171 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000172 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100173 char msg[1024], *certpath = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000174 int r, fd, perms_ok, ret = -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000175 Buffer keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176
Damien Miller2ce12ef2011-05-05 14:17:18 +1000177 if (strcmp(filename, "-") == 0) {
178 fd = STDIN_FILENO;
179 filename = "(stdin)";
180 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000181 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000182 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000183 }
Damien Miller8275fad2006-03-15 12:06:23 +1100184
185 /*
186 * Since we'll try to load a keyfile multiple times, permission errors
187 * will occur multiple times, so check perms first and bail if wrong.
188 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000189 if (fd != STDIN_FILENO) {
190 perms_ok = key_perm_ok(fd, filename);
191 if (!perms_ok) {
192 close(fd);
193 return -1;
194 }
195 }
196 buffer_init(&keyblob);
197 if (!key_load_file(fd, filename, &keyblob)) {
198 buffer_free(&keyblob);
199 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100200 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000201 }
202 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100203
Damien Miller95def091999-11-25 00:26:21 +1100204 /* At first, try empty passphrase */
Damien Millerec3d0e22014-07-02 15:30:00 +1000205 if ((r = sshkey_parse_private_fileblob(&keyblob, "", filename,
Damien Miller86687062014-07-02 15:28:02 +1000206 &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE)
Damien Millerec3d0e22014-07-02 15:30:00 +1000207 fatal("Cannot parse %s: %s", filename, ssh_err(r));
Ben Lindstromee617942001-04-10 02:45:32 +0000208 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000209 if (private == NULL && pass != NULL) {
Damien Millerec3d0e22014-07-02 15:30:00 +1000210 if ((r = sshkey_parse_private_fileblob(&keyblob, pass, filename,
Damien Miller86687062014-07-02 15:28:02 +1000211 &private, &comment)) != 0 &&
212 r != SSH_ERR_KEY_WRONG_PASSPHRASE)
213 fatal("Cannot parse %s: %s", filename, ssh_err(r));
214 }
Damien Miller1d0df322014-07-11 09:19:04 +1000215 if (comment == NULL)
216 comment = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000217 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000218 /* clear passphrase since it did not work */
219 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000220 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000221 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100222 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000223 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100224 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000225 clear_pass();
Darren Tuckera627d422013-06-02 07:31:17 +1000226 free(comment);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000227 buffer_free(&keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000228 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100229 }
Damien Miller86687062014-07-02 15:28:02 +1000230 if ((r = sshkey_parse_private_fileblob(&keyblob,
Damien Millerec3d0e22014-07-02 15:30:00 +1000231 pass, filename, &private, NULL)) != 0 &&
Damien Miller86687062014-07-02 15:28:02 +1000232 r != SSH_ERR_KEY_WRONG_PASSPHRASE)
233 fatal("Cannot parse %s: %s",
234 filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000235 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100236 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000237 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000238 snprintf(msg, sizeof msg,
239 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100240 }
241 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000242 buffer_free(&keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000243
Damien Millera8e06ce2003-11-21 23:48:55 +1100244 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
245 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000246 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000247 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000248 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000249 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000250 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100251 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100252 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000253 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000254 } else {
Damien Miller95def091999-11-25 00:26:21 +1100255 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000256 }
257
Damien Miller8f4279e2011-10-18 16:06:33 +1100258 /* Skip trying to load the cert if requested */
259 if (key_only)
260 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100261
262 /* Now try to add the certificate flavour too */
263 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000264 if ((cert = key_load_public(certpath, NULL)) == NULL)
265 goto out;
266
267 if (!key_equal_public(cert, private)) {
268 error("Certificate %s does not match private key %s",
269 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100270 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000271 goto out;
272 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100273
Damien Millerc6afb5f2010-05-21 14:56:47 +1000274 /* Graft with private bits */
275 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
276 error("%s: key_to_certified failed", __func__);
277 key_free(cert);
278 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100279 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000280 key_cert_copy(cert, private);
281 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100282
Damien Millerc6afb5f2010-05-21 14:56:47 +1000283 if (!ssh_add_identity_constrained(ac, private, comment,
284 lifetime, confirm)) {
285 error("Certificate %s (%s) add failed", certpath,
286 private->cert->key_id);
287 }
288 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
289 private->cert->key_id);
290 if (lifetime != 0)
291 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
292 if (confirm != 0)
293 fprintf(stderr, "The user must confirm each use of the key\n");
294 out:
Damien Miller8f4279e2011-10-18 16:06:33 +1100295 if (certpath != NULL)
Darren Tuckera627d422013-06-02 07:31:17 +1000296 free(certpath);
297 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000298 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100299
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000300 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301}
302
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000303static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000304update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000305{
Damien Milleref275ea2013-12-29 17:44:07 +1100306 char *pin = NULL;
Damien Miller00111382003-03-10 11:21:17 +1100307 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000308
Damien Milleref275ea2013-12-29 17:44:07 +1100309 if (add) {
310 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
311 RP_ALLOW_STDIN)) == NULL)
312 return -1;
313 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000314
Damien Milleref275ea2013-12-29 17:44:07 +1100315 if (ssh_update_card(ac, add, id, pin == NULL ? "" : pin,
316 lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000317 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100318 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100319 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000320 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000321 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100322 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100323 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000324 }
Darren Tuckera627d422013-06-02 07:31:17 +1000325 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100326 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000327}
328
Damien Miller43cba342002-02-05 12:12:49 +1100329static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000330list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331{
Damien Millerad833b32000-08-23 10:46:23 +1000332 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000333 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000334 int had_identities = 0;
335 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Millerad833b32000-08-23 10:46:23 +1000337 for (version = 1; version <= 2; version++) {
338 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100339 key != NULL;
340 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000341 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000342 if (do_fp) {
343 fp = key_fingerprint(key, SSH_FP_MD5,
344 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100345 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000346 key_size(key), fp, comment, key_type(key));
Darren Tuckera627d422013-06-02 07:31:17 +1000347 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100348 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000349 if (!key_write(key, stdout))
350 fprintf(stderr, "key_write failed");
351 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100352 }
Damien Millerad833b32000-08-23 10:46:23 +1000353 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000354 free(comment);
Damien Miller95def091999-11-25 00:26:21 +1100355 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100356 }
Damien Miller43cba342002-02-05 12:12:49 +1100357 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100358 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100359 return -1;
360 }
361 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362}
363
Damien Miller6e1057c2002-01-22 23:05:59 +1100364static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000365lock_agent(AuthenticationConnection *ac, int lock)
366{
367 char prompt[100], *p1, *p2;
368 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000369
Ben Lindstrom2f717042002-06-06 21:52:03 +0000370 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
371 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
372 if (lock) {
373 strlcpy(prompt, "Again: ", sizeof prompt);
374 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
375 if (strcmp(p1, p2) != 0) {
376 fprintf(stderr, "Passwords do not match.\n");
377 passok = 0;
378 }
Damien Millera5103f42014-02-04 11:20:14 +1100379 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000380 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000381 }
382 if (passok && ssh_lock_agent(ac, lock, p1)) {
383 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
384 ret = 0;
385 } else
386 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
Damien Millera5103f42014-02-04 11:20:14 +1100387 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000388 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000389 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000390}
391
392static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100393do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100394{
395 if (deleting) {
Damien Miller33a81362012-12-03 09:50:24 +1100396 if (delete_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100397 return -1;
398 } else {
Damien Miller8f4279e2011-10-18 16:06:33 +1100399 if (add_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100400 return -1;
401 }
402 return 0;
403}
404
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000405static void
406usage(void)
407{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000408 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000409 fprintf(stderr, "Options:\n");
410 fprintf(stderr, " -l List fingerprints of all identities.\n");
411 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100412 fprintf(stderr, " -k Load only keys and not certificates.\n");
413 fprintf(stderr, " -c Require confirmation to sign using identities\n");
414 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000415 fprintf(stderr, " -d Delete identity.\n");
416 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000417 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000418 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100419 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
420 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000421}
422
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423int
Damien Miller01ab4a21999-10-28 15:23:30 +1000424main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000426 extern char *optarg;
427 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100428 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100429 char *pkcs11provider = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100430 int i, ch, deleting = 0, ret = 0, key_only = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Darren Tuckerce321d82005-10-03 18:11:24 +1000432 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
433 sanitise_stdfd();
434
Damien Miller59d3d5b2003-08-22 09:34:41 +1000435 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000436 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000437
Damien Miller4314c2b2010-09-10 11:12:09 +1000438 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000439
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000440 setlinebuf(stdout);
441
Damien Miller95def091999-11-25 00:26:21 +1100442 /* At first, get a connection to the authentication agent. */
443 ac = ssh_get_authentication_connection();
444 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100445 fprintf(stderr,
446 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100447 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448 }
Damien Miller8f4279e2011-10-18 16:06:33 +1100449 while ((ch = getopt(argc, argv, "klLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000450 switch (ch) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100451 case 'k':
452 key_only = 1;
453 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000454 case 'l':
455 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100456 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
457 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000458 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000459 case 'x':
460 case 'X':
461 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
462 ret = 1;
463 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100464 case 'c':
465 confirm = 1;
466 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000467 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100468 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000469 break;
470 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000471 if (delete_all(ac) == -1)
472 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000473 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000474 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100475 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000476 break;
477 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100478 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100479 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000480 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000481 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000482 if ((lifetime = convtime(optarg)) == -1) {
483 fprintf(stderr, "Invalid lifetime\n");
484 ret = 1;
485 goto done;
486 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000487 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000488 default:
489 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000490 ret = 1;
491 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000492 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000494 argc -= optind;
495 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100496 if (pkcs11provider != NULL) {
497 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000498 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000499 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000500 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000501 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100502 char buf[MAXPATHLEN];
503 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000504 struct stat st;
505 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100506
507 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100508 fprintf(stderr, "No user found with uid %u\n",
509 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000510 ret = 1;
511 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100512 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100513
Darren Tucker47eede72005-03-14 23:08:12 +1100514 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000515 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100516 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000517 if (stat(buf, &st) < 0)
518 continue;
Damien Miller8f4279e2011-10-18 16:06:33 +1100519 if (do_file(ac, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000520 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000521 else
522 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000523 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000524 if (count == 0)
525 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000526 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100527 for (i = 0; i < argc; i++) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100528 if (do_file(ac, deleting, key_only, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100529 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000530 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531 }
Ben Lindstromee617942001-04-10 02:45:32 +0000532 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000533
534done:
Damien Miller95def091999-11-25 00:26:21 +1100535 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000536 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537}