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