Merge from vendor branch TEXINFO:
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / check / named-checkzone.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-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: named-checkzone.c,v 1.13.2.4 2004/03/09 06:09:09 marka Exp $ */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <isc/app.h>
25 #include <isc/commandline.h>
26 #include <isc/log.h>
27 #include <isc/mem.h>
28 #include <isc/socket.h>
29 #include <isc/string.h>
30 #include <isc/task.h>
31 #include <isc/timer.h>
32 #include <isc/util.h>
33
34 #include <dns/db.h>
35 #include <dns/fixedname.h>
36 #include <dns/log.h>
37 #include <dns/rdataclass.h>
38 #include <dns/rdataset.h>
39 #include <dns/result.h>
40 #include <dns/zone.h>
41
42 #include "check-tool.h"
43
44 static int debug = 0;
45 isc_boolean_t nomerge = ISC_TRUE;
46 static int quiet = 0;
47 static isc_mem_t *mctx = NULL;
48 dns_zone_t *zone = NULL;
49 dns_zonetype_t zonetype = dns_zone_master;
50 static const char *dbtype[] = { "rbt" };
51
52 #define ERRRET(result, function) \
53         do { \
54                 if (result != ISC_R_SUCCESS) { \
55                         if (!quiet) \
56                                 fprintf(stderr, "%s() returned %s\n", \
57                                         function, dns_result_totext(result)); \
58                         return (result); \
59                 } \
60         } while (0)
61
62 static void
63 usage(void) {
64         fprintf(stderr,
65                 "usage: named-checkzone [-djqv] [-c class] zonename filename \n");
66         exit(1);
67 }
68
69 static isc_result_t
70 setup(char *zonename, char *filename, char *classname) {
71         isc_result_t result;
72         dns_rdataclass_t rdclass;
73         isc_textregion_t region;
74         isc_buffer_t buffer;
75         dns_fixedname_t fixorigin;
76         dns_name_t *origin;
77
78         if (debug)
79                 fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
80                         zonename, filename, classname);
81         result = dns_zone_create(&zone, mctx);
82         ERRRET(result, "dns_zone_new");
83
84         dns_zone_settype(zone, zonetype);
85
86         isc_buffer_init(&buffer, zonename, strlen(zonename));
87         isc_buffer_add(&buffer, strlen(zonename));
88         dns_fixedname_init(&fixorigin);
89         result = dns_name_fromtext(dns_fixedname_name(&fixorigin),
90                                    &buffer, dns_rootname, ISC_FALSE, NULL);
91         ERRRET(result, "dns_name_fromtext");
92         origin = dns_fixedname_name(&fixorigin);
93
94         result = dns_zone_setorigin(zone, origin);
95         ERRRET(result, "dns_zone_setorigin");
96
97         result = dns_zone_setdbtype(zone, 1, (const char * const *) dbtype);
98         ERRRET(result, "dns_zone_setdatabase");
99
100         result = dns_zone_setfile(zone, filename);
101         ERRRET(result, "dns_zone_setdatabase");
102
103         region.base = classname;
104         region.length = strlen(classname);
105         result = dns_rdataclass_fromtext(&rdclass, &region);
106         ERRRET(result, "dns_rdataclass_fromtext");
107
108         dns_zone_setclass(zone, rdclass);
109         dns_zone_setoption(zone, DNS_ZONEOPT_MANYERRORS, ISC_TRUE);
110         dns_zone_setoption(zone, DNS_ZONEOPT_NOMERGE, nomerge);
111
112         result = dns_zone_load(zone);
113
114         return (result);
115 }
116
117 static void
118 destroy(void) {
119         if (zone != NULL)
120                 dns_zone_detach(&zone);
121 }
122
123 int
124 main(int argc, char **argv) {
125         int c;
126         char *origin = NULL;
127         char *filename = NULL;
128         isc_log_t *lctx = NULL;
129         isc_result_t result;
130         char classname_in[] = "IN";
131         char *classname = classname_in;
132
133         while ((c = isc_commandline_parse(argc, argv, "c:djqsv")) != EOF) {
134                 switch (c) {
135                 case 'c':
136                         classname = isc_commandline_argument;
137                         break;
138                 case 'd':
139                         debug++;
140                         break;
141
142                 case 'j':
143                         nomerge = ISC_FALSE;
144                         break;
145                 case 'q':
146                         quiet++;
147                         break;
148                 case 'v':
149                         printf(VERSION "\n");
150                         exit(0);
151                 default:
152                         usage();
153                 }
154         }
155
156         if (isc_commandline_index + 2 > argc)
157                 usage();
158
159         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
160         if (!quiet) {
161                 RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
162                 dns_log_init(lctx);
163                 dns_log_setcontext(lctx);
164         }
165
166         dns_result_register();
167
168         origin = argv[isc_commandline_index++];
169         filename = argv[isc_commandline_index++];
170         result = setup(origin, filename, classname);
171         if (!quiet && result == ISC_R_SUCCESS)
172                 fprintf(stdout, "OK\n");
173         destroy();
174         if (lctx != NULL)
175                 isc_log_destroy(&lctx);
176         isc_mem_destroy(&mctx);
177         return ((result == ISC_R_SUCCESS) ? 0 : 1);
178 }