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