/*
* 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) 1999-2001 Internet Software Consortium.
*
* 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 INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM 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.
*/
#ifdef DLZ_POSTGRES
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <isc/platform.h>
#include <dlz/sdlz_helper.h>
#include <dlz/dlz_postgres_driver.h>
/* temporarily include time. */
#include <time.h>
#include <libpq-fe.h>
/*
* Private methods
*/
/* ---------------
* Escaping arbitrary strings to get valid SQL strings/identifiers.
*
* Replaces "\\" with "\\\\" and "'" with "''".
* length is the length of the buffer pointed to by
* from. The buffer at to must be at least 2*length + 1 characters
* long. A terminating NUL character is written.
*
* NOTICE!!!
* This function was borrowed directly from PostgreSQL's libpq.
* The function was originally called PQescapeString and renamed
* to postgres_makesafe to avoid a naming collision.
* PQescapeString is a new function made available in Postgres 7.2.
* For some reason the function is not properly exported on Win32
* builds making the function unavailable on Windows. Also, since
* this function is new it would require building this driver with
* the libpq 7.2. By borrowing this function the Windows problem
* is solved, and the dependence on libpq 7.2 is removed. Libpq is
* still required of course, but an older version should work now too.
*
* The copyright statements from the original file containing this
* function are included below:
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
* ---------------
*/
static size_t
{
while (remaining > 0)
{
switch (*source)
{
case '\\':
*target = '\\';
target++;
*target = '\\';
/* target and remaining are updated below. */
break;
case '\'':
*target = '\'';
target++;
*target = '\'';
/* target and remaining are updated below. */
break;
default:
/* target and remaining are updated below. */
}
source++;
target++;
remaining--;
}
/* Write the terminating NUL character. */
*target = '\0';
}
#ifdef ISC_PLATFORM_USETHREADS
/*%
* Properly cleans up a list of database instances.
* This function is only used when the driver is compiled for
* multithreaded operation.
*/
static void
{
/* get the first DBI in the list */
/* loop through the list */
/* get the next DBI in the list */
/* release DB connection */
/* release all memory that comprised a DBI */
}
/* release memory for the list structure */
}
/*%
* Loops through the list of DB instances, attempting to lock
* on the mutex. If successful, the DBI is reserved for use
* and the thread can perform queries against the database.
* If the lock fails, the next one in the list is tried.
* looping continues until a lock is obtained, or until
* the list has been searched dbc_search_limit times.
* This function is only used when the driver is compiled for
* multithreaded operation.
*/
static dbinstance_t *
{
int count = 0;
/* get top of list */
/* loop through list */
while (count < dbc_search_limit) {
/* try to lock on the mutex */
return dbi; /* success, return the DBI for use. */
/* not successful, keep trying */
/* check to see if we have gone to the top of the list. */
count++;
}
}
"Postgres driver unable to find available connection "
"after searching %d times",
count);
return NULL;
}
#endif /* ISC_PLATFORM_USETHREADS */
/*%
* Allocates memory for a new string, and then constructs the new
* string by "escaping" the input string. The new string is
* safe to be used in queries. This is necessary because we cannot
* be sure of what types of strings are passed to us, and we don't
* want special characters in the string causing problems.
*/
static char *
char *outstr;
unsigned int len;
return NULL;
return NULL;
/* PQescapeString(outstr, instr, len); */
return outstr;
}
/*%
* This function is the real core of the driver. Zone, record
* and client strings are passed in (or NULL is passed if the
* string is not available). The type of query we want to run
* is indicated by the query flag, and the dbdata object is passed
* passed in to. dbdata really holds either:
* 1) a list of database instances (in multithreaded mode) OR
* 2) a single database instance (in single threaded mode)
* The function will construct the query and obtain an available
* database instance (DBI). It will then run the query and hopefully
* obtain a result set. Postgres is nice, in that once the result
* set is returned, we can make the db connection available for another
* thread to use, while this thread continues on. So, the DBI is made
* available ASAP by unlocking the instance_lock after we have cleaned
* it up properly.
*/
static isc_result_t
{
unsigned int i = 0;
unsigned int j = 0;
/* temporarily get a unique thread # */
#if 0
/* temporary logging message */
"%d Getting DBI", dlz_thread_num);
#endif
/* get db instance / connection */
#ifdef ISC_PLATFORM_USETHREADS
/* find an available DBI from the list */
#else /* ISC_PLATFORM_USETHREADS */
/*
* only 1 DBI - no need to lock instance lock either
* only 1 thread in the whole process, no possible contention.
*/
#endif /* ISC_PLATFORM_USETHREADS */
#if 0
/* temporary logging message */
"%d Got DBI - checking query", dlz_thread_num);
#endif
/* if DBI is null, can't do anything else */
goto cleanup;
}
/* what type of query are we going to run? */
switch(query) {
case ALLNODES:
/*
* if the query was not passed in from the config file
* then we can't run it. return not_implemented, so
* it's like the code for that operation was never
* built into the driver.... AHHH flexibility!!!
*/
goto cleanup;
}
break;
case ALLOWXFR:
/* same as comments as ALLNODES */
goto cleanup;
}
break;
case AUTHORITY:
/* same as comments as ALLNODES */
goto cleanup;
}
break;
case FINDZONE:
/* this is required. It's the whole point of DLZ! */
"No query specified for findzone. "
"Findzone requires a query");
goto cleanup;
}
break;
case LOOKUP:
/* this is required. It's also a major point of DLZ! */
"No query specified for lookup. "
"Lookup requires a query");
goto cleanup;
}
break;
default:
/*
* this should never happen. If it does, the code is
* screwed up!
*/
"Incorrect query flag passed to "
"postgres_get_resultset");
goto cleanup;
}
#if 0
/* temporary logging message */
"%d checked query", dlz_thread_num);
#endif
/*
* was a zone string passed? If so, make it safe for use in
* queries.
*/
goto cleanup;
}
} else { /* no string passed, set the string pointer to NULL */
}
#if 0
/* temporary logging message */
"%d did zone", dlz_thread_num);
#endif
/*
* was a record string passed? If so, make it safe for use in
* queries.
*/
goto cleanup;
}
} else { /* no string passed, set the string pointer to NULL */
}
#if 0
/* temporary logging message */
"%d did record", dlz_thread_num);
#endif
/*
* was a client string passed? If so, make it safe for use in
* queries.
*/
goto cleanup;
}
} else { /* no string passed, set the string pointer to NULL */
}
#if 0
/* temporary logging message */
"%d did client", dlz_thread_num);
#endif
/*
* what type of query are we going to run?
* this time we build the actual query to run.
*/
switch(query) {
case ALLNODES:
break;
case ALLOWXFR:
break;
case AUTHORITY:
break;
case FINDZONE:
break;
case LOOKUP:
break;
default:
/*
* this should never happen. If it does, the code is
* screwed up!
*/
"Incorrect query flag passed to "
"postgres_get_resultset");
goto cleanup;
}
#if 0
/* temporary logging message */
"%d built query", dlz_thread_num);
#endif
/* if the querystring is null, Bummer, outta RAM. UPGRADE TIME!!! */
if (querystring == NULL) {
goto cleanup;
}
#if 0
/* temporary logging message */
#endif
/*
* output the full query string during debug so we can see
* what lame error the query has.
*/
"\nQuery String: %s\n", querystring);
/* attempt query up to 3 times. */
for (j=0; j < 3; j++) {
#if 0
/* temporary logging message */
"%d executing query for %d time",
dlz_thread_num, j);
#endif
/* try to get result set */
/*
* if result set is null, reset DB connection, max 3
* attempts.
*/
#if 0
/* temporary logging message */
"%d resetting connection",
#endif
/* connection ok, break inner loop */
break;
}
/* result set ok, break outter loop */
#if 0
/* temporary logging message */
"%d rs ok", dlz_thread_num);
#endif
break;
} else {
/* we got a result set object, but it's not right. */
#if 0
/* temporary logging message */
"%d clearing rs", dlz_thread_num);
#endif
/* in case this was the last attempt */
}
}
/* it's always good to cleanup after yourself */
#if 0
/* temporary logging message */
"%d cleaning up", dlz_thread_num);
#endif
/* if we couldn't even allocate DBI, just return NULL */
return ISC_R_FAILURE;
/* free dbi->zone string */
/* free dbi->record string */
/* free dbi->client string */
#ifdef ISC_PLATFORM_USETHREADS
#if 0
/* temporary logging message */
"%d unlocking mutex", dlz_thread_num);
#endif
/* release the lock so another thread can use this dbi */
#endif /* ISC_PLATFORM_USETHREADS */
/* release query string */
if (querystring != NULL)
#if 0
/* temporary logging message */
"%d returning", dlz_thread_num);
#endif
/* return result */
return result;
}
/*%
* The processing of result sets for lookup and authority are
* exactly the same. So that functionality has been moved
* into this function to minimize code.
*/
static isc_result_t
{
unsigned int i;
unsigned int rows;
unsigned int fields;
unsigned int j;
unsigned int len;
char *tmpString;
char *endp;
int ttl;
for (i=0; i < rows; i++) {
switch(fields) {
case 1:
/*
* one column in rs, it's the data field. use
* default type of A record, and default TTL
* of 86400
*/
PQgetvalue(rs, i, 0));
break;
case 2:
/* two columns, data field, and data type.
* use default TTL of 86400.
*/
break;
case 3:
/* three columns, all data no defaults.
* convert text to int, make sure it worked
* right.
*/
"Postgres driver ttl must be "
"a positive number");
}
break;
default:
/*
* more than 3 fields, concatenate the last
* ones together. figure out how long to make
* string
*/
}
/*
* allocate string memory, allow for NULL to
* term string
*/
/* major bummer, need more ram */
"Postgres driver unable to "
"allocate memory for "
"temporary string");
return (ISC_R_FAILURE); /* Yeah, I'd say! */
}
/* copy field to tmpString */
/*
* concat the rest of fields together, space
* between each one.
*/
for (j=3; j < fields; j++) {
}
/* convert text to int, make sure it worked right */
"Postgres driver ttl must be "
"a postive number");
}
/* ok, now tell Bind about it. */
/* done, get rid of this thing. */
}
/* I sure hope we were successful */
if (result != ISC_R_SUCCESS) {
/* nope, get rid of the Result set, and log a msg */
"dns_sdlz_putrr returned error. "
"Error code was: %s",
return (ISC_R_FAILURE);
}
}
/* free result set memory */
/* if we did return results, we are successful */
if (rows > 0)
return (ISC_R_SUCCESS);
/* empty result set, no data found */
return (ISC_R_NOTFOUND);
}
/*
* SDLZ interface methods
*/
/*% determine if the zone is supported by (in) the database */
static isc_result_t
{
unsigned int rows;
/* run the query and get the result set from the database. */
/* if we didn't get a result set, log an err msg. */
if (result != ISC_R_SUCCESS) {
"Postgres driver unable to return "
"result set for findzone query");
return (ISC_R_FAILURE);
}
/* count how many rows in result set */
/* get rid of result set, we are done with it. */
/* if we returned any rows, zone is supported. */
if (rows > 0)
return (ISC_R_SUCCESS);
/* no rows returned, zone is not supported. */
return (ISC_R_NOTFOUND);
}
/*% Determine if the client is allowed to perform a zone transfer */
static isc_result_t
const char *client)
{
unsigned int rows;
/* first check if the zone is supported by the database. */
if (result != ISC_R_SUCCESS)
return (ISC_R_NOTFOUND);
/*
* if we get to this point we know the zone is supported by
* the database the only questions now are is the zone
* transfer is allowed for this client and did the config file
* have an allow zone xfr query.
*
* Run our query, and get a result set from the database.
*/
/* if we get "not implemented", send it along. */
if (result == ISC_R_NOTIMPLEMENTED)
return result;
/* if we didn't get a result set, log an err msg. */
if (result != ISC_R_SUCCESS) {
"Postgres driver unable to return "
"result set for allow xfr query");
return (ISC_R_FAILURE);
}
/* count how many rows in result set */
/* get rid of result set, we are done with it. */
/* if we returned any rows, zone xfr is allowed. */
if (rows > 0)
return (ISC_R_SUCCESS);
/* no rows returned, zone xfr not allowed */
return (ISC_R_NOPERM);
}
/*%
* If the client is allowed to perform a zone transfer, the next order of
* business is to get all the nodes in the zone, so bind can respond to the
* query.
*/
static isc_result_t
{
unsigned int i;
unsigned int rows;
unsigned int fields;
unsigned int j;
unsigned int len;
char *tmpString;
char *endp;
int ttl;
/* run the query and get the result set from the database. */
/* if we get "not implemented", send it along */
if (result == ISC_R_NOTIMPLEMENTED)
return result;
/* if we didn't get a result set, log an err msg. */
if (result != ISC_R_SUCCESS) {
"Postgres driver unable to return "
"result set for all nodes query");
return (ISC_R_FAILURE);
}
for (i=0; i < rows; i++) {
"Postgres driver too few fields "
"returned by all nodes query");
}
/* convert text to int, make sure it worked right */
"Postgres driver ttl must be "
"a postive number");
}
if (fields == 4) {
/* tell Bind about it. */
ttl,
} else {
/*
* more than 4 fields, concatonat the last
* ones together. figure out how long to make
* string
*/
}
/* allocate memory, allow for NULL to term string */
"Postgres driver unable to "
"allocate memory for "
"temporary string");
return (ISC_R_FAILURE);
}
/* copy this field to tmpString */
/* concatonate the rest, with spaces between */
for (j=4; j < fields; j++) {
}
/* tell Bind about it. */
}
/* if we weren't successful, log err msg */
if (result != ISC_R_SUCCESS) {
"dns_sdlz_putnamedrr returned error. "
"Error code was: %s",
return (ISC_R_FAILURE);
}
}
/* free result set memory */
/* if we did return results, we are successful */
if (rows > 0)
return (ISC_R_SUCCESS);
/* empty result set, no data found */
return (ISC_R_NOTFOUND);
}
/*%
* if the lookup function does not return SOA or NS records for the zone,
* use this function to get that information for Bind.
*/
static isc_result_t
{
/* run the query and get the result set from the database. */
/* if we get "not implemented", send it along */
if (result == ISC_R_NOTIMPLEMENTED)
return result;
/* if we didn't get a result set, log an err msg. */
if (result != ISC_R_SUCCESS) {
"Postgres driver unable to return "
"result set for authority query");
return (ISC_R_FAILURE);
}
/*
* lookup and authority result sets are processed in the same
* manner postgres_process_rs does the job for both
* functions.
*/
}
/*% if zone is supported, lookup up a (or multiple) record(s) in it */
static isc_result_t
{
/* run the query and get the result set from the database. */
/* if we didn't get a result set, log an err msg. */
if (result != ISC_R_SUCCESS) {
"Postgres driver unable to "
"return result set for lookup query");
return (ISC_R_FAILURE);
}
/*
* lookup and authority result sets are processed in the same
* manner postgres_process_rs does the job for both functions.
*/
}
/*%
* create an instance of the driver. Remember, only 1 copy of the driver's
* code is ever loaded, the driver has to remember which context it's
* operating in. This is done via use of the dbdata argument which is
* passed into all query functions.
*/
static isc_result_t
{
unsigned int j;
#ifdef ISC_PLATFORM_USETHREADS
/* if multi-threaded, we need a few extra variables. */
int dbcount;
int i;
char *endp;
#endif /* ISC_PLATFORM_USETHREADS */
/* seed random # generator */
#ifdef ISC_PLATFORM_USETHREADS
/* if debugging, let user know we are multithreaded. */
"Postgres driver running multithreaded");
#else /* ISC_PLATFORM_USETHREADS */
/* if debugging, let user know we are single threaded. */
"Postgres driver running single threaded");
#endif /* ISC_PLATFORM_USETHREADS */
/* verify we have at least 5 arg's passed to the driver */
if (argc < 5) {
"Postgres driver requires at least "
"4 command line args.");
return (ISC_R_FAILURE);
}
/* no more than 8 arg's should be passed to the driver */
if (argc > 8) {
"Postgres driver cannot accept more than "
"7 command line args.");
return (ISC_R_FAILURE);
}
/* multithreaded build can have multiple DB connections */
#ifdef ISC_PLATFORM_USETHREADS
/* check how many db connections we should create */
"Postgres driver database connection count "
"must be positive.");
return (ISC_R_FAILURE);
}
/* allocate memory for database connection list */
return (ISC_R_NOMEMORY);
/* initialize DB connection list */
/*
* create the appropriate number of database instances (DBI)
* append each new DBI to the end of the list
*/
for (i=0; i < dbcount; i++) {
#endif /* ISC_PLATFORM_USETHREADS */
/* how many queries were passed in from config file? */
switch(argc) {
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default:
/* not really needed, should shut up compiler. */
}
if (result == ISC_R_SUCCESS) {
"Postgres driver created database "
"instance object.");
} else { /* unsuccessful?, log err msg and cleanup. */
"Postgres driver could not create "
"database instance object.");
goto cleanup;
}
#ifdef ISC_PLATFORM_USETHREADS
/* when multithreaded, build a list of DBI's */
#endif
/* create and set db connection */
/*
* if db connection cannot be created, log err msg and
* cleanup.
*/
"Postgres driver could not allocate "
"memory for database connection");
goto cleanup;
}
/* if we cannot connect the first time, try 3 more times. */
for (j = 0;
j < 3;
j++)
#ifdef ISC_PLATFORM_USETHREADS
/*
* if multi threaded, let user know which connection
* failed. user could be attempting to create 10 db
* connections and for some reason the db backend only
* allows 9
*/
"Postgres driver failed to create "
"database connection number %u "
"after 4 attempts",
i + 1);
goto cleanup;
}
/* set DBI = null for next loop through. */
} /* end for loop */
/* set dbdata to the list we created. */
#else /* ISC_PLATFORM_USETHREADS */
/* if single threaded, just let user know we couldn't connect. */
"Postgres driver failed to create database "
"connection after 4 attempts");
goto cleanup;
}
/*
* single threaded build can only use 1 db connection, return
* it via dbdata
*/
#endif /* ISC_PLATFORM_USETHREADS */
/* hey, we got through all of that ok, return success. */
return(ISC_R_SUCCESS);
#ifdef ISC_PLATFORM_USETHREADS
/*
* if multithreaded, we could fail because only 1 connection
* couldn't be made. We should cleanup the other successful
* connections properly.
*/
#else /* ISC_PLATFORM_USETHREADS */
#endif /* ISC_PLATFORM_USETHREADS */
return(ISC_R_FAILURE);
}
/*%
* destroy an instance of the driver. Remember, only 1 copy of the driver's
* code is ever loaded, the driver has to remember which context it's
* operating in. This is done via use of the dbdata argument.
* so we really only need to clean it up since we are not using driverarg.
*/
static void
{
#ifdef ISC_PLATFORM_USETHREADS
/* cleanup the list of DBI's */
#else /* ISC_PLATFORM_USETHREADS */
/* release DB connection */
/* destroy single DB instance */
#endif /* ISC_PLATFORM_USETHREADS */
}
/* pointers to all our runtime methods. */
/* this is used during driver registration */
/* i.e. in dlz_postgres_init below. */
};
/*%
* Wrapper around dns_sdlzregister().
*/
dlz_postgres_init(void) {
/*
* Write debugging message to log
*/
"Registering DLZ postgres driver.");
/*
* Driver is always threadsafe. When multithreaded all
* functions use multithreaded code. When not multithreaded,
* all functions can only be entered once, but only 1 thread
* of operation is available in Bind. So everything is still
* threadsafe.
*/
/* if we can't register the driver, there are big problems. */
if (result != ISC_R_SUCCESS) {
"dns_sdlzregister() failed: %s",
}
return result;
}
/*%
* Wrapper around dns_sdlzunregister().
*/
void
dlz_postgres_clear(void) {
/*
* Write debugging message to log
*/
"Unregistering DLZ postgres driver.");
/* unregister the driver. */
if (dlz_postgres != NULL)
}
#endif