check-tool.c revision 135446
1/*
2 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: check-tool.c,v 1.4.12.5 2004/03/08 04:04:13 marka Exp $ */
19
20#include <config.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "check-tool.h"
26#include <isc/util.h>
27
28#include <isc/buffer.h>
29#include <isc/log.h>
30#include <isc/region.h>
31#include <isc/stdio.h>
32#include <isc/types.h>
33
34#include <dns/fixedname.h>
35#include <dns/name.h>
36#include <dns/rdataclass.h>
37#include <dns/types.h>
38#include <dns/zone.h>
39
40#define CHECK(r) \
41        do { \
42		result = (r); \
43                if (result != ISC_R_SUCCESS) \
44                        goto cleanup; \
45        } while (0)
46
47static const char *dbtype[] = { "rbt" };
48
49int debug = 0;
50isc_boolean_t nomerge = ISC_TRUE;
51unsigned int zone_options = DNS_ZONEOPT_CHECKNS|DNS_ZONEOPT_MANYERRORS;
52
53isc_result_t
54setup_logging(isc_mem_t *mctx, isc_log_t **logp) {
55	isc_logdestination_t destination;
56	isc_logconfig_t *logconfig = NULL;
57	isc_log_t *log = NULL;
58
59	RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
60	isc_log_setcontext(log);
61
62	destination.file.stream = stdout;
63	destination.file.name = NULL;
64	destination.file.versions = ISC_LOG_ROLLNEVER;
65	destination.file.maximum_size = 0;
66	RUNTIME_CHECK(isc_log_createchannel(logconfig, "stderr",
67				       ISC_LOG_TOFILEDESC,
68				       ISC_LOG_DYNAMIC,
69				       &destination, 0) == ISC_R_SUCCESS);
70	RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
71					 NULL, NULL) == ISC_R_SUCCESS);
72
73	*logp = log;
74	return (ISC_R_SUCCESS);
75}
76
77isc_result_t
78load_zone(isc_mem_t *mctx, const char *zonename, const char *filename,
79	  const char *classname, dns_zone_t **zonep)
80{
81	isc_result_t result;
82	dns_rdataclass_t rdclass;
83	isc_textregion_t region;
84	isc_buffer_t buffer;
85	dns_fixedname_t fixorigin;
86	dns_name_t *origin;
87	dns_zone_t *zone = NULL;
88
89	REQUIRE(zonep == NULL || *zonep == NULL);
90
91	if (debug)
92		fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
93			zonename, filename, classname);
94
95	CHECK(dns_zone_create(&zone, mctx));
96
97	dns_zone_settype(zone, dns_zone_master);
98
99	isc_buffer_init(&buffer, zonename, strlen(zonename));
100	isc_buffer_add(&buffer, strlen(zonename));
101	dns_fixedname_init(&fixorigin);
102	origin = dns_fixedname_name(&fixorigin);
103	CHECK(dns_name_fromtext(origin, &buffer, dns_rootname,
104			        ISC_FALSE, NULL));
105	CHECK(dns_zone_setorigin(zone, origin));
106	CHECK(dns_zone_setdbtype(zone, 1, (const char * const *) dbtype));
107	CHECK(dns_zone_setfile(zone, filename));
108
109	DE_CONST(classname, region.base);
110	region.length = strlen(classname);
111	CHECK(dns_rdataclass_fromtext(&rdclass, &region));
112
113	dns_zone_setclass(zone, rdclass);
114	dns_zone_setoption(zone, zone_options, ISC_TRUE);
115	dns_zone_setoption(zone, DNS_ZONEOPT_NOMERGE, nomerge);
116
117	CHECK(dns_zone_load(zone));
118	if (zonep != NULL){
119		*zonep = zone;
120		zone = NULL;
121	}
122
123 cleanup:
124	if (zone != NULL)
125		dns_zone_detach(&zone);
126	return (result);
127}
128
129isc_result_t
130dump_zone(const char *zonename, dns_zone_t *zone, const char *filename)
131{
132	isc_result_t result;
133	FILE *output = stdout;
134
135	if (debug) {
136		if (filename != NULL)
137			fprintf(stderr, "dumping \"%s\" to \"%s\"\n",
138				zonename, filename);
139		else
140			fprintf(stderr, "dumping \"%s\"\n", zonename);
141	}
142
143	if (filename != NULL) {
144		result = isc_stdio_open(filename, "w+", &output);
145
146		if (result != ISC_R_SUCCESS) {
147			fprintf(stderr, "could not open output "
148				"file \"%s\" for writing\n", filename);
149			return (ISC_R_FAILURE);
150		}
151	}
152
153	result = dns_zone_fulldumptostream(zone, output);
154
155	if (filename != NULL)
156		(void)isc_stdio_close(output);
157
158	return (result);
159}
160