]> andersk Git - openssh.git/blame - ssh-add.c
- (tim) [contrib/cygwin/README] add minires-devel requirement. Patch from
[openssh.git] / ssh-add.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Adds an identity to the authentication server, or removes an identity.
2e73a022 6 *
bcbf86ec 7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
2e73a022 13 * SSH2 implementation,
a96070d4 14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
bcbf86ec 15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 35 */
8efc0c15 36
37#include "includes.h"
58ae9cb8 38RCSID("$OpenBSD: ssh-add.c,v 1.70 2004/05/08 00:21:31 djm Exp $");
8efc0c15 39
2e73a022 40#include <openssl/evp.h>
a306f2dd 41
8efc0c15 42#include "ssh.h"
42f11eb2 43#include "rsa.h"
44#include "log.h"
8efc0c15 45#include "xmalloc.h"
a306f2dd 46#include "key.h"
4c8722d9 47#include "authfd.h"
a306f2dd 48#include "authfile.h"
42f11eb2 49#include "pathnames.h"
c3baacd1 50#include "misc.h"
8efc0c15 51
f601d847 52#ifdef HAVE___PROGNAME
53extern char *__progname;
260d427b 54#else
55char *__progname;
56#endif
f601d847 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{
2e73a022 133 struct stat st;
a306f2dd 134 Key *private;
561e5254 135 char *comment = NULL;
136 char msg[1024];
e0543e42 137 int ret = -1;
5260325f 138
2e73a022 139 if (stat(filename, &st) < 0) {
140 perror(filename);
e0543e42 141 return -1;
2e73a022 142 }
5260325f 143 /* At first, try empty passphrase */
aeaa3d9e 144 private = key_load_private(filename, "", &comment);
145 if (comment == NULL)
146 comment = xstrdup(filename);
a8666d84 147 /* try last */
148 if (private == NULL && pass != NULL)
149 private = key_load_private(filename, pass, NULL);
aeaa3d9e 150 if (private == NULL) {
a8666d84 151 /* clear passphrase since it did not work */
152 clear_pass();
065604bb 153 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
561e5254 154 comment);
aa3378df 155 for (;;) {
1843a425 156 pass = read_passphrase(msg, RP_ALLOW_STDIN);
5260325f 157 if (strcmp(pass, "") == 0) {
eae942e2 158 clear_pass();
aeaa3d9e 159 xfree(comment);
e0543e42 160 return -1;
5260325f 161 }
aeaa3d9e 162 private = key_load_private(filename, pass, &comment);
aeaa3d9e 163 if (private != NULL)
5260325f 164 break;
a8666d84 165 clear_pass();
360a4aae 166 snprintf(msg, sizeof msg,
167 "Bad passphrase, try again for %.200s: ", comment);
5260325f 168 }
169 }
ee900f87 170
aff51935 171 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
172 confirm)) {
aeaa3d9e 173 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
e0543e42 174 ret = 0;
ee900f87 175 if (lifetime != 0)
b77a87e5 176 fprintf(stderr,
ee900f87 177 "Lifetime set to %d seconds\n", lifetime);
aff51935 178 if (confirm != 0)
c4087616 179 fprintf(stderr,
180 "The user has to confirm each use of the key\n");
ee900f87 181 } else if (ssh_add_identity(ac, private, comment)) {
182 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
183 ret = 0;
184 } else {
5260325f 185 fprintf(stderr, "Could not add identity: %s\n", filename);
264572cc 186 }
187
aeaa3d9e 188 xfree(comment);
a306f2dd 189 key_free(private);
184eed6a 190
e0543e42 191 return ret;
8efc0c15 192}
193
e0543e42 194static int
9ff6f66f 195update_card(AuthenticationConnection *ac, int add, const char *id)
983def13 196{
76139bd8 197 char *pin;
3e2f2431 198 int ret = -1;
76139bd8 199
200 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
201 if (pin == NULL)
202 return -1;
203
ca719034 204 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
9ff6f66f 205 fprintf(stderr, "Card %s: %s\n",
184eed6a 206 add ? "added" : "removed", id);
3e2f2431 207 ret = 0;
e0543e42 208 } else {
9ff6f66f 209 fprintf(stderr, "Could not %s card: %s\n",
184eed6a 210 add ? "add" : "remove", id);
3e2f2431 211 ret = -1;
e0543e42 212 }
3e2f2431 213 xfree(pin);
214 return ret;
983def13 215}
216
a2863956 217static int
22138a36 218list_identities(AuthenticationConnection *ac, int do_fp)
8efc0c15 219{
2e73a022 220 Key *key;
22138a36 221 char *comment, *fp;
2e73a022 222 int had_identities = 0;
223 int version;
224
225 for (version = 1; version <= 2; version++) {
226 for (key = ssh_get_first_identity(ac, &comment, version);
184eed6a 227 key != NULL;
228 key = ssh_get_next_identity(ac, &comment, version)) {
2e73a022 229 had_identities = 1;
22138a36 230 if (do_fp) {
231 fp = key_fingerprint(key, SSH_FP_MD5,
232 SSH_FP_HEX);
fa08c86b 233 printf("%d %s %s (%s)\n",
22138a36 234 key_size(key), fp, comment, key_type(key));
235 xfree(fp);
5260325f 236 } else {
2e73a022 237 if (!key_write(key, stdout))
238 fprintf(stderr, "key_write failed");
239 fprintf(stdout, " %s\n", comment);
5260325f 240 }
2e73a022 241 key_free(key);
242 xfree(comment);
5260325f 243 }
f095fcc7 244 }
a2863956 245 if (!had_identities) {
5260325f 246 printf("The agent has no identities.\n");
a2863956 247 return -1;
248 }
249 return 0;
8efc0c15 250}
251
3db7f994 252static int
253lock_agent(AuthenticationConnection *ac, int lock)
254{
255 char prompt[100], *p1, *p2;
256 int passok = 1, ret = -1;
7203d6bb 257
3db7f994 258 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
259 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
260 if (lock) {
261 strlcpy(prompt, "Again: ", sizeof prompt);
262 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
263 if (strcmp(p1, p2) != 0) {
264 fprintf(stderr, "Passwords do not match.\n");
265 passok = 0;
266 }
267 memset(p2, 0, strlen(p2));
268 xfree(p2);
269 }
270 if (passok && ssh_lock_agent(ac, lock, p1)) {
271 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
272 ret = 0;
273 } else
274 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
275 memset(p1, 0, strlen(p1));
276 xfree(p1);
1431a900 277 return (ret);
3db7f994 278}
279
67656ffc 280static int
281do_file(AuthenticationConnection *ac, int deleting, char *file)
282{
283 if (deleting) {
284 if (delete_file(ac, file) == -1)
285 return -1;
286 } else {
287 if (add_file(ac, file) == -1)
288 return -1;
289 }
290 return 0;
291}
292
983def13 293static void
294usage(void)
295{
33e766d2 296 fprintf(stderr, "Usage: %s [options]\n", __progname);
297 fprintf(stderr, "Options:\n");
298 fprintf(stderr, " -l List fingerprints of all identities.\n");
299 fprintf(stderr, " -L List public key parameters of all identities.\n");
300 fprintf(stderr, " -d Delete identity.\n");
301 fprintf(stderr, " -D Delete all identities.\n");
73861c4e 302 fprintf(stderr, " -x Lock agent.\n");
c7724abb 303 fprintf(stderr, " -X Unlock agent.\n");
264572cc 304 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
c4087616 305 fprintf(stderr, " -c Require confirmation to sign using identities\n");
33e766d2 306#ifdef SMARTCARD
307 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
308 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
309#endif
983def13 310}
311
8efc0c15 312int
5068f573 313main(int argc, char **argv)
8efc0c15 314{
34e02b83 315 extern char *optarg;
316 extern int optind;
5260325f 317 AuthenticationConnection *ac = NULL;
9ff6f66f 318 char *sc_reader_id = NULL;
e0543e42 319 int i, ch, deleting = 0, ret = 0;
5260325f 320
fda04d7d 321 __progname = ssh_get_progname(argv[0]);
264dce47 322 init_rng();
044b0662 323 seed_rng();
264dce47 324
2b87da3b 325 SSLeay_add_all_algorithms();
2e73a022 326
5260325f 327 /* At first, get a connection to the authentication agent. */
328 ac = ssh_get_authentication_connection();
329 if (ac == NULL) {
330 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
a2863956 331 exit(2);
8efc0c15 332 }
c4087616 333 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
34e02b83 334 switch (ch) {
335 case 'l':
336 case 'L':
a2863956 337 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
338 ret = 1;
34e02b83 339 goto done;
340 break;
3db7f994 341 case 'x':
342 case 'X':
343 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
344 ret = 1;
345 goto done;
346 break;
c4087616 347 case 'c':
348 confirm = 1;
349 break;
34e02b83 350 case 'd':
5260325f 351 deleting = 1;
34e02b83 352 break;
353 case 'D':
e0543e42 354 if (delete_all(ac) == -1)
355 ret = 1;
34e02b83 356 goto done;
357 break;
358 case 's':
9ff6f66f 359 sc_reader_id = optarg;
34e02b83 360 break;
361 case 'e':
184eed6a 362 deleting = 1;
9ff6f66f 363 sc_reader_id = optarg;
34e02b83 364 break;
264572cc 365 case 't':
c3baacd1 366 if ((lifetime = convtime(optarg)) == -1) {
367 fprintf(stderr, "Invalid lifetime\n");
368 ret = 1;
369 goto done;
370 }
264572cc 371 break;
34e02b83 372 default:
373 usage();
e0543e42 374 ret = 1;
375 goto done;
983def13 376 }
8efc0c15 377 }
34e02b83 378 argc -= optind;
379 argv += optind;
9ff6f66f 380 if (sc_reader_id != NULL) {
e0543e42 381 if (update_card(ac, !deleting, sc_reader_id) == -1)
382 ret = 1;
34e02b83 383 goto done;
983def13 384 }
34e02b83 385 if (argc == 0) {
67656ffc 386 char buf[MAXPATHLEN];
387 struct passwd *pw;
256debd0 388 struct stat st;
389 int count = 0;
67656ffc 390
391 if ((pw = getpwuid(getuid())) == NULL) {
14a9a859 392 fprintf(stderr, "No user found with uid %u\n",
393 (u_int)getuid());
e0543e42 394 ret = 1;
395 goto done;
5260325f 396 }
67656ffc 397
398 for(i = 0; default_files[i]; i++) {
762715ce 399 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
67656ffc 400 default_files[i]);
256debd0 401 if (stat(buf, &st) < 0)
402 continue;
67656ffc 403 if (do_file(ac, deleting, buf) == -1)
e0543e42 404 ret = 1;
256debd0 405 else
406 count++;
e0543e42 407 }
256debd0 408 if (count == 0)
409 ret = 1;
34e02b83 410 } else {
67656ffc 411 for(i = 0; i < argc; i++) {
0e0bba68 412 if (do_file(ac, deleting, argv[i]) == -1)
67656ffc 413 ret = 1;
34e02b83 414 }
8efc0c15 415 }
a8666d84 416 clear_pass();
34e02b83 417
418done:
5260325f 419 ssh_close_authentication_connection(ac);
e0543e42 420 return ret;
8efc0c15 421}
This page took 0.274814 seconds and 5 git commands to generate.