2 * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: named-checkzone.c,v 1.29.18.21 2008/10/24 01:43:17 tbox Exp $ */
27 #include <isc/commandline.h>
29 #include <isc/entropy.h>
33 #include <isc/socket.h>
34 #include <isc/string.h>
36 #include <isc/timer.h>
40 #include <dns/fixedname.h>
42 #include <dns/masterdump.h>
44 #include <dns/rdataclass.h>
45 #include <dns/rdataset.h>
46 #include <dns/result.h>
47 #include <dns/types.h>
50 #include "check-tool.h"
53 static isc_mem_t *mctx = NULL;
54 static isc_entropy_t *ectx = NULL;
55 dns_zone_t *zone = NULL;
56 dns_zonetype_t zonetype = dns_zone_master;
57 static int dumpzone = 0;
58 static const char *output_filename;
59 static char *prog_name = NULL;
60 static const dns_master_style_t *outputstyle = NULL;
61 static enum { progmode_check, progmode_compile } progmode;
63 #define ERRRET(result, function) \
65 if (result != ISC_R_SUCCESS) { \
67 fprintf(stderr, "%s() returned %s\n", \
68 function, dns_result_totext(result)); \
76 "usage: %s [-djqvD] [-c class] [-o output] "
77 "[-f inputformat] [-F outputformat] "
78 "[-t directory] [-w directory] [-k (ignore|warn|fail)] "
79 "[-n (ignore|warn|fail)] [-m (ignore|warn|fail)] "
80 "[-i (full|full-sibling|local|local-sibling|none)] "
81 "[-M (ignore|warn|fail)] [-S (ignore|warn|fail)] "
83 "zonename filename\n", prog_name);
90 dns_zone_detach(&zone);
94 /*% main processing routine */
96 main(int argc, char **argv) {
99 char *filename = NULL;
100 isc_log_t *lctx = NULL;
102 char classname_in[] = "IN";
103 char *classname = classname_in;
104 const char *workdir = NULL;
105 const char *inputformatstr = NULL;
106 const char *outputformatstr = NULL;
107 dns_masterformat_t inputformat = dns_masterformat_text;
108 dns_masterformat_t outputformat = dns_masterformat_text;
110 outputstyle = &dns_master_style_full;
112 prog_name = strrchr(argv[0], '/');
113 if (prog_name == NULL)
114 prog_name = strrchr(argv[0], '\\');
115 if (prog_name != NULL)
120 * Libtool doesn't preserve the program name prior to final
121 * installation. Remove the libtool prefix ("lt-").
123 if (strncmp(prog_name, "lt-", 3) == 0)
125 if (strcmp(prog_name, "named-checkzone") == 0)
126 progmode = progmode_check;
127 else if (strcmp(prog_name, "named-compilezone") == 0)
128 progmode = progmode_compile;
132 /* Compilation specific defaults */
133 if (progmode == progmode_compile) {
134 zone_options |= (DNS_ZONEOPT_CHECKNS |
135 DNS_ZONEOPT_FATALNS |
136 DNS_ZONEOPT_CHECKNAMES |
137 DNS_ZONEOPT_CHECKNAMESFAIL |
138 DNS_ZONEOPT_CHECKWILDCARD);
141 #define ARGCMP(X) (strcmp(isc_commandline_argument, X) == 0)
143 while ((c = isc_commandline_parse(argc, argv,
144 "c:df:i:jk:m:n:qs:t:o:vw:DF:M:S:W:"))
148 classname = isc_commandline_argument;
156 if (ARGCMP("full")) {
157 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY |
158 DNS_ZONEOPT_CHECKSIBLING;
159 docheckmx = ISC_TRUE;
160 docheckns = ISC_TRUE;
161 dochecksrv = ISC_TRUE;
162 } else if (ARGCMP("full-sibling")) {
163 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
164 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
165 docheckmx = ISC_TRUE;
166 docheckns = ISC_TRUE;
167 dochecksrv = ISC_TRUE;
168 } else if (ARGCMP("local")) {
169 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
170 zone_options |= DNS_ZONEOPT_CHECKSIBLING;
171 docheckmx = ISC_FALSE;
172 docheckns = ISC_FALSE;
173 dochecksrv = ISC_FALSE;
174 } else if (ARGCMP("local-sibling")) {
175 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
176 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
177 docheckmx = ISC_FALSE;
178 docheckns = ISC_FALSE;
179 dochecksrv = ISC_FALSE;
180 } else if (ARGCMP("none")) {
181 zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
182 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
183 docheckmx = ISC_FALSE;
184 docheckns = ISC_FALSE;
185 dochecksrv = ISC_FALSE;
187 fprintf(stderr, "invalid argument to -i: %s\n",
188 isc_commandline_argument);
194 inputformatstr = isc_commandline_argument;
198 outputformatstr = isc_commandline_argument;
206 if (ARGCMP("warn")) {
207 zone_options |= DNS_ZONEOPT_CHECKNAMES;
208 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
209 } else if (ARGCMP("fail")) {
210 zone_options |= DNS_ZONEOPT_CHECKNAMES |
211 DNS_ZONEOPT_CHECKNAMESFAIL;
212 } else if (ARGCMP("ignore")) {
213 zone_options &= ~(DNS_ZONEOPT_CHECKNAMES |
214 DNS_ZONEOPT_CHECKNAMESFAIL);
216 fprintf(stderr, "invalid argument to -k: %s\n",
217 isc_commandline_argument);
223 if (ARGCMP("ignore")) {
224 zone_options &= ~(DNS_ZONEOPT_CHECKNS|
225 DNS_ZONEOPT_FATALNS);
226 } else if (ARGCMP("warn")) {
227 zone_options |= DNS_ZONEOPT_CHECKNS;
228 zone_options &= ~DNS_ZONEOPT_FATALNS;
229 } else if (ARGCMP("fail")) {
230 zone_options |= DNS_ZONEOPT_CHECKNS|
233 fprintf(stderr, "invalid argument to -n: %s\n",
234 isc_commandline_argument);
240 if (ARGCMP("warn")) {
241 zone_options |= DNS_ZONEOPT_CHECKMX;
242 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
243 } else if (ARGCMP("fail")) {
244 zone_options |= DNS_ZONEOPT_CHECKMX |
245 DNS_ZONEOPT_CHECKMXFAIL;
246 } else if (ARGCMP("ignore")) {
247 zone_options &= ~(DNS_ZONEOPT_CHECKMX |
248 DNS_ZONEOPT_CHECKMXFAIL);
250 fprintf(stderr, "invalid argument to -m: %s\n",
251 isc_commandline_argument);
261 result = isc_dir_chroot(isc_commandline_argument);
262 if (result != ISC_R_SUCCESS) {
263 fprintf(stderr, "isc_dir_chroot: %s: %s\n",
264 isc_commandline_argument,
265 isc_result_totext(result));
268 result = isc_dir_chdir("/");
269 if (result != ISC_R_SUCCESS) {
270 fprintf(stderr, "isc_dir_chdir: %s\n",
271 isc_result_totext(result));
278 outputstyle = &dns_master_style_full;
279 else if (ARGCMP("relative")) {
280 outputstyle = &dns_master_style_default;
283 "unknown or unsupported style: %s\n",
284 isc_commandline_argument);
290 output_filename = isc_commandline_argument;
294 printf(VERSION "\n");
298 workdir = isc_commandline_argument;
306 if (ARGCMP("fail")) {
307 zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
308 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
309 } else if (ARGCMP("warn")) {
310 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
311 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
312 } else if (ARGCMP("ignore")) {
313 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
314 zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
316 fprintf(stderr, "invalid argument to -M: %s\n",
317 isc_commandline_argument);
323 if (ARGCMP("fail")) {
324 zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
325 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
326 } else if (ARGCMP("warn")) {
327 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
328 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
329 } else if (ARGCMP("ignore")) {
330 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
331 zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
333 fprintf(stderr, "invalid argument to -S: %s\n",
334 isc_commandline_argument);
341 zone_options |= DNS_ZONEOPT_CHECKWILDCARD;
342 else if (ARGCMP("ignore"))
343 zone_options &= ~DNS_ZONEOPT_CHECKWILDCARD;
351 if (progmode == progmode_compile) {
352 dumpzone = 1; /* always dump */
353 if (output_filename == NULL) {
355 "output file required, but not specified\n");
360 if (workdir != NULL) {
361 result = isc_dir_chdir(workdir);
362 if (result != ISC_R_SUCCESS) {
363 fprintf(stderr, "isc_dir_chdir: %s: %s\n",
364 workdir, isc_result_totext(result));
369 if (inputformatstr != NULL) {
370 if (strcasecmp(inputformatstr, "text") == 0)
371 inputformat = dns_masterformat_text;
372 else if (strcasecmp(inputformatstr, "raw") == 0)
373 inputformat = dns_masterformat_raw;
375 fprintf(stderr, "unknown file format: %s\n",
381 if (outputformatstr != NULL) {
382 if (strcasecmp(outputformatstr, "text") == 0)
383 outputformat = dns_masterformat_text;
384 else if (strcasecmp(outputformatstr, "raw") == 0)
385 outputformat = dns_masterformat_raw;
387 fprintf(stderr, "unknown file format: %s\n",
393 if (isc_commandline_index + 2 > argc)
396 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
398 RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
399 RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
400 RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
403 dns_result_register();
405 origin = argv[isc_commandline_index++];
406 filename = argv[isc_commandline_index++];
407 result = load_zone(mctx, origin, filename, inputformat, classname,
410 if (result == ISC_R_SUCCESS && dumpzone) {
411 if (!quiet && progmode == progmode_compile) {
412 fprintf(stdout, "dump zone to %s...", output_filename);
415 result = dump_zone(origin, zone, output_filename,
416 outputformat, outputstyle);
417 if (!quiet && progmode == progmode_compile)
418 fprintf(stdout, "done\n");
421 if (!quiet && result == ISC_R_SUCCESS)
422 fprintf(stdout, "OK\n");
425 isc_log_destroy(&lctx);
427 isc_entropy_detach(&ectx);
428 isc_mem_destroy(&mctx);
429 return ((result == ISC_R_SUCCESS) ? 0 : 1);