1 /* $NetBSD: zkt.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
2
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.
10 **
11 ** Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions
13 ** are met:
14 **
15 ** Redistributions of source code must retain the above copyright notice,
16 ** this list of conditions and the following disclaimer.
17 **
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.
21 **
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.
25 **
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.
37 **
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
54
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;
66
67 static void printkeyinfo (const dki_t *dkp, const char *oldpath);
68
printkeyinfo(const dki_t * dkp,const char * oldpath)69 static void printkeyinfo (const dki_t *dkp, const char *oldpath)
70 {
71 time_t currtime;
72
73 if ( dkp == NULL ) /* print headline */
74 {
75 if ( headerflag )
76 {
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');
90 }
91 return;
92 }
93 time (&currtime);
94
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);
99
100 if ( (kskflag && dki_isksk (dkp)) || (zskflag && !dki_isksk (dkp)) )
101 {
102 int color;
103
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");
110
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);
122
123 printf ("%-7s", dki_algo2sstr(dkp->algo));
124
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);
130
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 )
138 {
139 if ( dkp->status == 'a' )
140 printf ("%c", (currtime < dkp->time + dkp->lifetime) ? '<' : '!');
141 else
142 putchar (' ');
143 printf ("%hdd", dki_lifetimedays (dkp));
144 }
145 tc_attr (stdout, color, 0);
146 putchar ('\n');
147 }
148 }
149
150 #if defined(USE_TREE) && USE_TREE
list_key(const dki_t ** nodep,const VISIT which,int depth)151 static void list_key (const dki_t **nodep, const VISIT which, int depth)
152 {
153 const dki_t *dkp;
154 static const char *oldpath = "";
155
156 if ( nodep == NULL )
157 return;
158 //fprintf (stderr, "listkey %d %d %s\n", which, depth, dkp->name);
159
160 if ( which == INORDER || which == LEAF )
161 {
162 dkp = *nodep;
163 while ( dkp ) /* loop through list */
164 {
165 if ( labellist == NULL || isinlist (dkp->name, labellist) )
166 printkeyinfo (dkp, oldpath); /* print entry */
167 oldpath = dkp->dname;
168 dkp = dkp->next;
169 }
170 }
171 }
172 #endif
173
zkt_list_keys(const dki_t * data)174 void zkt_list_keys (const dki_t *data)
175 {
176 #if ! defined(USE_TREE) || !USE_TREE
177 const dki_t *dkp;
178 const char *oldpath;
179 #endif
180
181 if ( data ) /* print headline if list is not empty */
182 printkeyinfo (NULL, "");
183
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 */
189 {
190 if ( labellist == NULL || isinlist (dkp->name, labellist) )
191 printkeyinfo (dkp, oldpath); /* print entry */
192 oldpath = dkp->dname;
193 }
194 #endif
195 }
196
197 #if defined(USE_TREE) && USE_TREE
198 # if 0
199 static void list_trustedkey (const dki_t **nodep, const VISIT which, int depth)
200 {
201 const dki_t *dkp;
202
203 if ( nodep == NULL )
204 return;
205
206 dkp = *nodep;
207 if ( which == INORDER || which == LEAF )
208 {
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 )
212 {
213 if ( (dki_isksk (dkp) || zskflag) &&
214 (labellist == NULL || isinlist (dkp->name, labellist)) )
215 dki_prt_trustedkey (dkp, stdout);
216 dkp = dkp->next;
217 }
218 }
219 }
220 # else
221 const dki_t *parent;
list_trustedkey(const dki_t ** nodep,const VISIT which,int depth)222 static void list_trustedkey (const dki_t **nodep, const VISIT which, int depth)
223 {
224 const dki_t *dkp;
225
226 if ( nodep == NULL )
227 return;
228
229 dkp = *nodep;
230 if ( which == INORDER || which == LEAF )
231 {
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;
235
236 if ( parent == NULL || !issubdomain (dkp->name, parent->name) )
237 {
238 parent = dkp;
239 /* loop through list */
240 while ( dkp )
241 {
242 if ( (dki_isksk (dkp) || zskflag) )
243 dki_prt_trustedkey (dkp, stdout);
244 dkp = dkp->next;
245 }
246 }
247 }
248 }
list_managedkey(const dki_t ** nodep,const VISIT which,int depth)249 static void list_managedkey (const dki_t **nodep, const VISIT which, int depth)
250 {
251 const dki_t *dkp;
252
253 if ( nodep == NULL )
254 return;
255
256 dkp = *nodep;
257 if ( which == INORDER || which == LEAF )
258 {
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;
262
263 if ( parent == NULL || !issubdomain (dkp->name, parent->name) )
264 {
265 const dki_t *dkp_head = NULL;
266 const dki_t *standby = NULL;
267
268 parent = dkp;
269
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;
275
276 if ( !standby ) /* no standby key found ? */
277 return;
278
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);
283 }
284 }
285 }
286 # endif
287 #endif
288
zkt_list_trustedkeys(const dki_t * data)289 void zkt_list_trustedkeys (const dki_t *data)
290 {
291
292 /* print headline if list is not empty */
293 if ( data && headerflag )
294 printf ("trusted-keys {\n");
295
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
304
305 /* print end of trusted-key section */
306 if ( data && headerflag )
307 printf ("};\n");
308 }
309
zkt_list_managedkeys(const dki_t * data)310 void zkt_list_managedkeys (const dki_t *data)
311 {
312
313 /* print headline if list is not empty */
314 if ( data && headerflag )
315 printf ("managed-keys {\n");
316
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
325
326 /* print end of trusted-key section */
327 if ( data && headerflag )
328 printf ("};\n");
329 }
330
331 #if defined(USE_TREE) && USE_TREE
list_dnskey(const dki_t ** nodep,const VISIT which,int depth)332 static void list_dnskey (const dki_t **nodep, const VISIT which, int depth)
333 {
334 const dki_t *dkp;
335 int ksk;
336
337 if ( nodep == NULL )
338 return;
339
340 if ( which == INORDER || which == LEAF )
341 for ( dkp = *nodep; dkp; dkp = dkp->next )
342 {
343 ksk = dki_isksk (dkp);
344 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
345 continue;
346
347 if ( labellist == NULL || isinlist (dkp->name, labellist) )
348 {
349 if ( headerflag )
350 dki_prt_comment (dkp, stdout);
351 dki_prt_dnskey (dkp, stdout);
352 }
353 }
354 }
355 #endif
356
zkt_list_dnskeys(const dki_t * data)357 void zkt_list_dnskeys (const dki_t *data)
358 {
359 #if defined(USE_TREE) && USE_TREE
360 twalk (data, list_dnskey);
361 #else
362 const dki_t *dkp;
363 int ksk;
364
365 for ( dkp = data; dkp; dkp = dkp->next )
366 {
367 ksk = dki_isksk (dkp);
368 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
369 continue;
370
371 if ( labellist == NULL || isinlist (dkp->name, labellist) )
372 {
373 if ( headerflag )
374 dki_prt_comment (dkp, stdout);
375 dki_prt_dnskey (dkp, stdout);
376 }
377 }
378 #endif
379 }
380
381 #if defined(USE_TREE) && USE_TREE
set_keylifetime(const dki_t ** nodep,const VISIT which,int depth)382 static void set_keylifetime (const dki_t **nodep, const VISIT which, int depth)
383 {
384 const dki_t *dkp;
385 int ksk;
386
387 if ( nodep == NULL )
388 return;
389
390 if ( which == INORDER || which == LEAF )
391 for ( dkp = *nodep; dkp; dkp = dkp->next )
392 {
393 ksk = dki_isksk (dkp);
394 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
395 continue;
396
397 if ( labellist == NULL || isinlist (dkp->name, labellist) )
398 dki_setlifetime ((dki_t *)dkp, lifetime);
399 }
400 }
401 #endif
402
zkt_setkeylifetime(dki_t * data)403 void zkt_setkeylifetime (dki_t *data)
404 {
405 #if defined(USE_TREE) && USE_TREE
406 twalk (data, set_keylifetime);
407 #else
408 dki_t *dkp;
409 int ksk;
410
411 for ( dkp = data; dkp; dkp = dkp->next )
412 {
413 ksk = dki_isksk (dkp);
414 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
415 continue;
416
417 if ( labellist == NULL || isinlist (dkp->name, labellist) )
418 {
419 dki_setlifetime (dkp, lifetime);
420 }
421 }
422 #endif
423 }
424
425
426 #if defined(USE_TREE) && USE_TREE
427 static const dki_t *searchresult;
428 static int searchitem;
tag_search(const dki_t ** nodep,const VISIT which,int depth)429 static void tag_search (const dki_t **nodep, const VISIT which, int depth)
430 {
431 const dki_t *dkp;
432
433 if ( nodep == NULL )
434 return;
435
436 if ( which == PREORDER || which == LEAF )
437 for ( dkp = *nodep; dkp; dkp = dkp->next )
438 {
439 if ( dkp->tag == searchitem )
440 {
441 if ( searchresult == NULL )
442 searchresult = dkp;
443 else
444 searchitem = 0;
445 }
446 }
447 }
448 #endif
zkt_search(const dki_t * data,int searchtag,const char * keyname)449 const dki_t *zkt_search (const dki_t *data, int searchtag, const char *keyname)
450 {
451 const dki_t *dkp = NULL;
452
453 #if defined(USE_TREE) && USE_TREE
454 if ( keyname == NULL || *keyname == '\0' )
455 {
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;
463 }
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;
470 }
471
472