Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Adds an identity to the authentication server, or removes an identity. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * As far as I am concerned, the code I have written for this software |
| 8 | * can be used freely for any purpose. Any derived versions of this |
| 9 | * software must be clearly marked as such, and if the derived work is |
| 10 | * incompatible with the protocol description in the RFC file, it must be |
| 11 | * called by a name other than "ssh" or "Secure Shell". |
| 12 | * |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 13 | * SSH2 implementation, |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 14 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
| 37 | #include "includes.h" |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 38 | RCSID("$OpenBSD: ssh-add.c,v 1.72 2005/07/17 07:17:55 djm Exp $"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 39 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 40 | #include <openssl/evp.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 42 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "rsa.h" |
| 44 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 46 | #include "key.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 47 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 48 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 49 | #include "pathnames.h" |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 50 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 52 | /* argv0 */ |
| 53 | extern char *__progname; |
| 54 | |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 55 | /* Default files to add */ |
| 56 | static char *default_files[] = { |
| 57 | _PATH_SSH_CLIENT_ID_RSA, |
| 58 | _PATH_SSH_CLIENT_ID_DSA, |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 59 | _PATH_SSH_CLIENT_IDENTITY, |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 60 | NULL |
| 61 | }; |
| 62 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 63 | /* Default lifetime (0 == forever) */ |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 64 | static int lifetime = 0; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 65 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 66 | /* User has to confirm key use */ |
| 67 | static int confirm = 0; |
| 68 | |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 69 | /* we keep a cache of one passphrases */ |
| 70 | static char *pass = NULL; |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 71 | static void |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 72 | clear_pass(void) |
| 73 | { |
| 74 | if (pass) { |
| 75 | memset(pass, 0, strlen(pass)); |
| 76 | xfree(pass); |
| 77 | pass = NULL; |
| 78 | } |
| 79 | } |
| 80 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 81 | static int |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 82 | delete_file(AuthenticationConnection *ac, const char *filename) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 84 | Key *public; |
Ben Lindstrom | d5730a8 | 2001-04-08 18:04:36 +0000 | [diff] [blame] | 85 | char *comment = NULL; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 86 | int ret = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 87 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 88 | public = key_load_public(filename, &comment); |
| 89 | if (public == NULL) { |
| 90 | printf("Bad key file %s\n", filename); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 91 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | } |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 93 | if (ssh_remove_identity(ac, public)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 95 | ret = 0; |
| 96 | } else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | fprintf(stderr, "Could not remove identity: %s\n", filename); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 98 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 99 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 100 | xfree(comment); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 101 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 102 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | } |
| 104 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 105 | /* Send a request to remove all identities. */ |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 106 | static int |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 107 | delete_all(AuthenticationConnection *ac) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 108 | { |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 109 | int ret = -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 110 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 111 | if (ssh_remove_all_identities(ac, 1)) |
| 112 | ret = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 113 | /* ignore error-code for ssh2 */ |
| 114 | ssh_remove_all_identities(ac, 2); |
| 115 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 116 | if (ret == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | fprintf(stderr, "All identities removed.\n"); |
| 118 | else |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 119 | fprintf(stderr, "Failed to remove all identities.\n"); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 120 | |
| 121 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | } |
| 123 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 124 | static int |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 125 | add_file(AuthenticationConnection *ac, const char *filename) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 126 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 127 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 128 | Key *private; |
Ben Lindstrom | 5eb97b6 | 2001-04-19 20:33:07 +0000 | [diff] [blame] | 129 | char *comment = NULL; |
| 130 | char msg[1024]; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 131 | int ret = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 133 | if (stat(filename, &st) < 0) { |
| 134 | perror(filename); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 135 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 136 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | /* At first, try empty passphrase */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 138 | private = key_load_private(filename, "", &comment); |
| 139 | if (comment == NULL) |
| 140 | comment = xstrdup(filename); |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 141 | /* try last */ |
| 142 | if (private == NULL && pass != NULL) |
| 143 | private = key_load_private(filename, pass, NULL); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 144 | if (private == NULL) { |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 145 | /* clear passphrase since it did not work */ |
| 146 | clear_pass(); |
Ben Lindstrom | 8a13713 | 2001-05-02 22:40:12 +0000 | [diff] [blame] | 147 | snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ", |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 148 | comment); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 149 | for (;;) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 150 | pass = read_passphrase(msg, RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | if (strcmp(pass, "") == 0) { |
Ben Lindstrom | 7457f2a | 2001-04-14 23:10:09 +0000 | [diff] [blame] | 152 | clear_pass(); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 153 | xfree(comment); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 154 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 156 | private = key_load_private(filename, pass, &comment); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 157 | if (private != NULL) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | break; |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 159 | clear_pass(); |
Damien Miller | 80163e1 | 2003-06-18 20:29:18 +1000 | [diff] [blame] | 160 | snprintf(msg, sizeof msg, |
| 161 | "Bad passphrase, try again for %.200s: ", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | } |
| 163 | } |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 164 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 165 | if (ssh_add_identity_constrained(ac, private, comment, lifetime, |
| 166 | confirm)) { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 167 | fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 168 | ret = 0; |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 169 | if (lifetime != 0) |
Ben Lindstrom | 93576d9 | 2002-12-23 02:06:19 +0000 | [diff] [blame] | 170 | fprintf(stderr, |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 171 | "Lifetime set to %d seconds\n", lifetime); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 172 | if (confirm != 0) |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 173 | fprintf(stderr, |
| 174 | "The user has to confirm each use of the key\n"); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 175 | } else if (ssh_add_identity(ac, private, comment)) { |
| 176 | fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); |
| 177 | ret = 0; |
| 178 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | fprintf(stderr, "Could not add identity: %s\n", filename); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 182 | xfree(comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 183 | key_free(private); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 184 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 185 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | } |
| 187 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 188 | static int |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 189 | update_card(AuthenticationConnection *ac, int add, const char *id) |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 190 | { |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 191 | char *pin; |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 192 | int ret = -1; |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 193 | |
| 194 | pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN); |
| 195 | if (pin == NULL) |
| 196 | return -1; |
| 197 | |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 198 | if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) { |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 199 | fprintf(stderr, "Card %s: %s\n", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 200 | add ? "added" : "removed", id); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 201 | ret = 0; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 202 | } else { |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 203 | fprintf(stderr, "Could not %s card: %s\n", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 204 | add ? "add" : "remove", id); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 205 | ret = -1; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 206 | } |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 207 | xfree(pin); |
| 208 | return ret; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 211 | static int |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 212 | list_identities(AuthenticationConnection *ac, int do_fp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 214 | Key *key; |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 215 | char *comment, *fp; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 216 | int had_identities = 0; |
| 217 | int version; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 218 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 219 | for (version = 1; version <= 2; version++) { |
| 220 | for (key = ssh_get_first_identity(ac, &comment, version); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 221 | key != NULL; |
| 222 | key = ssh_get_next_identity(ac, &comment, version)) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 223 | had_identities = 1; |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 224 | if (do_fp) { |
| 225 | fp = key_fingerprint(key, SSH_FP_MD5, |
| 226 | SSH_FP_HEX); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 227 | printf("%d %s %s (%s)\n", |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 228 | key_size(key), fp, comment, key_type(key)); |
| 229 | xfree(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | } else { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 231 | if (!key_write(key, stdout)) |
| 232 | fprintf(stderr, "key_write failed"); |
| 233 | fprintf(stdout, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 234 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 235 | key_free(key); |
| 236 | xfree(comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 237 | } |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 238 | } |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 239 | if (!had_identities) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | printf("The agent has no identities.\n"); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | } |
| 245 | |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 246 | static int |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 247 | lock_agent(AuthenticationConnection *ac, int lock) |
| 248 | { |
| 249 | char prompt[100], *p1, *p2; |
| 250 | int passok = 1, ret = -1; |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 251 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 252 | strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); |
| 253 | p1 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 254 | if (lock) { |
| 255 | strlcpy(prompt, "Again: ", sizeof prompt); |
| 256 | p2 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 257 | if (strcmp(p1, p2) != 0) { |
| 258 | fprintf(stderr, "Passwords do not match.\n"); |
| 259 | passok = 0; |
| 260 | } |
| 261 | memset(p2, 0, strlen(p2)); |
| 262 | xfree(p2); |
| 263 | } |
| 264 | if (passok && ssh_lock_agent(ac, lock, p1)) { |
| 265 | fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); |
| 266 | ret = 0; |
| 267 | } else |
| 268 | fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un"); |
| 269 | memset(p1, 0, strlen(p1)); |
| 270 | xfree(p1); |
Ben Lindstrom | 3390749 | 2002-06-27 00:21:59 +0000 | [diff] [blame] | 271 | return (ret); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static int |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 275 | do_file(AuthenticationConnection *ac, int deleting, char *file) |
| 276 | { |
| 277 | if (deleting) { |
| 278 | if (delete_file(ac, file) == -1) |
| 279 | return -1; |
| 280 | } else { |
| 281 | if (add_file(ac, file) == -1) |
| 282 | return -1; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 287 | static void |
| 288 | usage(void) |
| 289 | { |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 290 | fprintf(stderr, "Usage: %s [options]\n", __progname); |
| 291 | fprintf(stderr, "Options:\n"); |
| 292 | fprintf(stderr, " -l List fingerprints of all identities.\n"); |
| 293 | fprintf(stderr, " -L List public key parameters of all identities.\n"); |
| 294 | fprintf(stderr, " -d Delete identity.\n"); |
| 295 | fprintf(stderr, " -D Delete all identities.\n"); |
Ben Lindstrom | 163f3b8 | 2002-06-06 21:53:11 +0000 | [diff] [blame] | 296 | fprintf(stderr, " -x Lock agent.\n"); |
Damien Miller | 2138d15 | 2002-09-22 01:26:00 +1000 | [diff] [blame] | 297 | fprintf(stderr, " -X Unlock agent.\n"); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 298 | fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 299 | fprintf(stderr, " -c Require confirmation to sign using identities\n"); |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 300 | #ifdef SMARTCARD |
| 301 | fprintf(stderr, " -s reader Add key in smartcard reader.\n"); |
| 302 | fprintf(stderr, " -e reader Remove key in smartcard reader.\n"); |
| 303 | #endif |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 306 | int |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 307 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 308 | { |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 309 | extern char *optarg; |
| 310 | extern int optind; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 311 | AuthenticationConnection *ac = NULL; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 312 | char *sc_reader_id = NULL; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 313 | int i, ch, deleting = 0, ret = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 315 | __progname = ssh_get_progname(argv[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 316 | init_rng(); |
Ben Lindstrom | 93d1fe8 | 2001-05-06 02:57:20 +0000 | [diff] [blame] | 317 | seed_rng(); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 318 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 319 | SSLeay_add_all_algorithms(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 320 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 321 | /* At first, get a connection to the authentication agent. */ |
| 322 | ac = ssh_get_authentication_connection(); |
| 323 | if (ac == NULL) { |
| 324 | fprintf(stderr, "Could not open a connection to your authentication agent.\n"); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 325 | exit(2); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 326 | } |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 327 | while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) { |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 328 | switch (ch) { |
| 329 | case 'l': |
| 330 | case 'L': |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 331 | if (list_identities(ac, ch == 'l' ? 1 : 0) == -1) |
| 332 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 333 | goto done; |
| 334 | break; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 335 | case 'x': |
| 336 | case 'X': |
| 337 | if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1) |
| 338 | ret = 1; |
| 339 | goto done; |
| 340 | break; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 341 | case 'c': |
| 342 | confirm = 1; |
| 343 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 344 | case 'd': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | deleting = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 346 | break; |
| 347 | case 'D': |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 348 | if (delete_all(ac) == -1) |
| 349 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 350 | goto done; |
| 351 | break; |
| 352 | case 's': |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 353 | sc_reader_id = optarg; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 354 | break; |
| 355 | case 'e': |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 356 | deleting = 1; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 357 | sc_reader_id = optarg; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 358 | break; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 359 | case 't': |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 360 | if ((lifetime = convtime(optarg)) == -1) { |
| 361 | fprintf(stderr, "Invalid lifetime\n"); |
| 362 | ret = 1; |
| 363 | goto done; |
| 364 | } |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 365 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 366 | default: |
| 367 | usage(); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 368 | ret = 1; |
| 369 | goto done; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 370 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 371 | } |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 372 | argc -= optind; |
| 373 | argv += optind; |
Ben Lindstrom | f7db3bb | 2001-08-06 21:35:51 +0000 | [diff] [blame] | 374 | if (sc_reader_id != NULL) { |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 375 | if (update_card(ac, !deleting, sc_reader_id) == -1) |
| 376 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 377 | goto done; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 378 | } |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 379 | if (argc == 0) { |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 380 | char buf[MAXPATHLEN]; |
| 381 | struct passwd *pw; |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 382 | struct stat st; |
| 383 | int count = 0; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 384 | |
| 385 | if ((pw = getpwuid(getuid())) == NULL) { |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 386 | fprintf(stderr, "No user found with uid %u\n", |
| 387 | (u_int)getuid()); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 388 | ret = 1; |
| 389 | goto done; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 390 | } |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 391 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 392 | for (i = 0; default_files[i]; i++) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 393 | snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 394 | default_files[i]); |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 395 | if (stat(buf, &st) < 0) |
| 396 | continue; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 397 | if (do_file(ac, deleting, buf) == -1) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 398 | ret = 1; |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 399 | else |
| 400 | count++; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 401 | } |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 402 | if (count == 0) |
| 403 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 404 | } else { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 405 | for (i = 0; i < argc; i++) { |
Damien Miller | a055c25 | 2002-01-22 23:07:01 +1100 | [diff] [blame] | 406 | if (do_file(ac, deleting, argv[i]) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 407 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 408 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 409 | } |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 410 | clear_pass(); |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 411 | |
| 412 | done: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 413 | ssh_close_authentication_connection(ac); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 414 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 415 | } |