blob: 8bf5675fbddb862a40d6e4ae44e7a33437b74b48 [file] [log] [blame]
Damien Miller4314c2b2010-09-10 11:12:09 +10001/* $OpenBSD: ssh-add.c,v 1.100 2010/08/31 12:33:38 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 Millereb8b60e2010-08-31 22:41:14 +100073 _PATH_SSH_CLIENT_ID_ECDSA,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000074 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110075 NULL
76};
77
Ben Lindstrom61d328a2002-06-06 21:54:57 +000078/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000079static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110080
Damien Miller6c711792003-01-24 11:36:23 +110081/* User has to confirm key use */
82static int confirm = 0;
83
Ben Lindstromee617942001-04-10 02:45:32 +000084/* we keep a cache of one passphrases */
85static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000086static void
Ben Lindstromee617942001-04-10 02:45:32 +000087clear_pass(void)
88{
89 if (pass) {
90 memset(pass, 0, strlen(pass));
91 xfree(pass);
92 pass = NULL;
93 }
94}
95
Ben Lindstrom569f88d2001-10-03 17:43:01 +000096static int
Damien Miller01ab4a21999-10-28 15:23:30 +100097delete_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098{
Damien Millereba71ba2000-04-29 23:57:08 +100099 Key *public;
Ben Lindstromd5730a82001-04-08 18:04:36 +0000100 char *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000101 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Ben Lindstromd0fca422001-03-26 13:44:06 +0000103 public = key_load_public(filename, &comment);
104 if (public == NULL) {
105 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000106 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100107 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000108 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100109 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000110 ret = 0;
111 } else
Damien Miller95def091999-11-25 00:26:21 +1100112 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000113
Damien Millereba71ba2000-04-29 23:57:08 +1000114 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100115 xfree(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100116
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000117 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118}
119
Damien Millerad833b32000-08-23 10:46:23 +1000120/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000121static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000122delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000124 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000125
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000126 if (ssh_remove_all_identities(ac, 1))
127 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000128 /* ignore error-code for ssh2 */
129 ssh_remove_all_identities(ac, 2);
130
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000131 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100132 fprintf(stderr, "All identities removed.\n");
133 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000134 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000135
136 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137}
138
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000139static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000140add_file(AuthenticationConnection *ac, const char *filename)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller0a80ca12010-02-27 07:55:05 +1100142 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000143 char *comment = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100144 char msg[1024], *certpath;
Damien Miller8275fad2006-03-15 12:06:23 +1100145 int fd, perms_ok, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller211838d2006-07-10 21:14:00 +1000147 if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000148 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000149 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000150 }
Damien Miller8275fad2006-03-15 12:06:23 +1100151
152 /*
153 * Since we'll try to load a keyfile multiple times, permission errors
154 * will occur multiple times, so check perms first and bail if wrong.
155 */
156 perms_ok = key_perm_ok(fd, filename);
157 close(fd);
158 if (!perms_ok)
159 return -1;
160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* At first, try empty passphrase */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000162 private = key_load_private(filename, "", &comment);
163 if (comment == NULL)
164 comment = xstrdup(filename);
Ben Lindstromee617942001-04-10 02:45:32 +0000165 /* try last */
166 if (private == NULL && pass != NULL)
167 private = key_load_private(filename, pass, NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000168 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000169 /* clear passphrase since it did not work */
170 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000171 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000172 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100173 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000174 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100175 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000176 clear_pass();
Ben Lindstromd0fca422001-03-26 13:44:06 +0000177 xfree(comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000178 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100179 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000180 private = key_load_private(filename, pass, &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000181 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100182 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000183 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000184 snprintf(msg, sizeof msg,
185 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100186 }
187 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000188
Damien Millera8e06ce2003-11-21 23:48:55 +1100189 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
190 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000191 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000192 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000193 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000194 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000195 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100196 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100197 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000198 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000199 } else {
Damien Miller95def091999-11-25 00:26:21 +1100200 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000201 }
202
Damien Miller0a80ca12010-02-27 07:55:05 +1100203
204 /* Now try to add the certificate flavour too */
205 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000206 if ((cert = key_load_public(certpath, NULL)) == NULL)
207 goto out;
208
209 if (!key_equal_public(cert, private)) {
210 error("Certificate %s does not match private key %s",
211 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100212 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000213 goto out;
214 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100215
Damien Millerc6afb5f2010-05-21 14:56:47 +1000216 /* Graft with private bits */
217 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
218 error("%s: key_to_certified failed", __func__);
219 key_free(cert);
220 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100221 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000222 key_cert_copy(cert, private);
223 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100224
Damien Millerc6afb5f2010-05-21 14:56:47 +1000225 if (!ssh_add_identity_constrained(ac, private, comment,
226 lifetime, confirm)) {
227 error("Certificate %s (%s) add failed", certpath,
228 private->cert->key_id);
229 }
230 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
231 private->cert->key_id);
232 if (lifetime != 0)
233 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
234 if (confirm != 0)
235 fprintf(stderr, "The user must confirm each use of the key\n");
236 out:
Damien Miller0a80ca12010-02-27 07:55:05 +1100237 xfree(certpath);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000238 xfree(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000239 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100240
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000241 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242}
243
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000244static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000245update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000246{
Ben Lindstromba72d302002-03-22 03:51:06 +0000247 char *pin;
Damien Miller00111382003-03-10 11:21:17 +1100248 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000249
Damien Miller7ea845e2010-02-12 09:21:02 +1100250 pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN);
Ben Lindstromba72d302002-03-22 03:51:06 +0000251 if (pin == NULL)
252 return -1;
253
Damien Millerd94f20d2003-06-11 22:06:33 +1000254 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000255 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100256 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100257 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000258 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000259 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100260 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100261 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000262 }
Damien Miller00111382003-03-10 11:21:17 +1100263 xfree(pin);
264 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000265}
266
Damien Miller43cba342002-02-05 12:12:49 +1100267static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000268list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269{
Damien Millerad833b32000-08-23 10:46:23 +1000270 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000271 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000272 int had_identities = 0;
273 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274
Damien Millerad833b32000-08-23 10:46:23 +1000275 for (version = 1; version <= 2; version++) {
276 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100277 key != NULL;
278 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000279 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000280 if (do_fp) {
281 fp = key_fingerprint(key, SSH_FP_MD5,
282 SSH_FP_HEX);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100283 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000284 key_size(key), fp, comment, key_type(key));
285 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100286 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000287 if (!key_write(key, stdout))
288 fprintf(stderr, "key_write failed");
289 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100290 }
Damien Millerad833b32000-08-23 10:46:23 +1000291 key_free(key);
292 xfree(comment);
Damien Miller95def091999-11-25 00:26:21 +1100293 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100294 }
Damien Miller43cba342002-02-05 12:12:49 +1100295 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100296 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100297 return -1;
298 }
299 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300}
301
Damien Miller6e1057c2002-01-22 23:05:59 +1100302static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000303lock_agent(AuthenticationConnection *ac, int lock)
304{
305 char prompt[100], *p1, *p2;
306 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000307
Ben Lindstrom2f717042002-06-06 21:52:03 +0000308 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
309 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
310 if (lock) {
311 strlcpy(prompt, "Again: ", sizeof prompt);
312 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
313 if (strcmp(p1, p2) != 0) {
314 fprintf(stderr, "Passwords do not match.\n");
315 passok = 0;
316 }
317 memset(p2, 0, strlen(p2));
318 xfree(p2);
319 }
320 if (passok && ssh_lock_agent(ac, lock, p1)) {
321 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
322 ret = 0;
323 } else
324 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
325 memset(p1, 0, strlen(p1));
326 xfree(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000327 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000328}
329
330static int
Damien Miller6e1057c2002-01-22 23:05:59 +1100331do_file(AuthenticationConnection *ac, int deleting, char *file)
332{
333 if (deleting) {
334 if (delete_file(ac, file) == -1)
335 return -1;
336 } else {
337 if (add_file(ac, file) == -1)
338 return -1;
339 }
340 return 0;
341}
342
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000343static void
344usage(void)
345{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000346 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000347 fprintf(stderr, "Options:\n");
348 fprintf(stderr, " -l List fingerprints of all identities.\n");
349 fprintf(stderr, " -L List public key parameters of all identities.\n");
350 fprintf(stderr, " -d Delete identity.\n");
351 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000352 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000353 fprintf(stderr, " -X Unlock agent.\n");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000354 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Damien Miller6c711792003-01-24 11:36:23 +1100355 fprintf(stderr, " -c Require confirmation to sign using identities\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100356 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
357 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000358}
359
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360int
Damien Miller01ab4a21999-10-28 15:23:30 +1000361main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000363 extern char *optarg;
364 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100365 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100366 char *pkcs11provider = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000367 int i, ch, deleting = 0, ret = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
Darren Tuckerce321d82005-10-03 18:11:24 +1000369 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
370 sanitise_stdfd();
371
Damien Miller59d3d5b2003-08-22 09:34:41 +1000372 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000373 init_rng();
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000374 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000375
Damien Miller4314c2b2010-09-10 11:12:09 +1000376 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000377
Damien Miller95def091999-11-25 00:26:21 +1100378 /* At first, get a connection to the authentication agent. */
379 ac = ssh_get_authentication_connection();
380 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100381 fprintf(stderr,
382 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100383 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384 }
Damien Miller6c711792003-01-24 11:36:23 +1100385 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000386 switch (ch) {
387 case 'l':
388 case 'L':
Damien Miller43cba342002-02-05 12:12:49 +1100389 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
390 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000391 goto done;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000392 case 'x':
393 case 'X':
394 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
395 ret = 1;
396 goto done;
Damien Miller6c711792003-01-24 11:36:23 +1100397 case 'c':
398 confirm = 1;
399 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000400 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100401 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000402 break;
403 case 'D':
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000404 if (delete_all(ac) == -1)
405 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000406 goto done;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000407 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100408 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000409 break;
410 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100411 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100412 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000413 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000414 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000415 if ((lifetime = convtime(optarg)) == -1) {
416 fprintf(stderr, "Invalid lifetime\n");
417 ret = 1;
418 goto done;
419 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000420 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000421 default:
422 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000423 ret = 1;
424 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000425 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000427 argc -= optind;
428 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100429 if (pkcs11provider != NULL) {
430 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000431 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000432 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000433 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000434 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100435 char buf[MAXPATHLEN];
436 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000437 struct stat st;
438 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100439
440 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100441 fprintf(stderr, "No user found with uid %u\n",
442 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000443 ret = 1;
444 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100445 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100446
Darren Tucker47eede72005-03-14 23:08:12 +1100447 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000448 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100449 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000450 if (stat(buf, &st) < 0)
451 continue;
Damien Miller6e1057c2002-01-22 23:05:59 +1100452 if (do_file(ac, deleting, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000453 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000454 else
455 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000456 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000457 if (count == 0)
458 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000459 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100460 for (i = 0; i < argc; i++) {
Damien Millera055c252002-01-22 23:07:01 +1100461 if (do_file(ac, deleting, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100462 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000463 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 }
Ben Lindstromee617942001-04-10 02:45:32 +0000465 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000466
467done:
Damien Miller95def091999-11-25 00:26:21 +1100468 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000469 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470}