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