- Fix indentation in VarFind()
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / check / named-checkconf.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  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-checkconf.c,v 1.12.2.1 2004/03/09 06:09:09 marka Exp $ */
19
20 #include <config.h>
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include <isc/commandline.h>
27 #include <isc/dir.h>
28 #include <isc/log.h>
29 #include <isc/mem.h>
30 #include <isc/result.h>
31 #include <isc/string.h>
32 #include <isc/util.h>
33
34 #include <isccfg/cfg.h>
35 #include <isccfg/check.h>
36
37 #include "check-tool.h"
38
39 isc_log_t *logc = NULL;
40
41 static void
42 usage(void) {
43         fprintf(stderr, "usage: named-checkconf [-v] [-t directory] [named.conf]\n");
44         exit(1);
45 }
46
47 static isc_result_t
48 directory_callback(const char *clausename, cfg_obj_t *obj, void *arg) {
49         isc_result_t result;
50         char *directory;
51
52         REQUIRE(strcasecmp("directory", clausename) == 0);
53
54         UNUSED(arg);
55         UNUSED(clausename);
56
57         /*
58          * Change directory.
59          */
60         directory = cfg_obj_asstring(obj);
61         result = isc_dir_chdir(directory);
62         if (result != ISC_R_SUCCESS) {
63                 cfg_obj_log(obj, logc, ISC_LOG_ERROR,
64                             "change directory to '%s' failed: %s",
65                             directory, isc_result_totext(result));
66                 return (result);
67         }
68
69         return (ISC_R_SUCCESS);
70 }
71
72 int
73 main(int argc, char **argv) {
74         int c;
75         cfg_parser_t *parser = NULL;
76         cfg_obj_t *config = NULL;
77         const char *conffile = NULL;
78         isc_mem_t *mctx = NULL;
79         isc_result_t result;
80         int exit_status = 0;
81
82         while ((c = isc_commandline_parse(argc, argv, "t:v")) != EOF) {
83                 switch (c) {
84                 case 't':
85                         result = isc_dir_chroot(isc_commandline_argument);
86                         if (result != ISC_R_SUCCESS) {
87                                 fprintf(stderr, "isc_dir_chroot: %s\n",
88                                         isc_result_totext(result));
89                                 exit(1);
90                         }
91                         result = isc_dir_chdir("/");
92                         if (result != ISC_R_SUCCESS) {
93                                 fprintf(stderr, "isc_dir_chdir: %s\n",
94                                         isc_result_totext(result));
95                                 exit(1);
96                         }
97                         break;
98
99                 case 'v':
100                         printf(VERSION "\n");
101                         exit(0);
102
103                 default:
104                         usage();
105                 }
106         }
107
108         if (argv[isc_commandline_index] != NULL)
109                 conffile = argv[isc_commandline_index];
110         if (conffile == NULL || conffile[0] == '\0')
111                 conffile = NAMED_CONFFILE;
112
113         RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
114
115         RUNTIME_CHECK(setup_logging(mctx, &logc) == ISC_R_SUCCESS);
116
117         RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
118
119         cfg_parser_setcallback(parser, directory_callback, NULL);
120
121         if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
122             ISC_R_SUCCESS)
123                 exit(1);
124
125         result = cfg_check_namedconf(config, logc, mctx);
126         if (result != ISC_R_SUCCESS)
127                 exit_status = 1;
128
129         cfg_obj_destroy(parser, &config);
130
131         cfg_parser_destroy(&parser);
132
133         isc_log_destroy(&logc);
134
135         isc_mem_destroy(&mctx);
136
137         return (exit_status);
138 }