03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Permission to use, copy, modify, and distribute this software for any
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * purpose with or without fee is hereby granted, provided that the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * above copyright notice and this permission notice appear in all
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * copies.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * USE OR PERFORMANCE OF THIS SOFTWARE.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * conceived and contributed by Rob Butler.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Permission to use, copy, modify, and distribute this software for any
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * purpose with or without fee is hereby granted, provided that the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * above copyright notice and this permission notice appear in all
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * copies.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * USE OR PERFORMANCE OF THIS SOFTWARE.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * Copyright (C) 1999-2001, 2016 Internet Systems Consortium, Inc. ("ISC")
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews *
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#ifdef DLZ_BDB
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <config.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <stdio.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <string.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <stdlib.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dns/log.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dns/sdlz.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dns/result.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/mem.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/print.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/result.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/util.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <named/globals.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dlz/dlz_bdb_driver.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <db.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic dns_sdlzimplementation_t *dlz_bdb = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/* should the bdb driver use threads. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#ifdef ISC_PLATFORM_USETHREADS
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#define bdb_threads DB_THREAD
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#else
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#define bdb_threads 0
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#endif
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/* BDB database names */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#define dlz_data "dns_data"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#define dlz_zone "dns_zone"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#define dlz_host "dns_host"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#define dlz_client "dns_client"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * This structure contains all the Berkeley DB handles
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * for this instance of the BDB driver.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewstypedef struct bdb_instance {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB_ENV *dbenv; /*%< BDB environment */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB *data; /*%< dns_data database handle */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB *zone; /*%< zone database handle */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB *host; /*%< host database handle */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB *client; /*%< client database handle */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_t *mctx; /*%< memory context */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews} bdb_instance_t;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewstypedef struct parsed_data {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *zone;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *host;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *type;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int ttl;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *data;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews} parsed_data_t;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/* forward reference */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
abff0f462a758383d012887d3a97da4dac0c5a94Evan Huntbdb_findzone(void *driverarg, void *dbdata, const char *name,
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Parses the DBT from the Berkeley DB into a parsed_data record
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * The parsed_data record should be allocated before and passed into the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * bdb_parse_data function. The char (type & data) fields should not
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * be "free"d as that memory is part of the DBT data field. It will be
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * "free"d when the DBT is freed.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_parse_data(char *in, parsed_data_t *pd) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *endp, *ttlStr;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *tmp = in;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *lastchar = (char *) &tmp[strlen(tmp) + 1];
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * String should be formated as:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * zone(a space)host(a space)ttl(a space)type(a space)remaining data
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * examples:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * example.com www 10 A 127.0.0.1
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * example.com mail 10 A 127.0.0.2
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * example.com @ 10 MX 20 mail.example.com
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save pointer to zone */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews pd->zone = tmp;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* find space after zone and change it to a '\0' */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp = strchr(tmp, ' ');
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* verify we found a space */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* change the space to a null (string terminator) */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp[0] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* make sure it is safe to increment pointer */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (++tmp > lastchar)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save pointer to host */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews pd->host = tmp;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* find space after type and change it to a '\0' */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp = strchr(tmp, ' ');
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* verify we found a space */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* change the space to a null (string terminator) */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp[0] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* make sure it is safe to increment pointer */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (++tmp > lastchar)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save pointer to dns type */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews pd->type = tmp;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* find space after type and change it to a '\0' */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp = strchr(tmp, ' ');
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* verify we found a space */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* change the space to a null (string terminator) */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp[0] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* make sure it is safe to increment pointer */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (++tmp > lastchar)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save pointer to dns ttl */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews ttlStr = tmp;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* find space after ttl and change it to a '\0' */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp = strchr(tmp, ' ');
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* verify we found a space */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* change the space to a null (string terminator) */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp[0] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* make sure it is safe to increment pointer */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (++tmp > lastchar)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save pointer to remainder of DNS data */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews pd->data = tmp;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* convert ttl string to integer */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews pd->ttl = strtol(ttlStr, &endp, 10);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (*endp != '\0' || pd->ttl < 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB driver ttl must be a postive number");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if we get this far everything should have worked. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_SUCCESS;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * DLZ methods
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_allowzonexfr(void *driverarg, void *dbdata, const char *name,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews const char *client)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_instance_t *db = (bdb_instance_t *) dbdata;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *client_cursor = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBT key, data;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* check to see if we are authoritative for the zone first. */
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt result = bdb_findzone(driverarg, dbdata, name, NULL, NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_NOTFOUND);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&key, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.flags = DB_DBT_MALLOC;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.data = strdup(name);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto xfr_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.size = strlen(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&data, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews data.flags = DB_DBT_MALLOC;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews data.data = strdup(client);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (data.data == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto xfr_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews data.size = strlen(data.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get a cursor to loop through zone data */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->client->cursor(db->client, NULL, &client_cursor, 0) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto xfr_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews switch(client_cursor->c_get(client_cursor, &key, &data, DB_GET_BOTH)) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews case DB_NOTFOUND:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews case DB_SECONDARY_BAD:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews break;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews case 0:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_SUCCESS;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews break;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews default:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews xfr_cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* free any memory duplicate string in the key field */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* free any memory allocated to the data field. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (data.data != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(data.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of zone_cursor */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (client_cursor != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews client_cursor->c_close(client_cursor);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_allnodes(const char *zone, void *driverarg, void *dbdata,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dns_sdlzallnodes_t *allnodes)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_instance_t *db = (bdb_instance_t *) dbdata;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *zone_cursor = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBT key, data;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int flags;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int bdbres;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews parsed_data_t pd;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *tmp = NULL, *tmp_zone;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNUSED(driverarg);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&key, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&data, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.data = tmp_zone = strdup(zone);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_NOMEMORY);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.size = strlen(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get a cursor to loop through zone data */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->zone->cursor(db->zone, NULL, &zone_cursor, 0) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto allnodes_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews flags = DB_SET;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews while ((bdbres = zone_cursor->c_get(zone_cursor, &key, &data,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews flags)) == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews flags = DB_NEXT_DUP;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp = realloc(tmp, data.size + 1);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto allnodes_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews strncpy(tmp, data.data, data.size);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp[data.size] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (bdb_parse_data(tmp, &pd) != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto allnodes_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = dns_sdlz_putnamedrr(allnodes, pd.host, pd.type,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews pd.ttl, pd.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto allnodes_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews } /* end while loop */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews allnodes_cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(tmp);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* free any memory duplicate string in the key field */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp_zone != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(tmp_zone);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of zone_cursor */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (zone_cursor != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews zone_cursor->c_close(zone_cursor);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Performs BDB cleanup.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Used by bdb_create if there is an error starting up.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Used by bdb_destroy when the driver is shutting down.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic void
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_cleanup(bdb_instance_t *db) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_t *mctx;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* close databases */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->data != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->data->close(db->data, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->host != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->host->close(db->host, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->zone != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->zone->close(db->zone, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->client != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->client->close(db->client, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* close environment */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->dbenv != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->dbenv->close(db->dbenv, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* cleanup memory */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->mctx != NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save mctx for later */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews mctx = db->mctx;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return, and detach the memory */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_put(mctx, db, sizeof(bdb_instance_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_detach(&mctx);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
abff0f462a758383d012887d3a97da4dac0c5a94Evan Huntbdb_findzone(void *driverarg, void *dbdata, const char *name,
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_instance_t *db = (bdb_instance_t *) dbdata;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *zone_cursor = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBT key, data;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNUSED(driverarg);
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt UNUSED(methods);
abff0f462a758383d012887d3a97da4dac0c5a94Evan Hunt UNUSED(clientinfo);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&key, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&data, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews data.flags = DB_DBT_MALLOC;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.data = strdup(name);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_NOMEMORY);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.size = strlen(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get a cursor to loop through zone data */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->zone->cursor(db->zone, NULL, &zone_cursor, 0) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto findzone_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews switch(zone_cursor->c_get(zone_cursor, &key, &data, DB_SET)) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews case DB_NOTFOUND:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews case DB_SECONDARY_BAD:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews break;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews case 0:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_SUCCESS;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews break;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews default:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews findzone_cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* free any memory duplicate string in the key field */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* free any memory allocated to the data field. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (data.data != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(data.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of zone_cursor */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (zone_cursor != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews zone_cursor->c_close(zone_cursor);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_lookup(const char *zone, const char *name, void *driverarg,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt void *dbdata, dns_sdlzlookup_t *lookup,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_instance_t *db = (bdb_instance_t *) dbdata;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *zone_cursor = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *host_cursor = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *join_cursor = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBT key, data;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DBC *cur_arr[3];
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int bdbres;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews parsed_data_t pd;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *tmp_zone, *tmp_host = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *tmp = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNUSED(driverarg);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt UNUSED(methods);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt UNUSED(clientinfo);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&key, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(&data, 0, sizeof(DBT));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set zone key */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.data = tmp_zone = strdup(zone);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.size = strlen(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get a cursor to loop through zone data */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->zone->cursor(db->zone, NULL, &zone_cursor, 0) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize zone_cursor with zone_key */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (zone_cursor->c_get(zone_cursor, &key, &data, DB_SET) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set host key */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.data = tmp_host = strdup(name);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key.data == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews key.size = strlen(key.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get a cursor to loop through host data */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db->host->cursor(db->host, NULL, &host_cursor, 0) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize host_cursor with host_key */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (host_cursor->c_get(host_cursor, &key, &data, DB_SET) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOTFOUND;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews cur_arr[0] = zone_cursor;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews cur_arr[1] = host_cursor;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews cur_arr[2] = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->data->join(db->data, cur_arr, &join_cursor, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews while ((bdbres = join_cursor->c_get(join_cursor, &key,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &data, 0)) == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp = realloc(tmp, data.size + 1);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews strncpy(tmp, data.data, data.size);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tmp[data.size] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (bdb_parse_data(tmp, &pd) != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = dns_sdlz_putrr(lookup, pd.type, pd.ttl, pd.data);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto lookup_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews } /* end while loop */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews lookup_cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(tmp);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp_zone != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(tmp_zone);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tmp_host != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews free(tmp_host);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of the joined cusor */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (join_cursor != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews join_cursor->c_close(join_cursor);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of zone_cursor */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (zone_cursor != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews zone_cursor->c_close(zone_cursor);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of host_cursor */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (host_cursor != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews host_cursor->c_close(host_cursor);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*% Initializes, sets flags and then opens Berkeley databases. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_opendb(DB_ENV *db_env, DBTYPE db_type, DB **db, const char *db_name,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *db_file, int flags) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* Initialize the database. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if ((result = db_create(db, db_env, 0)) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB could not initialize %s database. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db_name, db_strerror(result));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set database flags. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if ((result = (*db)->set_flags(*db, flags)) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB could not set flags for %s database. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db_name, db_strerror(result));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* open the database. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if ((result = (*db)->open(*db, NULL, db_file, db_name, db_type,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB_RDONLY | bdb_threads, 0)) != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB could not open %s database in %s. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db_name, db_file, db_strerror(result));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return ISC_R_SUCCESS;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_create(const char *dlzname, unsigned int argc, char *argv[],
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews void *driverarg, void **dbdata)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int bdbres;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_instance_t *db = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNUSED(dlzname);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNUSED(driverarg);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* verify we have 3 arg's passed to the driver */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (argc != 3) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Berkeley DB driver requires at least "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "2 command line args.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_FAILURE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate and zero memory for driver structure */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db = isc_mem_get(ns_g_mctx, sizeof(bdb_instance_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not allocate memory for "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "database instance object.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_NOMEMORY);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(db, 0, sizeof(bdb_instance_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* attach to the memory context */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_attach(ns_g_mctx, &db->mctx);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* create BDB environment
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Basically BDB allocates and assigns memory to db->dbenv
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdbres = db_env_create(&db->dbenv, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (bdbres != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB environment could not be created. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db_strerror(bdbres));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* open BDB environment */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdbres = db->dbenv->open(db->dbenv, argv[1],
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB_INIT_CDB | DB_INIT_MPOOL |
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_threads | DB_CREATE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (bdbres != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB environment at '%s' could not be opened. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews argv[1], db_strerror(bdbres));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* open dlz_data database. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->data,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dlz_data, argv[2], 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* open dlz_host database. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->host,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dlz_host, argv[2],
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB_DUP | DB_DUPSORT);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* open dlz_zone database. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dlz_zone, argv[2],
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DB_DUP | DB_DUPSORT);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* open dlz_client database. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dlz_client, argv[2], DB_DUP | DB_DUPSORT);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* associate the host secondary database with the primary database */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdbres = db->data->associate(db->data, NULL, db->host, NULL, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (bdbres != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB could not associate %s database with %s. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dlz_host, dlz_data, db_strerror(bdbres));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* associate the zone secondary database with the primary database */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdbres = db->data->associate(db->data, NULL, db->zone, NULL, 0);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (bdbres != 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB could not associate %s database with %s. "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "BDB error: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dlz_zone, dlz_data, db_strerror(bdbres));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto init_cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *dbdata = db;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return(ISC_R_SUCCESS);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews init_cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_cleanup(db);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic void
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbdb_destroy(void *driverarg, void *dbdata)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNUSED(driverarg);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_cleanup((bdb_instance_t *) dbdata);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/* bdb_authority not needed as authority data is returned by lookup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic dns_sdlzmethods_t dlz_bdb_methods = {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_create,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_destroy,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_findzone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_lookup,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews NULL,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews bdb_allnodes,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt bdb_allowzonexfr,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
71bd858d8ed62672e7c23999dc7c02fd16a55089Evan Hunt NULL,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews};
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Wrapper around dns_sdlzregister().
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsisc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsdlz_bdb_init(void) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Write debugging message to log
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Registering DLZ bdb driver.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = dns_sdlzregister("bdb", &dlz_bdb_methods, NULL,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_SDLZFLAG_RELATIVEOWNER |
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_SDLZFLAG_RELATIVERDATA |
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_SDLZFLAG_THREADSAFE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews ns_g_mctx, &dlz_bdb);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "dns_sdlzregister() failed: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_totext(result));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_UNEXPECTED;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Wrapper around dns_sdlzunregister().
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsvoid
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsdlz_bdb_clear(void) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Write debugging message to log
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Unregistering DLZ bdb driver.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (dlz_bdb != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dns_sdlzunregister(&dlz_bdb);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#endif