blob: 738644d275a7808ea31e5b4fbfc8ebe573a5ee39 [file] [log] [blame]
Darren Tuckere68cf842011-11-04 10:51:51 +11001/* $OpenBSD: ssh-add.c,v 1.103 2011/10/18 23:37:42 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 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
Ben Lindstrom6328ab32002-03-22 02:54:23 +000076 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110077 NULL
78};
79
Ben Lindstrom61d328a2002-06-06 21:54:57 +000080/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000081static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110082
Damien Miller6c711792003-01-24 11:36:23 +110083/* User has to confirm key use */
84static int confirm = 0;
85
Ben Lindstromee617942001-04-10 02:45:32 +000086/* we keep a cache of one passphrases */
87static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000088static void
Ben Lindstromee617942001-04-10 02:45:32 +000089clear_pass(void)
90{
91 if (pass) {
92 memset(pass, 0, strlen(pass));
93 xfree(pass);
94 pass = NULL;
95 }
96}
97
Ben Lindstrom569f88d2001-10-03 17:43:01 +000098static int
Damien Miller01ab4a21999-10-28 15:23:30 +100099delete_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100{
Damien Millereba71ba2000-04-29 23:57:08 +1000101 Key *public;
Ben Lindstromd5730a82001-04-08 18:04:36 +0000102 char *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000103 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Ben Lindstromd0fca422001-03-26 13:44:06 +0000105 public = key_load_public(filename, &comment);
106 if (public == NULL) {
107 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000108 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100109 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000110 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100111 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000112 ret = 0;
113 } else
Damien Miller95def091999-11-25 00:26:21 +1100114 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000115
Damien Millereba71ba2000-04-29 23:57:08 +1000116 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100117 xfree(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100118
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000119 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120}
121
Damien Millerad833b32000-08-23 10:46:23 +1000122/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000123static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000124delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000126 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000127
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000128 if (ssh_remove_all_identities(ac, 1))
129 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000130 /* ignore error-code for ssh2 */
131 ssh_remove_all_identities(ac, 2);
132
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000133 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100134 fprintf(stderr, "All identities removed.\n");
135 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000136 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000137
138 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139}
140
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000141static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100142add_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Miller0a80ca12010-02-27 07:55:05 +1100144 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000145 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100146 char msg[1024], *certpath = NULL;
Damien Miller8275fad2006-03-15 12:06:23 +1100147 int fd, perms_ok, ret = -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000148 Buffer keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller2ce12ef2011-05-05 14:17:18 +1000150 if (strcmp(filename, "-") == 0) {
151 fd = STDIN_FILENO;
152 filename = "(stdin)";
153 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000154 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000155 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000156 }
Damien Miller8275fad2006-03-15 12:06:23 +1100157
158 /*
159 * Since we'll try to load a keyfile multiple times, permission errors
160 * will occur multiple times, so check perms first and bail if wrong.
161 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000162 if (fd != STDIN_FILENO) {
163 perms_ok = key_perm_ok(fd, filename);
164 if (!perms_ok) {
165 close(fd);
166 return -1;
167 }
168 }
169 buffer_init(&keyblob);
170 if (!key_load_file(fd, filename, &keyblob)) {
171 buffer_free(&keyblob);
172 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100173 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000174 }
175 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100176
Damien Miller95def091999-11-25 00:26:21 +1100177 /* At first, try empty passphrase */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000178 private = key_parse_private(&keyblob, filename, "", &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000179 if (comment == NULL)
180 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000181 /* try last */
182 if (private == NULL && pass != NULL)
Damien Miller2ce12ef2011-05-05 14:17:18 +1000183 private = key_parse_private(&keyblob, filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000184 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000185 /* clear passphrase since it did not work */
186 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000187 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000188 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100189 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000190 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100191 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000192 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000193 xfree(comment);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000194 buffer_free(&keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000195 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100196 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000197 private = key_parse_private(&keyblob, filename, pass,
198 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000199 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100200 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000201 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000202 snprintf(msg, sizeof msg,
203 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100204 }
205 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000206 buffer_free(&keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000207
Damien Millera8e06ce2003-11-21 23:48:55 +1100208 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
209 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000210 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000211 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000212 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000213 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000214 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100215 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100216 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000217 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000218 } else {
Damien Miller95def091999-11-25 00:26:21 +1100219 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000220 }
221
Damien Miller8f4279e2011-10-18 16:06:33 +1100222 /* Skip trying to load the cert if requested */
223 if (key_only)
224 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100225
226 /* Now try to add the certificate flavour too */
227 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000228 if ((cert = key_load_public(certpath, NULL)) == NULL)
229 goto out;
230
231 if (!key_equal_public(cert, private)) {
232 error("Certificate %s does not match private key %s",
233 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100234 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000235 goto out;
236 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100237
Damien Millerc6afb5f2010-05-21 14:56:47 +1000238 /* Graft with private bits */
239 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
240 error("%s: key_to_certified failed", __func__);
241 key_free(cert);
242 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100243 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000244 key_cert_copy(cert, private);
245 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100246
Damien Millerc6afb5f2010-05-21 14:56:47 +1000247 if (!ssh_add_identity_constrained(ac, private, comment,
248 lifetime, confirm)) {
249 error("Certificate %s (%s) add failed", certpath,
250 private->cert->key_id);
251 }
252 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
253 private->cert->key_id);
254 if (lifetime != 0)
255 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
256 if (confirm != 0)
257 fprintf(stderr, "The user must confirm each use of the key\n");
258 out:
Damien Miller8f4279e2011-10-18 16:06:33 +1100259 if (certpath != NULL)
260 xfree(certpath);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000261 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000262 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100263
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000264 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265}
266
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000267static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000268update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000269{
Ben Lindstromba72d302002-03-22 03:51:06 +0000270 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100271 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000272
Damien Miller7ea845e2010-02-12 09:21:02 +1100273 pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN);
Ben Lindstromba72d302002-03-22 03:51:06 +0000274 if (pin == NULL)
275 return -1;
276
Damien Millerd94f20d2003-06-11 22:06:33 +1000277 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000278 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100279 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100280 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000281 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000282 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100283 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100284 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000285 }
Damien Miller00111382003-03-10 11:21:17 +1100286 xfree(pin);
287 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000288}
289
Damien Miller43cba342002-02-05 12:12:49 +1100290static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000291list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292{
Damien Millerad833b32000-08-23 10:46:23 +1000293 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000294 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000295 int had_identities = 0;
296 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Millerad833b32000-08-23 10:46:23 +1000298 for (version = 1; version <= 2; version++) {
299 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100300 key != NULL;
301 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000302 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000303 if (do_fp) {
304 fp = key_fingerprint(key, SSH_FP_MD5,
305 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100306 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000307 key_size(key), fp, comment, key_type(key));
308 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100309 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000310 if (!key_write(key, stdout))
311 fprintf(stderr, "key_write failed");
312 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100313 }
Damien Millerad833b32000-08-23 10:46:23 +1000314 key_free(key);
315 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100316 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100317 }
Damien Miller43cba342002-02-05 12:12:49 +1100318 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100319 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100320 return -1;
321 }
322 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
324
Damien Miller6e1057c2002-01-22 23:05:59 +1100325static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000326lock_agent(AuthenticationConnection *ac, int lock)
327{
328 char prompt[100], *p1, *p2;
329 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000330
Ben Lindstrom2f717042002-06-06 21:52:03 +0000331 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
332 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
333 if (lock) {
334 strlcpy(prompt, "Again: ", sizeof prompt);
335 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
336 if (strcmp(p1, p2) != 0) {
337 fprintf(stderr, "Passwords do not match.\n");
338 passok = 0;
339 }
340 memset(p2, 0, strlen(p2));
341 xfree(p2);
342 }
343 if (passok && ssh_lock_agent(ac, lock, p1)) {
344 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
345 ret = 0;
346 } else
347 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
348 memset(p1, 0, strlen(p1));
349 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000350 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000351}
352
353static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100354do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100355{
356 if (deleting) {
357 if (delete_file(ac, file) == -1)
358 return -1;
359 } else {
Damien Miller8f4279e2011-10-18 16:06:33 +1100360 if (add_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100361 return -1;
362 }
363 return 0;
364}
365
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000366static void
367usage(void)
368{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000369 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000370 fprintf(stderr, "Options:\n");
371 fprintf(stderr, " -l List fingerprints of all identities.\n");
372 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100373 fprintf(stderr, " -k Load only keys and not certificates.\n");
374 fprintf(stderr, " -c Require confirmation to sign using identities\n");
375 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000376 fprintf(stderr, " -d Delete identity.\n");
377 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000378 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000379 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100380 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
381 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000382}
383
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384int
Damien Miller01ab4a21999-10-28 15:23:30 +1000385main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000387 extern char *optarg;
388 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100389 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100390 char *pkcs11provider = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100391 int i, ch, deleting = 0, ret = 0, key_only = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392
Darren Tuckerce321d82005-10-03 18:11:24 +1000393 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
394 sanitise_stdfd();
395
Damien Miller59d3d5b2003-08-22 09:34:41 +1000396 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000397 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000398
Damien Miller4314c2b2010-09-10 11:12:09 +1000399 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 /* At first, get a connection to the authentication agent. */
402 ac = ssh_get_authentication_connection();
403 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100404 fprintf(stderr,
405 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100406 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407 }
Damien Miller8f4279e2011-10-18 16:06:33 +1100408 while ((ch = getopt(argc, argv, "klLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000409 switch (ch) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100410 case 'k':
411 key_only = 1;
412 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000413 case 'l':
414 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100415 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
416 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000417 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000418 case 'x':
419 case 'X':
420 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
421 ret = 1;
422 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100423 case 'c':
424 confirm = 1;
425 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000426 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100427 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000428 break;
429 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000430 if (delete_all(ac) == -1)
431 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000432 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000433 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100434 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000435 break;
436 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100437 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100438 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000439 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000440 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000441 if ((lifetime = convtime(optarg)) == -1) {
442 fprintf(stderr, "Invalid lifetime\n");
443 ret = 1;
444 goto done;
445 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000446 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000447 default:
448 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000449 ret = 1;
450 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000451 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000453 argc -= optind;
454 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100455 if (pkcs11provider != NULL) {
456 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000457 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000458 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000459 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000460 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100461 char buf[MAXPATHLEN];
462 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000463 struct stat st;
464 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100465
466 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100467 fprintf(stderr, "No user found with uid %u\n",
468 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000469 ret = 1;
470 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100471 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100472
Darren Tucker47eede72005-03-14 23:08:12 +1100473 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000474 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100475 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000476 if (stat(buf, &st) < 0)
477 continue;
Damien Miller8f4279e2011-10-18 16:06:33 +1100478 if (do_file(ac, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000479 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000480 else
481 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000482 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000483 if (count == 0)
484 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000485 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100486 for (i = 0; i < argc; i++) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100487 if (do_file(ac, deleting, key_only, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100488 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000489 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 }
Ben Lindstromee617942001-04-10 02:45:32 +0000491 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000492
493done:
Damien Miller95def091999-11-25 00:26:21 +1100494 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000495 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496}