djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-add.c,v 1.123 2015/07/03 03:43:18 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Adds an identity to the authentication server, or removes an identity. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * 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 Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 14 | * SSH2 implementation, |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 16 | * |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 43 | #include <openssl/evp.h> |
Darren Tucker | bfaaf96 | 2008-02-28 19:13:52 +1100 | [diff] [blame] | 44 | #include "openbsd-compat/openssl-compat.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 46 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 47 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 48 | #include <pwd.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 49 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 50 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 51 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 52 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 53 | #include <unistd.h> |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 54 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 55 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 58 | #include "rsa.h" |
| 59 | #include "log.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 60 | #include "sshkey.h" |
| 61 | #include "sshbuf.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 62 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 63 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 64 | #include "pathnames.h" |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 65 | #include "misc.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 66 | #include "ssherr.h" |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 67 | #include "digest.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 69 | /* argv0 */ |
| 70 | extern char *__progname; |
| 71 | |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 72 | /* Default files to add */ |
| 73 | static char *default_files[] = { |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 74 | #ifdef WITH_OPENSSL |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 75 | _PATH_SSH_CLIENT_ID_RSA, |
| 76 | _PATH_SSH_CLIENT_ID_DSA, |
Damien Miller | dd190dd | 2010-11-11 14:17:02 +1100 | [diff] [blame] | 77 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 78 | _PATH_SSH_CLIENT_ID_ECDSA, |
Damien Miller | dd190dd | 2010-11-11 14:17:02 +1100 | [diff] [blame] | 79 | #endif |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 80 | #endif /* WITH_OPENSSL */ |
Damien Miller | 059321d | 2013-12-18 17:49:48 +1100 | [diff] [blame] | 81 | _PATH_SSH_CLIENT_ID_ED25519, |
naddy@openbsd.org | 1b0ef38 | 2015-03-26 12:32:38 +0000 | [diff] [blame] | 82 | #ifdef WITH_SSH1 |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 83 | _PATH_SSH_CLIENT_IDENTITY, |
naddy@openbsd.org | 1b0ef38 | 2015-03-26 12:32:38 +0000 | [diff] [blame] | 84 | #endif |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 85 | NULL |
| 86 | }; |
| 87 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 88 | static int fingerprint_hash = SSH_FP_HASH_DEFAULT; |
| 89 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 90 | /* Default lifetime (0 == forever) */ |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 91 | static int lifetime = 0; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 92 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 93 | /* User has to confirm key use */ |
| 94 | static int confirm = 0; |
| 95 | |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 96 | /* we keep a cache of one passphrases */ |
| 97 | static char *pass = NULL; |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 98 | static void |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 99 | clear_pass(void) |
| 100 | { |
| 101 | if (pass) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 102 | explicit_bzero(pass, strlen(pass)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 103 | free(pass); |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 104 | pass = NULL; |
| 105 | } |
| 106 | } |
| 107 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 108 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 109 | delete_file(int agent_fd, const char *filename, int key_only) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 111 | struct sshkey *public, *cert = NULL; |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 112 | char *certpath = NULL, *comment = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 113 | int r, ret = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 115 | if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { |
| 116 | printf("Bad key file %s: %s\n", filename, ssh_err(r)); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 117 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 119 | if ((r = ssh_remove_identity(agent_fd, public)) == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 121 | ret = 0; |
| 122 | } else |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 123 | fprintf(stderr, "Could not remove identity \"%s\": %s\n", |
| 124 | filename, ssh_err(r)); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 125 | |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 126 | if (key_only) |
| 127 | goto out; |
| 128 | |
| 129 | /* Now try to delete the corresponding certificate too */ |
| 130 | free(comment); |
Darren Tucker | 8a96522 | 2012-12-07 13:07:02 +1100 | [diff] [blame] | 131 | comment = NULL; |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 132 | xasprintf(&certpath, "%s-cert.pub", filename); |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 133 | if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { |
| 134 | if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) |
| 135 | error("Failed to load certificate \"%s\": %s", |
| 136 | certpath, ssh_err(r)); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 137 | goto out; |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 138 | } |
| 139 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 140 | if (!sshkey_equal_public(cert, public)) |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 141 | fatal("Certificate %s does not match private key %s", |
| 142 | certpath, filename); |
| 143 | |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 144 | if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 145 | fprintf(stderr, "Identity removed: %s (%s)\n", certpath, |
| 146 | comment); |
| 147 | ret = 0; |
| 148 | } else |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 149 | fprintf(stderr, "Could not remove identity \"%s\": %s\n", |
| 150 | certpath, ssh_err(r)); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 151 | |
| 152 | out: |
| 153 | if (cert != NULL) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 154 | sshkey_free(cert); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 155 | if (public != NULL) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 156 | sshkey_free(public); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 157 | free(certpath); |
| 158 | free(comment); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 159 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 160 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | } |
| 162 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 163 | /* Send a request to remove all identities. */ |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 164 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 165 | delete_all(int agent_fd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | { |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 167 | int ret = -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 168 | |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 169 | if (ssh_remove_all_identities(agent_fd, 2) == 0) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 170 | ret = 0; |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 171 | /* ignore error-code for ssh1 */ |
| 172 | ssh_remove_all_identities(agent_fd, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 173 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 174 | if (ret == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | fprintf(stderr, "All identities removed.\n"); |
| 176 | else |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 177 | fprintf(stderr, "Failed to remove all identities.\n"); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 178 | |
| 179 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 182 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 183 | add_file(int agent_fd, const char *filename, int key_only) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 185 | struct sshkey *private, *cert; |
Ben Lindstrom | 5eb97b6 | 2001-04-19 20:33:07 +0000 | [diff] [blame] | 186 | char *comment = NULL; |
Damien Miller | 8f4279e | 2011-10-18 16:06:33 +1100 | [diff] [blame] | 187 | char msg[1024], *certpath = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 188 | int r, fd, ret = -1; |
| 189 | struct sshbuf *keyblob; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 191 | if (strcmp(filename, "-") == 0) { |
| 192 | fd = STDIN_FILENO; |
| 193 | filename = "(stdin)"; |
| 194 | } else if ((fd = open(filename, O_RDONLY)) < 0) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 195 | perror(filename); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 196 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 197 | } |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Since we'll try to load a keyfile multiple times, permission errors |
| 201 | * will occur multiple times, so check perms first and bail if wrong. |
| 202 | */ |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 203 | if (fd != STDIN_FILENO) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 204 | if (sshkey_perm_ok(fd, filename) != 0) { |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 205 | close(fd); |
| 206 | return -1; |
| 207 | } |
| 208 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 209 | if ((keyblob = sshbuf_new()) == NULL) |
| 210 | fatal("%s: sshbuf_new failed", __func__); |
| 211 | if ((r = sshkey_load_file(fd, keyblob)) != 0) { |
| 212 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 213 | filename, ssh_err(r)); |
| 214 | sshbuf_free(keyblob); |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 215 | close(fd); |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 216 | return -1; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 217 | } |
| 218 | close(fd); |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 219 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 220 | /* At first, try empty passphrase */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 221 | if ((r = sshkey_parse_private_fileblob(keyblob, "", filename, |
| 222 | &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 223 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 224 | filename, ssh_err(r)); |
| 225 | goto fail_load; |
| 226 | } |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 227 | /* try last */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 228 | if (private == NULL && pass != NULL) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 229 | if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 230 | &private, &comment)) != 0 && |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 231 | r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 232 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 233 | filename, ssh_err(r)); |
| 234 | goto fail_load; |
| 235 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 236 | } |
Damien Miller | 1d0df32 | 2014-07-11 09:19:04 +1000 | [diff] [blame] | 237 | if (comment == NULL) |
| 238 | comment = xstrdup(filename); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 239 | if (private == NULL) { |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 240 | /* clear passphrase since it did not work */ |
| 241 | clear_pass(); |
halex@openbsd.org | 5d34aa9 | 2015-02-03 00:34:14 +0000 | [diff] [blame] | 242 | snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ", |
| 243 | comment, confirm ? " (will confirm each use)" : ""); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 244 | for (;;) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 245 | pass = read_passphrase(msg, RP_ALLOW_STDIN); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 246 | if (strcmp(pass, "") == 0) |
| 247 | goto fail_load; |
| 248 | if ((r = sshkey_parse_private_fileblob(keyblob, pass, |
| 249 | filename, &private, NULL)) == 0) |
| 250 | break; |
| 251 | else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 252 | fprintf(stderr, |
| 253 | "Error loading key \"%s\": %s\n", |
| 254 | filename, ssh_err(r)); |
| 255 | fail_load: |
Ben Lindstrom | 7457f2a | 2001-04-14 23:10:09 +0000 | [diff] [blame] | 256 | clear_pass(); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 257 | free(comment); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 258 | sshbuf_free(keyblob); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 259 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 260 | } |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 261 | clear_pass(); |
Damien Miller | 80163e1 | 2003-06-18 20:29:18 +1000 | [diff] [blame] | 262 | snprintf(msg, sizeof msg, |
halex@openbsd.org | 5d34aa9 | 2015-02-03 00:34:14 +0000 | [diff] [blame] | 263 | "Bad passphrase, try again for %.200s%s: ", comment, |
| 264 | confirm ? " (will confirm each use)" : ""); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | } |
| 266 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 267 | sshbuf_free(keyblob); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 268 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 269 | if ((r = ssh_add_identity_constrained(agent_fd, private, comment, |
| 270 | lifetime, confirm)) == 0) { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 271 | fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 272 | ret = 0; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 273 | if (lifetime != 0) |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 274 | fprintf(stderr, |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 275 | "Lifetime set to %d seconds\n", lifetime); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 276 | if (confirm != 0) |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 277 | fprintf(stderr, |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 278 | "The user must confirm each use of the key\n"); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 279 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 280 | fprintf(stderr, "Could not add identity \"%s\": %s\n", |
| 281 | filename, ssh_err(r)); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Damien Miller | 8f4279e | 2011-10-18 16:06:33 +1100 | [diff] [blame] | 284 | /* Skip trying to load the cert if requested */ |
| 285 | if (key_only) |
| 286 | goto out; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 287 | |
| 288 | /* Now try to add the certificate flavour too */ |
| 289 | xasprintf(&certpath, "%s-cert.pub", filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 290 | if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { |
| 291 | if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) |
| 292 | error("Failed to load certificate \"%s\": %s", |
| 293 | certpath, ssh_err(r)); |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 294 | goto out; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 295 | } |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 296 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 297 | if (!sshkey_equal_public(cert, private)) { |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 298 | error("Certificate %s does not match private key %s", |
| 299 | certpath, filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 300 | sshkey_free(cert); |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 301 | goto out; |
| 302 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 303 | |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 304 | /* Graft with private bits */ |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 305 | if ((r = sshkey_to_certified(private)) != 0) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 306 | error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); |
| 307 | sshkey_free(cert); |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 308 | goto out; |
Damien Miller | 0bd4186 | 2010-03-03 10:25:41 +1100 | [diff] [blame] | 309 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 310 | if ((r = sshkey_cert_copy(cert, private)) != 0) { |
| 311 | error("%s: key_cert_copy: %s", __func__, ssh_err(r)); |
| 312 | sshkey_free(cert); |
| 313 | goto out; |
| 314 | } |
| 315 | sshkey_free(cert); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 316 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 317 | if ((r = ssh_add_identity_constrained(agent_fd, private, comment, |
| 318 | lifetime, confirm)) != 0) { |
| 319 | error("Certificate %s (%s) add failed: %s", certpath, |
| 320 | private->cert->key_id, ssh_err(r)); |
| 321 | goto out; |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 322 | } |
| 323 | fprintf(stderr, "Certificate added: %s (%s)\n", certpath, |
| 324 | private->cert->key_id); |
| 325 | if (lifetime != 0) |
| 326 | fprintf(stderr, "Lifetime set to %d seconds\n", lifetime); |
| 327 | if (confirm != 0) |
| 328 | fprintf(stderr, "The user must confirm each use of the key\n"); |
| 329 | out: |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 330 | free(certpath); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 331 | free(comment); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 332 | sshkey_free(private); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 333 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 334 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 335 | } |
| 336 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 337 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 338 | update_card(int agent_fd, int add, const char *id) |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 339 | { |
Damien Miller | ef275ea | 2013-12-29 17:44:07 +1100 | [diff] [blame] | 340 | char *pin = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 341 | int r, ret = -1; |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 342 | |
Damien Miller | ef275ea | 2013-12-29 17:44:07 +1100 | [diff] [blame] | 343 | if (add) { |
| 344 | if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", |
| 345 | RP_ALLOW_STDIN)) == NULL) |
| 346 | return -1; |
| 347 | } |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 348 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 349 | if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, |
| 350 | lifetime, confirm)) == 0) { |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 351 | fprintf(stderr, "Card %s: %s\n", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 352 | add ? "added" : "removed", id); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 353 | ret = 0; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 354 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 355 | fprintf(stderr, "Could not %s card \"%s\": %s\n", |
| 356 | add ? "add" : "remove", id, ssh_err(r)); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 357 | ret = -1; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 358 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 359 | free(pin); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 360 | return ret; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 363 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 364 | list_identities(int agent_fd, int do_fp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 365 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 366 | char *fp; |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 367 | int r, had_identities = 0; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 368 | struct ssh_identitylist *idlist; |
| 369 | size_t i; |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 370 | #ifdef WITH_SSH1 |
| 371 | int version = 1; |
| 372 | #else |
| 373 | int version = 2; |
| 374 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 375 | |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 376 | for (; version <= 2; version++) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 377 | if ((r = ssh_fetch_identitylist(agent_fd, version, |
| 378 | &idlist)) != 0) { |
| 379 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 380 | fprintf(stderr, "error fetching identities for " |
| 381 | "protocol %d: %s\n", version, ssh_err(r)); |
| 382 | continue; |
| 383 | } |
| 384 | for (i = 0; i < idlist->nkeys; i++) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 385 | had_identities = 1; |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 386 | if (do_fp) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 387 | fp = sshkey_fingerprint(idlist->keys[i], |
| 388 | fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 389 | printf("%d %s %s (%s)\n", |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 390 | sshkey_size(idlist->keys[i]), |
| 391 | fp == NULL ? "(null)" : fp, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 392 | idlist->comments[i], |
| 393 | sshkey_type(idlist->keys[i])); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 394 | free(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 395 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 396 | if ((r = sshkey_write(idlist->keys[i], |
| 397 | stdout)) != 0) { |
| 398 | fprintf(stderr, "sshkey_write: %s\n", |
| 399 | ssh_err(r)); |
| 400 | continue; |
| 401 | } |
| 402 | fprintf(stdout, " %s\n", idlist->comments[i]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 403 | } |
| 404 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 405 | ssh_free_identitylist(idlist); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 406 | } |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 407 | if (!had_identities) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 408 | printf("The agent has no identities.\n"); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 412 | } |
| 413 | |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 414 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 415 | lock_agent(int agent_fd, int lock) |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 416 | { |
| 417 | char prompt[100], *p1, *p2; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 418 | int r, passok = 1, ret = -1; |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 419 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 420 | strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); |
| 421 | p1 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 422 | if (lock) { |
| 423 | strlcpy(prompt, "Again: ", sizeof prompt); |
| 424 | p2 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 425 | if (strcmp(p1, p2) != 0) { |
| 426 | fprintf(stderr, "Passwords do not match.\n"); |
| 427 | passok = 0; |
| 428 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 429 | explicit_bzero(p2, strlen(p2)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 430 | free(p2); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 431 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 432 | if (passok) { |
| 433 | if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { |
| 434 | fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); |
| 435 | ret = 0; |
| 436 | } else { |
| 437 | fprintf(stderr, "Failed to %slock agent: %s\n", |
| 438 | lock ? "" : "un", ssh_err(r)); |
| 439 | } |
| 440 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 441 | explicit_bzero(p1, strlen(p1)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 442 | free(p1); |
Ben Lindstrom | 3390749 | 2002-06-27 00:21:59 +0000 | [diff] [blame] | 443 | return (ret); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 447 | do_file(int agent_fd, int deleting, int key_only, char *file) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 448 | { |
| 449 | if (deleting) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 450 | if (delete_file(agent_fd, file, key_only) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 451 | return -1; |
| 452 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 453 | if (add_file(agent_fd, file, key_only) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 454 | return -1; |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 459 | static void |
| 460 | usage(void) |
| 461 | { |
Damien Miller | 5cbe7ca | 2007-09-17 16:05:50 +1000 | [diff] [blame] | 462 | fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 463 | fprintf(stderr, "Options:\n"); |
| 464 | fprintf(stderr, " -l List fingerprints of all identities.\n"); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 465 | fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n"); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 466 | fprintf(stderr, " -L List public key parameters of all identities.\n"); |
Darren Tucker | e68cf84 | 2011-11-04 10:51:51 +1100 | [diff] [blame] | 467 | fprintf(stderr, " -k Load only keys and not certificates.\n"); |
| 468 | fprintf(stderr, " -c Require confirmation to sign using identities\n"); |
| 469 | fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 470 | fprintf(stderr, " -d Delete identity.\n"); |
| 471 | fprintf(stderr, " -D Delete all identities.\n"); |
Ben Lindstrom | 163f3b8 | 2002-06-06 21:53:11 +0000 | [diff] [blame] | 472 | fprintf(stderr, " -x Lock agent.\n"); |
Damien Miller | 2138d15 | 2002-09-22 01:26:00 +1000 | [diff] [blame] | 473 | fprintf(stderr, " -X Unlock agent.\n"); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 474 | fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); |
| 475 | fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 478 | int |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 479 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 480 | { |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 481 | extern char *optarg; |
| 482 | extern int optind; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 483 | int agent_fd; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 484 | char *pkcs11provider = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 485 | int r, i, ch, deleting = 0, ret = 0, key_only = 0; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 486 | int xflag = 0, lflag = 0, Dflag = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 487 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 488 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 489 | sanitise_stdfd(); |
| 490 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 491 | __progname = ssh_get_progname(argv[0]); |
Ben Lindstrom | 93d1fe8 | 2001-05-06 02:57:20 +0000 | [diff] [blame] | 492 | seed_rng(); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 493 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 494 | #ifdef WITH_OPENSSL |
Damien Miller | 4314c2b | 2010-09-10 11:12:09 +1000 | [diff] [blame] | 495 | OpenSSL_add_all_algorithms(); |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 496 | #endif |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 497 | |
millert@openbsd.org | db995f2 | 2014-11-26 18:34:51 +0000 | [diff] [blame] | 498 | setvbuf(stdout, NULL, _IOLBF, 0); |
Damien Miller | d7ca2cd | 2014-07-03 21:23:01 +1000 | [diff] [blame] | 499 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 500 | /* First, get a connection to the authentication agent. */ |
| 501 | switch (r = ssh_get_authentication_socket(&agent_fd)) { |
| 502 | case 0: |
| 503 | break; |
| 504 | case SSH_ERR_AGENT_NOT_PRESENT: |
| 505 | fprintf(stderr, "Could not open a connection to your " |
| 506 | "authentication agent.\n"); |
| 507 | exit(2); |
| 508 | default: |
| 509 | fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 510 | exit(2); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 511 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 512 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 513 | while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) { |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 514 | switch (ch) { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 515 | case 'E': |
| 516 | fingerprint_hash = ssh_digest_alg_by_name(optarg); |
| 517 | if (fingerprint_hash == -1) |
| 518 | fatal("Invalid hash algorithm \"%s\"", optarg); |
| 519 | break; |
Damien Miller | 8f4279e | 2011-10-18 16:06:33 +1100 | [diff] [blame] | 520 | case 'k': |
| 521 | key_only = 1; |
| 522 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 523 | case 'l': |
| 524 | case 'L': |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 525 | if (lflag != 0) |
| 526 | fatal("-%c flag already specified", lflag); |
| 527 | lflag = ch; |
| 528 | break; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 529 | case 'x': |
| 530 | case 'X': |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 531 | if (xflag != 0) |
| 532 | fatal("-%c flag already specified", xflag); |
| 533 | xflag = ch; |
| 534 | break; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 535 | case 'c': |
| 536 | confirm = 1; |
| 537 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 538 | case 'd': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 539 | deleting = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 540 | break; |
| 541 | case 'D': |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 542 | Dflag = 1; |
| 543 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 544 | case 's': |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 545 | pkcs11provider = optarg; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 546 | break; |
| 547 | case 'e': |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 548 | deleting = 1; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 549 | pkcs11provider = optarg; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 550 | break; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 551 | case 't': |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 552 | if ((lifetime = convtime(optarg)) == -1) { |
| 553 | fprintf(stderr, "Invalid lifetime\n"); |
| 554 | ret = 1; |
| 555 | goto done; |
| 556 | } |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 557 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 558 | default: |
| 559 | usage(); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 560 | ret = 1; |
| 561 | goto done; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 562 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 563 | } |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 564 | |
| 565 | if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) |
| 566 | fatal("Invalid combination of actions"); |
| 567 | else if (xflag) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 568 | if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 569 | ret = 1; |
| 570 | goto done; |
| 571 | } else if (lflag) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 572 | if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 573 | ret = 1; |
| 574 | goto done; |
| 575 | } else if (Dflag) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 576 | if (delete_all(agent_fd) == -1) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 577 | ret = 1; |
| 578 | goto done; |
| 579 | } |
| 580 | |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 581 | argc -= optind; |
| 582 | argv += optind; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 583 | if (pkcs11provider != NULL) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 584 | if (update_card(agent_fd, !deleting, pkcs11provider) == -1) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 585 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 586 | goto done; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 587 | } |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 588 | if (argc == 0) { |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 589 | char buf[PATH_MAX]; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 590 | struct passwd *pw; |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 591 | struct stat st; |
| 592 | int count = 0; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 593 | |
| 594 | if ((pw = getpwuid(getuid())) == NULL) { |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 595 | fprintf(stderr, "No user found with uid %u\n", |
| 596 | (u_int)getuid()); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 597 | ret = 1; |
| 598 | goto done; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 599 | } |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 600 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 601 | for (i = 0; default_files[i]; i++) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 602 | snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 603 | default_files[i]); |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 604 | if (stat(buf, &st) < 0) |
| 605 | continue; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 606 | if (do_file(agent_fd, deleting, key_only, buf) == -1) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 607 | ret = 1; |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 608 | else |
| 609 | count++; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 610 | } |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 611 | if (count == 0) |
| 612 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 613 | } else { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 614 | for (i = 0; i < argc; i++) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 615 | if (do_file(agent_fd, deleting, key_only, |
| 616 | argv[i]) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 617 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 618 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | } |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 620 | clear_pass(); |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 621 | |
| 622 | done: |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 623 | ssh_close_authentication_socket(agent_fd); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 624 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 625 | } |