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