2 * Copyright (C) 2004-2007 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.28.18.16 2007/11/26 23:46:18 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 isc_log_t *logc = NULL;
55 if (result != ISC_R_SUCCESS) \
62 fprintf(stderr, "usage: named-checkconf [-j] [-v] [-z] [-t directory] "
67 /*% directory callback */
69 directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
71 const char *directory;
73 REQUIRE(strcasecmp("directory", clausename) == 0);
81 directory = cfg_obj_asstring(obj);
82 result = isc_dir_chdir(directory);
83 if (result != ISC_R_SUCCESS) {
84 cfg_obj_log(obj, logc, ISC_LOG_ERROR,
85 "change directory to '%s' failed: %s\n",
86 directory, isc_result_totext(result));
90 return (ISC_R_SUCCESS);
94 get_maps(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
99 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
105 get_checknames(const cfg_obj_t **maps, const cfg_obj_t **obj) {
106 const cfg_listelt_t *element;
107 const cfg_obj_t *checknames;
108 const cfg_obj_t *type;
109 const cfg_obj_t *value;
117 result = cfg_map_get(maps[i], "check-names", &checknames);
118 if (result != ISC_R_SUCCESS)
120 if (checknames != NULL && !cfg_obj_islist(checknames)) {
124 for (element = cfg_list_first(checknames);
126 element = cfg_list_next(element)) {
127 value = cfg_listelt_value(element);
128 type = cfg_tuple_get(value, "type");
129 if (strcasecmp(cfg_obj_asstring(type), "master") != 0)
131 *obj = cfg_tuple_get(value, "mode");
138 config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
143 return (ISC_R_NOTFOUND);
144 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
145 return (ISC_R_SUCCESS);
149 /*% configure the zone */
151 configure_zone(const char *vclass, const char *view,
152 const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
153 const cfg_obj_t *config, isc_mem_t *mctx)
160 const cfg_obj_t *maps[4];
161 const cfg_obj_t *zoptions = NULL;
162 const cfg_obj_t *classobj = NULL;
163 const cfg_obj_t *typeobj = NULL;
164 const cfg_obj_t *fileobj = NULL;
165 const cfg_obj_t *dbobj = NULL;
166 const cfg_obj_t *obj = NULL;
167 const cfg_obj_t *fmtobj = NULL;
168 dns_masterformat_t masterformat;
170 zone_options = DNS_ZONEOPT_CHECKNS | DNS_ZONEOPT_MANYERRORS;
172 zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
173 classobj = cfg_tuple_get(zconfig, "class");
174 if (!cfg_obj_isstring(classobj))
177 zclass = cfg_obj_asstring(classobj);
179 zoptions = cfg_tuple_get(zconfig, "options");
180 maps[i++] = zoptions;
182 maps[i++] = cfg_tuple_get(vconfig, "options");
183 if (config != NULL) {
184 cfg_map_get(config, "options", &obj);
190 cfg_map_get(zoptions, "type", &typeobj);
192 return (ISC_R_FAILURE);
193 if (strcasecmp(cfg_obj_asstring(typeobj), "master") != 0)
194 return (ISC_R_SUCCESS);
195 cfg_map_get(zoptions, "database", &dbobj);
197 return (ISC_R_SUCCESS);
198 cfg_map_get(zoptions, "file", &fileobj);
200 return (ISC_R_FAILURE);
201 zfile = cfg_obj_asstring(fileobj);
204 if (get_maps(maps, "check-mx", &obj)) {
205 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
206 zone_options |= DNS_ZONEOPT_CHECKMX;
207 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
208 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
209 zone_options |= DNS_ZONEOPT_CHECKMX;
210 zone_options |= DNS_ZONEOPT_CHECKMXFAIL;
211 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
212 zone_options &= ~DNS_ZONEOPT_CHECKMX;
213 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
217 zone_options |= DNS_ZONEOPT_CHECKMX;
218 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
222 if (get_maps(maps, "check-integrity", &obj)) {
223 if (cfg_obj_asboolean(obj))
224 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
226 zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
228 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
231 if (get_maps(maps, "check-mx-cname", &obj)) {
232 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
233 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
234 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
235 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
236 zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
237 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
238 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
239 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
240 zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
244 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
245 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
249 if (get_maps(maps, "check-srv-cname", &obj)) {
250 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
251 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
252 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
253 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
254 zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
255 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
256 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
257 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
258 zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
262 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
263 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
267 if (get_maps(maps, "check-sibling", &obj)) {
268 if (cfg_obj_asboolean(obj))
269 zone_options |= DNS_ZONEOPT_CHECKSIBLING;
271 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
275 if (get_checknames(maps, &obj)) {
276 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
277 zone_options |= DNS_ZONEOPT_CHECKNAMES;
278 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
279 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
280 zone_options |= DNS_ZONEOPT_CHECKNAMES;
281 zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
282 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
283 zone_options &= ~DNS_ZONEOPT_CHECKNAMES;
284 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
288 zone_options |= DNS_ZONEOPT_CHECKNAMES;
289 zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
292 masterformat = dns_masterformat_text;
294 result = config_get(maps, "masterfile-format", &fmtobj);
295 if (result == ISC_R_SUCCESS) {
296 const char *masterformatstr = cfg_obj_asstring(fmtobj);
297 if (strcasecmp(masterformatstr, "text") == 0)
298 masterformat = dns_masterformat_text;
299 else if (strcasecmp(masterformatstr, "raw") == 0)
300 masterformat = dns_masterformat_raw;
305 result = load_zone(mctx, zname, zfile, masterformat, zclass, NULL);
306 if (result != ISC_R_SUCCESS)
307 fprintf(stderr, "%s/%s/%s: %s\n", view, zname, zclass,
308 dns_result_totext(result));
312 /*% configure a view */
314 configure_view(const char *vclass, const char *view, const cfg_obj_t *config,
315 const cfg_obj_t *vconfig, isc_mem_t *mctx)
317 const cfg_listelt_t *element;
318 const cfg_obj_t *voptions;
319 const cfg_obj_t *zonelist;
320 isc_result_t result = ISC_R_SUCCESS;
321 isc_result_t tresult;
325 voptions = cfg_tuple_get(vconfig, "options");
328 if (voptions != NULL)
329 (void)cfg_map_get(voptions, "zone", &zonelist);
331 (void)cfg_map_get(config, "zone", &zonelist);
333 for (element = cfg_list_first(zonelist);
335 element = cfg_list_next(element))
337 const cfg_obj_t *zconfig = cfg_listelt_value(element);
338 tresult = configure_zone(vclass, view, zconfig, vconfig,
340 if (tresult != ISC_R_SUCCESS)
347 /*% load zones from the configuration */
349 load_zones_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx) {
350 const cfg_listelt_t *element;
351 const cfg_obj_t *classobj;
352 const cfg_obj_t *views;
353 const cfg_obj_t *vconfig;
355 isc_result_t result = ISC_R_SUCCESS;
356 isc_result_t tresult;
360 (void)cfg_map_get(config, "view", &views);
361 for (element = cfg_list_first(views);
363 element = cfg_list_next(element))
368 vconfig = cfg_listelt_value(element);
369 if (vconfig != NULL) {
370 classobj = cfg_tuple_get(vconfig, "class");
371 if (cfg_obj_isstring(classobj))
372 vclass = cfg_obj_asstring(classobj);
374 vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
375 tresult = configure_view(vclass, vname, config, vconfig, mctx);
376 if (tresult != ISC_R_SUCCESS)
381 tresult = configure_view("IN", "_default", config, NULL, mctx);
382 if (tresult != ISC_R_SUCCESS)
388 /*% The main processing routine */
390 main(int argc, char **argv) {
392 cfg_parser_t *parser = NULL;
393 cfg_obj_t *config = NULL;
394 const char *conffile = NULL;
395 isc_mem_t *mctx = NULL;
398 isc_entropy_t *ectx = NULL;
399 isc_boolean_t load_zones = ISC_FALSE;
401 while ((c = isc_commandline_parse(argc, argv, "djt:vz")) != EOF) {
412 result = isc_dir_chroot(isc_commandline_argument);
413 if (result != ISC_R_SUCCESS) {
414 fprintf(stderr, "isc_dir_chroot: %s\n",
415 isc_result_totext(result));
418 result = isc_dir_chdir("/");
419 if (result != ISC_R_SUCCESS) {
420 fprintf(stderr, "isc_dir_chdir: %s\n",
421 isc_result_totext(result));
427 printf(VERSION "\n");
431 load_zones = ISC_TRUE;
432 docheckmx = ISC_FALSE;
433 docheckns = ISC_FALSE;
434 dochecksrv = ISC_FALSE;
442 if (argv[isc_commandline_index] != NULL)
443 conffile = argv[isc_commandline_index];
444 if (conffile == NULL || conffile[0] == '\0')
445 conffile = NAMED_CONFFILE;
447 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
449 RUNTIME_CHECK(setup_logging(mctx, &logc) == ISC_R_SUCCESS);
451 RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
452 RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
455 dns_result_register();
457 RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
459 cfg_parser_setcallback(parser, directory_callback, NULL);
461 if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
465 result = bind9_check_namedconf(config, logc, mctx);
466 if (result != ISC_R_SUCCESS)
469 if (result == ISC_R_SUCCESS && load_zones) {
470 result = load_zones_fromconfig(config, mctx);
471 if (result != ISC_R_SUCCESS)
475 cfg_obj_destroy(parser, &config);
477 cfg_parser_destroy(&parser);
481 isc_log_destroy(&logc);
484 isc_entropy_detach(&ectx);
486 isc_mem_destroy(&mctx);
488 return (exit_status);