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