4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001-2003 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: builtin.c,v 1.15 2009/03/01 02:45:38 each Exp */
24 * The built-in "version", "hostname", "id", "authors" and "empty" databases.
33 #include <isc/print.h>
34 #include <isc/result.h>
37 #include <dns/result.h>
40 #include <named/builtin.h>
41 #include <named/globals.h>
42 #include <named/server.h>
45 typedef struct builtin builtin_t
;
47 static isc_result_t
do_version_lookup(dns_sdblookup_t
*lookup
);
48 static isc_result_t
do_hostname_lookup(dns_sdblookup_t
*lookup
);
49 static isc_result_t
do_authors_lookup(dns_sdblookup_t
*lookup
);
50 static isc_result_t
do_id_lookup(dns_sdblookup_t
*lookup
);
51 static isc_result_t
do_empty_lookup(dns_sdblookup_t
*lookup
);
54 * We can't use function pointers as the db_data directly
55 * because ANSI C does not guarantee that function pointers
56 * can safely be cast to void pointers and back.
60 isc_result_t (*do_lookup
)(dns_sdblookup_t
*lookup
);
65 static builtin_t version_builtin
= { do_version_lookup
, NULL
, NULL
};
66 static builtin_t hostname_builtin
= { do_hostname_lookup
, NULL
, NULL
};
67 static builtin_t authors_builtin
= { do_authors_lookup
, NULL
, NULL
};
68 static builtin_t id_builtin
= { do_id_lookup
, NULL
, NULL
};
69 static builtin_t empty_builtin
= { do_empty_lookup
, NULL
, NULL
};
71 static dns_sdbimplementation_t
*builtin_impl
;
74 builtin_lookup(const char *zone
, const char *name
, void *dbdata
,
75 dns_sdblookup_t
*lookup
)
77 builtin_t
*b
= (builtin_t
*) dbdata
;
81 if (strcmp(name
, "@") == 0)
82 return (b
->do_lookup(lookup
));
84 return (ISC_R_NOTFOUND
);
88 put_txt(dns_sdblookup_t
*lookup
, const char *text
) {
89 unsigned char buf
[256];
90 unsigned int len
= strlen(text
);
92 len
= 255; /* Silently truncate */
94 memcpy(&buf
[1], text
, len
);
95 return (dns_sdb_putrdata(lookup
, dns_rdatatype_txt
, 0, buf
, len
+ 1));
99 do_version_lookup(dns_sdblookup_t
*lookup
) {
100 if (ns_g_server
->version_set
) {
101 if (ns_g_server
->version
== NULL
)
102 return (ISC_R_SUCCESS
);
104 return (put_txt(lookup
, ns_g_server
->version
));
106 return (put_txt(lookup
, ns_g_version
));
111 do_hostname_lookup(dns_sdblookup_t
*lookup
) {
112 if (ns_g_server
->hostname_set
) {
113 if (ns_g_server
->hostname
== NULL
)
114 return (ISC_R_SUCCESS
);
116 return (put_txt(lookup
, ns_g_server
->hostname
));
119 isc_result_t result
= ns_os_gethostname(buf
, sizeof(buf
));
120 if (result
!= ISC_R_SUCCESS
)
122 return (put_txt(lookup
, buf
));
127 do_authors_lookup(dns_sdblookup_t
*lookup
) {
130 static const char *authors
[] = {
135 "Andreas Gustafsson",
149 * If a version string is specified, disable the authors.bind zone.
151 if (ns_g_server
->version_set
)
152 return (ISC_R_SUCCESS
);
154 for (p
= authors
; *p
!= NULL
; p
++) {
155 result
= put_txt(lookup
, *p
);
156 if (result
!= ISC_R_SUCCESS
)
159 return (ISC_R_SUCCESS
);
163 do_id_lookup(dns_sdblookup_t
*lookup
) {
165 if (ns_g_server
->server_usehostname
) {
167 isc_result_t result
= ns_os_gethostname(buf
, sizeof(buf
));
168 if (result
!= ISC_R_SUCCESS
)
170 return (put_txt(lookup
, buf
));
173 if (ns_g_server
->server_id
== NULL
)
174 return (ISC_R_SUCCESS
);
176 return (put_txt(lookup
, ns_g_server
->server_id
));
180 do_empty_lookup(dns_sdblookup_t
*lookup
) {
183 return (ISC_R_SUCCESS
);
187 builtin_authority(const char *zone
, void *dbdata
, dns_sdblookup_t
*lookup
) {
189 const char *contact
= "hostmaster";
190 const char *server
= "@";
191 builtin_t
*b
= (builtin_t
*) dbdata
;
196 if (b
== &empty_builtin
) {
200 if (b
->server
!= NULL
)
202 if (b
->contact
!= NULL
)
203 contact
= b
->contact
;
206 result
= dns_sdb_putsoa(lookup
, server
, contact
, 0);
207 if (result
!= ISC_R_SUCCESS
)
208 return (ISC_R_FAILURE
);
210 result
= dns_sdb_putrr(lookup
, "ns", 0, server
);
211 if (result
!= ISC_R_SUCCESS
)
212 return (ISC_R_FAILURE
);
214 return (ISC_R_SUCCESS
);
218 builtin_create(const char *zone
, int argc
, char **argv
,
219 void *driverdata
, void **dbdata
)
226 if (strcmp(argv
[0], "empty") == 0) {
228 return (DNS_R_SYNTAX
);
229 } else if (argc
!= 1)
230 return (DNS_R_SYNTAX
);
232 if (strcmp(argv
[0], "version") == 0)
233 *dbdata
= &version_builtin
;
234 else if (strcmp(argv
[0], "hostname") == 0)
235 *dbdata
= &hostname_builtin
;
236 else if (strcmp(argv
[0], "authors") == 0)
237 *dbdata
= &authors_builtin
;
238 else if (strcmp(argv
[0], "id") == 0)
239 *dbdata
= &id_builtin
;
240 else if (strcmp(argv
[0], "empty") == 0) {
245 * We don't want built-in zones to fail. Fallback to
246 * the static configuration if memory allocation fails.
248 empty
= isc_mem_get(ns_g_mctx
, sizeof(*empty
));
249 server
= isc_mem_strdup(ns_g_mctx
, argv
[1]);
250 contact
= isc_mem_strdup(ns_g_mctx
, argv
[2]);
251 if (empty
== NULL
|| server
== NULL
|| contact
== NULL
) {
252 *dbdata
= &empty_builtin
;
254 isc_mem_free(ns_g_mctx
, server
);
256 isc_mem_free(ns_g_mctx
, contact
);
258 isc_mem_put(ns_g_mctx
, empty
, sizeof (*empty
));
260 memcpy(empty
, &empty_builtin
, sizeof (empty_builtin
));
261 empty
->server
= server
;
262 empty
->contact
= contact
;
266 return (ISC_R_NOTIMPLEMENTED
);
267 return (ISC_R_SUCCESS
);
271 builtin_destroy(const char *zone
, void *driverdata
, void **dbdata
) {
272 builtin_t
*b
= (builtin_t
*) *dbdata
;
278 * Don't free the static versions.
280 if (*dbdata
== &version_builtin
|| *dbdata
== &hostname_builtin
||
281 *dbdata
== &authors_builtin
|| *dbdata
== &id_builtin
||
282 *dbdata
== &empty_builtin
)
285 isc_mem_free(ns_g_mctx
, b
->server
);
286 isc_mem_free(ns_g_mctx
, b
->contact
);
287 isc_mem_put(ns_g_mctx
, b
, sizeof (*b
));
290 static dns_sdbmethods_t builtin_methods
= {
299 ns_builtin_init(void) {
300 RUNTIME_CHECK(dns_sdb_register("_builtin", &builtin_methods
, NULL
,
301 DNS_SDBFLAG_RELATIVEOWNER
|
302 DNS_SDBFLAG_RELATIVERDATA
,
303 ns_g_mctx
, &builtin_impl
)
305 return (ISC_R_SUCCESS
);
309 ns_builtin_deinit(void) {
310 dns_sdb_unregister(&builtin_impl
);