Adam Tkac c30533
--- bind-9.3.4/contrib/sdb/sqlite/sqlitedb.c.sdb-sqlite-src	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
+++ bind-9.3.4/contrib/sdb/sqlite/sqlitedb.c	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
@@ -0,0 +1,324 @@
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Copyright (C) 2007  Internet Software Consortium.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * Permission to use, copy, modify, and distribute this software for any
Adam Tkac c30533
+ * purpose with or without fee is hereby granted, provided that the above
Adam Tkac c30533
+ * copyright notice and this permission notice appear in all copies.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
Adam Tkac c30533
+ * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
Adam Tkac c30533
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
Adam Tkac c30533
+ * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
Adam Tkac c30533
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
Adam Tkac c30533
+ * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
Adam Tkac c30533
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
Adam Tkac c30533
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Adam Tkac c30533
+ */
Adam Tkac c30533
+
Adam Tkac c30533
+/* $Id: sqlitedb.c Exp $ */
Adam Tkac c30533
+
Adam Tkac c30533
+#include <config.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <stdio.h>
Adam Tkac c30533
+#include <string.h>
Adam Tkac c30533
+#include <stdlib.h>
Adam Tkac c30533
+#include <unistd.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <sqlite3.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <isc/mem.h>
Adam Tkac c30533
+#include <isc/print.h>
Adam Tkac c30533
+#include <isc/result.h>
Adam Tkac c30533
+#include <isc/util.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <dns/sdb.h>
Adam Tkac c30533
+#include <dns/result.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <named/globals.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include "sqlitedb.h"
Adam Tkac c30533
+
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * A simple database driver that interfaces to a SQLite database.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * The table must contain the fields "name", "rdtype", and "rdata", and 
Adam Tkac c30533
+ * is expected to contain a properly constructed zone.  The program "zonetodb"
Adam Tkac c30533
+ * creates such a table.
Adam Tkac c30533
+ */
Adam Tkac c30533
+
Adam Tkac c30533
+static dns_sdbimplementation_t *sqlitedb = NULL;
Adam Tkac c30533
+
Adam Tkac c30533
+typedef struct _dbinfo {
Adam Tkac c30533
+    sqlite3 *db;
Adam Tkac c30533
+    char *filename;
Adam Tkac c30533
+    char *table;
Adam Tkac c30533
+} dbinfo_t;
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static isc_result_t
Adam Tkac c30533
+db_connect(dbinfo_t *dbi)
Adam Tkac c30533
+{
Adam Tkac c30533
+    if (sqlite3_open(dbi->filename, &dbi->db) == SQLITE_OK) {
Adam Tkac c30533
+	return (ISC_R_SUCCESS);
Adam Tkac c30533
+    } else {
Adam Tkac c30533
+	/* a connection is returned even if the open fails */
Adam Tkac c30533
+	sqlite3_close(dbi->db);
Adam Tkac c30533
+	dbi->db = NULL;
Adam Tkac c30533
+	return (ISC_R_FAILURE);
Adam Tkac c30533
+    }
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+typedef struct _lookup_parm_t {
Adam Tkac c30533
+    int              i;
Adam Tkac c30533
+    dns_sdblookup_t *lookup;
Adam Tkac c30533
+    isc_result_t     result;
Adam Tkac c30533
+} lookup_parm_t;
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static int
Adam Tkac c30533
+sqlitedb_lookup_cb(void *p, int cc, char **cv, char **cn)
Adam Tkac c30533
+{
Adam Tkac c30533
+    lookup_parm_t *parm = p;
Adam Tkac c30533
+    dns_ttl_t ttl;
Adam Tkac c30533
+    char *endp;
Adam Tkac c30533
+
Adam Tkac c30533
+    /* FIXME - check these(num/names); I'm assuming a mapping for now */
Adam Tkac c30533
+    char *ttlstr = cv[0];
Adam Tkac c30533
+    char *type   = cv[1];
Adam Tkac c30533
+    char *data   = cv[2];
Adam Tkac c30533
+
Adam Tkac c30533
+    UNUSED(cc);
Adam Tkac c30533
+    UNUSED(cn);
Adam Tkac c30533
+
Adam Tkac c30533
+    ttl = strtol(ttlstr, &endp, 10);
Adam Tkac c30533
+    if (*endp) {
Adam Tkac c30533
+	parm->result = DNS_R_BADTTL;
Adam Tkac c30533
+	return 1;
Adam Tkac c30533
+    }
Adam Tkac c30533
+
Adam Tkac c30533
+    parm->result = dns_sdb_putrr(parm->lookup, type, ttl, data);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (parm->result != ISC_R_SUCCESS)
Adam Tkac c30533
+	return 1;
Adam Tkac c30533
+
Adam Tkac c30533
+    (parm->i)++;
Adam Tkac c30533
+
Adam Tkac c30533
+    return 0;
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static isc_result_t
Adam Tkac c30533
+sqlitedb_lookup(const char *zone,
Adam Tkac c30533
+		const char *name, void *dbdata,
Adam Tkac c30533
+		dns_sdblookup_t *lookup)
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * synchronous absolute name lookup
Adam Tkac c30533
+ */
Adam Tkac c30533
+{
Adam Tkac c30533
+    dbinfo_t *dbi = (dbinfo_t *) dbdata;
Adam Tkac c30533
+    char *sql;
Adam Tkac c30533
+    lookup_parm_t parm = { 0, lookup, ISC_R_SUCCESS };
Adam Tkac c30533
+    char *errmsg = NULL;
Adam Tkac c30533
+    int result;
Adam Tkac c30533
+
Adam Tkac c30533
+    UNUSED(zone);
Adam Tkac c30533
+
Adam Tkac c30533
+    sql = sqlite3_mprintf(
Adam Tkac c30533
+	"SELECT TTL,RDTYPE,RDATA FROM \"%q\" WHERE "
Adam Tkac c30533
+	"lower(NAME) = lower('%q')",
Adam Tkac c30533
+	dbi->table, name);
Adam Tkac c30533
+
Adam Tkac c30533
+    result = sqlite3_exec(dbi->db, sql,
Adam Tkac c30533
+			  &sqlitedb_lookup_cb, &parm,
Adam Tkac c30533
+			  &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (result != SQLITE_OK)
Adam Tkac c30533
+	return (ISC_R_FAILURE);
Adam Tkac c30533
+    if (parm.i == 0)
Adam Tkac c30533
+	return (ISC_R_NOTFOUND);
Adam Tkac c30533
+
Adam Tkac c30533
+    return (ISC_R_SUCCESS);
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+typedef struct _allnodes_parm_t {
Adam Tkac c30533
+    int                i;
Adam Tkac c30533
+    dns_sdballnodes_t *allnodes;
Adam Tkac c30533
+    isc_result_t       result;
Adam Tkac c30533
+} allnodes_parm_t;
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static int
Adam Tkac c30533
+sqlitedb_allnodes_cb(void *p, int cc, char **cv, char **cn)
Adam Tkac c30533
+{
Adam Tkac c30533
+    allnodes_parm_t *parm = p;
Adam Tkac c30533
+    dns_ttl_t ttl;
Adam Tkac c30533
+    char *endp;
Adam Tkac c30533
+
Adam Tkac c30533
+    /* FIXME - check these(num/names); I'm assuming a mapping for now */
Adam Tkac c30533
+    char *ttlstr = cv[0];
Adam Tkac c30533
+    char *name   = cv[1];
Adam Tkac c30533
+    char *type   = cv[2];
Adam Tkac c30533
+    char *data   = cv[3];
Adam Tkac c30533
+
Adam Tkac c30533
+    UNUSED(cc);
Adam Tkac c30533
+    UNUSED(cn);
Adam Tkac c30533
+
Adam Tkac c30533
+    ttl = strtol(ttlstr, &endp, 10);
Adam Tkac c30533
+    if (*endp) {
Adam Tkac c30533
+	parm->result = DNS_R_BADTTL;
Adam Tkac c30533
+	return 1;
Adam Tkac c30533
+    }
Adam Tkac c30533
+
Adam Tkac c30533
+    parm->result = dns_sdb_putnamedrr(parm->allnodes, name, type, ttl, data);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (parm->result != ISC_R_SUCCESS)
Adam Tkac c30533
+	return 1;
Adam Tkac c30533
+
Adam Tkac c30533
+    (parm->i)++;
Adam Tkac c30533
+
Adam Tkac c30533
+    return 0;
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static isc_result_t
Adam Tkac c30533
+sqlitedb_allnodes(const char *zone,
Adam Tkac c30533
+		  void *dbdata,
Adam Tkac c30533
+		  dns_sdballnodes_t *allnodes)
Adam Tkac c30533
+{
Adam Tkac c30533
+    dbinfo_t *dbi = (dbinfo_t *) dbdata;
Adam Tkac c30533
+    char *sql;
Adam Tkac c30533
+    allnodes_parm_t parm = { 0, allnodes, ISC_R_SUCCESS };
Adam Tkac c30533
+    char *errmsg = NULL;
Adam Tkac c30533
+    int result;
Adam Tkac c30533
+
Adam Tkac c30533
+    UNUSED(zone);
Adam Tkac c30533
+
Adam Tkac c30533
+    sql = sqlite3_mprintf(
Adam Tkac c30533
+	"SELECT TTL,NAME,RDTYPE,RDATA FROM \"%q\" ORDER BY NAME",
Adam Tkac c30533
+	dbi->table);
Adam Tkac c30533
+
Adam Tkac c30533
+    result = sqlite3_exec(dbi->db, sql,
Adam Tkac c30533
+			  &sqlitedb_allnodes_cb, &parm,
Adam Tkac c30533
+			  &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (result != SQLITE_OK)
Adam Tkac c30533
+	return (ISC_R_FAILURE);
Adam Tkac c30533
+    if (parm.i == 0)
Adam Tkac c30533
+	return (ISC_R_NOTFOUND);
Adam Tkac c30533
+
Adam Tkac c30533
+    return (ISC_R_SUCCESS);
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static void
Adam Tkac c30533
+sqlitedb_destroy(const char *zone, void *driverdata, void **dbdata)
Adam Tkac c30533
+{
Adam Tkac c30533
+    dbinfo_t *dbi = *dbdata;
Adam Tkac c30533
+
Adam Tkac c30533
+    UNUSED(zone);
Adam Tkac c30533
+    UNUSED(driverdata);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (dbi->db != NULL)
Adam Tkac c30533
+	sqlite3_close(dbi->db);
Adam Tkac c30533
+    if (dbi->table != NULL)
Adam Tkac c30533
+	isc_mem_free(ns_g_mctx, dbi->table);
Adam Tkac c30533
+    if (dbi->filename != NULL)
Adam Tkac c30533
+	isc_mem_free(ns_g_mctx, dbi->filename);
Adam Tkac c30533
+
Adam Tkac c30533
+    isc_mem_put(ns_g_mctx, dbi, sizeof(dbinfo_t));
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+#define STRDUP_OR_FAIL(target, source)				\
Adam Tkac c30533
+	do {							\
Adam Tkac c30533
+		target = isc_mem_strdup(ns_g_mctx, source);	\
Adam Tkac c30533
+		if (target == NULL) {				\
Adam Tkac c30533
+			result = ISC_R_NOMEMORY;		\
Adam Tkac c30533
+			goto cleanup;				\
Adam Tkac c30533
+		}						\
Adam Tkac c30533
+	} while (0);
Adam Tkac c30533
+
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Create a connection to the database and save any necessary information
Adam Tkac c30533
+ * in dbdata.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * argv[0] is the name of the database file
Adam Tkac c30533
+ * argv[1] is the name of the table
Adam Tkac c30533
+ */
Adam Tkac c30533
+static isc_result_t
Adam Tkac c30533
+sqlitedb_create(const char *zone,
Adam Tkac c30533
+		int argc, char **argv,
Adam Tkac c30533
+		void *driverdata, void **dbdata)
Adam Tkac c30533
+{
Adam Tkac c30533
+    dbinfo_t *dbi;
Adam Tkac c30533
+    isc_result_t result;
Adam Tkac c30533
+
Adam Tkac c30533
+    UNUSED(zone);
Adam Tkac c30533
+    UNUSED(driverdata);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (argc < 2)
Adam Tkac c30533
+	return (ISC_R_FAILURE);
Adam Tkac c30533
+
Adam Tkac c30533
+    dbi = isc_mem_get(ns_g_mctx, sizeof(dbinfo_t));
Adam Tkac c30533
+    if (dbi == NULL)
Adam Tkac c30533
+	return (ISC_R_NOMEMORY);
Adam Tkac c30533
+    dbi->db       = NULL;
Adam Tkac c30533
+    dbi->filename = NULL;
Adam Tkac c30533
+    dbi->table    = NULL;
Adam Tkac c30533
+
Adam Tkac c30533
+    STRDUP_OR_FAIL(dbi->filename, argv[0]);
Adam Tkac c30533
+    STRDUP_OR_FAIL(dbi->table, argv[1]);
Adam Tkac c30533
+
Adam Tkac c30533
+    result = db_connect(dbi);
Adam Tkac c30533
+    if (result != ISC_R_SUCCESS)
Adam Tkac c30533
+	goto cleanup;
Adam Tkac c30533
+
Adam Tkac c30533
+    *dbdata = dbi;
Adam Tkac c30533
+    return (ISC_R_SUCCESS);
Adam Tkac c30533
+
Adam Tkac c30533
+cleanup:
Adam Tkac c30533
+    sqlitedb_destroy(zone, driverdata, (void **)&dbi;;
Adam Tkac c30533
+    return (result);
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Since the SQL database corresponds to a zone, the authority data should
Adam Tkac c30533
+ * be returned by the lookup() function.  Therefore the authority() function
Adam Tkac c30533
+ * is NULL.
Adam Tkac c30533
+ */
Adam Tkac c30533
+static dns_sdbmethods_t sqlitedb_methods = {
Adam Tkac c30533
+    sqlitedb_lookup,
Adam Tkac c30533
+    NULL, /* authority */
Adam Tkac c30533
+    sqlitedb_allnodes,
Adam Tkac c30533
+    sqlitedb_create,
Adam Tkac c30533
+    sqlitedb_destroy
Adam Tkac c30533
+};
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Wrapper around dns_sdb_register().
Adam Tkac c30533
+ */
Adam Tkac c30533
+isc_result_t
Adam Tkac c30533
+sqlitedb_init(void)
Adam Tkac c30533
+{
Adam Tkac c30533
+    unsigned int flags;
Adam Tkac c30533
+    flags = 0;
Adam Tkac c30533
+    return (dns_sdb_register("sqlite", &sqlitedb_methods, NULL, flags,
Adam Tkac c30533
+			     ns_g_mctx, &sqlitedb));
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Wrapper around dns_sdb_unregister().
Adam Tkac c30533
+ */
Adam Tkac c30533
+void
Adam Tkac c30533
+sqlitedb_clear(void)
Adam Tkac c30533
+{
Adam Tkac c30533
+    if (sqlitedb != NULL)
Adam Tkac c30533
+	dns_sdb_unregister(&sqlitedb);
Adam Tkac c30533
+}
Adam Tkac c30533
--- bind-9.3.4/contrib/sdb/sqlite/sqlitedb.h.sdb-sqlite-src	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
+++ bind-9.3.4/contrib/sdb/sqlite/sqlitedb.h	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
@@ -0,0 +1,25 @@
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Copyright (C) 2000-2002  Internet Software Consortium.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * Permission to use, copy, modify, and distribute this software for any
Adam Tkac c30533
+ * purpose with or without fee is hereby granted, provided that the above
Adam Tkac c30533
+ * copyright notice and this permission notice appear in all copies.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
Adam Tkac c30533
+ * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
Adam Tkac c30533
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
Adam Tkac c30533
+ * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
Adam Tkac c30533
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
Adam Tkac c30533
+ * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
Adam Tkac c30533
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
Adam Tkac c30533
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Adam Tkac c30533
+ */
Adam Tkac c30533
+
Adam Tkac c30533
+/* $Id: pgsqldb.h,v 1.2.4.2 2002/08/05 06:57:07 marka Exp $ */
Adam Tkac c30533
+
Adam Tkac c30533
+#include <isc/types.h>
Adam Tkac c30533
+
Adam Tkac c30533
+isc_result_t sqlitedb_init(void);
Adam Tkac c30533
+
Adam Tkac c30533
+void sqlitedb_clear(void);
Adam Tkac c30533
+
Adam Tkac c30533
--- bind-9.3.4/contrib/sdb/sqlite/README.sdb_sqlite.sdb-sqlite-src	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
+++ bind-9.3.4/contrib/sdb/sqlite/README.sdb_sqlite	2007-03-01 23:17:01.000000000 -0500
Adam Tkac c30533
@@ -0,0 +1,67 @@
Adam Tkac c30533
+			SQLite BIND SDB driver
Adam Tkac c30533
+
Adam Tkac c30533
+The SQLite BIND SDB "driver" is intended as an alternative both to the
Adam Tkac c30533
+pgsqldb and dirdb drivers, for situations that would like the management
Adam Tkac c30533
+simplicity and convenience of single filesystem files, with the additional
Adam Tkac c30533
+capability of SQL databases.  It is also intended as an alternative to
Adam Tkac c30533
+the standard dynamic DNS update capability in bind, which effectively
Adam Tkac c30533
+requires use of DNSSEC keys for authorization, and is limited to 'nsupdate'
Adam Tkac c30533
+for updates.  An sqlite database, by contrast, uses and requires only
Adam Tkac c30533
+normal filesystem permissions, and may be updated however a typical SQLite
Adam Tkac c30533
+database might be updated, e.g., via a web service with an SQLite backend.
Adam Tkac c30533
+
Adam Tkac c30533
+This driver is not considered suitable for very high volume public
Adam Tkac c30533
+nameserver use, while likely useful for smaller private nameserver
Adam Tkac c30533
+applications, whether or not in a production environment.  It should
Adam Tkac c30533
+generally be suitable wherever SQLite is preferable over larger database
Adam Tkac c30533
+engines, and not suitable where SQLite is not preferable.
Adam Tkac c30533
+
Adam Tkac c30533
+Usage:
Adam Tkac c30533
+
Adam Tkac c30533
+o Use the named_sdb process ( put ENABLE_SDB=yes in /etc/sysconfig/named )
Adam Tkac c30533
+
Adam Tkac c30533
+o Edit your named.conf to contain a database zone, eg.:
Adam Tkac c30533
+  
Adam Tkac c30533
+zone "mydomain.net." IN {
Adam Tkac c30533
+        type master;
Adam Tkac c30533
+        database "sqlite mydomain.db mydomain";
Adam Tkac c30533
+        #                ^- DB name  ^-Table
Adam Tkac c30533
+};
Adam Tkac c30533
+
Adam Tkac c30533
+o Create the database zone table
Adam Tkac c30533
+  The table must contain the columns "name", "rdtype", and "rdata", and
Adam Tkac c30533
+  is expected to contain a properly constructed zone.  The program
Adam Tkac c30533
+  "zone2sqlite" creates such a table.
Adam Tkac c30533
+  
Adam Tkac c30533
+  zone2sqlite usage:
Adam Tkac c30533
+    
Adam Tkac c30533
+    zone2sqlite origin zonefile dbfile dbtable
Adam Tkac c30533
+
Adam Tkac c30533
+    where
Adam Tkac c30533
+	origin   : zone origin, eg "mydomain.net."
Adam Tkac c30533
+	zonefile : master zone database file, eg. mydomain.net.zone
Adam Tkac c30533
+	dbfile   : name of SQLite database file
Adam Tkac c30533
+        dbtable  : name of table in database
Adam Tkac c30533
+
Adam Tkac c30533
+---
Adam Tkac c30533
+# mydomain.net.zone:
Adam Tkac c30533
+$TTL 1H
Adam Tkac c30533
+@       SOA     localhost.      root.localhost. (       1
Adam Tkac c30533
+                                                3H
Adam Tkac c30533
+                                                1H
Adam Tkac c30533
+                                                1W
Adam Tkac c30533
+                                                1H )
Adam Tkac c30533
+        NS      localhost.
Adam Tkac c30533
+host1   A       192.168.2.1
Adam Tkac c30533
+host2   A       192.168.2.2
Adam Tkac c30533
+host3   A       192.168.2.3
Adam Tkac c30533
+host4   A       192.168.2.4
Adam Tkac c30533
+host5   A       192.168.2.5
Adam Tkac c30533
+host6   A       192.168.2.6
Adam Tkac c30533
+host7   A       192.168.2.7
Adam Tkac c30533
+---
Adam Tkac c30533
+
Adam Tkac c30533
+# zone2sqlite mydomain.net. mydomain.net.zone mydomain.net.db mydomain
Adam Tkac c30533
+
Adam Tkac c30533
+will create/update the 'mydomain' table in database file 'mydomain.net.db'.
Adam Tkac c30533
+
Adam Tkac c30533
--- bind-9.3.4/contrib/sdb/sqlite/zone2sqlite.c.sdb-sqlite-src	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
+++ bind-9.3.4/contrib/sdb/sqlite/zone2sqlite.c	2007-03-01 23:06:02.000000000 -0500
Adam Tkac c30533
@@ -0,0 +1,301 @@
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Copyright (C) 2007  Internet Software Consortium.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * Permission to use, copy, modify, and distribute this software for any
Adam Tkac c30533
+ * purpose with or without fee is hereby granted, provided that the above
Adam Tkac c30533
+ * copyright notice and this permission notice appear in all copies.
Adam Tkac c30533
+ *
Adam Tkac c30533
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
Adam Tkac c30533
+ * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
Adam Tkac c30533
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
Adam Tkac c30533
+ * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
Adam Tkac c30533
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
Adam Tkac c30533
+ * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
Adam Tkac c30533
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
Adam Tkac c30533
+ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Adam Tkac c30533
+ */
Adam Tkac c30533
+
Adam Tkac c30533
+/* $Id: zonetosqlite.c Exp $ */
Adam Tkac c30533
+
Adam Tkac c30533
+#include <stdlib.h>
Adam Tkac c30533
+#include <string.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <isc/buffer.h>
Adam Tkac c30533
+#include <isc/mem.h>
Adam Tkac c30533
+#include <isc/print.h>
Adam Tkac c30533
+#include <isc/result.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <dns/db.h>
Adam Tkac c30533
+#include <dns/dbiterator.h>
Adam Tkac c30533
+#include <dns/fixedname.h>
Adam Tkac c30533
+#include <dns/name.h>
Adam Tkac c30533
+#include <dns/rdata.h>
Adam Tkac c30533
+#include <dns/rdataset.h>
Adam Tkac c30533
+#include <dns/rdatasetiter.h>
Adam Tkac c30533
+#include <dns/rdatatype.h>
Adam Tkac c30533
+#include <dns/result.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#include <sqlite3.h>
Adam Tkac c30533
+
Adam Tkac c30533
+#ifndef UNUSED
Adam Tkac c30533
+#define UNUSED(x)  (x) = (x)
Adam Tkac c30533
+#endif
Adam Tkac c30533
+
Adam Tkac c30533
+/*
Adam Tkac c30533
+ * Generate an SQLite table from a zone.
Adam Tkac c30533
+ */
Adam Tkac c30533
+
Adam Tkac c30533
+typedef struct _dbinfo {
Adam Tkac c30533
+    sqlite3 *db;
Adam Tkac c30533
+    char *filename;
Adam Tkac c30533
+    char *table;
Adam Tkac c30533
+} dbinfo_t;
Adam Tkac c30533
+
Adam Tkac c30533
+dbinfo_t dbi = { NULL, NULL, NULL };
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static void
Adam Tkac c30533
+closeandexit(int status)
Adam Tkac c30533
+{
Adam Tkac c30533
+    if (dbi.db) {
Adam Tkac c30533
+	sqlite3_close(dbi.db);
Adam Tkac c30533
+	dbi.db = NULL;
Adam Tkac c30533
+    }
Adam Tkac c30533
+    exit(status);
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+static void
Adam Tkac c30533
+check_result(isc_result_t result, const char *message)
Adam Tkac c30533
+{
Adam Tkac c30533
+    if (result != ISC_R_SUCCESS) {
Adam Tkac c30533
+	fprintf(stderr, "%s: %s\n", message,
Adam Tkac c30533
+		isc_result_totext(result));
Adam Tkac c30533
+	closeandexit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+static isc_result_t
Adam Tkac c30533
+db_connect(dbinfo_t *dbi)
Adam Tkac c30533
+{
Adam Tkac c30533
+    if (sqlite3_open(dbi->filename, &dbi->db) == SQLITE_OK) {
Adam Tkac c30533
+	return (ISC_R_SUCCESS);
Adam Tkac c30533
+    } else {
Adam Tkac c30533
+	/* a connection is returned even if the open fails */
Adam Tkac c30533
+	sqlite3_close(dbi->db);
Adam Tkac c30533
+	dbi->db = NULL;
Adam Tkac c30533
+	return (ISC_R_FAILURE);
Adam Tkac c30533
+    }
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+static int
Adam Tkac c30533
+add_rdata_cb(void *parm, int cc, char **cv, char **cn)
Adam Tkac c30533
+{
Adam Tkac c30533
+    UNUSED(parm);
Adam Tkac c30533
+    UNUSED(cc);
Adam Tkac c30533
+    UNUSED(cv);
Adam Tkac c30533
+    UNUSED(cn);
Adam Tkac c30533
+
Adam Tkac c30533
+    return 0;
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+
Adam Tkac c30533
+static void
Adam Tkac c30533
+addrdata(dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata)
Adam Tkac c30533
+{
Adam Tkac c30533
+    unsigned char namearray[DNS_NAME_MAXTEXT + 1];
Adam Tkac c30533
+    unsigned char typearray[20];
Adam Tkac c30533
+    unsigned char dataarray[2048];
Adam Tkac c30533
+    isc_buffer_t b;
Adam Tkac c30533
+    isc_result_t result;
Adam Tkac c30533
+    char *sql;
Adam Tkac c30533
+    char *errmsg = NULL;
Adam Tkac c30533
+    int res;
Adam Tkac c30533
+    
Adam Tkac c30533
+    isc_buffer_init(&b, namearray, sizeof(namearray) - 1);
Adam Tkac c30533
+    result = dns_name_totext(name, ISC_TRUE, &b);
Adam Tkac c30533
+    check_result(result, "dns_name_totext");
Adam Tkac c30533
+    namearray[isc_buffer_usedlength(&b)] = 0;
Adam Tkac c30533
+    
Adam Tkac c30533
+    isc_buffer_init(&b, typearray, sizeof(typearray) - 1);
Adam Tkac c30533
+    result = dns_rdatatype_totext(rdata->type, &b);
Adam Tkac c30533
+    check_result(result, "dns_rdatatype_totext");
Adam Tkac c30533
+    typearray[isc_buffer_usedlength(&b)] = 0;
Adam Tkac c30533
+    
Adam Tkac c30533
+    isc_buffer_init(&b, dataarray, sizeof(dataarray) - 1);
Adam Tkac c30533
+    result = dns_rdata_totext(rdata, NULL, &b);
Adam Tkac c30533
+    check_result(result, "dns_rdata_totext");
Adam Tkac c30533
+    dataarray[isc_buffer_usedlength(&b)] = 0;
Adam Tkac c30533
+    
Adam Tkac c30533
+    sql = sqlite3_mprintf(
Adam Tkac c30533
+	"INSERT INTO %q (NAME, TTL, RDTYPE, RDATA)"
Adam Tkac c30533
+	" VALUES ('%q', %d, '%q', '%q') ",
Adam Tkac c30533
+	dbi.table,
Adam Tkac c30533
+	namearray, ttl, typearray, dataarray);
Adam Tkac c30533
+    printf("%s\n", sql);
Adam Tkac c30533
+    res = sqlite3_exec(dbi.db, sql, add_rdata_cb, NULL, &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+
Adam Tkac c30533
+    if (result != SQLITE_OK) {
Adam Tkac c30533
+	fprintf(stderr, "INSERT failed: %s\n", errmsg);
Adam Tkac c30533
+	closeandexit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+}
Adam Tkac c30533
+
Adam Tkac c30533
+int
Adam Tkac c30533
+main(int argc, char *argv[])
Adam Tkac c30533
+{
Adam Tkac c30533
+    char *sql;
Adam Tkac c30533
+    int res;
Adam Tkac c30533
+    char *errmsg = NULL;
Adam Tkac c30533
+    char *porigin, *zonefile;
Adam Tkac c30533
+    dns_fixedname_t forigin, fname;
Adam Tkac c30533
+    dns_name_t *origin, *name;
Adam Tkac c30533
+    dns_db_t *db = NULL;
Adam Tkac c30533
+    dns_dbiterator_t *dbiter;
Adam Tkac c30533
+    dns_dbnode_t *node;
Adam Tkac c30533
+    dns_rdatasetiter_t *rdsiter;
Adam Tkac c30533
+    dns_rdataset_t rdataset;
Adam Tkac c30533
+    dns_rdata_t rdata = DNS_RDATA_INIT;
Adam Tkac c30533
+    isc_mem_t *mctx = NULL;
Adam Tkac c30533
+    isc_buffer_t b;
Adam Tkac c30533
+    isc_result_t result;
Adam Tkac c30533
+
Adam Tkac c30533
+    if (argc != 5) {
Adam Tkac c30533
+	printf("usage: %s <zone> <zonefile> <dbfile> <dbtable>\n", argv[0]);
Adam Tkac c30533
+	exit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+    
Adam Tkac c30533
+    porigin  = argv[1];
Adam Tkac c30533
+    zonefile = argv[2];
Adam Tkac c30533
+
Adam Tkac c30533
+    dbi.filename = argv[3];
Adam Tkac c30533
+    dbi.table    = argv[4];
Adam Tkac c30533
+    
Adam Tkac c30533
+    dns_result_register();
Adam Tkac c30533
+    
Adam Tkac c30533
+    mctx = NULL;
Adam Tkac c30533
+    result = isc_mem_create(0, 0, &mctx);
Adam Tkac c30533
+    check_result(result, "isc_mem_create");
Adam Tkac c30533
+    
Adam Tkac c30533
+    isc_buffer_init(&b, porigin, strlen(porigin));
Adam Tkac c30533
+    isc_buffer_add(&b, strlen(porigin));
Adam Tkac c30533
+    dns_fixedname_init(&forigin);
Adam Tkac c30533
+    origin = dns_fixedname_name(&forigin);
Adam Tkac c30533
+    result = dns_name_fromtext(origin, &b, dns_rootname, ISC_FALSE, NULL);
Adam Tkac c30533
+    check_result(result, "dns_name_fromtext");
Adam Tkac c30533
+    
Adam Tkac c30533
+    db = NULL;
Adam Tkac c30533
+    result = dns_db_create(mctx, "rbt", origin, dns_dbtype_zone,
Adam Tkac c30533
+			   dns_rdataclass_in, 0, NULL, &db);
Adam Tkac c30533
+    check_result(result, "dns_db_create");
Adam Tkac c30533
+    
Adam Tkac c30533
+    result = dns_db_load(db, zonefile);
Adam Tkac c30533
+    if (result == DNS_R_SEENINCLUDE)
Adam Tkac c30533
+	result = ISC_R_SUCCESS;
Adam Tkac c30533
+    check_result(result, "dns_db_load");
Adam Tkac c30533
+
Adam Tkac c30533
+    printf("Connecting to '%s'\n", dbi.filename);
Adam Tkac c30533
+    
Adam Tkac c30533
+    if ((result = db_connect(&dbi)) != ISC_R_SUCCESS) {
Adam Tkac c30533
+	fprintf(stderr, "Connection to database '%s' failed\n",
Adam Tkac c30533
+		dbi.filename);
Adam Tkac c30533
+	closeandexit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+    
Adam Tkac c30533
+    sql = sqlite3_mprintf("DROP TABLE %q ", dbi.table);
Adam Tkac c30533
+    printf("%s\n", sql);
Adam Tkac c30533
+    res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+#if 0
Adam Tkac c30533
+    if (res != SQLITE_OK) {
Adam Tkac c30533
+	fprintf(stderr, "DROP TABLE %s failed: %s\n",
Adam Tkac c30533
+		dbi.table, errmsg);
Adam Tkac c30533
+    }
Adam Tkac c30533
+#endif
Adam Tkac c30533
+
Adam Tkac c30533
+#if 0    
Adam Tkac c30533
+    sql = sqlite3_mprintf(sql, "BEGIN TRANSACTION");
Adam Tkac c30533
+    printf("%s\n", sql);
Adam Tkac c30533
+    res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+    if (res != SQLITE_OK) {
Adam Tkac c30533
+	fprintf(stderr, "BEGIN TRANSACTION failed: %s\n", errmsg);
Adam Tkac c30533
+	closeandexit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+#endif
Adam Tkac c30533
+    
Adam Tkac c30533
+    sql = sqlite3_mprintf(
Adam Tkac c30533
+	"CREATE TABLE %q "
Adam Tkac c30533
+	"(NAME TEXT, TTL INTEGER, RDTYPE TEXT, RDATA TEXT) ",
Adam Tkac c30533
+	dbi.table);
Adam Tkac c30533
+    printf("%s\n", sql);
Adam Tkac c30533
+    res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+    if (res != SQLITE_OK) {
Adam Tkac c30533
+	fprintf(stderr, "CREATE TABLE %s failed: %s\n",
Adam Tkac c30533
+		dbi.table, errmsg);
Adam Tkac c30533
+	closeandexit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+    
Adam Tkac c30533
+    dbiter = NULL;
Adam Tkac c30533
+    result = dns_db_createiterator(db, ISC_FALSE, &dbiter);
Adam Tkac c30533
+    check_result(result, "dns_db_createiterator()");
Adam Tkac c30533
+    
Adam Tkac c30533
+    result = dns_dbiterator_first(dbiter);
Adam Tkac c30533
+    check_result(result, "dns_dbiterator_first");
Adam Tkac c30533
+    
Adam Tkac c30533
+    dns_fixedname_init(&fname);
Adam Tkac c30533
+    name = dns_fixedname_name(&fname);
Adam Tkac c30533
+    dns_rdataset_init(&rdataset);
Adam Tkac c30533
+    dns_rdata_init(&rdata);
Adam Tkac c30533
+    
Adam Tkac c30533
+    while (result == ISC_R_SUCCESS) {
Adam Tkac c30533
+	node = NULL;
Adam Tkac c30533
+	result = dns_dbiterator_current(dbiter, &node, name);
Adam Tkac c30533
+	if (result == ISC_R_NOMORE)
Adam Tkac c30533
+	    break;
Adam Tkac c30533
+	check_result(result, "dns_dbiterator_current");
Adam Tkac c30533
+	
Adam Tkac c30533
+	rdsiter = NULL;
Adam Tkac c30533
+	result = dns_db_allrdatasets(db, node, NULL, 0, &rdsiter);
Adam Tkac c30533
+	check_result(result, "dns_db_allrdatasets");
Adam Tkac c30533
+
Adam Tkac c30533
+	result = dns_rdatasetiter_first(rdsiter);
Adam Tkac c30533
+
Adam Tkac c30533
+	while (result == ISC_R_SUCCESS) {
Adam Tkac c30533
+	    dns_rdatasetiter_current(rdsiter, &rdataset);
Adam Tkac c30533
+	    result = dns_rdataset_first(&rdataset);
Adam Tkac c30533
+	    check_result(result, "dns_rdataset_first");
Adam Tkac c30533
+	    while (result == ISC_R_SUCCESS) {
Adam Tkac c30533
+		dns_rdataset_current(&rdataset, &rdata);
Adam Tkac c30533
+		addrdata(name, rdataset.ttl, &rdata);
Adam Tkac c30533
+		dns_rdata_reset(&rdata);
Adam Tkac c30533
+		result = dns_rdataset_next(&rdataset);
Adam Tkac c30533
+	    }
Adam Tkac c30533
+	    dns_rdataset_disassociate(&rdataset);
Adam Tkac c30533
+	    result = dns_rdatasetiter_next(rdsiter);
Adam Tkac c30533
+	}
Adam Tkac c30533
+	dns_rdatasetiter_destroy(&rdsiter);
Adam Tkac c30533
+	dns_db_detachnode(db, &node);
Adam Tkac c30533
+	result = dns_dbiterator_next(dbiter);
Adam Tkac c30533
+    }
Adam Tkac c30533
+
Adam Tkac c30533
+#if 0
Adam Tkac c30533
+    sql = sqlite3_mprintf(sql, "COMMIT TRANSACTION ");
Adam Tkac c30533
+    printf("%s\n", sql);
Adam Tkac c30533
+    res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
Adam Tkac c30533
+    sqlite3_free(sql);
Adam Tkac c30533
+    if (res != SQLITE_OK) {
Adam Tkac c30533
+	fprintf(stderr, "COMMIT TRANSACTION failed: %s\n", errmsg);
Adam Tkac c30533
+	closeandexit(1);
Adam Tkac c30533
+    }
Adam Tkac c30533
+#endif
Adam Tkac c30533
+    
Adam Tkac c30533
+    dns_dbiterator_destroy(&dbiter);
Adam Tkac c30533
+    dns_db_detach(&db);
Adam Tkac c30533
+    isc_mem_destroy(&mctx);
Adam Tkac c30533
+
Adam Tkac c30533
+    closeandexit(0);
Adam Tkac c30533
+
Adam Tkac c30533
+    exit(0);
Adam Tkac c30533
+}