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