added sample sdb driver where the zone data is specified

algorithmically in Tcl code
This commit is contained in:
Andreas Gustafsson 2000-11-18 00:14:34 +00:00
parent 5aee7a4da4
commit 22aa1c0bc2
6 changed files with 448 additions and 0 deletions

17
contrib/sdb/lookup.tcl Normal file
View file

@ -0,0 +1,17 @@
#
# Sample lookup procedure for tcldb
#
proc lookup {zone name} {
global dbargs
puts $zone
switch -- $name {
@ { return [list \
{SOA 86400 "ns.isp.nil. hostmaster.isp.nil. 0 3600 1800 1814400 3600"} \
{NS 86400 "ns1"} \
{NS 86400 "ns2"} \
{MX 86400 "10 mail.isp.nil."} ] }
www { return [list [list A 3600 $dbargs($zone)] ] }
}
return NXDOMAIN
}

View file

@ -0,0 +1,17 @@
#
# Sample lookup procedure for tcldb
#
proc lookup {zone name} {
global dbargs
puts $zone
switch -- $name {
@ { return [list \
{SOA 86400 "ns.isp.nil. hostmaster.isp.nil. 0 3600 1800 1814400 3600"} \
{NS 86400 "ns1"} \
{NS 86400 "ns2"} \
{MX 86400 "10 mail.isp.nil."} ] }
www { return [list [list A 3600 $dbargs($zone)] ] }
}
return NXDOMAIN
}

201
contrib/sdb/tcl/tcldb.c Normal file
View file

@ -0,0 +1,201 @@
#include <config.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/result.h>
#include <isc/util.h>
#include <dns/log.h>
#include <dns/sdb.h>
#include <named/globals.h>
#include <tcl.h>
#include <tcldb.h>
/*
* A simple database driver that calls Tcl procedures to define
* the contents of the DNS namespace.
*/
static dns_sdbimplementation_t *tcldb = NULL;
#define CHECK(op) \
do { result = (op); \
if (result != ISC_R_SUCCESS) return (result); \
} while (0)
typedef struct tcldb_driver {
isc_mem_t *mctx;
Tcl_Interp *interp;
} tcldb_driver_t;
static tcldb_driver_t *the_driver;
static isc_result_t
tcldb_driver_create(isc_mem_t *mctx, tcldb_driver_t **driverp) {
int tclres;
tcldb_driver_t *driver = isc_mem_get(mctx, sizeof(tcldb_driver_t));
if (driver == NULL)
return (ISC_R_NOMEMORY);
driver->mctx = mctx;
driver->interp = Tcl_CreateInterp();
tclres = Tcl_EvalFile(driver->interp, (char *) "lookup.tcl");
if (tclres != TCL_OK) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
"initializing tcldb: "
"loading lookup.tcl failed: %s",
driver->interp->result);
return (ISC_R_FAILURE);
}
*driverp = driver;
return (ISC_R_SUCCESS);
}
static void
tcldb_driver_destroy(tcldb_driver_t **driverp) {
tcldb_driver_t *driver = *driverp;
Tcl_DeleteInterp(driver->interp);
isc_mem_put(driver->mctx, driver, sizeof(tcldb_driver_t));
}
/*
* This database operates on relative names.
*
* Any name will be interpreted as a pathname offset from the tclectory
* specified in the configuration file.
*/
static isc_result_t
tcldb_lookup(const char *zone, const char *name, void *dbdata,
dns_sdblookup_t *lookup)
{
isc_result_t result = ISC_R_SUCCESS;
int tclres;
int rrc; /* RR count */
char **rrv; /* RR vector */
int i;
char *cmdv[3];
char *cmd;
tcldb_driver_t *driver = (tcldb_driver_t *) dbdata;
cmdv[0] = "lookup";
cmdv[1] = zone;
cmdv[2] = name;
cmd = Tcl_Merge(3, cmdv);
tclres = Tcl_Eval(driver->interp, cmd);
Tcl_Free(cmd);
if (tclres != TCL_OK) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
"zone '%s': tcl lookup function failed: %s",
zone, driver->interp->result);
return (ISC_R_FAILURE);
}
if (strcmp(driver->interp->result, "NXDOMAIN") == 0) {
result = ISC_R_NOTFOUND;
goto fail;
}
tclres = Tcl_SplitList(driver->interp, driver->interp->result, &rrc, &rrv);
if (tclres != TCL_OK)
goto malformed;
for (i = 0; i < rrc; i++) {
isc_result_t tmpres;
int fieldc; /* Field count */
char **fieldv; /* Field vector */
tclres = Tcl_SplitList(driver->interp, rrv[i], &fieldc, &fieldv);
if (tclres != TCL_OK) {
tmpres = ISC_R_FAILURE;
goto failrr;
}
if (fieldc != 3)
goto malformed;
tmpres = dns_sdb_putrr(lookup, fieldv[0], atoi(fieldv[1]), fieldv[2]);
Tcl_Free((char *) fieldv);
failrr:
if (tmpres != ISC_R_SUCCESS)
result = tmpres;
}
Tcl_Free((char *) rrv);
if (result == ISC_R_SUCCESS)
return (result);
malformed:
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
"zone '%s': malformed return value from tcl lookup function: %s",
zone, driver->interp->result);
result = ISC_R_FAILURE;
fail:
return (result);
}
/*
* Each database stores the top-level tclectory as the dbdata opaque
* object. The create() function allocates it. argv[0] holds the top
* level tclectory.
*/
static isc_result_t
tcldb_create(const char *zone, int argc, char **argv,
void *driverdata, void **dbdata)
{
tcldb_driver_t *driver = (tcldb_driver_t *) driverdata;
char *list = Tcl_Merge(argc, argv);
Tcl_SetVar2(driver->interp, (char *) "dbargs", (char *) zone, list, 0);
Tcl_Free(list);
*dbdata = driverdata;
return (ISC_R_SUCCESS);
}
/*
* This zone does not support zone transfer, so allnodes() is NULL.
*/
static dns_sdbmethods_t tcldb_methods = {
tcldb_lookup,
NULL, /* authority */
NULL, /* allnodes */
tcldb_create,
NULL /* destroy */
};
/*
* Initialize the tcldb driver.
*/
isc_result_t
tcldb_init(void) {
isc_result_t result;
int flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA;
result = tcldb_driver_create(ns_g_mctx, &the_driver);
if (result != ISC_R_SUCCESS)
return (result);
return (dns_sdb_register("tcl", &tcldb_methods, the_driver, flags,
ns_g_mctx, &tcldb));
}
/*
* Wrapper around dns_sdb_unregister().
*/
void
tcldb_clear(void) {
dns_sdb_unregister(&tcldb);
tcldb_driver_destroy(&the_driver);
}

6
contrib/sdb/tcl/tcldb.h Normal file
View file

@ -0,0 +1,6 @@
#include <isc/types.h>
isc_result_t tcldb_init(void);
void tcldb_clear(void);

201
contrib/sdb/tcldb.c Normal file
View file

@ -0,0 +1,201 @@
#include <config.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/result.h>
#include <isc/util.h>
#include <dns/log.h>
#include <dns/sdb.h>
#include <named/globals.h>
#include <tcl.h>
#include <tcldb.h>
/*
* A simple database driver that calls Tcl procedures to define
* the contents of the DNS namespace.
*/
static dns_sdbimplementation_t *tcldb = NULL;
#define CHECK(op) \
do { result = (op); \
if (result != ISC_R_SUCCESS) return (result); \
} while (0)
typedef struct tcldb_driver {
isc_mem_t *mctx;
Tcl_Interp *interp;
} tcldb_driver_t;
static tcldb_driver_t *the_driver;
static isc_result_t
tcldb_driver_create(isc_mem_t *mctx, tcldb_driver_t **driverp) {
int tclres;
tcldb_driver_t *driver = isc_mem_get(mctx, sizeof(tcldb_driver_t));
if (driver == NULL)
return (ISC_R_NOMEMORY);
driver->mctx = mctx;
driver->interp = Tcl_CreateInterp();
tclres = Tcl_EvalFile(driver->interp, (char *) "lookup.tcl");
if (tclres != TCL_OK) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
"initializing tcldb: "
"loading lookup.tcl failed: %s",
driver->interp->result);
return (ISC_R_FAILURE);
}
*driverp = driver;
return (ISC_R_SUCCESS);
}
static void
tcldb_driver_destroy(tcldb_driver_t **driverp) {
tcldb_driver_t *driver = *driverp;
Tcl_DeleteInterp(driver->interp);
isc_mem_put(driver->mctx, driver, sizeof(tcldb_driver_t));
}
/*
* This database operates on relative names.
*
* Any name will be interpreted as a pathname offset from the tclectory
* specified in the configuration file.
*/
static isc_result_t
tcldb_lookup(const char *zone, const char *name, void *dbdata,
dns_sdblookup_t *lookup)
{
isc_result_t result = ISC_R_SUCCESS;
int tclres;
int rrc; /* RR count */
char **rrv; /* RR vector */
int i;
char *cmdv[3];
char *cmd;
tcldb_driver_t *driver = (tcldb_driver_t *) dbdata;
cmdv[0] = "lookup";
cmdv[1] = zone;
cmdv[2] = name;
cmd = Tcl_Merge(3, cmdv);
tclres = Tcl_Eval(driver->interp, cmd);
Tcl_Free(cmd);
if (tclres != TCL_OK) {
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
"zone '%s': tcl lookup function failed: %s",
zone, driver->interp->result);
return (ISC_R_FAILURE);
}
if (strcmp(driver->interp->result, "NXDOMAIN") == 0) {
result = ISC_R_NOTFOUND;
goto fail;
}
tclres = Tcl_SplitList(driver->interp, driver->interp->result, &rrc, &rrv);
if (tclres != TCL_OK)
goto malformed;
for (i = 0; i < rrc; i++) {
isc_result_t tmpres;
int fieldc; /* Field count */
char **fieldv; /* Field vector */
tclres = Tcl_SplitList(driver->interp, rrv[i], &fieldc, &fieldv);
if (tclres != TCL_OK) {
tmpres = ISC_R_FAILURE;
goto failrr;
}
if (fieldc != 3)
goto malformed;
tmpres = dns_sdb_putrr(lookup, fieldv[0], atoi(fieldv[1]), fieldv[2]);
Tcl_Free((char *) fieldv);
failrr:
if (tmpres != ISC_R_SUCCESS)
result = tmpres;
}
Tcl_Free((char *) rrv);
if (result == ISC_R_SUCCESS)
return (result);
malformed:
isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
DNS_LOGMODULE_SDB, ISC_LOG_ERROR,
"zone '%s': malformed return value from tcl lookup function: %s",
zone, driver->interp->result);
result = ISC_R_FAILURE;
fail:
return (result);
}
/*
* Each database stores the top-level tclectory as the dbdata opaque
* object. The create() function allocates it. argv[0] holds the top
* level tclectory.
*/
static isc_result_t
tcldb_create(const char *zone, int argc, char **argv,
void *driverdata, void **dbdata)
{
tcldb_driver_t *driver = (tcldb_driver_t *) driverdata;
char *list = Tcl_Merge(argc, argv);
Tcl_SetVar2(driver->interp, (char *) "dbargs", (char *) zone, list, 0);
Tcl_Free(list);
*dbdata = driverdata;
return (ISC_R_SUCCESS);
}
/*
* This zone does not support zone transfer, so allnodes() is NULL.
*/
static dns_sdbmethods_t tcldb_methods = {
tcldb_lookup,
NULL, /* authority */
NULL, /* allnodes */
tcldb_create,
NULL /* destroy */
};
/*
* Initialize the tcldb driver.
*/
isc_result_t
tcldb_init(void) {
isc_result_t result;
int flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA;
result = tcldb_driver_create(ns_g_mctx, &the_driver);
if (result != ISC_R_SUCCESS)
return (result);
return (dns_sdb_register("tcl", &tcldb_methods, the_driver, flags,
ns_g_mctx, &tcldb));
}
/*
* Wrapper around dns_sdb_unregister().
*/
void
tcldb_clear(void) {
dns_sdb_unregister(&tcldb);
tcldb_driver_destroy(&the_driver);
}

6
contrib/sdb/tcldb.h Normal file
View file

@ -0,0 +1,6 @@
#include <isc/types.h>
isc_result_t tcldb_init(void);
void tcldb_clear(void);