pgsqldb.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 2000, 2001, 2004, 2007, 2011, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id: pgsqldb.c,v 1.17 2011/10/11 23:46:45 tbox Exp $ */
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pgsql/libpq-fe.h>
#include "pgsqldb.h"
/*
* 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
* not handle quoting correctly.
*
* 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.
*/
struct dbinfo {
char *database;
char *table;
char *host;
char *user;
char *passwd;
};
static void
/*
* Canonicalize a string before writing it to the database.
* "dest" must be an array of at least size 2*strlen(source) + 1.
*/
static void
while (*source != 0) {
if (*source == '\'')
*dest++ = '\'';
/* SQL doesn't treat \ as special, but PostgreSQL does */
else if (*source == '\\')
*dest++ = '\\';
}
*dest++ = 0;
}
/*
* Connect to the database.
*/
static isc_result_t
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
return (ISC_R_SUCCESS);
return (db_connect(dbi));
}
/*
* This database operates on absolute names.
*
* Queries are converted into SQL queries and issued synchronously. Errors
* are handled really badly.
*/
#ifdef DNS_CLIENTINFO_VERSION
static isc_result_t
#else
static isc_result_t
#endif /* DNS_CLIENTINFO_VERSION */
{
char str[1500];
char *canonname;
int i;
#ifdef DNS_CLIENTINFO_VERSION
#endif /* DNS_CLIENTINFO_VERSION */
return (ISC_R_NOMEMORY);
"SELECT TTL,RDTYPE,RDATA FROM \"%s\" WHERE "
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_FAILURE);
}
return (ISC_R_NOTFOUND);
}
char *endp;
if (*endp != '\0') {
return (DNS_R_BADTTL);
}
if (result != ISC_R_SUCCESS) {
return (ISC_R_FAILURE);
}
}
return (ISC_R_SUCCESS);
}
/*
* Issue an SQL query to return all nodes in the database and fill the
* allnodes structure.
*/
static isc_result_t
char str[1500];
int i;
"SELECT TTL,NAME,RDTYPE,RDATA FROM \"%s\" ORDER BY NAME",
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_FAILURE);
}
return (ISC_R_NOTFOUND);
}
char *endp;
if (*endp != '\0') {
return (DNS_R_BADTTL);
}
if (result != ISC_R_SUCCESS) {
return (ISC_R_FAILURE);
}
}
return (ISC_R_SUCCESS);
}
/*
* 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
*/
static isc_result_t
void *driverdata, void **dbdata)
{
if (argc < 2)
return (ISC_R_FAILURE);
return (ISC_R_NOMEMORY);
do { \
result = ISC_R_NOMEMORY; \
goto cleanup; \
} \
} while (0);
if (argc > 2)
if (argc > 3)
if (argc > 4)
if (result != ISC_R_SUCCESS)
goto cleanup;
return (ISC_R_SUCCESS);
return (result);
}
/*
* Close the connection to the database.
*/
static void
}
/*
* 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 = {
NULL, /* authority */
NULL /* lookup2 */
};
/*
* Wrapper around dns_sdb_register().
*/
pgsqldb_init(void) {
unsigned int flags;
flags = 0;
}
/*
* Wrapper around dns_sdb_unregister().
*/
void
pgsqldb_clear(void) {
}