Add Belarus locales (identical with FreeBSD's).
[dragonfly.git] / crypto / openssh-4 / ssh-add.c
1 /* $OpenBSD: ssh-add.c,v 1.89 2006/08/03 03:34:42 deraadt Exp $ */
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"
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/param.h>
43
44 #include <openssl/evp.h>
45
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"
55 #include "ssh.h"
56 #include "rsa.h"
57 #include "log.h"
58 #include "key.h"
59 #include "buffer.h"
60 #include "authfd.h"
61 #include "authfile.h"
62 #include "pathnames.h"
63 #include "misc.h"
64
65 /* argv0 */
66 extern char *__progname;
67
68 /* Default files to add */
69 static char *default_files[] = {
70         _PATH_SSH_CLIENT_ID_RSA,
71         _PATH_SSH_CLIENT_ID_DSA,
72         _PATH_SSH_CLIENT_IDENTITY,
73         NULL
74 };
75
76 /* Default lifetime (0 == forever) */
77 static int lifetime = 0;
78
79 /* User has to confirm key use */
80 static int confirm = 0;
81
82 /* we keep a cache of one passphrases */
83 static char *pass = NULL;
84 static void
85 clear_pass(void)
86 {
87         if (pass) {
88                 memset(pass, 0, strlen(pass));
89                 xfree(pass);
90                 pass = NULL;
91         }
92 }
93
94 static int
95 delete_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);
114
115         return ret;
116 }
117
118 /* Send a request to remove all identities. */
119 static int
120 delete_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
137 static int
138 add_file(AuthenticationConnection *ac, const char *filename)
139 {
140         Key *private;
141         char *comment = NULL;
142         char msg[1024];
143         int fd, perms_ok, ret = -1;
144
145         if ((fd = open(filename, O_RDONLY)) < 0) {
146                 perror(filename);
147                 return -1;
148         }
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
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: ",
170                     comment);
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();
182                         snprintf(msg, sizeof msg,
183                             "Bad passphrase, try again for %.200s: ", comment);
184                 }
185         }
186
187         if (ssh_add_identity_constrained(ac, private, comment, lifetime,
188             confirm)) {
189                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
190                 ret = 0;
191                 if (lifetime != 0)
192                         fprintf(stderr,
193                             "Lifetime set to %d seconds\n", lifetime);
194                 if (confirm != 0)
195                         fprintf(stderr,
196                             "The user has to confirm each use of the key\n");
197         } else if (ssh_add_identity(ac, private, comment)) {
198                 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
199                 ret = 0;
200         } else {
201                 fprintf(stderr, "Could not add identity: %s\n", filename);
202         }
203
204         xfree(comment);
205         key_free(private);
206
207         return ret;
208 }
209
210 static int
211 update_card(AuthenticationConnection *ac, int add, const char *id)
212 {
213         char *pin;
214         int ret = -1;
215
216         pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
217         if (pin == NULL)
218                 return -1;
219
220         if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
221                 fprintf(stderr, "Card %s: %s\n",
222                     add ? "added" : "removed", id);
223                 ret = 0;
224         } else {
225                 fprintf(stderr, "Could not %s card: %s\n",
226                     add ? "add" : "remove", id);
227                 ret = -1;
228         }
229         xfree(pin);
230         return ret;
231 }
232
233 static int
234 list_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);
243                     key != NULL;
244                     key = ssh_get_next_identity(ac, &comment, version)) {
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         }
261         if (!had_identities) {
262                 printf("The agent has no identities.\n");
263                 return -1;
264         }
265         return 0;
266 }
267
268 static int
269 lock_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);
293         return (ret);
294 }
295
296 static int
297 do_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;
307 }
308
309 static void
310 usage(void)
311 {
312         fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
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");
318         fprintf(stderr, "  -x          Lock agent.\n");
319         fprintf(stderr, "  -X          Unlock agent.\n");
320         fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
321         fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
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
328 int
329 main(int argc, char **argv)
330 {
331         extern char *optarg;
332         extern int optind;
333         AuthenticationConnection *ac = NULL;
334         char *sc_reader_id = NULL;
335         int i, ch, deleting = 0, ret = 0;
336
337         /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
338         sanitise_stdfd();
339
340         __progname = ssh_get_progname(argv[0]);
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) {
349                 fprintf(stderr,
350                     "Could not open a connection to your authentication agent.\n");
351                 exit(2);
352         }
353         while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
354                 switch (ch) {
355                 case 'l':
356                 case 'L':
357                         if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
358                                 ret = 1;
359                         goto done;
360                 case 'x':
361                 case 'X':
362                         if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
363                                 ret = 1;
364                         goto done;
365                 case 'c':
366                         confirm = 1;
367                         break;
368                 case 'd':
369                         deleting = 1;
370                         break;
371                 case 'D':
372                         if (delete_all(ac) == -1)
373                                 ret = 1;
374                         goto done;
375                 case 's':
376                         sc_reader_id = optarg;
377                         break;
378                 case 'e':
379                         deleting = 1;
380                         sc_reader_id = optarg;
381                         break;
382                 case 't':
383                         if ((lifetime = convtime(optarg)) == -1) {
384                                 fprintf(stderr, "Invalid lifetime\n");
385                                 ret = 1;
386                                 goto done;
387                         }
388                         break;
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) {
403                 char buf[MAXPATHLEN];
404                 struct passwd *pw;
405                 struct stat st;
406                 int count = 0;
407
408                 if ((pw = getpwuid(getuid())) == NULL) {
409                         fprintf(stderr, "No user found with uid %u\n",
410                             (u_int)getuid());
411                         ret = 1;
412                         goto done;
413                 }
414
415                 for (i = 0; default_files[i]; i++) {
416                         snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
417                             default_files[i]);
418                         if (stat(buf, &st) < 0)
419                                 continue;
420                         if (do_file(ac, deleting, buf) == -1)
421                                 ret = 1;
422                         else
423                                 count++;
424                 }
425                 if (count == 0)
426                         ret = 1;
427         } else {
428                 for (i = 0; i < argc; i++) {
429                         if (do_file(ac, deleting, argv[i]) == -1)
430                                 ret = 1;
431                 }
432         }
433         clear_pass();
434
435 done:
436         ssh_close_authentication_connection(ac);
437         return ret;
438 }