]> andersk Git - gssapi-openssh.git/blame - openssh/ssh-add.c
Import of OpenSSH 3.1p1
[gssapi-openssh.git] / openssh / ssh-add.c
CommitLineData
3c0ef626 1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Adds an identity to the authentication server, or removes an identity.
6 *
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 *
13 * SSH2 implementation,
14 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
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.
35 */
36
37#include "includes.h"
e9a17296 38RCSID("$OpenBSD: ssh-add.c,v 1.50 2002/01/29 14:27:57 markus Exp $");
3c0ef626 39
40#include <openssl/evp.h>
41
42#include "ssh.h"
43#include "rsa.h"
44#include "log.h"
45#include "xmalloc.h"
46#include "key.h"
47#include "authfd.h"
48#include "authfile.h"
49#include "pathnames.h"
50#include "readpass.h"
51
52#ifdef HAVE___PROGNAME
53extern char *__progname;
54#else
55char *__progname;
56#endif
57
58/* argv0 */
59extern char *__progname;
60
e9a17296 61/* Default files to add */
62static char *default_files[] = {
63 _PATH_SSH_CLIENT_ID_RSA,
64 _PATH_SSH_CLIENT_ID_DSA,
65 _PATH_SSH_CLIENT_IDENTITY,
66 NULL
67};
68
69
3c0ef626 70/* we keep a cache of one passphrases */
71static char *pass = NULL;
72static void
73clear_pass(void)
74{
75 if (pass) {
76 memset(pass, 0, strlen(pass));
77 xfree(pass);
78 pass = NULL;
79 }
80}
81
82static int
83delete_file(AuthenticationConnection *ac, const char *filename)
84{
85 Key *public;
86 char *comment = NULL;
87 int ret = -1;
88
89 public = key_load_public(filename, &comment);
90 if (public == NULL) {
91 printf("Bad key file %s\n", filename);
92 return -1;
93 }
94 if (ssh_remove_identity(ac, public)) {
95 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
96 ret = 0;
97 } else
98 fprintf(stderr, "Could not remove identity: %s\n", filename);
99
100 key_free(public);
101 xfree(comment);
e9a17296 102
3c0ef626 103 return ret;
104}
105
106/* Send a request to remove all identities. */
107static int
108delete_all(AuthenticationConnection *ac)
109{
110 int ret = -1;
111
112 if (ssh_remove_all_identities(ac, 1))
113 ret = 0;
114 /* ignore error-code for ssh2 */
115 ssh_remove_all_identities(ac, 2);
116
117 if (ret == 0)
118 fprintf(stderr, "All identities removed.\n");
119 else
120 fprintf(stderr, "Failed to remove all identities.\n");
121
122 return ret;
123}
124
125static int
126add_file(AuthenticationConnection *ac, const char *filename)
127{
128 struct stat st;
129 Key *private;
130 char *comment = NULL;
131 char msg[1024];
132 int ret = -1;
133
134 if (stat(filename, &st) < 0) {
135 perror(filename);
136 return -1;
137 }
138 /* At first, try empty passphrase */
139 private = key_load_private(filename, "", &comment);
140 if (comment == NULL)
141 comment = xstrdup(filename);
142 /* try last */
143 if (private == NULL && pass != NULL)
144 private = key_load_private(filename, pass, NULL);
145 if (private == NULL) {
146 /* clear passphrase since it did not work */
147 clear_pass();
148 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
149 comment);
150 for (;;) {
151 pass = read_passphrase(msg, RP_ALLOW_STDIN);
152 if (strcmp(pass, "") == 0) {
153 clear_pass();
154 xfree(comment);
155 return -1;
156 }
157 private = key_load_private(filename, pass, &comment);
158 if (private != NULL)
159 break;
160 clear_pass();
161 strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
162 }
163 }
164 if (ssh_add_identity(ac, private, comment)) {
165 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
166 ret = 0;
167 } else
168 fprintf(stderr, "Could not add identity: %s\n", filename);
169
170 xfree(comment);
171 key_free(private);
e9a17296 172
3c0ef626 173 return ret;
174}
175
176static int
177update_card(AuthenticationConnection *ac, int add, const char *id)
178{
179 if (ssh_update_card(ac, add, id)) {
180 fprintf(stderr, "Card %s: %s\n",
e9a17296 181 add ? "added" : "removed", id);
3c0ef626 182 return 0;
183 } else {
184 fprintf(stderr, "Could not %s card: %s\n",
e9a17296 185 add ? "add" : "remove", id);
3c0ef626 186 return -1;
187 }
188}
189
e9a17296 190static int
3c0ef626 191list_identities(AuthenticationConnection *ac, int do_fp)
192{
193 Key *key;
194 char *comment, *fp;
195 int had_identities = 0;
196 int version;
197
198 for (version = 1; version <= 2; version++) {
199 for (key = ssh_get_first_identity(ac, &comment, version);
e9a17296 200 key != NULL;
201 key = ssh_get_next_identity(ac, &comment, version)) {
3c0ef626 202 had_identities = 1;
203 if (do_fp) {
204 fp = key_fingerprint(key, SSH_FP_MD5,
205 SSH_FP_HEX);
206 printf("%d %s %s (%s)\n",
207 key_size(key), fp, comment, key_type(key));
208 xfree(fp);
209 } else {
210 if (!key_write(key, stdout))
211 fprintf(stderr, "key_write failed");
212 fprintf(stdout, " %s\n", comment);
213 }
214 key_free(key);
215 xfree(comment);
216 }
217 }
e9a17296 218 if (!had_identities) {
3c0ef626 219 printf("The agent has no identities.\n");
e9a17296 220 return -1;
221 }
222 return 0;
223}
224
225static int
226do_file(AuthenticationConnection *ac, int deleting, char *file)
227{
228 if (deleting) {
229 if (delete_file(ac, file) == -1)
230 return -1;
231 } else {
232 if (add_file(ac, file) == -1)
233 return -1;
234 }
235 return 0;
3c0ef626 236}
237
238static void
239usage(void)
240{
241 fprintf(stderr, "Usage: %s [options]\n", __progname);
242 fprintf(stderr, "Options:\n");
243 fprintf(stderr, " -l List fingerprints of all identities.\n");
244 fprintf(stderr, " -L List public key parameters of all identities.\n");
245 fprintf(stderr, " -d Delete identity.\n");
246 fprintf(stderr, " -D Delete all identities.\n");
247#ifdef SMARTCARD
248 fprintf(stderr, " -s reader Add key in smartcard reader.\n");
249 fprintf(stderr, " -e reader Remove key in smartcard reader.\n");
250#endif
251}
252
253int
254main(int argc, char **argv)
255{
256 extern char *optarg;
257 extern int optind;
258 AuthenticationConnection *ac = NULL;
3c0ef626 259 char *sc_reader_id = NULL;
260 int i, ch, deleting = 0, ret = 0;
261
262 __progname = get_progname(argv[0]);
263 init_rng();
264 seed_rng();
265
266 SSLeay_add_all_algorithms();
267
268 /* At first, get a connection to the authentication agent. */
269 ac = ssh_get_authentication_connection();
270 if (ac == NULL) {
271 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
e9a17296 272 exit(2);
3c0ef626 273 }
e9a17296 274 while ((ch = getopt(argc, argv, "lLdDe:s:")) != -1) {
3c0ef626 275 switch (ch) {
276 case 'l':
277 case 'L':
e9a17296 278 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
279 ret = 1;
3c0ef626 280 goto done;
281 break;
282 case 'd':
283 deleting = 1;
284 break;
285 case 'D':
286 if (delete_all(ac) == -1)
287 ret = 1;
288 goto done;
289 break;
290 case 's':
291 sc_reader_id = optarg;
292 break;
293 case 'e':
e9a17296 294 deleting = 1;
3c0ef626 295 sc_reader_id = optarg;
296 break;
297 default:
298 usage();
299 ret = 1;
300 goto done;
301 }
302 }
303 argc -= optind;
304 argv += optind;
305 if (sc_reader_id != NULL) {
306 if (update_card(ac, !deleting, sc_reader_id) == -1)
307 ret = 1;
308 goto done;
309 }
310 if (argc == 0) {
e9a17296 311 char buf[MAXPATHLEN];
312 struct passwd *pw;
313
314 if ((pw = getpwuid(getuid())) == NULL) {
3c0ef626 315 fprintf(stderr, "No user found with uid %u\n",
316 (u_int)getuid());
317 ret = 1;
318 goto done;
319 }
e9a17296 320
321 for(i = 0; default_files[i]; i++) {
322 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
323 default_files[i]);
324 if (do_file(ac, deleting, buf) == -1)
3c0ef626 325 ret = 1;
326 }
327 } else {
e9a17296 328 for(i = 0; i < argc; i++) {
329 if (do_file(ac, deleting, argv[i]) == -1)
330 ret = 1;
3c0ef626 331 }
332 }
333 clear_pass();
334
335done:
336 ssh_close_authentication_connection(ac);
337 return ret;
338}
This page took 0.096813 seconds and 5 git commands to generate.