timedb.c revision 32f2764f4c62c0d2d838625ba326e5576989c932
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <config.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <string.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <stdio.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <time.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <isc/print.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <isc/result.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <isc/util.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <dns/sdb.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier#include <named/globals.h>
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier/*
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier * A simple database driver that enables the server to return the
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier * current time in a DNS record.
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier */
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhierstatic dns_sdbimplementation_t *timedb = NULL;
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier/*
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier * This database operates on relative names.
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier *
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier * "time" and "@" return the time in a TXT record.
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier * "clock" is a CNAME to "time"
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier * "current" is a DNAME to "@" (try time.current.time)
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier */
380c874925f684847d9278b909cf511cb5b0289aShawn Routhierstatic isc_result_t
380c874925f684847d9278b909cf511cb5b0289aShawn Routhiertimedb_lookup(const char *zone, const char *name, void *dbdata,
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier dns_sdblookup_t *lookup)
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier{
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier isc_result_t result;
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier UNUSED(zone);
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier UNUSED(dbdata);
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier if (strcmp(name, "@") == 0 || strcmp(name, "time") == 0) {
380c874925f684847d9278b909cf511cb5b0289aShawn Routhier 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 database 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);
}