]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/bin/check/named-checkconf.c
MFC r362623:
[FreeBSD/stable/8.git] / contrib / bind9 / bin / check / named-checkconf.c
1 /*
2  * Copyright (C) 2004-2007, 2009-2014  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  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-checkconf.c,v 1.54.62.2 2011/03/12 04:59:13 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include <isc/commandline.h>
29 #include <isc/dir.h>
30 #include <isc/entropy.h>
31 #include <isc/hash.h>
32 #include <isc/log.h>
33 #include <isc/mem.h>
34 #include <isc/result.h>
35 #include <isc/string.h>
36 #include <isc/util.h>
37
38 #include <isccfg/namedconf.h>
39
40 #include <bind9/check.h>
41
42 #include <dns/db.h>
43 #include <dns/fixedname.h>
44 #include <dns/log.h>
45 #include <dns/name.h>
46 #include <dns/rdataclass.h>
47 #include <dns/result.h>
48 #include <dns/rootns.h>
49 #include <dns/zone.h>
50
51 #include "check-tool.h"
52
53 static const char *program = "named-checkconf";
54
55 isc_log_t *logc = NULL;
56
57 #define CHECK(r)\
58         do { \
59                 result = (r); \
60                 if (result != ISC_R_SUCCESS) \
61                         goto cleanup; \
62         } while (0)
63
64 /*% usage */
65 ISC_PLATFORM_NORETURN_PRE static void
66 usage(void) ISC_PLATFORM_NORETURN_POST;
67
68 static void
69 usage(void) {
70         fprintf(stderr, "usage: %s [-h] [-j] [-p] [-v] [-z] [-t directory] "
71                 "[named.conf]\n", program);
72         exit(1);
73 }
74
75 /*% directory callback */
76 static isc_result_t
77 directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
78         isc_result_t result;
79         const char *directory;
80
81         REQUIRE(strcasecmp("directory", clausename) == 0);
82
83         UNUSED(arg);
84         UNUSED(clausename);
85
86         /*
87          * Change directory.
88          */
89         directory = cfg_obj_asstring(obj);
90         result = isc_dir_chdir(directory);
91         if (result != ISC_R_SUCCESS) {
92                 cfg_obj_log(obj, logc, ISC_LOG_ERROR,
93                             "change directory to '%s' failed: %s\n",
94                             directory, isc_result_totext(result));
95                 return (result);
96         }
97
98         return (ISC_R_SUCCESS);
99 }
100
101 static isc_boolean_t
102 get_maps(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
103         int i;
104         for (i = 0;; i++) {
105                 if (maps[i] == NULL)
106                         return (ISC_FALSE);
107                 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
108                         return (ISC_TRUE);
109         }
110 }
111
112 static isc_boolean_t
113 get_checknames(const cfg_obj_t **maps, const cfg_obj_t **obj) {
114         const cfg_listelt_t *element;
115         const cfg_obj_t *checknames;
116         const cfg_obj_t *type;
117         const cfg_obj_t *value;
118         isc_result_t result;
119         int i;
120
121         for (i = 0;; i++) {
122                 if (maps[i] == NULL)
123                         return (ISC_FALSE);
124                 checknames = NULL;
125                 result = cfg_map_get(maps[i], "check-names", &checknames);
126                 if (result != ISC_R_SUCCESS)
127                         continue;
128                 if (checknames != NULL && !cfg_obj_islist(checknames)) {
129                         *obj = checknames;
130                         return (ISC_TRUE);
131                 }
132                 for (element = cfg_list_first(checknames);
133                      element != NULL;
134                      element = cfg_list_next(element)) {
135                         value = cfg_listelt_value(element);
136                         type = cfg_tuple_get(value, "type");
137                         if (strcasecmp(cfg_obj_asstring(type), "master") != 0)
138                                 continue;
139                         *obj = cfg_tuple_get(value, "mode");
140                         return (ISC_TRUE);
141                 }
142         }
143 }
144
145 static isc_result_t
146 config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
147         int i;
148
149         for (i = 0;; i++) {
150                 if (maps[i] == NULL)
151                         return (ISC_R_NOTFOUND);
152                 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
153                         return (ISC_R_SUCCESS);
154         }
155 }
156
157 static isc_result_t
158 configure_hint(const char *zfile, const char *zclass, isc_mem_t *mctx) {
159         isc_result_t result;
160         dns_db_t *db = NULL;
161         dns_rdataclass_t rdclass;
162         isc_textregion_t r;
163
164         if (zfile == NULL)
165                 return (ISC_R_FAILURE);
166
167         DE_CONST(zclass, r.base);
168         r.length = strlen(zclass);
169         result = dns_rdataclass_fromtext(&rdclass, &r);
170         if (result != ISC_R_SUCCESS)
171                 return (result);
172
173         result = dns_rootns_create(mctx, rdclass, zfile, &db);
174         if (result != ISC_R_SUCCESS)
175                 return (result);
176
177         dns_db_detach(&db);
178         return (ISC_R_SUCCESS);
179 }
180
181 /*% configure the zone */
182 static isc_result_t
183 configure_zone(const char *vclass, const char *view,
184                const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
185                const cfg_obj_t *config, isc_mem_t *mctx)
186 {
187         int i = 0;
188         isc_result_t result;
189         const char *zclass;
190         const char *zname;
191         const char *zfile = NULL;
192         const cfg_obj_t *maps[4];
193         const cfg_obj_t *zoptions = NULL;
194         const cfg_obj_t *classobj = NULL;
195         const cfg_obj_t *typeobj = NULL;
196         const cfg_obj_t *fileobj = NULL;
197         const cfg_obj_t *dbobj = NULL;
198         const cfg_obj_t *obj = NULL;
199         const cfg_obj_t *fmtobj = NULL;
200         dns_masterformat_t masterformat;
201
202         zone_options = DNS_ZONEOPT_CHECKNS | DNS_ZONEOPT_MANYERRORS;
203
204         zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
205         classobj = cfg_tuple_get(zconfig, "class");
206         if (!cfg_obj_isstring(classobj))
207                 zclass = vclass;
208         else
209                 zclass = cfg_obj_asstring(classobj);
210
211         zoptions = cfg_tuple_get(zconfig, "options");
212         maps[i++] = zoptions;
213         if (vconfig != NULL)
214                 maps[i++] = cfg_tuple_get(vconfig, "options");
215         if (config != NULL) {
216                 cfg_map_get(config, "options", &obj);
217                 if (obj != NULL)
218                         maps[i++] = obj;
219         }
220         maps[i] = NULL;
221
222         cfg_map_get(zoptions, "type", &typeobj);
223         if (typeobj == NULL)
224                 return (ISC_R_FAILURE);
225
226         cfg_map_get(zoptions, "file", &fileobj);
227         if (fileobj != NULL)
228                 zfile = cfg_obj_asstring(fileobj);
229
230         /*
231          * Check hints files for hint zones.
232          * Skip loading checks for any type other than master.
233          */
234         if (strcasecmp(cfg_obj_asstring(typeobj), "hint") == 0)
235                 return (configure_hint(zfile, zclass, mctx));
236         else if ((strcasecmp(cfg_obj_asstring(typeobj), "master") != 0))
237                 return (ISC_R_SUCCESS);
238
239         if (zfile == NULL)
240                 return (ISC_R_FAILURE);
241
242         cfg_map_get(zoptions, "database", &dbobj);
243         if (dbobj != NULL)
244                 return (ISC_R_SUCCESS);
245
246         obj = NULL;
247         if (get_maps(maps, "check-dup-records", &obj)) {
248                 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
249                         zone_options |= DNS_ZONEOPT_CHECKDUPRR;
250                         zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
251                 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
252                         zone_options |= DNS_ZONEOPT_CHECKDUPRR;
253                         zone_options |= DNS_ZONEOPT_CHECKDUPRRFAIL;
254                 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
255                         zone_options &= ~DNS_ZONEOPT_CHECKDUPRR;
256                         zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
257                 } else
258                         INSIST(0);
259         } else {
260                 zone_options |= DNS_ZONEOPT_CHECKDUPRR;
261                 zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
262         }
263
264         obj = NULL;
265         if (get_maps(maps, "check-mx", &obj)) {
266                 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
267                         zone_options |= DNS_ZONEOPT_CHECKMX;
268                         zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
269                 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
270                         zone_options |= DNS_ZONEOPT_CHECKMX;
271                         zone_options |= DNS_ZONEOPT_CHECKMXFAIL;
272                 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
273                         zone_options &= ~DNS_ZONEOPT_CHECKMX;
274                         zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
275                 } else
276                         INSIST(0);
277         } else {
278                 zone_options |= DNS_ZONEOPT_CHECKMX;
279                 zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
280         }
281
282         obj = NULL;
283         if (get_maps(maps, "check-integrity", &obj)) {
284                 if (cfg_obj_asboolean(obj))
285                         zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
286                 else
287                         zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
288         } else
289                 zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
290
291         obj = NULL;
292         if (get_maps(maps, "check-mx-cname", &obj)) {
293                 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
294                         zone_options |= DNS_ZONEOPT_WARNMXCNAME;
295                         zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
296                 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
297                         zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
298                         zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
299                 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
300                         zone_options |= DNS_ZONEOPT_WARNMXCNAME;
301                         zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
302                 } else
303                         INSIST(0);
304         } else {
305                 zone_options |= DNS_ZONEOPT_WARNMXCNAME;
306                 zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
307         }
308
309         obj = NULL;
310         if (get_maps(maps, "check-srv-cname", &obj)) {
311                 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
312                         zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
313                         zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
314                 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
315                         zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
316                         zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
317                 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
318                         zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
319                         zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
320                 } else
321                         INSIST(0);
322         } else {
323                 zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
324                 zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
325         }
326
327         obj = NULL;
328         if (get_maps(maps, "check-sibling", &obj)) {
329                 if (cfg_obj_asboolean(obj))
330                         zone_options |= DNS_ZONEOPT_CHECKSIBLING;
331                 else
332                         zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
333         }
334
335         obj = NULL;
336         if (get_maps(maps, "check-spf", &obj)) {
337                 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
338                         zone_options |= DNS_ZONEOPT_CHECKSPF;
339                 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
340                         zone_options &= ~DNS_ZONEOPT_CHECKSPF;
341                 } else
342                         INSIST(0);
343         } else {
344                 zone_options |= DNS_ZONEOPT_CHECKSPF;
345         }
346
347         obj = NULL;
348         if (get_checknames(maps, &obj)) {
349                 if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
350                         zone_options |= DNS_ZONEOPT_CHECKNAMES;
351                         zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
352                 } else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
353                         zone_options |= DNS_ZONEOPT_CHECKNAMES;
354                         zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
355                 } else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
356                         zone_options &= ~DNS_ZONEOPT_CHECKNAMES;
357                         zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
358                 } else
359                         INSIST(0);
360         } else {
361                zone_options |= DNS_ZONEOPT_CHECKNAMES;
362                zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
363         }
364
365         masterformat = dns_masterformat_text;
366         fmtobj = NULL;
367         result = config_get(maps, "masterfile-format", &fmtobj);
368         if (result == ISC_R_SUCCESS) {
369                 const char *masterformatstr = cfg_obj_asstring(fmtobj);
370                 if (strcasecmp(masterformatstr, "text") == 0)
371                         masterformat = dns_masterformat_text;
372                 else if (strcasecmp(masterformatstr, "raw") == 0)
373                         masterformat = dns_masterformat_raw;
374                 else
375                         INSIST(0);
376         }
377
378         result = load_zone(mctx, zname, zfile, masterformat, zclass, NULL);
379         if (result != ISC_R_SUCCESS)
380                 fprintf(stderr, "%s/%s/%s: %s\n", view, zname, zclass,
381                         dns_result_totext(result));
382         return (result);
383 }
384
385 /*% configure a view */
386 static isc_result_t
387 configure_view(const char *vclass, const char *view, const cfg_obj_t *config,
388                const cfg_obj_t *vconfig, isc_mem_t *mctx)
389 {
390         const cfg_listelt_t *element;
391         const cfg_obj_t *voptions;
392         const cfg_obj_t *zonelist;
393         isc_result_t result = ISC_R_SUCCESS;
394         isc_result_t tresult;
395
396         voptions = NULL;
397         if (vconfig != NULL)
398                 voptions = cfg_tuple_get(vconfig, "options");
399
400         zonelist = NULL;
401         if (voptions != NULL)
402                 (void)cfg_map_get(voptions, "zone", &zonelist);
403         else
404                 (void)cfg_map_get(config, "zone", &zonelist);
405
406         for (element = cfg_list_first(zonelist);
407              element != NULL;
408              element = cfg_list_next(element))
409         {
410                 const cfg_obj_t *zconfig = cfg_listelt_value(element);
411                 tresult = configure_zone(vclass, view, zconfig, vconfig,
412                                          config, mctx);
413                 if (tresult != ISC_R_SUCCESS)
414                         result = tresult;
415         }
416         return (result);
417 }
418
419
420 /*% load zones from the configuration */
421 static isc_result_t
422 load_zones_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx) {
423         const cfg_listelt_t *element;
424         const cfg_obj_t *classobj;
425         const cfg_obj_t *views;
426         const cfg_obj_t *vconfig;
427         const char *vclass;
428         isc_result_t result = ISC_R_SUCCESS;
429         isc_result_t tresult;
430
431         views = NULL;
432
433         (void)cfg_map_get(config, "view", &views);
434         for (element = cfg_list_first(views);
435              element != NULL;
436              element = cfg_list_next(element))
437         {
438                 const char *vname;
439
440                 vclass = "IN";
441                 vconfig = cfg_listelt_value(element);
442                 if (vconfig != NULL) {
443                         classobj = cfg_tuple_get(vconfig, "class");
444                         if (cfg_obj_isstring(classobj))
445                                 vclass = cfg_obj_asstring(classobj);
446                 }
447                 vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
448                 tresult = configure_view(vclass, vname, config, vconfig, mctx);
449                 if (tresult != ISC_R_SUCCESS)
450                         result = tresult;
451         }
452
453         if (views == NULL) {
454                 tresult = configure_view("IN", "_default", config, NULL, mctx);
455                 if (tresult != ISC_R_SUCCESS)
456                         result = tresult;
457         }
458         return (result);
459 }
460
461 static void
462 output(void *closure, const char *text, int textlen) {
463         UNUSED(closure);
464         if (fwrite(text, 1, textlen, stdout) != (size_t)textlen) {
465                 perror("fwrite");
466                 exit(1);
467         }
468 }
469
470 /*% The main processing routine */
471 int
472 main(int argc, char **argv) {
473         int c;
474         cfg_parser_t *parser = NULL;
475         cfg_obj_t *config = NULL;
476         const char *conffile = NULL;
477         isc_mem_t *mctx = NULL;
478         isc_result_t result;
479         int exit_status = 0;
480         isc_entropy_t *ectx = NULL;
481         isc_boolean_t load_zones = ISC_FALSE;
482         isc_boolean_t print = ISC_FALSE;
483         unsigned int flags = 0;
484
485         isc_commandline_errprint = ISC_FALSE;
486
487         while ((c = isc_commandline_parse(argc, argv, "dhjt:pvxz")) != EOF) {
488                 switch (c) {
489                 case 'd':
490                         debug++;
491                         break;
492
493                 case 'j':
494                         nomerge = ISC_FALSE;
495                         break;
496
497                 case 't':
498                         result = isc_dir_chroot(isc_commandline_argument);
499                         if (result != ISC_R_SUCCESS) {
500                                 fprintf(stderr, "isc_dir_chroot: %s\n",
501                                         isc_result_totext(result));
502                                 exit(1);
503                         }
504                         break;
505
506                 case 'p':
507                         print = ISC_TRUE;
508                         break;
509
510                 case 'v':
511                         printf(VERSION "\n");
512                         exit(0);
513
514                 case 'x':
515                         flags |= CFG_PRINTER_XKEY;
516                         break;
517
518                 case 'z':
519                         load_zones = ISC_TRUE;
520                         docheckmx = ISC_FALSE;
521                         docheckns = ISC_FALSE;
522                         dochecksrv = ISC_FALSE;
523                         break;
524
525                 case '?':
526                         if (isc_commandline_option != '?')
527                                 fprintf(stderr, "%s: invalid argument -%c\n",
528                                         program, isc_commandline_option);
529                         /* FALLTHROUGH */
530                 case 'h':
531                         usage();
532
533                 default:
534                         fprintf(stderr, "%s: unhandled option -%c\n",
535                                 program, isc_commandline_option);
536                         exit(1);
537                 }
538         }
539
540         if (((flags & CFG_PRINTER_XKEY) != 0) && !print) {
541                 fprintf(stderr, "%s: -x cannot be used without -p\n", program);
542                 exit(1);
543         }
544
545         if (isc_commandline_index + 1 < argc)
546                 usage();
547         if (argv[isc_commandline_index] != NULL)
548                 conffile = argv[isc_commandline_index];
549         if (conffile == NULL || conffile[0] == '\0')
550                 conffile = NAMED_CONFFILE;
551
552 #ifdef _WIN32
553         InitSockets();
554 #endif
555
556         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
557
558         RUNTIME_CHECK(setup_logging(mctx, stdout, &logc) == ISC_R_SUCCESS);
559
560         RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
561         RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
562                       == ISC_R_SUCCESS);
563
564         dns_result_register();
565
566         RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
567
568         cfg_parser_setcallback(parser, directory_callback, NULL);
569
570         if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
571             ISC_R_SUCCESS)
572                 exit(1);
573
574         result = bind9_check_namedconf(config, logc, mctx);
575         if (result != ISC_R_SUCCESS)
576                 exit_status = 1;
577
578         if (result == ISC_R_SUCCESS && load_zones) {
579                 result = load_zones_fromconfig(config, mctx);
580                 if (result != ISC_R_SUCCESS)
581                         exit_status = 1;
582         }
583
584         if (print && exit_status == 0)
585                 cfg_printx(config, flags, output, NULL);
586         cfg_obj_destroy(parser, &config);
587
588         cfg_parser_destroy(&parser);
589
590         dns_name_destroy();
591
592         isc_log_destroy(&logc);
593
594         isc_hash_destroy();
595         isc_entropy_detach(&ectx);
596
597         isc_mem_destroy(&mctx);
598
599 #ifdef _WIN32
600         DestroySockets();
601 #endif
602
603         return (exit_status);
604 }