4 * Copyright (C) 2004, 2007 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.10 2007/06/19 23:47:10 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)
53 timedb_lookup(const char *zone
, const char *name
, void *dbdata
,
54 dns_sdblookup_t
*lookup
)
61 if (strcmp(name
, "@") == 0 || strcmp(name
, "time") == 0) {
62 time_t now
= time(NULL
);
67 * Call ctime to create the string, put it in quotes, and
68 * remove the trailing newline.
70 n
= snprintf(buf
, sizeof(buf
), "\"%s", ctime(&now
));
72 return (ISC_R_FAILURE
);
74 result
= dns_sdb_putrr(lookup
, "txt", 1, buf
);
75 if (result
!= ISC_R_SUCCESS
)
76 return (ISC_R_FAILURE
);
77 } else if (strcmp(name
, "clock") == 0) {
78 result
= dns_sdb_putrr(lookup
, "cname", 1, "time");
79 if (result
!= ISC_R_SUCCESS
)
80 return (ISC_R_FAILURE
);
81 } else if (strcmp(name
, "current") == 0) {
82 result
= dns_sdb_putrr(lookup
, "dname", 1, "@");
83 if (result
!= ISC_R_SUCCESS
)
84 return (ISC_R_FAILURE
);
86 return (ISC_R_NOTFOUND
);
88 return (ISC_R_SUCCESS
);
92 * lookup() does not return SOA or NS records, so authority() must be defined.
95 timedb_authority(const char *zone
, void *dbdata
, dns_sdblookup_t
*lookup
) {
101 result
= dns_sdb_putsoa(lookup
, "localhost.", "root.localhost.", 0);
102 if (result
!= ISC_R_SUCCESS
)
103 return (ISC_R_FAILURE
);
105 result
= dns_sdb_putrr(lookup
, "ns", 86400, "ns1.localdomain.");
106 if (result
!= ISC_R_SUCCESS
)
107 return (ISC_R_FAILURE
);
108 result
= dns_sdb_putrr(lookup
, "ns", 86400, "ns2.localdomain.");
109 if (result
!= ISC_R_SUCCESS
)
110 return (ISC_R_FAILURE
);
112 return (ISC_R_SUCCESS
);
116 * This zone does not support zone transfer, so allnodes() is NULL. There
117 * is no database specific data, so create() and destroy() are NULL.
119 static dns_sdbmethods_t timedb_methods
= {
128 * Wrapper around dns_sdb_register().
133 flags
= DNS_SDBFLAG_RELATIVEOWNER
| DNS_SDBFLAG_RELATIVERDATA
;
134 return (dns_sdb_register("time", &timedb_methods
, NULL
, flags
,
135 ns_g_mctx
, &timedb
));
139 * Wrapper around dns_sdb_unregister().
144 dns_sdb_unregister(&timedb
);