Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / contrib / bind9 / bin / check / named-checkconf.c
blobcc63153e941a1966ae024230ba71b67a9fddc27a
1 /*
2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.14 2006/02/28 03:10:47 marka Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
28 #include <isc/commandline.h>
29 #include <isc/dir.h>
30 #include <isc/entropy.h>
31 #include <isc/hash.h>
32 #include <isc/log.h>
33 #include <isc/mem.h>
34 #include <isc/result.h>
35 #include <isc/string.h>
36 #include <isc/util.h>
38 #include <isccfg/namedconf.h>
40 #include <bind9/check.h>
42 #include <dns/fixedname.h>
43 #include <dns/log.h>
44 #include <dns/name.h>
45 #include <dns/result.h>
46 #include <dns/zone.h>
48 #include "check-tool.h"
50 isc_log_t *logc = NULL;
52 #define CHECK(r)\
53 do { \
54 result = (r); \
55 if (result != ISC_R_SUCCESS) \
56 goto cleanup; \
57 } while (0)
59 /*% usage */
60 static void
61 usage(void) {
62 fprintf(stderr, "usage: named-checkconf [-j] [-v] [-z] [-t directory] "
63 "[named.conf]\n");
64 exit(1);
67 /*% directory callback */
68 static isc_result_t
69 directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
70 isc_result_t result;
71 const char *directory;
73 REQUIRE(strcasecmp("directory", clausename) == 0);
75 UNUSED(arg);
76 UNUSED(clausename);
79 * Change directory.
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));
87 return (result);
90 return (ISC_R_SUCCESS);
93 static isc_boolean_t
94 get_maps(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
95 int i;
96 for (i = 0;; i++) {
97 if (maps[i] == NULL)
98 return (ISC_FALSE);
99 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
100 return (ISC_TRUE);
104 static isc_boolean_t
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;
110 isc_result_t result;
111 int i;
113 for (i = 0;; i++) {
114 if (maps[i] == NULL)
115 return (ISC_FALSE);
116 checknames = NULL;
117 result = cfg_map_get(maps[i], "check-names", &checknames);
118 if (result != ISC_R_SUCCESS)
119 continue;
120 if (checknames != NULL && !cfg_obj_islist(checknames)) {
121 *obj = checknames;
122 return (ISC_TRUE);
124 for (element = cfg_list_first(checknames);
125 element != NULL;
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)
130 continue;
131 *obj = cfg_tuple_get(value, "mode");
132 return (ISC_TRUE);
137 static isc_result_t
138 config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
139 int i;
141 for (i = 0;; i++) {
142 if (maps[i] == NULL)
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 */
150 static isc_result_t
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)
155 int i = 0;
156 isc_result_t result;
157 const char *zclass;
158 const char *zname;
159 const char *zfile;
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))
175 zclass = vclass;
176 else
177 zclass = cfg_obj_asstring(classobj);
179 zoptions = cfg_tuple_get(zconfig, "options");
180 maps[i++] = zoptions;
181 if (vconfig != NULL)
182 maps[i++] = cfg_tuple_get(vconfig, "options");
183 if (config != NULL) {
184 cfg_map_get(config, "options", &obj);
185 if (obj != NULL)
186 maps[i++] = obj;
188 maps[i++] = NULL;
190 cfg_map_get(zoptions, "type", &typeobj);
191 if (typeobj == NULL)
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);
196 if (dbobj != NULL)
197 return (ISC_R_SUCCESS);
198 cfg_map_get(zoptions, "file", &fileobj);
199 if (fileobj == NULL)
200 return (ISC_R_FAILURE);
201 zfile = cfg_obj_asstring(fileobj);
203 obj = NULL;
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;
214 } else
215 INSIST(0);
216 } else {
217 zone_options |= DNS_ZONEOPT_CHECKMX;
218 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
221 obj = NULL;
222 if (get_maps(maps, "check-integrity", &obj)) {
223 if (cfg_obj_asboolean(obj))
224 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
225 else
226 zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
229 obj = NULL;
230 if (get_maps(maps, "check-mx-cname", &obj)) {
231 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
232 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
233 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
234 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
235 zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
236 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
237 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
238 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
239 zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
240 } else
241 INSIST(0);
242 } else {
243 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
244 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
247 obj = NULL;
248 if (get_maps(maps, "check-srv-cname", &obj)) {
249 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
250 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
251 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
252 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
253 zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
254 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
255 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
256 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
257 zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
258 } else
259 INSIST(0);
260 } else {
261 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
262 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
265 obj = NULL;
266 if (get_maps(maps, "check-sibling", &obj)) {
267 if (cfg_obj_asboolean(obj))
268 zone_options |= DNS_ZONEOPT_CHECKSIBLING;
269 else
270 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
273 obj = NULL;
274 if (get_checknames(maps, &obj)) {
275 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
276 zone_options |= DNS_ZONEOPT_CHECKNAMES;
277 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
278 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
279 zone_options |= DNS_ZONEOPT_CHECKNAMES;
280 zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
281 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
282 zone_options &= ~DNS_ZONEOPT_CHECKNAMES;
283 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
284 } else
285 INSIST(0);
286 } else {
287 zone_options |= DNS_ZONEOPT_CHECKNAMES;
288 zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
291 masterformat = dns_masterformat_text;
292 fmtobj = NULL;
293 result = config_get(maps, "masterfile-format", &fmtobj);
294 if (result == ISC_R_SUCCESS) {
295 const char *masterformatstr = cfg_obj_asstring(fmtobj);
296 if (strcasecmp(masterformatstr, "text") == 0)
297 masterformat = dns_masterformat_text;
298 else if (strcasecmp(masterformatstr, "raw") == 0)
299 masterformat = dns_masterformat_raw;
300 else
301 INSIST(0);
304 result = load_zone(mctx, zname, zfile, masterformat, zclass, NULL);
305 if (result != ISC_R_SUCCESS)
306 fprintf(stderr, "%s/%s/%s: %s\n", view, zname, zclass,
307 dns_result_totext(result));
308 return(result);
311 /*% configure a view */
312 static isc_result_t
313 configure_view(const char *vclass, const char *view, const cfg_obj_t *config,
314 const cfg_obj_t *vconfig, isc_mem_t *mctx)
316 const cfg_listelt_t *element;
317 const cfg_obj_t *voptions;
318 const cfg_obj_t *zonelist;
319 isc_result_t result = ISC_R_SUCCESS;
320 isc_result_t tresult;
322 voptions = NULL;
323 if (vconfig != NULL)
324 voptions = cfg_tuple_get(vconfig, "options");
326 zonelist = NULL;
327 if (voptions != NULL)
328 (void)cfg_map_get(voptions, "zone", &zonelist);
329 else
330 (void)cfg_map_get(config, "zone", &zonelist);
332 for (element = cfg_list_first(zonelist);
333 element != NULL;
334 element = cfg_list_next(element))
336 const cfg_obj_t *zconfig = cfg_listelt_value(element);
337 tresult = configure_zone(vclass, view, zconfig, vconfig,
338 config, mctx);
339 if (tresult != ISC_R_SUCCESS)
340 result = tresult;
342 return (result);
346 /*% load zones from the configuration */
347 static isc_result_t
348 load_zones_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx) {
349 const cfg_listelt_t *element;
350 const cfg_obj_t *classobj;
351 const cfg_obj_t *views;
352 const cfg_obj_t *vconfig;
353 const char *vclass;
354 isc_result_t result = ISC_R_SUCCESS;
355 isc_result_t tresult;
357 views = NULL;
359 (void)cfg_map_get(config, "view", &views);
360 for (element = cfg_list_first(views);
361 element != NULL;
362 element = cfg_list_next(element))
364 const char *vname;
366 vclass = "IN";
367 vconfig = cfg_listelt_value(element);
368 if (vconfig != NULL) {
369 classobj = cfg_tuple_get(vconfig, "class");
370 if (cfg_obj_isstring(classobj))
371 vclass = cfg_obj_asstring(classobj);
373 vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
374 tresult = configure_view(vclass, vname, config, vconfig, mctx);
375 if (tresult != ISC_R_SUCCESS)
376 result = tresult;
379 if (views == NULL) {
380 tresult = configure_view("IN", "_default", config, NULL, mctx);
381 if (tresult != ISC_R_SUCCESS)
382 result = tresult;
384 return (result);
387 /*% The main processing routine */
389 main(int argc, char **argv) {
390 int c;
391 cfg_parser_t *parser = NULL;
392 cfg_obj_t *config = NULL;
393 const char *conffile = NULL;
394 isc_mem_t *mctx = NULL;
395 isc_result_t result;
396 int exit_status = 0;
397 isc_entropy_t *ectx = NULL;
398 isc_boolean_t load_zones = ISC_FALSE;
400 while ((c = isc_commandline_parse(argc, argv, "djt:vz")) != EOF) {
401 switch (c) {
402 case 'd':
403 debug++;
404 break;
406 case 'j':
407 nomerge = ISC_FALSE;
408 break;
410 case 't':
411 result = isc_dir_chroot(isc_commandline_argument);
412 if (result != ISC_R_SUCCESS) {
413 fprintf(stderr, "isc_dir_chroot: %s\n",
414 isc_result_totext(result));
415 exit(1);
417 result = isc_dir_chdir("/");
418 if (result != ISC_R_SUCCESS) {
419 fprintf(stderr, "isc_dir_chdir: %s\n",
420 isc_result_totext(result));
421 exit(1);
423 break;
425 case 'v':
426 printf(VERSION "\n");
427 exit(0);
429 case 'z':
430 load_zones = ISC_TRUE;
431 docheckmx = ISC_FALSE;
432 docheckns = ISC_FALSE;
433 dochecksrv = ISC_FALSE;
434 break;
436 default:
437 usage();
441 if (argv[isc_commandline_index] != NULL)
442 conffile = argv[isc_commandline_index];
443 if (conffile == NULL || conffile[0] == '\0')
444 conffile = NAMED_CONFFILE;
446 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
448 RUNTIME_CHECK(setup_logging(mctx, &logc) == ISC_R_SUCCESS);
450 RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
451 RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
452 == ISC_R_SUCCESS);
454 dns_result_register();
456 RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
458 cfg_parser_setcallback(parser, directory_callback, NULL);
460 if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
461 ISC_R_SUCCESS)
462 exit(1);
464 result = bind9_check_namedconf(config, logc, mctx);
465 if (result != ISC_R_SUCCESS)
466 exit_status = 1;
468 if (result == ISC_R_SUCCESS && load_zones) {
469 result = load_zones_fromconfig(config, mctx);
470 if (result != ISC_R_SUCCESS)
471 exit_status = 1;
474 cfg_obj_destroy(parser, &config);
476 cfg_parser_destroy(&parser);
478 dns_name_destroy();
480 isc_log_destroy(&logc);
482 isc_hash_destroy();
483 isc_entropy_detach(&ectx);
485 isc_mem_destroy(&mctx);
487 return (exit_status);