Update repub branch u/fanf2/patch to rebasing branch u/fanf2/rebasing revision v9_15_...
[ipreg/bind9.git] / bin / dnssec / dnssec-verify.c
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*! \file */
13
14 #include <stdbool.h>
15 #include <stdlib.h>
16 #include <time.h>
17
18 #include <isc/app.h>
19 #include <isc/base32.h>
20 #include <isc/commandline.h>
21 #include <isc/event.h>
22 #include <isc/file.h>
23 #include <isc/hash.h>
24 #include <isc/hex.h>
25 #include <isc/mem.h>
26 #include <isc/mutex.h>
27 #include <isc/os.h>
28 #include <isc/print.h>
29 #include <isc/random.h>
30 #include <isc/rwlock.h>
31 #include <isc/serial.h>
32 #include <isc/stdio.h>
33 #include <isc/string.h>
34 #include <isc/time.h>
35 #include <isc/util.h>
36
37 #include <dns/db.h>
38 #include <dns/dbiterator.h>
39 #include <dns/diff.h>
40 #include <dns/dnssec.h>
41 #include <dns/ds.h>
42 #include <dns/fixedname.h>
43 #include <dns/keyvalues.h>
44 #include <dns/log.h>
45 #include <dns/master.h>
46 #include <dns/masterdump.h>
47 #include <dns/nsec.h>
48 #include <dns/nsec3.h>
49 #include <dns/rdata.h>
50 #include <dns/rdatalist.h>
51 #include <dns/rdataset.h>
52 #include <dns/rdataclass.h>
53 #include <dns/rdatasetiter.h>
54 #include <dns/rdatastruct.h>
55 #include <dns/rdatatype.h>
56 #include <dns/result.h>
57 #include <dns/soa.h>
58 #include <dns/time.h>
59 #include <dns/zoneverify.h>
60
61 #include <dst/dst.h>
62
63 #if USE_PKCS11
64 #include <pk11/result.h>
65 #endif
66
67 #include "dnssectool.h"
68
69 const char *program = "dnssec-verify";
70
71 static isc_stdtime_t now;
72 static isc_mem_t *mctx = NULL;
73 static dns_masterformat_t inputformat = dns_masterformat_text;
74 static dns_db_t *gdb; /* The database */
75 static dns_dbversion_t *gversion; /* The database version */
76 static dns_rdataclass_t gclass; /* The class */
77 static dns_name_t *gorigin; /* The database origin */
78 static bool ignore_kskflag = false;
79 static bool keyset_kskonly = false;
80
81 /*%
82 * Load the zone file from disk
83 */
84 static void
85 loadzone(char *file, char *origin, dns_rdataclass_t rdclass, dns_db_t **db) {
86 isc_buffer_t b;
87 int len;
88 dns_fixedname_t fname;
89 dns_name_t *name;
90 isc_result_t result;
91
92 len = strlen(origin);
93 isc_buffer_init(&b, origin, len);
94 isc_buffer_add(&b, len);
95
96 name = dns_fixedname_initname(&fname);
97 result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
98 if (result != ISC_R_SUCCESS)
99 fatal("failed converting name '%s' to dns format: %s",
100 origin, isc_result_totext(result));
101
102 result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
103 rdclass, 0, NULL, db);
104 check_result(result, "dns_db_create()");
105
106 result = dns_db_load(*db, file, inputformat, 0);
107 switch (result) {
108 case DNS_R_SEENINCLUDE:
109 case ISC_R_SUCCESS:
110 break;
111 case DNS_R_NOTZONETOP:
112 /*
113 * Comparing pointers (vs. using strcmp()) is intentional: we
114 * want to check whether -o was supplied on the command line,
115 * not whether origin and file contain the same string.
116 */
117 if (origin == file) {
118 fatal("failed loading zone '%s' from file '%s': "
119 "use -o to specify a different zone origin",
120 origin, file);
121 }
122 /* FALLTHROUGH */
123 default:
124 fatal("failed loading zone from '%s': %s",
125 file, isc_result_totext(result));
126 }
127 }
128
129 ISC_PLATFORM_NORETURN_PRE static void
130 usage(void) ISC_PLATFORM_NORETURN_POST;
131
132 static void
133 usage(void) {
134 fprintf(stderr, "Usage:\n");
135 fprintf(stderr, "\t%s [options] zonefile [keys]\n", program);
136
137 fprintf(stderr, "\n");
138
139 fprintf(stderr, "Version: %s\n", VERSION);
140
141 fprintf(stderr, "Options: (default value in parenthesis) \n");
142 fprintf(stderr, "\t-v debuglevel (0)\n");
143 fprintf(stderr, "\t-V:\tprint version information\n");
144 fprintf(stderr, "\t-o origin:\n");
145 fprintf(stderr, "\t\tzone origin (name of zonefile)\n");
146 fprintf(stderr, "\t-I format:\n");
147 fprintf(stderr, "\t\tfile format of input zonefile (text)\n");
148 fprintf(stderr, "\t-c class (IN)\n");
149 fprintf(stderr, "\t-E engine:\n");
150 #if USE_PKCS11
151 fprintf(stderr, "\t\tpath to PKCS#11 provider library "
152 "(default is %s)\n", PK11_LIB_LOCATION);
153 #else
154 fprintf(stderr, "\t\tname of an OpenSSL engine to use\n");
155 #endif
156 fprintf(stderr, "\t-x:\tDNSKEY record signed with KSKs only, "
157 "not ZSKs\n");
158 fprintf(stderr, "\t-z:\tAll records signed with KSKs\n");
159 exit(0);
160 }
161
162 int
163 main(int argc, char *argv[]) {
164 char *origin = NULL, *file = NULL;
165 char *inputformatstr = NULL;
166 isc_result_t result;
167 isc_log_t *log = NULL;
168 const char *engine = NULL;
169 char *classname = NULL;
170 dns_rdataclass_t rdclass;
171 char *endp;
172 int ch;
173
174 #define CMDLINE_FLAGS \
175 "hm:o:I:c:E:v:Vxz"
176
177 /*
178 * Process memory debugging argument first.
179 */
180 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
181 switch (ch) {
182 case 'm':
183 if (strcasecmp(isc_commandline_argument, "record") == 0)
184 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
185 if (strcasecmp(isc_commandline_argument, "trace") == 0)
186 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
187 if (strcasecmp(isc_commandline_argument, "usage") == 0)
188 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
189 if (strcasecmp(isc_commandline_argument, "size") == 0)
190 isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
191 if (strcasecmp(isc_commandline_argument, "mctx") == 0)
192 isc_mem_debugging |= ISC_MEM_DEBUGCTX;
193 break;
194 default:
195 break;
196 }
197 }
198 isc_commandline_reset = true;
199 check_result(isc_app_start(), "isc_app_start");
200
201 result = isc_mem_create(0, 0, &mctx);
202 if (result != ISC_R_SUCCESS)
203 fatal("out of memory");
204
205 #if USE_PKCS11
206 pk11_result_register();
207 #endif
208 dns_result_register();
209
210 isc_commandline_errprint = false;
211
212 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
213 switch (ch) {
214 case 'c':
215 classname = isc_commandline_argument;
216 break;
217
218 case 'E':
219 engine = isc_commandline_argument;
220 break;
221
222 case 'I':
223 inputformatstr = isc_commandline_argument;
224 break;
225
226 case 'm':
227 break;
228
229 case 'o':
230 origin = isc_commandline_argument;
231 break;
232
233 case 'v':
234 endp = NULL;
235 verbose = strtol(isc_commandline_argument, &endp, 0);
236 if (*endp != '\0')
237 fatal("verbose level must be numeric");
238 break;
239
240 case 'x':
241 keyset_kskonly = true;
242 break;
243
244 case 'z':
245 ignore_kskflag = true;
246 break;
247
248 case '?':
249 if (isc_commandline_option != '?')
250 fprintf(stderr, "%s: invalid argument -%c\n",
251 program, isc_commandline_option);
252 /* FALLTHROUGH */
253
254 case 'h':
255 /* Does not return. */
256 usage();
257
258 case 'V':
259 /* Does not return. */
260 version(program);
261
262 default:
263 fprintf(stderr, "%s: unhandled option -%c\n",
264 program, isc_commandline_option);
265 exit(1);
266 }
267 }
268
269 result = dst_lib_init(mctx, engine);
270 if (result != ISC_R_SUCCESS)
271 fatal("could not initialize dst: %s",
272 isc_result_totext(result));
273
274 isc_stdtime_get(&now);
275
276 rdclass = strtoclass(classname);
277
278 setup_logging(mctx, &log);
279
280 argc -= isc_commandline_index;
281 argv += isc_commandline_index;
282
283 if (argc < 1)
284 usage();
285
286 file = argv[0];
287
288 argc -= 1;
289 argv += 1;
290
291 POST(argc);
292 POST(argv);
293
294 if (origin == NULL)
295 origin = file;
296
297 if (inputformatstr != NULL) {
298 if (strcasecmp(inputformatstr, "text") == 0)
299 inputformat = dns_masterformat_text;
300 else if (strcasecmp(inputformatstr, "raw") == 0)
301 inputformat = dns_masterformat_raw;
302 else
303 fatal("unknown file format: %s\n", inputformatstr);
304 }
305
306 gdb = NULL;
307 fprintf(stderr, "Loading zone '%s' from file '%s'\n", origin, file);
308 loadzone(file, origin, rdclass, &gdb);
309 gorigin = dns_db_origin(gdb);
310 gclass = dns_db_class(gdb);
311
312 gversion = NULL;
313 result = dns_db_newversion(gdb, &gversion);
314 check_result(result, "dns_db_newversion()");
315
316 result = dns_zoneverify_dnssec(NULL, gdb, gversion, gorigin, NULL,
317 mctx, ignore_kskflag, keyset_kskonly);
318
319 dns_db_closeversion(gdb, &gversion, false);
320 dns_db_detach(&gdb);
321
322 cleanup_logging(&log);
323 dst_lib_destroy();
324 dns_name_destroy();
325 if (verbose > 10)
326 isc_mem_stats(mctx, stdout);
327 isc_mem_destroy(&mctx);
328
329 (void) isc_app_finish();
330
331 return (result == ISC_R_SUCCESS ? 0 : 1);
332 }