No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / contrib / zkt / zconf.c
blob086f0c4c022b85e95c0b094be10dec8395b07d0d
1 /* $NetBSD$ */
3 /****************************************************************
4 **
5 ** @(#) zconf.c -- configuration file parser for dnssec.conf
6 **
7 ** Most of the code is from the SixXS Heartbeat Client
8 ** written by Jeroen Massar <jeroen@sixxs.net>
9 **
10 ** New config types and some slightly code changes by Holger Zuleger
12 ** Copyright (c) Aug 2005, Jeroen Massar, Holger Zuleger.
13 ** All rights reserved.
14 **
15 ** This software is open source.
16 **
17 ** Redistribution and use in source and binary forms, with or without
18 ** modification, are permitted provided that the following conditions
19 ** are met:
20 **
21 ** Redistributions of source code must retain the above copyright notice,
22 ** this list of conditions and the following disclaimer.
23 **
24 ** Redistributions in binary form must reproduce the above copyright notice,
25 ** this list of conditions and the following disclaimer in the documentation
26 ** and/or other materials provided with the distribution.
27 **
28 ** Neither the name of Jeroen Masar or Holger Zuleger nor the
29 ** names of its contributors may be used to endorse or promote products
30 ** derived from this software without specific prior written permission.
31 **
32 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
36 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 ** POSSIBILITY OF SUCH DAMAGE.
44 ****************************************************************/
45 # include <sys/types.h>
46 # include <stdio.h>
47 # include <errno.h>
48 # include <unistd.h>
49 # include <stdlib.h>
50 # include <stdarg.h>
51 # include <string.h>
52 # include <strings.h>
53 # include <assert.h>
54 # include <ctype.h>
56 #ifdef HAVE_CONFIG_H
57 # include "config.h"
58 #endif
59 # include "config_zkt.h"
60 # include "debug.h"
61 # include "misc.h"
62 #define extern
63 # include "zconf.h"
64 #undef extern
65 # include "dki.h"
67 # define ISTRUE(val) (strcasecmp (val, "yes") == 0 || \
68 strcasecmp (val, "true") == 0 )
69 # define ISCOMMENT(cp) (*(cp) == '#' || *(cp) == ';' || \
70 (*(cp) == '/' && *((cp)+1) == '/') )
71 # define ISDELIM(c) ( isspace (c) || (c) == ':' || (c) == '=' )
74 typedef enum {
75 CONF_END = 0,
76 CONF_STRING,
77 CONF_INT,
78 CONF_TIMEINT,
79 CONF_BOOL,
80 CONF_ALGO,
81 CONF_SERIAL,
82 CONF_FACILITY,
83 CONF_LEVEL,
84 CONF_COMMENT,
85 } ctype_t;
87 /*****************************************************************
88 ** private (static) variables
89 *****************************************************************/
90 static zconf_t def = {
91 ZONEDIR, RECURSIVE,
92 PRINTTIME, PRINTAGE, LJUST,
93 SIG_VALIDITY, MAX_TTL, KEY_TTL, PROPTIME, Incremental,
94 RESIGN_INT,
95 KEY_ALGO, ADDITIONAL_KEY_ALGO,
96 KSK_LIFETIME, KSK_BITS, KSK_RANDOM,
97 ZSK_LIFETIME, ZSK_BITS, ZSK_RANDOM,
98 SALTLEN,
99 NULL, /* viewname cmdline parameter */
100 0, /* noexec cmdline parameter */
101 LOGFILE, LOGLEVEL, SYSLOGFACILITY, SYSLOGLEVEL, VERBOSELOG, 0,
102 DNSKEYFILE, ZONEFILE, KEYSETDIR,
103 LOOKASIDEDOMAIN,
104 SIG_RANDOM, SIG_PSEUDO, SIG_GENDS, SIG_PARAM,
105 DIST_CMD, /* defaults to NULL which means to run "rndc reload" */
106 NAMED_CHROOT
109 typedef struct {
110 char *label; /* the name of the paramter */
111 int cmdline; /* is this a command line parameter ? */
112 ctype_t type; /* the parameter type */
113 void *var; /* pointer to the parameter variable */
114 } zconf_para_t;
116 static zconf_para_t confpara[] = {
117 { "", 0, CONF_COMMENT, ""},
118 { "", 0, CONF_COMMENT, "\t@(#) dnssec.conf " ZKT_VERSION },
119 { "", 0, CONF_COMMENT, ""},
120 { "", 0, CONF_COMMENT, NULL },
122 { "", 0, CONF_COMMENT, "dnssec-zkt options" },
123 { "Zonedir", 0, CONF_STRING, &def.zonedir },
124 { "Recursive", 0, CONF_BOOL, &def.recursive },
125 { "PrintTime", 0, CONF_BOOL, &def.printtime },
126 { "PrintAge", 0, CONF_BOOL, &def.printage },
127 { "LeftJustify", 0, CONF_BOOL, &def.ljust },
129 { "", 0, CONF_COMMENT, NULL },
130 { "", 0, CONF_COMMENT, "zone specific values" },
131 { "ResignInterval", 0, CONF_TIMEINT, &def.resign },
132 { "Sigvalidity", 0, CONF_TIMEINT, &def.sigvalidity },
133 { "Max_TTL", 0, CONF_TIMEINT, &def.max_ttl },
134 { "Propagation", 0, CONF_TIMEINT, &def.proptime },
135 { "KEY_TTL", 0, CONF_TIMEINT, &def.key_ttl },
136 #if defined (DEF_TTL)
137 { "def_ttl", 0, CONF_TIMEINT, &def.def_ttl },
138 #endif
139 { "Serialformat", 0, CONF_SERIAL, &def.serialform },
141 { "", 0, CONF_COMMENT, NULL },
142 { "", 0, CONF_COMMENT, "signing key parameters"},
143 { "Key_algo", 0, CONF_ALGO, &def.k_algo }, /* now used as general KEY algoritjm (KSK & ZSK) */
144 { "AddKey_algo", 0, CONF_ALGO, &def.k2_algo }, /* second key algorithm added (v0.99) */
145 { "KSK_lifetime", 0, CONF_TIMEINT, &def.k_life },
146 { "KSK_algo", 1, CONF_ALGO, &def.k_algo }, /* old KSK value changed to key algorithm */
147 { "KSK_bits", 0, CONF_INT, &def.k_bits },
148 { "KSK_randfile", 0, CONF_STRING, &def.k_random },
149 { "ZSK_lifetime", 0, CONF_TIMEINT, &def.z_life },
150 /* { "ZSK_algo", 1, CONF_ALGO, &def.z_algo }, ZSK algo removed (set to same as ksk) */
151 { "ZSK_algo", 1, CONF_ALGO, &def.k2_algo }, /* if someone using it already, map the algo to the additional key algorithm */
152 { "ZSK_bits", 0, CONF_INT, &def.z_bits },
153 { "ZSK_randfile", 0, CONF_STRING, &def.z_random },
154 { "SaltBits", 0, CONF_INT, &def.saltbits },
156 { "", 0, CONF_COMMENT, NULL },
157 { "", 0, CONF_COMMENT, "dnssec-signer options"},
158 { "--view", 1, CONF_STRING, &def.view },
159 { "--noexec", 1, CONF_BOOL, &def.noexec },
160 { "LogFile", 0, CONF_STRING, &def.logfile },
161 { "LogLevel", 0, CONF_LEVEL, &def.loglevel },
162 { "SyslogFacility", 0, CONF_FACILITY, &def.syslogfacility },
163 { "SyslogLevel", 0, CONF_LEVEL, &def.sysloglevel },
164 { "VerboseLog", 0, CONF_INT, &def.verboselog },
165 { "-v", 1, CONF_INT, &def.verbosity },
166 { "Keyfile", 0, CONF_STRING, &def.keyfile },
167 { "Zonefile", 0, CONF_STRING, &def.zonefile },
168 { "KeySetDir", 0, CONF_STRING, &def.keysetdir },
169 { "DLV_Domain", 0, CONF_STRING, &def.lookaside },
170 { "Sig_Randfile", 0, CONF_STRING, &def.sig_random },
171 { "Sig_Pseudorand", 0, CONF_BOOL, &def.sig_pseudo },
172 { "Sig_GenerateDS", 0, CONF_BOOL, &def.sig_gends },
173 { "Sig_Parameter", 0, CONF_STRING, &def.sig_param },
174 { "Distribute_Cmd", 0, CONF_STRING, &def.dist_cmd },
175 { "NamedChrootDir", 0, CONF_STRING, &def.chroot_dir },
177 { NULL, 0, CONF_END, NULL},
180 /*****************************************************************
181 ** private (static) function deklaration and definition
182 *****************************************************************/
183 static const char *bool2str (int val)
185 return val ? "True" : "False";
188 static const char *timeint2str (ulong val)
190 static char str[20+1];
192 if ( val == 0 )
193 snprintf (str, sizeof (str), "%lu", val / YEARSEC);
194 else if ( val % YEARSEC == 0 )
195 snprintf (str, sizeof (str), "%luy", val / YEARSEC);
196 else if ( val % WEEKSEC == 0 )
197 snprintf (str, sizeof (str), "%luw", val / WEEKSEC);
198 else if ( val % DAYSEC == 0 )
199 snprintf (str, sizeof (str), "%lud", val / DAYSEC);
200 else if ( val % HOURSEC == 0 )
201 snprintf (str, sizeof (str), "%luh", val / HOURSEC);
202 else if ( val % MINSEC == 0 )
203 snprintf (str, sizeof (str), "%lum", val / MINSEC);
204 else
205 snprintf (str, sizeof (str), "%lus", val);
207 return str;
210 static int set_varptr (char *entry, void *ptr)
212 zconf_para_t *c;
214 for ( c = confpara; c->label; c++ )
215 if ( strcasecmp (entry, c->label) == 0 )
217 c->var = ptr;
218 return 1;
220 return 0;
223 static void set_all_varptr (zconf_t *cp)
225 set_varptr ("zonedir", &cp->zonedir);
226 set_varptr ("recursive", &cp->recursive);
227 set_varptr ("printage", &cp->printage);
228 set_varptr ("printtime", &cp->printtime);
229 set_varptr ("leftjustify", &cp->ljust);
231 set_varptr ("resigninterval", &cp->resign);
232 set_varptr ("sigvalidity", &cp->sigvalidity);
233 set_varptr ("max_ttl", &cp->max_ttl);
234 set_varptr ("key_ttl", &cp->key_ttl);
235 set_varptr ("propagation", &cp->proptime);
236 #if defined (DEF_TTL)
237 set_varptr ("def_ttl", &cp->def_ttl);
238 #endif
239 set_varptr ("serialformat", &cp->serialform);
241 set_varptr ("key_algo", &cp->k_algo);
242 set_varptr ("addkey_algo", &cp->k2_algo);
243 set_varptr ("ksk_lifetime", &cp->k_life);
244 set_varptr ("ksk_algo", &cp->k_algo); /* to be removed in next release */
245 set_varptr ("ksk_bits", &cp->k_bits);
246 set_varptr ("ksk_randfile", &cp->k_random);
248 set_varptr ("zsk_lifetime", &cp->z_life);
249 // set_varptr ("zsk_algo", &cp->z_algo);
250 set_varptr ("zsk_algo", &cp->k2_algo);
251 set_varptr ("zsk_bits", &cp->z_bits);
252 set_varptr ("zsk_randfile", &cp->z_random);
253 set_varptr ("saltbits", &cp->saltbits);
255 set_varptr ("--view", &cp->view);
256 set_varptr ("--noexec", &cp->noexec);
257 set_varptr ("logfile", &cp->logfile);
258 set_varptr ("loglevel", &cp->loglevel);
259 set_varptr ("syslogfacility", &cp->syslogfacility);
260 set_varptr ("sysloglevel", &cp->sysloglevel);
261 set_varptr ("verboselog", &cp->verboselog);
262 set_varptr ("-v", &cp->verbosity);
263 set_varptr ("keyfile", &cp->keyfile);
264 set_varptr ("zonefile", &cp->zonefile);
265 set_varptr ("keysetdir", &cp->keysetdir);
266 set_varptr ("dlv_domain", &cp->lookaside);
267 set_varptr ("sig_randfile", &cp->sig_random);
268 set_varptr ("sig_pseudorand", &cp->sig_pseudo);
269 set_varptr ("sig_generateds", &cp->sig_gends);
270 set_varptr ("sig_parameter", &cp->sig_param);
271 set_varptr ("distribute_cmd", &cp->dist_cmd);
272 set_varptr ("namedchrootdir", &cp->chroot_dir);
275 static void parseconfigline (char *buf, unsigned int line, zconf_t *z)
277 char *end, *val, *p;
278 char *tag;
279 unsigned int len, found;
280 zconf_para_t *c;
282 assert (buf[0] != '\0');
284 p = &buf[strlen(buf)-1]; /* Chop off white space at eol */
285 while ( p >= buf && isspace (*p) )
286 *p-- = '\0';
288 for (p = buf; isspace (*p); p++ ) /* Ignore leading white space */
291 /* Ignore comments and emtpy lines */
292 if ( *p == '\0' || ISCOMMENT (p) )
293 return;
295 tag = p;
296 /* Get the end of the first argument */
297 end = &buf[strlen(buf)-1];
298 while ( p < end && !ISDELIM (*p) ) /* Skip until delim */
299 p++;
300 *p++ = '\0'; /* Terminate this argument */
301 dbg_val1 ("Parsing \"%s\"\n", tag);
304 while ( p < end && ISDELIM (*p) ) /* Skip delim chars */
305 p++;
307 val = p; /* Start of the value */
308 dbg_val1 ("\tgot value \"%s\"\n", val);
310 /* If starting with quote, skip until next quote */
311 if ( *p == '"' || *p == '\'' )
313 p++; /* Find next quote */
314 while ( p <= end && *p && *p != *val )
315 p++;
316 *p = '\0';
317 val++; /* Skip the first quote */
319 else /* Otherwise check if there is any comment char at the end */
321 while ( p < end && *p && !ISCOMMENT(p) )
322 p++;
323 if ( ISCOMMENT (p) )
325 do /* Chop off white space before comment */
326 *p-- = '\0';
327 while ( p >= val && isspace (*p) );
331 /* Otherwise it is already terminated above */
333 found = 0;
334 c = confpara;
335 while ( !found && c->type != CONF_END )
337 len = strlen (c->label);
338 if ( strcasecmp (tag, c->label) == 0 )
340 char **str;
341 char quantity;
342 long lval;
344 found = 1;
345 switch ( c->type )
347 case CONF_LEVEL:
348 case CONF_FACILITY:
349 case CONF_STRING:
350 str = (char **)c->var;
351 *str = strdup (val);
352 str_untaint (*str); /* remove "bad" characters */
353 break;
354 case CONF_INT:
355 sscanf (val, "%d", (int *)c->var);
356 break;
357 case CONF_TIMEINT:
358 quantity = 'd';
359 sscanf (val, "%ld%c", &lval, &quantity);
360 if ( quantity == 'm' )
361 lval *= MINSEC;
362 else if ( quantity == 'h' )
363 lval *= HOURSEC;
364 else if ( quantity == 'd' )
365 lval *= DAYSEC;
366 else if ( quantity == 'w' )
367 lval *= WEEKSEC;
368 else if ( quantity == 'y' )
369 lval *= YEARSEC;
370 (*(long *)c->var) = lval;
371 break;
372 case CONF_ALGO:
373 if ( strcasecmp (val, "rsa") == 0 || strcasecmp (val, "rsamd5") == 0 )
374 *((int *)c->var) = DK_ALGO_RSA;
375 else if ( strcasecmp (val, "dsa") == 0 )
376 *((int *)c->var) = DK_ALGO_DSA;
377 else if ( strcasecmp (val, "rsasha1") == 0 )
378 *((int *)c->var) = DK_ALGO_RSASHA1;
379 else if ( strcasecmp (val, "nsec3dsa") == 0 ||
380 strcasecmp (val, "n3dsa") == 0 )
381 *((int *)c->var) = DK_ALGO_NSEC3DSA;
382 else if ( strcasecmp (val, "nsec3rsasha1") == 0 ||
383 strcasecmp (val, "n3rsasha1") == 0 )
384 *((int *)c->var) = DK_ALGO_NSEC3RSASHA1;
385 else
386 error ("Illegal algorithm \"%s\" "
387 "in line %d.\n" , val, line);
388 break;
389 case CONF_SERIAL:
390 if ( strcasecmp (val, "unixtime") == 0 )
391 *((serial_form_t *)c->var) = Unixtime;
392 else if ( strcasecmp (val, "incremental") == 0 )
393 *((serial_form_t *)c->var) = Incremental;
394 else
395 error ("Illegal serial no format \"%s\" "
396 "in line %d.\n" , val, line);
397 break;
398 case CONF_BOOL:
399 *((int *)c->var) = ISTRUE (val);
400 break;
401 default:
402 fatal ("Illegal configuration type in line %d.\n", line);
405 c++;
407 if ( !found )
408 error ("Unknown configuration statement: %s \"%s\"\n", tag, val);
409 return;
412 static void printconfigline (FILE *fp, zconf_para_t *cp)
414 int i;
415 long lval;
417 assert (fp != NULL);
418 assert (cp != NULL);
420 switch ( cp->type )
422 case CONF_COMMENT:
423 if ( cp->var )
424 fprintf (fp, "# %s\n", (char *)cp->var);
425 else
426 fprintf (fp, "\n");
427 break;
428 case CONF_LEVEL:
429 case CONF_FACILITY:
430 if ( *(char **)cp->var != NULL )
432 if ( **(char **)cp->var != '\0' )
434 char *p;
436 fprintf (fp, "%s:\t", cp->label);
437 for ( p = *(char **)cp->var; *p; p++ )
438 putc (toupper (*p), fp);
439 fprintf (fp, "\n");
441 else
442 fprintf (fp, "%s:\tNONE", cp->label);
444 break;
445 case CONF_STRING:
446 if ( *(char **)cp->var )
447 fprintf (fp, "%s:\t\"%s\"\n", cp->label, *(char **)cp->var);
448 break;
449 case CONF_BOOL:
450 fprintf (fp, "%s:\t%s\n", cp->label, bool2str ( *(int*)cp->var ));
451 break;
452 case CONF_TIMEINT:
453 lval = *(ulong*)cp->var; /* in that case it should be of type ulong */
454 fprintf (fp, "%s:\t%s", cp->label, timeint2str (lval));
455 if ( lval )
456 fprintf (fp, "\t# (%ld seconds)", lval);
457 putc ('\n', fp);
458 break;
459 case CONF_ALGO:
460 i = *(int*)cp->var;
461 if ( i )
463 fprintf (fp, "%s:\t%s", cp->label, dki_algo2str (i));
464 fprintf (fp, "\t# (Algorithm ID %d)\n", i);
466 break;
467 case CONF_SERIAL:
468 fprintf (fp, "%s:\t", cp->label);
469 if ( *(serial_form_t*)cp->var == Unixtime )
470 fprintf (fp, "unixtime\n");
471 else
472 fprintf (fp, "incremental\n");
473 break;
474 case CONF_INT:
475 fprintf (fp, "%s:\t%d\n", cp->label, *(int *)cp->var);
476 break;
477 case CONF_END:
478 /* NOTREACHED */
479 break;
483 /*****************************************************************
484 ** public function definition
485 *****************************************************************/
487 /*****************************************************************
488 ** loadconfig (file, conf)
489 ** Loads a config file into the "conf" structure pointed to by "z".
490 ** If "z" is NULL then a new conf struct will be dynamically
491 ** allocated.
492 ** If no filename is given the conf struct will be initialized
493 ** by the builtin default config
494 *****************************************************************/
495 zconf_t *loadconfig (const char *filename, zconf_t *z)
497 FILE *fp;
498 char buf[1023+1];
499 unsigned int line;
501 if ( z == NULL ) /* allocate new memory for zconf_t */
503 if ( (z = calloc (1, sizeof (zconf_t))) == NULL )
504 return NULL;
506 if ( filename && *filename )
507 memcpy (z, &def, sizeof (zconf_t)); /* init new struct with defaults */
510 if ( filename == NULL || *filename == '\0' ) /* no file name given... */
512 dbg_val0("loadconfig (NULL)\n");
513 memcpy (z, &def, sizeof (zconf_t)); /* ..then init with defaults */
514 return z;
517 dbg_val1 ("loadconfig (%s)\n", filename);
518 set_all_varptr (z);
520 if ( (fp = fopen(filename, "r")) == NULL )
521 fatal ("Could not open config file \"%s\"\n", filename);
523 line = 0;
524 while (fgets(buf, sizeof(buf), fp))
525 parseconfigline (buf, ++line, z);
527 fclose(fp);
528 return z;
531 # define STRCONFIG_DELIMITER ";\r\n"
532 zconf_t *loadconfig_fromstr (const char *str, zconf_t *z)
534 char *buf;
535 char *tok, *toksave;
536 unsigned int line;
538 if ( z == NULL )
540 if ( (z = calloc (1, sizeof (zconf_t))) == NULL )
541 return NULL;
542 memcpy (z, &def, sizeof (zconf_t)); /* init with defaults */
545 if ( str == NULL || *str == '\0' )
547 dbg_val0("loadconfig_fromstr (NULL)\n");
548 memcpy (z, &def, sizeof (zconf_t)); /* init with defaults */
549 return z;
552 dbg_val1 ("loadconfig_fromstr (\"%s\")\n", str);
553 set_all_varptr (z);
555 /* str is const, so we have to copy it into a new buffer */
556 if ( (buf = strdup (str)) == NULL )
557 fatal ("loadconfig_fromstr: Out of memory");
559 line = 0;
560 tok = strtok_r (buf, STRCONFIG_DELIMITER, &toksave);
561 while ( tok )
563 line++;
564 parseconfigline (tok, line, z);
565 tok = strtok_r (NULL, STRCONFIG_DELIMITER, &toksave);
567 free (buf);
568 return z;
571 /*****************************************************************
572 ** dupconfig (config)
573 ** duplicate config struct and return a ptr to the new struct
574 *****************************************************************/
575 zconf_t *dupconfig (const zconf_t *conf)
577 zconf_t *z;
579 assert (conf != NULL);
581 if ( (z = calloc (1, sizeof (zconf_t))) == NULL )
582 return NULL;
584 memcpy (z, conf, sizeof (zconf_t));
586 return z;
589 /*****************************************************************
590 ** setconfigpar (entry, pval)
591 *****************************************************************/
592 int setconfigpar (zconf_t *config, char *entry, const void *pval)
594 char *str;
595 zconf_para_t *c;
597 set_all_varptr (config);
599 for ( c = confpara; c->type != CONF_END; c++ )
600 if ( strcasecmp (entry, c->label) == 0 )
602 switch ( c->type )
604 case CONF_LEVEL:
605 case CONF_FACILITY:
606 case CONF_STRING:
607 if ( pval )
609 str = strdup ((char *)pval);
610 str_untaint (str); /* remove "bad" characters */
612 else
613 str = NULL;
614 *((char **)c->var) = str;
615 break;
616 case CONF_BOOL:
617 /* fall through */
618 case CONF_ALGO:
619 /* fall through */
620 case CONF_INT:
621 *((int *)c->var) = *((int *)pval);
622 break;
623 case CONF_TIMEINT:
624 *((long *)c->var) = *((long *)pval);
625 break;
626 case CONF_SERIAL:
627 *((serial_form_t *)c->var) = *((serial_form_t *)pval);
628 break;
629 case CONF_COMMENT:
630 case CONF_END:
631 /* NOTREACHED */
632 break;
634 return 1;
636 return 0;
639 /*****************************************************************
640 ** printconfig (fname, config)
641 *****************************************************************/
642 int printconfig (const char *fname, const zconf_t *z)
644 zconf_para_t *cp;
645 FILE *fp;
647 if ( z == NULL )
648 return 0;
650 fp = stdout;
651 if ( fname && *fname )
653 if ( strcmp (fname, "stdout") == 0 )
654 fp = stdout;
655 else if ( strcmp (fname, "stderr") == 0 )
656 fp = stderr;
657 else if ( (fp = fopen(fname, "w")) == NULL )
659 error ("Could not open config file \"%s\" for writing\n", fname);
660 return -1;
664 set_all_varptr ((zconf_t *)z);
666 for ( cp = confpara; cp->type != CONF_END; cp++ ) /* loop through all parameter */
667 if ( !cp->cmdline ) /* if this is not a command line parameter ? */
668 printconfigline (fp, cp); /* print it out */
670 if ( fp && fp != stdout && fp != stderr )
671 fclose (fp);
673 return 1;
676 #if 0
677 /*****************************************************************
678 ** printconfigdiff (fname, conf_a, conf_b)
679 *****************************************************************/
680 int printconfigdiff (const char *fname, const zconf_t *ref, const zconf_t *z)
682 zconf_para_t *cp;
683 FILE *fp;
685 if ( ref == NULL || z == NULL )
686 return 0;
688 fp = NULL;
689 if ( fname && *fname )
691 if ( strcmp (fname, "stdout") == 0 )
692 fp = stdout;
693 else if ( strcmp (fname, "stderr") == 0 )
694 fp = stderr;
695 else if ( (fp = fopen(fname, "w")) == NULL )
697 error ("Could not open config file \"%s\" for writing\n", fname);
698 return -1;
702 set_all_varptr ((zconf_t *)z);
704 for ( cp = confpara; cp->type != CONF_END; cp++ ) /* loop through all parameter */
706 if ( cp->cmdline )
707 continue;
710 printconfigline (fp, cp); /* print it out */
713 if ( fp && fp != stdout && fp != stderr )
714 fclose (fp);
716 return 1;
718 #endif
720 /*****************************************************************
721 ** checkconfig (config)
722 *****************************************************************/
723 int checkconfig (const zconf_t *z)
725 if ( z == NULL )
726 return 1;
728 if ( z->saltbits < 4 )
729 fprintf (stderr, "Saltlength must be at least 4 bits\n");
730 if ( z->saltbits > 128 )
732 fprintf (stderr, "While the maximum is 520 bits of salt, it's not recommended to use more than 128 bits.\n");
733 fprintf (stderr, "The current value is %d bits\n", z->saltbits);
736 if ( z->sigvalidity < (1 * DAYSEC) || z->sigvalidity > (12 * WEEKSEC) )
738 fprintf (stderr, "Signature should be valid for at least 1 day and no longer than 3 month (12 weeks)\n");
739 fprintf (stderr, "The current value is %s\n", timeint2str (z->sigvalidity));
742 if ( z->resign > (z->sigvalidity*5/6) - (z->max_ttl + z->proptime) )
744 fprintf (stderr, "Re-signing interval (%s) should be less than ", timeint2str (z->resign));
745 fprintf (stderr, "5/6 of sigvalidity\n");
747 if ( z->resign < (z->max_ttl + z->proptime) )
749 fprintf (stderr, "Re-signing interval (%s) should be ", timeint2str (z->resign));
750 fprintf (stderr, "greater than max_ttl (%ld) plus ", z->max_ttl);
751 fprintf (stderr, "propagation time (%ld)\n", z->proptime);
754 if ( z->max_ttl >= z->sigvalidity )
755 fprintf (stderr, "Max TTL (%ld) should be less than signature validity (%ld)\n",
756 z->max_ttl, z->sigvalidity);
758 if ( z->z_life > (12 * WEEKSEC) * (z->z_bits / 512.) )
760 fprintf (stderr, "Lifetime of zone signing key (%s) ", timeint2str (z->z_life));
761 fprintf (stderr, "seems a little bit high ");
762 fprintf (stderr, "(In respect of key size (%d))\n", z->z_bits);
765 if ( z->k_life > 0 && z->k_life <= z->z_life )
767 fprintf (stderr, "Lifetime of key signing key (%s) ", timeint2str (z->k_life));
768 fprintf (stderr, "should be greater than lifetime of zsk\n");
770 if ( z->k_life > 0 && z->k_life > (26 * WEEKSEC) * (z->k_bits / 512.) )
772 fprintf (stderr, "Lifetime of key signing key (%s) ", timeint2str (z->k_life));
773 fprintf (stderr, "seems a little bit high ");
774 fprintf (stderr, "(In respect of key size (%d))\n", z->k_bits);
777 return 1;
780 #ifdef CONF_TEST
781 const char *progname;
782 static zconf_t *config;
784 main (int argc, char *argv[])
786 char *optstr;
787 int val;
789 progname = *argv;
791 config = loadconfig ("", (zconf_t *) NULL); /* load built in defaults */
793 while ( --argc >= 1 )
795 optstr = *++argv;
796 config = loadconfig_fromstr (optstr, config);
799 val = 1;
800 setconfigpar (config, "-v", &val);
801 val = 2;
802 setconfigpar (config, "verboselog", &val);
803 val = 1;
804 setconfigpar (config, "recursive", &val);
805 val = 1200;
806 setconfigpar (config, "propagation", &val);
808 printconfig ("stdout", config);
810 #endif