]> andersk Git - gssapi-openssh.git/blame - openssh/ssh-add.c
Import of OpenSSH 4.4p1
[gssapi-openssh.git] / openssh / ssh-add.c
CommitLineData
9108f8d9 1/* $OpenBSD: ssh-add.c,v 1.89 2006/08/03 03:34:42 deraadt Exp $ */
3c0ef626 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 * Adds an identity to the authentication server, or removes an identity.
7 *
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 *
14 * SSH2 implementation,
15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
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.
36 */
37
38#include "includes.h"
9108f8d9 39
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <sys/param.h>
3c0ef626 43
44#include <openssl/evp.h>
45
9108f8d9 46#include <fcntl.h>
47#include <pwd.h>
48#include <stdarg.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#include "xmalloc.h"
3c0ef626 55#include "ssh.h"
56#include "rsa.h"
57#include "log.h"
3c0ef626 58#include "key.h"
9108f8d9 59#include "buffer.h"
3c0ef626 60#include "authfd.h"
61#include "authfile.h"
62#include "pathnames.h"
f5799ae1 63#include "misc.h"
3c0ef626 64
3c0ef626 65/* argv0 */
66extern char *__progname;
67
e9a17296 68/* Default files to add */
69static char *default_files[] = {
70 _PATH_SSH_CLIENT_ID_RSA,
71 _PATH_SSH_CLIENT_ID_DSA,
700318f3 72 _PATH_SSH_CLIENT_IDENTITY,
e9a17296 73 NULL
74};
75
f5799ae1 76/* Default lifetime (0 == forever) */
77static int lifetime = 0;
e9a17296 78
6a9b3198 79/* User has to confirm key use */
80static int confirm = 0;
81
3c0ef626 82/* we keep a cache of one passphrases */
83static char *pass = NULL;
84static void
85clear_pass(void)
86{
87 if (pass) {
88 memset(pass, 0, strlen(pass));
89 xfree(pass);
90 pass = NULL;
91 }
92}
93
94static int
95delete_file(AuthenticationConnection *ac, const char *filename)
96{
97 Key *public;
98 char *comment = NULL;
99 int ret = -1;
100
101 public = key_load_public(filename, &comment);
102 if (public == NULL) {
103 printf("Bad key file %s\n", filename);
104 return -1;
105 }
106 if (ssh_remove_identity(ac, public)) {
107 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
108 ret = 0;
109 } else
110 fprintf(stderr, "Could not remove identity: %s\n", filename);
111
112 key_free(public);
113 xfree(comment);
e9a17296 114
3c0ef626 115 return ret;
116}
117
118/* Send a request to remove all identities. */
119static int
120delete_all(AuthenticationConnection *ac)
121{
122 int ret = -1;
123
124 if (ssh_remove_all_identities(ac, 1))
125 ret = 0;
126 /* ignore error-code for ssh2 */
127 ssh_remove_all_identities(ac, 2);
128
129 if (ret == 0)
130 fprintf(stderr, "All identities removed.\n");
131 else
132 fprintf(stderr, "Failed to remove all identities.\n");
133
134 return ret;
135}
136
137static int
138add_file(AuthenticationConnection *ac, const char *filename)
139{
3c0ef626 140 Key *private;
141 char *comment = NULL;
142 char msg[1024];
9108f8d9 143 int fd, perms_ok, ret = -1;
3c0ef626 144
9108f8d9 145 if ((fd = open(filename, O_RDONLY)) < 0) {
3c0ef626 146 perror(filename);
147 return -1;
148 }
9108f8d9 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
3c0ef626 159 /* At first, try empty passphrase */
160 private = key_load_private(filename, "", &comment);
161 if (comment == NULL)
162 comment = xstrdup(filename);
163 /* try last */
164 if (private == NULL && pass != NULL)
165 private = key_load_private(filename, pass, NULL);
166 if (private == NULL) {
167 /* clear passphrase since it did not work */
168 clear_pass();
169 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
665a873d 170 comment);
3c0ef626 171 for (;;) {
172 pass = read_passphrase(msg, RP_ALLOW_STDIN);
173 if (strcmp(pass, "") == 0) {
174 clear_pass();
175 xfree(comment);
176 return -1;
177 }
178 private = key_load_private(filename, pass, &comment);
179 if (private != NULL)
180 break;
181 clear_pass();
0fff78ff 182 snprintf(msg, sizeof msg,
183 "Bad passphrase, try again for %.200s: ", comment);
3c0ef626 184 }
185 }
f5799ae1 186
cdd66111 187 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
188 confirm)) {
3c0ef626 189 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
190 ret = 0;
f5799ae1 191 if (lifetime != 0)
6a9b3198 192 fprintf(stderr,
f5799ae1 193 "Lifetime set to %d seconds\n", lifetime);
cdd66111 194 if (confirm != 0)
6a9b3198 195 fprintf(stderr,
196 "The user has to confirm each use of the key\n");
f5799ae1 197 } else if (ssh_add_identity(ac, private, comment)) {
198 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
199 ret = 0;
200 } else {
3c0ef626 201 fprintf(stderr, "Could not add identity: %s\n", filename);
f5799ae1 202 }
3c0ef626 203
204 xfree(comment);
205 key_free(private);
e9a17296 206
3c0ef626 207 return ret;
208}
209
210static int
211update_card(AuthenticationConnection *ac, int add, const char *id)
212{
700318f3 213 char *pin;
6a9b3198 214 int ret = -1;
700318f3 215
216 pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
217 if (pin == NULL)
218 return -1;
219
0fff78ff 220 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
3c0ef626 221 fprintf(stderr, "Card %s: %s\n",
e9a17296 222 add ? "added" : "removed", id);
6a9b3198 223 ret = 0;
3c0ef626 224 } else {
225 fprintf(stderr, "Could not %s card: %s\n",
e9a17296 226 add ? "add" : "remove", id);
6a9b3198 227 ret = -1;
3c0ef626 228 }
6a9b3198 229 xfree(pin);
230 return ret;
3c0ef626 231}
232
e9a17296 233static int
3c0ef626 234list_identities(AuthenticationConnection *ac, int do_fp)
235{
236 Key *key;
237 char *comment, *fp;
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);
e9a17296 243 key != NULL;
244 key = ssh_get_next_identity(ac, &comment, version)) {
3c0ef626 245 had_identities = 1;
246 if (do_fp) {
247 fp = key_fingerprint(key, SSH_FP_MD5,
248 SSH_FP_HEX);
249 printf("%d %s %s (%s)\n",
250 key_size(key), fp, comment, key_type(key));
251 xfree(fp);
252 } else {
253 if (!key_write(key, stdout))
254 fprintf(stderr, "key_write failed");
255 fprintf(stdout, " %s\n", comment);
256 }
257 key_free(key);
258 xfree(comment);
259 }
260 }
e9a17296 261 if (!had_identities) {
3c0ef626 262 printf("The agent has no identities.\n");
e9a17296 263 return -1;
264 }
265 return 0;
266}
267
f5799ae1 268static int
269lock_agent(AuthenticationConnection *ac, int lock)
270{
271 char prompt[100], *p1, *p2;
272 int passok = 1, ret = -1;
273
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);
41b2f314 293 return (ret);
f5799ae1 294}
295
e9a17296 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;
3c0ef626 307}
308
309static void
310usage(void)
311{
9108f8d9 312 fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
3c0ef626 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");
f5799ae1 318 fprintf(stderr, " -x Lock agent.\n");
41b2f314 319 fprintf(stderr, " -X Unlock agent.\n");
f5799ae1 320 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
6a9b3198 321 fprintf(stderr, " -c Require confirmation to sign using identities\n");
3c0ef626 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
326}
327
328int
329main(int argc, char **argv)
330{
331 extern char *optarg;
332 extern int optind;
333 AuthenticationConnection *ac = NULL;
3c0ef626 334 char *sc_reader_id = NULL;
335 int i, ch, deleting = 0, ret = 0;
336
2c06c99b 337 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
338 sanitise_stdfd();
339
0fff78ff 340 __progname = ssh_get_progname(argv[0]);
3c0ef626 341 init_rng();
342 seed_rng();
343
344 SSLeay_add_all_algorithms();
345
346 /* At first, get a connection to the authentication agent. */
347 ac = ssh_get_authentication_connection();
348 if (ac == NULL) {
2c06c99b 349 fprintf(stderr,
350 "Could not open a connection to your authentication agent.\n");
e9a17296 351 exit(2);
3c0ef626 352 }
6a9b3198 353 while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
3c0ef626 354 switch (ch) {
355 case 'l':
356 case 'L':
e9a17296 357 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
358 ret = 1;
3c0ef626 359 goto done;
f5799ae1 360 case 'x':
361 case 'X':
362 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
363 ret = 1;
364 goto done;
6a9b3198 365 case 'c':
366 confirm = 1;
367 break;
3c0ef626 368 case 'd':
369 deleting = 1;
370 break;
371 case 'D':
372 if (delete_all(ac) == -1)
373 ret = 1;
374 goto done;
3c0ef626 375 case 's':
376 sc_reader_id = optarg;
377 break;
378 case 'e':
e9a17296 379 deleting = 1;
3c0ef626 380 sc_reader_id = optarg;
381 break;
f5799ae1 382 case 't':
383 if ((lifetime = convtime(optarg)) == -1) {
384 fprintf(stderr, "Invalid lifetime\n");
385 ret = 1;
386 goto done;
387 }
388 break;
3c0ef626 389 default:
390 usage();
391 ret = 1;
392 goto done;
393 }
394 }
395 argc -= optind;
396 argv += optind;
397 if (sc_reader_id != NULL) {
398 if (update_card(ac, !deleting, sc_reader_id) == -1)
399 ret = 1;
400 goto done;
401 }
402 if (argc == 0) {
e9a17296 403 char buf[MAXPATHLEN];
404 struct passwd *pw;
700318f3 405 struct stat st;
406 int count = 0;
e9a17296 407
408 if ((pw = getpwuid(getuid())) == NULL) {
3c0ef626 409 fprintf(stderr, "No user found with uid %u\n",
410 (u_int)getuid());
411 ret = 1;
412 goto done;
413 }
e9a17296 414
dec6d9fe 415 for (i = 0; default_files[i]; i++) {
700318f3 416 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
e9a17296 417 default_files[i]);
700318f3 418 if (stat(buf, &st) < 0)
419 continue;
e9a17296 420 if (do_file(ac, deleting, buf) == -1)
3c0ef626 421 ret = 1;
700318f3 422 else
423 count++;
3c0ef626 424 }
700318f3 425 if (count == 0)
426 ret = 1;
3c0ef626 427 } else {
dec6d9fe 428 for (i = 0; i < argc; i++) {
e9a17296 429 if (do_file(ac, deleting, argv[i]) == -1)
430 ret = 1;
3c0ef626 431 }
432 }
433 clear_pass();
434
435done:
436 ssh_close_authentication_connection(ac);
437 return ret;
438}
This page took 0.117939 seconds and 5 git commands to generate.