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