summaryrefslogtreecommitdiffstats
path: root/contrib/sdb/pgsql
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 18:37:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-05 18:37:14 +0000
commitea648e70a989cca190cd7403fe892fd2dcc290b4 (patch)
treee2b6b1c647da68b0d4d66082835e256eb30970e8 /contrib/sdb/pgsql
parentInitial commit. (diff)
downloadbind9-ea648e70a989cca190cd7403fe892fd2dcc290b4.tar.xz
bind9-ea648e70a989cca190cd7403fe892fd2dcc290b4.zip
Adding upstream version 1:9.11.5.P4+dfsg.upstream/1%9.11.5.P4+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'contrib/sdb/pgsql')
-rw-r--r--contrib/sdb/pgsql/pgsqldb.c353
-rw-r--r--contrib/sdb/pgsql/pgsqldb.h18
-rw-r--r--contrib/sdb/pgsql/zonetodb.c283
3 files changed, 654 insertions, 0 deletions
diff --git a/contrib/sdb/pgsql/pgsqldb.c b/contrib/sdb/pgsql/pgsqldb.c
new file mode 100644
index 0000000..2551099
--- /dev/null
+++ b/contrib/sdb/pgsql/pgsqldb.c
@@ -0,0 +1,353 @@
+/*
+ * Copyright (C) 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/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+
+#include <config.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#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>
+#include <dns/result.h>
+
+#include <named/globals.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.
+ */
+
+static dns_sdbimplementation_t *pgsqldb = NULL;
+
+struct dbinfo {
+ PGconn *conn;
+ char *database;
+ char *table;
+ char *host;
+ char *user;
+ char *passwd;
+};
+
+static void
+pgsqldb_destroy(const char *zone, void *driverdata, void **dbdata);
+
+/*
+ * Canonicalize a string before writing it to the database.
+ * "dest" must be an array of at least size 2*strlen(source) + 1.
+ */
+static void
+quotestring(const char *source, char *dest) {
+ while (*source != 0) {
+ if (*source == '\'')
+ *dest++ = '\'';
+ /* SQL doesn't treat \ as special, but PostgreSQL does */
+ else if (*source == '\\')
+ *dest++ = '\\';
+ *dest++ = *source++;
+ }
+ *dest++ = 0;
+}
+
+/*
+ * 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));
+}
+
+/*
+ * 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
+pgsqldb_lookup(const char *zone, const char *name, void *dbdata,
+ dns_sdblookup_t *lookup, dns_clientinfomethods_t *methods,
+ dns_clientinfo_t *clientinfo)
+#else
+static isc_result_t
+pgsqldb_lookup(const char *zone, const char *name, void *dbdata,
+ dns_sdblookup_t *lookup)
+#endif /* DNS_CLIENTINFO_VERSION */
+{
+ isc_result_t result;
+ struct dbinfo *dbi = dbdata;
+ PGresult *res;
+ char str[1500];
+ char *canonname;
+ int i;
+
+ UNUSED(zone);
+#ifdef DNS_CLIENTINFO_VERSION
+ UNUSED(methods);
+ UNUSED(clientinfo);
+#endif /* DNS_CLIENTINFO_VERSION */
+
+ canonname = isc_mem_get(ns_g_mctx, strlen(name) * 2 + 1);
+ if (canonname == NULL)
+ return (ISC_R_NOMEMORY);
+ quotestring(name, canonname);
+ snprintf(str, sizeof(str),
+ "SELECT TTL,RDTYPE,RDATA FROM \"%s\" WHERE "
+ "lower(NAME) = lower('%s')", dbi->table, canonname);
+ isc_mem_put(ns_g_mctx, canonname, strlen(name) * 2 + 1);
+
+ result = maybe_reconnect(dbi);
+ if (result != ISC_R_SUCCESS)
+ return (result);
+
+ 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++) {
+ 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);
+ 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),
+ "SELECT TTL,NAME,RDTYPE,RDATA FROM \"%s\" ORDER BY NAME",
+ dbi->table);
+
+ result = maybe_reconnect(dbi);
+ if (result != ISC_R_SUCCESS)
+ return (result);
+
+ 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++) {
+ 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);
+ if (result != ISC_R_SUCCESS) {
+ PQclear(res);
+ return (ISC_R_FAILURE);
+ }
+ }
+
+ PQclear(res);
+ 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
+pgsqldb_create(const char *zone, int argc, char **argv,
+ void *driverdata, void **dbdata)
+{
+ struct dbinfo *dbi;
+ isc_result_t result;
+
+ 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);
+ 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;
+
+ *dbdata = dbi;
+ return (ISC_R_SUCCESS);
+
+ cleanup:
+ pgsqldb_destroy(zone, driverdata, (void **)&dbi);
+ return (result);
+}
+
+/*
+ * 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);
+
+ 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);
+ 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,
+ NULL /* lookup2 */
+};
+
+/*
+ * 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) {
+ if (pgsqldb != NULL)
+ dns_sdb_unregister(&pgsqldb);
+}
diff --git a/contrib/sdb/pgsql/pgsqldb.h b/contrib/sdb/pgsql/pgsqldb.h
new file mode 100644
index 0000000..cd60f14
--- /dev/null
+++ b/contrib/sdb/pgsql/pgsqldb.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 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/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+
+#include <isc/types.h>
+
+isc_result_t pgsqldb_init(void);
+
+void pgsqldb_clear(void);
+
diff --git a/contrib/sdb/pgsql/zonetodb.c b/contrib/sdb/pgsql/zonetodb.c
new file mode 100644
index 0000000..df7e939
--- /dev/null
+++ b/contrib/sdb/pgsql/zonetodb.c
@@ -0,0 +1,283 @@
+/*
+ * Copyright (C) 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/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <isc/buffer.h>
+#include <isc/entropy.h>
+#include <isc/hash.h>
+#include <isc/mem.h>
+#include <isc/print.h>
+#include <isc/result.h>
+
+#include <dns/db.h>
+#include <dns/dbiterator.h>
+#include <dns/fixedname.h>
+#include <dns/name.h>
+#include <dns/rdata.h>
+#include <dns/rdataset.h>
+#include <dns/rdatasetiter.h>
+#include <dns/rdatatype.h>
+#include <dns/result.h>
+
+#include <pgsql/libpq-fe.h>
+
+/*
+ * Generate a PostgreSQL table from a zone.
+ *
+ * This is compiled this with something like the following (assuming bind9 has
+ * been installed):
+ *
+ * gcc -g `isc-config.sh --cflags isc dns` -c zonetodb.c
+ * gcc -g -o zonetodb zonetodb.o `isc-config.sh --libs isc dns` -lpq
+ */
+
+PGconn *conn = NULL;
+char *dbname, *dbtable;
+char str[10240];
+
+void
+closeandexit(int status) {
+ if (conn != NULL)
+ PQfinish(conn);
+ exit(status);
+}
+
+void
+check_result(isc_result_t result, const char *message) {
+ if (result != ISC_R_SUCCESS) {
+ fprintf(stderr, "%s: %s\n", message,
+ isc_result_totext(result));
+ closeandexit(1);
+ }
+}
+
+/*
+ * Canonicalize a string before writing it to the database.
+ * "dest" must be an array of at least size 2*strlen(source) + 1.
+ */
+static void
+quotestring(const unsigned char *source, unsigned char *dest) {
+ while (*source != 0) {
+ if (*source == '\'')
+ *dest++ = '\'';
+ else if (*source == '\\')
+ *dest++ = '\\';
+ *dest++ = *source++;
+ }
+ *dest++ = 0;
+}
+
+void
+addrdata(dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata) {
+ unsigned char namearray[DNS_NAME_MAXTEXT + 1];
+ unsigned char canonnamearray[2 * DNS_NAME_MAXTEXT + 1];
+ unsigned char typearray[20];
+ unsigned char canontypearray[40];
+ unsigned char dataarray[2048];
+ unsigned char canondataarray[4096];
+ isc_buffer_t b;
+ isc_result_t result;
+ PGresult *res;
+
+ isc_buffer_init(&b, namearray, sizeof(namearray) - 1);
+ result = dns_name_totext(name, true, &b);
+ check_result(result, "dns_name_totext");
+ namearray[isc_buffer_usedlength(&b)] = 0;
+ quotestring((const unsigned char *)namearray, canonnamearray);
+
+ isc_buffer_init(&b, typearray, sizeof(typearray) - 1);
+ result = dns_rdatatype_totext(rdata->type, &b);
+ check_result(result, "dns_rdatatype_totext");
+ typearray[isc_buffer_usedlength(&b)] = 0;
+ quotestring((const unsigned char *)typearray, canontypearray);
+
+ isc_buffer_init(&b, dataarray, sizeof(dataarray) - 1);
+ result = dns_rdata_totext(rdata, NULL, &b);
+ check_result(result, "dns_rdata_totext");
+ dataarray[isc_buffer_usedlength(&b)] = 0;
+ quotestring((const unsigned char *)dataarray, canondataarray);
+
+ snprintf(str, sizeof(str),
+ "INSERT INTO %s (NAME, TTL, RDTYPE, RDATA)"
+ " VALUES ('%s', %d, '%s', '%s')",
+ dbtable, canonnamearray, ttl, canontypearray, canondataarray);
+ printf("%s\n", str);
+ res = PQexec(conn, str);
+ if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
+ fprintf(stderr, "INSERT INTO command failed: %s\n",
+ PQresultErrorMessage(res));
+ PQclear(res);
+ closeandexit(1);
+ }
+ PQclear(res);
+}
+
+int
+main(int argc, char **argv) {
+ char *porigin, *zonefile;
+ dns_fixedname_t forigin, fname;
+ dns_name_t *origin, *name;
+ dns_db_t *db = NULL;
+ dns_dbiterator_t *dbiter;
+ dns_dbnode_t *node;
+ dns_rdatasetiter_t *rdsiter;
+ dns_rdataset_t rdataset;
+ dns_rdata_t rdata = DNS_RDATA_INIT;
+ isc_mem_t *mctx = NULL;
+ isc_entropy_t *ectx = NULL;
+ isc_buffer_t b;
+ isc_result_t result;
+ PGresult *res;
+
+ if (argc != 5) {
+ printf("usage: %s origin file dbname dbtable\n", argv[0]);
+ printf("Note that dbname must be an existing database.\n");
+ exit(1);
+ }
+
+ porigin = argv[1];
+ zonefile = argv[2];
+ dbname = argv[3];
+ dbtable = argv[4];
+
+ dns_result_register();
+
+ mctx = NULL;
+ result = isc_mem_create(0, 0, &mctx);
+ check_result(result, "isc_mem_create");
+
+ result = isc_entropy_create(mctx, &ectx);
+ check_result(result, "isc_entropy_create");
+
+ result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
+ check_result(result, "isc_hash_create");
+
+ isc_buffer_init(&b, porigin, strlen(porigin));
+ isc_buffer_add(&b, strlen(porigin));
+ origin = dns_fixedname_initname(&forigin);
+ result = dns_name_fromtext(origin, &b, dns_rootname, 0, NULL);
+ check_result(result, "dns_name_fromtext");
+
+ db = NULL;
+ result = dns_db_create(mctx, "rbt", origin, dns_dbtype_zone,
+ dns_rdataclass_in, 0, NULL, &db);
+ check_result(result, "dns_db_create");
+
+ result = dns_db_load(db, zonefile);
+ if (result == DNS_R_SEENINCLUDE)
+ result = ISC_R_SUCCESS;
+ check_result(result, "dns_db_load");
+
+ printf("Connecting to '%s'\n", dbname);
+ conn = PQsetdb(NULL, NULL, NULL, NULL, dbname);
+ if (PQstatus(conn) == CONNECTION_BAD) {
+ fprintf(stderr, "Connection to database '%s' failed: %s\n",
+ dbname, PQerrorMessage(conn));
+ closeandexit(1);
+ }
+
+ snprintf(str, sizeof(str),
+ "DROP TABLE %s", dbtable);
+ printf("%s\n", str);
+ res = PQexec(conn, str);
+ if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
+ fprintf(stderr, "DROP TABLE command failed: %s\n",
+ PQresultErrorMessage(res));
+ PQclear(res);
+
+ snprintf(str, sizeof(str), "BEGIN");
+ printf("%s\n", str);
+ res = PQexec(conn, str);
+ if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
+ fprintf(stderr, "BEGIN command failed: %s\n",
+ PQresultErrorMessage(res));
+ PQclear(res);
+ closeandexit(1);
+ }
+ PQclear(res);
+
+ snprintf(str, sizeof(str),
+ "CREATE TABLE %s "
+ "(NAME TEXT, TTL INTEGER, RDTYPE TEXT, RDATA TEXT)",
+ dbtable);
+ printf("%s\n", str);
+ res = PQexec(conn, str);
+ if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
+ fprintf(stderr, "CREATE TABLE command failed: %s\n",
+ PQresultErrorMessage(res));
+ PQclear(res);
+ closeandexit(1);
+ }
+ PQclear(res);
+
+ dbiter = NULL;
+ result = dns_db_createiterator(db, 0, &dbiter);
+ check_result(result, "dns_db_createiterator()");
+
+ result = dns_dbiterator_first(dbiter);
+ check_result(result, "dns_dbiterator_first");
+
+ name = dns_fixedname_initname(&fname);
+ dns_rdataset_init(&rdataset);
+ dns_rdata_init(&rdata);
+
+ while (result == ISC_R_SUCCESS) {
+ node = NULL;
+ result = dns_dbiterator_current(dbiter, &node, name);
+ if (result == ISC_R_NOMORE)
+ break;
+ check_result(result, "dns_dbiterator_current");
+
+ rdsiter = NULL;
+ result = dns_db_allrdatasets(db, node, NULL, 0, &rdsiter);
+ check_result(result, "dns_db_allrdatasets");
+
+ result = dns_rdatasetiter_first(rdsiter);
+
+ while (result == ISC_R_SUCCESS) {
+ dns_rdatasetiter_current(rdsiter, &rdataset);
+ result = dns_rdataset_first(&rdataset);
+ check_result(result, "dns_rdataset_first");
+ while (result == ISC_R_SUCCESS) {
+ dns_rdataset_current(&rdataset, &rdata);
+ addrdata(name, rdataset.ttl, &rdata);
+ dns_rdata_reset(&rdata);
+ result = dns_rdataset_next(&rdataset);
+ }
+ dns_rdataset_disassociate(&rdataset);
+ result = dns_rdatasetiter_next(rdsiter);
+ }
+ dns_rdatasetiter_destroy(&rdsiter);
+ dns_db_detachnode(db, &node);
+ result = dns_dbiterator_next(dbiter);
+ }
+
+ snprintf(str, sizeof(str), "COMMIT TRANSACTION");
+ printf("%s\n", str);
+ res = PQexec(conn, str);
+ if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
+ fprintf(stderr, "COMMIT command failed: %s\n",
+ PQresultErrorMessage(res));
+ PQclear(res);
+ closeandexit(1);
+ }
+ PQclear(res);
+ dns_dbiterator_destroy(&dbiter);
+ dns_db_detach(&db);
+ isc_hash_destroy();
+ isc_entropy_detach(&ectx);
+ isc_mem_destroy(&mctx);
+ closeandexit(0);
+ exit(0);
+}