]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/bind9/bin/dnssec/dnssec-revoke.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / bind9 / bin / dnssec / dnssec-revoke.c
1 /*
2  * Copyright (C) 2009, 2010  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id: dnssec-revoke.c,v 1.22 2010-05-06 23:50:56 tbox Exp $ */
18
19 /*! \file */
20
21 #include <config.h>
22
23 #include <libgen.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include <isc/buffer.h>
28 #include <isc/commandline.h>
29 #include <isc/entropy.h>
30 #include <isc/file.h>
31 #include <isc/hash.h>
32 #include <isc/mem.h>
33 #include <isc/print.h>
34 #include <isc/string.h>
35 #include <isc/util.h>
36
37 #include <dns/keyvalues.h>
38 #include <dns/result.h>
39
40 #include <dst/dst.h>
41
42 #include "dnssectool.h"
43
44 const char *program = "dnssec-revoke";
45 int verbose;
46
47 static isc_mem_t        *mctx = NULL;
48
49 ISC_PLATFORM_NORETURN_PRE static void
50 usage(void) ISC_PLATFORM_NORETURN_POST;
51
52 static void
53 usage(void) {
54         fprintf(stderr, "Usage:\n");
55         fprintf(stderr, "    %s [options] keyfile\n\n", program);
56         fprintf(stderr, "Version: %s\n", VERSION);
57 #ifdef USE_PKCS11
58         fprintf(stderr, "    -E engine:    specify OpenSSL engine "
59                                            "(default \"pkcs11\")\n");
60 #else
61         fprintf(stderr, "    -E engine:    specify OpenSSL engine\n");
62 #endif
63         fprintf(stderr, "    -f:           force overwrite\n");
64         fprintf(stderr, "    -K directory: use directory for key files\n");
65         fprintf(stderr, "    -h:           help\n");
66         fprintf(stderr, "    -r:           remove old keyfiles after "
67                                            "creating revoked version\n");
68         fprintf(stderr, "    -v level:     set level of verbosity\n");
69         fprintf(stderr, "Output:\n");
70         fprintf(stderr, "     K<name>+<alg>+<new id>.key, "
71                              "K<name>+<alg>+<new id>.private\n");
72
73         exit (-1);
74 }
75
76 int
77 main(int argc, char **argv) {
78         isc_result_t result;
79 #ifdef USE_PKCS11
80         const char *engine = "pkcs11";
81 #else
82         const char *engine = NULL;
83 #endif
84         char *filename = NULL, *dir = NULL;
85         char newname[1024], oldname[1024];
86         char keystr[DST_KEY_FORMATSIZE];
87         char *endp;
88         int ch;
89         isc_entropy_t *ectx = NULL;
90         dst_key_t *key = NULL;
91         isc_uint32_t flags;
92         isc_buffer_t buf;
93         isc_boolean_t force = ISC_FALSE;
94         isc_boolean_t remove = ISC_FALSE;
95
96         if (argc == 1)
97                 usage();
98
99         result = isc_mem_create(0, 0, &mctx);
100         if (result != ISC_R_SUCCESS)
101                 fatal("Out of memory");
102
103         dns_result_register();
104
105         isc_commandline_errprint = ISC_FALSE;
106
107         while ((ch = isc_commandline_parse(argc, argv, "E:fK:rhv:")) != -1) {
108                 switch (ch) {
109                     case 'E':
110                         engine = isc_commandline_argument;
111                         break;
112                     case 'f':
113                         force = ISC_TRUE;
114                         break;
115                     case 'K':
116                         /*
117                          * We don't have to copy it here, but do it to
118                          * simplify cleanup later
119                          */
120                         dir = isc_mem_strdup(mctx, isc_commandline_argument);
121                         if (dir == NULL) {
122                                 fatal("Failed to allocate memory for "
123                                       "directory");
124                         }
125                         break;
126                     case 'r':
127                         remove = ISC_TRUE;
128                         break;
129                     case 'v':
130                         verbose = strtol(isc_commandline_argument, &endp, 0);
131                         if (*endp != '\0')
132                                 fatal("-v must be followed by a number");
133                         break;
134                     case '?':
135                         if (isc_commandline_option != '?')
136                                 fprintf(stderr, "%s: invalid argument -%c\n",
137                                         program, isc_commandline_option);
138                         /* Falls into */
139                     case 'h':
140                         usage();
141
142                     default:
143                         fprintf(stderr, "%s: unhandled option -%c\n",
144                                 program, isc_commandline_option);
145                         exit(1);
146                 }
147         }
148
149         if (argc < isc_commandline_index + 1 ||
150             argv[isc_commandline_index] == NULL)
151                 fatal("The key file name was not specified");
152         if (argc > isc_commandline_index + 1)
153                 fatal("Extraneous arguments");
154
155         if (dir != NULL) {
156                 filename = argv[isc_commandline_index];
157         } else {
158                 result = isc_file_splitpath(mctx, argv[isc_commandline_index],
159                                             &dir, &filename);
160                 if (result != ISC_R_SUCCESS)
161                         fatal("cannot process filename %s: %s",
162                               argv[isc_commandline_index],
163                               isc_result_totext(result));
164                 if (strcmp(dir, ".") == 0) {
165                         isc_mem_free(mctx, dir);
166                         dir = NULL;
167                 }
168         }
169
170         if (ectx == NULL)
171                 setup_entropy(mctx, NULL, &ectx);
172         result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
173         if (result != ISC_R_SUCCESS)
174                 fatal("Could not initialize hash");
175         result = dst_lib_init2(mctx, ectx, engine,
176                                ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
177         if (result != ISC_R_SUCCESS)
178                 fatal("Could not initialize dst: %s",
179                       isc_result_totext(result));
180         isc_entropy_stopcallbacksources(ectx);
181
182         result = dst_key_fromnamedfile(filename, dir,
183                                        DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
184                                        mctx, &key);
185         if (result != ISC_R_SUCCESS)
186                 fatal("Invalid keyfile name %s: %s",
187                       filename, isc_result_totext(result));
188
189         dst_key_format(key, keystr, sizeof(keystr));
190
191         if (verbose > 2)
192                 fprintf(stderr, "%s: %s\n", program, keystr);
193
194         if (force)
195                 set_keyversion(key);
196         else
197                 check_keyversion(key, keystr);
198
199
200         flags = dst_key_flags(key);
201         if ((flags & DNS_KEYFLAG_REVOKE) == 0) {
202                 isc_stdtime_t now;
203
204                 if ((flags & DNS_KEYFLAG_KSK) == 0)
205                         fprintf(stderr, "%s: warning: Key is not flagged "
206                                         "as a KSK. Revoking a ZSK is "
207                                         "legal, but undefined.\n",
208                                         program);
209
210                 isc_stdtime_get(&now);
211                 dst_key_settime(key, DST_TIME_REVOKE, now);
212
213                 dst_key_setflags(key, flags | DNS_KEYFLAG_REVOKE);
214
215                 isc_buffer_init(&buf, newname, sizeof(newname));
216                 dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf);
217
218                 if (access(newname, F_OK) == 0 && !force) {
219                         fatal("Key file %s already exists; "
220                               "use -f to force overwrite", newname);
221                 }
222
223                 result = dst_key_tofile(key, DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
224                                         dir);
225                 if (result != ISC_R_SUCCESS) {
226                         dst_key_format(key, keystr, sizeof(keystr));
227                         fatal("Failed to write key %s: %s", keystr,
228                               isc_result_totext(result));
229                 }
230
231                 isc_buffer_clear(&buf);
232                 dst_key_buildfilename(key, 0, dir, &buf);
233                 printf("%s\n", newname);
234
235                 /*
236                  * Remove old key file, if told to (and if
237                  * it isn't the same as the new file)
238                  */
239                 if (remove && dst_key_alg(key) != DST_ALG_RSAMD5) {
240                         isc_buffer_init(&buf, oldname, sizeof(oldname));
241                         dst_key_setflags(key, flags & ~DNS_KEYFLAG_REVOKE);
242                         dst_key_buildfilename(key, DST_TYPE_PRIVATE, dir, &buf);
243                         if (strcmp(oldname, newname) == 0)
244                                 goto cleanup;
245                         if (access(oldname, F_OK) == 0)
246                                 unlink(oldname);
247                         isc_buffer_clear(&buf);
248                         dst_key_buildfilename(key, DST_TYPE_PUBLIC, dir, &buf);
249                         if (access(oldname, F_OK) == 0)
250                                 unlink(oldname);
251                 }
252         } else {
253                 dst_key_format(key, keystr, sizeof(keystr));
254                 fatal("Key %s is already revoked", keystr);
255         }
256
257 cleanup:
258         dst_key_free(&key);
259         dst_lib_destroy();
260         isc_hash_destroy();
261         cleanup_entropy(&ectx);
262         if (verbose > 10)
263                 isc_mem_stats(mctx, stdout);
264         if (dir != NULL)
265                 isc_mem_free(mctx, dir);
266         isc_mem_destroy(&mctx);
267
268         return (0);
269 }