1 /* $NetBSD: timedb.c,v 1.4 2014/12/10 04:37:57 christos Exp $ */
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.
33 #include <isc/print.h>
34 #include <isc/result.h>
39 #include <named/globals.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
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
)
59 timedb_lookup(const char *zone
, const char *name
, void *dbdata
,
60 dns_sdblookup_t
*lookup
)
61 #endif /* DNS_CLIENTINFO_VERSION */
67 #ifdef DNS_CLIENTINFO_VERSION
70 #endif /* DNS_CLIENTINFO_VERSION */
72 if (strcmp(name
, "@") == 0 || strcmp(name
, "time") == 0) {
73 time_t now
= time(NULL
);
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
));
83 return (ISC_R_FAILURE
);
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
);
97 return (ISC_R_NOTFOUND
);
99 return (ISC_R_SUCCESS
);
103 * lookup() does not return SOA or NS records, so authority() must be defined.
106 timedb_authority(const char *zone
, void *dbdata
, dns_sdblookup_t
*lookup
) {
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
= {
140 * Wrapper around dns_sdb_register().
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().
156 dns_sdb_unregister(&timedb
);