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