dyndb.c revision 1.1 1 1.1 christos /* $NetBSD: dyndb.c,v 1.1 2018/08/12 12:08:14 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.1 christos *
10 1.1 christos * See the COPYRIGHT file distributed with this work for additional
11 1.1 christos * information regarding copyright ownership.
12 1.1 christos */
13 1.1 christos
14 1.1 christos
15 1.1 christos #include <config.h>
16 1.1 christos
17 1.1 christos #if HAVE_DLFCN_H
18 1.1 christos #include <dlfcn.h>
19 1.1 christos #elif _WIN32
20 1.1 christos #include <windows.h>
21 1.1 christos #endif
22 1.1 christos
23 1.1 christos #include <isc/buffer.h>
24 1.1 christos #include <isc/mem.h>
25 1.1 christos #include <isc/mutex.h>
26 1.1 christos #include <isc/once.h>
27 1.1 christos #include <isc/result.h>
28 1.1 christos #include <isc/region.h>
29 1.1 christos #include <isc/task.h>
30 1.1 christos #include <isc/types.h>
31 1.1 christos #include <isc/util.h>
32 1.1 christos
33 1.1 christos #include <dns/dyndb.h>
34 1.1 christos #include <dns/log.h>
35 1.1 christos #include <dns/types.h>
36 1.1 christos #include <dns/view.h>
37 1.1 christos #include <dns/zone.h>
38 1.1 christos
39 1.1 christos #include <string.h>
40 1.1 christos
41 1.1 christos #define CHECK(op) \
42 1.1 christos do { result = (op); \
43 1.1 christos if (result != ISC_R_SUCCESS) goto cleanup; \
44 1.1 christos } while (0)
45 1.1 christos
46 1.1 christos
47 1.1 christos typedef struct dyndb_implementation dyndb_implementation_t;
48 1.1 christos struct dyndb_implementation {
49 1.1 christos isc_mem_t *mctx;
50 1.1 christos void *handle;
51 1.1 christos dns_dyndb_register_t *register_func;
52 1.1 christos dns_dyndb_destroy_t *destroy_func;
53 1.1 christos char *name;
54 1.1 christos void *inst;
55 1.1 christos LINK(dyndb_implementation_t) link;
56 1.1 christos };
57 1.1 christos
58 1.1 christos /*
59 1.1 christos * List of dyndb implementations. Locked by dyndb_lock.
60 1.1 christos *
61 1.1 christos * These are stored here so they can be cleaned up on shutdown.
62 1.1 christos * (The order in which they are stored is not important.)
63 1.1 christos */
64 1.1 christos static LIST(dyndb_implementation_t) dyndb_implementations;
65 1.1 christos
66 1.1 christos /* Locks dyndb_implementations. */
67 1.1 christos static isc_mutex_t dyndb_lock;
68 1.1 christos static isc_once_t once = ISC_ONCE_INIT;
69 1.1 christos
70 1.1 christos static void
71 1.1 christos dyndb_initialize(void) {
72 1.1 christos RUNTIME_CHECK(isc_mutex_init(&dyndb_lock) == ISC_R_SUCCESS);
73 1.1 christos INIT_LIST(dyndb_implementations);
74 1.1 christos }
75 1.1 christos
76 1.1 christos static dyndb_implementation_t *
77 1.1 christos impfind(const char *name) {
78 1.1 christos dyndb_implementation_t *imp;
79 1.1 christos
80 1.1 christos for (imp = ISC_LIST_HEAD(dyndb_implementations);
81 1.1 christos imp != NULL;
82 1.1 christos imp = ISC_LIST_NEXT(imp, link))
83 1.1 christos if (strcasecmp(name, imp->name) == 0)
84 1.1 christos return (imp);
85 1.1 christos return (NULL);
86 1.1 christos }
87 1.1 christos
88 1.1 christos #if HAVE_DLFCN_H && HAVE_DLOPEN
89 1.1 christos static isc_result_t
90 1.1 christos load_symbol(void *handle, const char *filename,
91 1.1 christos const char *symbol_name, void **symbolp)
92 1.1 christos {
93 1.1 christos const char *errmsg;
94 1.1 christos void *symbol;
95 1.1 christos
96 1.1 christos REQUIRE(handle != NULL);
97 1.1 christos REQUIRE(symbolp != NULL && *symbolp == NULL);
98 1.1 christos
99 1.1 christos symbol = dlsym(handle, symbol_name);
100 1.1 christos if (symbol == NULL) {
101 1.1 christos errmsg = dlerror();
102 1.1 christos if (errmsg == NULL)
103 1.1 christos errmsg = "returned function pointer is NULL";
104 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
105 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
106 1.1 christos "failed to lookup symbol %s in "
107 1.1 christos "dyndb module '%s': %s",
108 1.1 christos symbol_name, filename, errmsg);
109 1.1 christos return (ISC_R_FAILURE);
110 1.1 christos }
111 1.1 christos dlerror();
112 1.1 christos
113 1.1 christos *symbolp = symbol;
114 1.1 christos
115 1.1 christos return (ISC_R_SUCCESS);
116 1.1 christos }
117 1.1 christos
118 1.1 christos static isc_result_t
119 1.1 christos load_library(isc_mem_t *mctx, const char *filename, const char *instname,
120 1.1 christos dyndb_implementation_t **impp)
121 1.1 christos {
122 1.1 christos isc_result_t result;
123 1.1 christos void *handle = NULL;
124 1.1 christos dyndb_implementation_t *imp = NULL;
125 1.1 christos dns_dyndb_register_t *register_func = NULL;
126 1.1 christos dns_dyndb_destroy_t *destroy_func = NULL;
127 1.1 christos dns_dyndb_version_t *version_func = NULL;
128 1.1 christos int version, flags;
129 1.1 christos
130 1.1 christos REQUIRE(impp != NULL && *impp == NULL);
131 1.1 christos
132 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
133 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_INFO,
134 1.1 christos "loading DynDB instance '%s' driver '%s'",
135 1.1 christos instname, filename);
136 1.1 christos
137 1.1 christos flags = RTLD_NOW|RTLD_LOCAL;
138 1.1 christos #ifdef RTLD_DEEPBIND
139 1.1 christos flags |= RTLD_DEEPBIND;
140 1.1 christos #endif
141 1.1 christos
142 1.1 christos handle = dlopen(filename, flags);
143 1.1 christos if (handle == NULL)
144 1.1 christos CHECK(ISC_R_FAILURE);
145 1.1 christos
146 1.1 christos /* Clear dlerror */
147 1.1 christos dlerror();
148 1.1 christos
149 1.1 christos CHECK(load_symbol(handle, filename, "dyndb_version",
150 1.1 christos (void **)&version_func));
151 1.1 christos
152 1.1 christos version = version_func(NULL);
153 1.1 christos if (version < (DNS_DYNDB_VERSION - DNS_DYNDB_AGE) ||
154 1.1 christos version > DNS_DYNDB_VERSION)
155 1.1 christos {
156 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
157 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
158 1.1 christos "driver API version mismatch: %d/%d",
159 1.1 christos version, DNS_DYNDB_VERSION);
160 1.1 christos CHECK(ISC_R_FAILURE);
161 1.1 christos }
162 1.1 christos
163 1.1 christos CHECK(load_symbol(handle, filename, "dyndb_init",
164 1.1 christos (void **)®ister_func));
165 1.1 christos CHECK(load_symbol(handle, filename, "dyndb_destroy",
166 1.1 christos (void **)&destroy_func));
167 1.1 christos
168 1.1 christos imp = isc_mem_get(mctx, sizeof(dyndb_implementation_t));
169 1.1 christos if (imp == NULL)
170 1.1 christos CHECK(ISC_R_NOMEMORY);
171 1.1 christos
172 1.1 christos imp->mctx = NULL;
173 1.1 christos isc_mem_attach(mctx, &imp->mctx);
174 1.1 christos imp->handle = handle;
175 1.1 christos imp->register_func = register_func;
176 1.1 christos imp->destroy_func = destroy_func;
177 1.1 christos imp->name = isc_mem_strdup(mctx, instname);
178 1.1 christos if (imp->name == NULL)
179 1.1 christos CHECK(ISC_R_NOMEMORY);
180 1.1 christos
181 1.1 christos imp->inst = NULL;
182 1.1 christos INIT_LINK(imp, link);
183 1.1 christos
184 1.1 christos *impp = imp;
185 1.1 christos imp = NULL;
186 1.1 christos
187 1.1 christos cleanup:
188 1.1 christos if (result != ISC_R_SUCCESS)
189 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
190 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
191 1.1 christos "failed to dynamically load instance '%s' "
192 1.1 christos "driver '%s': %s (%s)", instname, filename,
193 1.1 christos dlerror(), isc_result_totext(result));
194 1.1 christos if (imp != NULL)
195 1.1 christos isc_mem_putanddetach(&imp->mctx, imp, sizeof(dyndb_implementation_t));
196 1.1 christos if (result != ISC_R_SUCCESS && handle != NULL)
197 1.1 christos dlclose(handle);
198 1.1 christos
199 1.1 christos return (result);
200 1.1 christos }
201 1.1 christos
202 1.1 christos static void
203 1.1 christos unload_library(dyndb_implementation_t **impp) {
204 1.1 christos dyndb_implementation_t *imp;
205 1.1 christos
206 1.1 christos REQUIRE(impp != NULL && *impp != NULL);
207 1.1 christos
208 1.1 christos imp = *impp;
209 1.1 christos
210 1.1 christos isc_mem_free(imp->mctx, imp->name);
211 1.1 christos isc_mem_putanddetach(&imp->mctx, imp, sizeof(dyndb_implementation_t));
212 1.1 christos
213 1.1 christos *impp = NULL;
214 1.1 christos }
215 1.1 christos #elif _WIN32
216 1.1 christos static isc_result_t
217 1.1 christos load_symbol(HMODULE handle, const char *filename,
218 1.1 christos const char *symbol_name, void **symbolp)
219 1.1 christos {
220 1.1 christos void *symbol;
221 1.1 christos
222 1.1 christos REQUIRE(handle != NULL);
223 1.1 christos REQUIRE(symbolp != NULL && *symbolp == NULL);
224 1.1 christos
225 1.1 christos symbol = GetProcAddress(handle, symbol_name);
226 1.1 christos if (symbol == NULL) {
227 1.1 christos int errstatus = GetLastError();
228 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
229 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
230 1.1 christos "failed to lookup symbol %s in "
231 1.1 christos "dyndb module '%s': %d",
232 1.1 christos symbol_name, filename, errstatus);
233 1.1 christos return (ISC_R_FAILURE);
234 1.1 christos }
235 1.1 christos
236 1.1 christos *symbolp = symbol;
237 1.1 christos
238 1.1 christos return (ISC_R_SUCCESS);
239 1.1 christos }
240 1.1 christos
241 1.1 christos static isc_result_t
242 1.1 christos load_library(isc_mem_t *mctx, const char *filename, const char *instname,
243 1.1 christos dyndb_implementation_t **impp)
244 1.1 christos {
245 1.1 christos isc_result_t result;
246 1.1 christos HMODULE handle;
247 1.1 christos dyndb_implementation_t *imp = NULL;
248 1.1 christos dns_dyndb_register_t *register_func = NULL;
249 1.1 christos dns_dyndb_destroy_t *destroy_func = NULL;
250 1.1 christos dns_dyndb_version_t *version_func = NULL;
251 1.1 christos int version;
252 1.1 christos
253 1.1 christos REQUIRE(impp != NULL && *impp == NULL);
254 1.1 christos
255 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
256 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_INFO,
257 1.1 christos "loading DynDB instance '%s' driver '%s'",
258 1.1 christos instname, filename);
259 1.1 christos
260 1.1 christos handle = LoadLibraryA(filename);
261 1.1 christos if (handle == NULL)
262 1.1 christos CHECK(ISC_R_FAILURE);
263 1.1 christos
264 1.1 christos CHECK(load_symbol(handle, filename, "dyndb_version",
265 1.1 christos (void **)&version_func));
266 1.1 christos
267 1.1 christos version = version_func(NULL);
268 1.1 christos if (version < (DNS_DYNDB_VERSION - DNS_DYNDB_AGE) ||
269 1.1 christos version > DNS_DYNDB_VERSION)
270 1.1 christos {
271 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
272 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
273 1.1 christos "driver API version mismatch: %d/%d",
274 1.1 christos version, DNS_DYNDB_VERSION);
275 1.1 christos CHECK(ISC_R_FAILURE);
276 1.1 christos }
277 1.1 christos
278 1.1 christos CHECK(load_symbol(handle, filename, "dyndb_init",
279 1.1 christos (void **)®ister_func));
280 1.1 christos CHECK(load_symbol(handle, filename, "dyndb_destroy",
281 1.1 christos (void **)&destroy_func));
282 1.1 christos
283 1.1 christos imp = isc_mem_get(mctx, sizeof(dyndb_implementation_t));
284 1.1 christos if (imp == NULL)
285 1.1 christos CHECK(ISC_R_NOMEMORY);
286 1.1 christos
287 1.1 christos imp->mctx = NULL;
288 1.1 christos isc_mem_attach(mctx, &imp->mctx);
289 1.1 christos imp->handle = handle;
290 1.1 christos imp->register_func = register_func;
291 1.1 christos imp->destroy_func = destroy_func;
292 1.1 christos imp->name = isc_mem_strdup(mctx, instname);
293 1.1 christos if (imp->name == NULL)
294 1.1 christos CHECK(ISC_R_NOMEMORY);
295 1.1 christos
296 1.1 christos imp->inst = NULL;
297 1.1 christos INIT_LINK(imp, link);
298 1.1 christos
299 1.1 christos *impp = imp;
300 1.1 christos imp = NULL;
301 1.1 christos
302 1.1 christos cleanup:
303 1.1 christos if (result != ISC_R_SUCCESS)
304 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
305 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
306 1.1 christos "failed to dynamically load instance '%s' "
307 1.1 christos "driver '%s': %d (%s)", instname, filename,
308 1.1 christos GetLastError(), isc_result_totext(result));
309 1.1 christos if (imp != NULL)
310 1.1 christos isc_mem_putanddetach(&imp->mctx, imp, sizeof(dyndb_implementation_t));
311 1.1 christos if (result != ISC_R_SUCCESS && handle != NULL)
312 1.1 christos FreeLibrary(handle);
313 1.1 christos
314 1.1 christos return (result);
315 1.1 christos }
316 1.1 christos
317 1.1 christos static void
318 1.1 christos unload_library(dyndb_implementation_t **impp) {
319 1.1 christos dyndb_implementation_t *imp;
320 1.1 christos
321 1.1 christos REQUIRE(impp != NULL && *impp != NULL);
322 1.1 christos
323 1.1 christos imp = *impp;
324 1.1 christos
325 1.1 christos isc_mem_free(imp->mctx, imp->name);
326 1.1 christos isc_mem_putanddetach(&imp->mctx, imp, sizeof(dyndb_implementation_t));
327 1.1 christos
328 1.1 christos *impp = NULL;
329 1.1 christos }
330 1.1 christos #else /* HAVE_DLFCN_H || _WIN32 */
331 1.1 christos static isc_result_t
332 1.1 christos load_library(isc_mem_t *mctx, const char *filename, const char *instname,
333 1.1 christos dyndb_implementation_t **impp)
334 1.1 christos {
335 1.1 christos UNUSED(mctx);
336 1.1 christos UNUSED(filename);
337 1.1 christos UNUSED(instname);
338 1.1 christos UNUSED(impp);
339 1.1 christos
340 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DYNDB,
341 1.1 christos ISC_LOG_ERROR,
342 1.1 christos "dynamic database support is not implemented");
343 1.1 christos
344 1.1 christos return (ISC_R_NOTIMPLEMENTED);
345 1.1 christos }
346 1.1 christos
347 1.1 christos static void
348 1.1 christos unload_library(dyndb_implementation_t **impp)
349 1.1 christos {
350 1.1 christos UNUSED(impp);
351 1.1 christos }
352 1.1 christos #endif /* HAVE_DLFCN_H */
353 1.1 christos
354 1.1 christos isc_result_t
355 1.1 christos dns_dyndb_load(const char *libname, const char *name, const char *parameters,
356 1.1 christos const char *file, unsigned long line, isc_mem_t *mctx,
357 1.1 christos const dns_dyndbctx_t *dctx)
358 1.1 christos {
359 1.1 christos isc_result_t result;
360 1.1 christos dyndb_implementation_t *implementation = NULL;
361 1.1 christos
362 1.1 christos REQUIRE(DNS_DYNDBCTX_VALID(dctx));
363 1.1 christos REQUIRE(name != NULL);
364 1.1 christos
365 1.1 christos RUNTIME_CHECK(isc_once_do(&once, dyndb_initialize) == ISC_R_SUCCESS);
366 1.1 christos
367 1.1 christos LOCK(&dyndb_lock);
368 1.1 christos
369 1.1 christos /* duplicate instance names are not allowed */
370 1.1 christos if (impfind(name) != NULL)
371 1.1 christos CHECK(ISC_R_EXISTS);
372 1.1 christos
373 1.1 christos CHECK(load_library(mctx, libname, name, &implementation));
374 1.1 christos CHECK(implementation->register_func(mctx, name, parameters, file, line,
375 1.1 christos dctx, &implementation->inst));
376 1.1 christos
377 1.1 christos APPEND(dyndb_implementations, implementation, link);
378 1.1 christos result = ISC_R_SUCCESS;
379 1.1 christos
380 1.1 christos cleanup:
381 1.1 christos if (result != ISC_R_SUCCESS)
382 1.1 christos if (implementation != NULL)
383 1.1 christos unload_library(&implementation);
384 1.1 christos
385 1.1 christos UNLOCK(&dyndb_lock);
386 1.1 christos return (result);
387 1.1 christos }
388 1.1 christos
389 1.1 christos void
390 1.1 christos dns_dyndb_cleanup(isc_boolean_t exiting) {
391 1.1 christos dyndb_implementation_t *elem;
392 1.1 christos dyndb_implementation_t *prev;
393 1.1 christos
394 1.1 christos RUNTIME_CHECK(isc_once_do(&once, dyndb_initialize) == ISC_R_SUCCESS);
395 1.1 christos
396 1.1 christos LOCK(&dyndb_lock);
397 1.1 christos elem = TAIL(dyndb_implementations);
398 1.1 christos while (elem != NULL) {
399 1.1 christos prev = PREV(elem, link);
400 1.1 christos UNLINK(dyndb_implementations, elem, link);
401 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
402 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_INFO,
403 1.1 christos "unloading DynDB instance '%s'", elem->name);
404 1.1 christos elem->destroy_func(&elem->inst);
405 1.1 christos ENSURE(elem->inst == NULL);
406 1.1 christos unload_library(&elem);
407 1.1 christos elem = prev;
408 1.1 christos }
409 1.1 christos UNLOCK(&dyndb_lock);
410 1.1 christos
411 1.1 christos if (exiting == ISC_TRUE)
412 1.1 christos isc_mutex_destroy(&dyndb_lock);
413 1.1 christos }
414 1.1 christos
415 1.1 christos isc_result_t
416 1.1 christos dns_dyndb_createctx(isc_mem_t *mctx, const void *hashinit, isc_log_t *lctx,
417 1.1 christos dns_view_t *view, dns_zonemgr_t *zmgr, isc_task_t *task,
418 1.1 christos isc_timermgr_t *tmgr, dns_dyndbctx_t **dctxp)
419 1.1 christos {
420 1.1 christos dns_dyndbctx_t *dctx;
421 1.1 christos
422 1.1 christos REQUIRE(dctxp != NULL && *dctxp == NULL);
423 1.1 christos
424 1.1 christos dctx = isc_mem_get(mctx, sizeof(*dctx));
425 1.1 christos if (dctx == NULL)
426 1.1 christos return (ISC_R_NOMEMORY);
427 1.1 christos
428 1.1 christos memset(dctx, 0, sizeof(*dctx));
429 1.1 christos if (view != NULL)
430 1.1 christos dns_view_attach(view, &dctx->view);
431 1.1 christos if (zmgr != NULL)
432 1.1 christos dns_zonemgr_attach(zmgr, &dctx->zmgr);
433 1.1 christos if (task != NULL)
434 1.1 christos isc_task_attach(task, &dctx->task);
435 1.1 christos dctx->timermgr = tmgr;
436 1.1 christos dctx->hashinit = hashinit;
437 1.1 christos dctx->lctx = lctx;
438 1.1 christos dctx->refvar = &isc_bind9;
439 1.1 christos
440 1.1 christos isc_mem_attach(mctx, &dctx->mctx);
441 1.1 christos dctx->magic = DNS_DYNDBCTX_MAGIC;
442 1.1 christos
443 1.1 christos *dctxp = dctx;
444 1.1 christos
445 1.1 christos return (ISC_R_SUCCESS);
446 1.1 christos }
447 1.1 christos
448 1.1 christos void
449 1.1 christos dns_dyndb_destroyctx(dns_dyndbctx_t **dctxp) {
450 1.1 christos dns_dyndbctx_t *dctx;
451 1.1 christos
452 1.1 christos REQUIRE(dctxp != NULL && DNS_DYNDBCTX_VALID(*dctxp));
453 1.1 christos
454 1.1 christos dctx = *dctxp;
455 1.1 christos *dctxp = NULL;
456 1.1 christos
457 1.1 christos dctx->magic = 0;
458 1.1 christos
459 1.1 christos if (dctx->view != NULL)
460 1.1 christos dns_view_detach(&dctx->view);
461 1.1 christos if (dctx->zmgr != NULL)
462 1.1 christos dns_zonemgr_detach(&dctx->zmgr);
463 1.1 christos if (dctx->task != NULL)
464 1.1 christos isc_task_detach(&dctx->task);
465 1.1 christos dctx->timermgr = NULL;
466 1.1 christos dctx->lctx = NULL;
467 1.1 christos
468 1.1 christos isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(*dctx));
469 1.1 christos }
470