]> andersk Git - openssh.git/blame - ssh-add.c
- djm@cvs.openbsd.org 2001/12/21 08:53:45
[openssh.git] / ssh-add.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Adds an identity to the authentication server, or removes an identity.
2e73a022 6 *
bcbf86ec 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 *
2e73a022 13 * SSH2 implementation,
a96070d4 14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 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.
5260325f 35 */
8efc0c15 36
37#include "includes.h"
184eed6a 38RCSID("$OpenBSD: ssh-add.c,v 1.47 2001/12/19 07:18:56 deraadt Exp $");
8efc0c15 39
2e73a022 40#include <openssl/evp.h>
a306f2dd 41
8efc0c15 42#include "ssh.h"
42f11eb2 43#include "rsa.h"
44#include "log.h"
8efc0c15 45#include "xmalloc.h"
a306f2dd 46#include "key.h"
4c8722d9 47#include "authfd.h"
a306f2dd 48#include "authfile.h"
42f11eb2 49#include "pathnames.h"
50#include "readpass.h"
8efc0c15 51
f601d847 52#ifdef HAVE___PROGNAME
53extern char *__progname;
260d427b 54#else
55char *__progname;
56#endif
f601d847 57
33e766d2 58/* argv0 */
59extern char *__progname;
60
a8666d84 61/* we keep a cache of one passphrases */
62static char *pass = NULL;
396c147e 63static void
a8666d84 64clear_pass(void)
65{
66 if (pass) {
67 memset(pass, 0, strlen(pass));
68 xfree(pass);
69 pass = NULL;
70 }
71}
72
e0543e42 73static int
5068f573 74delete_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 75{
a306f2dd 76 Key *public;
d64050ef 77 char *comment = NULL;
e0543e42 78 int ret = -1;
5260325f 79
aeaa3d9e 80 public = key_load_public(filename, &comment);
81 if (public == NULL) {
82 printf("Bad key file %s\n", filename);
e0543e42 83 return -1;
5260325f 84 }
e0543e42 85 if (ssh_remove_identity(ac, public)) {
5260325f 86 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
e0543e42 87 ret = 0;
88 } else
5260325f 89 fprintf(stderr, "Could not remove identity: %s\n", filename);
e0543e42 90
a306f2dd 91 key_free(public);
5260325f 92 xfree(comment);
184eed6a 93
e0543e42 94 return ret;
8efc0c15 95}
96
2e73a022 97/* Send a request to remove all identities. */
e0543e42 98static int
5068f573 99delete_all(AuthenticationConnection *ac)
8efc0c15 100{
e0543e42 101 int ret = -1;
2e73a022 102
e0543e42 103 if (ssh_remove_all_identities(ac, 1))
104 ret = 0;
2e73a022 105 /* ignore error-code for ssh2 */
106 ssh_remove_all_identities(ac, 2);
107
e0543e42 108 if (ret == 0)
5260325f 109 fprintf(stderr, "All identities removed.\n");
110 else
b5c334cc 111 fprintf(stderr, "Failed to remove all identities.\n");
e0543e42 112
113 return ret;
8efc0c15 114}
115
e0543e42 116static int
5068f573 117add_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 118{
2e73a022 119 struct stat st;
a306f2dd 120 Key *private;
561e5254 121 char *comment = NULL;
122 char msg[1024];
e0543e42 123 int ret = -1;
5260325f 124
2e73a022 125 if (stat(filename, &st) < 0) {
126 perror(filename);
e0543e42 127 return -1;
2e73a022 128 }
5260325f 129 /* At first, try empty passphrase */
aeaa3d9e 130 private = key_load_private(filename, "", &comment);
131 if (comment == NULL)
132 comment = xstrdup(filename);
a8666d84 133 /* try last */
134 if (private == NULL && pass != NULL)
135 private = key_load_private(filename, pass, NULL);
aeaa3d9e 136 if (private == NULL) {
a8666d84 137 /* clear passphrase since it did not work */
138 clear_pass();
065604bb 139 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
561e5254 140 comment);
aa3378df 141 for (;;) {
1843a425 142 pass = read_passphrase(msg, RP_ALLOW_STDIN);
5260325f 143 if (strcmp(pass, "") == 0) {
eae942e2 144 clear_pass();
aeaa3d9e 145 xfree(comment);
e0543e42 146 return -1;
5260325f 147 }
aeaa3d9e 148 private = key_load_private(filename, pass, &comment);
aeaa3d9e 149 if (private != NULL)
5260325f 150 break;
a8666d84 151 clear_pass();
065604bb 152 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
5260325f 153 }
154 }
e0543e42 155 if (ssh_add_identity(ac, private, comment)) {
aeaa3d9e 156 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
e0543e42 157 ret = 0;
158 } else
5260325f 159 fprintf(stderr, "Could not add identity: %s\n", filename);
e0543e42 160
aeaa3d9e 161 xfree(comment);
a306f2dd 162 key_free(private);
184eed6a 163
e0543e42 164 return ret;
8efc0c15 165}
166
e0543e42 167static int
9ff6f66f 168update_card(AuthenticationConnection *ac, int add, const char *id)
983def13 169{
e0543e42 170 if (ssh_update_card(ac, add, id)) {
9ff6f66f 171 fprintf(stderr, "Card %s: %s\n",
184eed6a 172 add ? "added" : "removed", id);
e0543e42 173 return 0;
174 } else {
9ff6f66f 175 fprintf(stderr, "Could not %s card: %s\n",
184eed6a 176 add ? "add" : "remove", id);
e0543e42 177 return -1;
178 }
983def13 179}
180
396c147e 181static void
22138a36 182list_identities(AuthenticationConnection *ac, int do_fp)
8efc0c15 183{
2e73a022 184 Key *key;
22138a36 185 char *comment, *fp;
2e73a022 186 int had_identities = 0;
187 int version;
188
189 for (version = 1; version <= 2; version++) {
190 for (key = ssh_get_first_identity(ac, &comment, version);
184eed6a 191 key != NULL;
192 key = ssh_get_next_identity(ac, &comment, version)) {
2e73a022 193 had_identities = 1;
22138a36 194 if (do_fp) {
195 fp = key_fingerprint(key, SSH_FP_MD5,
196 SSH_FP_HEX);
fa08c86b 197 printf("%d %s %s (%s)\n",
22138a36 198 key_size(key), fp, comment, key_type(key));
199 xfree(fp);
5260325f 200 } else {
2e73a022 201 if (!key_write(key, stdout))
202 fprintf(stderr, "key_write failed");
203 fprintf(stdout, " %s\n", comment);
5260325f 204 }
2e73a022 205 key_free(key);
206 xfree(comment);
5260325f 207 }
f095fcc7 208 }
5260325f 209 if (!had_identities)
210 printf("The agent has no identities.\n");
8efc0c15 211}
212
983def13 213static void
214usage(void)
215{
33e766d2 216 fprintf(stderr, "Usage: %s [options]\n", __progname);
217 fprintf(stderr, "Options:\n");
218 fprintf(stderr, " -l List fingerprints of all identities.\n");
219 fprintf(stderr, " -L List public key parameters of all identities.\n");
220 fprintf(stderr, " -d Delete identity.\n");
221 fprintf(stderr, " -D Delete all identities.\n");
222#ifdef SMARTCARD
223 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
224 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
225#endif
983def13 226}
227
8efc0c15 228int
5068f573 229main(int argc, char **argv)
8efc0c15 230{
34e02b83 231 extern char *optarg;
232 extern int optind;
5260325f 233 AuthenticationConnection *ac = NULL;
234 struct passwd *pw;
235 char buf[1024];
9ff6f66f 236 char *sc_reader_id = NULL;
e0543e42 237 int i, ch, deleting = 0, ret = 0;
5260325f 238
260d427b 239 __progname = get_progname(argv[0]);
264dce47 240 init_rng();
044b0662 241 seed_rng();
264dce47 242
2b87da3b 243 SSLeay_add_all_algorithms();
2e73a022 244
5260325f 245 /* At first, get a connection to the authentication agent. */
246 ac = ssh_get_authentication_connection();
247 if (ac == NULL) {
248 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
249 exit(1);
8efc0c15 250 }
184eed6a 251 while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) {
34e02b83 252 switch (ch) {
253 case 'l':
254 case 'L':
255 list_identities(ac, ch == 'l' ? 1 : 0);
256 goto done;
257 break;
258 case 'd':
5260325f 259 deleting = 1;
34e02b83 260 break;
261 case 'D':
e0543e42 262 if (delete_all(ac) == -1)
263 ret = 1;
34e02b83 264 goto done;
265 break;
266 case 's':
9ff6f66f 267 sc_reader_id = optarg;
34e02b83 268 break;
269 case 'e':
184eed6a 270 deleting = 1;
9ff6f66f 271 sc_reader_id = optarg;
34e02b83 272 break;
273 default:
274 usage();
e0543e42 275 ret = 1;
276 goto done;
983def13 277 }
8efc0c15 278 }
34e02b83 279 argc -= optind;
280 argv += optind;
9ff6f66f 281 if (sc_reader_id != NULL) {
e0543e42 282 if (update_card(ac, !deleting, sc_reader_id) == -1)
283 ret = 1;
34e02b83 284 goto done;
983def13 285 }
34e02b83 286 if (argc == 0) {
5260325f 287 pw = getpwuid(getuid());
288 if (!pw) {
14a9a859 289 fprintf(stderr, "No user found with uid %u\n",
290 (u_int)getuid());
e0543e42 291 ret = 1;
292 goto done;
5260325f 293 }
42f11eb2 294 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
e0543e42 295 if (deleting) {
296 if (delete_file(ac, buf) == -1)
297 ret = 1;
298 } else {
299 if (add_file(ac, buf) == -1)
300 ret = 1;
301 }
34e02b83 302 } else {
303 for (i = 0; i < argc; i++) {
e0543e42 304 if (deleting) {
305 if (delete_file(ac, argv[i]) == -1)
306 ret = 1;
307 } else {
308 if (add_file(ac, argv[i]) == -1)
309 ret = 1;
310 }
34e02b83 311 }
8efc0c15 312 }
a8666d84 313 clear_pass();
34e02b83 314
315done:
5260325f 316 ssh_close_authentication_connection(ac);
e0543e42 317 return ret;
8efc0c15 318}
This page took 0.213992 seconds and 5 git commands to generate.