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