timedb.c revision e6f82b9dbba43699d9f8231a566ec4830da2cee5
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/*
1fdd2470b625a58b57d0b155e6caf8c4fc0afe8aAutomatic Updater * Copyright (C) 2000 Internet Software Consortium.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Permission to use, copy, modify, and distribute this software for any
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater * purpose with or without fee is hereby granted, provided that the above
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * copyright notice and this permission notice appear in all copies.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/* $Id: timedb.c,v 1.3 2000/11/17 22:01:43 bwelling Exp $ */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <config.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
e21a2904f02a03fa06b6db04d348f65fe9c67b2bMark Andrews#include <string.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <stdio.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <time.h>
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <isc/print.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <isc/result.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <isc/util.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <dns/sdb.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews#include <named/globals.h>
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater/*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * A simple database driver that enables the server to return the
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * current time in a DNS record.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic dns_sdbimplementation_t *timedb = NULL;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * This database operates on relative names.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews *
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * "time" and "@" return the time in a TXT record.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * "clock" is a CNAME to "time"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * "current" is a DNAME to "@" (try time.current.time)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic isc_result_t
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewstimedb_lookup(const char *zone, const char *name, void *dbdata,
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater dns_sdblookup_t *lookup)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews{
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews isc_result_t result;
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews UNUSED(zone);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews UNUSED(dbdata);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (strcmp(name, "@") == 0 || strcmp(name, "time") == 0) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews time_t now = time(NULL);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews char buf[100];
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews int n;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews /*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Call ctime to create the string, put it in quotes, and
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater * remove the trailing newline.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews n = snprintf(buf, sizeof(buf), "\"%s", ctime(&now));
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (n < 0)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (ISC_R_FAILURE);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews buf[n - 1] = '\"';
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews result = dns_sdb_putrr(lookup, "txt", 1, buf);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (result != ISC_R_SUCCESS)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (ISC_R_FAILURE);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews } else if (strcmp(name, "clock") == 0) {
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater result = dns_sdb_putrr(lookup, "cname", 1, "time");
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater if (result != ISC_R_SUCCESS)
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater return (ISC_R_FAILURE);
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater } else if (strcmp(name, "current") == 0) {
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater result = dns_sdb_putrr(lookup, "dname", 1, "@");
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater if (result != ISC_R_SUCCESS)
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater return (ISC_R_FAILURE);
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater } else
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater return (ISC_R_NOTFOUND);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return (ISC_R_SUCCESS);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater}
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater/*
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater * lookup() does not return SOA or NS records, so authority() must be defined.
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater */
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updaterstatic isc_result_t
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updatertimedb_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) {
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater isc_result_t result;
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater UNUSED(zone);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater UNUSED(dbdata);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater result = dns_sdb_putsoa(lookup, "localhost.", "root.localhost.", 0);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (result != ISC_R_SUCCESS)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return (ISC_R_FAILURE);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater result = dns_sdb_putrr(lookup, "ns", 86400, "ns1.localdomain.");
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (result != ISC_R_SUCCESS)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return (ISC_R_FAILURE);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater result = dns_sdb_putrr(lookup, "ns", 86400, "ns2.localdomain.");
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if (result != ISC_R_SUCCESS)
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (ISC_R_FAILURE);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (ISC_R_SUCCESS);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * This zone does not support zone transfer, so allnodes() is NULL. There
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * is no database specific data, so create() and destroy() are NULL.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsstatic dns_sdbmethods_t timedb_methods = {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews timedb_lookup,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews timedb_authority,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews NULL, /* allnodes */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews NULL, /* create */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews NULL /* destroy */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews};
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Wrapper around dns_sdb_register().
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews */
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsisc_result_t
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewstimedb_init(void) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews unsigned int flags;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA;
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return (dns_sdb_register("time", &timedb_methods, NULL, flags,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews ns_g_mctx, &timedb));
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews/*
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Wrapper around dns_sdb_unregister().
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updater */
2895f101b5585a19015ac2c2c1e1812ac467fa12Automatic Updatervoid
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewstimedb_clear(void) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews dns_sdb_unregister(&timedb);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews}
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews