From a19f3422d355f690c69834e965bf2f2c5b7e95af Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Fri, 17 Nov 2000 19:33:16 +0000 Subject: [PATCH] The "time" simple database driver. --- contrib/sdb/time/timedb.c | 121 ++++++++++++++++++++++++++++++++++++++ contrib/sdb/timedb.c | 121 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 contrib/sdb/time/timedb.c create mode 100644 contrib/sdb/timedb.c diff --git a/contrib/sdb/time/timedb.c b/contrib/sdb/time/timedb.c new file mode 100644 index 0000000000..4e210f37e6 --- /dev/null +++ b/contrib/sdb/time/timedb.c @@ -0,0 +1,121 @@ +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include + +/* + * A simple database driver that enables the server to return the + * current time in a DNS record. + */ + +static dns_sdbimplementation_t *timedb = NULL; + +/* + * This database operates on relative names. + * + * "time" and "@" return the time in a TXT record. + * "clock" is a CNAME to "time" + * "current" is a DNAME to "@" (try time.current.time) + */ +static isc_result_t +timedb_lookup(const char *zone, const char *name, void *dbdata, + dns_sdblookup_t *lookup) +{ + isc_result_t result; + + UNUSED(zone); + UNUSED(dbdata); + + if (strcmp(name, "@") == 0 || strcmp(name, "time") == 0) { + time_t now = time(NULL); + char buf[100]; + int n; + + /* + * Call ctime to create the string, put it in quotes, and + * remove the trailing newline. + */ + n = snprintf(buf, sizeof(buf), "\"%s", ctime(&now)); + if (n < 0) + return (ISC_R_FAILURE); + buf[n - 1] = '\"'; + result = dns_sdb_putrr(lookup, "txt", 1, buf); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + } else if (strcmp(name, "clock") == 0) { + result = dns_sdb_putrr(lookup, "cname", 1, "time"); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + } else if (strcmp(name, "current") == 0) { + result = dns_sdb_putrr(lookup, "dname", 1, "@"); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + } else + return (ISC_R_NOTFOUND); + + return (ISC_R_SUCCESS); +} + +/* + * lookup() does not return SOA or NS records, so authority() must be defined. + */ +static isc_result_t +timedb_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) { + isc_result_t result; + + UNUSED(zone); + UNUSED(dbdata); + + result = dns_sdb_putsoa(lookup, "localhost.", "root.localhost.", 0); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + + result = dns_sdb_putrr(lookup, "ns", 86400, "ns1.localdomain."); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + result = dns_sdb_putrr(lookup, "ns", 86400, "ns2.localdomain."); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + + return (ISC_R_SUCCESS); +} + +/* + * This zone does not support zone transfer, so allnodes() is NULL. There + * is no driver specific data, so create() and destroy() are NULL. + */ +static dns_sdbmethods_t timedb_methods = { + timedb_lookup, + timedb_authority, + NULL, /* allnodes */ + NULL, /* create */ + NULL /* destroy */ +}; + +/* + * Wrapper around dns_sdb_register(). + */ +isc_result_t +timedb_init(void) { + unsigned int flags; + flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA; + return (dns_sdb_register("time", &timedb_methods, NULL, flags, + ns_g_mctx, &timedb)); +} + +/* + * Wrapper around dns_sdb_unregister(). + */ +void +timedb_clear(void) { + dns_sdb_unregister(&timedb); +} diff --git a/contrib/sdb/timedb.c b/contrib/sdb/timedb.c new file mode 100644 index 0000000000..4e210f37e6 --- /dev/null +++ b/contrib/sdb/timedb.c @@ -0,0 +1,121 @@ +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include + +/* + * A simple database driver that enables the server to return the + * current time in a DNS record. + */ + +static dns_sdbimplementation_t *timedb = NULL; + +/* + * This database operates on relative names. + * + * "time" and "@" return the time in a TXT record. + * "clock" is a CNAME to "time" + * "current" is a DNAME to "@" (try time.current.time) + */ +static isc_result_t +timedb_lookup(const char *zone, const char *name, void *dbdata, + dns_sdblookup_t *lookup) +{ + isc_result_t result; + + UNUSED(zone); + UNUSED(dbdata); + + if (strcmp(name, "@") == 0 || strcmp(name, "time") == 0) { + time_t now = time(NULL); + char buf[100]; + int n; + + /* + * Call ctime to create the string, put it in quotes, and + * remove the trailing newline. + */ + n = snprintf(buf, sizeof(buf), "\"%s", ctime(&now)); + if (n < 0) + return (ISC_R_FAILURE); + buf[n - 1] = '\"'; + result = dns_sdb_putrr(lookup, "txt", 1, buf); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + } else if (strcmp(name, "clock") == 0) { + result = dns_sdb_putrr(lookup, "cname", 1, "time"); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + } else if (strcmp(name, "current") == 0) { + result = dns_sdb_putrr(lookup, "dname", 1, "@"); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + } else + return (ISC_R_NOTFOUND); + + return (ISC_R_SUCCESS); +} + +/* + * lookup() does not return SOA or NS records, so authority() must be defined. + */ +static isc_result_t +timedb_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) { + isc_result_t result; + + UNUSED(zone); + UNUSED(dbdata); + + result = dns_sdb_putsoa(lookup, "localhost.", "root.localhost.", 0); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + + result = dns_sdb_putrr(lookup, "ns", 86400, "ns1.localdomain."); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + result = dns_sdb_putrr(lookup, "ns", 86400, "ns2.localdomain."); + if (result != ISC_R_SUCCESS) + return (ISC_R_FAILURE); + + return (ISC_R_SUCCESS); +} + +/* + * This zone does not support zone transfer, so allnodes() is NULL. There + * is no driver specific data, so create() and destroy() are NULL. + */ +static dns_sdbmethods_t timedb_methods = { + timedb_lookup, + timedb_authority, + NULL, /* allnodes */ + NULL, /* create */ + NULL /* destroy */ +}; + +/* + * Wrapper around dns_sdb_register(). + */ +isc_result_t +timedb_init(void) { + unsigned int flags; + flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA; + return (dns_sdb_register("time", &timedb_methods, NULL, flags, + ns_g_mctx, &timedb)); +} + +/* + * Wrapper around dns_sdb_unregister(). + */ +void +timedb_clear(void) { + dns_sdb_unregister(&timedb); +}