No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / contrib / zkt / zkt.c
blob176262d82f53239a8e1a430bdaa328f4f4c22779
1 /* $NetBSD$ */
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 #define extern
50 # include "zkt.h"
51 #undef extern
53 extern char *labellist;
54 extern int headerflag;
55 extern int timeflag;
56 extern int exptimeflag;
57 extern int lifetime;
58 extern int ageflag;
59 extern int lifetimeflag;
60 extern int kskflag;
61 extern int zskflag;
62 extern int pathflag;
63 extern int ljustflag;
65 static void printkeyinfo (const dki_t *dkp, const char *oldpath);
67 static void printkeyinfo (const dki_t *dkp, const char *oldpath)
69 time_t currtime;
71 if ( dkp == NULL ) /* print headline */
73 if ( headerflag )
75 printf ("%-33.33s %5s %3s %3.3s %-7s", "Keyname",
76 "Tag", "Typ", "Status", "Algorit");
77 if ( timeflag )
78 printf (" %-20s", "Generation Time");
79 if ( exptimeflag )
80 printf (" %-20s", "Expiration Time");
81 if ( ageflag )
82 printf (" %16s", "Age");
83 if ( lifetimeflag )
84 printf (" %4s", "LfTm");
85 putchar ('\n');
87 return;
89 time (&currtime);
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)) )
98 if ( ljustflag )
99 printf ("%-33.33s ", dkp->name);
100 else
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));
106 if ( timeflag )
107 printf (" %-20s", time2str (dkp->gentime ? dkp->gentime: dkp->time, 's'));
108 if ( exptimeflag )
109 printf (" %-20s", time2str (dkp->exptime, 's'));
110 if ( ageflag )
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) ? '<' : '!');
116 else
117 putchar (' ');
118 printf ("%hdd", dki_lifetimedays (dkp));
120 putchar ('\n');
124 #if defined(USE_TREE) && USE_TREE
125 static void list_key (const dki_t **nodep, const VISIT which, int depth)
127 const dki_t *dkp;
128 static const char *oldpath = "";
130 if ( nodep == NULL )
131 return;
132 //fprintf (stderr, "listkey %d %d %s\n", which, depth, dkp->name);
134 if ( which == INORDER || which == LEAF )
136 dkp = *nodep;
137 while ( dkp ) /* loop through list */
139 if ( labellist == NULL || isinlist (dkp->name, labellist) )
140 printkeyinfo (dkp, oldpath); /* print entry */
141 oldpath = dkp->dname;
142 dkp = dkp->next;
146 #endif
148 void zkt_list_keys (const dki_t *data)
150 #if ! defined(USE_TREE) || !USE_TREE
151 const dki_t *dkp;
152 const char *oldpath;
153 #endif
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);
160 #else
161 oldpath = "";
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;
168 #endif
171 #if defined(USE_TREE) && USE_TREE
172 static void list_trustedkey (const dki_t **nodep, const VISIT which, int depth)
174 const dki_t *dkp;
176 if ( nodep == NULL )
177 return;
179 dkp = *nodep;
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);
187 dkp = dkp->next;
190 #endif
192 void zkt_list_trustedkeys (const dki_t *data)
194 #if !defined(USE_TREE) || !USE_TREE
195 const dki_t *dkp;
196 #endif
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);
203 #else
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);
209 #endif
211 /* print end of trusted-key section */
212 if ( data && headerflag )
213 printf ("};\n");
216 #if defined(USE_TREE) && USE_TREE
217 static void list_dnskey (const dki_t **nodep, const VISIT which, int depth)
219 const dki_t *dkp;
220 int ksk;
222 if ( nodep == NULL )
223 return;
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) )
230 continue;
232 if ( labellist == NULL || isinlist (dkp->name, labellist) )
234 if ( headerflag )
235 dki_prt_comment (dkp, stdout);
236 dki_prt_dnskey (dkp, stdout);
240 #endif
242 void zkt_list_dnskeys (const dki_t *data)
244 #if defined(USE_TREE) && USE_TREE
245 twalk (data, list_dnskey);
246 #else
247 const dki_t *dkp;
248 int ksk;
250 for ( dkp = data; dkp; dkp = dkp->next )
252 ksk = dki_isksk (dkp);
253 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
254 continue;
256 if ( labellist == NULL || isinlist (dkp->name, labellist) )
258 if ( headerflag )
259 dki_prt_comment (dkp, stdout);
260 dki_prt_dnskey (dkp, stdout);
263 #endif
266 #if defined(USE_TREE) && USE_TREE
267 static void set_keylifetime (const dki_t **nodep, const VISIT which, int depth)
269 const dki_t *dkp;
270 int ksk;
272 if ( nodep == NULL )
273 return;
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) )
280 continue;
282 if ( labellist == NULL || isinlist (dkp->name, labellist) )
283 dki_setlifetime ((dki_t *)dkp, lifetime);
286 #endif
288 void zkt_setkeylifetime (dki_t *data)
290 #if defined(USE_TREE) && USE_TREE
291 twalk (data, set_keylifetime);
292 #else
293 dki_t *dkp;
294 int ksk;
296 for ( dkp = data; dkp; dkp = dkp->next )
298 ksk = dki_isksk (dkp);
299 if ( (ksk && !kskflag) || (!ksk && !zskflag) )
300 continue;
302 if ( labellist == NULL || isinlist (dkp->name, labellist) )
304 dki_setlifetime (dkp, lifetime);
307 #endif
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)
316 const dki_t *dkp;
318 if ( nodep == NULL )
319 return;
321 if ( which == PREORDER || which == LEAF )
322 for ( dkp = *nodep; dkp; dkp = dkp->next )
324 if ( dkp->tag == searchitem )
326 if ( searchresult == NULL )
327 searchresult = dkp;
328 else
329 searchitem = 0;
333 #endif
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' )
341 searchresult = NULL;
342 searchitem = searchtag;
343 twalk (data, tag_search);
344 if ( searchresult != NULL && searchitem == 0 )
345 dkp = (void *)01;
346 else
347 dkp = searchresult;
349 else
350 dkp = (dki_t*)dki_tsearch (data, searchtag, keyname);
351 #else
352 dkp = (dki_t*)dki_search (data, searchtag, keyname);
353 #endif
354 return dkp;