2000-11-17 22:01:45 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 2000, 2001 Internet Software Consortium.
|
2000-11-17 22:01:45 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-02-28 23:42:37 +00:00
|
|
|
/* $Id: pgsqldb.c,v 1.12 2001/02/28 23:42:37 bwelling Exp $ */
|
2000-11-17 22:01:45 +00:00
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2000-11-17 23:12:11 +00:00
|
|
|
#include <string.h>
|
2000-11-17 23:57:33 +00:00
|
|
|
#include <stdlib.h>
|
2000-11-17 21:54:02 +00:00
|
|
|
|
|
|
|
#include <pgsql/libpq-fe.h>
|
|
|
|
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/print.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/sdb.h>
|
2000-11-17 23:57:33 +00:00
|
|
|
#include <dns/result.h>
|
2000-11-17 21:54:02 +00:00
|
|
|
|
|
|
|
#include <named/globals.h>
|
|
|
|
|
2000-12-06 00:59:08 +00:00
|
|
|
#include "pgsqldb.h"
|
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
/*
|
|
|
|
* A simple database driver that interfaces to a PostgreSQL database. This
|
|
|
|
* is not complete, and not designed for general use. It opens one
|
|
|
|
* connection to the database per zone, which is inefficient. It also may
|
2001-02-28 23:42:37 +00:00
|
|
|
* not handle quoting correctly.
|
2000-11-17 21:54:02 +00:00
|
|
|
*
|
|
|
|
* The table must contain the fields "name", "rdtype", and "rdata", and
|
|
|
|
* is expected to contain a properly constructed zone. The program "zonetodb"
|
|
|
|
* creates such a table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static dns_sdbimplementation_t *pgsqldb = NULL;
|
|
|
|
|
|
|
|
struct dbinfo {
|
|
|
|
PGconn *conn;
|
2001-02-28 23:42:37 +00:00
|
|
|
char *database;
|
2000-11-17 21:54:02 +00:00
|
|
|
char *table;
|
2001-02-28 23:42:37 +00:00
|
|
|
char *host;
|
|
|
|
char *user;
|
|
|
|
char *passwd;
|
2000-11-17 21:54:02 +00:00
|
|
|
};
|
|
|
|
|
2001-02-28 23:42:37 +00:00
|
|
|
static void
|
|
|
|
pgsqldb_destroy(const char *zone, void *driverdata, void **dbdata);
|
|
|
|
|
2000-11-17 23:12:11 +00:00
|
|
|
/*
|
2000-11-20 18:25:14 +00:00
|
|
|
* Canonicalize a string before writing it to the database.
|
|
|
|
* "dest" must be an array of at least size 2*strlen(source) + 1.
|
|
|
|
*/
|
2000-11-17 23:12:11 +00:00
|
|
|
static void
|
2000-11-20 19:34:13 +00:00
|
|
|
quotestring(const char *source, char *dest) {
|
2000-11-17 23:12:11 +00:00
|
|
|
while (*source != 0) {
|
2000-11-20 19:56:12 +00:00
|
|
|
if (*source == '\'')
|
|
|
|
*dest++ = '\'';
|
2000-11-22 21:18:12 +00:00
|
|
|
/* SQL doesn't treat \ as special, but PostgreSQL does */
|
2000-11-20 19:56:12 +00:00
|
|
|
else if (*source == '\\')
|
2000-11-17 23:12:11 +00:00
|
|
|
*dest++ = '\\';
|
|
|
|
*dest++ = *source++;
|
|
|
|
}
|
|
|
|
*dest++ = 0;
|
2001-02-28 23:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to the database.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
db_connect(struct dbinfo *dbi) {
|
|
|
|
dbi->conn = PQsetdbLogin(dbi->host, NULL, NULL, NULL, dbi->database,
|
|
|
|
dbi->user, dbi->passwd);
|
|
|
|
|
|
|
|
if (PQstatus(dbi->conn) == CONNECTION_OK)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
else
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if the connection is still valid. If not, attempt to
|
|
|
|
* reconnect.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
maybe_reconnect(struct dbinfo *dbi) {
|
|
|
|
if (PQstatus(dbi->conn) == CONNECTION_OK)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (db_connect(dbi));
|
|
|
|
}
|
2000-11-17 23:12:11 +00:00
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
/*
|
|
|
|
* This database operates on absolute names.
|
|
|
|
*
|
|
|
|
* Queries are converted into SQL queries and issued synchronously. Errors
|
|
|
|
* are handled really badly.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
pgsqldb_lookup(const char *zone, const char *name, void *dbdata,
|
|
|
|
dns_sdblookup_t *lookup)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
struct dbinfo *dbi = dbdata;
|
|
|
|
PGresult *res;
|
|
|
|
char str[1500];
|
2000-11-17 23:12:11 +00:00
|
|
|
char *canonname;
|
2000-11-17 21:54:02 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
UNUSED(zone);
|
|
|
|
|
2000-11-17 23:12:11 +00:00
|
|
|
canonname = isc_mem_get(ns_g_mctx, strlen(name) * 2 + 1);
|
|
|
|
if (canonname == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
2000-11-20 19:34:13 +00:00
|
|
|
quotestring(name, canonname);
|
2000-11-17 21:54:02 +00:00
|
|
|
snprintf(str, sizeof(str),
|
2000-11-17 23:57:33 +00:00
|
|
|
"SELECT TTL,RDTYPE,RDATA FROM \"%s\" WHERE "
|
2000-11-17 23:12:11 +00:00
|
|
|
"lower(NAME) = lower('%s')", dbi->table, canonname);
|
|
|
|
isc_mem_put(ns_g_mctx, canonname, strlen(name) * 2 + 1);
|
2001-02-28 23:42:37 +00:00
|
|
|
|
|
|
|
result = maybe_reconnect(dbi);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
res = PQexec(dbi->conn, str);
|
2000-11-20 18:25:14 +00:00
|
|
|
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
|
2000-11-17 21:54:02 +00:00
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
if (PQntuples(res) == 0) {
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < PQntuples(res); i++) {
|
2000-11-17 23:57:33 +00:00
|
|
|
char *ttlstr = PQgetvalue(res, i, 0);
|
|
|
|
char *type = PQgetvalue(res, i, 1);
|
|
|
|
char *data = PQgetvalue(res, i, 2);
|
|
|
|
dns_ttl_t ttl;
|
|
|
|
char *endp;
|
|
|
|
ttl = strtol(ttlstr, &endp, 10);
|
|
|
|
if (*endp != '\0') {
|
|
|
|
PQclear(res);
|
|
|
|
return (DNS_R_BADTTL);
|
|
|
|
}
|
|
|
|
result = dns_sdb_putrr(lookup, type, ttl, data);
|
2000-11-17 21:54:02 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue an SQL query to return all nodes in the database and fill the
|
|
|
|
* allnodes structure.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
pgsqldb_allnodes(const char *zone, void *dbdata, dns_sdballnodes_t *allnodes) {
|
|
|
|
struct dbinfo *dbi = dbdata;
|
|
|
|
PGresult *res;
|
|
|
|
isc_result_t result;
|
|
|
|
char str[1500];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
UNUSED(zone);
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str),
|
2000-11-17 23:57:33 +00:00
|
|
|
"SELECT TTL,NAME,RDTYPE,RDATA FROM \"%s\" ORDER BY NAME",
|
2000-11-17 21:54:02 +00:00
|
|
|
dbi->table);
|
2001-02-28 23:42:37 +00:00
|
|
|
|
|
|
|
result = maybe_reconnect(dbi);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
2000-11-17 21:54:02 +00:00
|
|
|
res = PQexec(dbi->conn, str);
|
|
|
|
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK ) {
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
if (PQntuples(res) == 0) {
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < PQntuples(res); i++) {
|
2000-11-17 23:57:33 +00:00
|
|
|
char *ttlstr = PQgetvalue(res, i, 0);
|
|
|
|
char *name = PQgetvalue(res, i, 1);
|
|
|
|
char *type = PQgetvalue(res, i, 2);
|
|
|
|
char *data = PQgetvalue(res, i, 3);
|
|
|
|
dns_ttl_t ttl;
|
|
|
|
char *endp;
|
|
|
|
ttl = strtol(ttlstr, &endp, 10);
|
|
|
|
if (*endp != '\0') {
|
|
|
|
PQclear(res);
|
|
|
|
return (DNS_R_BADTTL);
|
|
|
|
}
|
|
|
|
result = dns_sdb_putnamedrr(allnodes, name, type, ttl, data);
|
2000-11-17 21:54:02 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-28 23:42:37 +00:00
|
|
|
* Create a connection to the database and save any necessary information
|
|
|
|
* in dbdata.
|
|
|
|
*
|
|
|
|
* argv[0] is the name of the database
|
|
|
|
* argv[1] is the name of the table
|
|
|
|
* argv[2] (if present) is the name of the host to connect to
|
|
|
|
* argv[3] (if present) is the name of the user to connect as
|
|
|
|
* argv[4] (if present) is the name of the password to connect with
|
2000-11-17 21:54:02 +00:00
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
pgsqldb_create(const char *zone, int argc, char **argv,
|
|
|
|
void *driverdata, void **dbdata)
|
|
|
|
{
|
|
|
|
struct dbinfo *dbi;
|
2001-02-28 23:42:37 +00:00
|
|
|
isc_result_t result;
|
2000-11-17 21:54:02 +00:00
|
|
|
|
|
|
|
UNUSED(zone);
|
|
|
|
UNUSED(driverdata);
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
|
|
|
|
dbi = isc_mem_get(ns_g_mctx, sizeof(struct dbinfo));
|
|
|
|
if (dbi == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
2001-02-28 23:42:37 +00:00
|
|
|
dbi->conn = NULL;
|
|
|
|
dbi->database = NULL;
|
|
|
|
dbi->table = NULL;
|
|
|
|
dbi->host = NULL;
|
|
|
|
dbi->user = NULL;
|
|
|
|
dbi->passwd = NULL;
|
|
|
|
|
|
|
|
#define STRDUP_OR_FAIL(target, source) \
|
|
|
|
do { \
|
|
|
|
target = isc_mem_strdup(ns_g_mctx, source); \
|
|
|
|
if (target == NULL) { \
|
|
|
|
result = ISC_R_NOMEMORY; \
|
|
|
|
goto cleanup; \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
STRDUP_OR_FAIL(dbi->database, argv[0]);
|
|
|
|
STRDUP_OR_FAIL(dbi->table, argv[1]);
|
|
|
|
if (argc > 2)
|
|
|
|
STRDUP_OR_FAIL(dbi->host, argv[2]);
|
|
|
|
if (argc > 3)
|
|
|
|
STRDUP_OR_FAIL(dbi->user, argv[3]);
|
|
|
|
if (argc > 4)
|
|
|
|
STRDUP_OR_FAIL(dbi->passwd, argv[4]);
|
|
|
|
|
|
|
|
result = db_connect(dbi);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup;
|
2000-11-17 21:54:02 +00:00
|
|
|
|
|
|
|
*dbdata = dbi;
|
|
|
|
return (ISC_R_SUCCESS);
|
2001-02-28 23:42:37 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
pgsqldb_destroy(zone, driverdata, (void **)&dbi);
|
|
|
|
return (result);
|
2000-11-17 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the connection to the database.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pgsqldb_destroy(const char *zone, void *driverdata, void **dbdata) {
|
|
|
|
struct dbinfo *dbi = *dbdata;
|
|
|
|
|
|
|
|
UNUSED(zone);
|
|
|
|
UNUSED(driverdata);
|
|
|
|
|
2001-02-28 23:42:37 +00:00
|
|
|
if (dbi->conn != NULL)
|
|
|
|
PQfinish(dbi->conn);
|
|
|
|
if (dbi->database != NULL)
|
|
|
|
isc_mem_free(ns_g_mctx, dbi->database);
|
|
|
|
if (dbi->table != NULL)
|
|
|
|
isc_mem_free(ns_g_mctx, dbi->table);
|
|
|
|
if (dbi->host != NULL)
|
|
|
|
isc_mem_free(ns_g_mctx, dbi->host);
|
|
|
|
if (dbi->user != NULL)
|
|
|
|
isc_mem_free(ns_g_mctx, dbi->user);
|
|
|
|
if (dbi->passwd != NULL)
|
|
|
|
isc_mem_free(ns_g_mctx, dbi->passwd);
|
|
|
|
if (dbi->database != NULL)
|
|
|
|
isc_mem_free(ns_g_mctx, dbi->database);
|
2000-11-17 21:54:02 +00:00
|
|
|
isc_mem_put(ns_g_mctx, dbi, sizeof(struct dbinfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the SQL database corresponds to a zone, the authority data should
|
|
|
|
* be returned by the lookup() function. Therefore the authority() function
|
|
|
|
* is NULL.
|
|
|
|
*/
|
|
|
|
static dns_sdbmethods_t pgsqldb_methods = {
|
|
|
|
pgsqldb_lookup,
|
|
|
|
NULL, /* authority */
|
|
|
|
pgsqldb_allnodes,
|
|
|
|
pgsqldb_create,
|
|
|
|
pgsqldb_destroy
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around dns_sdb_register().
|
|
|
|
*/
|
|
|
|
isc_result_t
|
|
|
|
pgsqldb_init(void) {
|
|
|
|
unsigned int flags;
|
|
|
|
flags = 0;
|
|
|
|
return (dns_sdb_register("pgsql", &pgsqldb_methods, NULL, flags,
|
|
|
|
ns_g_mctx, &pgsqldb));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around dns_sdb_unregister().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pgsqldb_clear(void) {
|
2000-11-20 19:50:02 +00:00
|
|
|
if (pgsqldb != NULL)
|
|
|
|
dns_sdb_unregister(&pgsqldb);
|
2000-11-17 21:54:02 +00:00
|
|
|
}
|