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