Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / contrib / zkt-1.1.3 / zkt.c
blob8e29ed878f5fa7d22bab5be8cd4921a81b22b023
1 /* $NetBSD: zkt.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
4 **
5 ** @(#) zkt.c -- A library for managing a list of dns zone files.
6 **
7 ** Copyright (c) 2005 - 2008, Holger Zuleger HZnet. All rights reserved.
8 **
9 ** This software is open source.
11 ** Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions
13 ** are met:
15 ** Redistributions of source code must retain the above copyright notice,
16 ** this list of conditions and the following disclaimer.
18 ** Redistributions in binary form must reproduce the above copyright notice,
19 ** this list of conditions and the following disclaimer in the documentation
20 ** and/or other materials provided with the distribution.
22 ** Neither the name of Holger Zuleger HZnet nor the names of its contributors may
23 ** be used to endorse or promote products derived from this software without
24 ** specific prior written permission.
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 ** POSSIBILITY OF SUCH DAMAGE.
38 *****************************************************************/
39 # include <stdio.h>
40 # include <string.h>
41 #ifdef HAVE_CONFIG_H
42 # include <config.h>
43 #endif
44 # include "config_zkt.h"
45 # include "dki.h"
46 # include "misc.h"
47 # include "strlist.h"
48 # include "zconf.h"
49 # include "domaincmp.h"
50 # include "tcap.h"
51 #define extern
52 # include "zkt.h"
53 #undef extern
55 extern char *labellist;
56 extern int headerflag;
57 extern int timeflag;
58 extern int exptimeflag;
59 extern int lifetime;
60 extern int ageflag;
61 extern int lifetimeflag;
62 extern int kskflag;
63 extern int zskflag;
64 extern int pathflag;
65 extern int ljustflag;
67 static void printkeyinfo (const dki_t *dkp, const char *oldpath);
69 static void printkeyinfo (const dki_t *dkp, const char *oldpath)
71 time_t currtime;
73 if ( dkp == NULL ) /* print headline */
75 if ( headerflag )
77 tc_attr (stdout, TC_BOLD, 1);
78 printf ("%-33.33s %5s %3s %3.3s %-7s", "Keyname",
79 "Tag", "Typ", "Status", "Algorit");
80 if ( timeflag )
81 printf (" %-20s", "Generation Time");
82 if ( exptimeflag )
83 printf (" %-20s", "Expiration Time");
84 if ( ageflag )
85 printf (" %16s", "Age");
86 if ( lifetimeflag )
87 printf (" %4s", "LfTm");
88 tc_attr (stdout, TC_BOLD, 0);
89 putchar ('\n');
91 return;
93 time (&currtime);
95 /* TODO: use next line if dname is dynamically allocated */
96 /* if ( pathflag && dkp->dname && strcmp (oldpath, dkp->dname) != 0 ) */
97 if ( pathflag && strcmp (oldpath, dkp->dname) != 0 )
98 printf ("%s/\n", dkp->dname);
100 if ( (kskflag && dki_isksk (dkp)) || (zskflag && !dki_isksk (dkp)) )
102 int color;
104 if ( ljustflag )
105 printf ("%-33.33s ", dkp->name);
106 else
107 printf ("%33.33s ", dkp->name);
108 printf ("%05d ", dkp->tag);
109 printf ("%3s ", dki_isksk (dkp) ? "KSK" : "ZSK");
111 if ( dkp->status == DKI_ACT )
112 color = TC_GREEN;
113 else if ( dkp->status == DKI_PUB )
114 color = TC_BLUE;
115 else if ( dkp->status == DKI_DEP )
116 color = TC_RED;
117 else
118 color = TC_BLACK;
119 tc_attr (stdout, color, 1);
120 printf ("%-3.3s ", dki_statusstr (dkp) );
121 tc_attr (stdout, color, 0);
123 printf ("%-7s", dki_algo2sstr(dkp->algo));
125 if ( currtime < dkp->time + dkp->lifetime )
126 color = TC_GREEN;
127 else
128 color = TC_BOLD|TC_RED;
129 tc_attr (stdout, color, 1);
131 if ( timeflag )
132 printf (" %-20s", time2str (dkp->gentime ? dkp->gentime: dkp->time, 's'));
133 if ( exptimeflag )
134 printf (" %-20s", time2str (dkp->exptime, 's'));
135 if ( ageflag )
136 printf (" %16s", age2str (dki_age (dkp, currtime)));
137 if ( lifetimeflag && dkp->lifetime )
139 if ( dkp->status == 'a' )
140 printf ("%c", (currtime < dkp->time + dkp->lifetime) ? '<' : '!');
141 else
142 putchar (' ');
143 printf ("%hdd", dki_lifetimedays (dkp));
145 tc_attr (stdout, color, 0);
146 putchar ('\n');
150 #if defined(USE_TREE) && USE_TREE
151 static void list_key (const dki_t **nodep, const VISIT which, int depth)
153 const dki_t *dkp;
154 static const char *oldpath = "";
156 if ( nodep == NULL )
157 return;
158 //fprintf (stderr, "listkey %d %d %s\n", which, depth, dkp->name);
160 if ( which == INORDER || which == LEAF )
162 dkp = *nodep;
163 while ( dkp ) /* loop through list */
165 if ( labellist == NULL || isinlist (dkp->name, labellist) )
166 printkeyinfo (dkp, oldpath); /* print entry */
167 oldpath = dkp->dname;
168 dkp = dkp->next;
172 #endif
174 void zkt_list_keys (const dki_t *data)
176 #if ! defined(USE_TREE) || !USE_TREE
177 const dki_t *dkp;
178 const char *oldpath;
179 #endif
181 if ( data ) /* print headline if list is not empty */
182 printkeyinfo (NULL, "");
184 #if defined(USE_TREE) && USE_TREE
185 twalk (data, list_key);
186 #else
187 oldpath = "";
188 for ( dkp = data; dkp; dkp = dkp->next ) /* loop through list */
190 if ( labellist == NULL || isinlist (dkp->name, labellist) )
191 printkeyinfo (dkp, oldpath); /* print entry */
192 oldpath = dkp->dname;
194 #endif
197 #if defined(USE_TREE) && USE_TREE
198 # if 0
199 static void list_trustedkey (const dki_t **nodep, const VISIT which, int depth)
201 const dki_t *dkp;
203 if ( nodep == NULL )
204 return;
206 dkp = *nodep;
207 if ( which == INORDER || which == LEAF )
209 // fprintf (stderr, "list_trustedkey order=%d(pre=0,in=1,post=2,leaf=3) depth=%d %s\n", which, depth, dkp->name);
210 /* loop through list */
211 while ( dkp )
213 if ( (dki_isksk (dkp) || zskflag) &&
214 (labellist == NULL || isinlist (dkp->name, labellist)) )
215 dki_prt_trustedkey (dkp, stdout);
216 dkp = dkp->next;
220 # else
221 const dki_t *parent;
222 static void list_trustedkey (const dki_t **nodep, const VISIT which, int depth)
224 const dki_t *dkp;
226 if ( nodep == NULL )
227 return;
229 dkp = *nodep;
230 if ( which == INORDER || which == LEAF )
232 // fprintf (stderr, "list_trustedkey order=%d(pre=0,in=1,post=2,leaf=3) depth=%d %s\n", which, depth, dkp->name);
233 if ( labellist && !isinlist (dkp->name, labellist) )
234 return;
236 if ( parent == NULL || !issubdomain (dkp->name, parent->name) )
238 parent = dkp;
239 /* loop through list */
240 while ( dkp )
242 if ( (dki_isksk (dkp) || zskflag) )
243 dki_prt_trustedkey (dkp, stdout);
244 dkp = dkp->next;
249 static void list_managedkey (const dki_t **nodep, const VISIT which, int depth)
251 const dki_t *dkp;
253 if ( nodep == NULL )
254 return;
256 dkp = *nodep;
257 if ( which == INORDER || which == LEAF )
259 // fprintf (stderr, "list_trustedkey order=%d(pre=0,in=1,post=2,leaf=3) depth=%d %s\n", which, depth, dkp->name);
260 if ( labellist && !isinlist (dkp->name, labellist) )
261 return;
263 if ( parent == NULL || !issubdomain (dkp->name, parent->name) )
265 const dki_t *dkp_head = NULL;
266 const dki_t *standby = NULL;
268 parent = dkp;
270 dkp_head = dkp;
271 /* look for a standby key */
272 for ( dkp = dkp_head; dkp; dkp = dkp->next )
273 if ( dki_isksk (dkp) && dki_ispublished (dkp) )
274 standby = dkp;
276 if ( !standby ) /* no standby key found ? */
277 return;
279 /* print all non-standby ksk */
280 for ( dkp = dkp_head; dkp; dkp = dkp->next )
281 if ( dki_isksk (dkp) && dkp != standby )
282 dki_prt_managedkey (dkp, stdout);
286 # endif
287 #endif
289 void zkt_list_trustedkeys (const dki_t *data)
292 /* print headline if list is not empty */
293 if ( data && headerflag )
294 printf ("trusted-keys {\n");
296 #if defined(USE_TREE) && USE_TREE
297 twalk (data, list_trustedkey);
298 #else
299 for ( dkp = data; dkp; dkp = dkp->next ) /* loop through list */
300 if ( (dki_isksk (dkp) || zskflag) &&
301 (labellist == NULL || isinlist (dkp->name, labellist)) )
302 dki_prt_trustedkey (dkp, stdout);
303 #endif
305 /* print end of trusted-key section */
306 if ( data && headerflag )
307 printf ("};\n");
310 void zkt_list_managedkeys (const dki_t *data)
313 /* print headline if list is not empty */
314 if ( data && headerflag )
315 printf ("managed-keys {\n");
317 #if defined(USE_TREE) && USE_TREE
318 twalk (data, list_managedkey);
319 #else
320 for ( dkp = data; dkp; dkp = dkp->next ) /* loop through list */
321 if ( (dki_isksk (dkp) || zskflag) &&
322 (labellist == NULL || isinlist (dkp->name, labellist)) )
323 dki_prt_managedkey (dkp, stdout);
324 #endif
326 /* print end of trusted-key section */
327 if ( data && headerflag )
328 printf ("};\n");
331 #if defined(USE_TREE) && USE_TREE
332 static void list_dnskey (const dki_t **nodep, const VISIT which, int depth)
334 const dki_t *dkp;
335 int ksk;
337 if ( nodep == NULL )
338 return;
340 if ( which == INORDER || which == LEAF )
341 for ( dkp = *nodep; dkp; dkp = dkp->next )
343 ksk = dki_isksk (dkp);
344 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
345 continue;
347 if ( labellist == NULL || isinlist (dkp->name, labellist) )
349 if ( headerflag )
350 dki_prt_comment (dkp, stdout);
351 dki_prt_dnskey (dkp, stdout);
355 #endif
357 void zkt_list_dnskeys (const dki_t *data)
359 #if defined(USE_TREE) && USE_TREE
360 twalk (data, list_dnskey);
361 #else
362 const dki_t *dkp;
363 int ksk;
365 for ( dkp = data; dkp; dkp = dkp->next )
367 ksk = dki_isksk (dkp);
368 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
369 continue;
371 if ( labellist == NULL || isinlist (dkp->name, labellist) )
373 if ( headerflag )
374 dki_prt_comment (dkp, stdout);
375 dki_prt_dnskey (dkp, stdout);
378 #endif
381 #if defined(USE_TREE) && USE_TREE
382 static void set_keylifetime (const dki_t **nodep, const VISIT which, int depth)
384 const dki_t *dkp;
385 int ksk;
387 if ( nodep == NULL )
388 return;
390 if ( which == INORDER || which == LEAF )
391 for ( dkp = *nodep; dkp; dkp = dkp->next )
393 ksk = dki_isksk (dkp);
394 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
395 continue;
397 if ( labellist == NULL || isinlist (dkp->name, labellist) )
398 dki_setlifetime ((dki_t *)dkp, lifetime);
401 #endif
403 void zkt_setkeylifetime (dki_t *data)
405 #if defined(USE_TREE) && USE_TREE
406 twalk (data, set_keylifetime);
407 #else
408 dki_t *dkp;
409 int ksk;
411 for ( dkp = data; dkp; dkp = dkp->next )
413 ksk = dki_isksk (dkp);
414 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
415 continue;
417 if ( labellist == NULL || isinlist (dkp->name, labellist) )
419 dki_setlifetime (dkp, lifetime);
422 #endif
426 #if defined(USE_TREE) && USE_TREE
427 static const dki_t *searchresult;
428 static int searchitem;
429 static void tag_search (const dki_t **nodep, const VISIT which, int depth)
431 const dki_t *dkp;
433 if ( nodep == NULL )
434 return;
436 if ( which == PREORDER || which == LEAF )
437 for ( dkp = *nodep; dkp; dkp = dkp->next )
439 if ( dkp->tag == searchitem )
441 if ( searchresult == NULL )
442 searchresult = dkp;
443 else
444 searchitem = 0;
448 #endif
449 const dki_t *zkt_search (const dki_t *data, int searchtag, const char *keyname)
451 const dki_t *dkp = NULL;
453 #if defined(USE_TREE) && USE_TREE
454 if ( keyname == NULL || *keyname == '\0' )
456 searchresult = NULL;
457 searchitem = searchtag;
458 twalk (data, tag_search);
459 if ( searchresult != NULL && searchitem == 0 )
460 dkp = (void *)01;
461 else
462 dkp = searchresult;
464 else
465 dkp = (dki_t*)dki_tsearch (data, searchtag, keyname);
466 #else
467 dkp = (dki_t*)dki_search (data, searchtag, keyname);
468 #endif
469 return dkp;