5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
3b7f610bec248f85e25a52a0ad7dec9894389ca5Tinderbox User * Copyright (C) 1999-2009, 2011-2017 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <config.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson#include <stdlib.h>
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk#include <isc/buffer.h>
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews#include <isc/event.h>
3e14b69d196a3ebeecc4662c426344dcfd7db678Andreas Gustafsson#include <isc/file.h>
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews#include <isc/magic.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/mem.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <isc/print.h>
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence#include <isc/stdio.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/string.h>
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews#include <isc/task.h>
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews#include <isc/time.h>
e63d63dc8510c669e1575b2762265842e8783822Evan Hunt#include <isc/types.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/util.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/db.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/dbiterator.h>
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews#include <dns/events.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/fixedname.h>
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews#include <dns/lib.h>
3e14b69d196a3ebeecc4662c426344dcfd7db678Andreas Gustafsson#include <dns/log.h>
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#include <dns/master.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/masterdump.h>
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews#include <dns/ncache.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/rdata.h>
673ce7aebbb6d01c75b95f6df1ec491d6422b951Andreas Gustafsson#include <dns/rdataclass.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/rdataset.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/rdatasetiter.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdatatype.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/result.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/time.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/ttl.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews#define DNS_DCTX_MAGIC ISC_MAGIC('D', 'c', 't', 'x')
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews#define DNS_DCTX_VALID(d) ISC_MAGIC_VALID(d, DNS_DCTX_MAGIC)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define RETERR(x) do { \
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence isc_result_t _r = (x); \
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence if (_r != ISC_R_SUCCESS) \
600cfa2ba4c50017581b6c14e3a688a82ecebbe0David Lawrence return (_r); \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson } while (0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews#define CHECK(x) do { \
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews if ((x) != ISC_R_SUCCESS) \
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews goto cleanup; \
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews } while (0)
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstruct dns_master_style {
e63d63dc8510c669e1575b2762265842e8783822Evan Hunt dns_masterstyle_flags_t flags; /* DNS_STYLEFLAG_* */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int ttl_column;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int class_column;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int type_column;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int rdata_column;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int line_length;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int tab_width;
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt unsigned int split_width;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson};
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The maximum length of the newline+indentation that is output
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * when inserting a line break in an RR. This effectively puts an
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * upper limits on the value of "rdata_column", because if it is
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * very large, the tabs and spaces needed to reach it will not fit.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define DNS_TOTEXT_LINEBREAK_MAXLEN 100
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Context structure for a masterfile dump in progress.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssontypedef struct dns_totext_ctx {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_master_style_t style;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t class_printed;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson char * linebreak;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson char linebreak_buf[DNS_TOTEXT_LINEBREAK_MAXLEN];
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson dns_name_t * origin;
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews dns_name_t * neworigin;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_fixedname_t origin_fixname;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_uint32_t current_ttl;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t current_ttl_valid;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson} dns_totext_ctx_t;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark AndrewsLIBDNS_EXTERNAL_DATA const dns_master_style_t
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrewsdns_master_style_keyzone = {
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_OMIT_OWNER |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_OMIT_CLASS |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_REL_OWNER |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_REL_DATA |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_OMIT_TTL |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_TTL |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_COMMENT |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_RRCOMMENT |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_MULTILINE |
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews DNS_STYLEFLAG_KEYDATA,
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews 24, 24, 24, 32, 80, 8, UINT_MAX
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews};
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews
f621719829356f27e831507b75e88e8a655e48d8Danny MayerLIBDNS_EXTERNAL_DATA const dns_master_style_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondns_master_style_default = {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_OMIT_OWNER |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_OMIT_CLASS |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_REL_OWNER |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_REL_DATA |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_OMIT_TTL |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_TTL |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_COMMENT |
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt DNS_STYLEFLAG_RRCOMMENT |
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson DNS_STYLEFLAG_MULTILINE,
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews 24, 24, 24, 32, 80, 8, UINT_MAX
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington};
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
d0aebc5a55b6145297d94f8aee939852357c59fcMark AndrewsLIBDNS_EXTERNAL_DATA const dns_master_style_t
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrewsdns_master_style_full = {
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews DNS_STYLEFLAG_COMMENT |
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews DNS_STYLEFLAG_RESIGN,
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews 46, 46, 46, 64, 120, 8, UINT_MAX
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews};
d0aebc5a55b6145297d94f8aee939852357c59fcMark Andrews
f621719829356f27e831507b75e88e8a655e48d8Danny MayerLIBDNS_EXTERNAL_DATA const dns_master_style_t
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellingtondns_master_style_explicitttl = {
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_STYLEFLAG_OMIT_OWNER |
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_STYLEFLAG_OMIT_CLASS |
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_STYLEFLAG_REL_OWNER |
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_STYLEFLAG_REL_DATA |
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_STYLEFLAG_COMMENT |
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt DNS_STYLEFLAG_RRCOMMENT |
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_STYLEFLAG_MULTILINE,
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews 24, 32, 32, 40, 80, 8, UINT_MAX
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson};
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny MayerLIBDNS_EXTERNAL_DATA const dns_master_style_t
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellingtondns_master_style_cache = {
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington DNS_STYLEFLAG_OMIT_OWNER |
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington DNS_STYLEFLAG_OMIT_CLASS |
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson DNS_STYLEFLAG_MULTILINE |
16bd30ae6987cd4ba4fe3b873e72abf5b7178c26Mark Andrews DNS_STYLEFLAG_RRCOMMENT |
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson DNS_STYLEFLAG_TRUST |
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson DNS_STYLEFLAG_NCACHE,
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews 24, 32, 32, 40, 80, 8, UINT_MAX
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington};
2883651930dc85cacae940fe2a81277dfc14807dBrian Wellington
459d31fa838619cee728a90984d27d18345dc18aAndreas GustafssonLIBDNS_EXTERNAL_DATA const dns_master_style_t
459d31fa838619cee728a90984d27d18345dc18aAndreas Gustafssondns_master_style_simple = {
c67496c94321dfb68d209019f2b5872a81289c66Michael Sawyer 0,
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews 24, 32, 32, 40, 80, 8, UINT_MAX
c67496c94321dfb68d209019f2b5872a81289c66Michael Sawyer};
c67496c94321dfb68d209019f2b5872a81289c66Michael Sawyer
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * A style suitable for dns_rdataset_totext().
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
f621719829356f27e831507b75e88e8a655e48d8Danny MayerLIBDNS_EXTERNAL_DATA const dns_master_style_t
bfe313722d5b2eff6c80336ed1f19c2f99a53de6Andreas Gustafssondns_master_style_debug = {
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington DNS_STYLEFLAG_REL_OWNER,
b76715a02fbe0c373a5a03c4f09ac0d6de5abc43Mark Andrews 24, 32, 40, 48, 80, 8, UINT_MAX
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson};
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt/*%
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * Similar, but indented (i.e., prepended with dns_master_indentstr).
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt */
b66b333f59cf51ef87f973084a5023acd9317fb2Evan HuntLIBDNS_EXTERNAL_DATA const dns_master_style_t
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Huntdns_master_style_indent = {
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt DNS_STYLEFLAG_REL_OWNER |
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt DNS_STYLEFLAG_INDENT,
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt 24, 32, 40, 48, 80, 8, UINT_MAX
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt};
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt/*%
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt * Similar, but with each line commented out.
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt */
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan HuntLIBDNS_EXTERNAL_DATA const dns_master_style_t
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Huntdns_master_style_comment = {
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt DNS_STYLEFLAG_REL_OWNER |
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt DNS_STYLEFLAG_MULTILINE |
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt DNS_STYLEFLAG_RRCOMMENT |
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt DNS_STYLEFLAG_COMMENTDATA,
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt 24, 32, 40, 48, 80, 8, UINT_MAX
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt};
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk/*%
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk * YAML style
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk */
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpkLIBDNS_EXTERNAL_DATA const dns_master_style_t
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpkdns_master_style_yaml = {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk DNS_STYLEFLAG_YAML |
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk DNS_STYLEFLAG_REL_OWNER |
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk DNS_STYLEFLAG_INDENT,
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk 24, 32, 40, 48, 80, 8, UINT_MAX
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk};
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt/*%
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * Default indent string.
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt */
b66b333f59cf51ef87f973084a5023acd9317fb2Evan HuntLIBDNS_EXTERNAL_DATA const char *dns_master_indentstr = "\t";
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpkLIBDNS_EXTERNAL_DATA unsigned int dns_master_indent = 1;
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define N_SPACES 10
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graffstatic char spaces[N_SPACES+1] = " ";
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define N_TABS 10
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graffstatic char tabs[N_TABS+1] = "\t\t\t\t\t\t\t\t\t\t";
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstruct dns_dumpctx {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews unsigned int magic;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_t *mctx;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mutex_t lock;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews unsigned int references;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_boolean_t canceled;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_boolean_t first;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_boolean_t do_date;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_stdtime_t now;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews FILE *f;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_t *db;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dbversion_t *version;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dbiterator_t *dbiter;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_totext_ctx_t tctx;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_t *task;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpdonefunc_t done;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews void *done_arg;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews unsigned int nodes;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews /* dns_master_dumpinc() */
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *file;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *tmpfile;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_masterformat_t format;
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterrawheader_t header;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_result_t (*dumpsets)(isc_mem_t *mctx, dns_name_t *name,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdatasetiter_t *rdsiter,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_totext_ctx_t *ctx,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_t *buffer, FILE *f);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews};
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater#define NXDOMAIN(x) (((x)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
d788d738e3d29037651b42566519c9a0a66ba219Mark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Output tabs and spaces to go from column '*current' to
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * column 'to', and update '*current' to reflect the new
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * current column.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halleyindent(unsigned int *current, unsigned int to, int tabwidth,
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley isc_buffer_t *target)
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley{
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_region_t r;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned char *p;
544f5611e1fc84597db819d111c8fd98bb719de6Bob Halley unsigned int from;
544f5611e1fc84597db819d111c8fd98bb719de6Bob Halley int ntabs, nspaces, t;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson from = *current;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (to < from + 1)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson to = from + 1;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ntabs = to / tabwidth - from / tabwidth;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (ntabs < 0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ntabs = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (ntabs > 0) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_availableregion(target, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (r.length < (unsigned) ntabs)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_NOSPACE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson p = r.base;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson t = ntabs;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson while (t) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson int n = t;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (n > N_TABS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson n = N_TABS;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(p, tabs, n);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson p += n;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson t -= n;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_add(target, ntabs);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson from = (to / tabwidth) * tabwidth;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson nspaces = to - from;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INSIST(nspaces >= 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_availableregion(target, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (r.length < (unsigned) nspaces)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_NOSPACE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson p = r.base;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence t = nspaces;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson while (t) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson int n = t;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (n > N_SPACES)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson n = N_SPACES;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(p, spaces, n);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson p += n;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson t -= n;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_add(target, nspaces);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *current = to;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencetotext_ctx_init(const dns_master_style_t *style, dns_totext_ctx_t *ctx) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(style->tab_width != 0);
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafsson
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafsson ctx->style = *style;
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafsson ctx->class_printed = ISC_FALSE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_fixedname_init(&ctx->origin_fixname);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Set up the line break string if needed.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if ((ctx->style.flags & DNS_STYLEFLAG_MULTILINE) != 0) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t buf;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_region_t r;
42a5f9c8f535fb2a6d1cbfaa38533176e1f1667aBob Halley unsigned int col = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley isc_buffer_init(&buf, ctx->linebreak_buf,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence sizeof(ctx->linebreak_buf));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_availableregion(&buf, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (r.length < 1)
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley return (DNS_R_TEXTTOOLONG);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson r.base[0] = '\n';
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_add(&buf, 1);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 ||
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk unsigned int i, len = strlen(dns_master_indentstr);
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk for (i = 0; i < dns_master_indent; i++) {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if (isc_buffer_availablelength(&buf) < len)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk return (DNS_R_TEXTTOOLONG);
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk isc_buffer_putstr(&buf, dns_master_indentstr);
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk }
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt }
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt if ((ctx->style.flags & DNS_STYLEFLAG_COMMENTDATA) != 0) {
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt isc_buffer_availableregion(&buf, &r);
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt if (r.length < 1)
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt return (DNS_R_TEXTTOOLONG);
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt r.base[0] = ';';
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt isc_buffer_add(&buf, 1);
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt }
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence result = indent(&col, ctx->style.rdata_column,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->style.tab_width, &buf);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * Do not return ISC_R_NOSPACE if the line break string
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * buffer is too small, because that would just make
47b7dfffe5d806c6a5e99ef17f07bcde812c2132Francis Dupont * dump_rdataset() retry indefinitely with ever
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * bigger target buffers. That's a different buffer,
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley * so it won't help. Use DNS_R_TEXTTOOLONG as a substitute.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result == ISC_R_NOSPACE)
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley return (DNS_R_TEXTTOOLONG);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_availableregion(&buf, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (r.length < 1)
2e715dbdc263f859c01b57a9d733c1dfbf28b90eBob Halley return (DNS_R_TEXTTOOLONG);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson r.base[0] = '\0';
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_add(&buf, 1);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->linebreak = ctx->linebreak_buf;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson } else {
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->linebreak = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->origin = NULL;
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews ctx->neworigin = NULL;
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafsson ctx->current_ttl = 0;
ec7493d8d1966a3dc5f5306fc0a96519e0de6dceAndreas Gustafsson ctx->current_ttl_valid = ISC_FALSE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#define INDENT_TO(col) \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson do { \
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) { \
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((result = str_totext(" ", target)) \
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk != ISC_R_SUCCESS) \
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk return (result); \
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk } else if ((result = indent(&column, ctx->style.col, \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->style.tab_width, target)) \
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff != ISC_R_SUCCESS) \
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result); \
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington } while (0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafssonstatic isc_result_t
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafssonstr_totext(const char *source, isc_buffer_t *target) {
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson unsigned int l;
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson isc_region_t region;
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson isc_buffer_availableregion(target, &region);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson l = strlen(source);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson if (l > region.length)
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson return (ISC_R_NOSPACE);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(region.base, source, l);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson isc_buffer_add(target, l);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson return (ISC_R_SUCCESS);
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson}
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrewsstatic isc_result_t
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrewsncache_summary(dns_rdataset_t *rdataset, isc_boolean_t omit_final_dot,
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews isc_buffer_t *target)
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews{
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews isc_result_t result = ISC_R_SUCCESS;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_rdataset_t rds;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_name_t name;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_rdataset_init(&rds);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_name_init(&name, NULL);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews do {
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_ncache_current(rdataset, &name, &rds);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews for (result = dns_rdataset_first(&rds);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews result == ISC_R_SUCCESS;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews result = dns_rdataset_next(&rds)) {
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(str_totext("; ", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(dns_name_totext(&name, omit_final_dot, target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(str_totext(" ", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(dns_rdatatype_totext(rds.type, target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews if (rds.type == dns_rdatatype_rrsig) {
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(str_totext(" ", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(dns_rdatatype_totext(rds.covers, target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(str_totext(" ...\n", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews } else {
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_rdataset_current(&rds, &rdata);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(str_totext(" ", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(dns_rdata_tofmttext(&rdata, dns_rootname,
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt 0, 0, 0, " ", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews CHECK(str_totext("\n", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews }
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews }
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_rdataset_disassociate(&rds);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews result = dns_rdataset_next(rdataset);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews } while (result == ISC_R_SUCCESS);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews if (result == ISC_R_NOMORE)
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews result = ISC_R_SUCCESS;
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews cleanup:
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews if (dns_rdataset_isassociated(&rds))
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews dns_rdataset_disassociate(&rds);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews return (result);
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews}
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Convert 'rdataset' to master file text format according to 'ctx',
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * storing the result in 'target'. If 'owner_name' is NULL, it
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * is omitted; otherwise 'owner_name' must be valid and have at least
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * one label.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonrdataset_totext(dns_rdataset_t *rdataset,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_name_t *owner_name,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_totext_ctx_t *ctx,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t omit_final_dot,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t *target)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int column;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t first = ISC_TRUE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_uint32_t current_ttl;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t current_ttl_valid;
11c7a43642def4aaa2bed001ff018a0bb0b65c29Danny Mayer dns_rdatatype_t type;
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt unsigned int type_start;
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews dns_fixedname_t fixed;
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews dns_name_t *name = NULL;
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk unsigned int i;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(DNS_RDATASET_VALID(rdataset));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_rdataset_first(rdataset);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson current_ttl = ctx->current_ttl;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson current_ttl_valid = ctx->current_ttl_valid;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews if (owner_name != NULL) {
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews dns_fixedname_init(&fixed);
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews name = dns_fixedname_name(&fixed);
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews dns_name_copy(owner_name, name, NULL);
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews dns_rdataset_getownercase(rdataset, name);
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews }
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews
84f0bd3bc7ac72289cc0dfedd3d17872ad1169feEvan Hunt while (result == ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt /*
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt * Indent?
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt */
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 ||
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk for (i = 0; i < dns_master_indent; i++)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk RETERR(str_totext(dns_master_indentstr,
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk target));
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk /*
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk * YAML enumerator?
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk */
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk RETERR(str_totext("- ", target));
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk }
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt /*
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt * Comment?
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt */
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt if ((ctx->style.flags & DNS_STYLEFLAG_COMMENTDATA) != 0)
86856f4f3069bb2d75851b56401ffde18f41198fMark Andrews RETERR(str_totext(";", target));
1d761cb453c76353deb8423c78e98d00c5f86ffaEvan Hunt
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Owner name.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews if (name != NULL &&
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley ! ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0 &&
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley !first))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int name_start = target->used;
a8da00ef95ba37b9d071c2b8db1a0c967e060106Mark Andrews RETERR(dns_name_totext(name, omit_final_dot, target));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column += target->used - name_start;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * TTL.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews if ((ctx->style.flags & DNS_STYLEFLAG_NO_TTL) == 0 &&
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews !((ctx->style.flags & DNS_STYLEFLAG_OMIT_TTL) != 0 &&
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews current_ttl_valid &&
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews rdataset->ttl == current_ttl))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson char ttlbuf[64];
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_region_t r;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int length;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INDENT_TO(ttl_column);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt if ((ctx->style.flags & DNS_STYLEFLAG_TTL_UNITS) != 0) {
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt length = target->used;
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt result = dns_ttl_totext2(rdataset->ttl,
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt ISC_FALSE, ISC_FALSE,
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt target);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt if (result != ISC_R_SUCCESS)
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt return (result);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt column += target->used - length;
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt } else {
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt length = snprintf(ttlbuf, sizeof(ttlbuf), "%u",
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt rdataset->ttl);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt INSIST(length <= sizeof(ttlbuf));
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt isc_buffer_availableregion(target, &r);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt if (r.length < length)
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt return (ISC_R_NOSPACE);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt memmove(r.base, ttlbuf, length);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt isc_buffer_add(target, length);
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt column += length;
cd750f6e7449678173e8cfe080ae0bf3dcb424cfEvan Hunt }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * If the $TTL directive is not in use, the TTL we
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * just printed becomes the default for subsequent RRs.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley if ((ctx->style.flags & DNS_STYLEFLAG_TTL) == 0) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson current_ttl = rdataset->ttl;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson current_ttl_valid = ISC_TRUE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Class.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews if ((ctx->style.flags & DNS_STYLEFLAG_NO_CLASS) == 0 &&
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews ((ctx->style.flags & DNS_STYLEFLAG_OMIT_CLASS) == 0 ||
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews ctx->class_printed == ISC_FALSE))
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int class_start;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INDENT_TO(class_column);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson class_start = target->used;
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman if ((ctx->style.flags & DNS_STYLEFLAG_UNKNOWNFORMAT) != 0)
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdataclass_tounknowntext
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman (rdataset->rdclass, target);
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman else
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdataclass_totext
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman (rdataset->rdclass, target);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column += (target->used - class_start);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Type.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) {
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson type = rdataset->covers;
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson } else {
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson type = rdataset->type;
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson }
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt INDENT_TO(type_column);
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt type_start = target->used;
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt RETERR(str_totext("\\-", target));
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews switch (type) {
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews case dns_rdatatype_keydata:
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews#define KEYDATA "KEYDATA"
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews if ((ctx->style.flags & DNS_STYLEFLAG_KEYDATA) != 0) {
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews if (isc_buffer_availablelength(target) <
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews (sizeof(KEYDATA) - 1))
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews return (ISC_R_NOSPACE);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews isc_buffer_putstr(target, KEYDATA);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews break;
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews }
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews /* FALLTHROUGH */
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews default:
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman if ((ctx->style.flags & DNS_STYLEFLAG_UNKNOWNFORMAT) != 0)
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdatatype_tounknowntext(type, target);
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman else
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdatatype_totext(type, target);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews if (result != ISC_R_SUCCESS)
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews return (result);
d6f99498d624d5c5c8ee45067df02e6a6b35a1a3Mark Andrews }
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt column += (target->used - type_start);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Rdata.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson INDENT_TO(rdata_column);
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 ||
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk for (i = 0; i < dns_master_indent; i++)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk RETERR(str_totext(dns_master_indentstr,
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk target));
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk }
d788d738e3d29037651b42566519c9a0a66ba219Mark Andrews if (NXDOMAIN(rdataset))
d788d738e3d29037651b42566519c9a0a66ba219Mark Andrews RETERR(str_totext(";-$NXDOMAIN\n", target));
d788d738e3d29037651b42566519c9a0a66ba219Mark Andrews else
d788d738e3d29037651b42566519c9a0a66ba219Mark Andrews RETERR(str_totext(";-$NXRRSET\n", target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews /*
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews * Print a summary of the cached records which make
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews * up the negative response.
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews */
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews RETERR(ncache_summary(rdataset, omit_final_dot,
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews target));
47d89fcd4fb850b066f87dc3313afe1cfe92cd99Mark Andrews break;
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson } else {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_region_t r;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_rdataset_current(rdataset, &rdata);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson RETERR(dns_rdata_tofmttext(&rdata,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->origin,
4acedf9e13783c32a48133d29ed4be128b3fb1ffMark Andrews (unsigned int)
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->style.flags,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->style.line_length -
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->style.rdata_column,
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt ctx->style.split_width,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson ctx->linebreak,
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson target));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_availableregion(target, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (r.length < 1)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_NOSPACE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson r.base[0] = '\n';
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_add(target, 1);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson first = ISC_FALSE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_rdataset_next(rdataset);
84f0bd3bc7ac72289cc0dfedd3d17872ad1169feEvan Hunt }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_NOMORE)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Update the ctx state to reflect what we just printed.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * This is done last, only when we are sure we will return
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * success, because this function may be called multiple
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * times with increasing buffer sizes until it succeeds,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * and failed attempts must not update the state prematurely.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->class_printed = ISC_TRUE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->current_ttl= current_ttl;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->current_ttl_valid = current_ttl_valid;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Print the name, type, and class of an empty rdataset,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * such as those used to represent the question section
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * of a DNS message.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonquestion_totext(dns_rdataset_t *rdataset,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_name_t *owner_name,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_totext_ctx_t *ctx,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t omit_final_dot,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t *target)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int column;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley isc_region_t r;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(DNS_RDATASET_VALID(rdataset));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_rdataset_first(rdataset);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(result == ISC_R_NOMORE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column = 0;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Owner name */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int name_start = target->used;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson RETERR(dns_name_totext(owner_name,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson omit_final_dot,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson target));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column += target->used - name_start;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Class */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int class_start;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INDENT_TO(class_column);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson class_start = target->used;
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman if ((ctx->style.flags & DNS_STYLEFLAG_UNKNOWNFORMAT) != 0)
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdataclass_tounknowntext(rdataset->rdclass,
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman target);
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman else
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdataclass_totext(rdataset->rdclass,
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman target);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column += (target->used - class_start);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /* Type */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson unsigned int type_start;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson INDENT_TO(type_column);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson type_start = target->used;
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman if ((ctx->style.flags & DNS_STYLEFLAG_UNKNOWNFORMAT) != 0)
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdatatype_tounknowntext(rdataset->type,
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman target);
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman else
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman result = dns_rdatatype_totext(rdataset->type,
79a55d4f4dcf3d6f617523228503d8584b72a3a3Mukund Sivaraman target);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson column += (target->used - type_start);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_availableregion(target, &r);
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley if (r.length < 1)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_NOSPACE);
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley r.base[0] = '\n';
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley isc_buffer_add(target, 1);
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffisc_result_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondns_rdataset_totext(dns_rdataset_t *rdataset,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_name_t *owner_name,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_boolean_t omit_final_dot,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson isc_boolean_t question,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t *target)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_totext_ctx_t ctx;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
bfe313722d5b2eff6c80336ed1f19c2f99a53de6Andreas Gustafsson result = totext_ctx_init(&dns_master_style_debug, &ctx);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson "could not set master file style");
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_UNEXPECTED);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * The caller might want to give us an empty owner
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * name (e.g. if they are outputting into a master
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * file and this rdataset has the same name as the
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * previous one.)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (dns_name_countlabels(owner_name) == 0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson owner_name = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson if (question)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (question_totext(rdataset, owner_name, &ctx,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson omit_final_dot, target));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson else
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (rdataset_totext(rdataset, owner_name, &ctx,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson omit_final_dot, target));
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafssonisc_result_t
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafssondns_master_rdatasettotext(dns_name_t *owner_name,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson dns_rdataset_t *rdataset,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson const dns_master_style_t *style,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson isc_buffer_t *target)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson{
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson dns_totext_ctx_t ctx;
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson isc_result_t result;
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson result = totext_ctx_init(style, &ctx);
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson if (result != ISC_R_SUCCESS) {
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson "could not set master file style");
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson return (ISC_R_UNEXPECTED);
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson }
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson return (rdataset_totext(rdataset, owner_name, &ctx,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson ISC_FALSE, target));
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson}
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafssonisc_result_t
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafssondns_master_questiontotext(dns_name_t *owner_name,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson dns_rdataset_t *rdataset,
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson const dns_master_style_t *style,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson isc_buffer_t *target)
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson{
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson dns_totext_ctx_t ctx;
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson isc_result_t result;
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson result = totext_ctx_init(style, &ctx);
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson if (result != ISC_R_SUCCESS) {
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson "could not set master file style");
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson return (ISC_R_UNEXPECTED);
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson }
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson
bed7a352934efc8055902d83c750dca2d0cd6aaaAndreas Gustafsson return (question_totext(rdataset, owner_name, &ctx,
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson ISC_FALSE, target));
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson}
421551db8a61283420b0b6aed4ac28f9d76b9770Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Print an rdataset. 'buffer' is a scratch buffer, which must have been
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * dynamically allocated by the caller. It must be large enough to
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * hold the result from dns_ttl_totext(). If more than that is needed,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * the buffer will be grown automatically.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssondump_rdataset(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_totext_ctx_t *ctx,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_t *buffer, FILE *f)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_region_t r;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson REQUIRE(buffer->length > 0);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Output a $TTL directive if needed.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if ((ctx->style.flags & DNS_STYLEFLAG_TTL) != 0) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (ctx->current_ttl_valid == ISC_FALSE ||
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->current_ttl != rdataset->ttl)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if ((ctx->style.flags & DNS_STYLEFLAG_COMMENT) != 0)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_clear(buffer);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = dns_ttl_totext(rdataset->ttl,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_TRUE, buffer);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff INSIST(result == ISC_R_SUCCESS);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_usedregion(buffer, &r);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson fprintf(f, "$TTL %u\t; %.*s\n", rdataset->ttl,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson (int) r.length, (char *) r.base);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson } else {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson fprintf(f, "$TTL %u\n", rdataset->ttl);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->current_ttl = rdataset->ttl;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ctx->current_ttl_valid = ISC_TRUE;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson isc_buffer_clear(buffer);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Generate the text representation of the rdataset into
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * the buffer. If the buffer is too small, grow it.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson for (;;) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson int newlength;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson void *newmem;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson result = rdataset_totext(rdataset, name, ctx,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson ISC_FALSE, buffer);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_NOSPACE)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson break;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson newlength = buffer->length * 2;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson newmem = isc_mem_get(mctx, newlength);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (newmem == NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_NOMEMORY);
4e57d3ff7d92abdef4b0b6aebc23a9dfae2ba040Andreas Gustafsson isc_mem_put(mctx, buffer->base, buffer->length);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_init(buffer, newmem, newlength);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence /*
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence * Write the buffer contents to the master file.
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence */
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_usedregion(buffer, &r);
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence if (result != ISC_R_SUCCESS) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
c0d0a59d1b665423b8a0d1829d0f0da121cb3473Andreas Gustafsson "master file write failed: %s",
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence isc_result_totext(result));
d8e34837cd6c88c42b3ecdb9107a43ecf8252e79David Lawrence return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * Define the order in which rdatasets should be printed in zone
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * files. We will print SOA and NS records before others, SIGs
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * immediately following the things they sign, and order everything
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * else by RR number. This is all just for aesthetics and
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * compatibility with buggy software that expects the SOA to be first;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * the DNS specifications allow any order.
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson */
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafssonstatic int
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafssondump_order(const dns_rdataset_t *rds) {
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson int t;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson int sig;
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews if (rds->type == dns_rdatatype_rrsig) {
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson t = rds->covers;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson sig = 1;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson } else {
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson t = rds->type;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson sig = 0;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson }
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson switch (t) {
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson case dns_rdatatype_soa:
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson t = 0;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson break;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson case dns_rdatatype_ns:
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson t = 1;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson break;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson default:
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson t += 2;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson break;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson }
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson return (t << 1) + sig;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson}
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafssonstatic int
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafssondump_order_compare(const void *a, const void *b) {
e694d4e2016c0ab1f3c2bbe493c45379770e830dDavid Lawrence return (dump_order(*((const dns_rdataset_t * const *) a)) -
e694d4e2016c0ab1f3c2bbe493c45379770e830dDavid Lawrence dump_order(*((const dns_rdataset_t * const *) b)));
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson}
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson/*
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * Dump all the rdatasets of a domain name to a master file. We make
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * a "best effort" attempt to sort the RRsets in a nice order, but if
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * there are more than MAXSORT RRsets, we punt and only sort them in
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * groups of MAXSORT. This is not expected to ever happen in practice
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * since much less than 64 RR types have been registered with the
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * IANA, so far, and the output will be correct (though not
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * aesthetically pleasing) even if it does happen.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson#define MAXSORT 64
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_result_t
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsdump_rdatasets_text(isc_mem_t *mctx, dns_name_t *name,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_t *buffer, FILE *f)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson isc_result_t itresult, dumpresult;
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews isc_region_t r;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson dns_rdataset_t rdatasets[MAXSORT];
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson dns_rdataset_t *sorted[MAXSORT];
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson int i, n;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson itresult = dns_rdatasetiter_first(rdsiter);
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson dumpresult = ISC_R_SUCCESS;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews if (itresult == ISC_R_SUCCESS && ctx->neworigin != NULL) {
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews isc_buffer_clear(buffer);
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews itresult = dns_name_totext(ctx->neworigin, ISC_FALSE, buffer);
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews RUNTIME_CHECK(itresult == ISC_R_SUCCESS);
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews isc_buffer_usedregion(buffer, &r);
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews fprintf(f, "$ORIGIN %.*s\n", (int) r.length, (char *) r.base);
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews ctx->neworigin = NULL;
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews }
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson again:
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson for (i = 0;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson itresult == ISC_R_SUCCESS && i < MAXSORT;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson itresult = dns_rdatasetiter_next(rdsiter), i++) {
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson dns_rdataset_init(&rdatasets[i]);
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson dns_rdatasetiter_current(rdsiter, &rdatasets[i]);
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson sorted[i] = &rdatasets[i];
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington }
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson n = i;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson INSIST(n <= MAXSORT);
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson qsort(sorted, n, sizeof(sorted[0]), dump_order_compare);
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson for (i = 0; i < n; i++) {
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson dns_rdataset_t *rds = sorted[i];
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if (ctx->style.flags & DNS_STYLEFLAG_TRUST) {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 ||
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk unsigned int j;
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk for (j = 0; j < dns_master_indent; j++)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk fprintf(f, "%s", dns_master_indentstr);
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk }
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk fprintf(f, "; %s\n", dns_trust_totext(rds->trust));
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk }
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt if (((rds->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) &&
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson /* Omit negative cache entries */
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson } else {
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson isc_result_t result =
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson dump_rdataset(mctx, name, rds, ctx,
f621719829356f27e831507b75e88e8a655e48d8Danny Mayer buffer, f);
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson if (result != ISC_R_SUCCESS)
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson dumpresult = result;
123a3dddc94534d3a6c6f81c118a5b63dc5994c3Andreas Gustafsson if ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0)
123a3dddc94534d3a6c6f81c118a5b63dc5994c3Andreas Gustafsson name = NULL;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson }
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews if (ctx->style.flags & DNS_STYLEFLAG_RESIGN &&
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews rds->attributes & DNS_RDATASETATTR_RESIGN) {
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews isc_buffer_t b;
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews char buf[sizeof("YYYYMMDDHHMMSS")];
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews memset(buf, 0, sizeof(buf));
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews isc_buffer_init(&b, buf, sizeof(buf) - 1);
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews dns_time64_totext((isc_uint64_t)rds->resign, &b);
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 ||
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk {
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk unsigned int j;
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk for (j = 0; j < dns_master_indent; j++)
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk fprintf(f, "%s", dns_master_indentstr);
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk }
b1866070ef4fb9e17bff16ad458f629bbc5a4accwpk fprintf(f, "; resign=%s\n", buf);
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews }
e482a1c91ecb5e47bc26617bf310d6b5c41fad91Andreas Gustafsson dns_rdataset_disassociate(rds);
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington }
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson if (dumpresult != ISC_R_SUCCESS)
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson return (dumpresult);
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson /*
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * If we got more data than could be sorted at once,
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson * go handle the rest.
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson */
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson if (itresult == ISC_R_SUCCESS)
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson goto again;
674f1cfb1d7dfa92e52db950dbe80e60ef8f5cddBrian Wellington
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson if (itresult == ISC_R_NOMORE)
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson itresult = ISC_R_SUCCESS;
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson
593cb00bd17e5e2ab0dcb7c635a9a81082dc5d0eAndreas Gustafsson return (itresult);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews/*
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Dump given RRsets in the "raw" format.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsstatic isc_result_t
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsdump_rdataset_raw(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_t *buffer, FILE *f)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews{
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_result_t result;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_uint32_t totallen;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_uint16_t dlen;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_region_t r, r_hdr;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews REQUIRE(buffer->length > 0);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews REQUIRE(DNS_RDATASET_VALID(rdataset));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
67dc2f0536bcbbfa0970eb2893dcbc1c6713fad4Mark Andrews rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews restart:
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews totallen = 0;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = dns_rdataset_first(rdataset);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews REQUIRE(result == ISC_R_SUCCESS);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_clear(buffer);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews /*
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Common header and owner name (length followed by name)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * These fields should be in a moderate length, so we assume we
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * can store all of them in the initial buffer.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_availableregion(buffer, &r_hdr);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews INSIST(r_hdr.length >= sizeof(dns_masterrawrdataset_t));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint32(buffer, totallen); /* XXX: leave space */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint16(buffer, rdataset->rdclass); /* 16-bit class */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint16(buffer, rdataset->type); /* 16-bit type */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint16(buffer, rdataset->covers); /* same as type */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint32(buffer, rdataset->ttl); /* 32-bit TTL */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint32(buffer, dns_rdataset_count(rdataset));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews totallen = isc_buffer_usedlength(buffer);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews INSIST(totallen <= sizeof(dns_masterrawrdataset_t));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_name_toregion(name, &r);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews INSIST(isc_buffer_availablelength(buffer) >=
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews (sizeof(dlen) + r.length));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dlen = (isc_uint16_t)r.length;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint16(buffer, dlen);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_copyregion(buffer, &r);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews totallen += sizeof(dlen) + r.length;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews do {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdataset_current(rdataset, &rdata);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdata_toregion(&rdata, &r);
203629e729b756601646c639c0dbfb267030a617Mark Andrews INSIST(r.length <= 0xffffU);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dlen = (isc_uint16_t)r.length;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews /*
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Copy the rdata into the buffer. If the buffer is too small,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * grow it. This should be rare, so we'll simply restart the
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * entire procedure (or should we copy the old data and
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * continue?).
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
203629e729b756601646c639c0dbfb267030a617Mark Andrews if (isc_buffer_availablelength(buffer) <
203629e729b756601646c639c0dbfb267030a617Mark Andrews sizeof(dlen) + r.length) {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews int newlength;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews void *newmem;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews newlength = buffer->length * 2;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews newmem = isc_mem_get(mctx, newlength);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews if (newmem == NULL)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews return (ISC_R_NOMEMORY);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_mem_put(mctx, buffer->base, buffer->length);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_init(buffer, newmem, newlength);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews goto restart;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews }
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint16(buffer, dlen);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_copyregion(buffer, &r);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews totallen += sizeof(dlen) + r.length;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = dns_rdataset_next(rdataset);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews } while (result == ISC_R_SUCCESS);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews if (result != ISC_R_NOMORE)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews return (result);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews /*
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Fill in the total length field.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * XXX: this is a bit tricky. Since we have already "used" the space
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * for the total length in the buffer, we first remember the entire
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * buffer length in the region, "rewind", and then write the value.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_usedregion(buffer, &r);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_clear(buffer);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_putuint32(buffer, totallen);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews INSIST(isc_buffer_usedlength(buffer) < totallen);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews /*
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Write the buffer contents to the raw master file.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews if (result != ISC_R_SUCCESS) {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews "raw master file write failed: %s",
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_result_totext(result));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews return (result);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews }
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews return (result);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews}
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsstatic isc_result_t
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsdump_rdatasets_raw(isc_mem_t *mctx, dns_name_t *name,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_buffer_t *buffer, FILE *f)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews{
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_result_t result;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdataset_t rdataset;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews for (result = dns_rdatasetiter_first(rdsiter);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result == ISC_R_SUCCESS;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = dns_rdatasetiter_next(rdsiter)) {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdataset_init(&rdataset);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdatasetiter_current(rdsiter, &rdataset);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt if (((rdataset.attributes & DNS_RDATASETATTR_NEGATIVE) != 0) &&
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews /* Omit negative cache entries */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews } else {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = dump_rdataset_raw(mctx, name, &rdataset,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews buffer, f);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews }
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_rdataset_disassociate(&rdataset);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews if (result != ISC_R_SUCCESS)
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews return (result);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews }
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews if (result == ISC_R_NOMORE)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = ISC_R_SUCCESS;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews return (result);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
7829fad4093f2c1985b1efb7cea00287ff015d2bckbstatic isc_result_t
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Huntdump_rdatasets_map(isc_mem_t *mctx, dns_name_t *name,
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt isc_buffer_t *buffer, FILE *f)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb{
7829fad4093f2c1985b1efb7cea00287ff015d2bckb UNUSED(mctx);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb UNUSED(name);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb UNUSED(rdsiter);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb UNUSED(ctx);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb UNUSED(buffer);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb UNUSED(f);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb return (ISC_R_NOTIMPLEMENTED);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb}
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson/*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Initial size of text conversion buffer. The buffer is used
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * for several purposes: converting origin names, rdatasets,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * $DATE timestamps, and comment strings for $TTL directives.
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson *
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * When converting rdatasets, it is dynamically resized, but
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * when converting origins, timestamps, etc it is not. Therefore,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * the initial size must large enough to hold the longest possible
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * text representation of any domain name (for $ORIGIN).
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson */
68e4926b2262571e004b4be00b905ec776c01d9cMichael Graffstatic const int initial_buffer_length = 1200;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdumptostreaminc(dns_dumpctx_t *dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic void
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdumpctx_destroy(dns_dumpctx_t *dctx) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->magic = 0;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews DESTROYLOCK(&dctx->lock);
d99f88add1e61555e3193e9b33701c1a1ffa677aMichael Graff dns_dbiterator_destroy(&dctx->dbiter);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->version != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_detach(&dctx->db);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->task != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_detach(&dctx->task);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->file != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_free(dctx->mctx, dctx->file);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->tmpfile != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_free(dctx->mctx, dctx->tmpfile);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(*dctx));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsvoid
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_dumpctx_attach(dns_dumpctx_t *source, dns_dumpctx_t **target) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(DNS_DCTX_VALID(source));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(target != NULL && *target == NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews LOCK(&source->lock);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews INSIST(source->references > 0);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews source->references++;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews INSIST(source->references != 0); /* Overflow? */
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews UNLOCK(&source->lock);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews *target = source;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsvoid
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_dumpctx_detach(dns_dumpctx_t **dctxp) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_boolean_t need_destroy = ISC_FALSE;
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(dctxp != NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx = *dctxp;
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews REQUIRE(DNS_DCTX_VALID(dctx));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews *dctxp = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews LOCK(&dctx->lock);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews INSIST(dctx->references != 0);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->references--;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->references == 0)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews need_destroy = ISC_TRUE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews UNLOCK(&dctx->lock);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (need_destroy)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dumpctx_destroy(dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrewsdns_dbversion_t *
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrewsdns_dumpctx_version(dns_dumpctx_t *dctx) {
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater REQUIRE(DNS_DCTX_VALID(dctx));
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrews return (dctx->version);
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrews}
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrews
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrewsdns_db_t *
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrewsdns_dumpctx_db(dns_dumpctx_t *dctx) {
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews REQUIRE(DNS_DCTX_VALID(dctx));
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrews return (dctx->db);
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrews}
ed03e26c44347ec20aff6608de6082e3594d95fbMark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsvoid
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_dumpctx_cancel(dns_dumpctx_t *dctx) {
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews REQUIRE(DNS_DCTX_VALID(dctx));
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews LOCK(&dctx->lock);
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews dctx->canceled = ISC_TRUE;
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews UNLOCK(&dctx->lock);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_result_t
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Huntflushandsync(FILE *f, isc_result_t result, const char *temp) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_boolean_t logit = ISC_TF(result == ISC_R_SUCCESS);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result == ISC_R_SUCCESS)
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt result = isc_stdio_flush(f);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS && logit) {
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt if (temp != NULL)
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt "dumping to master file: %s: flush: %s",
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt temp, isc_result_totext(result));
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt else
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt "dumping to stream: flush: %s",
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_result_totext(result));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews logit = ISC_FALSE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews }
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt if (result == ISC_R_SUCCESS)
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt result = isc_stdio_sync(f);
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt if (result != ISC_R_SUCCESS && logit) {
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt if (temp != NULL)
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt "dumping to master file: %s: fsync: %s",
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt temp, isc_result_totext(result));
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt else
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt "dumping to stream: fsync: %s",
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_result_totext(result));
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt }
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt return (result);
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt}
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Huntstatic isc_result_t
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Huntcloseandrename(FILE *f, isc_result_t result, const char *temp, const char *file)
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt{
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_result_t tresult;
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt isc_boolean_t logit = ISC_TF(result == ISC_R_SUCCESS);
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt result = flushandsync(f, result, temp);
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt if (result != ISC_R_SUCCESS)
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt logit = ISC_FALSE;
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews tresult = isc_stdio_close(f);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result == ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = tresult;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS && logit) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews "dumping master file: %s: fclose: %s",
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews temp, isc_result_totext(result));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews logit = ISC_FALSE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews }
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result == ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = isc_file_rename(temp, file);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews else
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews (void)isc_file_remove(temp);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS && logit) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews "dumping master file: rename: %s: %s",
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews file, isc_result_totext(result));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews }
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic void
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdump_quantum(isc_task_t *task, isc_event_t *event) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
4e681da26da4fff442b3ae24b0da2de1f240c43cMark Andrews isc_result_t tresult;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(event != NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx = event->ev_arg;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(DNS_DCTX_VALID(dctx));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->canceled)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = ISC_R_CANCELED;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews else
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dumptostreaminc(dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result == DNS_R_CONTINUE) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews event->ev_arg = dctx;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_send(task, &event);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
4e681da26da4fff442b3ae24b0da2de1f240c43cMark Andrews if (dctx->file != NULL) {
4e681da26da4fff442b3ae24b0da2de1f240c43cMark Andrews tresult = closeandrename(dctx->f, result,
4e681da26da4fff442b3ae24b0da2de1f240c43cMark Andrews dctx->tmpfile, dctx->file);
4e681da26da4fff442b3ae24b0da2de1f240c43cMark Andrews if (tresult != ISC_R_SUCCESS && result == ISC_R_SUCCESS)
4e681da26da4fff442b3ae24b0da2de1f240c43cMark Andrews result = tresult;
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt } else
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt result = flushandsync(dctx->f, result, NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews (dctx->done)(dctx->done_arg, result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_event_free(&event);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_detach(&dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewstask_send(dns_dumpctx_t *dctx) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_event_t *event;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
b7e6fb4e8464ceb4a62a8c00e3127da3c2839329Mark Andrews event = isc_event_allocate(dctx->mctx, NULL, DNS_EVENT_DUMPQUANTUM,
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews dump_quantum, dctx, sizeof(*event));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (event == NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_NOMEMORY);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_send(dctx->task, &event);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (ISC_R_SUCCESS);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdumpctx_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews const dns_master_style_t *style, FILE *f, dns_dumpctx_t **dctxp,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterformat_t format, dns_masterrawheader_t *header)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews{
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_result_t result;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews unsigned int options;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx = isc_mem_get(mctx, sizeof(*dctx));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx == NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (ISC_R_NOMEMORY);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->mctx = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->f = f;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->dbiter = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->db = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->version = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->done = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->done_arg = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->task = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->nodes = 0;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->first = ISC_TRUE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->canceled = ISC_FALSE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->file = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->tmpfile = NULL;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dctx->format = format;
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt if (header == NULL)
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_master_initrawheader(&dctx->header);
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt else
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dctx->header = *header;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews switch (format) {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews case dns_masterformat_text:
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dctx->dumpsets = dump_rdatasets_text;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews break;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews case dns_masterformat_raw:
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dctx->dumpsets = dump_rdatasets_raw;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews break;
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt case dns_masterformat_map:
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt dctx->dumpsets = dump_rdatasets_map;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb break;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews default:
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews INSIST(0);
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews break;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews }
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = totext_ctx_init(style, &dctx->tctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews "could not set master file style");
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews goto cleanup;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_stdtime_get(&dctx->now);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_attach(db, &dctx->db);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->do_date = dns_db_iscache(dctx->db);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews if (dctx->format == dns_masterformat_text &&
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews (dctx->tctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) {
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews options = DNS_DB_RELATIVENAMES;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews } else
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews options = 0;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_db_createiterator(dctx->db, options, &dctx->dbiter);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews goto cleanup;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = isc_mutex_init(&dctx->lock);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews goto cleanup;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (version != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_attachversion(dctx->db, version, &dctx->version);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews else if (!dns_db_iscache(db))
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_currentversion(dctx->db, &dctx->version);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_attach(mctx, &dctx->mctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->references = 1;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->magic = DNS_DCTX_MAGIC;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews *dctxp = dctx;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (ISC_R_SUCCESS);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews cleanup:
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->dbiter != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dbiterator_destroy(&dctx->dbiter);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->db != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_detach(&dctx->db);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_put(mctx, dctx, sizeof(*dctx));
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_result_t
7829fad4093f2c1985b1efb7cea00287ff015d2bckbwriteheader(dns_dumpctx_t *dctx) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_result_t result = ISC_R_SUCCESS;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_buffer_t buffer;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *bufmem;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_region_t r;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb dns_masterrawheader_t rawheader;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_uint32_t rawversion, now32;
8e6b386ab7e2d1bd8efedecbb8f4efb6b572a866Tinderbox User
7829fad4093f2c1985b1efb7cea00287ff015d2bckb bufmem = isc_mem_get(dctx->mctx, initial_buffer_length);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb if (bufmem == NULL)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb return (ISC_R_NOMEMORY);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_init(&buffer, bufmem, initial_buffer_length);
8e6b386ab7e2d1bd8efedecbb8f4efb6b572a866Tinderbox User
7829fad4093f2c1985b1efb7cea00287ff015d2bckb switch (dctx->format) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb case dns_masterformat_text:
7829fad4093f2c1985b1efb7cea00287ff015d2bckb /*
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * If the database has cache semantics, output an
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * RFC2540 $DATE directive so that the TTLs can be
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * adjusted when it is reloaded. For zones it is not
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * really needed, and it would make the file
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * incompatible with pre-RFC2540 software, so we omit
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * it in the zone case.
7829fad4093f2c1985b1efb7cea00287ff015d2bckb */
7829fad4093f2c1985b1efb7cea00287ff015d2bckb if (dctx->do_date) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb result = dns_time32_totext(dctx->now, &buffer);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb RUNTIME_CHECK(result == ISC_R_SUCCESS);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_usedregion(&buffer, &r);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb fprintf(dctx->f, "$DATE %.*s\n",
7829fad4093f2c1985b1efb7cea00287ff015d2bckb (int) r.length, (char *) r.base);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb }
7829fad4093f2c1985b1efb7cea00287ff015d2bckb break;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb case dns_masterformat_raw:
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt case dns_masterformat_map:
7829fad4093f2c1985b1efb7cea00287ff015d2bckb r.base = (unsigned char *)&rawheader;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb r.length = sizeof(rawheader);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_region(&buffer, &r);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb#if !defined(STDTIME_ON_32BITS) || (STDTIME_ON_32BITS + 0) != 1
7829fad4093f2c1985b1efb7cea00287ff015d2bckb /*
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * We assume isc_stdtime_t is a 32-bit integer,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * which should be the case on most platforms.
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * If it turns out to be uncommon, we'll need
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * to bump the version number and revise the
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * header format.
7829fad4093f2c1985b1efb7cea00287ff015d2bckb */
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_log_write(dns_lctx,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb ISC_LOGCATEGORY_GENERAL,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb DNS_LOGMODULE_MASTERDUMP,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb ISC_LOG_INFO,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb "dumping master file in raw "
7829fad4093f2c1985b1efb7cea00287ff015d2bckb "format: stdtime is not 32bits");
7829fad4093f2c1985b1efb7cea00287ff015d2bckb now32 = 0;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb#else
7829fad4093f2c1985b1efb7cea00287ff015d2bckb now32 = dctx->now;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb#endif
7829fad4093f2c1985b1efb7cea00287ff015d2bckb rawversion = 1;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb if ((dctx->header.flags & DNS_MASTERRAW_COMPAT) != 0)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb rawversion = 0;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_putuint32(&buffer, dctx->format);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_putuint32(&buffer, rawversion);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_putuint32(&buffer, now32);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb if (rawversion == 1) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_putuint32(&buffer, dctx->header.flags);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_putuint32(&buffer,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb dctx->header.sourceserial);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_putuint32(&buffer, dctx->header.lastxfrin);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb }
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb INSIST(isc_buffer_usedlength(&buffer) <= sizeof(rawheader));
7829fad4093f2c1985b1efb7cea00287ff015d2bckb result = isc_stdio_write(buffer.base, 1,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_usedlength(&buffer),
7829fad4093f2c1985b1efb7cea00287ff015d2bckb dctx->f, NULL);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb if (result != ISC_R_SUCCESS)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb break;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb break;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb default:
7829fad4093f2c1985b1efb7cea00287ff015d2bckb INSIST(0);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb }
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_mem_put(dctx->mctx, buffer.base, buffer.length);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb return (result);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb}
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7829fad4093f2c1985b1efb7cea00287ff015d2bckbstatic isc_result_t
7829fad4093f2c1985b1efb7cea00287ff015d2bckbdumptostreaminc(dns_dumpctx_t *dctx) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_result_t result = ISC_R_SUCCESS;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb isc_buffer_t buffer;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb char *bufmem;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_name_t *name;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_fixedname_t fixname;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews unsigned int nodes;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews isc_time_t start;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews bufmem = isc_mem_get(dctx->mctx, initial_buffer_length);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (bufmem == NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (ISC_R_NOMEMORY);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_buffer_init(&buffer, bufmem, initial_buffer_length);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_fixedname_init(&fixname);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews name = dns_fixedname_name(&fixname);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->first) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb CHECK(writeheader(dctx));
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt
7829fad4093f2c1985b1efb7cea00287ff015d2bckb /*
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * Fast format is not currently written incrementally,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * so we make the call to dns_db_serialize() here.
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * If the database is anything other than an rbtdb,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb * this should result in not implemented
7829fad4093f2c1985b1efb7cea00287ff015d2bckb */
c9611b45736af157e2993c6ef852e55e8e24ca83Evan Hunt if (dctx->format == dns_masterformat_map) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb result = dns_db_serialize(dctx->db, dctx->version,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb dctx->f);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb goto cleanup;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews }
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dns_dbiterator_first(dctx->dbiter);
37702cb83cfae78df1bb2a90cde8580091dece0bMark Andrews if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb goto cleanup;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->first = ISC_FALSE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews } else
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = ISC_R_SUCCESS;
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews nodes = dctx->nodes;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews isc_time_now(&start);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews while (result == ISC_R_SUCCESS && (dctx->nodes == 0 || nodes--)) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_rdatasetiter_t *rdsiter = NULL;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson dns_dbnode_t *node = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dns_dbiterator_current(dctx->dbiter, &node, name);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson break;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson if (result == DNS_R_NEWORIGIN) {
c801dd02ed98321f3ccab93c159a1dce61961c58Bob Halley dns_name_t *origin =
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_fixedname_name(&dctx->tctx.origin_fixname);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dns_dbiterator_origin(dctx->dbiter, origin);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff RUNTIME_CHECK(result == ISC_R_SUCCESS);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb if ((dctx->tctx.style.flags &
7829fad4093f2c1985b1efb7cea00287ff015d2bckb DNS_STYLEFLAG_REL_DATA) != 0)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->tctx.origin = origin;
692ae2fff922f1c072169d1ddda8e600cb572a9bMark Andrews dctx->tctx.neworigin = origin;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dns_db_allrdatasets(dctx->db, node, dctx->version,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->now, &rdsiter);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_detachnode(dctx->db, &node);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb goto cleanup;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = (dctx->dumpsets)(dctx->mctx, name, rdsiter,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews &dctx->tctx, &buffer, dctx->f);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_rdatasetiter_destroy(&rdsiter);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_detachnode(dctx->db, &node);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb goto cleanup;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_db_detachnode(dctx->db, &node);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dns_dbiterator_next(dctx->dbiter);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews /*
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews * Work out how many nodes can be written in the time between
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews * two requests to the nameserver. Smooth the resulting number and
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews * use it as a estimate for the number of nodes to be written in the
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews * next iteration.
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews */
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx->nodes != 0 && result == ISC_R_SUCCESS) {
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews unsigned int pps = dns_pps; /* packets per second */
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews unsigned int interval;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews isc_uint64_t usecs;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews isc_time_t end;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews isc_time_now(&end);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (pps < 100)
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews pps = 100;
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews interval = 1000000 / pps; /* interval in usecs */
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (interval == 0)
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews interval = 1;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews usecs = isc_time_microdiff(&end, &start);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (usecs == 0) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews dctx->nodes = dctx->nodes * 2;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (dctx->nodes > 1000)
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews dctx->nodes = 1000;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews } else {
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews nodes = dctx->nodes * interval;
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews nodes /= (unsigned int)usecs;
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews if (nodes == 0)
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews nodes = 1;
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews else if (nodes > 1000)
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews nodes = 1000;
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews /* Smooth and assign. */
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews dctx->nodes = (nodes + dctx->nodes * 7) / 8;
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
cfaf65f53fb0c1779e7b2e07216e5fbfd3a2d52eMark Andrews DNS_LOGMODULE_MASTERDUMP,
cfaf65f53fb0c1779e7b2e07216e5fbfd3a2d52eMark Andrews ISC_LOG_DEBUG(1),
a135cb62d310883601285f8b511875f1b9586417Mark Andrews "dumptostreaminc(%p) new nodes -> %d",
4c1817c29c78d533eae9f4bdf8c9b4d5c90ebfdbMark Andrews dctx, dctx->nodes);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews }
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = DNS_R_CONTINUE;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews } else if (result == ISC_R_NOMORE)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = ISC_R_SUCCESS;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb cleanup:
515ada69db06a727ca1197e2aa1f6a286d278228Tatuya JINMEI 神明達哉 RUNTIME_CHECK(dns_dbiterator_pause(dctx->dbiter) == ISC_R_SUCCESS);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_put(dctx->mctx, buffer.base, buffer.length);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsisc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_master_dumptostreaminc(isc_mem_t *mctx, dns_db_t *db,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dbversion_t *version,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews const dns_master_style_t *style,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews FILE *f, isc_task_t *task,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpdonefunc_t done, void *done_arg,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t **dctxp)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews{
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_result_t result;
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(task != NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(f != NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews REQUIRE(done != NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = dumpctx_create(mctx, db, version, style, f, &dctx,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterformat_text, NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_attach(task, &dctx->task);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->done = done;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->done_arg = done_arg;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->nodes = 100;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = task_send(dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result == ISC_R_SUCCESS) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_attach(dctx, dctxp);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (DNS_R_CONTINUE);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
2674e1a455d4f71de09b2b60e7a8304b9a305588Mark Andrews dns_dumpctx_detach(&dctx);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews/*
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews * Dump an entire database into a master file.
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffisc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_master_dumptostream(isc_mem_t *mctx, dns_db_t *db,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dbversion_t *version,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews const dns_master_style_t *style,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews FILE *f)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews{
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt return (dns_master_dumptostream3(mctx, db, version, style,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterformat_text, NULL, f));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews}
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsisc_result_t
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsdns_master_dumptostream2(isc_mem_t *mctx, dns_db_t *db,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_dbversion_t *version,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews const dns_master_style_t *style,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_masterformat_t format, FILE *f)
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt{
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt return (dns_master_dumptostream3(mctx, db, version, style,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt format, NULL, f));
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt}
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt
b4d8192d210290112e07b0e22b491c45c50ba696Evan Huntisc_result_t
b4d8192d210290112e07b0e22b491c45c50ba696Evan Huntdns_master_dumptostream3(isc_mem_t *mctx, dns_db_t *db,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_dbversion_t *version,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt const dns_master_style_t *style,
b54ac42f19a5c914df39817acce4efe92607b907Automatic Updater dns_masterformat_t format,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterrawheader_t *header, FILE *f)
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson{
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_result_t result;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt result = dumpctx_create(mctx, db, version, style, f, &dctx,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt format, header);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dumptostreaminc(dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews INSIST(result != DNS_R_CONTINUE);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_detach(&dctx);
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt
d9eebc08497af272b2d44c07f4eb85153dec4253Evan Hunt result = flushandsync(f, result, NULL);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsstatic isc_result_t
7e9d637131516486630290d36c4c0db544cb700eMark Andrewsopentmp(isc_mem_t *mctx, dns_masterformat_t format, const char *file,
7e9d637131516486630290d36c4c0db544cb700eMark Andrews char **tempp, FILE **fp) {
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson FILE *f = NULL;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_result_t result;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *tempname = NULL;
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson int tempnamelen;
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews tempnamelen = strlen(file) + 20;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews tempname = isc_mem_allocate(mctx, tempnamelen);
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson if (tempname == NULL)
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson return (ISC_R_NOMEMORY);
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = isc_file_mktemplate(file, tempname, tempnamelen);
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson if (result != ISC_R_SUCCESS)
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson goto cleanup;
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson
7e9d637131516486630290d36c4c0db544cb700eMark Andrews if (format == dns_masterformat_text)
7e9d637131516486630290d36c4c0db544cb700eMark Andrews result = isc_file_openunique(tempname, &f);
7e9d637131516486630290d36c4c0db544cb700eMark Andrews else
7e9d637131516486630290d36c4c0db544cb700eMark Andrews result = isc_file_bopenunique(tempname, &f);
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson if (result != ISC_R_SUCCESS) {
3e14b69d196a3ebeecc4662c426344dcfd7db678Andreas Gustafsson isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
913bc4304db0c4e0613bf1404c1caa29f9530180Andreas Gustafsson DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson "dumping master file: %s: open: %s",
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson tempname, isc_result_totext(result));
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson goto cleanup;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews *tempp = tempname;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews *fp = f;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (ISC_R_SUCCESS);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewscleanup:
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_free(mctx, tempname);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsisc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_master_dumpinc(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews const dns_master_style_t *style, const char *filename,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t **dctxp)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews{
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt return (dns_master_dumpinc3(mctx, db, version, style, filename, task,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews done, done_arg, dctxp,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterformat_text, NULL));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews}
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsisc_result_t
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsdns_master_dumpinc2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews const dns_master_style_t *style, const char *filename,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_dumpctx_t **dctxp, dns_masterformat_t format)
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt{
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt return (dns_master_dumpinc3(mctx, db, version, style, filename, task,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt done, done_arg, dctxp, format, NULL));
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt}
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt
b4d8192d210290112e07b0e22b491c45c50ba696Evan Huntisc_result_t
b4d8192d210290112e07b0e22b491c45c50ba696Evan Huntdns_master_dumpinc3(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt const dns_master_style_t *style, const char *filename,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_dumpctx_t **dctxp, dns_masterformat_t format,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterrawheader_t *header)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews{
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews FILE *f = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_result_t result;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *tempname = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *file = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews file = isc_mem_strdup(mctx, filename);
1d32e01f17c9db5f3482807ce8f2617e58f600aeMark Andrews if (file == NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (ISC_R_NOMEMORY);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
7e9d637131516486630290d36c4c0db544cb700eMark Andrews result = opentmp(mctx, format, filename, &tempname, &f);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS)
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson goto cleanup;
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt result = dumpctx_create(mctx, db, version, style, f, &dctx,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt format, header);
7231717254c3e872f5d2ca87c554dbbb4bddeeecMark Andrews if (result != ISC_R_SUCCESS) {
7231717254c3e872f5d2ca87c554dbbb4bddeeecMark Andrews (void)isc_stdio_close(f);
7231717254c3e872f5d2ca87c554dbbb4bddeeecMark Andrews (void)isc_file_remove(tempname);
7231717254c3e872f5d2ca87c554dbbb4bddeeecMark Andrews goto cleanup;
7231717254c3e872f5d2ca87c554dbbb4bddeeecMark Andrews }
7231717254c3e872f5d2ca87c554dbbb4bddeeecMark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_task_attach(task, &dctx->task);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->done = done;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->done_arg = done_arg;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->nodes = 100;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->file = file;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews file = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dctx->tmpfile = tempname;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews tempname = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = task_send(dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result == ISC_R_SUCCESS) {
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_attach(dctx, dctxp);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (DNS_R_CONTINUE);
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson }
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews cleanup:
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (dctx != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_detach(&dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (file != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_free(mctx, file);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (tempname != NULL)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_free(mctx, tempname);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews}
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsisc_result_t
9aba20edee4e704433a464ae43b070b0775de506Mark Andrewsdns_master_dump(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews const dns_master_style_t *style, const char *filename)
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews{
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt return (dns_master_dump3(mctx, db, version, style, filename,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterformat_text, NULL));
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews}
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsisc_result_t
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsdns_master_dump2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews const dns_master_style_t *style, const char *filename,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews dns_masterformat_t format)
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt{
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt return (dns_master_dump3(mctx, db, version, style, filename,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt format, NULL));
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt}
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt
b4d8192d210290112e07b0e22b491c45c50ba696Evan Huntisc_result_t
b4d8192d210290112e07b0e22b491c45c50ba696Evan Huntdns_master_dump3(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt const dns_master_style_t *style, const char *filename,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt dns_masterformat_t format, dns_masterrawheader_t *header)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews{
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews FILE *f = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_result_t result;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews char *tempname;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_t *dctx = NULL;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
7e9d637131516486630290d36c4c0db544cb700eMark Andrews result = opentmp(mctx, format, filename, &tempname, &f);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews return (result);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt result = dumpctx_create(mctx, db, version, style, f, &dctx,
b4d8192d210290112e07b0e22b491c45c50ba696Evan Hunt format, header);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews if (result != ISC_R_SUCCESS)
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews goto cleanup;
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = dumptostreaminc(dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews INSIST(result != DNS_R_CONTINUE);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews dns_dumpctx_detach(&dctx);
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews result = closeandrename(f, result, tempname, filename);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
b7e031d5173476224027407d8e23eaa7557fc396Andreas Gustafsson cleanup:
9aba20edee4e704433a464ae43b070b0775de506Mark Andrews isc_mem_free(mctx, tempname);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson return (result);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington/*
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington * Dump a database node into a master file.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * XXX: this function assumes the text format.
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington */
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellingtonisc_result_t
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellingtondns_master_dumpnodetostream(isc_mem_t *mctx, dns_db_t *db,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington dns_dbversion_t *version,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington dns_dbnode_t *node, dns_name_t *name,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington const dns_master_style_t *style,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington FILE *f)
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington{
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_result_t result;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_buffer_t buffer;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington char *bufmem;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_stdtime_t now;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington dns_totext_ctx_t ctx;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington dns_rdatasetiter_t *rdsiter = NULL;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington result = totext_ctx_init(style, &ctx);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington if (result != ISC_R_SUCCESS) {
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington "could not set master file style");
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington return (ISC_R_UNEXPECTED);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington }
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_stdtime_get(&now);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington bufmem = isc_mem_get(mctx, initial_buffer_length);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington if (bufmem == NULL)
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington return (ISC_R_NOMEMORY);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_buffer_init(&buffer, bufmem, initial_buffer_length);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington result = dns_db_allrdatasets(db, node, version, now, &rdsiter);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington if (result != ISC_R_SUCCESS)
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington goto failure;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews result = dump_rdatasets_text(mctx, name, rdsiter, &ctx, &buffer, f);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington if (result != ISC_R_SUCCESS)
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington goto failure;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington dns_rdatasetiter_destroy(&rdsiter);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington result = ISC_R_SUCCESS;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington failure:
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_mem_put(mctx, buffer.base, buffer.length);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington return (result);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington}
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellingtonisc_result_t
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellingtondns_master_dumpnode(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington dns_dbnode_t *node, dns_name_t *name,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington const dns_master_style_t *style, const char *filename)
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington{
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington FILE *f = NULL;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_result_t result;
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington result = isc_stdio_open(filename, "w", &f);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington if (result != ISC_R_SUCCESS) {
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington "dumping node to file: %s: open: %s", filename,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_result_totext(result));
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington return (ISC_R_UNEXPECTED);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington }
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington result = dns_master_dumpnodetostream(mctx, db, version, node, name,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington style, f);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews if (result != ISC_R_SUCCESS) {
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews "dumping master file: %s: dump: %s", filename,
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews isc_result_totext(result));
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews (void)isc_stdio_close(f);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews return (ISC_R_UNEXPECTED);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews }
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington result = isc_stdio_close(f);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington if (result != ISC_R_SUCCESS) {
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington "dumping master file: %s: close: %s", filename,
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington isc_result_totext(result));
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington return (ISC_R_UNEXPECTED);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington }
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington return (result);
19f4d25fd5b35b1375b0b9f13b66770ee4a66154Brian Wellington}
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews
e63d63dc8510c669e1575b2762265842e8783822Evan Huntdns_masterstyle_flags_t
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Huntdns_master_styleflags(const dns_master_style_t *style) {
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt REQUIRE(style != NULL);
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt return (style->flags);
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt}
b66b333f59cf51ef87f973084a5023acd9317fb2Evan Hunt
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrewsisc_result_t
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrewsdns_master_stylecreate(dns_master_style_t **stylep, unsigned int flags,
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews unsigned int ttl_column, unsigned int class_column,
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews unsigned int type_column, unsigned int rdata_column,
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews unsigned int line_length, unsigned int tab_width,
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews isc_mem_t *mctx)
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt{
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt return (dns_master_stylecreate2(stylep, flags, ttl_column,
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt class_column, type_column,
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt rdata_column, line_length,
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt tab_width, 0xffffffff, mctx));
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt}
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Huntisc_result_t
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Huntdns_master_stylecreate2(dns_master_style_t **stylep, unsigned int flags,
0e27506ce3135f9bd49e12564ad0e15256135118Automatic Updater unsigned int ttl_column, unsigned int class_column,
0e27506ce3135f9bd49e12564ad0e15256135118Automatic Updater unsigned int type_column, unsigned int rdata_column,
0e27506ce3135f9bd49e12564ad0e15256135118Automatic Updater unsigned int line_length, unsigned int tab_width,
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt unsigned int split_width, isc_mem_t *mctx)
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews{
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews dns_master_style_t *style;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews REQUIRE(stylep != NULL && *stylep == NULL);
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style = isc_mem_get(mctx, sizeof(*style));
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews if (style == NULL)
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews return (ISC_R_NOMEMORY);
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->flags = flags;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->ttl_column = ttl_column;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->class_column = class_column;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->type_column = type_column;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->rdata_column = rdata_column;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->line_length = line_length;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style->tab_width = tab_width;
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt style->split_width = split_width;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews *stylep = style;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews return (ISC_R_SUCCESS);
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews}
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrewsvoid
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrewsdns_master_styledestroy(dns_master_style_t **stylep, isc_mem_t *mctx) {
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews dns_master_style_t *style;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews REQUIRE(stylep != NULL && *stylep != NULL);
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews style = *stylep;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews *stylep = NULL;
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews isc_mem_put(mctx, style, sizeof(*style));
b9efcf0a377381b29960137e54ecaf4db85a35c8Mark Andrews}