1 /* $NetBSD: zone.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
5 ** @(#) zone.c (c) Mar 2005 Holger Zuleger hznet.de
7 ** Copyright (c) Mar 2005, 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 *****************************************************************/
42 # include <sys/types.h>
43 # include <sys/stat.h>
49 # include "config_zkt.h"
51 # include "domaincmp.h"
59 /*****************************************************************
60 ** private (static) function declaration and definition
61 *****************************************************************/
62 static char zone_estr
[255+1];
64 /*****************************************************************
66 *****************************************************************/
67 static zone_t
*zone_alloc ()
71 if ( (zp
= malloc (sizeof (zone_t
))) )
73 memset (zp
, 0, sizeof (zone_t
));
77 snprintf (zone_estr
, sizeof (zone_estr
),
78 "zone_alloc: Out of memory");
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 /*****************************************************************
100 *****************************************************************/
101 void zone_free (zone_t
*zp
)
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
);
110 /* TODO: actually there are some problems freeing the config :-( */
111 if ( zp
->conf
) free ((zconf_t
*)zp
->conf
);
113 if ( zp
->keys
) dki_freelist (&zp
->keys
);
117 /*****************************************************************
119 *****************************************************************/
120 void zone_freelist (zone_t
**listp
)
125 assert (listp
!= NULL
);
138 /*****************************************************************
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];
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' )
154 if ( file
== NULL
|| *file
== '\0' )
157 { /* check if file contains a path */
159 if ( (p
= strrchr (file
, '/')) != NULL
)
161 snprintf (path
, sizeof (path
), "%s/%.*s", dir
, (int)(p
-file
), file
);
167 if ( (new = zone_alloc ()) != NULL
)
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
);
182 snprintf (path
, sizeof (path
), "%s%s", file
, signed_ext
);
183 new->sfile
= strdup (path
);
187 dki_readdir (new->dir
, &new->keys
, 0);
191 return zone_add (zp
, new);
194 /*****************************************************************
196 *****************************************************************/
197 int zone_readdir (const char *dir
, const char *zone
, const char *zfile
, zone_t
**listp
, const zconf_t
*conf
, int dyn_zone
)
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 */
214 if ( zone
== NULL
) /* zone name still null ? */
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
);
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
);
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() */
242 zfile
= conf
->zonefile
;
243 pathname (path
, sizeof (path
), dir
, zfile
, signed_ext
);
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
);
251 pathname (path
, sizeof (path
), dir
, zfile
, NULL
);
254 dbg_val1("zone_readdir: fileexist (%s): ", path
);
255 if ( !fileexist (path
) ) /* no .signed file found ? ... */
258 return 0; /* ... not a secure zone ! */
262 dbg_val("zone_readdir: add zone (%s)\n", zone
);
263 zone_new (listp
, zone
, dir
, zfile
, signed_ext
, conf
);
269 /*****************************************************************
271 ** return error string
272 *****************************************************************/
273 const char *zone_geterrstr ()
278 /*****************************************************************
280 *****************************************************************/
281 zone_t
*zone_add (zone_t
**list
, zone_t
*new)
292 while ( curr
&& zone_cmp (curr
, new) < 0 )
298 if ( curr
== *list
) /* add node at the begining of the list */
300 else /* add node at end or between two nodes */
307 /*****************************************************************
309 *****************************************************************/
310 const zone_t
*zone_search (const zone_t
*list
, const char *zone
)
312 if ( zone
== NULL
|| *zone
== '\0' )
315 while ( list
&& strcmp (zone
, list
->zone
) != 0 )
321 /*****************************************************************
323 *****************************************************************/
324 int zone_print (const char *mesg
, const zone_t
*z
)
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
);