etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / zkt-1.1.3 / zone.c
blobc9e2dbe41d211205f6c356c9e19a04c3bbae5927
1 /* $NetBSD: zone.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
4 **
5 ** @(#) zone.c (c) Mar 2005 Holger Zuleger hznet.de
6 **
7 ** Copyright (c) Mar 2005, 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 # include <stdlib.h>
42 # include <sys/types.h>
43 # include <sys/stat.h>
44 # include <dirent.h>
45 # include <assert.h>
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif
49 # include "config_zkt.h"
50 # include "debug.h"
51 # include "domaincmp.h"
52 # include "misc.h"
53 # include "zconf.h"
54 # include "dki.h"
55 #define extern
56 # include "zone.h"
57 #undef extern
59 /*****************************************************************
60 ** private (static) function declaration and definition
61 *****************************************************************/
62 static char zone_estr[255+1];
64 /*****************************************************************
65 ** zone_alloc ()
66 *****************************************************************/
67 static zone_t *zone_alloc ()
69 zone_t *zp;
71 if ( (zp = malloc (sizeof (zone_t))) )
73 memset (zp, 0, sizeof (zone_t));
74 return zp;
77 snprintf (zone_estr, sizeof (zone_estr),
78 "zone_alloc: Out of memory");
79 return NULL;
82 /*****************************************************************
83 ** zone_cmp () return <0 | 0 | >0
84 *****************************************************************/
85 static int zone_cmp (const zone_t *a, const zone_t *b)
87 if ( a == NULL ) return -1;
88 if ( b == NULL ) return 1;
90 return domaincmp (a->zone, b->zone);
94 /*****************************************************************
95 ** public function definition
96 *****************************************************************/
98 /*****************************************************************
99 ** zone_free ()
100 *****************************************************************/
101 void zone_free (zone_t *zp)
103 assert (zp != NULL);
105 if ( zp->zone ) free ((char *)zp->zone);
106 if ( zp->dir ) free ((char *)zp->dir);
107 if ( zp->file ) free ((char *)zp->file);
108 if ( zp->sfile ) free ((char *)zp->sfile);
109 #if 0
110 /* TODO: actually there are some problems freeing the config :-( */
111 if ( zp->conf ) free ((zconf_t *)zp->conf);
112 #endif
113 if ( zp->keys ) dki_freelist (&zp->keys);
114 free (zp);
117 /*****************************************************************
118 ** zone_freelist ()
119 *****************************************************************/
120 void zone_freelist (zone_t **listp)
122 zone_t *curr;
123 zone_t *next;
125 assert (listp != NULL);
127 curr = *listp;
128 while ( curr )
130 next = curr->next;
131 zone_free (curr);
132 curr = next;
134 if ( *listp )
135 *listp = NULL;
138 /*****************************************************************
139 ** zone_new ()
140 ** allocate memory for new zone structure and initialize it
141 *****************************************************************/
142 zone_t *zone_new (zone_t **zp, const char *zone, const char *dir, const char *file, const char *signed_ext, const zconf_t *cp)
144 char path[MAX_PATHSIZE+1];
145 zone_t *new;
147 assert (zp != NULL);
148 assert (zone != NULL && *zone != '\0');
150 dbg_val3 ("zone_new: (zp, zone: %s, dir: %s, file: %s, cp)\n", zone, dir, file);
151 if ( dir == NULL || *dir == '\0' )
152 dir = ".";
154 if ( file == NULL || *file == '\0' )
155 file = cp->zonefile;
156 else
157 { /* check if file contains a path */
158 const char *p;
159 if ( (p = strrchr (file, '/')) != NULL )
161 snprintf (path, sizeof (path), "%s/%.*s", dir, (int)(p-file), file);
162 dir = path;
163 file = p+1;
167 if ( (new = zone_alloc ()) != NULL )
169 char *p;
171 new->zone = domain_canonicdup (zone);
172 new->dir = strdup (dir);
173 new->file = strdup (file);
174 /* check if file ends with ".signed" ? */
175 if ( (p = strrchr (new->file, '.')) != NULL && strcmp (p, signed_ext) == 0 )
177 new->sfile = strdup (new->file);
178 *p = '\0';
180 else
182 snprintf (path, sizeof (path), "%s%s", file, signed_ext);
183 new->sfile = strdup (path);
185 new->conf = cp;
186 new->keys = NULL;
187 dki_readdir (new->dir, &new->keys, 0);
188 new->next = NULL;
191 return zone_add (zp, new);
194 /*****************************************************************
195 ** zone_readdir ()
196 *****************************************************************/
197 int zone_readdir (const char *dir, const char *zone, const char *zfile, zone_t **listp, const zconf_t *conf, int dyn_zone)
199 char *p;
200 char path[MAX_PATHSIZE+1];
201 char *signed_ext = ".signed";
202 zconf_t *localconf = NULL;
204 assert (dir != NULL && *dir != '\0');
205 assert (conf != NULL);
207 if ( zone == NULL ) /* zone not given ? */
209 if ( (zone = strrchr (dir, '/')) ) /* try to extract zone name out of directory */
210 zone++;
211 else
212 zone = dir;
214 if ( zone == NULL ) /* zone name still null ? */
215 return 0;
217 dbg_val4 ("zone_readdir: (dir: \"%s\", zone: \"%s\", zfile: \"%s\", zp, cp, dyn_zone = %d)\n",
218 dir, zone, zfile ? zfile: "NULL", dyn_zone);
220 if ( dyn_zone )
221 signed_ext = ".dsigned";
223 if ( zfile && (p = strrchr (zfile, '/')) ) /* check if zfile contains a directory */
225 char subdir[MAX_PATHSIZE+1];
227 snprintf (subdir, sizeof (subdir), "%s/%.*s", dir, (int)(p - zfile), zfile);
228 pathname (path, sizeof (path), subdir, LOCALCONF_FILE, NULL);
230 else
231 pathname (path, sizeof (path), dir, LOCALCONF_FILE, NULL);
232 dbg_val1 ("zone_readdir: check local config file %s\n", path);
233 if ( fileexist (path) ) /* load local config file */
235 localconf = dupconfig (conf);
236 conf = loadconfig (path, localconf);
237 /* do not free localconf, because a ptr to it will be added to the zone by zone_new() */
240 if ( zfile == NULL )
242 zfile = conf->zonefile;
243 pathname (path, sizeof (path), dir, zfile, signed_ext);
245 else
247 dbg_val2("zone_readdir: add %s to zonefile if not already there ? (%s)\n", signed_ext, zfile);
248 if ( (p = strrchr (zfile, '.')) == NULL || strcmp (p, signed_ext) != 0 )
249 pathname (path, sizeof (path), dir, zfile, signed_ext);
250 else
251 pathname (path, sizeof (path), dir, zfile, NULL);
254 dbg_val1("zone_readdir: fileexist (%s): ", path);
255 if ( !fileexist (path) ) /* no .signed file found ? ... */
257 dbg_val0("no!\n");
258 return 0; /* ... not a secure zone ! */
260 dbg_val0("yes!\n");
262 dbg_val("zone_readdir: add zone (%s)\n", zone);
263 zone_new (listp, zone, dir, zfile, signed_ext, conf);
265 return 1;
269 /*****************************************************************
270 ** zone_geterrstr ()
271 ** return error string
272 *****************************************************************/
273 const char *zone_geterrstr ()
275 return zone_estr;
278 /*****************************************************************
279 ** zone_add ()
280 *****************************************************************/
281 zone_t *zone_add (zone_t **list, zone_t *new)
283 zone_t *curr;
284 zone_t *last;
286 if ( list == NULL )
287 return NULL;
288 if ( new == NULL )
289 return *list;
291 last = curr = *list;
292 while ( curr && zone_cmp (curr, new) < 0 )
294 last = curr;
295 curr = curr->next;
298 if ( curr == *list ) /* add node at the begining of the list */
299 *list = new;
300 else /* add node at end or between two nodes */
301 last->next = new;
302 new->next = curr;
304 return new;
307 /*****************************************************************
308 ** zone_search ()
309 *****************************************************************/
310 const zone_t *zone_search (const zone_t *list, const char *zone)
312 if ( zone == NULL || *zone == '\0' )
313 return NULL;
315 while ( list && strcmp (zone, list->zone) != 0 )
316 list = list->next;
318 return list;
321 /*****************************************************************
322 ** zone_print ()
323 *****************************************************************/
324 int zone_print (const char *mesg, const zone_t *z)
326 dki_t *dkp;
328 if ( !z )
329 return 0;
330 fprintf (stderr, "%s: zone\t %s\n", mesg, z->zone);
331 fprintf (stderr, "%s: dir\t %s\n", mesg, z->dir);
332 fprintf (stderr, "%s: file\t %s\n", mesg, z->file);
333 fprintf (stderr, "%s: sfile\t %s\n", mesg, z->sfile);
335 for ( dkp = z->keys; dkp; dkp = dkp->next )
337 dki_prt_comment (dkp, stderr);
340 return 1;