master.c 74.1 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
Mark Andrews's avatar
Mark Andrews committed
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
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
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id: master.c,v 1.157 2005/08/23 04:05:49 marka Exp $ */
19 20

/*! \file */
Mark Andrews's avatar
Mark Andrews committed
21 22 23

#include <config.h>

24
#include <isc/event.h>
25
#include <isc/lex.h>
26
#include <isc/magic.h>
27
#include <isc/mem.h>
28
#include <isc/print.h>
29
#include <isc/serial.h>
30
#include <isc/stdio.h>
31
#include <isc/stdtime.h>
32
#include <isc/string.h>
33
#include <isc/task.h>
Bob Halley's avatar
Bob Halley committed
34
#include <isc/util.h>
35

36
#include <dns/callbacks.h>
37 38
#include <dns/events.h>
#include <dns/fixedname.h>
39
#include <dns/master.h>
40 41 42
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
43 44
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
45
#include <dns/rdatastruct.h>
46
#include <dns/rdatatype.h>
47
#include <dns/result.h>
48
#include <dns/soa.h>
49
#include <dns/time.h>
50
#include <dns/ttl.h>
51

52 53
/*!
 * Grow the number of dns_rdatalist_t (#RDLSZ) and dns_rdata_t (#RDSZ) structures
54 55
 * by these sizes when we need to.
 *
56 57 58 59
 */
/*% RDLSZ reflects the number of different types with the same name expected. */
#define RDLSZ 32
/*%
60
 * RDSZ reflects the number of rdata expected at a give name that can fit into
61
 * 64k.
62 63 64
 */
#define RDSZ 512

Mark Andrews's avatar
Mark Andrews committed
65
#define NBUFS 4
66 67
#define MAXWIRESZ 255

68
/*%
Bob Halley's avatar
Bob Halley committed
69
 * Target buffer size and minimum target size.
Andreas Gustafsson's avatar
Andreas Gustafsson committed
70
 * MINTSIZ must be big enough to hold the largest rdata record.
71
 * \brief
72 73 74
 * TSIZ >= MINTSIZ
 */
#define TSIZ (128*1024)
75
/*%
76 77 78
 * max message size - header - root - type - class - ttl - rdlen
 */
#define MINTSIZ (65535 - 12 - 1 - 2 - 2 - 4 - 2)
79
/*%
80
 * Size for tokens in the presentation format,
81
 * The largest tokens are the base64 blocks in KEY and CERT records,
82
 * Largest key allowed is about 1372 bytes but
Andreas Gustafsson's avatar
typos  
Andreas Gustafsson committed
83 84
 * there is no fixed upper bound on CERT records.
 * 2K is too small for some X.509s, 8K is overkill.
85 86
 */
#define TOKENSIZ (8*1024)
87

88 89
#define DNS_MASTER_BUFSZ 2048

90 91
typedef ISC_LIST(dns_rdatalist_t) rdatalist_head_t;

92 93
typedef struct dns_incctx dns_incctx_t;

94
/*%
95
 * Master file load state.
96
 */
97

98
struct dns_loadctx {
99
	unsigned int		magic;
100
	isc_mem_t		*mctx;
101 102
	dns_masterformat_t	format;

103 104 105 106
	dns_rdatacallbacks_t	*callbacks;
	isc_task_t		*task;
	dns_loaddonefunc_t	done;
	void			*done_arg;
107 108 109 110 111 112 113 114 115

	/* Common methods */
	isc_result_t		(*openfile)(dns_loadctx_t *lctx,
					    const char *filename);
	isc_result_t		(*load)(dns_loadctx_t *lctx);

	/* Members specific to the text format: */
	isc_lex_t		*lex;
	isc_boolean_t		keep_lex;
116
	unsigned int		options;
117 118 119
	isc_boolean_t		ttl_known;
	isc_boolean_t		default_ttl_known;
	isc_boolean_t		warn_1035;
120
	isc_boolean_t		warn_tcr;
121
	isc_boolean_t		warn_sigexpired;
122
	isc_boolean_t		seen_include;
123 124 125
	isc_uint32_t		ttl;
	isc_uint32_t		default_ttl;
	dns_rdataclass_t	zclass;
126
	dns_fixedname_t		fixed_top;
127
	dns_name_t		*top;			/*%< top of zone */
128 129 130 131 132

	/* Members specific to the raw format: */
	FILE			*f;
	isc_boolean_t		first;

133
	/* Which fixed buffers we are using? */
134
	unsigned int		loop_cnt;		/*% records per quantum,
135
							 * 0 => all. */
136
	isc_boolean_t		canceled;
137
	isc_mutex_t		lock;
138
	isc_result_t		result;
139 140
	/* locked by lock */
	isc_uint32_t		references;
141 142 143 144 145 146 147 148 149 150 151 152 153
	dns_incctx_t		*inc;
};

struct dns_incctx {
	dns_incctx_t		*parent;
	dns_name_t		*origin;
	dns_name_t		*current;
	dns_name_t		*glue;
	dns_fixedname_t		fixed[NBUFS];		/* working buffers */
	unsigned int		in_use[NBUFS];		/* covert to bitmap? */
	int			glue_in_use;
	int			current_in_use;
	int			origin_in_use;
154 155 156
	isc_boolean_t		drop;
	unsigned int		glue_line;
	unsigned int		current_line;
157 158 159
};

#define DNS_LCTX_MAGIC ISC_MAGIC('L','c','t','x')
160
#define DNS_LCTX_VALID(lctx) ISC_MAGIC_VALID(lctx, DNS_LCTX_MAGIC)
161

162 163
#define DNS_AS_STR(t) ((t).value.as_textregion.base)

164 165 166 167 168 169 170 171 172 173 174 175
static isc_result_t
openfile_text(dns_loadctx_t *lctx, const char *master_file);

static isc_result_t
openfile_raw(dns_loadctx_t *lctx, const char *master_file);

static isc_result_t
load_text(dns_loadctx_t *lctx);

static isc_result_t
load_raw(dns_loadctx_t *lctx);

176
static isc_result_t
177
pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx);
178 179

static isc_result_t
180
commit(dns_rdatacallbacks_t *, dns_loadctx_t *, rdatalist_head_t *,
181
       dns_name_t *, const char *, unsigned int);
182 183 184 185 186 187 188 189 190 191 192 193

static isc_boolean_t
is_glue(rdatalist_head_t *, dns_name_t *);

static dns_rdatalist_t *
grow_rdatalist(int, dns_rdatalist_t *, int, rdatalist_head_t *,
		rdatalist_head_t *, isc_mem_t *mctx);

static dns_rdata_t *
grow_rdata(int, dns_rdata_t *, int, rdatalist_head_t *, rdatalist_head_t *,
	   isc_mem_t *);

194 195 196 197
static void
load_quantum(isc_task_t *task, isc_event_t *event);

static isc_result_t
198
task_send(dns_loadctx_t *lctx);
199 200

static void
201
loadctx_destroy(dns_loadctx_t *lctx);
202

Mark Andrews's avatar
Mark Andrews committed
203 204
#define GETTOKEN(lexer, options, token, eol) \
	do { \
205 206
		result = gettoken(lexer, options, token, eol, callbacks); \
		switch (result) { \
207
		case ISC_R_SUCCESS: \
Mark Andrews's avatar
Mark Andrews committed
208
			break; \
209
		case ISC_R_UNEXPECTED: \
Mark Andrews's avatar
Mark Andrews committed
210
			goto insist_and_cleanup; \
Mark Andrews's avatar
Mark Andrews committed
211
		default: \
212 213
			if (MANYERRS(lctx, result)) { \
				SETRESULT(lctx, result); \
214 215 216 217 218
				LOGIT(result); \
				read_till_eol = ISC_TRUE; \
				goto next_line; \
			} else \
				goto log_and_cleanup; \
Mark Andrews's avatar
Mark Andrews committed
219
		} \
220 221
		if ((token)->type == isc_tokentype_special) { \
			result = DNS_R_SYNTAX; \
222 223
			if (MANYERRS(lctx, result)) { \
				SETRESULT(lctx, result); \
224 225 226 227 228
				LOGIT(result); \
				read_till_eol = ISC_TRUE; \
				goto next_line; \
			} else \
				goto log_and_cleanup; \
229
		} \
230 231
	} while (0)

232 233
#define COMMITALL \
	do { \
234
		result = commit(callbacks, lctx, &current_list, \
235
				ictx->current, source, ictx->current_line); \
236 237
		if (MANYERRS(lctx, result)) { \
			SETRESULT(lctx, result); \
238
		} else if (result != ISC_R_SUCCESS) \
239
			goto insist_and_cleanup; \
240
		result = commit(callbacks, lctx, &glue_list, \
241
				ictx->glue, source, ictx->glue_line); \
242 243
		if (MANYERRS(lctx, result)) { \
			SETRESULT(lctx, result); \
244
		} else if (result != ISC_R_SUCCESS) \
245
			goto insist_and_cleanup; \
246 247 248 249 250 251 252
		rdcount = 0; \
		rdlcount = 0; \
		isc_buffer_init(&target, target_mem, target_size); \
		rdcount_save = rdcount; \
		rdlcount_save = rdlcount; \
	} while (0)

253 254 255 256
#define WARNUNEXPECTEDEOF(lexer) \
	do { \
		if (isc_lex_isfile(lexer)) \
			(*callbacks->warn)(callbacks, \
Mark Andrews's avatar
Mark Andrews committed
257 258
				"%s: file does not end with newline", \
				source); \
259
	} while (0)
Mark Andrews's avatar
Mark Andrews committed
260

Mark Andrews's avatar
redo:  
Mark Andrews committed
261 262
#define EXPECTEOL \
	do { \
263
		GETTOKEN(lctx->lex, 0, &token, ISC_TRUE); \
Mark Andrews's avatar
redo:  
Mark Andrews committed
264
		if (token.type != isc_tokentype_eol) { \
265
			isc_lex_ungettoken(lctx->lex, &token); \
Mark Andrews's avatar
redo:  
Mark Andrews committed
266
			result = DNS_R_EXTRATOKEN; \
267 268
			if (MANYERRS(lctx, result)) { \
				SETRESULT(lctx, result); \
Mark Andrews's avatar
redo:  
Mark Andrews committed
269 270 271 272 273 274 275 276
				LOGIT(result); \
				read_till_eol = ISC_TRUE; \
				continue; \
			} else if (result != ISC_R_SUCCESS) \
				goto log_and_cleanup; \
		} \
	} while (0)

277
#define MANYERRS(lctx, result) \
278
		((result != ISC_R_SUCCESS) && \
279
		((lctx)->options & DNS_MASTER_MANYERRORS) != 0)
280

281
#define SETRESULT(lctx, r) \
282
		do { \
283 284
			if ((lctx)->result == ISC_R_SUCCESS) \
				(lctx)->result = r; \
285
		} while (0)
286 287 288 289 290 291 292 293 294 295

#define LOGITFILE(result, filename) \
	if (result == ISC_R_INVALIDFILE || result == ISC_R_FILENOTFOUND || \
	    result == ISC_R_IOERROR || result == ISC_R_TOOMANYOPENFILES || \
	    result == ISC_R_NOPERM) \
		(*callbacks->error)(callbacks, "%s: %s:%lu: %s: %s", \
				    "dns_master_load", source, line, \
				    filename, dns_result_totext(result)); \
	else LOGIT(result)

296 297 298 299 300 301 302
#define LOGIT(result) \
	if (result == ISC_R_NOMEMORY) \
		(*callbacks->error)(callbacks, "dns_master_load: %s", \
				    dns_result_totext(result)); \
	else \
		(*callbacks->error)(callbacks, "%s: %s:%lu: %s", \
				    "dns_master_load", \
303
				    source, line, dns_result_totext(result))
304

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

static unsigned char in_addr_arpa_data[]  = "\007IN-ADDR\004ARPA";
static unsigned char in_addr_arpa_offsets[] = { 0, 8, 13 };
static const dns_name_t in_addr_arpa =
{
	DNS_NAME_MAGIC,
	in_addr_arpa_data, 14, 3,
	DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
	in_addr_arpa_offsets, NULL,
	{(void *)-1, (void *)-1},
	{NULL, NULL}
};

static unsigned char ip6_int_data[]  = "\003IP6\003INT";
static unsigned char ip6_int_offsets[] = { 0, 4, 8 };
static const dns_name_t ip6_int =
{
	DNS_NAME_MAGIC,
	ip6_int_data, 9, 3,
	DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
	ip6_int_offsets, NULL,
	{(void *)-1, (void *)-1},
	{NULL, NULL}
};

static unsigned char ip6_arpa_data[]  = "\003IP6\004ARPA";
static unsigned char ip6_arpa_offsets[] = { 0, 4, 9 };
static const dns_name_t ip6_arpa =
{
	DNS_NAME_MAGIC,
	ip6_arpa_data, 10, 3,
	DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
	ip6_arpa_offsets, NULL,
	{(void *)-1, (void *)-1},
	{NULL, NULL}
};


343
static inline isc_result_t
Mark Andrews's avatar
Mark Andrews committed
344
gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *token,
Bob Halley's avatar
Bob Halley committed
345
	 isc_boolean_t eol, dns_rdatacallbacks_t *callbacks)
Mark Andrews's avatar
Mark Andrews committed
346 347 348
{
	isc_result_t result;

Andreas Gustafsson's avatar
Andreas Gustafsson committed
349 350
	options |= ISC_LEXOPT_EOL | ISC_LEXOPT_EOF | ISC_LEXOPT_DNSMULTILINE |
		ISC_LEXOPT_ESCAPE;
Mark Andrews's avatar
Mark Andrews committed
351 352 353 354
	result = isc_lex_gettoken(lex, options, token);
	if (result != ISC_R_SUCCESS) {
		switch (result) {
		case ISC_R_NOMEMORY:
355
			return (ISC_R_NOMEMORY);
Mark Andrews's avatar
Mark Andrews committed
356
		default:
357 358 359 360 361 362 363
			(*callbacks->error)(callbacks,
					    "dns_master_load: %s:%lu:"
					    " isc_lex_gettoken() failed: %s",
					    isc_lex_getsourcename(lex),
					    isc_lex_getsourceline(lex),
					    isc_result_totext(result));
			return (result);
Mark Andrews's avatar
Mark Andrews committed
364 365 366 367 368 369 370
		}
		/*NOTREACHED*/
	}
	if (eol != ISC_TRUE)
		if (token->type == isc_tokentype_eol ||
		    token->type == isc_tokentype_eof) {
			(*callbacks->error)(callbacks,
371
			    "dns_master_load: %s:%lu: unexpected end of %s",
Bob Halley's avatar
Bob Halley committed
372 373
					    isc_lex_getsourcename(lex),
					    isc_lex_getsourceline(lex),
374 375
					    (token->type ==
					     isc_tokentype_eol) ?
Bob Halley's avatar
Bob Halley committed
376
					    "line" : "file");
377
			return (ISC_R_UNEXPECTEDEND);
Mark Andrews's avatar
Mark Andrews committed
378
		}
379
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
380
}
Mark Andrews's avatar
Mark Andrews committed
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

void
dns_loadctx_attach(dns_loadctx_t *source, dns_loadctx_t **target) {

	REQUIRE(target != NULL && *target == NULL);
	REQUIRE(DNS_LCTX_VALID(source));

	LOCK(&source->lock);
	INSIST(source->references > 0);
	source->references++;
	INSIST(source->references != 0);	/* Overflow? */
	UNLOCK(&source->lock);

	*target = source;
}

void
399 400
dns_loadctx_detach(dns_loadctx_t **lctxp) {
	dns_loadctx_t *lctx;
401
	isc_boolean_t need_destroy = ISC_FALSE;
402

403 404 405
	REQUIRE(lctxp != NULL);
	lctx = *lctxp;
	REQUIRE(DNS_LCTX_VALID(lctx));
406

407 408 409 410
	LOCK(&lctx->lock);
	INSIST(lctx->references > 0);
	lctx->references--;
	if (lctx->references == 0)
411
		need_destroy = ISC_TRUE;
412
	UNLOCK(&lctx->lock);
413 414

	if (need_destroy)
415 416 417 418 419 420 421 422 423 424 425 426
		loadctx_destroy(lctx);
	*lctxp = NULL;
}

static void
incctx_destroy(isc_mem_t *mctx, dns_incctx_t *ictx) {
	dns_incctx_t *parent;

 again:
	parent = ictx->parent;
	ictx->parent = NULL;

Andreas Gustafsson's avatar
Andreas Gustafsson committed
427
	isc_mem_put(mctx, ictx, sizeof(*ictx));
428 429 430 431 432

	if (parent != NULL) {
		ictx = parent;
		goto again;
	}
433 434 435
}

static void
436
loadctx_destroy(dns_loadctx_t *lctx) {
437
	isc_mem_t *mctx;
438
	isc_result_t result;
439

440
	REQUIRE(DNS_LCTX_VALID(lctx));
441

442 443 444
	lctx->magic = 0;
	if (lctx->inc != NULL)
		incctx_destroy(lctx->mctx, lctx->inc);
445

446 447 448 449 450 451 452 453 454
	if (lctx->f != NULL) {
		result = isc_stdio_close(lctx->f);
		if (result != ISC_R_SUCCESS) {
			UNEXPECTED_ERROR(__FILE__, __LINE__,
					 "isc_stdio_close() failed: %s",
					 isc_result_totext(result));
		}
	}

455
	/* isc_lex_destroy() will close all open streams */
456
	if (lctx->lex != NULL && !lctx->keep_lex)
457
		isc_lex_destroy(&lctx->lex);
458

459 460 461
	if (lctx->task != NULL)
		isc_task_detach(&lctx->task);
	DESTROYLOCK(&lctx->lock);
462
	mctx = NULL;
463 464 465
	isc_mem_attach(lctx->mctx, &mctx);
	isc_mem_detach(&lctx->mctx);
	isc_mem_put(mctx, lctx, sizeof(*lctx));
466 467 468
	isc_mem_detach(&mctx);
}

469 470 471 472 473 474
static isc_result_t
incctx_create(isc_mem_t *mctx, dns_name_t *origin, dns_incctx_t **ictxp) {
	dns_incctx_t *ictx;
	isc_region_t r;
	int i;

Andreas Gustafsson's avatar
Andreas Gustafsson committed
475
	ictx = isc_mem_get(mctx, sizeof(*ictx));
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	if (ictx == NULL)
		return (ISC_R_NOMEMORY);

	for (i = 0; i < NBUFS; i++) {
		dns_fixedname_init(&ictx->fixed[i]);
		ictx->in_use[i] = ISC_FALSE;
	}

	ictx->origin_in_use = 0;
	ictx->origin = dns_fixedname_name(&ictx->fixed[ictx->origin_in_use]);
	ictx->in_use[ictx->origin_in_use] = ISC_TRUE;
	dns_name_toregion(origin, &r);
	dns_name_fromregion(ictx->origin, &r);

	ictx->glue = NULL;
	ictx->current = NULL;
	ictx->glue_in_use = -1;
	ictx->current_in_use = -1;
	ictx->parent = NULL;
495 496 497
	ictx->drop = ISC_FALSE;
	ictx->glue_line = 0;
	ictx->current_line = 0;
498 499 500 501 502

	*ictxp = ictx;
	return (ISC_R_SUCCESS);
}

503
static isc_result_t
504 505
loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
	       unsigned int options, dns_name_t *top,
506 507
	       dns_rdataclass_t zclass, dns_name_t *origin,
	       dns_rdatacallbacks_t *callbacks, isc_task_t *task,
508
	       dns_loaddonefunc_t done, void *done_arg, isc_lex_t *lex,
509
	       dns_loadctx_t **lctxp)
510
{
511
	dns_loadctx_t *lctx;
512 513 514 515
	isc_result_t result;
	isc_region_t r;
	isc_lexspecials_t specials;

516
	REQUIRE(lctxp != NULL && *lctxp == NULL);
517 518 519 520 521 522 523 524
	REQUIRE(callbacks != NULL);
	REQUIRE(callbacks->add != NULL);
	REQUIRE(callbacks->error != NULL);
	REQUIRE(callbacks->warn != NULL);
	REQUIRE(mctx != NULL);
	REQUIRE(dns_name_isabsolute(top));
	REQUIRE(dns_name_isabsolute(origin));
	REQUIRE((task == NULL && done == NULL) ||
525
		(task != NULL && done != NULL));
526

527 528
	lctx = isc_mem_get(mctx, sizeof(*lctx));
	if (lctx == NULL)
529
		return (ISC_R_NOMEMORY);
530
	result = isc_mutex_init(&lctx->lock);
531
	if (result != ISC_R_SUCCESS) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
532
		isc_mem_put(mctx, lctx, sizeof(*lctx));
533
		return (result);
534
	}
535

536 537 538
	lctx->inc = NULL;
	result = incctx_create(mctx, origin, &lctx->inc);
	if (result != ISC_R_SUCCESS) 
539
		goto cleanup_ctx;
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554
	lctx->format = format;
	switch (format) {
	default:
		INSIST(0);
	case dns_masterformat_text:
		lctx->openfile = openfile_text;
		lctx->load = load_text;
		break;
	case dns_masterformat_raw:
		lctx->openfile = openfile_raw;
		lctx->load = load_raw;
		break;
	}

555
	if (lex != NULL) {
556
		lctx->lex = lex;
557 558
		lctx->keep_lex = ISC_TRUE;
	} else {
559 560 561 562
		lctx->lex = NULL;
		result = isc_lex_create(mctx, TOKENSIZ, &lctx->lex);
		if (result != ISC_R_SUCCESS)
			goto cleanup_inc;
563
		lctx->keep_lex = ISC_FALSE;
564 565 566 567 568 569 570
		memset(specials, 0, sizeof(specials));
		specials['('] = 1;
		specials[')'] = 1;
		specials['"'] = 1;
		isc_lex_setspecials(lctx->lex, specials);
		isc_lex_setcomments(lctx->lex, ISC_LEXCOMMENT_DNSMASTERFILE);
	}
571 572 573 574 575 576

	lctx->ttl_known = ISC_FALSE;
	lctx->ttl = 0;
	lctx->default_ttl_known = ISC_FALSE;
	lctx->default_ttl = 0;
	lctx->warn_1035 = ISC_TRUE;	/* XXX Argument? */
577
	lctx->warn_tcr = ISC_TRUE;	/* XXX Argument? */
578
	lctx->warn_sigexpired = ISC_TRUE;	/* XXX Argument? */
579 580 581 582 583 584 585
	lctx->options = options;
	lctx->seen_include = ISC_FALSE;
	lctx->zclass = zclass;
	lctx->result = ISC_R_SUCCESS;

	dns_fixedname_init(&lctx->fixed_top);
	lctx->top = dns_fixedname_name(&lctx->fixed_top);
586
	dns_name_toregion(top, &r);
587
	dns_name_fromregion(lctx->top, &r);
588

589 590 591
	lctx->f = NULL;
	lctx->first = ISC_TRUE;

592 593 594
	lctx->loop_cnt = (done != NULL) ? 100 : 0;
	lctx->callbacks = callbacks;
	lctx->task = NULL;
595
	if (task != NULL)
596 597 598 599 600 601 602 603 604
		isc_task_attach(task, &lctx->task);
	lctx->done = done;
	lctx->done_arg = done_arg;
	lctx->canceled = ISC_FALSE;
	lctx->mctx = NULL;
	isc_mem_attach(mctx, &lctx->mctx);
	lctx->references = 1;			/* Implicit attach. */
	lctx->magic = DNS_LCTX_MAGIC;
	*lctxp = lctx;
605 606
	return (ISC_R_SUCCESS);

607 608
 cleanup_inc:
	incctx_destroy(mctx, lctx->inc);
609
 cleanup_ctx:
610
	isc_mem_put(mctx, lctx, sizeof(*lctx));
611
	return (result);
612
}
613

614 615 616 617 618
static isc_result_t
genname(char *name, int it, char *buffer, size_t length) {
	char fmt[sizeof("%04000000000d")];
	char numbuf[128];
	char *cp;
619
	char mode[2];
620 621 622 623 624 625 626 627 628 629 630
	int delta = 0;
	isc_textregion_t r;
	unsigned int n;
	unsigned int width;

	r.base = buffer;
	r.length = length;

	while (*name != '\0') {
		if (*name == '$') {
			name++;
631 632 633 634 635 636 637
			if (*name == '$') {
				if (r.length == 0)
					return (ISC_R_NOSPACE);
				r.base[0] = *name++;
				isc_textregion_consume(&r, 1);
				continue;
			}
638 639 640 641
			strcpy(fmt, "%d");
			/* Get format specifier. */
			if (*name == '{' ) {
				n = sscanf(name, "{%d,%u,%1[doxX]}",
642
					   &delta, &width, mode);
643 644 645 646 647 648 649 650 651
				switch (n) {
				case 1:
					break;
				case 2:
					n = snprintf(fmt, sizeof(fmt),
						     "%%0%ud", width);
					break;
				case 3:
					n = snprintf(fmt, sizeof(fmt),
652
						     "%%0%u%c", width, mode[0]);
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
					break;
				default:
					return (DNS_R_SYNTAX);
				}
				if (n >= sizeof(fmt))
					return (ISC_R_NOSPACE);
				/* Skip past closing brace. */
				while (*name != '\0' && *name++ != '}')
					continue;
			}
			n = snprintf(numbuf, sizeof(numbuf), fmt, it + delta);
			if (n >= sizeof(numbuf))
				return (ISC_R_NOSPACE);
			cp = numbuf;
			while (*cp != '\0') {
				if (r.length == 0)
					return (ISC_R_NOSPACE);
				r.base[0] = *cp++;
				isc_textregion_consume(&r, 1);
			}
		} else if (*name == '\\') {
			if (r.length == 0)
				return (ISC_R_NOSPACE);
			r.base[0] = *name++;
			isc_textregion_consume(&r, 1);
			if (*name == '\0')
				continue;
			if (r.length == 0)
				return (ISC_R_NOSPACE);
			r.base[0] = *name++;
			isc_textregion_consume(&r, 1);
		} else {
			if (r.length == 0)
				return (ISC_R_NOSPACE);
			r.base[0] = *name++;
			isc_textregion_consume(&r, 1);
		}
	}
	if (r.length == 0)
		return (ISC_R_NOSPACE);
	r.base[0] = '\0';
	return (ISC_R_SUCCESS);
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
static isc_result_t
openfile_text(dns_loadctx_t *lctx, const char *master_file) {
	return (isc_lex_openfile(lctx->lex, master_file));
}

static isc_result_t
openfile_raw(dns_loadctx_t *lctx, const char *master_file) {
	isc_result_t result;

	result = isc_stdio_open(master_file, "r", &lctx->f);
	if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_stdio_open() failed: %s",
				 isc_result_totext(result));		
	}

	return (result);
}

716
static isc_result_t
717 718 719
generate(dns_loadctx_t *lctx, char *range, char *lhs, char *gtype, char *rhs,
	 const char *source, unsigned int line)
{
720 721 722 723 724
	char *target_mem = NULL;
	char *lhsbuf = NULL;
	char *rhsbuf = NULL;
	dns_fixedname_t ownerfixed;
	dns_name_t *owner;
725
	dns_rdata_t rdata = DNS_RDATA_INIT;
726 727 728 729 730 731 732 733 734 735 736
	dns_rdatacallbacks_t *callbacks;
	dns_rdatalist_t rdatalist;
	dns_rdatatype_t type;
	rdatalist_head_t head;
	int n;
	int target_size = MINTSIZ;	/* only one rdata at a time */
	isc_buffer_t buffer;
	isc_buffer_t target;
	isc_result_t result;
	isc_textregion_t r;
	unsigned int start, stop, step, i;
737
	dns_incctx_t *ictx;
738

739 740
	ictx = lctx->inc;
	callbacks = lctx->callbacks;
741 742 743 744
	dns_fixedname_init(&ownerfixed);
	owner = dns_fixedname_name(&ownerfixed);
	ISC_LIST_INIT(head);

745 746 747
	target_mem = isc_mem_get(lctx->mctx, target_size);
	rhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_BUFSZ);
	lhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_BUFSZ);
748 749 750 751 752 753 754
	if (target_mem == NULL || rhsbuf == NULL || lhsbuf == NULL) {
		result = ISC_R_NOMEMORY;
		goto error_cleanup;
	}
	isc_buffer_init(&target, target_mem, target_size);

	n = sscanf(range, "%u-%u/%u", &start, &stop, &step);
755
	if (n < 2 || stop < start) {
756
	       (*callbacks->error)(callbacks,
757
				  "%s: %s:%lu: invalid range '%s'",
758
				  "$GENERATE", source, line, range);
759 760 761 762 763 764 765 766 767 768 769 770 771
		result = DNS_R_SYNTAX;
		goto insist_cleanup;
	}
	if (n == 2)
		step = 1;

	/*
	 * Get type.
	 */
	r.base = gtype;
	r.length = strlen(gtype);
	result = dns_rdatatype_fromtext(&type, &r);
	if (result != ISC_R_SUCCESS) {
772
		(*callbacks->error)(callbacks,
773
				   "%s: %s:%lu: unknown RR type '%s'",
774
				   "$GENERATE", source, line, gtype);
775 776 777 778 779 780 781
		goto insist_cleanup;
	}

	switch (type) {
	case dns_rdatatype_ns:
	case dns_rdatatype_ptr:
	case dns_rdatatype_cname:
782
	case dns_rdatatype_dname:
783 784 785 786
		break;

	case dns_rdatatype_a:
	case dns_rdatatype_aaaa:
787
		if (lctx->zclass == dns_rdataclass_in ||
788
		    lctx->zclass == dns_rdataclass_ch ||
789
		    lctx->zclass == dns_rdataclass_hs)
790 791 792
			break;
		/* FALLTHROUGH */
	default:
793
	       (*callbacks->error)(callbacks,
794
				  "%s: %s:%lu: unsupported type '%s'",
795
				  "$GENERATE", source, line, gtype);
796 797 798 799
		result = ISC_R_NOTIMPLEMENTED;
		goto error_cleanup;
	}

800 801
	ISC_LIST_INIT(rdatalist.rdata);
	ISC_LINK_INIT(&rdatalist, link);
802
	for (i = start; i <= stop; i += step) {
803 804 805 806 807 808 809 810 811 812
		result = genname(lhs, i, lhsbuf, DNS_MASTER_BUFSZ);
		if (result != ISC_R_SUCCESS)
			goto error_cleanup;
		result = genname(rhs, i, rhsbuf, DNS_MASTER_BUFSZ);
		if (result != ISC_R_SUCCESS)
			goto error_cleanup;

		isc_buffer_init(&buffer, lhsbuf, strlen(lhsbuf));
		isc_buffer_add(&buffer, strlen(lhsbuf));
		isc_buffer_setactive(&buffer, strlen(lhsbuf));
813
		result = dns_name_fromtext(owner, &buffer, ictx->origin,
814
					   0, NULL);
815 816 817
		if (result != ISC_R_SUCCESS)
			goto error_cleanup;

818 819 820 821
		if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
		    (lctx->options & DNS_MASTER_SLAVE) == 0 &&
		    !dns_name_issubdomain(owner, lctx->top))
		{
822 823 824 825 826 827
			char namebuf[DNS_NAME_FORMATSIZE];
			dns_name_format(owner, namebuf, sizeof(namebuf));
			/*
			 * Ignore out-of-zone data.
			 */
			(*callbacks->warn)(callbacks,
828
					   "%s:%lu: "
829 830 831 832 833
					   "ignoring out-of-zone data (%s)",
					   source, line, namebuf);
			continue;
		}

834 835 836 837
		isc_buffer_init(&buffer, rhsbuf, strlen(rhsbuf));
		isc_buffer_add(&buffer, strlen(rhsbuf));
		isc_buffer_setactive(&buffer, strlen(rhsbuf));

838
		result = isc_lex_openbuffer(lctx->lex, &buffer);
839 840 841 842
		if (result != ISC_R_SUCCESS)
			goto error_cleanup;

		isc_buffer_init(&target, target_mem, target_size);
843
		result = dns_rdata_fromtext(&rdata, lctx->zclass, type,
844
					    lctx->lex, ictx->origin, 0,
845
					    lctx->mctx, &target, callbacks);
846
		RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
847 848 849 850 851
		if (result != ISC_R_SUCCESS)
			goto error_cleanup;

		rdatalist.type = type;
		rdatalist.covers = 0;
852 853
		rdatalist.rdclass = lctx->zclass;
		rdatalist.ttl = lctx->ttl;
854 855
		ISC_LIST_PREPEND(head, &rdatalist, link);
		ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
856
		result = commit(callbacks, lctx, &head, owner, source, line);
857
		ISC_LIST_UNLINK(rdatalist.rdata, &rdata, link);
858 859
		if (result != ISC_R_SUCCESS)
			goto error_cleanup;
860
		dns_rdata_reset(&rdata);
861 862 863 864 865 866 867 868 869 870
	}
	result = ISC_R_SUCCESS;
	goto cleanup;

 error_cleanup:
	if (result == ISC_R_NOMEMORY)
		(*callbacks->error)(callbacks, "$GENERATE: %s",
				    dns_result_totext(result));
	else
		(*callbacks->error)(callbacks, "$GENERATE: %s:%lu: %s",
871
				    source, line, dns_result_totext(result));
872 873 874 875 876 877

 insist_cleanup:
	INSIST(result != ISC_R_SUCCESS);

 cleanup:
	if (target_mem != NULL)
878
		isc_mem_put(lctx->mctx, target_mem, target_size);
879
	if (lhsbuf != NULL)
880
		isc_mem_put(lctx->mctx, lhsbuf, DNS_MASTER_BUFSZ);
881
	if (rhsbuf != NULL)
882
		isc_mem_put(lctx->mctx, rhsbuf, DNS_MASTER_BUFSZ);
883 884 885
	return (result);
}

886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
static void
limit_ttl(dns_rdatacallbacks_t *callbacks, const char *source, unsigned int line,
	  isc_uint32_t *ttlp)
{
	if (*ttlp > 0x7fffffffUL) {
		(callbacks->warn)(callbacks,
				  "%s: %s:%lu: "
				  "$TTL %lu > MAXTTL, "
				  "setting $TTL to 0",
				  "dns_master_load",
				  source, line,
				  *ttlp);
		*ttlp = 0;
	}
}

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
static isc_result_t
check_ns(dns_loadctx_t *lctx, isc_token_t *token, const char *source,
	 unsigned long line)
{
	char *tmp = NULL;
	isc_result_t result = ISC_R_SUCCESS;
	void (*callback)(struct dns_rdatacallbacks *, const char *, ...);

	if ((lctx->options & DNS_MASTER_FATALNS) != 0)
		callback = lctx->callbacks->error;
	else
		callback = lctx->callbacks->warn;
		
	if (token->type == isc_tokentype_string) {
		struct in_addr addr;
		struct in6_addr addr6;

		tmp = isc_mem_strdup(lctx->mctx, DNS_AS_STR(*token));
		if (tmp == NULL)
			return (ISC_R_NOMEMORY);
		/*
		 * Catch both "1.2.3.4" and "1.2.3.4."
		 */
		if (tmp[strlen(tmp) - 1] == '.')
			tmp[strlen(tmp) - 1] = '\0';
		if (inet_aton(tmp, &addr) == 1 ||
		    inet_pton(AF_INET6, tmp, &addr6) == 1)
			result = DNS_R_NSISADDRESS;
	}
	if (result != ISC_R_SUCCESS)
		(*callback)(lctx->callbacks, "%s:%lu: NS record '%s' "
			    "appears to be an address",
			    source, line, DNS_AS_STR(*token));
	if (tmp != NULL)
		isc_mem_free(lctx->mctx, tmp);
	return (result);
}

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
static void
check_wildcard(dns_incctx_t *ictx, const char *source, unsigned long line,
	       dns_rdatacallbacks_t *callbacks)
{
	dns_name_t *name;

	name = (ictx->glue != NULL) ? ictx->glue : ictx->current;
	if (dns_name_internalwildcard(name)) {
		char namebuf[DNS_NAME_FORMATSIZE];

		dns_name_format(name, namebuf, sizeof(namebuf));
		(*callbacks->warn)(callbacks, "%s:%lu: warning: ownername "
				   "'%s' contains an non-terminal wildcard",
				   source, line, namebuf);
	}
}

957
static isc_result_t
958
load_text(dns_loadctx_t *lctx) {
Bob Halley's avatar
Bob Halley committed
959
	dns_rdataclass_t rdclass;
Bob Halley's avatar
Bob Halley committed
960
	dns_rdatatype_t type, covers;
961
	isc_uint32_t ttl_offset = 0;
962
	dns_name_t *new_name;
963 964
	isc_boolean_t current_has_delegation = ISC_FALSE;
	isc_boolean_t done = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
965
	isc_boolean_t finish_origin = ISC_FALSE;
966
	isc_boolean_t finish_include = ISC_FALSE;
Mark Andrews's avatar
Mark Andrews committed
967
	isc_boolean_t read_till_eol = ISC_FALSE;
968
	isc_boolean_t initialws;
969
	char *include_file = NULL;
970
	isc_token_t token;
971
	isc_result_t result = ISC_R_UNEXPECTED;
972 973 974 975 976 977 978 979 980 981
	rdatalist_head_t glue_list;
	rdatalist_head_t current_list;
	dns_rdatalist_t *this;
	dns_rdatalist_t *rdatalist = NULL;
	dns_rdatalist_t *new_rdatalist;
	int rdlcount = 0;
	int rdlcount_save = 0;
	int rdatalist_size = 0;
	isc_buffer_t buffer;
	isc_buffer_t target;
982
	isc_buffer_t target_ft;
983 984 985 986 987 988 989
	isc_buffer_t target_save;
	dns_rdata_t *rdata = NULL;
	dns_rdata_t *new_rdata;
	int rdcount = 0;
	int rdcount_save = 0;
	int rdata_size = 0;
	unsigned char *target_mem = NULL;
990
	int target_size = TSIZ;
Mark Andrews's avatar
Mark Andrews committed
991
	int new_in_use;
992 993 994
	unsigned int loop_cnt = 0;
	isc_mem_t *mctx;
	dns_rdatacallbacks_t *callbacks;
995
	dns_incctx_t *ictx;
996 997 998