]> andersk Git - openssh.git/blame - ssh-add.c
- markus@cvs.openbsd.org 2003/06/16 08:22:35
[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"
ca719034 38RCSID("$OpenBSD: ssh-add.c,v 1.67 2003/06/11 11:18:38 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"
50#include "readpass.h"
c3baacd1 51#include "misc.h"
8efc0c15 52
f601d847 53#ifdef HAVE___PROGNAME
54extern char *__progname;
260d427b 55#else
56char *__progname;
57#endif
f601d847 58
33e766d2 59/* argv0 */
60extern char *__progname;
61
67656ffc 62/* Default files to add */
63static char *default_files[] = {
64 _PATH_SSH_CLIENT_ID_RSA,
65 _PATH_SSH_CLIENT_ID_DSA,
762715ce 66 _PATH_SSH_CLIENT_IDENTITY,
67656ffc 67 NULL
68};
69
264572cc 70/* Default lifetime (0 == forever) */
c3baacd1 71static int lifetime = 0;
67656ffc 72
c4087616 73/* User has to confirm key use */
74static int confirm = 0;
75
a8666d84 76/* we keep a cache of one passphrases */
77static char *pass = NULL;
396c147e 78static void
a8666d84 79clear_pass(void)
80{
81 if (pass) {
82 memset(pass, 0, strlen(pass));
83 xfree(pass);
84 pass = NULL;
85 }
86}
87
e0543e42 88static int
5068f573 89delete_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 90{
a306f2dd 91 Key *public;
d64050ef 92 char *comment = NULL;
e0543e42 93 int ret = -1;
5260325f 94
aeaa3d9e 95 public = key_load_public(filename, &comment);
96 if (public == NULL) {
97 printf("Bad key file %s\n", filename);
e0543e42 98 return -1;
5260325f 99 }
e0543e42 100 if (ssh_remove_identity(ac, public)) {
5260325f 101 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
e0543e42 102 ret = 0;
103 } else
5260325f 104 fprintf(stderr, "Could not remove identity: %s\n", filename);
e0543e42 105
a306f2dd 106 key_free(public);
5260325f 107 xfree(comment);
184eed6a 108
e0543e42 109 return ret;
8efc0c15 110}
111
2e73a022 112/* Send a request to remove all identities. */
e0543e42 113static int
5068f573 114delete_all(AuthenticationConnection *ac)
8efc0c15 115{
e0543e42 116 int ret = -1;
2e73a022 117
e0543e42 118 if (ssh_remove_all_identities(ac, 1))
119 ret = 0;
2e73a022 120 /* ignore error-code for ssh2 */
121 ssh_remove_all_identities(ac, 2);
122
e0543e42 123 if (ret == 0)
5260325f 124 fprintf(stderr, "All identities removed.\n");
125 else
b5c334cc 126 fprintf(stderr, "Failed to remove all identities.\n");
e0543e42 127
128 return ret;
8efc0c15 129}
130
e0543e42 131static int
5068f573 132add_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 133{
2e73a022 134 struct stat st;
a306f2dd 135 Key *private;
561e5254 136 char *comment = NULL;
137 char msg[1024];
e0543e42 138 int ret = -1;
5260325f 139
2e73a022 140 if (stat(filename, &st) < 0) {
141 perror(filename);
e0543e42 142 return -1;
2e73a022 143 }
5260325f 144 /* At first, try empty passphrase */
aeaa3d9e 145 private = key_load_private(filename, "", &comment);
146 if (comment == NULL)
147 comment = xstrdup(filename);
a8666d84 148 /* try last */
149 if (private == NULL && pass != NULL)
150 private = key_load_private(filename, pass, NULL);
aeaa3d9e 151 if (private == NULL) {
a8666d84 152 /* clear passphrase since it did not work */
153 clear_pass();
065604bb 154 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
561e5254 155 comment);
aa3378df 156 for (;;) {
1843a425 157 pass = read_passphrase(msg, RP_ALLOW_STDIN);
5260325f 158 if (strcmp(pass, "") == 0) {
eae942e2 159 clear_pass();
aeaa3d9e 160 xfree(comment);
e0543e42 161 return -1;
5260325f 162 }
aeaa3d9e 163 private = key_load_private(filename, pass, &comment);
aeaa3d9e 164 if (private != NULL)
5260325f 165 break;
a8666d84 166 clear_pass();
065604bb 167 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
5260325f 168 }
169 }
ee900f87 170
c4087616 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);
c4087616 178 if (confirm != 0)
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
260d427b 321 __progname = 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.289731 seconds and 5 git commands to generate.