blob: 008084704415a05dd10baa77329148593edac303 [file] [log] [blame]
Darren Tucker8a965222012-12-07 13:07:02 +11001/* $OpenBSD: ssh-add.c,v 1.105 2012/12/05 15:42:52 markus 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 Miller33a81362012-12-03 09:50:24 +110099delete_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100{
Damien Miller33a81362012-12-03 09:50:24 +1100101 Key *public = NULL, *cert = NULL;
102 char *certpath = NULL, *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 Miller33a81362012-12-03 09:50:24 +1100116 if (key_only)
117 goto out;
118
119 /* Now try to delete the corresponding certificate too */
120 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100121 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100122 xasprintf(&certpath, "%s-cert.pub", filename);
123 if ((cert = key_load_public(certpath, &comment)) == NULL)
124 goto out;
125 if (!key_equal_public(cert, public))
126 fatal("Certificate %s does not match private key %s",
127 certpath, filename);
128
129 if (ssh_remove_identity(ac, cert)) {
130 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
131 comment);
132 ret = 0;
133 } else
134 fprintf(stderr, "Could not remove identity: %s\n", certpath);
135
136 out:
137 if (cert != NULL)
138 key_free(cert);
139 if (public != NULL)
140 key_free(public);
141 free(certpath);
142 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100143
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000144 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Millerad833b32000-08-23 10:46:23 +1000147/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000148static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000149delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000151 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000152
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000153 if (ssh_remove_all_identities(ac, 1))
154 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000155 /* ignore error-code for ssh2 */
156 ssh_remove_all_identities(ac, 2);
157
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000158 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100159 fprintf(stderr, "All identities removed.\n");
160 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000161 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000162
163 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164}
165
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000166static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100167add_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168{
Damien Miller0a80ca12010-02-27 07:55:05 +1100169 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000170 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100171 char msg[1024], *certpath = NULL;
Damien Miller8275fad2006-03-15 12:06:23 +1100172 int fd, perms_ok, ret = -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000173 Buffer keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174
Damien Miller2ce12ef2011-05-05 14:17:18 +1000175 if (strcmp(filename, "-") == 0) {
176 fd = STDIN_FILENO;
177 filename = "(stdin)";
178 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000179 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000180 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000181 }
Damien Miller8275fad2006-03-15 12:06:23 +1100182
183 /*
184 * Since we'll try to load a keyfile multiple times, permission errors
185 * will occur multiple times, so check perms first and bail if wrong.
186 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000187 if (fd != STDIN_FILENO) {
188 perms_ok = key_perm_ok(fd, filename);
189 if (!perms_ok) {
190 close(fd);
191 return -1;
192 }
193 }
194 buffer_init(&keyblob);
195 if (!key_load_file(fd, filename, &keyblob)) {
196 buffer_free(&keyblob);
197 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100198 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000199 }
200 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100201
Damien Miller95def091999-11-25 00:26:21 +1100202 /* At first, try empty passphrase */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000203 private = key_parse_private(&keyblob, filename, "", &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000204 if (comment == NULL)
205 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000206 /* try last */
207 if (private == NULL && pass != NULL)
Damien Miller2ce12ef2011-05-05 14:17:18 +1000208 private = key_parse_private(&keyblob, filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000209 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000210 /* clear passphrase since it did not work */
211 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000212 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000213 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100214 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000215 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100216 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000217 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000218 xfree(comment);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000219 buffer_free(&keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000220 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100221 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000222 private = key_parse_private(&keyblob, filename, pass,
223 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000224 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100225 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000226 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000227 snprintf(msg, sizeof msg,
228 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100229 }
230 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000231 buffer_free(&keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000232
Damien Millera8e06ce2003-11-21 23:48:55 +1100233 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
234 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000235 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000236 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000237 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000238 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000239 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100240 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100241 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000242 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000243 } else {
Damien Miller95def091999-11-25 00:26:21 +1100244 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000245 }
246
Damien Miller8f4279e2011-10-18 16:06:33 +1100247 /* Skip trying to load the cert if requested */
248 if (key_only)
249 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100250
251 /* Now try to add the certificate flavour too */
252 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000253 if ((cert = key_load_public(certpath, NULL)) == NULL)
254 goto out;
255
256 if (!key_equal_public(cert, private)) {
257 error("Certificate %s does not match private key %s",
258 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100259 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000260 goto out;
261 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100262
Damien Millerc6afb5f2010-05-21 14:56:47 +1000263 /* Graft with private bits */
264 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
265 error("%s: key_to_certified failed", __func__);
266 key_free(cert);
267 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100268 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000269 key_cert_copy(cert, private);
270 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100271
Damien Millerc6afb5f2010-05-21 14:56:47 +1000272 if (!ssh_add_identity_constrained(ac, private, comment,
273 lifetime, confirm)) {
274 error("Certificate %s (%s) add failed", certpath,
275 private->cert->key_id);
276 }
277 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
278 private->cert->key_id);
279 if (lifetime != 0)
280 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
281 if (confirm != 0)
282 fprintf(stderr, "The user must confirm each use of the key\n");
283 out:
Damien Miller8f4279e2011-10-18 16:06:33 +1100284 if (certpath != NULL)
285 xfree(certpath);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000286 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000287 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100288
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000289 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290}
291
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000292static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000293update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000294{
Ben Lindstromba72d302002-03-22 03:51:06 +0000295 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100296 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000297
Damien Miller7ea845e2010-02-12 09:21:02 +1100298 pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN);
Ben Lindstromba72d302002-03-22 03:51:06 +0000299 if (pin == NULL)
300 return -1;
301
Damien Millerd94f20d2003-06-11 22:06:33 +1000302 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000303 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100304 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100305 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000306 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000307 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100308 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100309 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000310 }
Damien Miller00111382003-03-10 11:21:17 +1100311 xfree(pin);
312 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000313}
314
Damien Miller43cba342002-02-05 12:12:49 +1100315static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000316list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317{
Damien Millerad833b32000-08-23 10:46:23 +1000318 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000319 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000320 int had_identities = 0;
321 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Millerad833b32000-08-23 10:46:23 +1000323 for (version = 1; version <= 2; version++) {
324 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100325 key != NULL;
326 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000327 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000328 if (do_fp) {
329 fp = key_fingerprint(key, SSH_FP_MD5,
330 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100331 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000332 key_size(key), fp, comment, key_type(key));
333 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100334 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000335 if (!key_write(key, stdout))
336 fprintf(stderr, "key_write failed");
337 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100338 }
Damien Millerad833b32000-08-23 10:46:23 +1000339 key_free(key);
340 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100341 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100342 }
Damien Miller43cba342002-02-05 12:12:49 +1100343 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100344 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100345 return -1;
346 }
347 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
Damien Miller6e1057c2002-01-22 23:05:59 +1100350static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000351lock_agent(AuthenticationConnection *ac, int lock)
352{
353 char prompt[100], *p1, *p2;
354 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000355
Ben Lindstrom2f717042002-06-06 21:52:03 +0000356 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
357 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
358 if (lock) {
359 strlcpy(prompt, "Again: ", sizeof prompt);
360 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
361 if (strcmp(p1, p2) != 0) {
362 fprintf(stderr, "Passwords do not match.\n");
363 passok = 0;
364 }
365 memset(p2, 0, strlen(p2));
366 xfree(p2);
367 }
368 if (passok && ssh_lock_agent(ac, lock, p1)) {
369 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
370 ret = 0;
371 } else
372 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
373 memset(p1, 0, strlen(p1));
374 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000375 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000376}
377
378static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100379do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100380{
381 if (deleting) {
Damien Miller33a81362012-12-03 09:50:24 +1100382 if (delete_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100383 return -1;
384 } else {
Damien Miller8f4279e2011-10-18 16:06:33 +1100385 if (add_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100386 return -1;
387 }
388 return 0;
389}
390
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000391static void
392usage(void)
393{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000394 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000395 fprintf(stderr, "Options:\n");
396 fprintf(stderr, " -l List fingerprints of all identities.\n");
397 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100398 fprintf(stderr, " -k Load only keys and not certificates.\n");
399 fprintf(stderr, " -c Require confirmation to sign using identities\n");
400 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000401 fprintf(stderr, " -d Delete identity.\n");
402 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000403 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000404 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100405 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
406 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000407}
408
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409int
Damien Miller01ab4a21999-10-28 15:23:30 +1000410main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000412 extern char *optarg;
413 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100414 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100415 char *pkcs11provider = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100416 int i, ch, deleting = 0, ret = 0, key_only = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Darren Tuckerce321d82005-10-03 18:11:24 +1000418 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
419 sanitise_stdfd();
420
Damien Miller59d3d5b2003-08-22 09:34:41 +1000421 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000422 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000423
Damien Miller4314c2b2010-09-10 11:12:09 +1000424 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000425
Damien Miller95def091999-11-25 00:26:21 +1100426 /* At first, get a connection to the authentication agent. */
427 ac = ssh_get_authentication_connection();
428 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100429 fprintf(stderr,
430 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100431 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432 }
Damien Miller8f4279e2011-10-18 16:06:33 +1100433 while ((ch = getopt(argc, argv, "klLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000434 switch (ch) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100435 case 'k':
436 key_only = 1;
437 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000438 case 'l':
439 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100440 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
441 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000442 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000443 case 'x':
444 case 'X':
445 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
446 ret = 1;
447 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100448 case 'c':
449 confirm = 1;
450 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000451 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100452 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000453 break;
454 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000455 if (delete_all(ac) == -1)
456 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000457 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000458 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100459 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000460 break;
461 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100462 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100463 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000464 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000465 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000466 if ((lifetime = convtime(optarg)) == -1) {
467 fprintf(stderr, "Invalid lifetime\n");
468 ret = 1;
469 goto done;
470 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000471 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000472 default:
473 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000474 ret = 1;
475 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000476 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000478 argc -= optind;
479 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100480 if (pkcs11provider != NULL) {
481 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000482 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000483 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000484 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000485 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100486 char buf[MAXPATHLEN];
487 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000488 struct stat st;
489 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100490
491 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100492 fprintf(stderr, "No user found with uid %u\n",
493 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000494 ret = 1;
495 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100496 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100497
Darren Tucker47eede72005-03-14 23:08:12 +1100498 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000499 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100500 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000501 if (stat(buf, &st) < 0)
502 continue;
Damien Miller8f4279e2011-10-18 16:06:33 +1100503 if (do_file(ac, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000504 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000505 else
506 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000507 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000508 if (count == 0)
509 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000510 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100511 for (i = 0; i < argc; i++) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100512 if (do_file(ac, deleting, key_only, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100513 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515 }
Ben Lindstromee617942001-04-10 02:45:32 +0000516 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000517
518done:
Damien Miller95def091999-11-25 00:26:21 +1100519 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000520 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521}