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

#include <config.h>

20
21
22
23
#include <errno.h>		/* XXX */

#include <isc/mem.h>
#include <isc/string.h>
Bob Halley's avatar
Bob Halley committed
24
#include <isc/util.h>
25

26
27
#include <dns/db.h>
#include <dns/dbiterator.h>
28
#include <dns/fixedname.h>
29
#include <dns/masterdump.h>
30
31
32
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
33
34
#include <dns/rdatatype.h>
#include <dns/result.h>
35
36
37
38
#include <dns/time.h>
#include <dns/ttl.h>

#define RETERR(x) do { \
39
	isc_result_t __r = (x); \
40
	if (__r != ISC_R_SUCCESS) \
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
		return (__r); \
	} while (0)

struct dns_master_style {
	unsigned int flags;		/* DNS_STYLEFLAG_* */
	unsigned int ttl_column;
	unsigned int class_column;
	unsigned int type_column;
	unsigned int rdata_column;
	unsigned int line_length;
	unsigned int tab_width;
};

/*
 * Flags affecting master file formatting.  Flags 0x0000FFFF
 * define the formatting of the rdata part and are defined in
 * rdata.h.
 */
 
/* Omit the owner name when possible. */
#define DNS_STYLEFLAG_OMIT_OWNER	0x00010000U

/*
 * Omit the TTL when possible.  If DNS_STYLEFLAG_TTL is 
 * also set, this means no TTLs are ever printed
 * because $TTL directives are generated before every
 * change in the TTL.  In this case, no columns need to 
 * be reserved for the TTL.  Master files generated with
 * these options will be rejected by BIND 4.x because it
 * does not recognize the $TTL directive.
 *
 * If DNS_STYLEFLAG_TTL is not also set, the TTL will be 
 * omitted when it is equal to the previous TTL.
 * This is correct according to RFC1035, but the 
 * TTLs may be silently misinterpreted by older 
 * versions of BIND which use the SOA MINTTL as a
 * default TTL value.
 */
#define DNS_STYLEFLAG_OMIT_TTL		0x00020000U

/* Omit the class when possible. */
#define DNS_STYLEFLAG_OMIT_CLASS	0x00040000U

/* Output $TTL directives. */
#define DNS_STYLEFLAG_TTL		0x00080000U

/*
 * Output $ORIGIN directives and print owner names relative to
 * the origin when possible.
 */
#define DNS_STYLEFLAG_REL_OWNER		0x00100000U

/* Print domain names in RR data in relative form when possible. 
   For this to take effect, DNS_STYLEFLAG_REL_OWNER must also be set. */
#define DNS_STYLEFLAG_REL_DATA		0x00200000U


/*
 * The maximum length of the newline+indentation that is output
 * when inserting a line break in an RR.  This effectively puts an 
 * upper limits on the value of "rdata_column", because if it is
 * very large, the tabs and spaces needed to reach it will not fit.
 */
#define DNS_TOTEXT_LINEBREAK_MAXLEN 100

/*
 * Context structure for a masterfile dump in progress.
 */
typedef struct dns_totext_ctx {
	dns_master_style_t	style;
	isc_boolean_t 		class_printed;
112
113
114
	char *			linebreak;
	char 			linebreak_buf[DNS_TOTEXT_LINEBREAK_MAXLEN];
	dns_name_t *		origin;
115
116
117
118
119
120
121
122
123
124
125
	dns_fixedname_t		origin_fixname;
	isc_uint32_t 		current_ttl;
	isc_boolean_t 		current_ttl_valid;
} dns_totext_ctx_t;

/*
 * The default master file style.
 *
 * Because the TTL is always omitted, and the class is almost always
 * omitted, neither is allocated any columns.
 */
Bob Halley's avatar
Bob Halley committed
126
const dns_master_style_t 
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
dns_master_style_default = {
	DNS_STYLEFLAG_OMIT_OWNER |
	DNS_STYLEFLAG_OMIT_CLASS |
	DNS_STYLEFLAG_REL_OWNER |
	DNS_STYLEFLAG_REL_DATA |
	DNS_STYLEFLAG_OMIT_TTL |
	DNS_STYLEFLAG_TTL |
	DNS_STYLEFLAG_COMMENT |
	DNS_STYLEFLAG_MULTILINE,
	24, 24, 24, 32, 80, 8 
};

/*
 * A style suitable for dns_rdataset_totext().
 */
dns_master_style_t 
dns_masterfile_style_debug = {
        DNS_STYLEFLAG_REL_OWNER,
	24, 32, 40, 48, 80, 8
};


#define N_SPACES 10
150
static char spaces[N_SPACES+1] = "          ";
151
152

#define N_TABS 10
153
static char tabs[N_TABS+1] = "\t\t\t\t\t\t\t\t\t\t";
154

155
156


157
158
159
160
161
/*
 * Output tabs and spaces to go from column '*current' to 
 * column 'to', and update '*current' to reflect the new
 * current column.
 */
162
static isc_result_t
Bob Halley's avatar
Bob Halley committed
163
164
165
indent(unsigned int *current, unsigned int to, int tabwidth,
       isc_buffer_t *target)
{
166
167
	isc_region_t r;
	unsigned char *p;
Bob Halley's avatar
lint    
Bob Halley committed
168
169
	unsigned int from;
	int ntabs, nspaces, t;
170
171
172
173
174
175
176
177
178
179
180

	from = *current;

	if (to < from + 1)
		to = from + 1;

	ntabs = to / tabwidth - from / tabwidth;
	if (ntabs < 0) 
		ntabs = 0;

	if (ntabs > 0) {
181
		isc_buffer_availableregion(target, &r);
182
		if (r.length < (unsigned) ntabs)
183
			return (ISC_R_NOSPACE);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		p = r.base;
	
		t = ntabs;
		while (t) {
			int n = t;
			if (n > N_TABS)
				n = N_TABS;
			memcpy(p, tabs, n);
			p += n;
			t -= n;
		}
		isc_buffer_add(target, ntabs);
		from = (to / tabwidth) * tabwidth;
	}

	nspaces = to - from;
	INSIST(nspaces >= 0);

202
	isc_buffer_availableregion(target, &r);
203
	if (r.length < (unsigned) nspaces)
204
		return (ISC_R_NOSPACE);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	p = r.base;

	t = nspaces;	
	while (t) {
		int n = t;
		if (n > N_SPACES)
			n = N_SPACES;
		memcpy(p, spaces, n);
		p += n;
		t -= n;
	}
	isc_buffer_add(target, nspaces);	

	*current = to;
219
	return (ISC_R_SUCCESS);
220
221
}

222
static isc_result_t
223
totext_ctx_init(const dns_master_style_t *style, dns_totext_ctx_t *ctx) {
224
	isc_result_t result;
225
226
227
228
229
	
	ctx->style = *style;
	REQUIRE(style->tab_width != 0);
	dns_fixedname_init(&ctx->origin_fixname);

230
231
232
	/*
	 * Set up the line break string if needed.
	 */
233
234
235
	if ((ctx->style.flags & DNS_STYLEFLAG_MULTILINE) != 0) {
		isc_buffer_t buf;
		isc_region_t r;
Bob Halley's avatar
Bob Halley committed
236
		unsigned int col = 0;
237

238
		isc_buffer_init(&buf, ctx->linebreak_buf,
239
				sizeof(ctx->linebreak_buf));
240
		
241
		isc_buffer_availableregion(&buf, &r);
242
		if (r.length < 1)
Bob Halley's avatar
Bob Halley committed
243
			return (DNS_R_TEXTTOOLONG);
244
245
246
247
248
249
		r.base[0] = '\n';
		isc_buffer_add(&buf, 1);

		result = indent(&col, ctx->style.rdata_column, 
				ctx->style.tab_width, &buf);
		/*
250
		 * Do not return ISC_R_NOSPACE if the line break string
251
		 * buffer is too small, because that would just make 
252
		 * dump_rdataset() retry indenfinitely with ever 
253
		 * bigger target buffers.  That's a different buffer,
Bob Halley's avatar
Bob Halley committed
254
		 * so it won't help.  Use DNS_R_TEXTTOOLONG as a substitute.
255
		 */
256
		if (result == ISC_R_NOSPACE)
Bob Halley's avatar
Bob Halley committed
257
			return (DNS_R_TEXTTOOLONG);
258
		if (result != ISC_R_SUCCESS)
259
260
			return (result);
		
261
		isc_buffer_availableregion(&buf, &r);
262
		if (r.length < 1)
Bob Halley's avatar
Bob Halley committed
263
			return (DNS_R_TEXTTOOLONG);
264
265
		r.base[0] = '\0';
		isc_buffer_add(&buf, 1);
266
		ctx->linebreak = ctx->linebreak_buf;
267
	} else {
268
		ctx->linebreak = NULL;
269
270
271
	}

	ctx->class_printed = ISC_FALSE;
272
	ctx->origin = NULL;
273
	
274
	return (ISC_R_SUCCESS);
275
276
277
278
279
280
}

#define INDENT_TO(col) \
	do { \
		 if ((result = indent(&column, ctx->style.col, \
				      ctx->style.tab_width, target)) \
281
		     != ISC_R_SUCCESS) \
282
283
284
285
286
287
288
289
290
291
292
			    return (result); \
        } while (0)


/*
 * Convert 'rdataset' to master file text format according to 'ctx',
 * storing the result in 'target'.  If 'owner_name' is NULL, it
 * is omitted; otherwise 'owner_name' must be valid and have at least
 * one label.
 */

293
static isc_result_t
294
295
296
297
298
299
rdataset_totext(dns_rdataset_t *rdataset,
		dns_name_t *owner_name,
		dns_totext_ctx_t *ctx,
		isc_boolean_t omit_final_dot,
		isc_buffer_t *target)
{
300
	isc_result_t result;
301
302
303
304
305
306
307
308
	unsigned int column;
	isc_boolean_t first = ISC_TRUE;
	isc_uint32_t current_ttl;
	isc_boolean_t current_ttl_valid;
	
	REQUIRE(DNS_RDATASET_VALID(rdataset));
	
	result = dns_rdataset_first(rdataset);
309
	REQUIRE(result == ISC_R_SUCCESS);
310
311
312
313
314
315
316

	current_ttl = ctx->current_ttl;
	current_ttl_valid = ctx->current_ttl_valid;

	do {
		column = 0;
		
317
318
319
		/*
		 * Owner name.
		 */
320
		if (owner_name != NULL &&
321
322
		    ! ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0 &&
		       !first))
323
324
325
326
327
328
329
330
		{
			unsigned int name_start = target->used;
			RETERR(dns_name_totext(owner_name,
					       omit_final_dot,
					       target));
			column += target->used - name_start;
		}

331
332
333
		/*
		 * TTL.
		 */
334
335
336
337
338
339
340
341
342
343
344
		if (! ((ctx->style.flags & DNS_STYLEFLAG_OMIT_TTL) != 0 &&
		       current_ttl_valid &&
		       rdataset->ttl == current_ttl))
		{
			char ttlbuf[64];
			isc_region_t r;
			unsigned int length;

			INDENT_TO(ttl_column);
			length = sprintf(ttlbuf, "%u", rdataset->ttl);
			INSIST(length <= sizeof ttlbuf);
345
			isc_buffer_availableregion(target, &r);
346
			if (r.length < length)
347
				return (ISC_R_NOSPACE);
348
349
350
351
352
353
354
355
			memcpy(r.base, ttlbuf, length);
			isc_buffer_add(target, length);
			column += length;

			/*
			 * If the $TTL directive is not in use, the TTL we 
			 * just printed becomes the default for subsequent RRs.
			 */
356
			if ((ctx->style.flags & DNS_STYLEFLAG_TTL) == 0) {
357
358
359
360
361
				current_ttl = rdataset->ttl;
				current_ttl_valid = ISC_TRUE;
			}
		}

362
363
364
		/*
		 * Class.
		 */
365
366
367
368
369
370
		if ((ctx->style.flags & DNS_STYLEFLAG_OMIT_CLASS) == 0 ||
		    ctx->class_printed == ISC_FALSE)
		{
			unsigned int class_start;
			INDENT_TO(class_column);
			class_start = target->used;
371
372
			result = dns_rdataclass_totext(rdataset->rdclass,
						       target);
373
			if (result != ISC_R_SUCCESS)
374
375
376
377
				return (result);
			column += (target->used - class_start);
		}

378
379
380
		/*
		 * Type.
		 */
381
382
383
384
385
		{
			unsigned int type_start;
			INDENT_TO(type_column);
			type_start = target->used;
			result = dns_rdatatype_totext(rdataset->type, target);
386
			if (result != ISC_R_SUCCESS)
387
388
389
390
				return (result);
			column += (target->used - type_start);
		}

391
392
393
		/*
		 * Rdata.
		 */ 
394
395
396
397
398
399
400
		{
			dns_rdata_t rdata;
			isc_region_t r;

			INDENT_TO(rdata_column);
			dns_rdataset_current(rdataset, &rdata);

401
402
403
404
405
406
407
			RETERR(dns_rdata_tofmttext(&rdata,
						   ctx->origin,
						   ctx->style.flags,
						   ctx->style.line_length -
						       ctx->style.rdata_column,
						   ctx->linebreak,
						   target));
408

409
			isc_buffer_availableregion(target, &r);
410
			if (r.length < 1)
411
				return (ISC_R_NOSPACE);
412
413
414
415
416
417
			r.base[0] = '\n';
			isc_buffer_add(target, 1);
		}

		first = ISC_FALSE;
		result = dns_rdataset_next(rdataset);
418
	} while (result == ISC_R_SUCCESS);
419

420
	if (result != ISC_R_NOMORE)
421
422
423
424
425
426
427
428
429
430
431
432
433
		return (result);

	/*
	 * Update the ctx state to reflect what we just printed.
	 * This is done last, only when we are sure we will return 
	 * success, because this function may be called multiple 
	 * times with increasing buffer sizes until it succeeds,
	 * and failed attempts must not update the state prematurely. 
	 */
	ctx->class_printed = ISC_TRUE;
	ctx->current_ttl= current_ttl;
	ctx->current_ttl_valid = current_ttl_valid;

434
	return (ISC_R_SUCCESS);
435
436
437
438
439
440
441
}

/*
 * Print the name, type, and class of an empty rdataset,
 * such as those used to represent the question section
 * of a DNS message.
 */
442
static isc_result_t
443
444
445
446
447
448
449
question_totext(dns_rdataset_t *rdataset,
		dns_name_t *owner_name,
		dns_totext_ctx_t *ctx,
		isc_boolean_t omit_final_dot,
		isc_buffer_t *target)
{
	unsigned int column;
450
	isc_result_t result;
451
	isc_region_t r;
452
453
454

	REQUIRE(DNS_RDATASET_VALID(rdataset));
	result = dns_rdataset_first(rdataset);
455
	REQUIRE(result == ISC_R_NOMORE);
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

	column = 0;

	/* Owner name */
	{
		unsigned int name_start = target->used;
		RETERR(dns_name_totext(owner_name,
				       omit_final_dot,
				       target));
		column += target->used - name_start;
	}

	/* Class */
	{
		unsigned int class_start;
		INDENT_TO(class_column);
		class_start = target->used;
		result = dns_rdataclass_totext(rdataset->rdclass, target);
474
		if (result != ISC_R_SUCCESS)
475
476
477
478
479
480
481
482
483
484
			return (result);
		column += (target->used - class_start);
	}

	/* Type */
	{
		unsigned int type_start;
		INDENT_TO(type_column);
		type_start = target->used;
		result = dns_rdatatype_totext(rdataset->type, target);
485
		if (result != ISC_R_SUCCESS)
486
487
488
			return (result);
		column += (target->used - type_start);
	}
489

490
	isc_buffer_availableregion(target, &r);
491
	if (r.length < 1)
492
		return (ISC_R_NOSPACE);
493
494
495
	r.base[0] = '\n';
	isc_buffer_add(target, 1);

496
	return (ISC_R_SUCCESS);
497
498
499
500
501
502
503
}

/*
 * Provide a backwards compatible interface for printing a
 * single rdataset or question section.  This is now used 
 * only by wire_test.c.
 */
504
isc_result_t
505
506
507
508
509
510
511
dns_rdataset_totext(dns_rdataset_t *rdataset,
		    dns_name_t *owner_name,
		    isc_boolean_t omit_final_dot,
		    isc_boolean_t no_rdata_or_ttl,
		    isc_buffer_t *target)
{
	dns_totext_ctx_t ctx;
512
	isc_result_t result;
513
	result = totext_ctx_init(&dns_masterfile_style_debug, &ctx);
514
	if (result != ISC_R_SUCCESS) {
515
516
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "could not set master file style");
517
		return (ISC_R_UNEXPECTED);
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
	}

	/*
	 * The caller might want to give us an empty owner
	 * name (e.g. if they are outputting into a master
	 * file and this rdataset has the same name as the
	 * previous one.)
	 */
	if (dns_name_countlabels(owner_name) == 0)
		owner_name = NULL;
	
	if (no_rdata_or_ttl)
		return (question_totext(rdataset, owner_name, &ctx, 
					omit_final_dot, target));
	else
		return (rdataset_totext(rdataset, owner_name, &ctx, 
					omit_final_dot, target));
}

/*
 * Print an rdataset.  'buffer' is a scratch buffer, which must have been
 * dynamically allocated by the caller.  It must be large enough to 
 * hold the result from dns_ttl_totext().  If more than that is needed,
 * the buffer will be grown automatically.
 */

544
static isc_result_t
545
546
547
548
549
dump_rdataset(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
	      dns_totext_ctx_t *ctx, 
	      isc_buffer_t *buffer, FILE *f)
{
	isc_region_t r;
550
	isc_result_t result;
551
552
553
554
	size_t nwritten;
	
	REQUIRE(buffer->length > 0);

555
556
557
	/*
	 * Output a $TTL directive if needed.
	 */
558
559
560
561
562
563
564
565
566
567
	
	if ((ctx->style.flags & DNS_STYLEFLAG_TTL) != 0) {
		if (ctx->current_ttl_valid == ISC_FALSE ||
		    ctx->current_ttl != rdataset->ttl)
		{
			if ((ctx->style.flags & DNS_STYLEFLAG_COMMENT) != 0)
			{
				isc_buffer_clear(buffer);
				result = dns_ttl_totext(rdataset->ttl,
							ISC_TRUE, buffer);
568
				INSIST(result == ISC_R_SUCCESS);
569
				isc_buffer_usedregion(buffer, &r);
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
				fprintf(f, "$TTL %u\t; %.*s\n", rdataset->ttl,
					(int) r.length, (char *) r.base);
			} else {
				fprintf(f, "$TTL %u\n", rdataset->ttl);
			}
			ctx->current_ttl = rdataset->ttl;
			ctx->current_ttl_valid = ISC_TRUE;
		}
	}
	
	isc_buffer_clear(buffer);

	/*
	 * Generate the text representation of the rdataset into
	 * the buffer.  If the buffer is too small, grow it.
	 */ 
	for (;;) {
		int newlength;
		void *newmem;
		result = rdataset_totext(rdataset, name, ctx,
					 ISC_FALSE, buffer);
591
		if (result != ISC_R_NOSPACE)
592
593
594
595
596
597
			break;

		isc_mem_put(mctx, buffer->base, buffer->length);
		newlength = buffer->length * 2;
		newmem = isc_mem_get(mctx, newlength);
		if (newmem == NULL)
598
			return (ISC_R_NOMEMORY);
599
		isc_buffer_init(buffer, newmem, newlength);
600
	}
601
	if (result != ISC_R_SUCCESS)
602
603
604
		return (result);

	/* Write the buffer contents to the master file. */
605
	isc_buffer_usedregion(buffer, &r);
606
607
608
609
	nwritten = fwrite(r.base, 1, (size_t) r.length, f);

	if (nwritten != (size_t) r.length) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
610
				 "master file write failed: %s",
611
				 strerror(errno));
612
		return (ISC_R_UNEXPECTED);
613
614
	}
	
615
	return (ISC_R_SUCCESS);
616
617
618
619
620
}

/*
 * Dump all the rdatasets of a domain name to a master file.
 */
621
static isc_result_t
622
623
624
625
dump_rdatasets(isc_mem_t *mctx, dns_name_t *name, dns_rdatasetiter_t *rdsiter, 
	       dns_totext_ctx_t *ctx,
	       isc_buffer_t *buffer, FILE *f)
{
626
	isc_result_t result;
627
628
629
630
	dns_rdataset_t rdataset;
	
	dns_rdataset_init(&rdataset);
	result = dns_rdatasetiter_first(rdsiter);
631
	while (result == ISC_R_SUCCESS) {
632
		dns_rdatasetiter_current(rdsiter, &rdataset);
633
634
635
636
637
638
639
640
641
642
		if (rdataset.type != 0) {
			/*
			 * XXX  We only dump the rdataset if it isn't a
			 * negative caching entry.  Maybe our dumping routines
			 * will learn how to usefully dump such an entry later
			 * on.
			 */
			result = dump_rdataset(mctx, name, &rdataset, ctx,
					       buffer, f);
		} else
643
			result = ISC_R_SUCCESS;
644
		dns_rdataset_disassociate(&rdataset);
645
		if (result != ISC_R_SUCCESS)
646
647
648
649
650
			return (result);
		result = dns_rdatasetiter_next(rdsiter);
		if ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0)
			name = NULL;
	}
651
	if (result != ISC_R_NOMORE)
652
		return (result);
653
	return (ISC_R_SUCCESS);
654
655
656
657
658
659
660
661
662
663
664
665
666
}


/*
 * Initial size of text conversion buffer.  The buffer is used
 * for several purposes: converting origin names, rdatasets, 
 * $DATE timestamps, and comment strings for $TTL directives.
 *
 * When converting rdatasets, it is dynamically resized, but
 * when converting origins, timestamps, etc it is not.  Therefore, 
 * the  initial size must large enough to hold the longest possible 
 * text representation of any domain name (for $ORIGIN).
 */
667
static const int initial_buffer_length = 1200;
668
669
670
671

/*
 * Dump an entire database into a master file.
 */
672
isc_result_t
673
674
675
676
677
678
679
680
dns_master_dumptostream(isc_mem_t *mctx, dns_db_t *db,
			dns_dbversion_t *version,
			const dns_master_style_t *style,
			FILE *f)
{
	dns_fixedname_t fixname;
	dns_name_t *name;
	dns_dbiterator_t *dbiter = NULL;
681
	isc_result_t result;
682
683
684
685
686
687
688
	isc_buffer_t buffer;
	char *bufmem;
	isc_stdtime_t now;
	isc_region_t r;
	dns_totext_ctx_t ctx;

	result = totext_ctx_init(style, &ctx);
689
	if (result != ISC_R_SUCCESS) {
690
691
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "could not set master file style");
692
		return (ISC_R_UNEXPECTED);
693
694
695
696
697
	}

	dns_fixedname_init(&fixname);
	name = dns_fixedname_name(&fixname);

698
	isc_stdtime_get(&now);
699
700
701

	bufmem = isc_mem_get(mctx, initial_buffer_length);
	if (bufmem == NULL)
702
		return (ISC_R_NOMEMORY);
703
	
704
	isc_buffer_init(&buffer, bufmem, initial_buffer_length);
705
706
707
708
709
710
711
712
713
714

	/*
	 * If the database has cache semantics, output an RFC2540
	 * $DATE directive so that the TTLs can be adjusted when
	 * it is reloaded.  For zones it is not really needed, and 
	 * it would make the file incompatible with pre-RFC2540
	 * software, so we omit it in the zone case.
	 */
	if (dns_db_iscache(db)) {
		result = dns_time32_totext(now, &buffer);
715
		RUNTIME_CHECK(result == ISC_R_SUCCESS);
716
		isc_buffer_usedregion(&buffer, &r);
717
718
719
720
721
722
723
		fprintf(f, "$DATE %.*s\n", (int) r.length, (char *) r.base);
	}

	result = dns_db_createiterator(db,
		       ((ctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) ? 
		           ISC_TRUE : ISC_FALSE,
		       &dbiter);
724
	if (result != ISC_R_SUCCESS)
725
726
727
728
		goto create_iter_failure;

	result = dns_dbiterator_first(dbiter);

729
	while (result == ISC_R_SUCCESS) {
730
731
732
		dns_rdatasetiter_t *rdsiter = NULL;
		dns_dbnode_t *node = NULL;
		result = dns_dbiterator_current(dbiter, &node, name);
733
		if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
734
735
			break;
		if (result == DNS_R_NEWORIGIN) {
736
737
			dns_name_t *origin =
				dns_fixedname_name(&ctx.origin_fixname);
738
			result = dns_dbiterator_origin(dbiter, origin);
739
			RUNTIME_CHECK(result == ISC_R_SUCCESS);
740
741
			isc_buffer_clear(&buffer);
			result = dns_name_totext(origin, ISC_FALSE, &buffer);
742
			RUNTIME_CHECK(result == ISC_R_SUCCESS);
743
			isc_buffer_usedregion(&buffer, &r);
744
745
			fprintf(f, "$ORIGIN %.*s\n", (int) r.length,
				(char *) r.base);
746
747
			if ((ctx.style.flags & DNS_STYLEFLAG_REL_DATA) != 0)
				ctx.origin = origin;
748
749
		}
		result = dns_db_allrdatasets(db, node, version, now, &rdsiter);
750
		if (result != ISC_R_SUCCESS) {
751
752
753
754
755
			dns_db_detachnode(db, &node);
			goto iter_failure;
		}
		result = dump_rdatasets(mctx, name, rdsiter, &ctx,
					&buffer, f);
756
		if (result != ISC_R_SUCCESS) {
757
758
759
760
761
762
763
			dns_db_detachnode(db, &node);
			goto iter_failure;
		}
		dns_rdatasetiter_destroy(&rdsiter);
		dns_db_detachnode(db, &node);
		result = dns_dbiterator_next(dbiter);
	}
764
	if (result != ISC_R_NOMORE)
765
766
		goto iter_failure;

767
	result = ISC_R_SUCCESS;
768
769
770
771
772
773
774
775
776
777
	
 iter_failure:
	dns_dbiterator_destroy(&dbiter);
	
 create_iter_failure:
	isc_mem_put(mctx, buffer.base, buffer.length);
	return (result);
}


778
isc_result_t
779
dns_master_dump(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
Bob Halley's avatar
Bob Halley committed
780
		const dns_master_style_t *style, const char *filename)
781
{
782
	FILE *f = NULL;
783
	isc_result_t result;
784
785
786
787
788
789
790

	result = isc_file_fopen(filename, "w", &f);
	if (result != ISC_R_SUCCESS) {
		isc_log_write(dns_lctx, ISC_LOGCATEOGORY_GENERAL,
			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
			      "dumping master file: %s: open: %s", filename,
			      isc_result_totext(result));
791
		return (ISC_R_UNEXPECTED);
792
793
794
795
	}

	result = dns_master_dumptostream(mctx, db, version, style, f);

796
797
798
799
800
801
	result = isc_file_fclose(f);
	if (result != ISC_R_SUCCESS) {
		isc_log_write(dns_lctx, ISC_LOGCATEOGORY_GENERAL,
			      DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
			      "dumping master file: %s: close: %s", filename,
			      isc_result_totext(result));
802
		return (ISC_R_UNEXPECTED);
803
804
805
806
	}

	return (result);
}