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