/*
* Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*
* The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
* conceived and contributed by Rob Butler.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
*
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is simply a merge of Andrew Tridgell's dlz_example.c and the
* original bdb_bdbhpt_driver.c
*
* This provides the externally loadable bdbhpt DLZ driver, without
* update support
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <db.h>
#include "dlz_minimal.h"
/* should the bdb driver use threads. */
#ifdef ISC_PLATFORM_USETHREADS
#else
#define bdbhpt_threads 0
#endif
/* bdbhpt database names */
/*
* This structure contains all our DB handles and helper functions we
* inherit from the dlz_dlopen driver
*
*/
typedef struct bdbhpt_instance {
/* Helper functions from the dlz_dlopen driver */
typedef struct bdbhpt_parsed_data {
char *host;
char *type;
int ttl;
char *data;
static void
/*%
* Reverses a string in place.
*/
static char
return str;
}
return str;
}
/*%
* Parses the DBT from the Berkeley DB into a parsed_data record
* The parsed_data record should be allocated before and passed into the
* bdbhpt_parse_data function. The char (type & data) fields should not
* be "free"d as that memory is part of the DBT data field. It will be
* "free"d when the DBT is freed.
*/
static isc_result_t
/*%
* String should be formatted as:
* replication_id
* (a space)
* host_name
* (a space)
* ttl
* (a space)
* type
* (a space)
* remaining data
*
* examples:
*
* 9191 host 10 A 127.0.0.1
* server1_212 host 10 A 127.0.0.2
* {xxxx-xxxx-xxxx-xxxx-xxxx} host 10 MX 20 mail.example.com
*/
/*
* we don't need the replication id, so don't
* bother saving a pointer to it.
*/
/* find space after replication id */
/* verify we found a space */
return ISC_R_FAILURE;
/* make sure it is safe to increment pointer */
return ISC_R_FAILURE;
/* save pointer to host */
/* find space after host and change it to a '\0' */
/* verify we found a space */
return ISC_R_FAILURE;
/* change the space to a null (string terminator) */
tmp[0] = '\0';
/* make sure it is safe to increment pointer */
return ISC_R_FAILURE;
/* save pointer to ttl string */
/* find space after ttl and change it to a '\0' */
/* verify we found a space */
return ISC_R_FAILURE;
/* change the space to a null (string terminator) */
tmp[0] = '\0';
/* make sure it is safe to increment pointer */
return ISC_R_FAILURE;
/* save pointer to dns type */
/* find space after type and change it to a '\0' */
/* verify we found a space */
return ISC_R_FAILURE;
/* change the space to a null (string terminator) */
tmp[0] = '\0';
/* make sure it is safe to increment pointer */
return ISC_R_FAILURE;
/* save pointer to remainder of DNS data */
/* convert ttl string to integer */
"bdbhpt_dynamic: "
"ttl must be a positive number");
return ISC_R_FAILURE;
}
/* if we get this far everything should have worked. */
return ISC_R_SUCCESS;
}
/*
* See if a zone transfer is allowed
*/
/* check to see if we are authoritative for the zone first. */
#if DLZ_DLOPEN_VERSION >= 3
#else
#endif
if (result != ISC_R_SUCCESS)
return (ISC_R_NOTFOUND);
goto xfr_cleanup;
}
goto xfr_cleanup;
}
case DB_NOTFOUND:
break;
case 0:
break;
default:
}
/* free any memory duplicate string in the key field */
/* free any memory allocated to the data field. */
return result;
}
/*%
* Perform a zone transfer
*
* BDB does not allow a secondary index on a database that allows
* duplicates. We have a few options:
*
* 1) kill speed by having lookup method use a secondary db which
* is associated to the primary DB with the DNS data. Then have
* another secondary db for zone transfer which also points to
* the dns_data primary. NO - The point of this driver is
* lookup performance.
*
* 2) Blow up database size by storing DNS data twice. Once for
* the lookup (dns_data) database, and a second time for the zone
* transfer (dns_xfr) database. NO - That would probably require
* a larger cache to provide good performance. Also, that would
* make the DB larger on disk potentially slowing it as well.
*
* 3) Loop through the dns_xfr database with a cursor to get
* all the different hosts in a zone. Then use the zone & host
* together to lookup the data in the dns_data database. YES -
* This may slow down zone xfr's a little, but that's ok they
* don't happen as often and don't need to be as fast. We can
* also use this table when deleting a zone (The BDB driver
* is read only - the delete would be used during replication
* updates by a separate process).
*/
int xfr_flags;
int dns_flags;
int bdbhptres;
return (ISC_R_NOMEMORY);
/* get a cursor to loop through dns_xfr table */
goto allnodes_cleanup;
}
/* get a cursor to loop through dns_data table */
goto allnodes_cleanup;
}
/* loop through xfr table for specified zone. */
{
/* +1 to allow for space between zone and host names */
/* +1 to allow for null term at end of string. */
goto allnodes_cleanup;
/*
* construct search key for dns_data.
* zone_name(a space)host_name
*/
&dns_key,
&dns_data,
dns_flags)) == 0)
{
/* +1 to allow for null term at end of string. */
goto allnodes_cleanup;
/* copy data to tmp string, and append null term. */
/* split string into dns data parts. */
goto allnodes_cleanup;
if (result != ISC_R_SUCCESS)
goto allnodes_cleanup;
} /* end inner while loop */
/* clean up memory */
if (tmp_zone_host != NULL) {
}
} /* end outer while loop */
/* free any memory */
if (tmp_zone_host != NULL)
/* get rid of cursors */
if (xfr_cursor != NULL)
if (dns_cursor != NULL)
return result;
}
/*%
* Performs bdbhpt cleanup.
* Used by bdbhpt_create if there is an error starting up.
* Used by bdbhpt_destroy when the driver is shutting down.
*/
static void
/* close databases */
/* close environment */
}
/*
* See if we handle a given zone
*/
#if DLZ_DLOPEN_VERSION < 3
#else
#endif
{
#if DLZ_DLOPEN_VERSION >= 3
#endif
return (ISC_R_NOMEMORY);
/*
* reverse string to take advantage of BDB locality of reference
* if we need futher lookups because the zone doesn't match the
* first time.
*/
case DB_NOTFOUND:
break;
case 0:
break;
default:
}
/* free any memory duplicate string in the key field */
/* free any memory allocated to the data field. */
return result;
}
/*
* Look up one record in the database.
*
*/
#if DLZ_DLOPEN_VERSION == 1
#else
#endif
{
int bdbhptres;
int flags;
#if DLZ_DLOPEN_VERSION >= 2
#endif
/* allocate mem for key */
return ISC_R_NOMEMORY;
/* get a cursor to loop through data */
goto lookup_cleanup;
}
flags)) == 0)
{
flags = DB_NEXT_DUP;
goto lookup_cleanup;
goto lookup_cleanup;
if (result != ISC_R_SUCCESS)
goto lookup_cleanup;
} /* end while loop */
/* get rid of cursor */
if (data_cursor != NULL)
return result;
}
/*%
* Initialises, sets flags and then opens Berkeley databases.
*/
static isc_result_t
{
int result;
/* Initialise the database. */
"bdbhpt_dynamic: could not initialize %s database. "
"BerkeleyDB error: %s",
return ISC_R_FAILURE;
}
/* set database flags. */
"bdbhpt_dynamic: could not set flags for %s database. "
"BerkeleyDB error: %s",
return ISC_R_FAILURE;
}
/* open the database. */
DB_RDONLY | bdbhpt_threads, 0)) != 0) {
"bdbhpt_dynamic: could not open %s database in %s. "
"BerkeleyDB error: %s",
return ISC_R_FAILURE;
}
return ISC_R_SUCCESS;
}
/*
* Called to initialize the driver
*/
void **dbdata, ...)
{
int bdbhptres;
int bdbFlags = 0;
const char *helper_name;
/* Allocate memory for our db structures and helper functions */
return (ISC_R_NOMEMORY);
/* Fill in the helper functions */
/* verify we have 4 arg's passed to the driver */
if (argc != 4) {
"bdbhpt_dynamic: please supply 3 command line args. "
"You supplied: %s", argc);
return (ISC_R_FAILURE);
}
switch((char) *argv[1]) {
/*
* Transactional mode. Highest safety - lowest speed.
*/
case 'T':
case 't':
"bdbhpt_dynamic: using transactional mode.");
break;
/*
* Concurrent mode. Lower safety (no rollback) -
* higher speed.
*/
case 'C':
case 'c':
"bdbhpt_dynamic: using concurrent mode.");
break;
/*
* Private mode. No inter-process communication & no locking.
* Lowest saftey - highest speed.
*/
case 'P':
case 'p':
"bdbhpt_dynamic: using private mode.");
break;
default:
"bdbhpt_dynamic: "
"operating mode must be set to P or C or T. "
return (ISC_R_FAILURE);
}
/*
* create bdbhpt environment
* Basically bdbhpt allocates and assigns memory to db->dbenv
*/
if (bdbhptres != 0) {
"bdbhpt_dynamic: db environment could not be created. "
goto init_cleanup;
}
/* open bdbhpt environment */
if (bdbhptres != 0) {
"bdbhpt_dynamic: "
"db environment at '%s' could not be opened. "
"BerkeleyDB error: %s",
goto init_cleanup;
}
/* open dlz_data database. */
if (result != ISC_R_SUCCESS)
goto init_cleanup;
/* open dlz_xfr database. */
if (result != ISC_R_SUCCESS)
goto init_cleanup;
/* open dlz_zone database. */
if (result != ISC_R_SUCCESS)
goto init_cleanup;
/* open dlz_client database. */
if (result != ISC_R_SUCCESS)
goto init_cleanup;
"bdbhpt_dynamic: version %s, started",
return(ISC_R_SUCCESS);
return result;
}
/*
* Shut down the backend
*/
void
"dlz_bdbhpt_dynamic (%s): shutting down",
}
/*
* Return the version of the API
*/
int
return (DLZ_DLOPEN_VERSION);
}
/*
* Register a helper function from the bind9 dlz_dlopen driver
*/
static void
}