1 /* $NetBSD: zkt-conf.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
5 ** @(#) zkt-conf.c (c) Jan 2005 / Jan 2010 Holger Zuleger hznet.de
7 ** A config file utility for the DNSSEC Zone Key Tool
9 ** Copyright (c) 2005 - 2008, Holger Zuleger HZnet. All rights reserved.
11 ** This software is open source.
13 ** Redistribution and use in source and binary forms, with or without
14 ** modification, are permitted provided that the following conditions
17 ** Redistributions of source code must retain the above copyright notice,
18 ** this list of conditions and the following disclaimer.
20 ** Redistributions in binary form must reproduce the above copyright notice,
21 ** this list of conditions and the following disclaimer in the documentation
22 ** and/or other materials provided with the distribution.
24 ** Neither the name of Holger Zuleger HZnet nor the names of its contributors may
25 ** be used to endorse or promote products derived from this software without
26 ** specific prior written permission.
28 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
32 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 ** POSSIBILITY OF SUCH DAMAGE.
40 *****************************************************************/
43 # include <stdlib.h> /* abort(), exit(), ... */
54 # include "config_zkt.h"
55 #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG
70 static const char *view
= "";
71 static int writeflag
= 0;
72 static int allflag
= 0;
73 static int testflag
= 0;
75 # define short_options ":aC:c:O:dlstvwV:rh"
76 #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG
77 static struct option long_options
[] = {
78 {"compability", required_argument
, NULL
, 'C'},
79 {"config", required_argument
, NULL
, 'c'},
80 {"option", required_argument
, NULL
, 'O'},
81 {"config-option", required_argument
, NULL
, 'O'},
82 {"default", no_argument
, NULL
, 'd'},
83 {"sidecfg", no_argument
, NULL
, 's'},
84 {"localcfg", no_argument
, NULL
, 'l'},
85 {"all-values", no_argument
, NULL
, 'a'},
86 {"test", no_argument
, NULL
, 't'},
87 {"overwrite", no_argument
, NULL
, 'w'},
88 {"version", no_argument
, NULL
, 'v' },
89 {"write", no_argument
, NULL
, 'w'},
90 {"view", required_argument
, NULL
, 'V' },
91 {"help", no_argument
, NULL
, 'h'},
96 static void usage (char *mesg
);
99 int main (int argc
, char *argv
[])
107 const char *defconfname
= NULL
;
108 const char *confname
= NULL
;
111 zconf_t
*refconfig
= NULL
;
115 if ( (p
= strrchr (progname
, '/')) )
117 view
= getnameappendix (progname
, "zkt-conf");
119 defconfname
= getdefconfname (view
);
120 dbg_val0 ("Load built in config\n");
121 config
= loadconfig ("", (zconf_t
*)NULL
); /* load built in config */
123 if ( fileexist (defconfname
) ) /* load default config file */
125 dbg_val ("Load site wide config file \"%s\"\n", defconfname
);
126 config
= loadconfig (defconfname
, config
);
128 if ( config
== NULL
)
129 fatal ("Out of memory\n");
130 confname
= defconfname
;
135 /* set current config version based on ZKT version */
136 switch ( sscanf (ZKT_VERSION
, "%d.%d.%d", &major
, &minor
, &revision
) )
138 case 3: major
= (major
* 100) + (minor
* 10) + revision
; break;
139 case 2: major
= (major
* 100) + (minor
* 10); break;
140 case 1: major
= major
* 100; break;
142 usage ("illegal release number");
144 setconfigversion (major
);
146 #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG
147 while ( (c
= getopt_long (argc
, argv
, short_options
, long_options
, &opt_index
)) != -1 )
149 while ( (c
= getopt (argc
, argv
, short_options
)) != -1 )
154 case 'V': /* view name */
156 defconfname
= getdefconfname (view
);
157 if ( fileexist (defconfname
) ) /* load default config file */
158 config
= loadconfig (defconfname
, config
);
159 if ( config
== NULL
)
160 fatal ("Out of memory\n");
161 confname
= defconfname
;
163 case 'O': /* read option from commandline */
164 config
= loadconfig_fromstr (optarg
, config
);
167 switch ( sscanf (optarg
, "%d.%d.%d", &major
, &minor
, &revision
) )
169 case 3: major
= (major
* 100) + (minor
* 10) + revision
; break;
170 case 2: major
= (major
* 100) + (minor
* 10); break;
171 case 1: major
= major
* 100; break;
173 usage ("illegal release number");
175 setconfigversion (major
);
178 if ( *optarg
== '\0' )
179 usage ("empty config file name");
180 config
= loadconfig (optarg
, config
);
181 if ( *optarg
== '-' || strcmp (optarg
, "stdin") == 0 )
186 case 'd': /* built-in default config */
187 config
= loadconfig ("", config
); /* load built-in config */
188 confname
= defconfname
;
190 case 's': /* side wide config */
191 /* this is the default **/
193 case 'a': /* set all flag */
196 case 'l': /* local config file */
197 refconfig
= dupconfig (config
); /* duplicate current config */
198 confname
= LOCALCONF_FILE
;
199 if ( fileexist (LOCALCONF_FILE
) ) /* try to load local config file */
201 dbg_val ("Load local config file \"%s\"\n", LOCALCONF_FILE
);
202 config
= loadconfig (LOCALCONF_FILE
, config
);
204 else if ( !writeflag
)
205 usage ("error: no local config file found");
207 case 't': /* test config */
210 case 'v': /* version */
211 fprintf (stderr
, "%s version %s compiled for BIND version %d\n",
212 progname
, ZKT_VERSION
, BIND_VERSION
);
213 fprintf (stderr
, "ZKT %s\n", ZKT_COPYRIGHT
);
216 case 'w': /* write back conf file */
219 case 'h': /* print help */
223 snprintf (str
, sizeof(str
), "option \"-%c\" requires an argument.",
228 if ( isprint (optopt
) )
229 snprintf (str
, sizeof(str
), "Unknown option \"-%c\".",
232 snprintf (str
, sizeof (str
), "Unknown option char \\x%x.",
242 if ( c
>= argc
) /* no arguments given on commandline */
246 if ( checkconfig (config
) )
247 fprintf (stderr
, "All config file parameter seems to be ok\n");
251 if ( !writeflag
) /* print to stdout */
254 if ( refconfig
) /* have we seen a local config file ? */
256 printconfig (confname
, config
);
258 printconfigdiff (confname
, refconfig
, config
);
260 printconfig (confname
, config
);
263 else /* command line argument found: use it as name of zone file */
265 char includefiles
[1023+1]; /* list of include files */
266 size_t filelistsize
; /* size of list */
274 dnskeydb
= config
->keyfile
;
278 includefiles
[0] = '\0';
279 filelistsize
= sizeof (includefiles
);
280 keydbfound
= parsezonefile (file
, &minttl
, &maxttl
, dnskeydb
, includefiles
, &filelistsize
);
281 if ( keydbfound
< 0 )
282 error ("can't parse zone file %s\n", file
);
284 if ( dnskeydb
&& !keydbfound
)
288 addkeydb (file
, dnskeydb
);
289 printf ("\"$INCLUDE %s\" directive added to \"%s\"\n", dnskeydb
, file
);
292 printf ("\"$INCLUDE %s\" should be added to \"%s\" (run with option -w)\n",
296 if ( isflistdelim (*includefiles
) )
298 printf ("InclFiles:\t\"%s\"\n", includefiles
+1);
301 if ( minttl
< (10 * MINSEC
) )
302 fprintf (stderr
, "MinimumTTL of %s (%ld seconds) is too low to use it in a signed zone (see RFC4641)\n",
303 timeint2str (minttl
), minttl
);
305 fprintf (stderr
, "MinimumTTL:\t%s\t# (%ld seconds)\n", timeint2str (minttl
), minttl
);
306 fprintf (stdout
, "MaximumTTL:\t%s\t# (%ld seconds)\n", timeint2str (maxttl
), maxttl
);
310 refconfig
= dupconfig (config
); /* duplicate current config */
311 confname
= LOCALCONF_FILE
;
312 if ( fileexist (LOCALCONF_FILE
) ) /* try to load local config file */
314 dbg_val ("Load local config file \"%s\"\n", LOCALCONF_FILE
);
315 config
= loadconfig (LOCALCONF_FILE
, config
);
317 setconfigpar (config
, "MaximumTTL", &maxttl
);
318 printconfigdiff (confname
, refconfig
, config
);
326 # define sopt_usage(mesg, value) fprintf (stderr, mesg, value)
327 #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG
328 # define lopt_usage(mesg, value) fprintf (stderr, mesg, value)
329 # define loptstr(lstr, sstr) lstr
331 # define lopt_usage(mesg, value)
332 # define loptstr(lstr, sstr) sstr
334 static void usage (char *mesg
)
336 fprintf (stderr
, "%s version %s\n", progname
, ZKT_VERSION
);
338 fprintf (stderr
, "%s\n", mesg
);
339 fprintf (stderr
, "\n");
340 fprintf (stderr
, "usage: %s -h\n", progname
);
341 fprintf (stderr
, "usage: %s [-V view] [-w|-t] -d [-O <optstr>]\n", progname
);
342 fprintf (stderr
, "usage: %s [-V view] [-w|-t] [-s] [-c config] [-O <optstr>]\n", progname
);
343 fprintf (stderr
, "usage: %s [-V view] [-w|-t] [-a] -l [-c config] [-O <optstr>]\n", progname
);
344 fprintf (stderr
, "\n");
345 fprintf (stderr
, "usage: %s [-c config] [-w] <zonefile>\n", progname
);
346 fprintf (stderr
, "\n");
347 fprintf (stderr
, " -V name%s", loptstr (", --view=name\n", ""));
348 fprintf (stderr
, "\t\t specify the view name \n");
349 fprintf (stderr
, " -d%s\tprint built-in default config parameter\n", loptstr (", --default", ""));
350 fprintf (stderr
, " -s%s\tprint site wide config file parameter (this is the default)\n", loptstr (", --sitecfg", ""));
351 fprintf (stderr
, " -l%s\tprint local config file parameter\n", loptstr (", --localcfg", ""));
352 fprintf (stderr
, " -a%s\tprint all parameter not only the different one\n", loptstr (", --all", ""));
353 fprintf (stderr
, " -c file%s", loptstr (", --config=file\n", ""));
354 fprintf (stderr
, " \t\tread config from <file> instead of %s\n", CONFIG_FILE
);
355 fprintf (stderr
, " -O optstr%s", loptstr (", --config-option=\"optstr\"\n", ""));
356 fprintf (stderr
, " \t\tread config options from commandline\n");
357 fprintf (stderr
, " -t%s\ttest the config parameter if they are useful \n", loptstr (", --test", "\t"));
358 fprintf (stderr
, " -w%s\twrite or rewrite config file \n", loptstr (", --write", "\t"));
359 fprintf (stderr
, " -h%s\tprint this help \n", loptstr (", --help", "\t"));