]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/smallapp/unbound-checkconf.c
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / smallapp / unbound-checkconf.c
1 /*
2  * checkconf/unbound-checkconf.c - config file checker for unbound.conf file.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /**
37  * \file
38  *
39  * The config checker checks for syntax and other errors in the unbound.conf
40  * file, and can be used to check for errors before the server is started
41  * or sigHUPped.
42  * Exit status 1 means an error.
43  */
44
45 #include "config.h"
46 #include "util/log.h"
47 #include "util/config_file.h"
48 #include "util/module.h"
49 #include "util/net_help.h"
50 #include "util/regional.h"
51 #include "iterator/iterator.h"
52 #include "iterator/iter_fwd.h"
53 #include "iterator/iter_hints.h"
54 #include "validator/validator.h"
55 #include "services/localzone.h"
56 #include "sldns/sbuffer.h"
57 #ifdef HAVE_GETOPT_H
58 #include <getopt.h>
59 #endif
60 #ifdef HAVE_PWD_H
61 #include <pwd.h>
62 #endif
63 #ifdef HAVE_SYS_STAT_H
64 #include <sys/stat.h>
65 #endif
66 #ifdef HAVE_GLOB_H
67 #include <glob.h>
68 #endif
69 #ifdef WITH_PYTHONMODULE
70 #include "pythonmod/pythonmod.h"
71 #endif
72
73 /** Give checkconf usage, and exit (1). */
74 static void
75 usage(void)
76 {
77         printf("Usage:  unbound-checkconf [file]\n");
78         printf("        Checks unbound configuration file for errors.\n");
79         printf("file    if omitted %s is used.\n", CONFIGFILE);
80         printf("-o option       print value of option to stdout.\n");
81         printf("-f              output full pathname with chroot applied, eg. with -o pidfile.\n");
82         printf("-h              show this usage help.\n");
83         printf("Version %s\n", PACKAGE_VERSION);
84         printf("BSD licensed, see LICENSE in source package for details.\n");
85         printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
86         exit(1);
87 }
88
89 /** 
90  * Print given option to stdout 
91  * @param cfg: config
92  * @param opt: option name without trailing :. 
93  *      This is different from config_set_option.
94  * @param final: if final pathname with chroot applied has to be printed.
95  */
96 static void
97 print_option(struct config_file* cfg, const char* opt, int final)
98 {
99         if(strcmp(opt, "pidfile") == 0 && final) {
100                 char *p = fname_after_chroot(cfg->pidfile, cfg, 1);
101                 if(!p) fatal_exit("out of memory");
102                 printf("%s\n", p);
103                 free(p);
104                 return;
105         }
106         if(!config_get_option(cfg, opt, config_print_func, stdout))
107                 fatal_exit("cannot print option '%s'", opt);
108 }
109
110 /** check if module works with config */
111 static void
112 check_mod(struct config_file* cfg, struct module_func_block* fb)
113 {
114         struct module_env env;
115         memset(&env, 0, sizeof(env));
116         env.cfg = cfg;
117         env.scratch = regional_create();
118         env.scratch_buffer = sldns_buffer_new(BUFSIZ);
119         if(!env.scratch || !env.scratch_buffer)
120                 fatal_exit("out of memory");
121         if(!edns_known_options_init(&env))
122                 fatal_exit("out of memory");
123         if(!(*fb->init)(&env, 0)) {
124                 fatal_exit("bad config for %s module", fb->name);
125         }
126         (*fb->deinit)(&env, 0);
127         sldns_buffer_free(env.scratch_buffer);
128         regional_destroy(env.scratch);
129         edns_known_options_delete(&env);
130 }
131
132 /** check localzones */
133 static void
134 localzonechecks(struct config_file* cfg)
135 {
136         struct local_zones* zs;
137         if(!(zs = local_zones_create()))
138                 fatal_exit("out of memory");
139         if(!local_zones_apply_cfg(zs, cfg))
140                 fatal_exit("failed local-zone, local-data configuration");
141         local_zones_delete(zs);
142 }
143
144 /** emit warnings for IP in hosts */
145 static void
146 warn_hosts(const char* typ, struct config_stub* list)
147 {
148         struct sockaddr_storage a;
149         socklen_t alen;
150         struct config_stub* s;
151         struct config_strlist* h;
152         for(s=list; s; s=s->next) {
153                 for(h=s->hosts; h; h=h->next) {
154                         if(extstrtoaddr(h->str, &a, &alen)) {
155                                 fprintf(stderr, "unbound-checkconf: warning:"
156                                   " %s %s: \"%s\" is an IP%s address, "
157                                   "and when looked up as a host name "
158                                   "during use may not resolve.\n", 
159                                   s->name, typ, h->str,
160                                   addr_is_ip6(&a, alen)?"6":"4");
161                         }
162                 }
163         }
164 }
165
166 /** check interface strings */
167 static void
168 interfacechecks(struct config_file* cfg)
169 {
170         int d;
171         struct sockaddr_storage a;
172         socklen_t alen;
173         int i, j;
174         for(i=0; i<cfg->num_ifs; i++) {
175                 if(!extstrtoaddr(cfg->ifs[i], &a, &alen)) {
176                         fatal_exit("cannot parse interface specified as '%s'",
177                                 cfg->ifs[i]);
178                 }
179                 for(j=0; j<cfg->num_ifs; j++) {
180                         if(i!=j && strcmp(cfg->ifs[i], cfg->ifs[j])==0)
181                                 fatal_exit("interface: %s present twice, "
182                                         "cannot bind same ports twice.",
183                                         cfg->ifs[i]);
184                 }
185         }
186         for(i=0; i<cfg->num_out_ifs; i++) {
187                 if(!ipstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen) &&
188                    !netblockstrtoaddr(cfg->out_ifs[i], UNBOUND_DNS_PORT, &a, &alen, &d)) {
189                         fatal_exit("cannot parse outgoing-interface "
190                                 "specified as '%s'", cfg->out_ifs[i]);
191                 }
192                 for(j=0; j<cfg->num_out_ifs; j++) {
193                         if(i!=j && strcmp(cfg->out_ifs[i], cfg->out_ifs[j])==0)
194                                 fatal_exit("outgoing-interface: %s present "
195                                         "twice, cannot bind same ports twice.",
196                                         cfg->out_ifs[i]);
197                 }
198         }
199 }
200
201 /** check acl ips */
202 static void
203 aclchecks(struct config_file* cfg)
204 {
205         int d;
206         struct sockaddr_storage a;
207         socklen_t alen;
208         struct config_str2list* acl;
209         for(acl=cfg->acls; acl; acl = acl->next) {
210                 if(!netblockstrtoaddr(acl->str, UNBOUND_DNS_PORT, &a, &alen, 
211                         &d)) {
212                         fatal_exit("cannot parse access control address %s %s",
213                                 acl->str, acl->str2);
214                 }
215         }
216 }
217
218 /** true if fname is a file */
219 static int
220 is_file(const char* fname) 
221 {
222         struct stat buf;
223         if(stat(fname, &buf) < 0) {
224                 if(errno==EACCES) {
225                         printf("warning: no search permission for one of the directories in path: %s\n", fname);
226                         return 1;
227                 }
228                 perror(fname);
229                 return 0;
230         }
231         if(S_ISDIR(buf.st_mode)) {
232                 printf("%s is not a file\n", fname);
233                 return 0;
234         }
235         return 1;
236 }
237
238 /** true if fname is a directory */
239 static int
240 is_dir(const char* fname) 
241 {
242         struct stat buf;
243         if(stat(fname, &buf) < 0) {
244                 if(errno==EACCES) {
245                         printf("warning: no search permission for one of the directories in path: %s\n", fname);
246                         return 1;
247                 }
248                 perror(fname);
249                 return 0;
250         }
251         if(!(S_ISDIR(buf.st_mode))) {
252                 printf("%s is not a directory\n", fname);
253                 return 0;
254         }
255         return 1;
256 }
257
258 /** get base dir of a fname */
259 static char*
260 basedir(char* fname)
261 {
262         char* rev;
263         if(!fname) fatal_exit("out of memory");
264         rev = strrchr(fname, '/');
265         if(!rev) return NULL;
266         if(fname == rev) return NULL;
267         rev[0] = 0;
268         return fname;
269 }
270
271 /** check chroot for a file string */
272 static void
273 check_chroot_string(const char* desc, char** ss,
274         const char* chrootdir, struct config_file* cfg)
275 {
276         char* str = *ss;
277         if(str && str[0]) {
278                 *ss = fname_after_chroot(str, cfg, 1);
279                 if(!*ss) fatal_exit("out of memory");
280                 if(!is_file(*ss)) {
281                         if(chrootdir && chrootdir[0])
282                                 fatal_exit("%s: \"%s\" does not exist in "
283                                         "chrootdir %s", desc, str, chrootdir);
284                         else
285                                 fatal_exit("%s: \"%s\" does not exist", 
286                                         desc, str);
287                 }
288                 /* put in a new full path for continued checking */
289                 free(str);
290         }
291 }
292
293 /** check file list, every file must be inside the chroot location */
294 static void
295 check_chroot_filelist(const char* desc, struct config_strlist* list,
296         const char* chrootdir, struct config_file* cfg)
297 {
298         struct config_strlist* p;
299         for(p=list; p; p=p->next) {
300                 check_chroot_string(desc, &p->str, chrootdir, cfg);
301         }
302 }
303
304 /** check file list, with wildcard processing */
305 static void
306 check_chroot_filelist_wild(const char* desc, struct config_strlist* list,
307         const char* chrootdir, struct config_file* cfg)
308 {
309         struct config_strlist* p;
310         for(p=list; p; p=p->next) {
311 #ifdef HAVE_GLOB
312                 if(strchr(p->str, '*') || strchr(p->str, '[') || 
313                         strchr(p->str, '?') || strchr(p->str, '{') || 
314                         strchr(p->str, '~')) {
315                         char* s = p->str;
316                         /* adjust whole pattern for chroot and check later */
317                         p->str = fname_after_chroot(p->str, cfg, 1);
318                         free(s);
319                 } else
320 #endif /* HAVE_GLOB */
321                         check_chroot_string(desc, &p->str, chrootdir, cfg);
322         }
323 }
324
325 /** check configuration for errors */
326 static void
327 morechecks(struct config_file* cfg, const char* fname)
328 {
329         warn_hosts("stub-host", cfg->stubs);
330         warn_hosts("forward-host", cfg->forwards);
331         interfacechecks(cfg);
332         aclchecks(cfg);
333
334         if(cfg->verbosity < 0)
335                 fatal_exit("verbosity value < 0");
336         if(cfg->num_threads <= 0 || cfg->num_threads > 10000)
337                 fatal_exit("num_threads value weird");
338         if(!cfg->do_ip4 && !cfg->do_ip6)
339                 fatal_exit("ip4 and ip6 are both disabled, pointless");
340         if(!cfg->do_ip6 && cfg->prefer_ip6)
341                 fatal_exit("cannot prefer and disable ip6, pointless");
342         if(!cfg->do_udp && !cfg->do_tcp)
343                 fatal_exit("udp and tcp are both disabled, pointless");
344         if(cfg->edns_buffer_size > cfg->msg_buffer_size)
345                 fatal_exit("edns-buffer-size larger than msg-buffer-size, "
346                         "answers will not fit in processing buffer");
347 #ifdef UB_ON_WINDOWS
348         w_config_adjust_directory(cfg);
349 #endif
350         if(cfg->chrootdir && cfg->chrootdir[0] && 
351                 cfg->chrootdir[strlen(cfg->chrootdir)-1] == '/')
352                 fatal_exit("chootdir %s has trailing slash '/' please remove.",
353                         cfg->chrootdir);
354         if(cfg->chrootdir && cfg->chrootdir[0] && 
355                 !is_dir(cfg->chrootdir)) {
356                 fatal_exit("bad chroot directory");
357         }
358         if(cfg->chrootdir && cfg->chrootdir[0]) {
359                 char buf[10240];
360                 buf[0] = 0;
361                 if(fname[0] != '/') {
362                         if(getcwd(buf, sizeof(buf)) == NULL)
363                                 fatal_exit("getcwd: %s", strerror(errno));
364                         (void)strlcat(buf, "/", sizeof(buf));
365                 }
366                 (void)strlcat(buf, fname, sizeof(buf));
367                 if(strncmp(buf, cfg->chrootdir, strlen(cfg->chrootdir)) != 0)
368                         fatal_exit("config file %s is not inside chroot %s",
369                                 buf, cfg->chrootdir);
370         }
371         if(cfg->directory && cfg->directory[0]) {
372                 char* ad = fname_after_chroot(cfg->directory, cfg, 0);
373                 if(!ad) fatal_exit("out of memory");
374                 if(!is_dir(ad)) fatal_exit("bad chdir directory");
375                 free(ad);
376         }
377         if( (cfg->chrootdir && cfg->chrootdir[0]) ||
378             (cfg->directory && cfg->directory[0])) {
379                 if(cfg->pidfile && cfg->pidfile[0]) {
380                         char* ad = (cfg->pidfile[0]=='/')?strdup(cfg->pidfile):
381                                 fname_after_chroot(cfg->pidfile, cfg, 1);
382                         char* bd = basedir(ad);
383                         if(bd && !is_dir(bd))
384                                 fatal_exit("pidfile directory does not exist");
385                         free(ad);
386                 }
387                 if(cfg->logfile && cfg->logfile[0]) {
388                         char* ad = fname_after_chroot(cfg->logfile, cfg, 1);
389                         char* bd = basedir(ad);
390                         if(bd && !is_dir(bd))
391                                 fatal_exit("logfile directory does not exist");
392                         free(ad);
393                 }
394         }
395
396         check_chroot_filelist("file with root-hints", 
397                 cfg->root_hints, cfg->chrootdir, cfg);
398         check_chroot_filelist("trust-anchor-file", 
399                 cfg->trust_anchor_file_list, cfg->chrootdir, cfg);
400         check_chroot_filelist("auto-trust-anchor-file", 
401                 cfg->auto_trust_anchor_file_list, cfg->chrootdir, cfg);
402         check_chroot_filelist_wild("trusted-keys-file", 
403                 cfg->trusted_keys_file_list, cfg->chrootdir, cfg);
404         check_chroot_string("dlv-anchor-file", &cfg->dlv_anchor_file, 
405                 cfg->chrootdir, cfg);
406         /* remove chroot setting so that modules are not stripping pathnames*/
407         free(cfg->chrootdir);
408         cfg->chrootdir = NULL;
409         
410         if(strcmp(cfg->module_conf, "iterator") != 0 
411                 && strcmp(cfg->module_conf, "validator iterator") != 0
412                 && strcmp(cfg->module_conf, "dns64 validator iterator") != 0
413                 && strcmp(cfg->module_conf, "dns64 iterator") != 0
414 #ifdef WITH_PYTHONMODULE
415                 && strcmp(cfg->module_conf, "python iterator") != 0 
416                 && strcmp(cfg->module_conf, "python validator iterator") != 0 
417                 && strcmp(cfg->module_conf, "validator python iterator") != 0
418                 && strcmp(cfg->module_conf, "dns64 python iterator") != 0 
419                 && strcmp(cfg->module_conf, "dns64 python validator iterator") != 0 
420                 && strcmp(cfg->module_conf, "dns64 validator python iterator") != 0
421                 && strcmp(cfg->module_conf, "python dns64 iterator") != 0 
422                 && strcmp(cfg->module_conf, "python dns64 validator iterator") != 0 
423 #endif
424 #ifdef USE_CACHEDB
425                 && strcmp(cfg->module_conf, "validator cachedb iterator") != 0
426                 && strcmp(cfg->module_conf, "cachedb iterator") != 0
427                 && strcmp(cfg->module_conf, "dns64 validator cachedb iterator") != 0
428                 && strcmp(cfg->module_conf, "dns64 cachedb iterator") != 0
429                 && strcmp(cfg->module_conf, "python dns64 cachedb iterator") != 0
430                 && strcmp(cfg->module_conf, "python dns64 validator cachedb iterator") != 0
431                 && strcmp(cfg->module_conf, "dns64 python cachedb iterator") != 0
432                 && strcmp(cfg->module_conf, "dns64 python validator cachedb iterator") != 0
433                 && strcmp(cfg->module_conf, "python cachedb iterator") != 0
434                 && strcmp(cfg->module_conf, "python validator cachedb iterator") != 0
435                 && strcmp(cfg->module_conf, "cachedb python iterator") != 0
436                 && strcmp(cfg->module_conf, "validator cachedb python iterator") != 0
437                 && strcmp(cfg->module_conf, "validator python cachedb iterator") != 0
438 #endif
439                 ) {
440                 fatal_exit("module conf '%s' is not known to work",
441                         cfg->module_conf);
442         }
443
444 #ifdef HAVE_GETPWNAM
445         if(cfg->username && cfg->username[0]) {
446                 if(getpwnam(cfg->username) == NULL)
447                         fatal_exit("user '%s' does not exist.", cfg->username);
448 #  ifdef HAVE_ENDPWENT
449                 endpwent();
450 #  endif
451         }
452 #endif
453         if(cfg->remote_control_enable && cfg->remote_control_use_cert) {
454                 check_chroot_string("server-key-file", &cfg->server_key_file,
455                         cfg->chrootdir, cfg);
456                 check_chroot_string("server-cert-file", &cfg->server_cert_file,
457                         cfg->chrootdir, cfg);
458                 if(!is_file(cfg->control_key_file))
459                         fatal_exit("control-key-file: \"%s\" does not exist",
460                                 cfg->control_key_file);
461                 if(!is_file(cfg->control_cert_file))
462                         fatal_exit("control-cert-file: \"%s\" does not exist",
463                                 cfg->control_cert_file);
464         }
465
466         localzonechecks(cfg);
467 }
468
469 /** check forwards */
470 static void
471 check_fwd(struct config_file* cfg)
472 {
473         struct iter_forwards* fwd = forwards_create();
474         if(!fwd || !forwards_apply_cfg(fwd, cfg)) {
475                 fatal_exit("Could not set forward zones");
476         }
477         forwards_delete(fwd);
478 }
479
480 /** check hints */
481 static void
482 check_hints(struct config_file* cfg)
483 {
484         struct iter_hints* hints = hints_create();
485         if(!hints || !hints_apply_cfg(hints, cfg)) {
486                 fatal_exit("Could not set root or stub hints");
487         }
488         hints_delete(hints);
489 }
490
491 /** check config file */
492 static void
493 checkconf(const char* cfgfile, const char* opt, int final)
494 {
495         char oldwd[4096];
496         struct config_file* cfg = config_create();
497         if(!cfg)
498                 fatal_exit("out of memory");
499         oldwd[0] = 0;
500         if(!getcwd(oldwd, sizeof(oldwd))) {
501                 log_err("cannot getcwd: %s", strerror(errno));
502                 oldwd[0] = 0;
503         }
504         if(!config_read(cfg, cfgfile, NULL)) {
505                 /* config_read prints messages to stderr */
506                 config_delete(cfg);
507                 exit(1);
508         }
509         if(oldwd[0] && chdir(oldwd) == -1)
510                 log_err("cannot chdir(%s): %s", oldwd, strerror(errno));
511         if(opt) {
512                 print_option(cfg, opt, final);
513                 config_delete(cfg);
514                 return;
515         }
516         morechecks(cfg, cfgfile);
517         check_mod(cfg, iter_get_funcblock());
518         check_mod(cfg, val_get_funcblock());
519 #ifdef WITH_PYTHONMODULE
520         if(strstr(cfg->module_conf, "python"))
521                 check_mod(cfg, pythonmod_get_funcblock());
522 #endif
523         check_fwd(cfg);
524         check_hints(cfg);
525         printf("unbound-checkconf: no errors in %s\n", cfgfile);
526         config_delete(cfg);
527 }
528
529 /** getopt global, in case header files fail to declare it. */
530 extern int optind;
531 /** getopt global, in case header files fail to declare it. */
532 extern char* optarg;
533
534 /** Main routine for checkconf */
535 int main(int argc, char* argv[])
536 {
537         int c;
538         int final = 0;
539         const char* f;
540         const char* opt = NULL;
541         const char* cfgfile = CONFIGFILE;
542         log_ident_set("unbound-checkconf");
543         log_init(NULL, 0, NULL);
544         checklock_start();
545 #ifdef USE_WINSOCK
546         /* use registry config file in preference to compiletime location */
547         if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
548                 cfgfile = CONFIGFILE;
549 #endif /* USE_WINSOCK */
550         /* parse the options */
551         while( (c=getopt(argc, argv, "fho:")) != -1) {
552                 switch(c) {
553                 case 'f':
554                         final = 1;
555                         break;
556                 case 'o':
557                         opt = optarg;
558                         break;
559                 case '?':
560                 case 'h':
561                 default:
562                         usage();
563                 }
564         }
565         argc -= optind;
566         argv += optind;
567         if(argc != 0 && argc != 1)
568                 usage();
569         if(argc == 1)
570                 f = argv[0];
571         else    f = cfgfile;
572         checkconf(f, opt, final);
573         checklock_stop();
574         return 0;
575 }