2 * Copyright (C) 2004-2007, 2009, 2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 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-checkconf.c,v 1.46.222.2.24.2 2010/09/07 23:46:26 tbox Exp $ */
28 #include <isc/commandline.h>
30 #include <isc/entropy.h>
34 #include <isc/result.h>
35 #include <isc/string.h>
38 #include <isccfg/namedconf.h>
40 #include <bind9/check.h>
42 #include <dns/fixedname.h>
45 #include <dns/result.h>
48 #include "check-tool.h"
50 static const char *program = "named-checkconf";
52 isc_log_t *logc = NULL;
57 if (result != ISC_R_SUCCESS) \
64 fprintf(stderr, "usage: %s [-h] [-j] [-v] [-z] [-t directory] "
65 "[named.conf]\n", program);
69 /*% directory callback */
71 directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
73 const char *directory;
75 REQUIRE(strcasecmp("directory", clausename) == 0);
83 directory = cfg_obj_asstring(obj);
84 result = isc_dir_chdir(directory);
85 if (result != ISC_R_SUCCESS) {
86 cfg_obj_log(obj, logc, ISC_LOG_ERROR,
87 "change directory to '%s' failed: %s\n",
88 directory, isc_result_totext(result));
92 return (ISC_R_SUCCESS);
96 get_maps(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
101 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
107 get_checknames(const cfg_obj_t **maps, const cfg_obj_t **obj) {
108 const cfg_listelt_t *element;
109 const cfg_obj_t *checknames;
110 const cfg_obj_t *type;
111 const cfg_obj_t *value;
119 result = cfg_map_get(maps[i], "check-names", &checknames);
120 if (result != ISC_R_SUCCESS)
122 if (checknames != NULL && !cfg_obj_islist(checknames)) {
126 for (element = cfg_list_first(checknames);
128 element = cfg_list_next(element)) {
129 value = cfg_listelt_value(element);
130 type = cfg_tuple_get(value, "type");
131 if (strcasecmp(cfg_obj_asstring(type), "master") != 0)
133 *obj = cfg_tuple_get(value, "mode");
140 config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
145 return (ISC_R_NOTFOUND);
146 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
147 return (ISC_R_SUCCESS);
151 /*% configure the zone */
153 configure_zone(const char *vclass, const char *view,
154 const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
155 const cfg_obj_t *config, isc_mem_t *mctx)
162 const cfg_obj_t *maps[4];
163 const cfg_obj_t *zoptions = NULL;
164 const cfg_obj_t *classobj = NULL;
165 const cfg_obj_t *typeobj = NULL;
166 const cfg_obj_t *fileobj = NULL;
167 const cfg_obj_t *dbobj = NULL;
168 const cfg_obj_t *obj = NULL;
169 const cfg_obj_t *fmtobj = NULL;
170 dns_masterformat_t masterformat;
172 zone_options = DNS_ZONEOPT_CHECKNS | DNS_ZONEOPT_MANYERRORS;
174 zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
175 classobj = cfg_tuple_get(zconfig, "class");
176 if (!cfg_obj_isstring(classobj))
179 zclass = cfg_obj_asstring(classobj);
181 zoptions = cfg_tuple_get(zconfig, "options");
182 maps[i++] = zoptions;
184 maps[i++] = cfg_tuple_get(vconfig, "options");
185 if (config != NULL) {
186 cfg_map_get(config, "options", &obj);
192 cfg_map_get(zoptions, "type", &typeobj);
194 return (ISC_R_FAILURE);
195 if (strcasecmp(cfg_obj_asstring(typeobj), "master") != 0)
196 return (ISC_R_SUCCESS);
197 cfg_map_get(zoptions, "database", &dbobj);
199 return (ISC_R_SUCCESS);
200 cfg_map_get(zoptions, "file", &fileobj);
202 return (ISC_R_FAILURE);
203 zfile = cfg_obj_asstring(fileobj);
206 if (get_maps(maps, "check-mx", &obj)) {
207 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
208 zone_options |= DNS_ZONEOPT_CHECKMX;
209 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
210 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
211 zone_options |= DNS_ZONEOPT_CHECKMX;
212 zone_options |= DNS_ZONEOPT_CHECKMXFAIL;
213 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
214 zone_options &= ~DNS_ZONEOPT_CHECKMX;
215 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
219 zone_options |= DNS_ZONEOPT_CHECKMX;
220 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
224 if (get_maps(maps, "check-integrity", &obj)) {
225 if (cfg_obj_asboolean(obj))
226 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
228 zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
230 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
233 if (get_maps(maps, "check-mx-cname", &obj)) {
234 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
235 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
236 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
237 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
238 zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
239 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
240 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
241 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
242 zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
246 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
247 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
251 if (get_maps(maps, "check-srv-cname", &obj)) {
252 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
253 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
254 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
255 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
256 zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
257 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
258 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
259 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
260 zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
264 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
265 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
269 if (get_maps(maps, "check-sibling", &obj)) {
270 if (cfg_obj_asboolean(obj))
271 zone_options |= DNS_ZONEOPT_CHECKSIBLING;
273 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
277 if (get_checknames(maps, &obj)) {
278 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
279 zone_options |= DNS_ZONEOPT_CHECKNAMES;
280 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
281 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
282 zone_options |= DNS_ZONEOPT_CHECKNAMES;
283 zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
284 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
285 zone_options &= ~DNS_ZONEOPT_CHECKNAMES;
286 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
290 zone_options |= DNS_ZONEOPT_CHECKNAMES;
291 zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
294 masterformat = dns_masterformat_text;
296 result = config_get(maps, "masterfile-format", &fmtobj);
297 if (result == ISC_R_SUCCESS) {
298 const char *masterformatstr = cfg_obj_asstring(fmtobj);
299 if (strcasecmp(masterformatstr, "text") == 0)
300 masterformat = dns_masterformat_text;
301 else if (strcasecmp(masterformatstr, "raw") == 0)
302 masterformat = dns_masterformat_raw;
307 result = load_zone(mctx, zname, zfile, masterformat, zclass, NULL);
308 if (result != ISC_R_SUCCESS)
309 fprintf(stderr, "%s/%s/%s: %s\n", view, zname, zclass,
310 dns_result_totext(result));
314 /*% configure a view */
316 configure_view(const char *vclass, const char *view, const cfg_obj_t *config,
317 const cfg_obj_t *vconfig, isc_mem_t *mctx)
319 const cfg_listelt_t *element;
320 const cfg_obj_t *voptions;
321 const cfg_obj_t *zonelist;
322 isc_result_t result = ISC_R_SUCCESS;
323 isc_result_t tresult;
327 voptions = cfg_tuple_get(vconfig, "options");
330 if (voptions != NULL)
331 (void)cfg_map_get(voptions, "zone", &zonelist);
333 (void)cfg_map_get(config, "zone", &zonelist);
335 for (element = cfg_list_first(zonelist);
337 element = cfg_list_next(element))
339 const cfg_obj_t *zconfig = cfg_listelt_value(element);
340 tresult = configure_zone(vclass, view, zconfig, vconfig,
342 if (tresult != ISC_R_SUCCESS)
349 /*% load zones from the configuration */
351 load_zones_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx) {
352 const cfg_listelt_t *element;
353 const cfg_obj_t *classobj;
354 const cfg_obj_t *views;
355 const cfg_obj_t *vconfig;
357 isc_result_t result = ISC_R_SUCCESS;
358 isc_result_t tresult;
362 (void)cfg_map_get(config, "view", &views);
363 for (element = cfg_list_first(views);
365 element = cfg_list_next(element))
370 vconfig = cfg_listelt_value(element);
371 if (vconfig != NULL) {
372 classobj = cfg_tuple_get(vconfig, "class");
373 if (cfg_obj_isstring(classobj))
374 vclass = cfg_obj_asstring(classobj);
376 vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
377 tresult = configure_view(vclass, vname, config, vconfig, mctx);
378 if (tresult != ISC_R_SUCCESS)
383 tresult = configure_view("IN", "_default", config, NULL, mctx);
384 if (tresult != ISC_R_SUCCESS)
390 /*% The main processing routine */
392 main(int argc, char **argv) {
394 cfg_parser_t *parser = NULL;
395 cfg_obj_t *config = NULL;
396 const char *conffile = NULL;
397 isc_mem_t *mctx = NULL;
400 isc_entropy_t *ectx = NULL;
401 isc_boolean_t load_zones = ISC_FALSE;
403 isc_commandline_errprint = ISC_FALSE;
405 while ((c = isc_commandline_parse(argc, argv, "dhjt:vz")) != EOF) {
416 result = isc_dir_chroot(isc_commandline_argument);
417 if (result != ISC_R_SUCCESS) {
418 fprintf(stderr, "isc_dir_chroot: %s\n",
419 isc_result_totext(result));
425 printf(VERSION "\n");
429 load_zones = ISC_TRUE;
430 docheckmx = ISC_FALSE;
431 docheckns = ISC_FALSE;
432 dochecksrv = ISC_FALSE;
436 if (isc_commandline_option != '?')
437 fprintf(stderr, "%s: invalid argument -%c\n",
438 program, isc_commandline_option);
443 fprintf(stderr, "%s: unhandled option -%c\n",
444 program, isc_commandline_option);
449 if (isc_commandline_index + 1 < argc)
451 if (argv[isc_commandline_index] != NULL)
452 conffile = argv[isc_commandline_index];
453 if (conffile == NULL || conffile[0] == '\0')
454 conffile = NAMED_CONFFILE;
460 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
462 RUNTIME_CHECK(setup_logging(mctx, stdout, &logc) == ISC_R_SUCCESS);
464 RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
465 RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
468 dns_result_register();
470 RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
472 cfg_parser_setcallback(parser, directory_callback, NULL);
474 if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
478 result = bind9_check_namedconf(config, logc, mctx);
479 if (result != ISC_R_SUCCESS)
482 if (result == ISC_R_SUCCESS && load_zones) {
483 result = load_zones_fromconfig(config, mctx);
484 if (result != ISC_R_SUCCESS)
488 cfg_obj_destroy(parser, &config);
490 cfg_parser_destroy(&parser);
494 isc_log_destroy(&logc);
497 isc_entropy_detach(&ectx);
499 isc_mem_destroy(&mctx);
505 return (exit_status);