1 /* $NetBSD: zkt.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
5 ** @(#) zkt.c -- A library for managing a list of dns zone files.
7 ** Copyright (c) 2005 - 2008, Holger Zuleger HZnet. All rights reserved.
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
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 *****************************************************************/
44 # include "config_zkt.h"
49 # include "domaincmp.h"
55 extern char *labellist
;
56 extern int headerflag
;
58 extern int exptimeflag
;
61 extern int lifetimeflag
;
67 static void printkeyinfo (const dki_t
*dkp
, const char *oldpath
);
69 static void printkeyinfo (const dki_t
*dkp
, const char *oldpath
)
73 if ( dkp
== NULL
) /* print headline */
77 tc_attr (stdout
, TC_BOLD
, 1);
78 printf ("%-33.33s %5s %3s %3.3s %-7s", "Keyname",
79 "Tag", "Typ", "Status", "Algorit");
81 printf (" %-20s", "Generation Time");
83 printf (" %-20s", "Expiration Time");
85 printf (" %16s", "Age");
87 printf (" %4s", "LfTm");
88 tc_attr (stdout
, TC_BOLD
, 0);
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
)) )
105 printf ("%-33.33s ", dkp
->name
);
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
)
113 else if ( dkp
->status
== DKI_PUB
)
115 else if ( dkp
->status
== DKI_DEP
)
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
)
128 color
= TC_BOLD
|TC_RED
;
129 tc_attr (stdout
, color
, 1);
132 printf (" %-20s", time2str (dkp
->gentime
? dkp
->gentime
: dkp
->time
, 's'));
134 printf (" %-20s", time2str (dkp
->exptime
, 's'));
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
) ? '<' : '!');
143 printf ("%hdd", dki_lifetimedays (dkp
));
145 tc_attr (stdout
, color
, 0);
150 #if defined(USE_TREE) && USE_TREE
151 static void list_key (const dki_t
**nodep
, const VISIT which
, int depth
)
154 static const char *oldpath
= "";
158 //fprintf (stderr, "listkey %d %d %s\n", which, depth, dkp->name);
160 if ( which
== INORDER
|| which
== LEAF
)
163 while ( dkp
) /* loop through list */
165 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
166 printkeyinfo (dkp
, oldpath
); /* print entry */
167 oldpath
= dkp
->dname
;
174 void zkt_list_keys (const dki_t
*data
)
176 #if ! defined(USE_TREE) || !USE_TREE
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
);
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
;
197 #if defined(USE_TREE) && USE_TREE
199 static void list_trustedkey (const dki_t
**nodep
, const VISIT which
, int depth
)
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 */
213 if ( (dki_isksk (dkp
) || zskflag
) &&
214 (labellist
== NULL
|| isinlist (dkp
->name
, labellist
)) )
215 dki_prt_trustedkey (dkp
, stdout
);
222 static void list_trustedkey (const dki_t
**nodep
, const VISIT which
, int depth
)
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
) )
236 if ( parent
== NULL
|| !issubdomain (dkp
->name
, parent
->name
) )
239 /* loop through list */
242 if ( (dki_isksk (dkp
) || zskflag
) )
243 dki_prt_trustedkey (dkp
, stdout
);
249 static void list_managedkey (const dki_t
**nodep
, const VISIT which
, int depth
)
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
) )
263 if ( parent
== NULL
|| !issubdomain (dkp
->name
, parent
->name
) )
265 const dki_t
*dkp_head
= NULL
;
266 const dki_t
*standby
= NULL
;
271 /* look for a standby key */
272 for ( dkp
= dkp_head
; dkp
; dkp
= dkp
->next
)
273 if ( dki_isksk (dkp
) && dki_ispublished (dkp
) )
276 if ( !standby
) /* no standby key found ? */
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
);
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
);
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
);
305 /* print end of trusted-key section */
306 if ( data
&& headerflag
)
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
);
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
);
326 /* print end of trusted-key section */
327 if ( data
&& headerflag
)
331 #if defined(USE_TREE) && USE_TREE
332 static void list_dnskey (const dki_t
**nodep
, const VISIT which
, int depth
)
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
) )
347 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
350 dki_prt_comment (dkp
, stdout
);
351 dki_prt_dnskey (dkp
, stdout
);
357 void zkt_list_dnskeys (const dki_t
*data
)
359 #if defined(USE_TREE) && USE_TREE
360 twalk (data
, list_dnskey
);
365 for ( dkp
= data
; dkp
; dkp
= dkp
->next
)
367 ksk
= dki_isksk (dkp
);
368 if ( (ksk
&& !kskflag
) || (!ksk
&& !zskflag
) )
371 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
374 dki_prt_comment (dkp
, stdout
);
375 dki_prt_dnskey (dkp
, stdout
);
381 #if defined(USE_TREE) && USE_TREE
382 static void set_keylifetime (const dki_t
**nodep
, const VISIT which
, int depth
)
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
) )
397 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
398 dki_setlifetime ((dki_t
*)dkp
, lifetime
);
403 void zkt_setkeylifetime (dki_t
*data
)
405 #if defined(USE_TREE) && USE_TREE
406 twalk (data
, set_keylifetime
);
411 for ( dkp
= data
; dkp
; dkp
= dkp
->next
)
413 ksk
= dki_isksk (dkp
);
414 if ( (ksk
&& !kskflag
) || (!ksk
&& !zskflag
) )
417 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
419 dki_setlifetime (dkp
, lifetime
);
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
)
436 if ( which
== PREORDER
|| which
== LEAF
)
437 for ( dkp
= *nodep
; dkp
; dkp
= dkp
->next
)
439 if ( dkp
->tag
== searchitem
)
441 if ( searchresult
== NULL
)
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' )
457 searchitem
= searchtag
;
458 twalk (data
, tag_search
);
459 if ( searchresult
!= NULL
&& searchitem
== 0 )
465 dkp
= (dki_t
*)dki_tsearch (data
, searchtag
, keyname
);
467 dkp
= (dki_t
*)dki_search (data
, searchtag
, keyname
);