check-tool.c 20.5 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2002  Internet Software Consortium.
Mark Andrews's avatar
Mark Andrews committed
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Mark Andrews's avatar
Mark Andrews committed
6 7 8
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Mark Andrews's avatar
Mark Andrews committed
16 17
 */

18
/* $Id: check-tool.c,v 1.44 2011/12/22 07:32:39 each Exp $ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21 22

#include <config.h>
Mark Andrews's avatar
Mark Andrews committed
23 24 25

#include <stdio.h>

26 27 28 29
#ifdef _WIN32
#include <Winsock2.h>
#endif

Mark Andrews's avatar
Mark Andrews committed
30
#include "check-tool.h"
31
#include <isc/buffer.h>
Mark Andrews's avatar
Mark Andrews committed
32
#include <isc/log.h>
Evan Hunt's avatar
Evan Hunt committed
33
#include <isc/mem.h>
34
#include <isc/netdb.h>
Evan Hunt's avatar
Evan Hunt committed
35
#include <isc/net.h>
36
#include <isc/region.h>
37
#include <isc/stdio.h>
Evan Hunt's avatar
Evan Hunt committed
38
#include <isc/string.h>
Mark Andrews's avatar
Mark Andrews committed
39
#include <isc/symtab.h>
Mark Andrews's avatar
Mark Andrews committed
40
#include <isc/types.h>
Evan Hunt's avatar
Evan Hunt committed
41
#include <isc/util.h>
Mark Andrews's avatar
Mark Andrews committed
42

Evan Hunt's avatar
Evan Hunt committed
43 44
#include <dns/db.h>
#include <dns/dbiterator.h>
45
#include <dns/fixedname.h>
46
#include <dns/log.h>
47
#include <dns/name.h>
48
#include <dns/rdata.h>
49
#include <dns/rdataclass.h>
50
#include <dns/rdataset.h>
Evan Hunt's avatar
Evan Hunt committed
51 52 53
#include <dns/rdatasetiter.h>
#include <dns/rdatatype.h>
#include <dns/result.h>
54 55 56
#include <dns/types.h>
#include <dns/zone.h>

57 58
#include <isccfg/log.h>

59 60 61 62 63 64 65 66
#ifndef CHECK_SIBLING
#define CHECK_SIBLING 1
#endif

#ifndef CHECK_LOCAL
#define CHECK_LOCAL 1
#endif

67 68 69 70 71 72 73 74
#ifdef HAVE_ADDRINFO
#ifdef HAVE_GETADDRINFO
#ifdef HAVE_GAISTRERROR
#define USE_GETADDRINFO
#endif
#endif
#endif

75
#define CHECK(r) \
76
	do { \
77
		result = (r); \
78 79
		if (result != ISC_R_SUCCESS) \
			goto cleanup; \
Automatic Updater's avatar
Automatic Updater committed
80
	} while (0)
81

Mark Andrews's avatar
Mark Andrews committed
82 83 84 85 86 87 88 89 90
#define ERR_IS_CNAME 1
#define ERR_NO_ADDRESSES 2
#define ERR_LOOKUP_FAILURE 3
#define ERR_EXTRA_A 4
#define ERR_EXTRA_AAAA 5
#define ERR_MISSING_GLUE 5
#define ERR_IS_MXCNAME 6
#define ERR_IS_SRVCNAME 7

91 92 93
static const char *dbtype[] = { "rbt" };

int debug = 0;
94
const char *journal = NULL;
95
isc_boolean_t nomerge = ISC_TRUE;
96
#if CHECK_LOCAL
97 98 99
isc_boolean_t docheckmx = ISC_TRUE;
isc_boolean_t dochecksrv = ISC_TRUE;
isc_boolean_t docheckns = ISC_TRUE;
100 101 102 103 104
#else
isc_boolean_t docheckmx = ISC_FALSE;
isc_boolean_t dochecksrv = ISC_FALSE;
isc_boolean_t docheckns = ISC_FALSE;
#endif
Automatic Updater's avatar
Automatic Updater committed
105
unsigned int zone_options = DNS_ZONEOPT_CHECKNS |
106
			    DNS_ZONEOPT_CHECKMX |
107
			    DNS_ZONEOPT_MANYERRORS |
108
			    DNS_ZONEOPT_CHECKNAMES |
109
			    DNS_ZONEOPT_CHECKINTEGRITY |
110 111 112
#if CHECK_SIBLING
			    DNS_ZONEOPT_CHECKSIBLING |
#endif
113 114 115
			    DNS_ZONEOPT_CHECKWILDCARD |
			    DNS_ZONEOPT_WARNMXCNAME |
			    DNS_ZONEOPT_WARNSRVCNAME;
Evan Hunt's avatar
Evan Hunt committed
116
unsigned int zone_options2 = 0;
117

118 119 120 121 122 123 124 125 126 127 128
/*
 * This needs to match the list in bin/named/log.c.
 */
static isc_logcategory_t categories[] = {
	{ "",		     0 },
	{ "client",	     0 },
	{ "network",	     0 },
	{ "update",	     0 },
	{ "queries",	     0 },
	{ "unmatched", 	     0 },
	{ "update-security", 0 },
129
	{ "query-errors",    0 },
130 131 132
	{ NULL,		     0 }
};

Mark Andrews's avatar
Mark Andrews committed
133 134 135 136 137 138 139 140
static isc_symtab_t *symtab = NULL;
static isc_mem_t *sym_mctx;

static void
freekey(char *key, unsigned int type, isc_symvalue_t value, void *userarg) {
	UNUSED(type);
	UNUSED(value);
	isc_mem_free(userarg, key);
Automatic Updater's avatar
Automatic Updater committed
141
}
Mark Andrews's avatar
Mark Andrews committed
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

static void
add(char *key, int value) {
	isc_result_t result;
	isc_symvalue_t symvalue;

	if (sym_mctx == NULL) {
		result = isc_mem_create(0, 0, &sym_mctx);
		if (result != ISC_R_SUCCESS)
			return;
	}

	if (symtab == NULL) {
		result = isc_symtab_create(sym_mctx, 100, freekey, sym_mctx,
					   ISC_FALSE, &symtab);
		if (result != ISC_R_SUCCESS)
			return;
	}

	key = isc_mem_strdup(sym_mctx, key);
	if (key == NULL)
		return;

	symvalue.as_pointer = NULL;
	result = isc_symtab_define(symtab, key, value, symvalue,
				   isc_symexists_reject);
	if (result != ISC_R_SUCCESS)
		isc_mem_free(sym_mctx, key);
}

static isc_boolean_t
logged(char *key, int value) {
	isc_result_t result;

	if (symtab == NULL)
		return (ISC_FALSE);

	result = isc_symtab_lookup(symtab, key, value, NULL);
	if (result == ISC_R_SUCCESS)
		return (ISC_TRUE);
	return (ISC_FALSE);
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static isc_boolean_t
checkns(dns_zone_t *zone, dns_name_t *name, dns_name_t *owner,
	dns_rdataset_t *a, dns_rdataset_t *aaaa)
{
#ifdef USE_GETADDRINFO
	dns_rdataset_t *rdataset;
	dns_rdata_t rdata = DNS_RDATA_INIT;
	struct addrinfo hints, *ai, *cur;
	char namebuf[DNS_NAME_FORMATSIZE + 1];
	char ownerbuf[DNS_NAME_FORMATSIZE];
	char addrbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123")];
	isc_boolean_t answer = ISC_TRUE;
	isc_boolean_t match;
	const char *type;
	void *ptr = NULL;
	int result;

	REQUIRE(a == NULL || !dns_rdataset_isassociated(a) ||
		a->type == dns_rdatatype_a);
	REQUIRE(aaaa == NULL || !dns_rdataset_isassociated(aaaa) ||
		aaaa->type == dns_rdatatype_aaaa);
Evan Hunt's avatar
Evan Hunt committed
206 207 208 209

	if (a == NULL || aaaa == NULL)
		return (answer);

210 211 212 213 214 215 216 217 218 219 220 221 222
	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	/*
	 * Turn off search.
	 */
	if (dns_name_countlabels(name) > 1U)
		strcat(namebuf, ".");
	dns_name_format(owner, ownerbuf, sizeof(ownerbuf));
Mark Andrews's avatar
Mark Andrews committed
223

224 225 226 227
	result = getaddrinfo(namebuf, NULL, &hints, &ai);
	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	switch (result) {
	case 0:
228 229 230 231 232 233 234 235
		/*
		 * Work around broken getaddrinfo() implementations that
		 * fail to set ai_canonname on first entry.
		 */
		cur = ai;
		while (cur != NULL && cur->ai_canonname == NULL &&
		       cur->ai_next != NULL)
			cur = cur->ai_next;
236 237
		if (cur != NULL && cur->ai_canonname != NULL &&
		    strcasecmp(cur->ai_canonname, namebuf) != 0 &&
Mark Andrews's avatar
Mark Andrews committed
238
		    !logged(namebuf, ERR_IS_CNAME)) {
239
			dns_zone_log(zone, ISC_LOG_ERROR,
240
				     "%s/NS '%s' (out of zone) "
241 242 243
				     "is a CNAME '%s' (illegal)",
				     ownerbuf, namebuf,
				     cur->ai_canonname);
244 245
			/* XXX950 make fatal for 9.5.0 */
			/* answer = ISC_FALSE; */
Mark Andrews's avatar
Mark Andrews committed
246
			add(namebuf, ERR_IS_CNAME);
247 248 249 250 251 252
		}
		break;
	case EAI_NONAME:
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
#endif
Mark Andrews's avatar
Mark Andrews committed
253 254 255 256 257 258 259
		if (!logged(namebuf, ERR_NO_ADDRESSES)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/NS '%s' (out of zone) "
				     "has no addresses records (A or AAAA)",
				     ownerbuf, namebuf);
			add(namebuf, ERR_NO_ADDRESSES);
		}
260 261
		/* XXX950 make fatal for 9.5.0 */
		return (ISC_TRUE);
262 263

	default:
Mark Andrews's avatar
Mark Andrews committed
264 265 266 267 268 269
		if (!logged(namebuf, ERR_LOOKUP_FAILURE)) {
			dns_zone_log(zone, ISC_LOG_WARNING,
				     "getaddrinfo(%s) failed: %s",
				     namebuf, gai_strerror(result));
			add(namebuf, ERR_LOOKUP_FAILURE);
		}
270 271
		return (ISC_TRUE);
	}
Evan Hunt's avatar
Evan Hunt committed
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	/*
	 * Check that all glue records really exist.
	 */
	if (!dns_rdataset_isassociated(a))
		goto checkaaaa;
	result = dns_rdataset_first(a);
	while (result == ISC_R_SUCCESS) {
		dns_rdataset_current(a, &rdata);
		match = ISC_FALSE;
		for (cur = ai; cur != NULL; cur = cur->ai_next) {
			if (cur->ai_family != AF_INET)
				continue;
			ptr = &((struct sockaddr_in *)(cur->ai_addr))->sin_addr;
			if (memcmp(ptr, rdata.data, rdata.length) == 0) {
				match = ISC_TRUE;
				break;
			}
		}
Mark Andrews's avatar
Mark Andrews committed
291
		if (!match && !logged(namebuf, ERR_EXTRA_A)) {
292 293 294 295 296
			dns_zone_log(zone, ISC_LOG_ERROR, "%s/NS '%s' "
				     "extra GLUE A record (%s)",
				     ownerbuf, namebuf,
				     inet_ntop(AF_INET, rdata.data,
					       addrbuf, sizeof(addrbuf)));
Mark Andrews's avatar
Mark Andrews committed
297
			add(namebuf, ERR_EXTRA_A);
298 299
			/* XXX950 make fatal for 9.5.0 */
			/* answer = ISC_FALSE; */
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
		}
		dns_rdata_reset(&rdata);
		result = dns_rdataset_next(a);
	}

 checkaaaa:
	if (!dns_rdataset_isassociated(aaaa))
		goto checkmissing;
	result = dns_rdataset_first(aaaa);
	while (result == ISC_R_SUCCESS) {
		dns_rdataset_current(aaaa, &rdata);
		match = ISC_FALSE;
		for (cur = ai; cur != NULL; cur = cur->ai_next) {
			if (cur->ai_family != AF_INET6)
				continue;
			ptr = &((struct sockaddr_in6 *)(cur->ai_addr))->sin6_addr;
			if (memcmp(ptr, rdata.data, rdata.length) == 0) {
				match = ISC_TRUE;
				break;
			}
		}
Mark Andrews's avatar
Mark Andrews committed
321
		if (!match && !logged(namebuf, ERR_EXTRA_AAAA)) {
322 323 324 325 326
			dns_zone_log(zone, ISC_LOG_ERROR, "%s/NS '%s' "
				     "extra GLUE AAAA record (%s)",
				     ownerbuf, namebuf,
				     inet_ntop(AF_INET6, rdata.data,
					       addrbuf, sizeof(addrbuf)));
Mark Andrews's avatar
Mark Andrews committed
327
			add(namebuf, ERR_EXTRA_AAAA);
328 329
			/* XXX950 make fatal for 9.5.0. */
			/* answer = ISC_FALSE; */
330 331 332 333 334 335 336 337 338
		}
		dns_rdata_reset(&rdata);
		result = dns_rdataset_next(aaaa);
	}

 checkmissing:
	/*
	 * Check that all addresses appear in the glue.
	 */
Mark Andrews's avatar
Mark Andrews committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	if (!logged(namebuf, ERR_MISSING_GLUE)) {
		isc_boolean_t missing_glue = ISC_FALSE;
		for (cur = ai; cur != NULL; cur = cur->ai_next) {
			switch (cur->ai_family) {
			case AF_INET:
				rdataset = a;
				ptr = &((struct sockaddr_in *)(cur->ai_addr))->sin_addr;
				type = "A";
				break;
			case AF_INET6:
				rdataset = aaaa;
				ptr = &((struct sockaddr_in6 *)(cur->ai_addr))->sin6_addr;
				type = "AAAA";
				break;
			default:
				 continue;
			}
			match = ISC_FALSE;
			if (dns_rdataset_isassociated(rdataset))
				result = dns_rdataset_first(rdataset);
			else
				result = ISC_R_FAILURE;
			while (result == ISC_R_SUCCESS && !match) {
				dns_rdataset_current(rdataset, &rdata);
				if (memcmp(ptr, rdata.data, rdata.length) == 0)
					match = ISC_TRUE;
				dns_rdata_reset(&rdata);
				result = dns_rdataset_next(rdataset);
			}
			if (!match) {
				dns_zone_log(zone, ISC_LOG_ERROR, "%s/NS '%s' "
					     "missing GLUE %s record (%s)",
					     ownerbuf, namebuf, type,
					     inet_ntop(cur->ai_family, ptr,
						       addrbuf, sizeof(addrbuf)));
				/* XXX950 make fatal for 9.5.0. */
				/* answer = ISC_FALSE; */
				missing_glue = ISC_TRUE;
			}
378
		}
Mark Andrews's avatar
Mark Andrews committed
379 380
		if (missing_glue)
			add(namebuf, ERR_MISSING_GLUE);
381 382 383 384 385 386 387 388 389 390 391
	}
	freeaddrinfo(ai);
	return (answer);
#else
	return (ISC_TRUE);
#endif
}

static isc_boolean_t
checkmx(dns_zone_t *zone, dns_name_t *name, dns_name_t *owner) {
#ifdef USE_GETADDRINFO
392
	struct addrinfo hints, *ai, *cur;
393 394 395
	char namebuf[DNS_NAME_FORMATSIZE + 1];
	char ownerbuf[DNS_NAME_FORMATSIZE];
	int result;
396 397
	int level = ISC_LOG_ERROR;
	isc_boolean_t answer = ISC_TRUE;
398 399 400 401 402 403 404 405 406 407 408 409 410 411

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	/*
	 * Turn off search.
	 */
	if (dns_name_countlabels(name) > 1U)
		strcat(namebuf, ".");
	dns_name_format(owner, ownerbuf, sizeof(ownerbuf));
Automatic Updater's avatar
Automatic Updater committed
412

413 414 415 416
	result = getaddrinfo(namebuf, NULL, &hints, &ai);
	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	switch (result) {
	case 0:
417 418 419 420 421 422 423 424 425 426
		/*
		 * Work around broken getaddrinfo() implementations that
		 * fail to set ai_canonname on first entry.
		 */
		cur = ai;
		while (cur != NULL && cur->ai_canonname == NULL &&
		       cur->ai_next != NULL)
			cur = cur->ai_next;
		if (cur != NULL && cur->ai_canonname != NULL &&
		    strcasecmp(cur->ai_canonname, namebuf) != 0) {
427 428 429
			if ((zone_options & DNS_ZONEOPT_WARNMXCNAME) != 0)
				level = ISC_LOG_WARNING;
			if ((zone_options & DNS_ZONEOPT_IGNOREMXCNAME) == 0) {
Mark Andrews's avatar
Mark Andrews committed
430 431 432
				if (!logged(namebuf, ERR_IS_MXCNAME)) {
					dns_zone_log(zone, level,
						     "%s/MX '%s' (out of zone)"
433 434 435 436
						     " is a CNAME '%s' "
						     "(illegal)",
						     ownerbuf, namebuf,
						     cur->ai_canonname);
Mark Andrews's avatar
Mark Andrews committed
437 438
					add(namebuf, ERR_IS_MXCNAME);
				}
439 440 441 442
				if (level == ISC_LOG_ERROR)
					answer = ISC_FALSE;
			}
		}
443
		freeaddrinfo(ai);
444 445
		return (answer);

446 447 448 449
	case EAI_NONAME:
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
#endif
Mark Andrews's avatar
Mark Andrews committed
450 451 452 453 454 455 456
		if (!logged(namebuf, ERR_NO_ADDRESSES)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/MX '%s' (out of zone) "
				     "has no addresses records (A or AAAA)",
				     ownerbuf, namebuf);
			add(namebuf, ERR_NO_ADDRESSES);
		}
457 458
		/* XXX950 make fatal for 9.5.0. */
		return (ISC_TRUE);
459 460

	default:
Mark Andrews's avatar
Mark Andrews committed
461 462
		if (!logged(namebuf, ERR_LOOKUP_FAILURE)) {
			dns_zone_log(zone, ISC_LOG_WARNING,
463 464
			     "getaddrinfo(%s) failed: %s",
			     namebuf, gai_strerror(result));
Mark Andrews's avatar
Mark Andrews committed
465 466
			add(namebuf, ERR_LOOKUP_FAILURE);
		}
467 468
		return (ISC_TRUE);
	}
Mark Andrews's avatar
Mark Andrews committed
469
#else
470
	return (ISC_TRUE);
Mark Andrews's avatar
Mark Andrews committed
471
#endif
472 473 474 475 476
}

static isc_boolean_t
checksrv(dns_zone_t *zone, dns_name_t *name, dns_name_t *owner) {
#ifdef USE_GETADDRINFO
477
	struct addrinfo hints, *ai, *cur;
478 479 480
	char namebuf[DNS_NAME_FORMATSIZE + 1];
	char ownerbuf[DNS_NAME_FORMATSIZE];
	int result;
481 482
	int level = ISC_LOG_ERROR;
	isc_boolean_t answer = ISC_TRUE;
483 484 485 486 487 488 489 490 491 492 493 494 495 496

	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	/*
	 * Turn off search.
	 */
	if (dns_name_countlabels(name) > 1U)
		strcat(namebuf, ".");
	dns_name_format(owner, ownerbuf, sizeof(ownerbuf));
Automatic Updater's avatar
Automatic Updater committed
497

498 499 500 501
	result = getaddrinfo(namebuf, NULL, &hints, &ai);
	dns_name_format(name, namebuf, sizeof(namebuf) - 1);
	switch (result) {
	case 0:
502 503 504 505 506 507 508 509 510 511
		/*
		 * Work around broken getaddrinfo() implementations that
		 * fail to set ai_canonname on first entry.
		 */
		cur = ai;
		while (cur != NULL && cur->ai_canonname == NULL &&
		       cur->ai_next != NULL)
			cur = cur->ai_next;
		if (cur != NULL && cur->ai_canonname != NULL &&
		    strcasecmp(cur->ai_canonname, namebuf) != 0) {
512 513 514
			if ((zone_options & DNS_ZONEOPT_WARNSRVCNAME) != 0)
				level = ISC_LOG_WARNING;
			if ((zone_options & DNS_ZONEOPT_IGNORESRVCNAME) == 0) {
Mark Andrews's avatar
Mark Andrews committed
515 516 517
				if (!logged(namebuf, ERR_IS_SRVCNAME)) {
					dns_zone_log(zone, level, "%s/SRV '%s'"
						     " (out of zone) is a "
518 519 520
						     "CNAME '%s' (illegal)",
						     ownerbuf, namebuf,
						     cur->ai_canonname);
Mark Andrews's avatar
Mark Andrews committed
521 522
					add(namebuf, ERR_IS_SRVCNAME);
				}
523 524 525 526
				if (level == ISC_LOG_ERROR)
					answer = ISC_FALSE;
			}
		}
527
		freeaddrinfo(ai);
528 529
		return (answer);

530 531 532 533
	case EAI_NONAME:
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
	case EAI_NODATA:
#endif
Mark Andrews's avatar
Mark Andrews committed
534 535 536 537 538 539 540
		if (!logged(namebuf, ERR_NO_ADDRESSES)) {
			dns_zone_log(zone, ISC_LOG_ERROR,
				     "%s/SRV '%s' (out of zone) "
				     "has no addresses records (A or AAAA)",
				     ownerbuf, namebuf);
			add(namebuf, ERR_NO_ADDRESSES);
		}
541 542
		/* XXX950 make fatal for 9.5.0. */
		return (ISC_TRUE);
543 544

	default:
Mark Andrews's avatar
Mark Andrews committed
545 546 547 548 549 550
		if (!logged(namebuf, ERR_LOOKUP_FAILURE)) {
			dns_zone_log(zone, ISC_LOG_WARNING,
				     "getaddrinfo(%s) failed: %s",
				     namebuf, gai_strerror(result));
			add(namebuf, ERR_LOOKUP_FAILURE);
		}
551 552
		return (ISC_TRUE);
	}
Mark Andrews's avatar
Mark Andrews committed
553
#else
554
	return (ISC_TRUE);
Mark Andrews's avatar
Mark Andrews committed
555
#endif
556 557
}

Mark Andrews's avatar
Mark Andrews committed
558
isc_result_t
559
setup_logging(isc_mem_t *mctx, FILE *errout, isc_log_t **logp) {
Mark Andrews's avatar
Mark Andrews committed
560 561 562 563 564
	isc_logdestination_t destination;
	isc_logconfig_t *logconfig = NULL;
	isc_log_t *log = NULL;

	RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
565
	isc_log_registercategories(log, categories);
Mark Andrews's avatar
Mark Andrews committed
566
	isc_log_setcontext(log);
567
	dns_log_init(log);
568
	dns_log_setcontext(log);
569
	cfg_log_init(log);
Mark Andrews's avatar
Mark Andrews committed
570

571
	destination.file.stream = errout;
Mark Andrews's avatar
Mark Andrews committed
572 573 574 575 576 577 578 579 580 581 582 583 584
	destination.file.name = NULL;
	destination.file.versions = ISC_LOG_ROLLNEVER;
	destination.file.maximum_size = 0;
	RUNTIME_CHECK(isc_log_createchannel(logconfig, "stderr",
				       ISC_LOG_TOFILEDESC,
				       ISC_LOG_DYNAMIC,
				       &destination, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
					 NULL, NULL) == ISC_R_SUCCESS);

	*logp = log;
	return (ISC_R_SUCCESS);
}
585

Evan Hunt's avatar
Evan Hunt committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
/*% scan the zone for oversize TTLs */
static isc_result_t
check_ttls(dns_zone_t *zone, dns_ttl_t maxttl) {
	isc_result_t result;
	dns_db_t *db = NULL;
	dns_dbversion_t *version = NULL;
	dns_dbnode_t *node = NULL;
	dns_dbiterator_t *dbiter = NULL;
	dns_rdatasetiter_t *rdsiter = NULL;
	dns_rdataset_t rdataset;
	dns_fixedname_t fname;
	dns_name_t *name;
	dns_fixedname_init(&fname);
	name = dns_fixedname_name(&fname);
	dns_rdataset_init(&rdataset);

	dns_zone_getdb(zone, &db);
	INSIST(db != NULL);

	CHECK(dns_db_newversion(db, &version));
	CHECK(dns_db_createiterator(db, 0, &dbiter));

	for (result = dns_dbiterator_first(dbiter);
	     result == ISC_R_SUCCESS;
	     result = dns_dbiterator_next(dbiter)) {
		result = dns_dbiterator_current(dbiter, &node, name);
		if (result == DNS_R_NEWORIGIN)
			result = ISC_R_SUCCESS;
		CHECK(result);

		CHECK(dns_db_allrdatasets(db, node, version, 0, &rdsiter));
		for (result = dns_rdatasetiter_first(rdsiter);
		     result == ISC_R_SUCCESS;
		     result = dns_rdatasetiter_next(rdsiter)) {
			dns_rdatasetiter_current(rdsiter, &rdataset);
			if (rdataset.ttl > maxttl) {
				char nbuf[DNS_NAME_FORMATSIZE];
				char tbuf[255];
				isc_buffer_t b;
				isc_region_t r;

				dns_name_format(name, nbuf, sizeof(nbuf));
				isc_buffer_init(&b, tbuf, sizeof(tbuf) - 1);
				CHECK(dns_rdatatype_totext(rdataset.type, &b));
				isc_buffer_usedregion(&b, &r);
				r.base[r.length] = 0;

				dns_zone_log(zone, ISC_LOG_ERROR,
					     "%s/%s TTL %d exceeds "
					     "maximum TTL %d",
					     nbuf, tbuf, rdataset.ttl, maxttl);
				dns_rdataset_disassociate(&rdataset);
				CHECK(ISC_R_RANGE);
			}
			dns_rdataset_disassociate(&rdataset);
		}
		if (result == ISC_R_NOMORE)
			result = ISC_R_SUCCESS;
		CHECK(result);
Mark Andrews's avatar
Mark Andrews committed
645

Evan Hunt's avatar
Evan Hunt committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
		dns_rdatasetiter_destroy(&rdsiter);
		dns_db_detachnode(db, &node);
	}

	if (result == ISC_R_NOMORE)
		result = ISC_R_SUCCESS;

 cleanup:
	if (node != NULL)
		dns_db_detachnode(db, &node);
	if (rdsiter != NULL)
		dns_rdatasetiter_destroy(&rdsiter);
	if (dbiter != NULL)
		dns_dbiterator_destroy(&dbiter);
	if (version != NULL)
		dns_db_closeversion(db, &version, ISC_FALSE);
	if (db != NULL)
		dns_db_detach(&db);

	return (result);
}

668
/*% load the zone */
669 670
isc_result_t
load_zone(isc_mem_t *mctx, const char *zonename, const char *filename,
671
	  dns_masterformat_t fileformat, const char *classname,
Evan Hunt's avatar
Evan Hunt committed
672
	  dns_ttl_t maxttl, dns_zone_t **zonep)
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
{
	isc_result_t result;
	dns_rdataclass_t rdclass;
	isc_textregion_t region;
	isc_buffer_t buffer;
	dns_fixedname_t fixorigin;
	dns_name_t *origin;
	dns_zone_t *zone = NULL;

	REQUIRE(zonep == NULL || *zonep == NULL);

	if (debug)
		fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
			zonename, filename, classname);

	CHECK(dns_zone_create(&zone, mctx));

	dns_zone_settype(zone, dns_zone_master);

692
	isc_buffer_constinit(&buffer, zonename, strlen(zonename));
693 694 695
	isc_buffer_add(&buffer, strlen(zonename));
	dns_fixedname_init(&fixorigin);
	origin = dns_fixedname_name(&fixorigin);
696
	CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
697 698
	CHECK(dns_zone_setorigin(zone, origin));
	CHECK(dns_zone_setdbtype(zone, 1, (const char * const *) dbtype));
699
	CHECK(dns_zone_setfile2(zone, filename, fileformat));
700 701
	if (journal != NULL)
		CHECK(dns_zone_setjournal(zone, journal));
702 703 704 705 706 707

	DE_CONST(classname, region.base);
	region.length = strlen(classname);
	CHECK(dns_rdataclass_fromtext(&rdclass, &region));

	dns_zone_setclass(zone, rdclass);
708
	dns_zone_setoption(zone, zone_options, ISC_TRUE);
Evan Hunt's avatar
Evan Hunt committed
709
	dns_zone_setoption2(zone, zone_options2, ISC_TRUE);
710
	dns_zone_setoption(zone, DNS_ZONEOPT_NOMERGE, nomerge);
Evan Hunt's avatar
Evan Hunt committed
711 712 713

	dns_zone_setmaxttl(zone, maxttl);

714 715 716 717 718 719
	if (docheckmx)
		dns_zone_setcheckmx(zone, checkmx);
	if (docheckns)
		dns_zone_setcheckns(zone, checkns);
	if (dochecksrv)
		dns_zone_setchecksrv(zone, checksrv);
720 721

	CHECK(dns_zone_load(zone));
Evan Hunt's avatar
Evan Hunt committed
722 723 724 725 726 727 728 729 730

	/*
	 * When loading map files we can't catch oversize TTLs during
	 * load, so we check for them here.
	 */
	if (fileformat == dns_masterformat_map && maxttl != 0) {
		CHECK(check_ttls(zone, maxttl));
	}

731
	if (zonep != NULL) {
732 733 734 735 736 737 738 739 740
		*zonep = zone;
		zone = NULL;
	}

 cleanup:
	if (zone != NULL)
		dns_zone_detach(&zone);
	return (result);
}
741

742
/*% dump the zone */
743
isc_result_t
744
dump_zone(const char *zonename, dns_zone_t *zone, const char *filename,
745 746
	  dns_masterformat_t fileformat, const dns_master_style_t *style,
	  const isc_uint32_t rawversion)
747 748 749
{
	isc_result_t result;
	FILE *output = stdout;
750 751 752
	const char *flags;

	flags = (fileformat == dns_masterformat_text) ? "w+" : "wb+";
753 754

	if (debug) {
755
		if (filename != NULL && strcmp(filename, "-") != 0)
756 757 758 759 760 761
			fprintf(stderr, "dumping \"%s\" to \"%s\"\n",
				zonename, filename);
		else
			fprintf(stderr, "dumping \"%s\"\n", zonename);
	}

762
	if (filename != NULL && strcmp(filename, "-") != 0) {
763
		result = isc_stdio_open(filename, flags, &output);
764 765 766 767 768 769 770 771

		if (result != ISC_R_SUCCESS) {
			fprintf(stderr, "could not open output "
				"file \"%s\" for writing\n", filename);
			return (ISC_R_FAILURE);
		}
	}

772 773
	result = dns_zone_dumptostream3(zone, output, fileformat, style,
					rawversion);
774
	if (output != stdout)
775 776 777 778
		(void)isc_stdio_close(output);

	return (result);
}
779 780 781 782

#ifdef _WIN32
void
InitSockets(void) {
Automatic Updater's avatar
Automatic Updater committed
783 784 785
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
786

Automatic Updater's avatar
Automatic Updater committed
787
	wVersionRequested = MAKEWORD(2, 0);
788

Automatic Updater's avatar
Automatic Updater committed
789 790 791 792 793
	err = WSAStartup( wVersionRequested, &wsaData );
	if (err != 0) {
		fprintf(stderr, "WSAStartup() failed: %d\n", err);
		exit(1);
	}
794 795 796 797
}

void
DestroySockets(void) {
Automatic Updater's avatar
Automatic Updater committed
798
	WSACleanup();
799 800 801
}
#endif