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