1 /* $NetBSD: timedb.c,v 1.4 2014/12/10 04:37:57 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2007, 2011, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 2000, 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: timedb.c,v 1.12 2011/10/11 23:46:45 tbox Exp */ 21 22 /* 23 * A simple database driver that enables the server to return the 24 * current time in a DNS record. 25 */ 26 27 #include <config.h> 28 29 #include <string.h> 30 #include <stdio.h> 31 #include <time.h> 32 33 #include <isc/print.h> 34 #include <isc/result.h> 35 #include <isc/util.h> 36 37 #include <dns/sdb.h> 38 39 #include <named/globals.h> 40 41 #include "timedb.h" 42 43 static dns_sdbimplementation_t *timedb = NULL; 44 45 /* 46 * This database operates on relative names. 47 * 48 * "time" and "@" return the time in a TXT record. 49 * "clock" is a CNAME to "time" 50 * "current" is a DNAME to "@" (try time.current.time) 51 */ 52 #ifdef DNS_CLIENTINFO_VERSION 53 static isc_result_t 54 timedb_lookup(const char *zone, const char *name, void *dbdata, 55 dns_sdblookup_t *lookup, dns_clientinfomethods_t *methods, 56 dns_clientinfo_t *clientinfo) 57 #else 58 static isc_result_t 59 timedb_lookup(const char *zone, const char *name, void *dbdata, 60 dns_sdblookup_t *lookup) 61 #endif /* DNS_CLIENTINFO_VERSION */ 62 { 63 isc_result_t result; 64 65 UNUSED(zone); 66 UNUSED(dbdata); 67 #ifdef DNS_CLIENTINFO_VERSION 68 UNUSED(methods); 69 UNUSED(clientinfo); 70 #endif /* DNS_CLIENTINFO_VERSION */ 71 72 if (strcmp(name, "@") == 0 || strcmp(name, "time") == 0) { 73 time_t now = time(NULL); 74 char buf[100]; 75 int n; 76 77 /* 78 * Call ctime to create the string, put it in quotes, and 79 * remove the trailing newline. 80 */ 81 n = snprintf(buf, sizeof(buf), "\"%s", ctime(&now)); 82 if (n < 0) 83 return (ISC_R_FAILURE); 84 buf[n - 1] = '\"'; 85 result = dns_sdb_putrr(lookup, "txt", 1, buf); 86 if (result != ISC_R_SUCCESS) 87 return (ISC_R_FAILURE); 88 } else if (strcmp(name, "clock") == 0) { 89 result = dns_sdb_putrr(lookup, "cname", 1, "time"); 90 if (result != ISC_R_SUCCESS) 91 return (ISC_R_FAILURE); 92 } else if (strcmp(name, "current") == 0) { 93 result = dns_sdb_putrr(lookup, "dname", 1, "@"); 94 if (result != ISC_R_SUCCESS) 95 return (ISC_R_FAILURE); 96 } else 97 return (ISC_R_NOTFOUND); 98 99 return (ISC_R_SUCCESS); 100 } 101 102 /* 103 * lookup() does not return SOA or NS records, so authority() must be defined. 104 */ 105 static isc_result_t 106 timedb_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) { 107 isc_result_t result; 108 109 UNUSED(zone); 110 UNUSED(dbdata); 111 112 result = dns_sdb_putsoa(lookup, "localhost.", "root.localhost.", 0); 113 if (result != ISC_R_SUCCESS) 114 return (ISC_R_FAILURE); 115 116 result = dns_sdb_putrr(lookup, "ns", 86400, "ns1.localdomain."); 117 if (result != ISC_R_SUCCESS) 118 return (ISC_R_FAILURE); 119 result = dns_sdb_putrr(lookup, "ns", 86400, "ns2.localdomain."); 120 if (result != ISC_R_SUCCESS) 121 return (ISC_R_FAILURE); 122 123 return (ISC_R_SUCCESS); 124 } 125 126 /* 127 * This zone does not support zone transfer, so allnodes() is NULL. There 128 * is no database specific data, so create() and destroy() are NULL. 129 */ 130 static dns_sdbmethods_t timedb_methods = { 131 timedb_lookup, 132 timedb_authority, 133 NULL, /* allnodes */ 134 NULL, /* create */ 135 NULL, /* destroy */ 136 NULL /* lookup2 */ 137 }; 138 139 /* 140 * Wrapper around dns_sdb_register(). 141 */ 142 isc_result_t 143 timedb_init(void) { 144 unsigned int flags; 145 flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA; 146 return (dns_sdb_register("time", &timedb_methods, NULL, flags, 147 ns_g_mctx, &timedb)); 148 } 149 150 /* 151 * Wrapper around dns_sdb_unregister(). 152 */ 153 void 154 timedb_clear(void) { 155 if (timedb != NULL) 156 dns_sdb_unregister(&timedb); 157 } 158