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