PageRenderTime 121ms CodeModel.GetById 35ms app.highlight 74ms RepoModel.GetById 1ms app.codeStats 1ms

/contrib/bind9/lib/dns/masterdump.c

https://bitbucket.org/freebsd/freebsd-head/
C | 1803 lines | 1373 code | 233 blank | 197 comment | 325 complexity | 080ab4612fdc680c26df08a04a5583f0 MD5 | raw file
   1/*
   2 * Copyright (C) 2004-2009, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
   3 * Copyright (C) 1999-2003  Internet Software Consortium.
   4 *
   5 * Permission to use, copy, modify, and/or distribute this software for any
   6 * purpose with or without fee is hereby granted, provided that the above
   7 * copyright notice and this permission notice appear in all copies.
   8 *
   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.
  16 */
  17
  18/* $Id$ */
  19
  20/*! \file */
  21
  22#include <config.h>
  23
  24#include <stdlib.h>
  25
  26#include <isc/event.h>
  27#include <isc/file.h>
  28#include <isc/magic.h>
  29#include <isc/mem.h>
  30#include <isc/print.h>
  31#include <isc/stdio.h>
  32#include <isc/string.h>
  33#include <isc/task.h>
  34#include <isc/time.h>
  35#include <isc/util.h>
  36
  37#include <dns/db.h>
  38#include <dns/dbiterator.h>
  39#include <dns/events.h>
  40#include <dns/fixedname.h>
  41#include <dns/lib.h>
  42#include <dns/log.h>
  43#include <dns/master.h>
  44#include <dns/masterdump.h>
  45#include <dns/ncache.h>
  46#include <dns/rdata.h>
  47#include <dns/rdataclass.h>
  48#include <dns/rdataset.h>
  49#include <dns/rdatasetiter.h>
  50#include <dns/rdatatype.h>
  51#include <dns/result.h>
  52#include <dns/time.h>
  53#include <dns/ttl.h>
  54
  55#define DNS_DCTX_MAGIC		ISC_MAGIC('D', 'c', 't', 'x')
  56#define DNS_DCTX_VALID(d)	ISC_MAGIC_VALID(d, DNS_DCTX_MAGIC)
  57
  58#define RETERR(x) do { \
  59	isc_result_t _r = (x); \
  60	if (_r != ISC_R_SUCCESS) \
  61		return (_r); \
  62	} while (0)
  63
  64#define CHECK(x) do { \
  65	if ((x) != ISC_R_SUCCESS) \
  66		goto cleanup; \
  67	} while (0)
  68
  69struct dns_master_style {
  70	unsigned int flags;		/* DNS_STYLEFLAG_* */
  71	unsigned int ttl_column;
  72	unsigned int class_column;
  73	unsigned int type_column;
  74	unsigned int rdata_column;
  75	unsigned int line_length;
  76	unsigned int tab_width;
  77};
  78
  79/*%
  80 * The maximum length of the newline+indentation that is output
  81 * when inserting a line break in an RR.  This effectively puts an
  82 * upper limits on the value of "rdata_column", because if it is
  83 * very large, the tabs and spaces needed to reach it will not fit.
  84 */
  85#define DNS_TOTEXT_LINEBREAK_MAXLEN 100
  86
  87/*%
  88 * Context structure for a masterfile dump in progress.
  89 */
  90typedef struct dns_totext_ctx {
  91	dns_master_style_t	style;
  92	isc_boolean_t 		class_printed;
  93	char *			linebreak;
  94	char 			linebreak_buf[DNS_TOTEXT_LINEBREAK_MAXLEN];
  95	dns_name_t *		origin;
  96	dns_name_t *		neworigin;
  97	dns_fixedname_t		origin_fixname;
  98	isc_uint32_t 		current_ttl;
  99	isc_boolean_t 		current_ttl_valid;
 100} dns_totext_ctx_t;
 101
 102LIBDNS_EXTERNAL_DATA const dns_master_style_t
 103dns_master_style_default = {
 104	DNS_STYLEFLAG_OMIT_OWNER |
 105	DNS_STYLEFLAG_OMIT_CLASS |
 106	DNS_STYLEFLAG_REL_OWNER |
 107	DNS_STYLEFLAG_REL_DATA |
 108	DNS_STYLEFLAG_OMIT_TTL |
 109	DNS_STYLEFLAG_TTL |
 110	DNS_STYLEFLAG_COMMENT |
 111	DNS_STYLEFLAG_MULTILINE,
 112	24, 24, 24, 32, 80, 8
 113};
 114
 115LIBDNS_EXTERNAL_DATA const dns_master_style_t
 116dns_master_style_full = {
 117	DNS_STYLEFLAG_COMMENT |
 118	DNS_STYLEFLAG_RESIGN,
 119	46, 46, 46, 64, 120, 8
 120};
 121
 122LIBDNS_EXTERNAL_DATA const dns_master_style_t
 123dns_master_style_explicitttl = {
 124	DNS_STYLEFLAG_OMIT_OWNER |
 125	DNS_STYLEFLAG_OMIT_CLASS |
 126	DNS_STYLEFLAG_REL_OWNER |
 127	DNS_STYLEFLAG_REL_DATA |
 128	DNS_STYLEFLAG_COMMENT |
 129	DNS_STYLEFLAG_MULTILINE,
 130	24, 32, 32, 40, 80, 8
 131};
 132
 133LIBDNS_EXTERNAL_DATA const dns_master_style_t
 134dns_master_style_cache = {
 135	DNS_STYLEFLAG_OMIT_OWNER |
 136	DNS_STYLEFLAG_OMIT_CLASS |
 137	DNS_STYLEFLAG_MULTILINE |
 138	DNS_STYLEFLAG_TRUST |
 139	DNS_STYLEFLAG_NCACHE,
 140	24, 32, 32, 40, 80, 8
 141};
 142
 143LIBDNS_EXTERNAL_DATA const dns_master_style_t
 144dns_master_style_simple = {
 145	0,
 146	24, 32, 32, 40, 80, 8
 147};
 148
 149/*%
 150 * A style suitable for dns_rdataset_totext().
 151 */
 152LIBDNS_EXTERNAL_DATA const dns_master_style_t
 153dns_master_style_debug = {
 154	DNS_STYLEFLAG_REL_OWNER,
 155	24, 32, 40, 48, 80, 8
 156};
 157
 158
 159#define N_SPACES 10
 160static char spaces[N_SPACES+1] = "          ";
 161
 162#define N_TABS 10
 163static char tabs[N_TABS+1] = "\t\t\t\t\t\t\t\t\t\t";
 164
 165#ifdef BIND9
 166struct dns_dumpctx {
 167	unsigned int		magic;
 168	isc_mem_t		*mctx;
 169	isc_mutex_t		lock;
 170	unsigned int		references;
 171	isc_boolean_t		canceled;
 172	isc_boolean_t		first;
 173	isc_boolean_t		do_date;
 174	isc_stdtime_t		now;
 175	FILE			*f;
 176	dns_db_t		*db;
 177	dns_dbversion_t		*version;
 178	dns_dbiterator_t	*dbiter;
 179	dns_totext_ctx_t	tctx;
 180	isc_task_t		*task;
 181	dns_dumpdonefunc_t	done;
 182	void			*done_arg;
 183	unsigned int		nodes;
 184	/* dns_master_dumpinc() */
 185	char			*file;
 186	char 			*tmpfile;
 187	dns_masterformat_t	format;
 188	isc_result_t		(*dumpsets)(isc_mem_t *mctx, dns_name_t *name,
 189					    dns_rdatasetiter_t *rdsiter,
 190					    dns_totext_ctx_t *ctx,
 191					    isc_buffer_t *buffer, FILE *f);
 192};
 193#endif /* BIND9 */
 194
 195#define NXDOMAIN(x) (((x)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
 196
 197/*%
 198 * Output tabs and spaces to go from column '*current' to
 199 * column 'to', and update '*current' to reflect the new
 200 * current column.
 201 */
 202static isc_result_t
 203indent(unsigned int *current, unsigned int to, int tabwidth,
 204       isc_buffer_t *target)
 205{
 206	isc_region_t r;
 207	unsigned char *p;
 208	unsigned int from;
 209	int ntabs, nspaces, t;
 210
 211	from = *current;
 212
 213	if (to < from + 1)
 214		to = from + 1;
 215
 216	ntabs = to / tabwidth - from / tabwidth;
 217	if (ntabs < 0)
 218		ntabs = 0;
 219
 220	if (ntabs > 0) {
 221		isc_buffer_availableregion(target, &r);
 222		if (r.length < (unsigned) ntabs)
 223			return (ISC_R_NOSPACE);
 224		p = r.base;
 225
 226		t = ntabs;
 227		while (t) {
 228			int n = t;
 229			if (n > N_TABS)
 230				n = N_TABS;
 231			memcpy(p, tabs, n);
 232			p += n;
 233			t -= n;
 234		}
 235		isc_buffer_add(target, ntabs);
 236		from = (to / tabwidth) * tabwidth;
 237	}
 238
 239	nspaces = to - from;
 240	INSIST(nspaces >= 0);
 241
 242	isc_buffer_availableregion(target, &r);
 243	if (r.length < (unsigned) nspaces)
 244		return (ISC_R_NOSPACE);
 245	p = r.base;
 246
 247	t = nspaces;
 248	while (t) {
 249		int n = t;
 250		if (n > N_SPACES)
 251			n = N_SPACES;
 252		memcpy(p, spaces, n);
 253		p += n;
 254		t -= n;
 255	}
 256	isc_buffer_add(target, nspaces);
 257
 258	*current = to;
 259	return (ISC_R_SUCCESS);
 260}
 261
 262static isc_result_t
 263totext_ctx_init(const dns_master_style_t *style, dns_totext_ctx_t *ctx) {
 264	isc_result_t result;
 265
 266	REQUIRE(style->tab_width != 0);
 267
 268	ctx->style = *style;
 269	ctx->class_printed = ISC_FALSE;
 270
 271	dns_fixedname_init(&ctx->origin_fixname);
 272
 273	/*
 274	 * Set up the line break string if needed.
 275	 */
 276	if ((ctx->style.flags & DNS_STYLEFLAG_MULTILINE) != 0) {
 277		isc_buffer_t buf;
 278		isc_region_t r;
 279		unsigned int col = 0;
 280
 281		isc_buffer_init(&buf, ctx->linebreak_buf,
 282				sizeof(ctx->linebreak_buf));
 283
 284		isc_buffer_availableregion(&buf, &r);
 285		if (r.length < 1)
 286			return (DNS_R_TEXTTOOLONG);
 287		r.base[0] = '\n';
 288		isc_buffer_add(&buf, 1);
 289
 290		result = indent(&col, ctx->style.rdata_column,
 291				ctx->style.tab_width, &buf);
 292		/*
 293		 * Do not return ISC_R_NOSPACE if the line break string
 294		 * buffer is too small, because that would just make
 295		 * dump_rdataset() retry indefinitely with ever
 296		 * bigger target buffers.  That's a different buffer,
 297		 * so it won't help.  Use DNS_R_TEXTTOOLONG as a substitute.
 298		 */
 299		if (result == ISC_R_NOSPACE)
 300			return (DNS_R_TEXTTOOLONG);
 301		if (result != ISC_R_SUCCESS)
 302			return (result);
 303
 304		isc_buffer_availableregion(&buf, &r);
 305		if (r.length < 1)
 306			return (DNS_R_TEXTTOOLONG);
 307		r.base[0] = '\0';
 308		isc_buffer_add(&buf, 1);
 309		ctx->linebreak = ctx->linebreak_buf;
 310	} else {
 311		ctx->linebreak = NULL;
 312	}
 313
 314	ctx->origin = NULL;
 315	ctx->neworigin = NULL;
 316	ctx->current_ttl = 0;
 317	ctx->current_ttl_valid = ISC_FALSE;
 318
 319	return (ISC_R_SUCCESS);
 320}
 321
 322#define INDENT_TO(col) \
 323	do { \
 324		 if ((result = indent(&column, ctx->style.col, \
 325				      ctx->style.tab_width, target)) \
 326		     != ISC_R_SUCCESS) \
 327			    return (result); \
 328	} while (0)
 329
 330
 331static isc_result_t
 332str_totext(const char *source, isc_buffer_t *target) {
 333	unsigned int l;
 334	isc_region_t region;
 335
 336	isc_buffer_availableregion(target, &region);
 337	l = strlen(source);
 338
 339	if (l > region.length)
 340		return (ISC_R_NOSPACE);
 341
 342	memcpy(region.base, source, l);
 343	isc_buffer_add(target, l);
 344	return (ISC_R_SUCCESS);
 345}
 346
 347static isc_result_t
 348ncache_summary(dns_rdataset_t *rdataset, isc_boolean_t omit_final_dot,
 349	       isc_buffer_t *target)
 350{
 351	isc_result_t result = ISC_R_SUCCESS;
 352	dns_rdataset_t rds;
 353	dns_name_t name;
 354
 355	dns_rdataset_init(&rds);
 356	dns_name_init(&name, NULL);
 357
 358	do {
 359		dns_ncache_current(rdataset, &name, &rds);
 360		for (result = dns_rdataset_first(&rds);
 361		     result == ISC_R_SUCCESS;
 362		     result = dns_rdataset_next(&rds)) {
 363			CHECK(str_totext("; ", target));
 364			CHECK(dns_name_totext(&name, omit_final_dot, target));
 365			CHECK(str_totext(" ", target));
 366			CHECK(dns_rdatatype_totext(rds.type, target));
 367			if (rds.type == dns_rdatatype_rrsig) {
 368				CHECK(str_totext(" ", target));
 369				CHECK(dns_rdatatype_totext(rds.covers, target));
 370				CHECK(str_totext(" ...\n", target));
 371			} else {
 372				dns_rdata_t rdata = DNS_RDATA_INIT;
 373				dns_rdataset_current(&rds, &rdata);
 374				CHECK(str_totext(" ", target));
 375				CHECK(dns_rdata_tofmttext(&rdata, dns_rootname,
 376							  0, 0, " ", target));
 377				CHECK(str_totext("\n", target));
 378			}
 379		}
 380		dns_rdataset_disassociate(&rds);
 381		result = dns_rdataset_next(rdataset);
 382	} while (result == ISC_R_SUCCESS);
 383
 384	if (result == ISC_R_NOMORE)
 385		result = ISC_R_SUCCESS;
 386 cleanup:
 387	if (dns_rdataset_isassociated(&rds))
 388		dns_rdataset_disassociate(&rds);
 389
 390	return (result);
 391}
 392
 393/*
 394 * Convert 'rdataset' to master file text format according to 'ctx',
 395 * storing the result in 'target'.  If 'owner_name' is NULL, it
 396 * is omitted; otherwise 'owner_name' must be valid and have at least
 397 * one label.
 398 */
 399
 400static isc_result_t
 401rdataset_totext(dns_rdataset_t *rdataset,
 402		dns_name_t *owner_name,
 403		dns_totext_ctx_t *ctx,
 404		isc_boolean_t omit_final_dot,
 405		isc_buffer_t *target)
 406{
 407	isc_result_t result;
 408	unsigned int column;
 409	isc_boolean_t first = ISC_TRUE;
 410	isc_uint32_t current_ttl;
 411	isc_boolean_t current_ttl_valid;
 412	dns_rdatatype_t type;
 413	unsigned int type_start;
 414
 415	REQUIRE(DNS_RDATASET_VALID(rdataset));
 416
 417	rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
 418	result = dns_rdataset_first(rdataset);
 419
 420	current_ttl = ctx->current_ttl;
 421	current_ttl_valid = ctx->current_ttl_valid;
 422
 423	while (result == ISC_R_SUCCESS) {
 424		column = 0;
 425
 426		/*
 427		 * Owner name.
 428		 */
 429		if (owner_name != NULL &&
 430		    ! ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0 &&
 431		       !first))
 432		{
 433			unsigned int name_start = target->used;
 434			RETERR(dns_name_totext(owner_name,
 435					       omit_final_dot,
 436					       target));
 437			column += target->used - name_start;
 438		}
 439
 440		/*
 441		 * TTL.
 442		 */
 443		if ((ctx->style.flags & DNS_STYLEFLAG_NO_TTL) == 0 &&
 444		    !((ctx->style.flags & DNS_STYLEFLAG_OMIT_TTL) != 0 &&
 445		      current_ttl_valid &&
 446		      rdataset->ttl == current_ttl))
 447		{
 448			char ttlbuf[64];
 449			isc_region_t r;
 450			unsigned int length;
 451
 452			INDENT_TO(ttl_column);
 453			length = snprintf(ttlbuf, sizeof(ttlbuf), "%u",
 454					  rdataset->ttl);
 455			INSIST(length <= sizeof(ttlbuf));
 456			isc_buffer_availableregion(target, &r);
 457			if (r.length < length)
 458				return (ISC_R_NOSPACE);
 459			memcpy(r.base, ttlbuf, length);
 460			isc_buffer_add(target, length);
 461			column += length;
 462
 463			/*
 464			 * If the $TTL directive is not in use, the TTL we
 465			 * just printed becomes the default for subsequent RRs.
 466			 */
 467			if ((ctx->style.flags & DNS_STYLEFLAG_TTL) == 0) {
 468				current_ttl = rdataset->ttl;
 469				current_ttl_valid = ISC_TRUE;
 470			}
 471		}
 472
 473		/*
 474		 * Class.
 475		 */
 476		if ((ctx->style.flags & DNS_STYLEFLAG_NO_CLASS) == 0 &&
 477		    ((ctx->style.flags & DNS_STYLEFLAG_OMIT_CLASS) == 0 ||
 478		     ctx->class_printed == ISC_FALSE))
 479		{
 480			unsigned int class_start;
 481			INDENT_TO(class_column);
 482			class_start = target->used;
 483			result = dns_rdataclass_totext(rdataset->rdclass,
 484						       target);
 485			if (result != ISC_R_SUCCESS)
 486				return (result);
 487			column += (target->used - class_start);
 488		}
 489
 490		/*
 491		 * Type.
 492		 */
 493
 494		if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) {
 495			type = rdataset->covers;
 496		} else {
 497			type = rdataset->type;
 498		}
 499
 500		INDENT_TO(type_column);
 501		type_start = target->used;
 502		if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
 503			RETERR(str_totext("\\-", target));
 504		result = dns_rdatatype_totext(type, target);
 505		if (result != ISC_R_SUCCESS)
 506			return (result);
 507		column += (target->used - type_start);
 508
 509		/*
 510		 * Rdata.
 511		 */
 512		INDENT_TO(rdata_column);
 513		if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) {
 514			if (NXDOMAIN(rdataset))
 515				RETERR(str_totext(";-$NXDOMAIN\n", target));
 516			else
 517				RETERR(str_totext(";-$NXRRSET\n", target));
 518			/*
 519			 * Print a summary of the cached records which make
 520			 * up the negative response.
 521			 */
 522			RETERR(ncache_summary(rdataset, omit_final_dot,
 523					      target));
 524			break;
 525		} else {
 526			dns_rdata_t rdata = DNS_RDATA_INIT;
 527			isc_region_t r;
 528
 529			dns_rdataset_current(rdataset, &rdata);
 530
 531			RETERR(dns_rdata_tofmttext(&rdata,
 532						   ctx->origin,
 533						   ctx->style.flags,
 534						   ctx->style.line_length -
 535						       ctx->style.rdata_column,
 536						   ctx->linebreak,
 537						   target));
 538
 539			isc_buffer_availableregion(target, &r);
 540			if (r.length < 1)
 541				return (ISC_R_NOSPACE);
 542			r.base[0] = '\n';
 543			isc_buffer_add(target, 1);
 544		}
 545
 546		first = ISC_FALSE;
 547		result = dns_rdataset_next(rdataset);
 548	}
 549
 550	if (result != ISC_R_NOMORE)
 551		return (result);
 552
 553	/*
 554	 * Update the ctx state to reflect what we just printed.
 555	 * This is done last, only when we are sure we will return
 556	 * success, because this function may be called multiple
 557	 * times with increasing buffer sizes until it succeeds,
 558	 * and failed attempts must not update the state prematurely.
 559	 */
 560	ctx->class_printed = ISC_TRUE;
 561	ctx->current_ttl= current_ttl;
 562	ctx->current_ttl_valid = current_ttl_valid;
 563
 564	return (ISC_R_SUCCESS);
 565}
 566
 567/*
 568 * Print the name, type, and class of an empty rdataset,
 569 * such as those used to represent the question section
 570 * of a DNS message.
 571 */
 572static isc_result_t
 573question_totext(dns_rdataset_t *rdataset,
 574		dns_name_t *owner_name,
 575		dns_totext_ctx_t *ctx,
 576		isc_boolean_t omit_final_dot,
 577		isc_buffer_t *target)
 578{
 579	unsigned int column;
 580	isc_result_t result;
 581	isc_region_t r;
 582
 583	REQUIRE(DNS_RDATASET_VALID(rdataset));
 584	result = dns_rdataset_first(rdataset);
 585	REQUIRE(result == ISC_R_NOMORE);
 586
 587	column = 0;
 588
 589	/* Owner name */
 590	{
 591		unsigned int name_start = target->used;
 592		RETERR(dns_name_totext(owner_name,
 593				       omit_final_dot,
 594				       target));
 595		column += target->used - name_start;
 596	}
 597
 598	/* Class */
 599	{
 600		unsigned int class_start;
 601		INDENT_TO(class_column);
 602		class_start = target->used;
 603		result = dns_rdataclass_totext(rdataset->rdclass, target);
 604		if (result != ISC_R_SUCCESS)
 605			return (result);
 606		column += (target->used - class_start);
 607	}
 608
 609	/* Type */
 610	{
 611		unsigned int type_start;
 612		INDENT_TO(type_column);
 613		type_start = target->used;
 614		result = dns_rdatatype_totext(rdataset->type, target);
 615		if (result != ISC_R_SUCCESS)
 616			return (result);
 617		column += (target->used - type_start);
 618	}
 619
 620	isc_buffer_availableregion(target, &r);
 621	if (r.length < 1)
 622		return (ISC_R_NOSPACE);
 623	r.base[0] = '\n';
 624	isc_buffer_add(target, 1);
 625
 626	return (ISC_R_SUCCESS);
 627}
 628
 629isc_result_t
 630dns_rdataset_totext(dns_rdataset_t *rdataset,
 631		    dns_name_t *owner_name,
 632		    isc_boolean_t omit_final_dot,
 633		    isc_boolean_t question,
 634		    isc_buffer_t *target)
 635{
 636	dns_totext_ctx_t ctx;
 637	isc_result_t result;
 638	result = totext_ctx_init(&dns_master_style_debug, &ctx);
 639	if (result != ISC_R_SUCCESS) {
 640		UNEXPECTED_ERROR(__FILE__, __LINE__,
 641				 "could not set master file style");
 642		return (ISC_R_UNEXPECTED);
 643	}
 644
 645	/*
 646	 * The caller might want to give us an empty owner
 647	 * name (e.g. if they are outputting into a master
 648	 * file and this rdataset has the same name as the
 649	 * previous one.)
 650	 */
 651	if (dns_name_countlabels(owner_name) == 0)
 652		owner_name = NULL;
 653
 654	if (question)
 655		return (question_totext(rdataset, owner_name, &ctx,
 656					omit_final_dot, target));
 657	else
 658		return (rdataset_totext(rdataset, owner_name, &ctx,
 659					omit_final_dot, target));
 660}
 661
 662isc_result_t
 663dns_master_rdatasettotext(dns_name_t *owner_name,
 664			  dns_rdataset_t *rdataset,
 665			  const dns_master_style_t *style,
 666			  isc_buffer_t *target)
 667{
 668	dns_totext_ctx_t ctx;
 669	isc_result_t result;
 670	result = totext_ctx_init(style, &ctx);
 671	if (result != ISC_R_SUCCESS) {
 672		UNEXPECTED_ERROR(__FILE__, __LINE__,
 673				 "could not set master file style");
 674		return (ISC_R_UNEXPECTED);
 675	}
 676
 677	return (rdataset_totext(rdataset, owner_name, &ctx,
 678				ISC_FALSE, target));
 679}
 680
 681isc_result_t
 682dns_master_questiontotext(dns_name_t *owner_name,
 683			  dns_rdataset_t *rdataset,
 684			  const dns_master_style_t *style,
 685			  isc_buffer_t *target)
 686{
 687	dns_totext_ctx_t ctx;
 688	isc_result_t result;
 689	result = totext_ctx_init(style, &ctx);
 690	if (result != ISC_R_SUCCESS) {
 691		UNEXPECTED_ERROR(__FILE__, __LINE__,
 692				 "could not set master file style");
 693		return (ISC_R_UNEXPECTED);
 694	}
 695
 696	return (question_totext(rdataset, owner_name, &ctx,
 697				ISC_FALSE, target));
 698}
 699
 700#ifdef BIND9
 701/*
 702 * Print an rdataset.  'buffer' is a scratch buffer, which must have been
 703 * dynamically allocated by the caller.  It must be large enough to
 704 * hold the result from dns_ttl_totext().  If more than that is needed,
 705 * the buffer will be grown automatically.
 706 */
 707
 708static isc_result_t
 709dump_rdataset(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
 710	      dns_totext_ctx_t *ctx,
 711	      isc_buffer_t *buffer, FILE *f)
 712{
 713	isc_region_t r;
 714	isc_result_t result;
 715
 716	REQUIRE(buffer->length > 0);
 717
 718	/*
 719	 * Output a $TTL directive if needed.
 720	 */
 721
 722	if ((ctx->style.flags & DNS_STYLEFLAG_TTL) != 0) {
 723		if (ctx->current_ttl_valid == ISC_FALSE ||
 724		    ctx->current_ttl != rdataset->ttl)
 725		{
 726			if ((ctx->style.flags & DNS_STYLEFLAG_COMMENT) != 0)
 727			{
 728				isc_buffer_clear(buffer);
 729				result = dns_ttl_totext(rdataset->ttl,
 730							ISC_TRUE, buffer);
 731				INSIST(result == ISC_R_SUCCESS);
 732				isc_buffer_usedregion(buffer, &r);
 733				fprintf(f, "$TTL %u\t; %.*s\n", rdataset->ttl,
 734					(int) r.length, (char *) r.base);
 735			} else {
 736				fprintf(f, "$TTL %u\n", rdataset->ttl);
 737			}
 738			ctx->current_ttl = rdataset->ttl;
 739			ctx->current_ttl_valid = ISC_TRUE;
 740		}
 741	}
 742
 743	isc_buffer_clear(buffer);
 744
 745	/*
 746	 * Generate the text representation of the rdataset into
 747	 * the buffer.  If the buffer is too small, grow it.
 748	 */
 749	for (;;) {
 750		int newlength;
 751		void *newmem;
 752		result = rdataset_totext(rdataset, name, ctx,
 753					 ISC_FALSE, buffer);
 754		if (result != ISC_R_NOSPACE)
 755			break;
 756
 757		newlength = buffer->length * 2;
 758		newmem = isc_mem_get(mctx, newlength);
 759		if (newmem == NULL)
 760			return (ISC_R_NOMEMORY);
 761		isc_mem_put(mctx, buffer->base, buffer->length);
 762		isc_buffer_init(buffer, newmem, newlength);
 763	}
 764	if (result != ISC_R_SUCCESS)
 765		return (result);
 766
 767	/*
 768	 * Write the buffer contents to the master file.
 769	 */
 770	isc_buffer_usedregion(buffer, &r);
 771	result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
 772
 773	if (result != ISC_R_SUCCESS) {
 774		UNEXPECTED_ERROR(__FILE__, __LINE__,
 775				 "master file write failed: %s",
 776				 isc_result_totext(result));
 777		return (result);
 778	}
 779
 780	return (ISC_R_SUCCESS);
 781}
 782
 783/*
 784 * Define the order in which rdatasets should be printed in zone
 785 * files.  We will print SOA and NS records before others, SIGs
 786 * immediately following the things they sign, and order everything
 787 * else by RR number.  This is all just for aesthetics and
 788 * compatibility with buggy software that expects the SOA to be first;
 789 * the DNS specifications allow any order.
 790 */
 791
 792static int
 793dump_order(const dns_rdataset_t *rds) {
 794	int t;
 795	int sig;
 796	if (rds->type == dns_rdatatype_rrsig) {
 797		t = rds->covers;
 798		sig = 1;
 799	} else {
 800		t = rds->type;
 801		sig = 0;
 802	}
 803	switch (t) {
 804	case dns_rdatatype_soa:
 805		t = 0;
 806		break;
 807	case dns_rdatatype_ns:
 808		t = 1;
 809		break;
 810	default:
 811		t += 2;
 812		break;
 813	}
 814	return (t << 1) + sig;
 815}
 816
 817static int
 818dump_order_compare(const void *a, const void *b) {
 819	return (dump_order(*((const dns_rdataset_t * const *) a)) -
 820		dump_order(*((const dns_rdataset_t * const *) b)));
 821}
 822
 823/*
 824 * Dump all the rdatasets of a domain name to a master file.  We make
 825 * a "best effort" attempt to sort the RRsets in a nice order, but if
 826 * there are more than MAXSORT RRsets, we punt and only sort them in
 827 * groups of MAXSORT.  This is not expected to ever happen in practice
 828 * since much less than 64 RR types have been registered with the
 829 * IANA, so far, and the output will be correct (though not
 830 * aesthetically pleasing) even if it does happen.
 831 */
 832
 833#define MAXSORT 64
 834
 835static isc_result_t
 836dump_rdatasets_text(isc_mem_t *mctx, dns_name_t *name,
 837		    dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
 838		    isc_buffer_t *buffer, FILE *f)
 839{
 840	isc_result_t itresult, dumpresult;
 841	isc_region_t r;
 842	dns_rdataset_t rdatasets[MAXSORT];
 843	dns_rdataset_t *sorted[MAXSORT];
 844	int i, n;
 845
 846	itresult = dns_rdatasetiter_first(rdsiter);
 847	dumpresult = ISC_R_SUCCESS;
 848
 849	if (itresult == ISC_R_SUCCESS && ctx->neworigin != NULL) {
 850		isc_buffer_clear(buffer);
 851		itresult = dns_name_totext(ctx->neworigin, ISC_FALSE, buffer);
 852		RUNTIME_CHECK(itresult == ISC_R_SUCCESS);
 853		isc_buffer_usedregion(buffer, &r);
 854		fprintf(f, "$ORIGIN %.*s\n", (int) r.length, (char *) r.base);
 855		ctx->neworigin = NULL;
 856	}
 857
 858 again:
 859	for (i = 0;
 860	     itresult == ISC_R_SUCCESS && i < MAXSORT;
 861	     itresult = dns_rdatasetiter_next(rdsiter), i++) {
 862		dns_rdataset_init(&rdatasets[i]);
 863		dns_rdatasetiter_current(rdsiter, &rdatasets[i]);
 864		sorted[i] = &rdatasets[i];
 865	}
 866	n = i;
 867	INSIST(n <= MAXSORT);
 868
 869	qsort(sorted, n, sizeof(sorted[0]), dump_order_compare);
 870
 871	for (i = 0; i < n; i++) {
 872		dns_rdataset_t *rds = sorted[i];
 873		if (ctx->style.flags & DNS_STYLEFLAG_TRUST)
 874			fprintf(f, "; %s\n", dns_trust_totext(rds->trust));
 875		if (((rds->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) &&
 876		    (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
 877			/* Omit negative cache entries */
 878		} else {
 879			isc_result_t result =
 880				dump_rdataset(mctx, name, rds, ctx,
 881					       buffer, f);
 882			if (result != ISC_R_SUCCESS)
 883				dumpresult = result;
 884			if ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0)
 885				name = NULL;
 886		}
 887		if (ctx->style.flags & DNS_STYLEFLAG_RESIGN &&
 888		    rds->attributes & DNS_RDATASETATTR_RESIGN) {
 889			isc_buffer_t b;
 890			char buf[sizeof("YYYYMMDDHHMMSS")];
 891			memset(buf, 0, sizeof(buf));
 892			isc_buffer_init(&b, buf, sizeof(buf) - 1);
 893			dns_time64_totext((isc_uint64_t)rds->resign, &b);
 894			fprintf(f, "; resign=%s\n", buf);
 895		}
 896		dns_rdataset_disassociate(rds);
 897	}
 898
 899	if (dumpresult != ISC_R_SUCCESS)
 900		return (dumpresult);
 901
 902	/*
 903	 * If we got more data than could be sorted at once,
 904	 * go handle the rest.
 905	 */
 906	if (itresult == ISC_R_SUCCESS)
 907		goto again;
 908
 909	if (itresult == ISC_R_NOMORE)
 910		itresult = ISC_R_SUCCESS;
 911
 912	return (itresult);
 913}
 914
 915/*
 916 * Dump given RRsets in the "raw" format.
 917 */
 918static isc_result_t
 919dump_rdataset_raw(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
 920		  isc_buffer_t *buffer, FILE *f)
 921{
 922	isc_result_t result;
 923	isc_uint32_t totallen;
 924	isc_uint16_t dlen;
 925	isc_region_t r, r_hdr;
 926
 927	REQUIRE(buffer->length > 0);
 928	REQUIRE(DNS_RDATASET_VALID(rdataset));
 929
 930	rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
 931 restart:
 932	totallen = 0;
 933	result = dns_rdataset_first(rdataset);
 934	REQUIRE(result == ISC_R_SUCCESS);
 935
 936	isc_buffer_clear(buffer);
 937
 938	/*
 939	 * Common header and owner name (length followed by name)
 940	 * These fields should be in a moderate length, so we assume we
 941	 * can store all of them in the initial buffer.
 942	 */
 943	isc_buffer_availableregion(buffer, &r_hdr);
 944	INSIST(r_hdr.length >= sizeof(dns_masterrawrdataset_t));
 945	isc_buffer_putuint32(buffer, totallen);	/* XXX: leave space */
 946	isc_buffer_putuint16(buffer, rdataset->rdclass); /* 16-bit class */
 947	isc_buffer_putuint16(buffer, rdataset->type); /* 16-bit type */
 948	isc_buffer_putuint16(buffer, rdataset->covers);	/* same as type */
 949	isc_buffer_putuint32(buffer, rdataset->ttl); /* 32-bit TTL */
 950	isc_buffer_putuint32(buffer, dns_rdataset_count(rdataset));
 951	totallen = isc_buffer_usedlength(buffer);
 952	INSIST(totallen <= sizeof(dns_masterrawrdataset_t));
 953
 954	dns_name_toregion(name, &r);
 955	INSIST(isc_buffer_availablelength(buffer) >=
 956	       (sizeof(dlen) + r.length));
 957	dlen = (isc_uint16_t)r.length;
 958	isc_buffer_putuint16(buffer, dlen);
 959	isc_buffer_copyregion(buffer, &r);
 960	totallen += sizeof(dlen) + r.length;
 961
 962	do {
 963		dns_rdata_t rdata = DNS_RDATA_INIT;
 964		isc_region_t r;
 965
 966		dns_rdataset_current(rdataset, &rdata);
 967		dns_rdata_toregion(&rdata, &r);
 968		INSIST(r.length <= 0xffffU);
 969		dlen = (isc_uint16_t)r.length;
 970
 971		/*
 972		 * Copy the rdata into the buffer.  If the buffer is too small,
 973		 * grow it.  This should be rare, so we'll simply restart the
 974		 * entire procedure (or should we copy the old data and
 975		 * continue?).
 976		 */
 977		if (isc_buffer_availablelength(buffer) <
 978						 sizeof(dlen) + r.length) {
 979			int newlength;
 980			void *newmem;
 981
 982			newlength = buffer->length * 2;
 983			newmem = isc_mem_get(mctx, newlength);
 984			if (newmem == NULL)
 985				return (ISC_R_NOMEMORY);
 986			isc_mem_put(mctx, buffer->base, buffer->length);
 987			isc_buffer_init(buffer, newmem, newlength);
 988			goto restart;
 989		}
 990		isc_buffer_putuint16(buffer, dlen);
 991		isc_buffer_copyregion(buffer, &r);
 992		totallen += sizeof(dlen) + r.length;
 993
 994		result = dns_rdataset_next(rdataset);
 995	} while (result == ISC_R_SUCCESS);
 996
 997	if (result != ISC_R_NOMORE)
 998		return (result);
 999
1000	/*
1001	 * Fill in the total length field.
1002	 * XXX: this is a bit tricky.  Since we have already "used" the space
1003	 * for the total length in the buffer, we first remember the entire
1004	 * buffer length in the region, "rewind", and then write the value.
1005	 */
1006	isc_buffer_usedregion(buffer, &r);
1007	isc_buffer_clear(buffer);
1008	isc_buffer_putuint32(buffer, totallen);
1009	INSIST(isc_buffer_usedlength(buffer) < totallen);
1010
1011	/*
1012	 * Write the buffer contents to the raw master file.
1013	 */
1014	result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
1015
1016	if (result != ISC_R_SUCCESS) {
1017		UNEXPECTED_ERROR(__FILE__, __LINE__,
1018				 "raw master file write failed: %s",
1019				 isc_result_totext(result));
1020		return (result);
1021	}
1022
1023	return (result);
1024}
1025
1026static isc_result_t
1027dump_rdatasets_raw(isc_mem_t *mctx, dns_name_t *name,
1028		   dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
1029		   isc_buffer_t *buffer, FILE *f)
1030{
1031	isc_result_t result;
1032	dns_rdataset_t rdataset;
1033
1034	for (result = dns_rdatasetiter_first(rdsiter);
1035	     result == ISC_R_SUCCESS;
1036	     result = dns_rdatasetiter_next(rdsiter)) {
1037
1038		dns_rdataset_init(&rdataset);
1039		dns_rdatasetiter_current(rdsiter, &rdataset);
1040
1041		if (((rdataset.attributes & DNS_RDATASETATTR_NEGATIVE) != 0) &&
1042		    (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
1043			/* Omit negative cache entries */
1044		} else {
1045			result = dump_rdataset_raw(mctx, name, &rdataset,
1046						   buffer, f);
1047		}
1048		dns_rdataset_disassociate(&rdataset);
1049		if (result != ISC_R_SUCCESS)
1050			return (result);
1051	}
1052
1053	if (result == ISC_R_NOMORE)
1054		result = ISC_R_SUCCESS;
1055
1056	return (result);
1057}
1058
1059/*
1060 * Initial size of text conversion buffer.  The buffer is used
1061 * for several purposes: converting origin names, rdatasets,
1062 * $DATE timestamps, and comment strings for $TTL directives.
1063 *
1064 * When converting rdatasets, it is dynamically resized, but
1065 * when converting origins, timestamps, etc it is not.  Therefore,
1066 * the initial size must large enough to hold the longest possible
1067 * text representation of any domain name (for $ORIGIN).
1068 */
1069static const int initial_buffer_length = 1200;
1070
1071static isc_result_t
1072dumptostreaminc(dns_dumpctx_t *dctx);
1073
1074static void
1075dumpctx_destroy(dns_dumpctx_t *dctx) {
1076
1077	dctx->magic = 0;
1078	DESTROYLOCK(&dctx->lock);
1079	dns_dbiterator_destroy(&dctx->dbiter);
1080	if (dctx->version != NULL)
1081		dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
1082	dns_db_detach(&dctx->db);
1083	if (dctx->task != NULL)
1084		isc_task_detach(&dctx->task);
1085	if (dctx->file != NULL)
1086		isc_mem_free(dctx->mctx, dctx->file);
1087	if (dctx->tmpfile != NULL)
1088		isc_mem_free(dctx->mctx, dctx->tmpfile);
1089	isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(*dctx));
1090}
1091
1092void
1093dns_dumpctx_attach(dns_dumpctx_t *source, dns_dumpctx_t **target) {
1094
1095	REQUIRE(DNS_DCTX_VALID(source));
1096	REQUIRE(target != NULL && *target == NULL);
1097
1098	LOCK(&source->lock);
1099	INSIST(source->references > 0);
1100	source->references++;
1101	INSIST(source->references != 0);	/* Overflow? */
1102	UNLOCK(&source->lock);
1103
1104	*target = source;
1105}
1106
1107void
1108dns_dumpctx_detach(dns_dumpctx_t **dctxp) {
1109	dns_dumpctx_t *dctx;
1110	isc_boolean_t need_destroy = ISC_FALSE;
1111
1112	REQUIRE(dctxp != NULL);
1113	dctx = *dctxp;
1114	REQUIRE(DNS_DCTX_VALID(dctx));
1115
1116	*dctxp = NULL;
1117
1118	LOCK(&dctx->lock);
1119	INSIST(dctx->references != 0);
1120	dctx->references--;
1121	if (dctx->references == 0)
1122		need_destroy = ISC_TRUE;
1123	UNLOCK(&dctx->lock);
1124	if (need_destroy)
1125		dumpctx_destroy(dctx);
1126}
1127
1128dns_dbversion_t *
1129dns_dumpctx_version(dns_dumpctx_t *dctx) {
1130	REQUIRE(DNS_DCTX_VALID(dctx));
1131	return (dctx->version);
1132}
1133
1134dns_db_t *
1135dns_dumpctx_db(dns_dumpctx_t *dctx) {
1136	REQUIRE(DNS_DCTX_VALID(dctx));
1137	return (dctx->db);
1138}
1139
1140void
1141dns_dumpctx_cancel(dns_dumpctx_t *dctx) {
1142	REQUIRE(DNS_DCTX_VALID(dctx));
1143
1144	LOCK(&dctx->lock);
1145	dctx->canceled = ISC_TRUE;
1146	UNLOCK(&dctx->lock);
1147}
1148
1149static isc_result_t
1150closeandrename(FILE *f, isc_result_t result, const char *temp, const char *file)
1151{
1152	isc_result_t tresult;
1153	isc_boolean_t logit = ISC_TF(result == ISC_R_SUCCESS);
1154
1155	if (result == ISC_R_SUCCESS)
1156		result = isc_stdio_sync(f);
1157	if (result != ISC_R_SUCCESS && logit) {
1158		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1159			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1160			      "dumping master file: %s: fsync: %s",
1161			      temp, isc_result_totext(result));
1162		logit = ISC_FALSE;
1163	}
1164	tresult = isc_stdio_close(f);
1165	if (result == ISC_R_SUCCESS)
1166		result = tresult;
1167	if (result != ISC_R_SUCCESS && logit) {
1168		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1169			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1170			      "dumping master file: %s: fclose: %s",
1171			      temp, isc_result_totext(result));
1172		logit = ISC_FALSE;
1173	}
1174	if (result == ISC_R_SUCCESS)
1175		result = isc_file_rename(temp, file);
1176	else
1177		(void)isc_file_remove(temp);
1178	if (result != ISC_R_SUCCESS && logit) {
1179		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1180			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1181			      "dumping master file: rename: %s: %s",
1182			      file, isc_result_totext(result));
1183	}
1184	return (result);
1185}
1186
1187static void
1188dump_quantum(isc_task_t *task, isc_event_t *event) {
1189	isc_result_t result;
1190	isc_result_t tresult;
1191	dns_dumpctx_t *dctx;
1192
1193	REQUIRE(event != NULL);
1194	dctx = event->ev_arg;
1195	REQUIRE(DNS_DCTX_VALID(dctx));
1196	if (dctx->canceled)
1197		result = ISC_R_CANCELED;
1198	else
1199		result = dumptostreaminc(dctx);
1200	if (result == DNS_R_CONTINUE) {
1201		event->ev_arg = dctx;
1202		isc_task_send(task, &event);
1203		return;
1204	}
1205
1206	if (dctx->file != NULL) {
1207		tresult = closeandrename(dctx->f, result,
1208					 dctx->tmpfile, dctx->file);
1209		if (tresult != ISC_R_SUCCESS && result == ISC_R_SUCCESS)
1210			result = tresult;
1211	}
1212	(dctx->done)(dctx->done_arg, result);
1213	isc_event_free(&event);
1214	dns_dumpctx_detach(&dctx);
1215}
1216
1217static isc_result_t
1218task_send(dns_dumpctx_t *dctx) {
1219	isc_event_t *event;
1220
1221	event = isc_event_allocate(dctx->mctx, NULL, DNS_EVENT_DUMPQUANTUM,
1222				   dump_quantum, dctx, sizeof(*event));
1223	if (event == NULL)
1224		return (ISC_R_NOMEMORY);
1225	isc_task_send(dctx->task, &event);
1226	return (ISC_R_SUCCESS);
1227}
1228
1229static isc_result_t
1230dumpctx_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1231	       const dns_master_style_t *style, FILE *f, dns_dumpctx_t **dctxp,
1232	       dns_masterformat_t format)
1233{
1234	dns_dumpctx_t *dctx;
1235	isc_result_t result;
1236	unsigned int options;
1237
1238	dctx = isc_mem_get(mctx, sizeof(*dctx));
1239	if (dctx == NULL)
1240		return (ISC_R_NOMEMORY);
1241
1242	dctx->mctx = NULL;
1243	dctx->f = f;
1244	dctx->dbiter = NULL;
1245	dctx->db = NULL;
1246	dctx->version = NULL;
1247	dctx->done = NULL;
1248	dctx->done_arg = NULL;
1249	dctx->task = NULL;
1250	dctx->nodes = 0;
1251	dctx->first = ISC_TRUE;
1252	dctx->canceled = ISC_FALSE;
1253	dctx->file = NULL;
1254	dctx->tmpfile = NULL;
1255	dctx->format = format;
1256
1257	switch (format) {
1258	case dns_masterformat_text:
1259		dctx->dumpsets = dump_rdatasets_text;
1260		break;
1261	case dns_masterformat_raw:
1262		dctx->dumpsets = dump_rdatasets_raw;
1263		break;
1264	default:
1265		INSIST(0);
1266		break;
1267	}
1268
1269	result = totext_ctx_init(style, &dctx->tctx);
1270	if (result != ISC_R_SUCCESS) {
1271		UNEXPECTED_ERROR(__FILE__, __LINE__,
1272				 "could not set master file style");
1273		goto cleanup;
1274	}
1275
1276	isc_stdtime_get(&dctx->now);
1277	dns_db_attach(db, &dctx->db);
1278
1279	dctx->do_date = dns_db_iscache(dctx->db);
1280
1281	if (dctx->format == dns_masterformat_text &&
1282	    (dctx->tctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) {
1283		options = DNS_DB_RELATIVENAMES;
1284	} else
1285		options = 0;
1286	result = dns_db_createiterator(dctx->db, options, &dctx->dbiter);
1287	if (result != ISC_R_SUCCESS)
1288		goto cleanup;
1289
1290	result = isc_mutex_init(&dctx->lock);
1291	if (result != ISC_R_SUCCESS)
1292		goto cleanup;
1293	if (version != NULL)
1294		dns_db_attachversion(dctx->db, version, &dctx->version);
1295	else if (!dns_db_iscache(db))
1296		dns_db_currentversion(dctx->db, &dctx->version);
1297	isc_mem_attach(mctx, &dctx->mctx);
1298	dctx->references = 1;
1299	dctx->magic = DNS_DCTX_MAGIC;
1300	*dctxp = dctx;
1301	return (ISC_R_SUCCESS);
1302
1303 cleanup:
1304	if (dctx->dbiter != NULL)
1305		dns_dbiterator_destroy(&dctx->dbiter);
1306	if (dctx->db != NULL)
1307		dns_db_detach(&dctx->db);
1308	if (dctx != NULL)
1309		isc_mem_put(mctx, dctx, sizeof(*dctx));
1310	return (result);
1311}
1312
1313static isc_result_t
1314dumptostreaminc(dns_dumpctx_t *dctx) {
1315	isc_result_t result;
1316	isc_buffer_t buffer;
1317	char *bufmem;
1318	isc_region_t r;
1319	dns_name_t *name;
1320	dns_fixedname_t fixname;
1321	unsigned int nodes;
1322	dns_masterrawheader_t rawheader;
1323	isc_uint32_t now32;
1324	isc_time_t start;
1325
1326	bufmem = isc_mem_get(dctx->mctx, initial_buffer_length);
1327	if (bufmem == NULL)
1328		return (ISC_R_NOMEMORY);
1329
1330	isc_buffer_init(&buffer, bufmem, initial_buffer_length);
1331
1332	dns_fixedname_init(&fixname);
1333	name = dns_fixedname_name(&fixname);
1334
1335	if (dctx->first) {
1336		switch (dctx->format) {
1337		case dns_masterformat_text:
1338			/*
1339			 * If the database has cache semantics, output an
1340			 * RFC2540 $DATE directive so that the TTLs can be
1341			 * adjusted when it is reloaded.  For zones it is not
1342			 * really needed, and it would make the file
1343			 * incompatible with pre-RFC2540 software, so we omit
1344			 * it in the zone case.
1345			 */
1346			if (dctx->do_date) {
1347				result = dns_time32_totext(dctx->now, &buffer);
1348				RUNTIME_CHECK(result == ISC_R_SUCCESS);
1349				isc_buffer_usedregion(&buffer, &r);
1350				fprintf(dctx->f, "$DATE %.*s\n",
1351					(int) r.length, (char *) r.base);
1352			}
1353			break;
1354		case dns_masterformat_raw:
1355			r.base = (unsigned char *)&rawheader;
1356			r.length = sizeof(rawheader);
1357			isc_buffer_region(&buffer, &r);
1358			isc_buffer_putuint32(&buffer, dns_masterformat_raw);
1359			isc_buffer_putuint32(&buffer, DNS_RAWFORMAT_VERSION);
1360#if !defined(STDTIME_ON_32BITS) || (STDTIME_ON_32BITS + 0) != 1
1361			/*
1362			 * We assume isc_stdtime_t is a 32-bit integer,
1363			 * which should be the case on most cases.
1364			 * If it turns out to be uncommon, we'll need
1365			 * to bump the version number and revise the
1366			 * header format.
1367			 */
1368			isc_log_write(dns_lctx,
1369				      ISC_LOGCATEGORY_GENERAL,
1370				      DNS_LOGMODULE_MASTERDUMP,
1371				      ISC_LOG_INFO,
1372				      "dumping master file in raw "
1373				      "format: stdtime is not 32bits");
1374			now32 = 0;
1375#else
1376			now32 = dctx->now;
1377#endif
1378			isc_buffer_putuint32(&buffer, now32);
1379			INSIST(isc_buffer_usedlength(&buffer) <=
1380			       sizeof(rawheader));
1381			result = isc_stdio_write(buffer.base, 1,
1382						 isc_buffer_usedlength(&buffer),
1383						 dctx->f, NULL);
1384			if (result != ISC_R_SUCCESS)
1385				return (result);
1386			isc_buffer_clear(&buffer);
1387			break;
1388		default:
1389			INSIST(0);
1390		}
1391
1392		result = dns_dbiterator_first(dctx->dbiter);
1393		dctx->first = ISC_FALSE;
1394	} else
1395		result = ISC_R_SUCCESS;
1396
1397	nodes = dctx->nodes;
1398	isc_time_now(&start);
1399	while (result == ISC_R_SUCCESS && (dctx->nodes == 0 || nodes--)) {
1400		dns_rdatasetiter_t *rdsiter = NULL;
1401		dns_dbnode_t *node = NULL;
1402
1403		result = dns_dbiterator_current(dctx->dbiter, &node, name);
1404		if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
1405			break;
1406		if (result == DNS_R_NEWORIGIN) {
1407			dns_name_t *origin =
1408				dns_fixedname_name(&dctx->tctx.origin_fixname);
1409			result = dns_dbiterator_origin(dctx->dbiter, origin);
1410			RUNTIME_CHECK(result == ISC_R_SUCCESS);
1411			if ((dctx->tctx.style.flags & DNS_STYLEFLAG_REL_DATA) != 0)
1412				dctx->tctx.origin = origin;
1413			dctx->tctx.neworigin = origin;
1414		}
1415		result = dns_db_allrdatasets(dctx->db, node, dctx->version,
1416					     dctx->now, &rdsiter);
1417		if (result != ISC_R_SUCCESS) {
1418			dns_db_detachnode(dctx->db, &node);
1419			goto fail;
1420		}
1421		result = (dctx->dumpsets)(dctx->mctx, name, rdsiter,
1422					  &dctx->tctx, &buffer, dctx->f);
1423		dns_rdatasetiter_destroy(&rdsiter);
1424		if (result != ISC_R_SUCCESS) {
1425			dns_db_detachnode(dctx->db, &node);
1426			goto fail;
1427		}
1428		dns_db_detachnode(dctx->db, &node);
1429		result = dns_dbiterator_next(dctx->dbiter);
1430	}
1431
1432	/*
1433	 * Work out how many nodes can be written in the time between
1434	 * two requests to the nameserver.  Smooth the resulting number and
1435	 * use it as a estimate for the number of nodes to be written in the
1436	 * next iteration.
1437	 */
1438	if (dctx->nodes != 0 && result == ISC_R_SUCCESS) {
1439		unsigned int pps = dns_pps;	/* packets per second */
1440		unsigned int interval;
1441		isc_uint64_t usecs;
1442		isc_time_t end;
1443
1444		isc_time_now(&end);
1445		if (pps < 100)
1446			pps = 100;
1447		interval = 1000000 / pps;	/* interval in usecs */
1448		if (interval == 0)
1449			interval = 1;
1450		usecs = isc_time_microdiff(&end, &start);
1451		if (usecs == 0) {
1452			dctx->nodes = dctx->nodes * 2;
1453			if (dctx->nodes > 1000)
1454				dctx->nodes = 1000;
1455		} else {
1456			nodes = dctx->nodes * interval;
1457			nodes /= (unsigned int)usecs;
1458			if (nodes == 0)
1459				nodes = 1;
1460			else if (nodes > 1000)
1461				nodes = 1000;
1462
1463			/* Smooth and assign. */
1464			dctx->nodes = (nodes + dctx->nodes * 7) / 8;
1465
1466			isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1467				      DNS_LOGMODULE_MASTERDUMP,
1468				      ISC_LOG_DEBUG(1),
1469				      "dumptostreaminc(%p) new nodes -> %d\n",
1470				      dctx, dctx->nodes);
1471		}
1472		result = DNS_R_CONTINUE;
1473	} else if (result == ISC_R_NOMORE)
1474		result = ISC_R_SUCCESS;
1475 fail:
1476	RUNTIME_CHECK(dns_dbiterator_pause(dctx->dbiter) == ISC_R_SUCCESS);
1477	isc_mem_put(dctx->mctx, buffer.base, buffer.length);
1478	return (result);
1479}
1480
1481isc_result_t
1482dns_master_dumptostreaminc(isc_mem_t *mctx, dns_db_t *db,
1483			   dns_dbversion_t *version,
1484			   const dns_master_style_t *style,
1485			   FILE *f, isc_task_t *task,
1486			   dns_dumpdonefunc_t done, void *done_arg,
1487			   dns_dumpctx_t **dctxp)
1488{
1489	dns_dumpctx_t *dctx = NULL;
1490	isc_result_t result;
1491
1492	REQUIRE(task != NULL);
1493	REQUIRE(f != NULL);
1494	REQUIRE(done != NULL);
1495
1496	result = dumpctx_create(mctx, db, version, style, f, &dctx,
1497				dns_masterformat_text);
1498	if (result != ISC_R_SUCCESS)
1499		return (result);
1500	isc_task_attach(task, &dctx->task);
1501	dctx->done = done;
1502	dctx->done_arg = done_arg;
1503	dctx->nodes = 100;
1504
1505	result = task_send(dctx);
1506	if (result == ISC_R_SUCCESS) {
1507		dns_dumpctx_attach(dctx, dctxp);
1508		return (DNS_R_CONTINUE);
1509	}
1510
1511	dns_dumpctx_detach(&dctx);
1512	return (result);
1513}
1514
1515/*
1516 * Dump an entire database into a master file.
1517 */
1518isc_result_t
1519dns_master_dumptostream(isc_mem_t *mctx, dns_db_t *db,
1520			dns_dbversion_t *version,
1521			const dns_master_style_t *style,
1522			FILE *f)
1523{
1524	return (dns_master_dumptostream2(mctx, db, version, style,
1525					 dns_masterformat_text, f));
1526}
1527
1528isc_result_t
1529dns_master_dumptostream2(isc_mem_t *mctx, dns_db_t *db,
1530			 dns_dbversion_t *version,
1531			 const dns_master_style_t *style,
1532			 dns_masterformat_t format, FILE *f)
1533{
1534	dns_dumpctx_t *dctx = NULL;
1535	isc_result_t result;
1536
1537	result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1538	if (result != ISC_R_SUCCESS)
1539		return (result);
1540
1541	result = dumptostreaminc(dctx);
1542	INSIST(result != DNS_R_CONTINUE);
1543	dns_dumpctx_detach(&dctx);
1544	return (result);
1545}
1546
1547static isc_result_t
1548opentmp(isc_mem_t *mctx, const char *file, char **tempp, FILE **fp) {
1549	FILE *f = NULL;
1550	isc_result_t result;
1551	char *tempname = NULL;
1552	int tempnamelen;
1553
1554	tempnamelen = strlen(file) + 20;
1555	tempname = isc_mem_allocate(mctx, tempnamelen);
1556	if (tempname == NULL)
1557		return (ISC_R_NOMEMORY);
1558
1559	result = isc_file_mktemplate(file, tempname, tempnamelen);
1560	if (result != ISC_R_SUCCESS)
1561		goto cleanup;
1562
1563	result = isc_file_openunique(tempname, &f);
1564	if (result != ISC_R_SUCCESS) {
1565		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1566			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1567			      "dumping master file: %s: open: %s",
1568			      tempname, isc_result_totext(result));
1569		goto cleanup;
1570	}
1571	*tempp = tempname;
1572	*fp = f;
1573	return (ISC_R_SUCCESS);
1574
1575cleanup:
1576	isc_mem_free(mctx, tempname);
1577	return (result);
1578}
1579
1580isc_result_t
1581dns_master_dumpinc(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1582		   const dns_master_style_t *style, const char *filename,
1583		   isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
1584		   dns_dumpctx_t **dctxp)
1585{
1586	return (dns_master_dumpinc2(mctx, db, version, style, filename, task,
1587				    done, done_arg, dctxp,
1588				    dns_masterformat_text));
1589}
1590
1591isc_result_t
1592dns_master_dumpinc2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1593		    const dns_master_style_t *style, const char *filename,
1594		    isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
1595		    dns_dumpctx_t **dctxp, dns_masterformat_t format)
1596{
1597	FILE *f = NULL;
1598	isc_result_t result;
1599	char *tempname = NULL;
1600	char *file = NULL;
1601	dns_dumpctx_t *dctx = NULL;
1602
1603	file = isc_mem_strdup(mctx, filename);
1604	if (file == NULL)
1605		return (ISC_R_NOMEMORY);
1606
1607	result = opentmp(mctx, filename, &tempname, &f);
1608	if (result != ISC_R_SUCCESS)
1609		goto cleanup;
1610
1611	result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1612	if (result != ISC_R_SUCCESS) {
1613		(void)isc_stdio_close(f);
1614		(void)isc_file_remove(tempname);
1615		goto cleanup;
1616	}
1617
1618	isc_task_attach(task, &dctx->task);
1619	dctx->done = done;
1620	dctx->done_arg = done_arg;
1621	dctx->nodes = 100;
1622	dctx->file = file;
1623	file = NULL;
1624	dctx->tmpfile = tempname;
1625	tempname = NULL;
1626
1627	result = task_send(dctx);
1628	if (result == ISC_R_SUCCESS) {
1629		dns_dumpctx_attach(dctx, dctxp);
1630		return (DNS_R_CONTINUE);
1631	}
1632
1633 cleanup:
1634	if (dctx != NULL)
1635		dns_dumpctx_detach(&dctx);
1636	if (file != NULL)
1637		isc_mem_free(mctx, file);
1638	if (tempname != NULL)
1639		isc_mem_free(mctx, tempname);
1640	return (result);
1641}
1642
1643isc_result_t
1644dns_master_dump(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1645		const dns_master_style_t *style, const char *filename)
1646{
1647	return (dns_master_dump2(mctx, db, version, style, filename,
1648				 dns_masterformat_text));
1649}
1650
1651isc_result_t
1652dns_master_dump2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1653		 const dns_master_style_t *style, const char *filename,
1654		 dns_masterformat_t format)
1655{
1656	FILE *f = NULL;
1657	isc_result_t result;
1658	char *tempname;
1659	dns_dumpctx_t *dctx = NULL;
1660
1661	result = opentmp(mctx, filename, &tempname, &f);
1662	if (result != ISC_R_SUCCESS)
1663		return (result);
1664
1665	result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1666	if (result != ISC_R_SUCCESS)
1667		goto cleanup;
1668
1669	result = dumptostreaminc(dctx);
1670	INSIST(result != DNS_R_CONTINUE);
1671	dns_dumpctx_detach(&dctx);
1672
1673	result = closeandrename(f, result, tempname, filename);
1674
1675 cleanup:
1676	isc_mem_free(mctx, tempname);
1677	return (result);
1678}
1679
1680/*
1681 * Dump a database node into a master file.
1682 * XXX: this function assumes the text format.
1683 */
1684isc_result_t
1685dns_master_dumpnodetostream(isc_mem_t *mctx, dns_db_t *db,
1686			    dns_dbversion_t *version,
1687			    dns_dbnode_t *node, dns_name_t *name,
1688			    const dns_master_style_t *style,
1689			    FILE *f)
1690{
1691	isc_result_t result;
1692	isc_buffer_t buffer;
1693	char *bufmem;
1694	isc_stdtime_t now;
1695	dns_totext_ctx_t ctx;
1696	dns_rdatasetiter_t *rdsiter = NULL;
1697
1698	result = totext_ctx_init(style, &ctx);
1699	if (result != ISC_R_SUCCESS) {
1700		UNEXPECTED_ERROR(__FILE__, __LINE__,
1701				 "could not set master file style");
1702		return (ISC_R_UNEXPECTED);
1703	}
1704
1705	isc_stdtime_get(&now);
1706
1707	bufmem = isc_mem_get(mctx, initial_buffer_length);
1708	if (bufmem == NULL)
1709		return (ISC_R_NOMEMORY);
1710
1711	isc_buffer_init(&buffer, bufmem, initial_buffer_length);
1712
1713	result = dns_db_allrdatasets(db, node, version, now, &rdsiter);
1714	if (result != ISC_R_SUCCESS)
1715		goto failure;
1716	result = dump_rdatasets_text(mctx, name, rdsiter, &ctx, &buffer, f);
1717	if (result != ISC_R_SUCCESS)
1718		goto failure;
1719	dns_rdatasetiter_destroy(&rdsiter);
1720
1721	result = ISC_R_SUCCESS;
1722
1723 failure:
1724	isc_mem_put(mctx, buffer.base, buffer.length);
1725	return (result);
1726}
1727
1728isc_result_t
1729dns_master_dumpnode(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1730		    dns_dbnode_t *node, dns_name_t *name,
1731		    const dns_master_style_t *style, const char *filename)
1732{
1733	FILE *f = NULL;
1734	isc_result_t result;
1735
1736	result = isc_stdio_open(filename, "w", &f);
1737	if (result != ISC_R_SUCCESS) {
1738		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1739			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1740			      "dumping node to file: %s: open: %s", filename,
1741			      isc_result_totext(result));
1742		return (ISC_R_UNEXPECTED);
1743	}
1744
1745	result = dns_master_dumpnodetostream(mctx, db, version, node, name,
1746					     style, f);
1747	if (result != ISC_R_SUCCESS) {
1748		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1749			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1750			      "dumping master file: %s: dump: %s", filename,
1751			      isc_result_totext(result));
1752		(void)isc_stdio_close(f);
1753		return (ISC_R_UNEXPECTED);
1754	}
1755
1756	result = isc_stdio_close(f);
1757	if (result != ISC_R_SUCCESS) {
1758		isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1759			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1760			      "dumping master file: %s: close: %s", filename,
1761			      isc_result_totext(result));
1762		return (ISC_R_UNEXPECTED);
1763	}
1764
1765	return (result);
1766}
1767#endif /* BIND9 */
1768
1769isc_result_t
1770dns_master_stylecreate(dns_master_style_t **stylep, unsigned int flags,
1771		       unsigned int ttl_column, unsigned int class_column,
1772		       unsigned int type_column, unsigned int rdata_column,
1773		       unsigned int line_length, unsigned int tab_width,
1774		       isc_mem_t *mctx)
1775{
1776	dns_master_style_t *style;
1777
1778	REQUIRE(stylep != NULL && *stylep == NULL);
1779	style = isc_mem_get(mctx, sizeof(*style));
1780	if (style == NULL)
1781		return (ISC_R_NOMEMORY);
1782
1783	style->flags = flags;
1784	style->ttl_column = ttl_column;
1785	style->class_column = class_column;
1786	style->type_column = type_column;
1787	style->rdata_column = rdata_column;
1788	style->line_length = line_length;
1789	style->tab_width = tab_width;
1790
1791	*stylep = style;
1792	return (ISC_R_SUCCESS);
1793}
1794
1795void
1796dns_master_styledestroy(dns_master_style_t **stylep, isc_mem_t *mctx) {
1797	dns_master_style_t *style;
1798
1799	REQUIRE(stylep != NULL && *stylep != NULL);
1800	style = *stylep;
1801	*stylep = NULL;
1802	isc_mem_put(mctx, style, sizeof(*style));
1803}