]> andersk Git - openssh.git/blob - ssh-add.c
- markus@cvs.openbsd.org 2002/06/15 00:07:38
[openssh.git] / ssh-add.c
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"
38 RCSID("$OpenBSD: ssh-add.c,v 1.59 2002/06/15 00:07:38 markus 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 #include "misc.h"
52
53 #ifdef HAVE___PROGNAME
54 extern char *__progname;
55 #else
56 char *__progname;
57 #endif
58
59 /* argv0 */
60 extern char *__progname;
61
62 /* Default files to add */
63 static char *default_files[] = {
64         _PATH_SSH_CLIENT_ID_RSA,
65         _PATH_SSH_CLIENT_ID_DSA,
66         _PATH_SSH_CLIENT_IDENTITY,
67         NULL
68 };
69
70 /* Default lifetime (0 == forever) */
71 static int lifetime = 0;
72
73 /* we keep a cache of one passphrases */
74 static char *pass = NULL;
75 static void
76 clear_pass(void)
77 {
78         if (pass) {
79                 memset(pass, 0, strlen(pass));
80                 xfree(pass);
81                 pass = NULL;
82         }
83 }
84
85 static int
86 delete_file(AuthenticationConnection *ac, const char *filename)
87 {
88         Key *public;
89         char *comment = NULL;
90         int ret = -1;
91
92         public = key_load_public(filename, &comment);
93         if (public == NULL) {
94                 printf("Bad key file %s\n", filename);
95                 return -1;
96         }
97         if (ssh_remove_identity(ac, public)) {
98                 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
99                 ret = 0;
100         } else
101                 fprintf(stderr, "Could not remove identity: %s\n", filename);
102
103         key_free(public);
104         xfree(comment);
105
106         return ret;
107 }
108
109 /* Send a request to remove all identities. */
110 static int
111 delete_all(AuthenticationConnection *ac)
112 {
113         int ret = -1;
114
115         if (ssh_remove_all_identities(ac, 1))
116                 ret = 0;
117         /* ignore error-code for ssh2 */
118         ssh_remove_all_identities(ac, 2);
119
120         if (ret == 0)
121                 fprintf(stderr, "All identities removed.\n");
122         else
123                 fprintf(stderr, "Failed to remove all identities.\n");
124
125         return ret;
126 }
127
128 static int
129 add_file(AuthenticationConnection *ac, const char *filename)
130 {
131         struct stat st;
132         Key *private;
133         char *comment = NULL;
134         char msg[1024];
135         int ret = -1;
136
137         if (stat(filename, &st) < 0) {
138                 perror(filename);
139                 return -1;
140         }
141         /* At first, try empty passphrase */
142         private = key_load_private(filename, "", &comment);
143         if (comment == NULL)
144                 comment = xstrdup(filename);
145         /* try last */
146         if (private == NULL && pass != NULL)
147                 private = key_load_private(filename, pass, NULL);
148         if (private == NULL) {
149                 /* clear passphrase since it did not work */
150                 clear_pass();
151                 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
152                    comment);
153                 for (;;) {
154                         pass = read_passphrase(msg, RP_ALLOW_STDIN);
155                         if (strcmp(pass, "") == 0) {
156                                 clear_pass();
157                                 xfree(comment);
158                                 return -1;
159                         }
160                         private = key_load_private(filename, pass, &comment);
161                         if (private != NULL)
162                                 break;
163                         clear_pass();
164                         strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
165                 }
166         }
167         if (ssh_add_identity(ac, private, comment)) {
168                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
169                 ret = 0;
170         } else
171                 fprintf(stderr, "Could not add identity: %s\n", filename);
172
173         if (ret == 0 && lifetime != 0) {
174                 if (ssh_constrain_identity(ac, private, lifetime)) {
175                         fprintf(stderr,
176                             "Lifetime set to %d seconds for: %s (%s)\n",
177                             lifetime, filename, comment);
178                 } else {
179                         fprintf(stderr,
180                             "Could not set lifetime for identity: %s\n",
181                             filename);
182                 }
183         }
184
185         xfree(comment);
186         key_free(private);
187
188         return ret;
189 }
190
191 static int
192 update_card(AuthenticationConnection *ac, int add, const char *id)
193 {
194         char *pin;
195
196         pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
197         if (pin == NULL)
198                 return -1;
199
200         if (ssh_update_card(ac, add, id, pin)) {
201                 fprintf(stderr, "Card %s: %s\n",
202                     add ? "added" : "removed", id);
203                 return 0;
204         } else {
205                 fprintf(stderr, "Could not %s card: %s\n",
206                     add ? "add" : "remove", id);
207                 return -1;
208         }
209 }
210
211 static int
212 list_identities(AuthenticationConnection *ac, int do_fp)
213 {
214         Key *key;
215         char *comment, *fp;
216         int had_identities = 0;
217         int version;
218
219         for (version = 1; version <= 2; version++) {
220                 for (key = ssh_get_first_identity(ac, &comment, version);
221                     key != NULL;
222                     key = ssh_get_next_identity(ac, &comment, version)) {
223                         had_identities = 1;
224                         if (do_fp) {
225                                 fp = key_fingerprint(key, SSH_FP_MD5,
226                                     SSH_FP_HEX);
227                                 printf("%d %s %s (%s)\n",
228                                     key_size(key), fp, comment, key_type(key));
229                                 xfree(fp);
230                         } else {
231                                 if (!key_write(key, stdout))
232                                         fprintf(stderr, "key_write failed");
233                                 fprintf(stdout, " %s\n", comment);
234                         }
235                         key_free(key);
236                         xfree(comment);
237                 }
238         }
239         if (!had_identities) {
240                 printf("The agent has no identities.\n");
241                 return -1;
242         }
243         return 0;
244 }
245
246 static int
247 lock_agent(AuthenticationConnection *ac, int lock)
248 {
249         char prompt[100], *p1, *p2;
250         int passok = 1, ret = -1;
251  
252         strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
253         p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
254         if (lock) {
255                 strlcpy(prompt, "Again: ", sizeof prompt);
256                 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
257                 if (strcmp(p1, p2) != 0) {
258                         fprintf(stderr, "Passwords do not match.\n");
259                         passok = 0;
260                 }
261                 memset(p2, 0, strlen(p2));
262                 xfree(p2);
263         }
264         if (passok && ssh_lock_agent(ac, lock, p1)) {
265                 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
266                 ret = 0;
267         } else
268                 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
269         memset(p1, 0, strlen(p1));
270         xfree(p1);
271         return -1;
272 }
273
274 static int
275 do_file(AuthenticationConnection *ac, int deleting, char *file)
276 {
277         if (deleting) {
278                 if (delete_file(ac, file) == -1)
279                         return -1;
280         } else {
281                 if (add_file(ac, file) == -1)
282                         return -1;
283         }
284         return 0;
285 }
286
287 static void
288 usage(void)
289 {
290         fprintf(stderr, "Usage: %s [options]\n", __progname);
291         fprintf(stderr, "Options:\n");
292         fprintf(stderr, "  -l          List fingerprints of all identities.\n");
293         fprintf(stderr, "  -L          List public key parameters of all identities.\n");
294         fprintf(stderr, "  -d          Delete identity.\n");
295         fprintf(stderr, "  -D          Delete all identities.\n");
296         fprintf(stderr, "  -x          Lock agent.\n");
297         fprintf(stderr, "  -x          Unlock agent.\n");
298         fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
299 #ifdef SMARTCARD
300         fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
301         fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
302 #endif
303 }
304
305 int
306 main(int argc, char **argv)
307 {
308         extern char *optarg;
309         extern int optind;
310         AuthenticationConnection *ac = NULL;
311         char *sc_reader_id = NULL;
312         int i, ch, deleting = 0, ret = 0;
313
314         __progname = get_progname(argv[0]);
315         init_rng();
316         seed_rng();
317
318         SSLeay_add_all_algorithms();
319
320         /* At first, get a connection to the authentication agent. */
321         ac = ssh_get_authentication_connection();
322         if (ac == NULL) {
323                 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
324                 exit(2);
325         }
326         while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
327                 switch (ch) {
328                 case 'l':
329                 case 'L':
330                         if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
331                                 ret = 1;
332                         goto done;
333                         break;
334                 case 'x':
335                 case 'X':
336                         if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
337                                 ret = 1;
338                         goto done;
339                         break;
340                 case 'd':
341                         deleting = 1;
342                         break;
343                 case 'D':
344                         if (delete_all(ac) == -1)
345                                 ret = 1;
346                         goto done;
347                         break;
348                 case 's':
349                         sc_reader_id = optarg;
350                         break;
351                 case 'e':
352                         deleting = 1;
353                         sc_reader_id = optarg;
354                         break;
355                 case 't':
356                         if ((lifetime = convtime(optarg)) == -1) {
357                                 fprintf(stderr, "Invalid lifetime\n");
358                                 ret = 1;
359                                 goto done;
360                         }
361                         break;
362                 default:
363                         usage();
364                         ret = 1;
365                         goto done;
366                 }
367         }
368         argc -= optind;
369         argv += optind;
370         if (sc_reader_id != NULL) {
371                 if (update_card(ac, !deleting, sc_reader_id) == -1)
372                         ret = 1;
373                 goto done;
374         }
375         if (argc == 0) {
376                 char buf[MAXPATHLEN];
377                 struct passwd *pw;
378                 struct stat st;
379                 int count = 0;
380
381                 if ((pw = getpwuid(getuid())) == NULL) {
382                         fprintf(stderr, "No user found with uid %u\n",
383                             (u_int)getuid());
384                         ret = 1;
385                         goto done;
386                 }
387
388                 for(i = 0; default_files[i]; i++) {
389                         snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
390                             default_files[i]);
391                         if (stat(buf, &st) < 0)
392                                 continue;
393                         if (do_file(ac, deleting, buf) == -1)
394                                 ret = 1;
395                         else
396                                 count++;
397                 }
398                 if (count == 0)
399                         ret = 1;
400         } else {
401                 for(i = 0; i < argc; i++) {
402                         if (do_file(ac, deleting, argv[i]) == -1)
403                                 ret = 1;
404                 }
405         }
406         clear_pass();
407
408 done:
409         ssh_close_authentication_connection(ac);
410         return ret;
411 }
This page took 0.070128 seconds and 5 git commands to generate.