]> andersk Git - openssh.git/blame - ssh-add.c
- stevesk@cvs.openbsd.org 2002/06/10 16:56:30
[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"
264572cc 38RCSID("$OpenBSD: ssh-add.c,v 1.56 2002/06/05 21:55:44 markus 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
67656ffc 61/* Default files to add */
62static char *default_files[] = {
63 _PATH_SSH_CLIENT_ID_RSA,
64 _PATH_SSH_CLIENT_ID_DSA,
762715ce 65 _PATH_SSH_CLIENT_IDENTITY,
67656ffc 66 NULL
67};
68
264572cc 69/* Default lifetime (0 == forever) */
70static u_int lifetime = 0;
67656ffc 71
a8666d84 72/* we keep a cache of one passphrases */
73static char *pass = NULL;
396c147e 74static void
a8666d84 75clear_pass(void)
76{
77 if (pass) {
78 memset(pass, 0, strlen(pass));
79 xfree(pass);
80 pass = NULL;
81 }
82}
83
e0543e42 84static int
5068f573 85delete_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 86{
a306f2dd 87 Key *public;
d64050ef 88 char *comment = NULL;
e0543e42 89 int ret = -1;
5260325f 90
aeaa3d9e 91 public = key_load_public(filename, &comment);
92 if (public == NULL) {
93 printf("Bad key file %s\n", filename);
e0543e42 94 return -1;
5260325f 95 }
e0543e42 96 if (ssh_remove_identity(ac, public)) {
5260325f 97 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
e0543e42 98 ret = 0;
99 } else
5260325f 100 fprintf(stderr, "Could not remove identity: %s\n", filename);
e0543e42 101
a306f2dd 102 key_free(public);
5260325f 103 xfree(comment);
184eed6a 104
e0543e42 105 return ret;
8efc0c15 106}
107
2e73a022 108/* Send a request to remove all identities. */
e0543e42 109static int
5068f573 110delete_all(AuthenticationConnection *ac)
8efc0c15 111{
e0543e42 112 int ret = -1;
2e73a022 113
e0543e42 114 if (ssh_remove_all_identities(ac, 1))
115 ret = 0;
2e73a022 116 /* ignore error-code for ssh2 */
117 ssh_remove_all_identities(ac, 2);
118
e0543e42 119 if (ret == 0)
5260325f 120 fprintf(stderr, "All identities removed.\n");
121 else
b5c334cc 122 fprintf(stderr, "Failed to remove all identities.\n");
e0543e42 123
124 return ret;
8efc0c15 125}
126
e0543e42 127static int
5068f573 128add_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 129{
2e73a022 130 struct stat st;
a306f2dd 131 Key *private;
561e5254 132 char *comment = NULL;
133 char msg[1024];
e0543e42 134 int ret = -1;
5260325f 135
2e73a022 136 if (stat(filename, &st) < 0) {
137 perror(filename);
e0543e42 138 return -1;
2e73a022 139 }
5260325f 140 /* At first, try empty passphrase */
aeaa3d9e 141 private = key_load_private(filename, "", &comment);
142 if (comment == NULL)
143 comment = xstrdup(filename);
a8666d84 144 /* try last */
145 if (private == NULL && pass != NULL)
146 private = key_load_private(filename, pass, NULL);
aeaa3d9e 147 if (private == NULL) {
a8666d84 148 /* clear passphrase since it did not work */
149 clear_pass();
065604bb 150 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
561e5254 151 comment);
aa3378df 152 for (;;) {
1843a425 153 pass = read_passphrase(msg, RP_ALLOW_STDIN);
5260325f 154 if (strcmp(pass, "") == 0) {
eae942e2 155 clear_pass();
aeaa3d9e 156 xfree(comment);
e0543e42 157 return -1;
5260325f 158 }
aeaa3d9e 159 private = key_load_private(filename, pass, &comment);
aeaa3d9e 160 if (private != NULL)
5260325f 161 break;
a8666d84 162 clear_pass();
065604bb 163 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
5260325f 164 }
165 }
e0543e42 166 if (ssh_add_identity(ac, private, comment)) {
aeaa3d9e 167 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
e0543e42 168 ret = 0;
169 } else
5260325f 170 fprintf(stderr, "Could not add identity: %s\n", filename);
e0543e42 171
264572cc 172 if (ret == 0 && lifetime != 0) {
173 if (ssh_lifetime_identity(ac, private, lifetime)) {
174 fprintf(stderr,
175 "Lifetime set to %d seconds for: %s (%s)\n",
176 lifetime, filename, comment);
177 } else {
178 fprintf(stderr,
179 "Could not set lifetime for identity: %s\n",
180 filename);
181 }
182 }
183
aeaa3d9e 184 xfree(comment);
a306f2dd 185 key_free(private);
184eed6a 186
e0543e42 187 return ret;
8efc0c15 188}
189
e0543e42 190static int
9ff6f66f 191update_card(AuthenticationConnection *ac, int add, const char *id)
983def13 192{
76139bd8 193 char *pin;
194
195 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
196 if (pin == NULL)
197 return -1;
198
199 if (ssh_update_card(ac, add, id, pin)) {
9ff6f66f 200 fprintf(stderr, "Card %s: %s\n",
184eed6a 201 add ? "added" : "removed", id);
e0543e42 202 return 0;
203 } else {
9ff6f66f 204 fprintf(stderr, "Could not %s card: %s\n",
184eed6a 205 add ? "add" : "remove", id);
e0543e42 206 return -1;
207 }
983def13 208}
209
a2863956 210static int
22138a36 211list_identities(AuthenticationConnection *ac, int do_fp)
8efc0c15 212{
2e73a022 213 Key *key;
22138a36 214 char *comment, *fp;
2e73a022 215 int had_identities = 0;
216 int version;
217
218 for (version = 1; version <= 2; version++) {
219 for (key = ssh_get_first_identity(ac, &comment, version);
184eed6a 220 key != NULL;
221 key = ssh_get_next_identity(ac, &comment, version)) {
2e73a022 222 had_identities = 1;
22138a36 223 if (do_fp) {
224 fp = key_fingerprint(key, SSH_FP_MD5,
225 SSH_FP_HEX);
fa08c86b 226 printf("%d %s %s (%s)\n",
22138a36 227 key_size(key), fp, comment, key_type(key));
228 xfree(fp);
5260325f 229 } else {
2e73a022 230 if (!key_write(key, stdout))
231 fprintf(stderr, "key_write failed");
232 fprintf(stdout, " %s\n", comment);
5260325f 233 }
2e73a022 234 key_free(key);
235 xfree(comment);
5260325f 236 }
f095fcc7 237 }
a2863956 238 if (!had_identities) {
5260325f 239 printf("The agent has no identities.\n");
a2863956 240 return -1;
241 }
242 return 0;
8efc0c15 243}
244
3db7f994 245static int
246lock_agent(AuthenticationConnection *ac, int lock)
247{
248 char prompt[100], *p1, *p2;
249 int passok = 1, ret = -1;
250
251 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
252 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
253 if (lock) {
254 strlcpy(prompt, "Again: ", sizeof prompt);
255 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
256 if (strcmp(p1, p2) != 0) {
257 fprintf(stderr, "Passwords do not match.\n");
258 passok = 0;
259 }
260 memset(p2, 0, strlen(p2));
261 xfree(p2);
262 }
263 if (passok && ssh_lock_agent(ac, lock, p1)) {
264 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
265 ret = 0;
266 } else
267 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
268 memset(p1, 0, strlen(p1));
269 xfree(p1);
270 return -1;
271}
272
67656ffc 273static int
274do_file(AuthenticationConnection *ac, int deleting, char *file)
275{
276 if (deleting) {
277 if (delete_file(ac, file) == -1)
278 return -1;
279 } else {
280 if (add_file(ac, file) == -1)
281 return -1;
282 }
283 return 0;
284}
285
983def13 286static void
287usage(void)
288{
33e766d2 289 fprintf(stderr, "Usage: %s [options]\n", __progname);
290 fprintf(stderr, "Options:\n");
291 fprintf(stderr, " -l List fingerprints of all identities.\n");
292 fprintf(stderr, " -L List public key parameters of all identities.\n");
293 fprintf(stderr, " -d Delete identity.\n");
294 fprintf(stderr, " -D Delete all identities.\n");
73861c4e 295 fprintf(stderr, " -x Lock agent.\n");
296 fprintf(stderr, " -x Unlock agent.\n");
264572cc 297 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
33e766d2 298#ifdef SMARTCARD
299 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
300 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
301#endif
983def13 302}
303
8efc0c15 304int
5068f573 305main(int argc, char **argv)
8efc0c15 306{
34e02b83 307 extern char *optarg;
308 extern int optind;
5260325f 309 AuthenticationConnection *ac = NULL;
9ff6f66f 310 char *sc_reader_id = NULL;
e0543e42 311 int i, ch, deleting = 0, ret = 0;
5260325f 312
260d427b 313 __progname = get_progname(argv[0]);
264dce47 314 init_rng();
044b0662 315 seed_rng();
264dce47 316
2b87da3b 317 SSLeay_add_all_algorithms();
2e73a022 318
5260325f 319 /* At first, get a connection to the authentication agent. */
320 ac = ssh_get_authentication_connection();
321 if (ac == NULL) {
322 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
a2863956 323 exit(2);
8efc0c15 324 }
264572cc 325 while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
34e02b83 326 switch (ch) {
327 case 'l':
328 case 'L':
a2863956 329 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
330 ret = 1;
34e02b83 331 goto done;
332 break;
3db7f994 333 case 'x':
334 case 'X':
335 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
336 ret = 1;
337 goto done;
338 break;
34e02b83 339 case 'd':
5260325f 340 deleting = 1;
34e02b83 341 break;
342 case 'D':
e0543e42 343 if (delete_all(ac) == -1)
344 ret = 1;
34e02b83 345 goto done;
346 break;
347 case 's':
9ff6f66f 348 sc_reader_id = optarg;
34e02b83 349 break;
350 case 'e':
184eed6a 351 deleting = 1;
9ff6f66f 352 sc_reader_id = optarg;
34e02b83 353 break;
264572cc 354 case 't':
355 lifetime = atoi(optarg);
356 break;
34e02b83 357 default:
358 usage();
e0543e42 359 ret = 1;
360 goto done;
983def13 361 }
8efc0c15 362 }
34e02b83 363 argc -= optind;
364 argv += optind;
9ff6f66f 365 if (sc_reader_id != NULL) {
e0543e42 366 if (update_card(ac, !deleting, sc_reader_id) == -1)
367 ret = 1;
34e02b83 368 goto done;
983def13 369 }
34e02b83 370 if (argc == 0) {
67656ffc 371 char buf[MAXPATHLEN];
372 struct passwd *pw;
256debd0 373 struct stat st;
374 int count = 0;
67656ffc 375
376 if ((pw = getpwuid(getuid())) == NULL) {
14a9a859 377 fprintf(stderr, "No user found with uid %u\n",
378 (u_int)getuid());
e0543e42 379 ret = 1;
380 goto done;
5260325f 381 }
67656ffc 382
383 for(i = 0; default_files[i]; i++) {
762715ce 384 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
67656ffc 385 default_files[i]);
256debd0 386 if (stat(buf, &st) < 0)
387 continue;
67656ffc 388 if (do_file(ac, deleting, buf) == -1)
e0543e42 389 ret = 1;
256debd0 390 else
391 count++;
e0543e42 392 }
256debd0 393 if (count == 0)
394 ret = 1;
34e02b83 395 } else {
67656ffc 396 for(i = 0; i < argc; i++) {
0e0bba68 397 if (do_file(ac, deleting, argv[i]) == -1)
67656ffc 398 ret = 1;
34e02b83 399 }
8efc0c15 400 }
a8666d84 401 clear_pass();
34e02b83 402
403done:
5260325f 404 ssh_close_authentication_connection(ac);
e0543e42 405 return ret;
8efc0c15 406}
This page took 0.469837 seconds and 5 git commands to generate.