named-checkconf.c 17.5 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Mark Andrews's avatar
Mark Andrews committed
10 11
 */

12 13

/*! \file */
Mark Andrews's avatar
Mark Andrews committed
14 15 16 17

#include <config.h>

#include <errno.h>
18
#include <stdbool.h>
Mark Andrews's avatar
Mark Andrews committed
19
#include <stdlib.h>
20
#include <stdio.h>
Mark Andrews's avatar
Mark Andrews committed
21

22 23
#include <isc/commandline.h>
#include <isc/dir.h>
24
#include <isc/hash.h>
25
#include <isc/log.h>
Mark Andrews's avatar
Mark Andrews committed
26
#include <isc/mem.h>
27
#include <isc/print.h>
28
#include <isc/result.h>
Mark Andrews's avatar
Mark Andrews committed
29 30 31
#include <isc/string.h>
#include <isc/util.h>

32
#include <isccfg/namedconf.h>
33 34

#include <bind9/check.h>
Mark Andrews's avatar
Mark Andrews committed
35

36
#include <dns/db.h>
37
#include <dns/fixedname.h>
38
#include <dns/log.h>
39
#include <dns/name.h>
40
#include <dns/rdataclass.h>
Mark Andrews's avatar
Mark Andrews committed
41
#include <dns/result.h>
42
#include <dns/rootns.h>
43
#include <dns/zone.h>
44

Mark Andrews's avatar
Mark Andrews committed
45 46
#include "check-tool.h"

47 48
static const char *program = "named-checkconf";

49
isc_log_t *logc = NULL;
50

51 52 53 54 55 56 57
#define CHECK(r)\
	do { \
		result = (r); \
		if (result != ISC_R_SUCCESS) \
			goto cleanup; \
	} while (0)

58
/*% usage */
Francis Dupont's avatar
Francis Dupont committed
59 60 61
ISC_PLATFORM_NORETURN_PRE static void
usage(void) ISC_PLATFORM_NORETURN_POST;

62 63
static void
usage(void) {
64
	fprintf(stderr, "usage: %s [-hjlvz] [-p [-x]] [-t directory] "
65
		"[named.conf]\n", program);
Automatic Updater's avatar
Automatic Updater committed
66
	exit(1);
67 68
}

69
/*% directory callback */
Mark Andrews's avatar
Mark Andrews committed
70
static isc_result_t
71
directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
72
	isc_result_t result;
73
	const char *directory;
74 75 76 77

	REQUIRE(strcasecmp("directory", clausename) == 0);

	UNUSED(arg);
Brian Wellington's avatar
Brian Wellington committed
78
	UNUSED(clausename);
79 80 81 82 83 84 85

	/*
	 * Change directory.
	 */
	directory = cfg_obj_asstring(obj);
	result = isc_dir_chdir(directory);
	if (result != ISC_R_SUCCESS) {
86
		cfg_obj_log(obj, logc, ISC_LOG_ERROR,
Mark Andrews's avatar
Mark Andrews committed
87
			    "change directory to '%s' failed: %s\n",
88 89 90
			    directory, isc_result_totext(result));
		return (result);
	}
Mark Andrews's avatar
Mark Andrews committed
91 92 93 94

	return (ISC_R_SUCCESS);
}

95
static bool
96
get_maps(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
97 98 99
	int i;
	for (i = 0;; i++) {
		if (maps[i] == NULL)
100
			return (false);
101
		if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
102
			return (true);
103 104 105
	}
}

106
static bool
107 108 109 110 111
get_checknames(const cfg_obj_t **maps, const cfg_obj_t **obj) {
	const cfg_listelt_t *element;
	const cfg_obj_t *checknames;
	const cfg_obj_t *type;
	const cfg_obj_t *value;
112 113 114 115 116
	isc_result_t result;
	int i;

	for (i = 0;; i++) {
		if (maps[i] == NULL)
117
			return (false);
118 119 120 121 122 123
		checknames = NULL;
		result = cfg_map_get(maps[i], "check-names", &checknames);
		if (result != ISC_R_SUCCESS)
			continue;
		if (checknames != NULL && !cfg_obj_islist(checknames)) {
			*obj = checknames;
124
			return (true);
125 126 127 128 129 130
		}
		for (element = cfg_list_first(checknames);
		     element != NULL;
		     element = cfg_list_next(element)) {
			value = cfg_listelt_value(element);
			type = cfg_tuple_get(value, "type");
131 132 133 134 135
			if ((strcasecmp(cfg_obj_asstring(type),
					"primary") != 0) &&
			    (strcasecmp(cfg_obj_asstring(type),
					"master") != 0))
			{
136
				continue;
137
			}
138
			*obj = cfg_tuple_get(value, "mode");
139
			return (true);
140 141 142 143
		}
	}
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static isc_result_t
configure_hint(const char *zfile, const char *zclass, isc_mem_t *mctx) {
	isc_result_t result;
	dns_db_t *db = NULL;
	dns_rdataclass_t rdclass;
	isc_textregion_t r;

	if (zfile == NULL)
		return (ISC_R_FAILURE);

	DE_CONST(zclass, r.base);
	r.length = strlen(zclass);
	result = dns_rdataclass_fromtext(&rdclass, &r);
	if (result != ISC_R_SUCCESS)
		return (result);

	result = dns_rootns_create(mctx, rdclass, zfile, &db);
	if (result != ISC_R_SUCCESS)
		return (result);

	dns_db_detach(&db);
	return (ISC_R_SUCCESS);
}

168
/*% configure the zone */
169
static isc_result_t
170 171
configure_zone(const char *vclass, const char *view,
	       const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
172
	       const cfg_obj_t *config, isc_mem_t *mctx, bool list)
Mark Andrews's avatar
Mark Andrews committed
173
{
174
	int i = 0;
Mark Andrews's avatar
Mark Andrews committed
175
	isc_result_t result;
176 177
	const char *zclass;
	const char *zname;
178
	const char *zfile = NULL;
179
	const cfg_obj_t *maps[4];
180
	const cfg_obj_t *mastersobj = NULL;
181
	const cfg_obj_t *inviewobj = NULL;
182 183 184 185
	const cfg_obj_t *zoptions = NULL;
	const cfg_obj_t *classobj = NULL;
	const cfg_obj_t *typeobj = NULL;
	const cfg_obj_t *fileobj = NULL;
186
	const cfg_obj_t *dlzobj = NULL;
187 188 189
	const cfg_obj_t *dbobj = NULL;
	const cfg_obj_t *obj = NULL;
	const cfg_obj_t *fmtobj = NULL;
190
	dns_masterformat_t masterformat;
Evan Hunt's avatar
Evan Hunt committed
191
	dns_ttl_t maxttl = 0;
192

193
	zone_options = DNS_ZONEOPT_CHECKNS | DNS_ZONEOPT_MANYERRORS;
194 195 196

	zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
	classobj = cfg_tuple_get(zconfig, "class");
Automatic Updater's avatar
Automatic Updater committed
197 198 199
	if (!cfg_obj_isstring(classobj))
		zclass = vclass;
	else
200
		zclass = cfg_obj_asstring(classobj);
201

202
	zoptions = cfg_tuple_get(zconfig, "options");
203 204 205 206 207 208 209 210
	maps[i++] = zoptions;
	if (vconfig != NULL)
		maps[i++] = cfg_tuple_get(vconfig, "options");
	if (config != NULL) {
		cfg_map_get(config, "options", &obj);
		if (obj != NULL)
			maps[i++] = obj;
	}
211
	maps[i] = NULL;
212

213
	cfg_map_get(zoptions, "in-view", &inviewobj);
214 215 216 217
	if (inviewobj != NULL && list) {
		const char *inview = cfg_obj_asstring(inviewobj);
		printf("%s %s %s in-view %s\n", zname, zclass, view, inview);
	}
218 219 220
	if (inviewobj != NULL)
		return (ISC_R_SUCCESS);

221 222 223
	cfg_map_get(zoptions, "type", &typeobj);
	if (typeobj == NULL)
		return (ISC_R_FAILURE);
224

225 226 227 228 229 230
	if (list) {
		const char *ztype = cfg_obj_asstring(typeobj);
		printf("%s %s %s %s\n", zname, zclass, view, ztype);
		return (ISC_R_SUCCESS);
	}

231 232 233 234 235 236 237 238 239 240 241 242 243
	/*
	 * Skip checks when using an alternate data source.
	 */
	cfg_map_get(zoptions, "database", &dbobj);
	if (dbobj != NULL &&
	    strcmp("rbt", cfg_obj_asstring(dbobj)) != 0 &&
	    strcmp("rbt64", cfg_obj_asstring(dbobj)) != 0)
		return (ISC_R_SUCCESS);

	cfg_map_get(zoptions, "dlz", &dlzobj);
	if (dlzobj != NULL)
		return (ISC_R_SUCCESS);

244 245 246 247 248 249 250 251 252
	cfg_map_get(zoptions, "file", &fileobj);
	if (fileobj != NULL)
		zfile = cfg_obj_asstring(fileobj);

	/*
	 * Check hints files for hint zones.
	 * Skip loading checks for any type other than
	 * master and redirect
	 */
253
	if (strcasecmp(cfg_obj_asstring(typeobj), "hint") == 0) {
254
		return (configure_hint(zfile, zclass, mctx));
255 256 257 258
	} else if ((strcasecmp(cfg_obj_asstring(typeobj), "primary") != 0) &&
		   (strcasecmp(cfg_obj_asstring(typeobj), "master") != 0) &&
		   (strcasecmp(cfg_obj_asstring(typeobj), "redirect") != 0))
	{
259
		return (ISC_R_SUCCESS);
260
	}
261

262 263 264 265 266 267 268 269 270
	/*
	 * Is the redirect zone configured as a slave?
	 */
	if (strcasecmp(cfg_obj_asstring(typeobj), "redirect") == 0) {
		cfg_map_get(zoptions, "masters", &mastersobj);
		if (mastersobj != NULL)
			return (ISC_R_SUCCESS);
	}

271 272 273
	if (zfile == NULL)
		return (ISC_R_FAILURE);

274 275 276 277 278 279 280 281 282 283 284
	obj = NULL;
	if (get_maps(maps, "check-dup-records", &obj)) {
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKDUPRR;
			zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKDUPRR;
			zone_options |= DNS_ZONEOPT_CHECKDUPRRFAIL;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			zone_options &= ~DNS_ZONEOPT_CHECKDUPRR;
			zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
285
		} else {
286
			INSIST(0);
287 288
			ISC_UNREACHABLE();
		}
289 290 291 292 293
	} else {
		zone_options |= DNS_ZONEOPT_CHECKDUPRR;
		zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
	}

294 295 296 297 298 299 300 301 302 303 304
	obj = NULL;
	if (get_maps(maps, "check-mx", &obj)) {
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKMX;
			zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKMX;
			zone_options |= DNS_ZONEOPT_CHECKMXFAIL;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			zone_options &= ~DNS_ZONEOPT_CHECKMX;
			zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
305
		} else {
306
			INSIST(0);
307 308
			ISC_UNREACHABLE();
		}
309 310 311 312 313
	} else {
		zone_options |= DNS_ZONEOPT_CHECKMX;
		zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
	}

314 315 316 317 318 319
	obj = NULL;
	if (get_maps(maps, "check-integrity", &obj)) {
		if (cfg_obj_asboolean(obj))
			zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
		else
			zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
320 321
	} else
		zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
322

323 324 325 326 327 328 329 330 331 332 333
	obj = NULL;
	if (get_maps(maps, "check-mx-cname", &obj)) {
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			zone_options |= DNS_ZONEOPT_WARNMXCNAME;
			zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
			zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			zone_options |= DNS_ZONEOPT_WARNMXCNAME;
			zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
334
		} else {
335
			INSIST(0);
336 337
			ISC_UNREACHABLE();
		}
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	} else {
		zone_options |= DNS_ZONEOPT_WARNMXCNAME;
		zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
	}

	obj = NULL;
	if (get_maps(maps, "check-srv-cname", &obj)) {
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
			zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
			zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
			zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
354
		} else {
355
			INSIST(0);
356 357
			ISC_UNREACHABLE();
		}
358 359 360 361 362
	} else {
		zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
		zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
	}

363 364 365 366 367 368 369 370
	obj = NULL;
	if (get_maps(maps, "check-sibling", &obj)) {
		if (cfg_obj_asboolean(obj))
			zone_options |= DNS_ZONEOPT_CHECKSIBLING;
		else
			zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
	}

371 372 373 374 375 376
	obj = NULL;
	if (get_maps(maps, "check-spf", &obj)) {
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKSPF;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			zone_options &= ~DNS_ZONEOPT_CHECKSPF;
377
		} else {
378
			INSIST(0);
379 380
			ISC_UNREACHABLE();
		}
381 382 383 384
	} else {
		zone_options |= DNS_ZONEOPT_CHECKSPF;
	}

385 386 387 388 389 390 391 392 393 394 395
	obj = NULL;
	if (get_checknames(maps, &obj)) {
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKNAMES;
			zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			zone_options |= DNS_ZONEOPT_CHECKNAMES;
			zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			zone_options &= ~DNS_ZONEOPT_CHECKNAMES;
			zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
396
		} else {
397
			INSIST(0);
398 399
			ISC_UNREACHABLE();
		}
400
	} else {
Automatic Updater's avatar
Automatic Updater committed
401 402
	       zone_options |= DNS_ZONEOPT_CHECKNAMES;
	       zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
403 404
	}

405 406
	masterformat = dns_masterformat_text;
	fmtobj = NULL;
407
	if (get_maps(maps, "masterfile-format", &fmtobj)) {
408
		const char *masterformatstr = cfg_obj_asstring(fmtobj);
409
		if (strcasecmp(masterformatstr, "text") == 0) {
410
			masterformat = dns_masterformat_text;
411
		} else if (strcasecmp(masterformatstr, "raw") == 0) {
412
			masterformat = dns_masterformat_raw;
413
		} else if (strcasecmp(masterformatstr, "map") == 0) {
Evan Hunt's avatar
Evan Hunt committed
414
			masterformat = dns_masterformat_map;
415
		} else {
416
			INSIST(0);
417 418
			ISC_UNREACHABLE();
		}
419 420
	}

Evan Hunt's avatar
Evan Hunt committed
421 422 423
	obj = NULL;
	if (get_maps(maps, "max-zone-ttl", &obj)) {
		maxttl = cfg_obj_asuint32(obj);
424
		zone_options |= DNS_ZONEOPT_CHECKTTL;
Evan Hunt's avatar
Evan Hunt committed
425 426 427 428
	}

	result = load_zone(mctx, zname, zfile, masterformat,
			   zclass, maxttl, NULL);
Mark Andrews's avatar
Mark Andrews committed
429 430 431
	if (result != ISC_R_SUCCESS)
		fprintf(stderr, "%s/%s/%s: %s\n", view, zname, zclass,
			dns_result_totext(result));
432
	return (result);
433 434
}

435
/*% configure a view */
436
static isc_result_t
437
configure_view(const char *vclass, const char *view, const cfg_obj_t *config,
438
	       const cfg_obj_t *vconfig, isc_mem_t *mctx, bool list)
439
{
440 441 442
	const cfg_listelt_t *element;
	const cfg_obj_t *voptions;
	const cfg_obj_t *zonelist;
443
	isc_result_t result = ISC_R_SUCCESS;
444
	isc_result_t tresult;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

	voptions = NULL;
	if (vconfig != NULL)
		voptions = cfg_tuple_get(vconfig, "options");

	zonelist = NULL;
	if (voptions != NULL)
		(void)cfg_map_get(voptions, "zone", &zonelist);
	else
		(void)cfg_map_get(config, "zone", &zonelist);

	for (element = cfg_list_first(zonelist);
	     element != NULL;
	     element = cfg_list_next(element))
	{
460
		const cfg_obj_t *zconfig = cfg_listelt_value(element);
461
		tresult = configure_zone(vclass, view, zconfig, vconfig,
462
					 config, mctx, list);
463 464
		if (tresult != ISC_R_SUCCESS)
			result = tresult;
465 466 467 468
	}
	return (result);
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482
static isc_result_t
config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
		dns_rdataclass_t *classp)
{
	isc_textregion_t r;

	if (!cfg_obj_isstring(classobj)) {
		*classp = defclass;
		return (ISC_R_SUCCESS);
	}
	DE_CONST(cfg_obj_asstring(classobj), r.base);
	r.length = strlen(r.base);
	return (dns_rdataclass_fromtext(classp, &r));
}
483

484
/*% load zones from the configuration */
485
static isc_result_t
486
load_zones_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx,
487
		      bool list_zones)
488
{
489 490 491
	const cfg_listelt_t *element;
	const cfg_obj_t *views;
	const cfg_obj_t *vconfig;
492
	isc_result_t result = ISC_R_SUCCESS;
493
	isc_result_t tresult;
494 495 496 497 498 499 500 501

	views = NULL;

	(void)cfg_map_get(config, "view", &views);
	for (element = cfg_list_first(views);
	     element != NULL;
	     element = cfg_list_next(element))
	{
502 503
		const cfg_obj_t *classobj;
		dns_rdataclass_t viewclass;
Mark Andrews's avatar
Mark Andrews committed
504
		const char *vname;
505
		char buf[sizeof("CLASS65535")];
506 507

		vconfig = cfg_listelt_value(element);
508 509 510 511 512 513 514 515 516 517
		if (vconfig == NULL)
			continue;

		classobj = cfg_tuple_get(vconfig, "class");
		CHECK(config_getclass(classobj, dns_rdataclass_in,
					 &viewclass));
		if (dns_rdataclass_ismeta(viewclass))
			CHECK(ISC_R_FAILURE);

		dns_rdataclass_format(viewclass, buf, sizeof(buf));
Mark Andrews's avatar
Mark Andrews committed
518
		vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
519 520
		tresult = configure_view(buf, vname, config, vconfig, mctx,
					 list_zones);
521 522
		if (tresult != ISC_R_SUCCESS)
			result = tresult;
523 524
	}

525
	if (views == NULL) {
526 527
		tresult = configure_view("IN", "_default", config, NULL, mctx,
					 list_zones);
528 529 530
		if (tresult != ISC_R_SUCCESS)
			result = tresult;
	}
531 532

cleanup:
533 534 535
	return (result);
}

536 537 538 539 540 541 542 543 544
static void
output(void *closure, const char *text, int textlen) {
	UNUSED(closure);
	if (fwrite(text, 1, textlen, stdout) != (size_t)textlen) {
		perror("fwrite");
		exit(1);
	}
}

545
/*% The main processing routine */
Mark Andrews's avatar
Mark Andrews committed
546
int
Andreas Gustafsson's avatar
spacing  
Andreas Gustafsson committed
547
main(int argc, char **argv) {
548
	int c;
549 550
	cfg_parser_t *parser = NULL;
	cfg_obj_t *config = NULL;
Mark Andrews's avatar
Mark Andrews committed
551 552
	const char *conffile = NULL;
	isc_mem_t *mctx = NULL;
553
	isc_result_t result;
554
	int exit_status = 0;
555 556 557
	bool load_zones = false;
	bool list_zones = false;
	bool print = false;
558
	unsigned int flags = 0;
Automatic Updater's avatar
Automatic Updater committed
559

560
	isc_commandline_errprint = false;
561

562 563 564
	/*
	 * Process memory debugging argument first.
	 */
565
#define CMDLINE_FLAGS "dhjlm:t:pvxz"
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	while ((c = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
		switch (c) {
		case 'm':
			if (strcasecmp(isc_commandline_argument, "record") == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
			if (strcasecmp(isc_commandline_argument, "trace") == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
			if (strcasecmp(isc_commandline_argument, "usage") == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
			if (strcasecmp(isc_commandline_argument, "size") == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
			if (strcasecmp(isc_commandline_argument, "mctx") == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGCTX;
			break;
		default:
			break;
		}
	}
584
	isc_commandline_reset = true;
585 586 587 588

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);

	while ((c = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != EOF) {
589
		switch (c) {
590 591 592 593
		case 'd':
			debug++;
			break;

594
		case 'j':
595
			nomerge = false;
596 597
			break;

598
		case 'l':
599
			list_zones = true;
600 601
			break;

602 603 604
		case 'm':
			break;

605 606 607 608 609 610 611 612 613
		case 't':
			result = isc_dir_chroot(isc_commandline_argument);
			if (result != ISC_R_SUCCESS) {
				fprintf(stderr, "isc_dir_chroot: %s\n",
					isc_result_totext(result));
				exit(1);
			}
			break;

614
		case 'p':
615
			print = true;
616 617
			break;

618 619 620 621
		case 'v':
			printf(VERSION "\n");
			exit(0);

622 623 624 625
		case 'x':
			flags |= CFG_PRINTER_XKEY;
			break;

626
		case 'z':
627 628 629 630
			load_zones = true;
			docheckmx = false;
			docheckns = false;
			dochecksrv = false;
631 632
			break;

633 634 635 636
		case '?':
			if (isc_commandline_option != '?')
				fprintf(stderr, "%s: invalid argument -%c\n",
					program, isc_commandline_option);
Evan Hunt's avatar
Evan Hunt committed
637
			/* FALLTHROUGH */
638
		case 'h':
639
			usage();
640 641 642 643 644

		default:
			fprintf(stderr, "%s: unhandled option -%c\n",
				program, isc_commandline_option);
			exit(1);
645 646 647
		}
	}

648 649 650 651
	if (((flags & CFG_PRINTER_XKEY) != 0) && !print) {
		fprintf(stderr, "%s: -x cannot be used without -p\n", program);
		exit(1);
	}
652 653 654 655
	if (print && list_zones) {
		fprintf(stderr, "%s: -l cannot be used with -p\n", program);
		exit(1);
	}
656

657 658
	if (isc_commandline_index + 1 < argc)
		usage();
659 660
	if (argv[isc_commandline_index] != NULL)
		conffile = argv[isc_commandline_index];
Mark Andrews's avatar
Mark Andrews committed
661
	if (conffile == NULL || conffile[0] == '\0')
662
		conffile = NAMED_CONFFILE;
Mark Andrews's avatar
Mark Andrews committed
663

664 665 666 667
#ifdef _WIN32
	InitSockets();
#endif

668
	RUNTIME_CHECK(setup_logging(mctx, stdout, &logc) == ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
669

670 671
	dns_result_register();

672
	RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
673 674 675 676 677

	cfg_parser_setcallback(parser, directory_callback, NULL);

	if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
	    ISC_R_SUCCESS)
Mark Andrews's avatar
Mark Andrews committed
678 679
		exit(1);

680
	result = bind9_check_namedconf(config, logc, mctx);
681 682
	if (result != ISC_R_SUCCESS)
		exit_status = 1;
683

684 685
	if (result == ISC_R_SUCCESS && (load_zones || list_zones)) {
		result = load_zones_fromconfig(config, mctx, list_zones);
686 687 688 689
		if (result != ISC_R_SUCCESS)
			exit_status = 1;
	}

690
	if (print && exit_status == 0)
691
		cfg_printx(config, flags, output, NULL);
692 693 694
	cfg_obj_destroy(parser, &config);

	cfg_parser_destroy(&parser);
Mark Andrews's avatar
Mark Andrews committed
695

696 697
	dns_name_destroy();

698
	isc_log_destroy(&logc);
Mark Andrews's avatar
Mark Andrews committed
699 700 701

	isc_mem_destroy(&mctx);

702 703 704 705
#ifdef _WIN32
	DestroySockets();
#endif

706
	return (exit_status);
Mark Andrews's avatar
Mark Andrews committed
707
}