Merge branch 'vendor/GCC44'
[games.git] / contrib / bind-9.3 / bin / check / check-tool.c
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.7 2004/11/30 01:15:40 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/log.h>
36 #include <dns/name.h>
37 #include <dns/rdataclass.h>
38 #include <dns/types.h>
39 #include <dns/zone.h>
40
41 #define CHECK(r) \
42         do { \
43                 result = (r); \
44                 if (result != ISC_R_SUCCESS) \
45                         goto cleanup; \
46         } while (0)   
47
48 static const char *dbtype[] = { "rbt" };
49
50 int debug = 0;
51 isc_boolean_t nomerge = ISC_TRUE;
52 unsigned int zone_options = DNS_ZONEOPT_CHECKNS | 
53                             DNS_ZONEOPT_MANYERRORS |
54                             DNS_ZONEOPT_CHECKNAMES;
55
56 isc_result_t
57 setup_logging(isc_mem_t *mctx, isc_log_t **logp) {
58         isc_logdestination_t destination;
59         isc_logconfig_t *logconfig = NULL;
60         isc_log_t *log = NULL;
61
62         RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
63         isc_log_setcontext(log);
64
65         destination.file.stream = stdout;
66         destination.file.name = NULL;
67         destination.file.versions = ISC_LOG_ROLLNEVER;
68         destination.file.maximum_size = 0;
69         RUNTIME_CHECK(isc_log_createchannel(logconfig, "stderr",
70                                        ISC_LOG_TOFILEDESC,
71                                        ISC_LOG_DYNAMIC,
72                                        &destination, 0) == ISC_R_SUCCESS);
73         RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
74                                          NULL, NULL) == ISC_R_SUCCESS);
75
76         *logp = log;
77         return (ISC_R_SUCCESS);
78 }
79
80 isc_result_t
81 load_zone(isc_mem_t *mctx, const char *zonename, const char *filename,
82           const char *classname, dns_zone_t **zonep)
83 {
84         isc_result_t result;
85         dns_rdataclass_t rdclass;
86         isc_textregion_t region;
87         isc_buffer_t buffer;
88         dns_fixedname_t fixorigin;
89         dns_name_t *origin;
90         dns_zone_t *zone = NULL;
91
92         REQUIRE(zonep == NULL || *zonep == NULL);
93
94         if (debug)
95                 fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
96                         zonename, filename, classname);
97
98         CHECK(dns_zone_create(&zone, mctx));
99
100         dns_zone_settype(zone, dns_zone_master);
101
102         isc_buffer_init(&buffer, zonename, strlen(zonename));
103         isc_buffer_add(&buffer, strlen(zonename));
104         dns_fixedname_init(&fixorigin);
105         origin = dns_fixedname_name(&fixorigin);
106         CHECK(dns_name_fromtext(origin, &buffer, dns_rootname,
107                                 ISC_FALSE, NULL));
108         CHECK(dns_zone_setorigin(zone, origin));
109         CHECK(dns_zone_setdbtype(zone, 1, (const char * const *) dbtype));
110         CHECK(dns_zone_setfile(zone, filename));
111
112         DE_CONST(classname, region.base);
113         region.length = strlen(classname);
114         CHECK(dns_rdataclass_fromtext(&rdclass, &region));
115
116         dns_zone_setclass(zone, rdclass);
117         dns_zone_setoption(zone, zone_options, ISC_TRUE);
118         dns_zone_setoption(zone, DNS_ZONEOPT_NOMERGE, nomerge);
119
120         CHECK(dns_zone_load(zone));
121         if (zonep != NULL){
122                 *zonep = zone;
123                 zone = NULL;
124         }
125
126  cleanup:
127         if (zone != NULL)
128                 dns_zone_detach(&zone);
129         return (result);
130 }
131
132 isc_result_t
133 dump_zone(const char *zonename, dns_zone_t *zone, const char *filename)
134 {
135         isc_result_t result;
136         FILE *output = stdout;
137
138         if (debug) {
139                 if (filename != NULL)
140                         fprintf(stderr, "dumping \"%s\" to \"%s\"\n",
141                                 zonename, filename);
142                 else
143                         fprintf(stderr, "dumping \"%s\"\n", zonename);
144         }
145
146         if (filename != NULL) {
147                 result = isc_stdio_open(filename, "w+", &output);
148
149                 if (result != ISC_R_SUCCESS) {
150                         fprintf(stderr, "could not open output "
151                                 "file \"%s\" for writing\n", filename);
152                         return (ISC_R_FAILURE);
153                 }
154         }
155
156         result = dns_zone_fulldumptostream(zone, output);
157
158         if (filename != NULL)
159                 (void)isc_stdio_close(output);
160
161         return (result);
162 }