1 1.12 christos /* $NetBSD: dyndb.c,v 1.13 2026/01/29 18:37:49 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.5 christos #include <string.h> 17 1.1 christos 18 1.1 christos #include <isc/buffer.h> 19 1.1 christos #include <isc/mem.h> 20 1.1 christos #include <isc/mutex.h> 21 1.1 christos #include <isc/once.h> 22 1.5 christos #include <isc/region.h> 23 1.1 christos #include <isc/result.h> 24 1.1 christos #include <isc/types.h> 25 1.1 christos #include <isc/util.h> 26 1.12 christos #include <isc/uv.h> 27 1.1 christos 28 1.1 christos #include <dns/dyndb.h> 29 1.1 christos #include <dns/log.h> 30 1.1 christos #include <dns/types.h> 31 1.1 christos #include <dns/view.h> 32 1.1 christos #include <dns/zone.h> 33 1.1 christos 34 1.1 christos typedef struct dyndb_implementation dyndb_implementation_t; 35 1.1 christos struct dyndb_implementation { 36 1.5 christos isc_mem_t *mctx; 37 1.11 christos uv_lib_t handle; 38 1.5 christos dns_dyndb_register_t *register_func; 39 1.5 christos dns_dyndb_destroy_t *destroy_func; 40 1.5 christos char *name; 41 1.5 christos void *inst; 42 1.5 christos LINK(dyndb_implementation_t) link; 43 1.1 christos }; 44 1.1 christos 45 1.1 christos /* 46 1.1 christos * List of dyndb implementations. Locked by dyndb_lock. 47 1.1 christos * 48 1.1 christos * These are stored here so they can be cleaned up on shutdown. 49 1.1 christos * (The order in which they are stored is not important.) 50 1.1 christos */ 51 1.1 christos static LIST(dyndb_implementation_t) dyndb_implementations; 52 1.1 christos 53 1.1 christos /* Locks dyndb_implementations. */ 54 1.1 christos static isc_mutex_t dyndb_lock; 55 1.1 christos static isc_once_t once = ISC_ONCE_INIT; 56 1.1 christos 57 1.1 christos static void 58 1.1 christos dyndb_initialize(void) { 59 1.3 christos isc_mutex_init(&dyndb_lock); 60 1.1 christos INIT_LIST(dyndb_implementations); 61 1.1 christos } 62 1.1 christos 63 1.1 christos static dyndb_implementation_t * 64 1.1 christos impfind(const char *name) { 65 1.1 christos dyndb_implementation_t *imp; 66 1.1 christos 67 1.5 christos for (imp = ISC_LIST_HEAD(dyndb_implementations); imp != NULL; 68 1.1 christos imp = ISC_LIST_NEXT(imp, link)) 69 1.5 christos { 70 1.5 christos if (strcasecmp(name, imp->name) == 0) { 71 1.12 christos return imp; 72 1.5 christos } 73 1.5 christos } 74 1.12 christos return NULL; 75 1.1 christos } 76 1.1 christos 77 1.1 christos static isc_result_t 78 1.11 christos load_symbol(uv_lib_t *handle, const char *filename, const char *symbol_name, 79 1.5 christos void **symbolp) { 80 1.1 christos void *symbol; 81 1.11 christos int r; 82 1.1 christos 83 1.1 christos REQUIRE(handle != NULL); 84 1.1 christos REQUIRE(symbolp != NULL && *symbolp == NULL); 85 1.1 christos 86 1.11 christos r = uv_dlsym(handle, symbol_name, &symbol); 87 1.11 christos if (r != 0) { 88 1.11 christos const char *errmsg = uv_dlerror(handle); 89 1.5 christos if (errmsg == NULL) { 90 1.1 christos errmsg = "returned function pointer is NULL"; 91 1.5 christos } 92 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, 93 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR, 94 1.1 christos "failed to lookup symbol %s in " 95 1.11 christos "DynDB module '%s': %s", 96 1.1 christos symbol_name, filename, errmsg); 97 1.12 christos return ISC_R_FAILURE; 98 1.1 christos } 99 1.1 christos 100 1.1 christos *symbolp = symbol; 101 1.1 christos 102 1.12 christos return ISC_R_SUCCESS; 103 1.1 christos } 104 1.1 christos 105 1.11 christos static void 106 1.11 christos unload_library(dyndb_implementation_t **impp); 107 1.11 christos 108 1.1 christos static isc_result_t 109 1.1 christos load_library(isc_mem_t *mctx, const char *filename, const char *instname, 110 1.5 christos dyndb_implementation_t **impp) { 111 1.1 christos isc_result_t result; 112 1.1 christos dyndb_implementation_t *imp = NULL; 113 1.1 christos dns_dyndb_version_t *version_func = NULL; 114 1.9 christos int version; 115 1.11 christos int r; 116 1.1 christos 117 1.1 christos REQUIRE(impp != NULL && *impp == NULL); 118 1.1 christos 119 1.5 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DYNDB, 120 1.5 christos ISC_LOG_INFO, "loading DynDB instance '%s' driver '%s'", 121 1.1 christos instname, filename); 122 1.1 christos 123 1.11 christos imp = isc_mem_get(mctx, sizeof(*imp)); 124 1.12 christos *imp = (dyndb_implementation_t){ 125 1.12 christos .name = isc_mem_strdup(mctx, instname), 126 1.12 christos }; 127 1.12 christos 128 1.11 christos isc_mem_attach(mctx, &imp->mctx); 129 1.1 christos 130 1.1 christos INIT_LINK(imp, link); 131 1.1 christos 132 1.11 christos r = uv_dlopen(filename, &imp->handle); 133 1.11 christos if (r != 0) { 134 1.11 christos const char *errmsg = uv_dlerror(&imp->handle); 135 1.11 christos if (errmsg == NULL) { 136 1.11 christos errmsg = "unknown error"; 137 1.11 christos } 138 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, 139 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR, 140 1.11 christos "failed to dlopen() DynDB instance '%s' driver " 141 1.11 christos "'%s': %s", 142 1.11 christos instname, filename, errmsg); 143 1.1 christos CHECK(ISC_R_FAILURE); 144 1.5 christos } 145 1.1 christos 146 1.11 christos CHECK(load_symbol(&imp->handle, filename, "dyndb_version", 147 1.1 christos (void **)&version_func)); 148 1.1 christos 149 1.1 christos version = version_func(NULL); 150 1.1 christos if (version < (DNS_DYNDB_VERSION - DNS_DYNDB_AGE) || 151 1.1 christos version > DNS_DYNDB_VERSION) 152 1.1 christos { 153 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, 154 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR, 155 1.5 christos "driver API version mismatch: %d/%d", version, 156 1.5 christos DNS_DYNDB_VERSION); 157 1.1 christos CHECK(ISC_R_FAILURE); 158 1.1 christos } 159 1.1 christos 160 1.11 christos CHECK(load_symbol(&imp->handle, filename, "dyndb_init", 161 1.11 christos (void **)&imp->register_func)); 162 1.11 christos CHECK(load_symbol(&imp->handle, filename, "dyndb_destroy", 163 1.11 christos (void **)&imp->destroy_func)); 164 1.1 christos 165 1.11 christos *impp = imp; 166 1.1 christos 167 1.12 christos return ISC_R_SUCCESS; 168 1.1 christos 169 1.11 christos cleanup: 170 1.11 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DYNDB, 171 1.11 christos ISC_LOG_ERROR, 172 1.11 christos "failed to dynamically load DynDB instance '%s' driver " 173 1.11 christos "'%s': %s", 174 1.11 christos instname, filename, isc_result_totext(result)); 175 1.1 christos 176 1.11 christos unload_library(&imp); 177 1.1 christos 178 1.12 christos return result; 179 1.1 christos } 180 1.1 christos 181 1.1 christos static void 182 1.1 christos unload_library(dyndb_implementation_t **impp) { 183 1.1 christos dyndb_implementation_t *imp; 184 1.1 christos 185 1.1 christos REQUIRE(impp != NULL && *impp != NULL); 186 1.1 christos 187 1.1 christos imp = *impp; 188 1.5 christos *impp = NULL; 189 1.1 christos 190 1.11 christos /* 191 1.11 christos * This is a resource leak, but there is nothing we can currently do 192 1.11 christos * about it due to how configuration loading/reloading is designed. 193 1.11 christos */ 194 1.11 christos /* uv_dlclose(&imp->handle); */ 195 1.1 christos isc_mem_free(imp->mctx, imp->name); 196 1.11 christos isc_mem_putanddetach(&imp->mctx, imp, sizeof(*imp)); 197 1.1 christos } 198 1.1 christos 199 1.1 christos isc_result_t 200 1.1 christos dns_dyndb_load(const char *libname, const char *name, const char *parameters, 201 1.1 christos const char *file, unsigned long line, isc_mem_t *mctx, 202 1.5 christos const dns_dyndbctx_t *dctx) { 203 1.1 christos isc_result_t result; 204 1.1 christos dyndb_implementation_t *implementation = NULL; 205 1.1 christos 206 1.1 christos REQUIRE(DNS_DYNDBCTX_VALID(dctx)); 207 1.1 christos REQUIRE(name != NULL); 208 1.1 christos 209 1.12 christos isc_once_do(&once, dyndb_initialize); 210 1.1 christos 211 1.1 christos LOCK(&dyndb_lock); 212 1.1 christos 213 1.1 christos /* duplicate instance names are not allowed */ 214 1.5 christos if (impfind(name) != NULL) { 215 1.1 christos CHECK(ISC_R_EXISTS); 216 1.5 christos } 217 1.1 christos 218 1.1 christos CHECK(load_library(mctx, libname, name, &implementation)); 219 1.1 christos CHECK(implementation->register_func(mctx, name, parameters, file, line, 220 1.1 christos dctx, &implementation->inst)); 221 1.1 christos 222 1.1 christos APPEND(dyndb_implementations, implementation, link); 223 1.1 christos result = ISC_R_SUCCESS; 224 1.1 christos 225 1.1 christos cleanup: 226 1.5 christos if (result != ISC_R_SUCCESS) { 227 1.5 christos if (implementation != NULL) { 228 1.1 christos unload_library(&implementation); 229 1.5 christos } 230 1.5 christos } 231 1.1 christos 232 1.1 christos UNLOCK(&dyndb_lock); 233 1.12 christos return result; 234 1.1 christos } 235 1.1 christos 236 1.1 christos void 237 1.3 christos dns_dyndb_cleanup(bool exiting) { 238 1.1 christos dyndb_implementation_t *elem; 239 1.1 christos dyndb_implementation_t *prev; 240 1.1 christos 241 1.12 christos isc_once_do(&once, dyndb_initialize); 242 1.1 christos 243 1.1 christos LOCK(&dyndb_lock); 244 1.1 christos elem = TAIL(dyndb_implementations); 245 1.1 christos while (elem != NULL) { 246 1.1 christos prev = PREV(elem, link); 247 1.1 christos UNLINK(dyndb_implementations, elem, link); 248 1.1 christos isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, 249 1.1 christos DNS_LOGMODULE_DYNDB, ISC_LOG_INFO, 250 1.1 christos "unloading DynDB instance '%s'", elem->name); 251 1.1 christos elem->destroy_func(&elem->inst); 252 1.1 christos ENSURE(elem->inst == NULL); 253 1.1 christos unload_library(&elem); 254 1.1 christos elem = prev; 255 1.1 christos } 256 1.1 christos UNLOCK(&dyndb_lock); 257 1.1 christos 258 1.6 christos if (exiting) { 259 1.1 christos isc_mutex_destroy(&dyndb_lock); 260 1.5 christos } 261 1.1 christos } 262 1.1 christos 263 1.1 christos isc_result_t 264 1.1 christos dns_dyndb_createctx(isc_mem_t *mctx, const void *hashinit, isc_log_t *lctx, 265 1.12 christos dns_view_t *view, dns_zonemgr_t *zmgr, 266 1.12 christos isc_loopmgr_t *loopmgr, dns_dyndbctx_t **dctxp) { 267 1.1 christos dns_dyndbctx_t *dctx; 268 1.1 christos 269 1.1 christos REQUIRE(dctxp != NULL && *dctxp == NULL); 270 1.1 christos 271 1.1 christos dctx = isc_mem_get(mctx, sizeof(*dctx)); 272 1.11 christos *dctx = (dns_dyndbctx_t){ 273 1.12 christos .loopmgr = loopmgr, 274 1.11 christos .hashinit = hashinit, 275 1.11 christos .lctx = lctx, 276 1.11 christos }; 277 1.1 christos 278 1.5 christos if (view != NULL) { 279 1.1 christos dns_view_attach(view, &dctx->view); 280 1.5 christos } 281 1.5 christos if (zmgr != NULL) { 282 1.1 christos dns_zonemgr_attach(zmgr, &dctx->zmgr); 283 1.5 christos } 284 1.1 christos 285 1.1 christos isc_mem_attach(mctx, &dctx->mctx); 286 1.1 christos dctx->magic = DNS_DYNDBCTX_MAGIC; 287 1.1 christos 288 1.1 christos *dctxp = dctx; 289 1.1 christos 290 1.12 christos return ISC_R_SUCCESS; 291 1.1 christos } 292 1.1 christos 293 1.1 christos void 294 1.1 christos dns_dyndb_destroyctx(dns_dyndbctx_t **dctxp) { 295 1.1 christos dns_dyndbctx_t *dctx; 296 1.1 christos 297 1.1 christos REQUIRE(dctxp != NULL && DNS_DYNDBCTX_VALID(*dctxp)); 298 1.1 christos 299 1.1 christos dctx = *dctxp; 300 1.1 christos *dctxp = NULL; 301 1.1 christos 302 1.1 christos dctx->magic = 0; 303 1.1 christos 304 1.5 christos if (dctx->view != NULL) { 305 1.1 christos dns_view_detach(&dctx->view); 306 1.5 christos } 307 1.5 christos if (dctx->zmgr != NULL) { 308 1.1 christos dns_zonemgr_detach(&dctx->zmgr); 309 1.5 christos } 310 1.12 christos dctx->loopmgr = NULL; 311 1.1 christos dctx->lctx = NULL; 312 1.1 christos 313 1.1 christos isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(*dctx)); 314 1.1 christos } 315