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