]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/bind9/bin/check/named-checkzone.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / bind9 / bin / check / named-checkzone.c
1 /*
2  * Copyright (C) 2004-2010  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or 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.61 2010-09-07 23:46:59 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25
26 #include <isc/app.h>
27 #include <isc/commandline.h>
28 #include <isc/dir.h>
29 #include <isc/entropy.h>
30 #include <isc/hash.h>
31 #include <isc/log.h>
32 #include <isc/mem.h>
33 #include <isc/socket.h>
34 #include <isc/string.h>
35 #include <isc/task.h>
36 #include <isc/timer.h>
37 #include <isc/util.h>
38
39 #include <dns/db.h>
40 #include <dns/fixedname.h>
41 #include <dns/log.h>
42 #include <dns/masterdump.h>
43 #include <dns/name.h>
44 #include <dns/rdataclass.h>
45 #include <dns/rdataset.h>
46 #include <dns/result.h>
47 #include <dns/types.h>
48 #include <dns/zone.h>
49
50 #include "check-tool.h"
51
52 static int quiet = 0;
53 static isc_mem_t *mctx = NULL;
54 static isc_entropy_t *ectx = NULL;
55 dns_zone_t *zone = NULL;
56 dns_zonetype_t zonetype = dns_zone_master;
57 static int dumpzone = 0;
58 static const char *output_filename;
59 static char *prog_name = NULL;
60 static const dns_master_style_t *outputstyle = NULL;
61 static enum { progmode_check, progmode_compile } progmode;
62
63 #define ERRRET(result, function) \
64         do { \
65                 if (result != ISC_R_SUCCESS) { \
66                         if (!quiet) \
67                                 fprintf(stderr, "%s() returned %s\n", \
68                                         function, dns_result_totext(result)); \
69                         return (result); \
70                 } \
71         } while (0)
72
73 ISC_PLATFORM_NORETURN_PRE static void
74 usage(void) ISC_PLATFORM_NORETURN_POST;
75
76 static void
77 usage(void) {
78         fprintf(stderr,
79                 "usage: %s [-djqvD] [-c class] "
80                 "[-f inputformat] [-F outputformat] "
81                 "[-t directory] [-w directory] [-k (ignore|warn|fail)] "
82                 "[-n (ignore|warn|fail)] [-m (ignore|warn|fail)] "
83                 "[-r (ignore|warn|fail)] "
84                 "[-i (full|full-sibling|local|local-sibling|none)] "
85                 "[-M (ignore|warn|fail)] [-S (ignore|warn|fail)] "
86                 "[-W (ignore|warn)] "
87                 "%s zonename filename\n",
88                 prog_name,
89                 progmode == progmode_check ? "[-o filename]" : "-o filename");
90         exit(1);
91 }
92
93 static void
94 destroy(void) {
95         if (zone != NULL)
96                 dns_zone_detach(&zone);
97         dns_name_destroy();
98 }
99
100 /*% main processing routine */
101 int
102 main(int argc, char **argv) {
103         int c;
104         char *origin = NULL;
105         char *filename = NULL;
106         isc_log_t *lctx = NULL;
107         isc_result_t result;
108         char classname_in[] = "IN";
109         char *classname = classname_in;
110         const char *workdir = NULL;
111         const char *inputformatstr = NULL;
112         const char *outputformatstr = NULL;
113         dns_masterformat_t inputformat = dns_masterformat_text;
114         dns_masterformat_t outputformat = dns_masterformat_text;
115         FILE *errout = stdout;
116
117         outputstyle = &dns_master_style_full;
118
119         prog_name = strrchr(argv[0], '/');
120         if (prog_name == NULL)
121                 prog_name = strrchr(argv[0], '\\');
122         if (prog_name != NULL)
123                 prog_name++;
124         else
125                 prog_name = argv[0];
126         /*
127          * Libtool doesn't preserve the program name prior to final
128          * installation.  Remove the libtool prefix ("lt-").
129          */
130         if (strncmp(prog_name, "lt-", 3) == 0)
131                 prog_name += 3;
132
133 #define PROGCMP(X) \
134         (strcasecmp(prog_name, X) == 0 || strcasecmp(prog_name, X ".exe") == 0)
135
136         if (PROGCMP("named-checkzone"))
137                 progmode = progmode_check;
138         else if (PROGCMP("named-compilezone"))
139                 progmode = progmode_compile;
140         else
141                 INSIST(0);
142
143         /* Compilation specific defaults */
144         if (progmode == progmode_compile) {
145                 zone_options |= (DNS_ZONEOPT_CHECKNS |
146                                  DNS_ZONEOPT_FATALNS |
147                                  DNS_ZONEOPT_CHECKDUPRR |
148                                  DNS_ZONEOPT_CHECKNAMES |
149                                  DNS_ZONEOPT_CHECKNAMESFAIL |
150                                  DNS_ZONEOPT_CHECKWILDCARD);
151         } else
152                 zone_options |= DNS_ZONEOPT_CHECKDUPRR;
153
154 #define ARGCMP(X) (strcmp(isc_commandline_argument, X) == 0)
155
156         isc_commandline_errprint = ISC_FALSE;
157
158         while ((c = isc_commandline_parse(argc, argv,
159                                        "c:df:hi:jk:m:n:qr:s:t:o:vw:DF:M:S:W:"))
160                != EOF) {
161                 switch (c) {
162                 case 'c':
163                         classname = isc_commandline_argument;
164                         break;
165
166                 case 'd':
167                         debug++;
168                         break;
169
170                 case 'i':
171                         if (ARGCMP("full")) {
172                                 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY |
173                                                 DNS_ZONEOPT_CHECKSIBLING;
174                                 docheckmx = ISC_TRUE;
175                                 docheckns = ISC_TRUE;
176                                 dochecksrv = ISC_TRUE;
177                         } else if (ARGCMP("full-sibling")) {
178                                 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
179                                 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
180                                 docheckmx = ISC_TRUE;
181                                 docheckns = ISC_TRUE;
182                                 dochecksrv = ISC_TRUE;
183                         } else if (ARGCMP("local")) {
184                                 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
185                                 zone_options |= DNS_ZONEOPT_CHECKSIBLING;
186                                 docheckmx = ISC_FALSE;
187                                 docheckns = ISC_FALSE;
188                                 dochecksrv = ISC_FALSE;
189                         } else if (ARGCMP("local-sibling")) {
190                                 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
191                                 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
192                                 docheckmx = ISC_FALSE;
193                                 docheckns = ISC_FALSE;
194                                 dochecksrv = ISC_FALSE;
195                         } else if (ARGCMP("none")) {
196                                 zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
197                                 zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
198                                 docheckmx = ISC_FALSE;
199                                 docheckns = ISC_FALSE;
200                                 dochecksrv = ISC_FALSE;
201                         } else {
202                                 fprintf(stderr, "invalid argument to -i: %s\n",
203                                         isc_commandline_argument);
204                                 exit(1);
205                         }
206                         break;
207
208                 case 'f':
209                         inputformatstr = isc_commandline_argument;
210                         break;
211
212                 case 'F':
213                         outputformatstr = isc_commandline_argument;
214                         break;
215
216                 case 'j':
217                         nomerge = ISC_FALSE;
218                         break;
219
220                 case 'k':
221                         if (ARGCMP("warn")) {
222                                 zone_options |= DNS_ZONEOPT_CHECKNAMES;
223                                 zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
224                         } else if (ARGCMP("fail")) {
225                                 zone_options |= DNS_ZONEOPT_CHECKNAMES |
226                                                 DNS_ZONEOPT_CHECKNAMESFAIL;
227                         } else if (ARGCMP("ignore")) {
228                                 zone_options &= ~(DNS_ZONEOPT_CHECKNAMES |
229                                                   DNS_ZONEOPT_CHECKNAMESFAIL);
230                         } else {
231                                 fprintf(stderr, "invalid argument to -k: %s\n",
232                                         isc_commandline_argument);
233                                 exit(1);
234                         }
235                         break;
236
237                 case 'n':
238                         if (ARGCMP("ignore")) {
239                                 zone_options &= ~(DNS_ZONEOPT_CHECKNS|
240                                                   DNS_ZONEOPT_FATALNS);
241                         } else if (ARGCMP("warn")) {
242                                 zone_options |= DNS_ZONEOPT_CHECKNS;
243                                 zone_options &= ~DNS_ZONEOPT_FATALNS;
244                         } else if (ARGCMP("fail")) {
245                                 zone_options |= DNS_ZONEOPT_CHECKNS|
246                                                 DNS_ZONEOPT_FATALNS;
247                         } else {
248                                 fprintf(stderr, "invalid argument to -n: %s\n",
249                                         isc_commandline_argument);
250                                 exit(1);
251                         }
252                         break;
253
254                 case 'm':
255                         if (ARGCMP("warn")) {
256                                 zone_options |= DNS_ZONEOPT_CHECKMX;
257                                 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
258                         } else if (ARGCMP("fail")) {
259                                 zone_options |= DNS_ZONEOPT_CHECKMX |
260                                                 DNS_ZONEOPT_CHECKMXFAIL;
261                         } else if (ARGCMP("ignore")) {
262                                 zone_options &= ~(DNS_ZONEOPT_CHECKMX |
263                                                   DNS_ZONEOPT_CHECKMXFAIL);
264                         } else {
265                                 fprintf(stderr, "invalid argument to -m: %s\n",
266                                         isc_commandline_argument);
267                                 exit(1);
268                         }
269                         break;
270
271                 case 'o':
272                         output_filename = isc_commandline_argument;
273                         break;
274
275                 case 'q':
276                         quiet++;
277                         break;
278
279                 case 'r':
280                         if (ARGCMP("warn")) {
281                                 zone_options |= DNS_ZONEOPT_CHECKDUPRR;
282                                 zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
283                         } else if (ARGCMP("fail")) {
284                                 zone_options |= DNS_ZONEOPT_CHECKDUPRR |
285                                                 DNS_ZONEOPT_CHECKDUPRRFAIL;
286                         } else if (ARGCMP("ignore")) {
287                                 zone_options &= ~(DNS_ZONEOPT_CHECKDUPRR |
288                                                   DNS_ZONEOPT_CHECKDUPRRFAIL);
289                         } else {
290                                 fprintf(stderr, "invalid argument to -r: %s\n",
291                                         isc_commandline_argument);
292                                 exit(1);
293                         }
294                         break;
295
296                 case 's':
297                         if (ARGCMP("full"))
298                                 outputstyle = &dns_master_style_full;
299                         else if (ARGCMP("relative")) {
300                                 outputstyle = &dns_master_style_default;
301                         } else {
302                                 fprintf(stderr,
303                                         "unknown or unsupported style: %s\n",
304                                         isc_commandline_argument);
305                                 exit(1);
306                         }
307                         break;
308
309                 case 't':
310                         result = isc_dir_chroot(isc_commandline_argument);
311                         if (result != ISC_R_SUCCESS) {
312                                 fprintf(stderr, "isc_dir_chroot: %s: %s\n",
313                                         isc_commandline_argument,
314                                         isc_result_totext(result));
315                                 exit(1);
316                         }
317                         break;
318
319                 case 'v':
320                         printf(VERSION "\n");
321                         exit(0);
322
323                 case 'w':
324                         workdir = isc_commandline_argument;
325                         break;
326
327                 case 'D':
328                         dumpzone++;
329                         break;
330
331                 case 'M':
332                         if (ARGCMP("fail")) {
333                                 zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
334                                 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
335                         } else if (ARGCMP("warn")) {
336                                 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
337                                 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
338                         } else if (ARGCMP("ignore")) {
339                                 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
340                                 zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
341                         } else {
342                                 fprintf(stderr, "invalid argument to -M: %s\n",
343                                         isc_commandline_argument);
344                                 exit(1);
345                         }
346                         break;
347
348                 case 'S':
349                         if (ARGCMP("fail")) {
350                                 zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
351                                 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
352                         } else if (ARGCMP("warn")) {
353                                 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
354                                 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
355                         } else if (ARGCMP("ignore")) {
356                                 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
357                                 zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
358                         } else {
359                                 fprintf(stderr, "invalid argument to -S: %s\n",
360                                         isc_commandline_argument);
361                                 exit(1);
362                         }
363                         break;
364
365                 case 'W':
366                         if (ARGCMP("warn"))
367                                 zone_options |= DNS_ZONEOPT_CHECKWILDCARD;
368                         else if (ARGCMP("ignore"))
369                                 zone_options &= ~DNS_ZONEOPT_CHECKWILDCARD;
370                         break;
371
372                 case '?':
373                         if (isc_commandline_option != '?')
374                                 fprintf(stderr, "%s: invalid argument -%c\n",
375                                         prog_name, isc_commandline_option);
376                 case 'h':
377                         usage();
378
379                 default:
380                         fprintf(stderr, "%s: unhandled option -%c\n",
381                                 prog_name, isc_commandline_option);
382                         exit(1);
383                 }
384         }
385
386         if (workdir != NULL) {
387                 result = isc_dir_chdir(workdir);
388                 if (result != ISC_R_SUCCESS) {
389                         fprintf(stderr, "isc_dir_chdir: %s: %s\n",
390                                 workdir, isc_result_totext(result));
391                         exit(1);
392                 }
393         }
394
395         if (inputformatstr != NULL) {
396                 if (strcasecmp(inputformatstr, "text") == 0)
397                         inputformat = dns_masterformat_text;
398                 else if (strcasecmp(inputformatstr, "raw") == 0)
399                         inputformat = dns_masterformat_raw;
400                 else {
401                         fprintf(stderr, "unknown file format: %s\n",
402                             inputformatstr);
403                         exit(1);
404                 }
405         }
406
407         if (outputformatstr != NULL) {
408                 if (strcasecmp(outputformatstr, "text") == 0)
409                         outputformat = dns_masterformat_text;
410                 else if (strcasecmp(outputformatstr, "raw") == 0)
411                         outputformat = dns_masterformat_raw;
412                 else {
413                         fprintf(stderr, "unknown file format: %s\n",
414                                 outputformatstr);
415                         exit(1);
416                 }
417         }
418
419         if (progmode == progmode_compile) {
420                 dumpzone = 1;   /* always dump */
421                 if (output_filename == NULL) {
422                         fprintf(stderr,
423                                 "output file required, but not specified\n");
424                         usage();
425                 }
426         }
427
428         if (output_filename != NULL)
429                 dumpzone = 1;
430
431         /*
432          * If we are outputing to stdout then send the informational
433          * output to stderr.
434          */
435         if (dumpzone &&
436             (output_filename == NULL ||
437              strcmp(output_filename, "-") == 0 ||
438              strcmp(output_filename, "/dev/fd/1") == 0 ||
439              strcmp(output_filename, "/dev/stdout") == 0))
440                 errout = stderr;
441
442         if (isc_commandline_index + 2 != argc)
443                 usage();
444
445 #ifdef _WIN32
446         InitSockets();
447 #endif
448
449         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
450         if (!quiet)
451                 RUNTIME_CHECK(setup_logging(mctx, errout, &lctx)
452                               == ISC_R_SUCCESS);
453         RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
454         RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
455                       == ISC_R_SUCCESS);
456
457         dns_result_register();
458
459         origin = argv[isc_commandline_index++];
460         filename = argv[isc_commandline_index++];
461         result = load_zone(mctx, origin, filename, inputformat, classname,
462                            &zone);
463
464         if (result == ISC_R_SUCCESS && dumpzone) {
465                 if (!quiet && progmode == progmode_compile) {
466                         fprintf(errout, "dump zone to %s...", output_filename);
467                         fflush(errout);
468                 }
469                 result = dump_zone(origin, zone, output_filename,
470                                    outputformat, outputstyle);
471                 if (!quiet && progmode == progmode_compile)
472                         fprintf(errout, "done\n");
473         }
474
475         if (!quiet && result == ISC_R_SUCCESS)
476                 fprintf(errout, "OK\n");
477         destroy();
478         if (lctx != NULL)
479                 isc_log_destroy(&lctx);
480         isc_hash_destroy();
481         isc_entropy_detach(&ectx);
482         isc_mem_destroy(&mctx);
483 #ifdef _WIN32
484         DestroySockets();
485 #endif
486         return ((result == ISC_R_SUCCESS) ? 0 : 1);
487 }