Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / contrib / sdb / time / timedb.c
blob0f9ba8573fb12918aa2722ecf2dd803a93a4afc2
1 /* $NetBSD: timedb.c,v 1.4 2014/12/10 04:37:57 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2007, 2011, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
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.
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.
20 /* Id: timedb.c,v 1.12 2011/10/11 23:46:45 tbox Exp */
23 * A simple database driver that enables the server to return the
24 * current time in a DNS record.
27 #include <config.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <time.h>
33 #include <isc/print.h>
34 #include <isc/result.h>
35 #include <isc/util.h>
37 #include <dns/sdb.h>
39 #include <named/globals.h>
41 #include "timedb.h"
43 static dns_sdbimplementation_t *timedb = NULL;
46 * This database operates on relative names.
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)
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 */
63 isc_result_t result;
65 UNUSED(zone);
66 UNUSED(dbdata);
67 #ifdef DNS_CLIENTINFO_VERSION
68 UNUSED(methods);
69 UNUSED(clientinfo);
70 #endif /* DNS_CLIENTINFO_VERSION */
72 if (strcmp(name, "@") == 0 || strcmp(name, "time") == 0) {
73 time_t now = time(NULL);
74 char buf[100];
75 int n;
78 * Call ctime to create the string, put it in quotes, and
79 * remove the trailing newline.
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);
99 return (ISC_R_SUCCESS);
103 * lookup() does not return SOA or NS records, so authority() must be defined.
105 static isc_result_t
106 timedb_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) {
107 isc_result_t result;
109 UNUSED(zone);
110 UNUSED(dbdata);
112 result = dns_sdb_putsoa(lookup, "localhost.", "root.localhost.", 0);
113 if (result != ISC_R_SUCCESS)
114 return (ISC_R_FAILURE);
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);
123 return (ISC_R_SUCCESS);
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.
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 */
140 * Wrapper around dns_sdb_register().
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));
151 * Wrapper around dns_sdb_unregister().
153 void
154 timedb_clear(void) {
155 if (timedb != NULL)
156 dns_sdb_unregister(&timedb);