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