blob: 6d5e2a95757f3ae3e7bdc35e1f4b2eddf042da25 [file] [log] [blame]
Damien Miller2ce12ef2011-05-05 14:17:18 +10001/* $OpenBSD: ssh-add.c,v 1.101 2011/05/04 21:15:29 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 Miller01ab4a21999-10-28 15:23:30 +1000142add_file(AuthenticationConnection *ac, const char *filename)
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 Miller0a80ca12010-02-27 07:55:05 +1100146 char msg[1024], *certpath;
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 Miller0a80ca12010-02-27 07:55:05 +1100222
223 /* Now try to add the certificate flavour too */
224 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000225 if ((cert = key_load_public(certpath, NULL)) == NULL)
226 goto out;
227
228 if (!key_equal_public(cert, private)) {
229 error("Certificate %s does not match private key %s",
230 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100231 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000232 goto out;
233 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100234
Damien Millerc6afb5f2010-05-21 14:56:47 +1000235 /* Graft with private bits */
236 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
237 error("%s: key_to_certified failed", __func__);
238 key_free(cert);
239 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100240 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000241 key_cert_copy(cert, private);
242 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100243
Damien Millerc6afb5f2010-05-21 14:56:47 +1000244 if (!ssh_add_identity_constrained(ac, private, comment,
245 lifetime, confirm)) {
246 error("Certificate %s (%s) add failed", certpath,
247 private->cert->key_id);
248 }
249 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
250 private->cert->key_id);
251 if (lifetime != 0)
252 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
253 if (confirm != 0)
254 fprintf(stderr, "The user must confirm each use of the key\n");
255 out:
Damien Miller0a80ca12010-02-27 07:55:05 +1100256 xfree(certpath);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000257 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000258 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100259
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000260 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261}
262
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000263static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000264update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000265{
Ben Lindstromba72d302002-03-22 03:51:06 +0000266 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100267 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000268
Damien Miller7ea845e2010-02-12 09:21:02 +1100269 pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN);
Ben Lindstromba72d302002-03-22 03:51:06 +0000270 if (pin == NULL)
271 return -1;
272
Damien Millerd94f20d2003-06-11 22:06:33 +1000273 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000274 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100275 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100276 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000277 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000278 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100279 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100280 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000281 }
Damien Miller00111382003-03-10 11:21:17 +1100282 xfree(pin);
283 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000284}
285
Damien Miller43cba342002-02-05 12:12:49 +1100286static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000287list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
Damien Millerad833b32000-08-23 10:46:23 +1000289 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000290 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000291 int had_identities = 0;
292 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293
Damien Millerad833b32000-08-23 10:46:23 +1000294 for (version = 1; version <= 2; version++) {
295 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100296 key != NULL;
297 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000298 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000299 if (do_fp) {
300 fp = key_fingerprint(key, SSH_FP_MD5,
301 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100302 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000303 key_size(key), fp, comment, key_type(key));
304 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100305 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000306 if (!key_write(key, stdout))
307 fprintf(stderr, "key_write failed");
308 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100309 }
Damien Millerad833b32000-08-23 10:46:23 +1000310 key_free(key);
311 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100312 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100313 }
Damien Miller43cba342002-02-05 12:12:49 +1100314 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100315 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100316 return -1;
317 }
318 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319}
320
Damien Miller6e1057c2002-01-22 23:05:59 +1100321static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000322lock_agent(AuthenticationConnection *ac, int lock)
323{
324 char prompt[100], *p1, *p2;
325 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000326
Ben Lindstrom2f717042002-06-06 21:52:03 +0000327 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
328 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
329 if (lock) {
330 strlcpy(prompt, "Again: ", sizeof prompt);
331 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
332 if (strcmp(p1, p2) != 0) {
333 fprintf(stderr, "Passwords do not match.\n");
334 passok = 0;
335 }
336 memset(p2, 0, strlen(p2));
337 xfree(p2);
338 }
339 if (passok && ssh_lock_agent(ac, lock, p1)) {
340 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
341 ret = 0;
342 } else
343 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
344 memset(p1, 0, strlen(p1));
345 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000346 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000347}
348
349static int
Damien Miller6e1057c2002-01-22 23:05:59 +1100350do_file(AuthenticationConnection *ac, int deleting, char *file)
351{
352 if (deleting) {
353 if (delete_file(ac, file) == -1)
354 return -1;
355 } else {
356 if (add_file(ac, file) == -1)
357 return -1;
358 }
359 return 0;
360}
361
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000362static void
363usage(void)
364{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000365 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000366 fprintf(stderr, "Options:\n");
367 fprintf(stderr, " -l List fingerprints of all identities.\n");
368 fprintf(stderr, " -L List public key parameters of all identities.\n");
369 fprintf(stderr, " -d Delete identity.\n");
370 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000371 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000372 fprintf(stderr, " -X Unlock agent.\n");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000373 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Damien Miller6c711792003-01-24 11:36:23 +1100374 fprintf(stderr, " -c Require confirmation to sign using identities\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100375 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
376 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000377}
378
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379int
Damien Miller01ab4a21999-10-28 15:23:30 +1000380main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000382 extern char *optarg;
383 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100384 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100385 char *pkcs11provider = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000386 int i, ch, deleting = 0, ret = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387
Darren Tuckerce321d82005-10-03 18:11:24 +1000388 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
389 sanitise_stdfd();
390
Damien Miller59d3d5b2003-08-22 09:34:41 +1000391 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000392 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000393
Damien Miller4314c2b2010-09-10 11:12:09 +1000394 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 /* At first, get a connection to the authentication agent. */
397 ac = ssh_get_authentication_connection();
398 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100399 fprintf(stderr,
400 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100401 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402 }
Damien Miller6c711792003-01-24 11:36:23 +1100403 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000404 switch (ch) {
405 case 'l':
406 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100407 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
408 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000409 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000410 case 'x':
411 case 'X':
412 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
413 ret = 1;
414 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100415 case 'c':
416 confirm = 1;
417 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000418 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100419 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000420 break;
421 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000422 if (delete_all(ac) == -1)
423 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000424 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000425 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100426 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000427 break;
428 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100429 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100430 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000431 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000432 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000433 if ((lifetime = convtime(optarg)) == -1) {
434 fprintf(stderr, "Invalid lifetime\n");
435 ret = 1;
436 goto done;
437 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000438 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000439 default:
440 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000441 ret = 1;
442 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000443 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000445 argc -= optind;
446 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100447 if (pkcs11provider != NULL) {
448 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000449 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000450 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000451 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000452 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100453 char buf[MAXPATHLEN];
454 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000455 struct stat st;
456 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100457
458 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100459 fprintf(stderr, "No user found with uid %u\n",
460 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000461 ret = 1;
462 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100463 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100464
Darren Tucker47eede72005-03-14 23:08:12 +1100465 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000466 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100467 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000468 if (stat(buf, &st) < 0)
469 continue;
Damien Miller6e1057c2002-01-22 23:05:59 +1100470 if (do_file(ac, deleting, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000471 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000472 else
473 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000474 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000475 if (count == 0)
476 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000477 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100478 for (i = 0; i < argc; i++) {
Damien Millera055c252002-01-22 23:07:01 +1100479 if (do_file(ac, deleting, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100480 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000481 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482 }
Ben Lindstromee617942001-04-10 02:45:32 +0000483 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000484
485done:
Damien Miller95def091999-11-25 00:26:21 +1100486 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000487 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488}