4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * Copyright (C) 2007, 2016 Internet Systems Consortium, Inc. ("ISC")
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews *
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt/* $Id: sqlitedb.c,v 1.2 2011/10/11 00:09:02 each Exp $ */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <config.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <stdio.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <string.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <stdlib.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <unistd.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <sqlite3.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/mem.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/print.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/result.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/util.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/sdb.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/result.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <named/globals.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include "sqlitedb.h"
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * A simple database driver that interfaces to a SQLite database.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews *
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * The table must contain the fields "name", "rdtype", and "rdata", and
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * is expected to contain a properly constructed zone. The program "zonetodb"
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * creates such a table.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic dns_sdbimplementation_t *sqlitedb = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewstypedef struct _dbinfo {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3 *db;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *filename;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *table;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews} dbinfo_t;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic isc_result_t
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsdb_connect(dbinfo_t *dbi)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (sqlite3_open(dbi->filename, &dbi->db) == SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_SUCCESS);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews } else {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* a connection is returned even if the open fails */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_close(dbi->db);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->db = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_FAILURE);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewstypedef struct _lookup_parm_t {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int i;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_sdblookup_t *lookup;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_result_t result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews} lookup_parm_t;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic int
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_lookup_cb(void *p, int cc, char **cv, char **cn)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews lookup_parm_t *parm = p;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_ttl_t ttl;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *endp;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* FIXME - check these(num/names); I'm assuming a mapping for now */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *ttlstr = cv[0];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *type = cv[1];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *data = cv[2];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cc);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cn);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews ttl = strtol(ttlstr, &endp, 10);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (*endp) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews parm->result = DNS_R_BADTTL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 1;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews parm->result = dns_sdb_putrr(parm->lookup, type, ttl, data);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (parm->result != ISC_R_SUCCESS)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 1;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews (parm->i)++;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#ifdef DNS_CLIENTINFO_VERSION
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic isc_result_t
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntsqlitedb_lookup(const char *zone, const char *name, void *dbdata,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt dns_sdblookup_t *lookup, dns_clientinfomethods_t *methods,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt dns_clientinfo_t *clientinfo)
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#else
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntstatic isc_result_t
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntsqlitedb_lookup(const char *zone, const char *name, void *dbdata,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_sdblookup_t *lookup)
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#endif /* DNS_CLIENTINFO_VERSION */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * synchronous absolute name lookup
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbinfo_t *dbi = (dbinfo_t *) dbdata;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *sql;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews lookup_parm_t parm = { 0, lookup, ISC_R_SUCCESS };
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *errmsg = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(zone);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#ifdef DNS_CLIENTINFO_VERSION
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt UNUSED(methods);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt UNUSED(clientinfo);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#endif /* DNS_CLIENTINFO_VERSION */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sql = sqlite3_mprintf(
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "SELECT TTL,RDTYPE,RDATA FROM \"%q\" WHERE "
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "lower(NAME) = lower('%q')",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->table, name);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = sqlite3_exec(dbi->db, sql,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews &sqlitedb_lookup_cb, &parm,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (result != SQLITE_OK)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_FAILURE);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (parm.i == 0)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_NOTFOUND);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_SUCCESS);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewstypedef struct _allnodes_parm_t {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int i;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_sdballnodes_t *allnodes;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_result_t result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews} allnodes_parm_t;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic int
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_allnodes_cb(void *p, int cc, char **cv, char **cn)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews allnodes_parm_t *parm = p;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_ttl_t ttl;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *endp;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* FIXME - check these(num/names); I'm assuming a mapping for now */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *ttlstr = cv[0];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *name = cv[1];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *type = cv[2];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *data = cv[3];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cc);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cn);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews ttl = strtol(ttlstr, &endp, 10);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (*endp) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews parm->result = DNS_R_BADTTL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 1;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews parm->result = dns_sdb_putnamedrr(parm->allnodes, name, type, ttl, data);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (parm->result != ISC_R_SUCCESS)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 1;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews (parm->i)++;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic isc_result_t
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_allnodes(const char *zone,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews void *dbdata,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_sdballnodes_t *allnodes)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbinfo_t *dbi = (dbinfo_t *) dbdata;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *sql;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews allnodes_parm_t parm = { 0, allnodes, ISC_R_SUCCESS };
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *errmsg = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(zone);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sql = sqlite3_mprintf(
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "SELECT TTL,NAME,RDTYPE,RDATA FROM \"%q\" ORDER BY NAME",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->table);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = sqlite3_exec(dbi->db, sql,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews &sqlitedb_allnodes_cb, &parm,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (result != SQLITE_OK)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_FAILURE);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (parm.i == 0)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_NOTFOUND);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_SUCCESS);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic void
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_destroy(const char *zone, void *driverdata, void **dbdata)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbinfo_t *dbi = *dbdata;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(zone);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(driverdata);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (dbi->db != NULL)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_close(dbi->db);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (dbi->table != NULL)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_mem_free(ns_g_mctx, dbi->table);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (dbi->filename != NULL)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_mem_free(ns_g_mctx, dbi->filename);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_mem_put(ns_g_mctx, dbi, sizeof(dbinfo_t));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#define STRDUP_OR_FAIL(target, source) \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews do { \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews target = isc_mem_strdup(ns_g_mctx, source); \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (target == NULL) { \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = ISC_R_NOMEMORY; \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews goto cleanup; \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews } \
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews } while (0);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Create a connection to the database and save any necessary information
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * in dbdata.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews *
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * argv[0] is the name of the database file
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * argv[1] is the name of the table
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic isc_result_t
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_create(const char *zone,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int argc, char **argv,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews void *driverdata, void **dbdata)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbinfo_t *dbi;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_result_t result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(zone);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(driverdata);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (argc < 2)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_FAILURE);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi = isc_mem_get(ns_g_mctx, sizeof(dbinfo_t));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (dbi == NULL)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_NOMEMORY);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->db = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->filename = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->table = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews STRDUP_OR_FAIL(dbi->filename, argv[0]);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews STRDUP_OR_FAIL(dbi->table, argv[1]);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = db_connect(dbi);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (result != ISC_R_SUCCESS)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews goto cleanup;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews *dbdata = dbi;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_SUCCESS);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewscleanup:
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlitedb_destroy(zone, driverdata, (void **)&dbi);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (result);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Since the SQL database corresponds to a zone, the authority data should
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * be returned by the lookup() function. Therefore the authority() function
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * is NULL.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic dns_sdbmethods_t sqlitedb_methods = {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlitedb_lookup,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews NULL, /* authority */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlitedb_allnodes,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlitedb_create,
58f1ac8dadf2c1f215343a0b2d1df2df954c4b19Mark Andrews sqlitedb_destroy,
58f1ac8dadf2c1f215343a0b2d1df2df954c4b19Mark Andrews NULL /* lookup2 */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews};
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Wrapper around dns_sdb_register().
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsisc_result_t
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_init(void)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews unsigned int flags;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews flags = 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (dns_sdb_register("sqlite", &sqlitedb_methods, NULL, flags,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews ns_g_mctx, &sqlitedb));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Wrapper around dns_sdb_unregister().
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsvoid
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_clear(void)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (sqlitedb != NULL)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_sdb_unregister(&sqlitedb);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}