1 /* $NetBSD: dlz_stub_driver.c,v 1.5 2014/12/10 04:37:55 christos Exp $ */
2
3 /*
4 * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the
8 * above copyright notice and this permission notice appear in all
9 * copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
12 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14 * STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
17 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
18 * USE OR PERFORMANCE OF THIS SOFTWARE.
19 *
20 * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
21 * conceived and contributed by Rob Butler.
22 *
23 * Permission to use, copy, modify, and distribute this software for any
24 * purpose with or without fee is hereby granted, provided that the
25 * above copyright notice and this permission notice appear in all
26 * copies.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
29 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
31 * ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
32 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
33 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
34 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
35 * USE OR PERFORMANCE OF THIS SOFTWARE.
36 */
37
38 /*
39 * Copyright (C) 1999-2001 Internet Software Consortium.
40 *
41 * Permission to use, copy, modify, and distribute this software for any
42 * purpose with or without fee is hereby granted, provided that the above
43 * copyright notice and this permission notice appear in all copies.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
46 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
48 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
49 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
50 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
51 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
52 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53 */
54
55 #ifdef DLZ_STUB
56
57 #include <config.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <stdlib.h>
61
62 #include <dns/log.h>
63 #include <dns/sdlz.h>
64 #include <dns/result.h>
65
66 #include <isc/mem.h>
67 #include <isc/print.h>
68 #include <isc/result.h>
69 #include <isc/util.h>
70
71 #include <named/globals.h>
72
73 #include <dlz/dlz_stub_driver.h>
74
75 static dns_sdlzimplementation_t *dlz_stub = NULL;
76
77 typedef struct config_data {
78 char *myzone;
79 char *myname;
80 char *myip;
81 isc_mem_t *mctx;
82 } config_data_t;
83
84 /*
85 * SDLZ methods
86 */
87
88 static isc_result_t
stub_dlz_allnodes(const char * zone,void * driverarg,void * dbdata,dns_sdlzallnodes_t * allnodes)89 stub_dlz_allnodes(const char *zone, void *driverarg, void *dbdata,
90 dns_sdlzallnodes_t *allnodes)
91 {
92 config_data_t *cd;
93 isc_result_t result;
94
95 UNUSED(zone);
96 UNUSED(driverarg);
97
98 cd = (config_data_t *) dbdata;
99
100 result = dns_sdlz_putnamedrr(allnodes, cd->myname, "soa", 86400,
101 "web root.localhost. "
102 "0 28800 7200 604800 86400");
103 if (result != ISC_R_SUCCESS)
104 return (ISC_R_FAILURE);
105 result = dns_sdlz_putnamedrr(allnodes, "ns", "ns", 86400, cd->myname);
106 if (result != ISC_R_SUCCESS)
107 return (ISC_R_FAILURE);
108 result = dns_sdlz_putnamedrr(allnodes, cd->myname, "a", 1, cd->myip);
109 if (result != ISC_R_SUCCESS)
110 return (ISC_R_FAILURE);
111 return (ISC_R_SUCCESS);
112 }
113
114 static isc_result_t
stub_dlz_allowzonexfr(void * driverarg,void * dbdata,const char * name,const char * client)115 stub_dlz_allowzonexfr(void *driverarg, void *dbdata, const char *name,
116 const char *client)
117 {
118 UNUSED(driverarg);
119 UNUSED(dbdata);
120 UNUSED(name);
121 UNUSED(client);
122 return ISC_R_SUCCESS;
123 }
124
125 static isc_result_t
stub_dlz_authority(const char * zone,void * driverarg,void * dbdata,dns_sdlzlookup_t * lookup)126 stub_dlz_authority(const char *zone, void *driverarg, void *dbdata,
127 dns_sdlzlookup_t *lookup)
128 {
129 isc_result_t result;
130 config_data_t *cd;
131
132 UNUSED(driverarg);
133
134 cd = (config_data_t *) dbdata;
135
136 if (strcmp(zone, cd->myzone) == 0) {
137 result = dns_sdlz_putsoa(lookup, cd->myname,
138 "root.localhost.", 0);
139 if (result != ISC_R_SUCCESS)
140 return (ISC_R_FAILURE);
141
142 result = dns_sdlz_putrr(lookup, "ns", 86400, cd->myname);
143 if (result != ISC_R_SUCCESS)
144 return (ISC_R_FAILURE);
145
146 return (ISC_R_SUCCESS);
147 }
148 return (ISC_R_NOTFOUND);
149 }
150
151 static isc_result_t
stub_dlz_findzonedb(void * driverarg,void * dbdata,const char * name,dns_clientinfomethods_t * methods,dns_clientinfo_t * clientinfo)152 stub_dlz_findzonedb(void *driverarg, void *dbdata, const char *name,
153 dns_clientinfomethods_t *methods,
154 dns_clientinfo_t *clientinfo)
155 {
156
157 config_data_t *cd;
158
159 UNUSED(driverarg);
160 UNUSED(methods);
161 UNUSED(clientinfo);
162
163 cd = (config_data_t *) dbdata;
164
165 /* Write info message to log */
166 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
167 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
168 "dlz_stub findzone looking for '%s'", name);
169
170 if (strcmp(cd->myzone, name) == 0)
171 return (ISC_R_SUCCESS);
172 else
173 return (ISC_R_NOTFOUND);
174 }
175
176
177 static isc_result_t
stub_dlz_lookup(const char * zone,const char * name,void * driverarg,void * dbdata,dns_sdlzlookup_t * lookup,dns_clientinfomethods_t * methods,dns_clientinfo_t * clientinfo)178 stub_dlz_lookup(const char *zone, const char *name, void *driverarg,
179 void *dbdata, dns_sdlzlookup_t *lookup,
180 dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo)
181 {
182 isc_result_t result;
183 config_data_t *cd;
184
185 UNUSED(zone);
186 UNUSED(driverarg);
187 UNUSED(methods);
188 UNUSED(clientinfo);
189
190 cd = (config_data_t *) dbdata;
191
192 if (strcmp(name, cd->myname) == 0) {
193 result = dns_sdlz_putrr(lookup, "a", 1, cd->myip);
194 if (result != ISC_R_SUCCESS)
195 return (ISC_R_FAILURE);
196
197 return (ISC_R_SUCCESS);
198 }
199 return (ISC_R_FAILURE);
200
201 }
202
203
204 static isc_result_t
stub_dlz_create(const char * dlzname,unsigned int argc,char * argv[],void * driverarg,void ** dbdata)205 stub_dlz_create(const char *dlzname, unsigned int argc, char *argv[],
206 void *driverarg, void **dbdata)
207 {
208
209 config_data_t *cd;
210
211 UNUSED(driverarg);
212
213 if (argc < 4)
214 return (ISC_R_FAILURE);
215 /*
216 * Write info message to log
217 */
218 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
219 DNS_LOGMODULE_DLZ, ISC_LOG_INFO,
220 "Loading '%s' using DLZ_stub driver. "
221 "Zone: %s, Name: %s IP: %s",
222 dlzname, argv[1], argv[2], argv[3]);
223
224 cd = isc_mem_get(ns_g_mctx, sizeof(config_data_t));
225 if ((cd) == NULL) {
226 return (ISC_R_NOMEMORY);
227 }
228
229 memset(cd, 0, sizeof(config_data_t));
230
231 cd->myzone = isc_mem_strdup(ns_g_mctx, argv[1]);
232 if (cd->myzone == NULL) {
233 isc_mem_put(ns_g_mctx, cd, sizeof(config_data_t));
234 return (ISC_R_NOMEMORY);
235 }
236
237 cd->myname = isc_mem_strdup(ns_g_mctx, argv[2]);
238 if (cd->myname == NULL) {
239 isc_mem_put(ns_g_mctx, cd, sizeof(config_data_t));
240 isc_mem_free(ns_g_mctx, cd->myzone);
241 return (ISC_R_NOMEMORY);
242 }
243
244 cd->myip = isc_mem_strdup(ns_g_mctx, argv[3]);
245 if (cd->myip == NULL) {
246 isc_mem_put(ns_g_mctx, cd, sizeof(config_data_t));
247 isc_mem_free(ns_g_mctx, cd->myname);
248 isc_mem_free(ns_g_mctx, cd->myzone);
249 return (ISC_R_NOMEMORY);
250 }
251
252 isc_mem_attach(ns_g_mctx, &cd->mctx);
253
254 *dbdata = cd;
255
256 return(ISC_R_SUCCESS);
257 }
258
259 static void
stub_dlz_destroy(void * driverarg,void * dbdata)260 stub_dlz_destroy(void *driverarg, void *dbdata)
261 {
262 config_data_t *cd;
263 isc_mem_t *mctx;
264
265 UNUSED(driverarg);
266
267 cd = (config_data_t *) dbdata;
268
269 /*
270 * Write debugging message to log
271 */
272 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
273 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
274 "Unloading DLZ_stub driver.");
275
276 isc_mem_free(ns_g_mctx, cd->myzone);
277 isc_mem_free(ns_g_mctx, cd->myname);
278 isc_mem_free(ns_g_mctx, cd->myip);
279 mctx = cd->mctx;
280 isc_mem_put(mctx, cd, sizeof(config_data_t));
281 isc_mem_detach(&mctx);
282 }
283
284 static dns_sdlzmethods_t dlz_stub_methods = {
285 stub_dlz_create,
286 stub_dlz_destroy,
287 stub_dlz_findzonedb,
288 stub_dlz_lookup,
289 stub_dlz_authority,
290 stub_dlz_allnodes,
291 stub_dlz_allowzonexfr,
292 NULL,
293 NULL,
294 NULL,
295 NULL,
296 NULL,
297 NULL,
298 NULL,
299 };
300
301 /*%
302 * Wrapper around dns_sdlzregister().
303 */
304 isc_result_t
dlz_stub_init(void)305 dlz_stub_init(void) {
306 isc_result_t result;
307
308 /*
309 * Write debugging message to log
310 */
311 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
312 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
313 "Registering DLZ_stub driver.");
314
315 result = dns_sdlzregister("dlz_stub", &dlz_stub_methods, NULL,
316 DNS_SDLZFLAG_RELATIVEOWNER |
317 DNS_SDLZFLAG_RELATIVERDATA,
318 ns_g_mctx, &dlz_stub);
319 if (result != ISC_R_SUCCESS) {
320 UNEXPECTED_ERROR(__FILE__, __LINE__,
321 "dns_sdlzregister() failed: %s",
322 isc_result_totext(result));
323 result = ISC_R_UNEXPECTED;
324 }
325
326
327 return result;
328 }
329
330 /*
331 * Wrapper around dns_sdlzunregister().
332 */
333 void
dlz_stub_clear(void)334 dlz_stub_clear(void) {
335
336 /*
337 * Write debugging message to log
338 */
339 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
340 DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
341 "Unregistering DLZ_stub driver.");
342
343 if (dlz_stub != NULL)
344 dns_sdlzunregister(&dlz_stub);
345 }
346
347 #endif
348