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"
53 extern char *labellist
;
54 extern int headerflag
;
56 extern int exptimeflag
;
59 extern int lifetimeflag
;
65 static void printkeyinfo (const dki_t
*dkp
, const char *oldpath
);
67 static void printkeyinfo (const dki_t
*dkp
, const char *oldpath
)
71 if ( dkp
== NULL
) /* print headline */
75 printf ("%-33.33s %5s %3s %3.3s %-7s", "Keyname",
76 "Tag", "Typ", "Status", "Algorit");
78 printf (" %-20s", "Generation Time");
80 printf (" %-20s", "Expiration Time");
82 printf (" %16s", "Age");
84 printf (" %4s", "LfTm");
91 /* TODO: use next line if dname is dynamically allocated */
92 /* if ( pathflag && dkp->dname && strcmp (oldpath, dkp->dname) != 0 ) */
93 if ( pathflag
&& strcmp (oldpath
, dkp
->dname
) != 0 )
94 printf ("%s/\n", dkp
->dname
);
96 if ( (kskflag
&& dki_isksk (dkp
)) || (zskflag
&& !dki_isksk (dkp
)) )
99 printf ("%-33.33s ", dkp
->name
);
101 printf ("%33.33s ", dkp
->name
);
102 printf ("%05d ", dkp
->tag
);
103 printf ("%3s ", dki_isksk (dkp
) ? "KSK" : "ZSK");
104 printf ("%-3.3s ", dki_statusstr (dkp
) );
105 printf ("%-7s", dki_algo2sstr(dkp
->algo
));
107 printf (" %-20s", time2str (dkp
->gentime
? dkp
->gentime
: dkp
->time
, 's'));
109 printf (" %-20s", time2str (dkp
->exptime
, 's'));
111 printf (" %16s", age2str (dki_age (dkp
, currtime
)));
112 if ( lifetimeflag
&& dkp
->lifetime
)
114 if ( dkp
->status
== 'a' )
115 printf ("%c", (currtime
< dkp
->time
+ dkp
->lifetime
) ? '<' : '!');
118 printf ("%hdd", dki_lifetimedays (dkp
));
124 #if defined(USE_TREE) && USE_TREE
125 static void list_key (const dki_t
**nodep
, const VISIT which
, int depth
)
128 static const char *oldpath
= "";
132 //fprintf (stderr, "listkey %d %d %s\n", which, depth, dkp->name);
134 if ( which
== INORDER
|| which
== LEAF
)
137 while ( dkp
) /* loop through list */
139 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
140 printkeyinfo (dkp
, oldpath
); /* print entry */
141 oldpath
= dkp
->dname
;
148 void zkt_list_keys (const dki_t
*data
)
150 #if ! defined(USE_TREE) || !USE_TREE
155 if ( data
) /* print headline if list is not empty */
156 printkeyinfo (NULL
, "");
158 #if defined(USE_TREE) && USE_TREE
159 twalk (data
, list_key
);
162 for ( dkp
= data
; dkp
; dkp
= dkp
->next
) /* loop through list */
164 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
165 printkeyinfo (dkp
, oldpath
); /* print entry */
166 oldpath
= dkp
->dname
;
171 #if defined(USE_TREE) && USE_TREE
172 static void list_trustedkey (const dki_t
**nodep
, const VISIT which
, int depth
)
180 //fprintf (stderr, "list_trustedkey %d %d %s\n", which, depth, dkp->name);
181 if ( which
== INORDER
|| which
== LEAF
)
182 while ( dkp
) /* loop through list */
184 if ( (dki_isksk (dkp
) || zskflag
) &&
185 (labellist
== NULL
|| isinlist (dkp
->name
, labellist
)) )
186 dki_prt_trustedkey (dkp
, stdout
);
192 void zkt_list_trustedkeys (const dki_t
*data
)
194 #if !defined(USE_TREE) || !USE_TREE
197 /* print headline if list is not empty */
198 if ( data
&& headerflag
)
199 printf ("trusted-keys {\n");
201 #if defined(USE_TREE) && USE_TREE
202 twalk (data
, list_trustedkey
);
205 for ( dkp
= data
; dkp
; dkp
= dkp
->next
) /* loop through list */
206 if ( (dki_isksk (dkp
) || zskflag
) &&
207 (labellist
== NULL
|| isinlist (dkp
->name
, labellist
)) )
208 dki_prt_trustedkey (dkp
, stdout
);
211 /* print end of trusted-key section */
212 if ( data
&& headerflag
)
216 #if defined(USE_TREE) && USE_TREE
217 static void list_dnskey (const dki_t
**nodep
, const VISIT which
, int depth
)
225 if ( which
== INORDER
|| which
== LEAF
)
226 for ( dkp
= *nodep
; dkp
; dkp
= dkp
->next
)
228 ksk
= dki_isksk (dkp
);
229 if ( (ksk
&& !kskflag
) || (!ksk
&& !zskflag
) )
232 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
235 dki_prt_comment (dkp
, stdout
);
236 dki_prt_dnskey (dkp
, stdout
);
242 void zkt_list_dnskeys (const dki_t
*data
)
244 #if defined(USE_TREE) && USE_TREE
245 twalk (data
, list_dnskey
);
250 for ( dkp
= data
; dkp
; dkp
= dkp
->next
)
252 ksk
= dki_isksk (dkp
);
253 if ( (ksk
&& !kskflag
) || (!ksk
&& !zskflag
) )
256 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
259 dki_prt_comment (dkp
, stdout
);
260 dki_prt_dnskey (dkp
, stdout
);
266 #if defined(USE_TREE) && USE_TREE
267 static void set_keylifetime (const dki_t
**nodep
, const VISIT which
, int depth
)
275 if ( which
== INORDER
|| which
== LEAF
)
276 for ( dkp
= *nodep
; dkp
; dkp
= dkp
->next
)
278 ksk
= dki_isksk (dkp
);
279 if ( (ksk
&& !kskflag
) || (!ksk
&& !zskflag
) )
282 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
283 dki_setlifetime ((dki_t
*)dkp
, lifetime
);
288 void zkt_setkeylifetime (dki_t
*data
)
290 #if defined(USE_TREE) && USE_TREE
291 twalk (data
, set_keylifetime
);
296 for ( dkp
= data
; dkp
; dkp
= dkp
->next
)
298 ksk
= dki_isksk (dkp
);
299 if ( (ksk
&& !kskflag
) || (!ksk
&& !zskflag
) )
302 if ( labellist
== NULL
|| isinlist (dkp
->name
, labellist
) )
304 dki_setlifetime (dkp
, lifetime
);
311 #if defined(USE_TREE) && USE_TREE
312 static const dki_t
*searchresult
;
313 static int searchitem
;
314 static void tag_search (const dki_t
**nodep
, const VISIT which
, int depth
)
321 if ( which
== PREORDER
|| which
== LEAF
)
322 for ( dkp
= *nodep
; dkp
; dkp
= dkp
->next
)
324 if ( dkp
->tag
== searchitem
)
326 if ( searchresult
== NULL
)
334 const dki_t
*zkt_search (const dki_t
*data
, int searchtag
, const char *keyname
)
336 const dki_t
*dkp
= NULL
;
338 #if defined(USE_TREE) && USE_TREE
339 if ( keyname
== NULL
|| *keyname
== '\0' )
342 searchitem
= searchtag
;
343 twalk (data
, tag_search
);
344 if ( searchresult
!= NULL
&& searchitem
== 0 )
350 dkp
= (dki_t
*)dki_tsearch (data
, searchtag
, keyname
);
352 dkp
= (dki_t
*)dki_search (data
, searchtag
, keyname
);