2 * Copyright (C) 2004, 2005, 2007, 2009-2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: builtin.c,v 1.20.14.3 2012/01/11 20:19:40 ckb Exp $ */
22 * The built-in "version", "hostname", "id", "authors" and "empty" databases.
31 #include <isc/print.h>
32 #include <isc/result.h>
35 #include <dns/result.h>
38 #include <named/builtin.h>
39 #include <named/globals.h>
40 #include <named/server.h>
43 typedef struct builtin builtin_t;
45 static isc_result_t do_version_lookup(dns_sdblookup_t *lookup);
46 static isc_result_t do_hostname_lookup(dns_sdblookup_t *lookup);
47 static isc_result_t do_authors_lookup(dns_sdblookup_t *lookup);
48 static isc_result_t do_id_lookup(dns_sdblookup_t *lookup);
49 static isc_result_t do_empty_lookup(dns_sdblookup_t *lookup);
50 static isc_result_t do_dns64_lookup(dns_sdblookup_t *lookup);
53 * We can't use function pointers as the db_data directly
54 * because ANSI C does not guarantee that function pointers
55 * can safely be cast to void pointers and back.
59 isc_result_t (*do_lookup)(dns_sdblookup_t *lookup);
64 static builtin_t version_builtin = { do_version_lookup, NULL, NULL };
65 static builtin_t hostname_builtin = { do_hostname_lookup, NULL, NULL };
66 static builtin_t authors_builtin = { do_authors_lookup, NULL, NULL };
67 static builtin_t id_builtin = { do_id_lookup, NULL, NULL };
68 static builtin_t empty_builtin = { do_empty_lookup, NULL, NULL };
69 static builtin_t dns64_builtin = { do_dns64_lookup, NULL, NULL };
71 static dns_sdbimplementation_t *builtin_impl;
72 static dns_sdbimplementation_t *dns64_impl;
75 * Pre computed HEX * 16 or 1 table.
77 static const unsigned char hex16[256] = {
78 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*00*/
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*10*/
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*20*/
81 0, 16, 32, 48, 64, 80, 96,112,128,144, 1, 1, 1, 1, 1, 1, /*30*/
82 1,160,176,192,208,224,240, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*40*/
83 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*50*/
84 1,160,176,192,208,224,240, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*60*/
85 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*70*/
86 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*80*/
87 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*90*/
88 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*A0*/
89 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*B0*/
90 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*C0*/
91 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*D0*/
92 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*E0*/
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /*F0*/
96 const unsigned char decimal[] = "0123456789";
99 dns64_rdata(unsigned char *v, size_t start, unsigned char *rdata) {
102 for (i = 0; i < 4U; i++) {
103 unsigned char c = v[start++];
108 rdata[j++] = decimal[c/100]; c = c % 100;
109 rdata[j++] = decimal[c/10]; c = c % 10;
110 rdata[j++] = decimal[c];
113 rdata[j++] = decimal[c/10]; c = c % 10;
114 rdata[j++] = decimal[c];
117 rdata[j++] = decimal[c];
120 memcpy(&rdata[j], "\07in-addr\04arpa", 14);
125 dns64_cname(const dns_name_t *zone, const dns_name_t *name,
126 dns_sdblookup_t *lookup)
128 size_t zlen, nlen, j, len;
129 unsigned char v[16], n;
131 unsigned char rdata[sizeof("123.123.123.123.in-addr.arpa.")];
132 unsigned char *ndata;
135 * The combined length of the zone and name is 74.
137 * The minimum zone length is 10 ((3)ip6(4)arpa(0)).
139 * The length of name should always be even as we are expecting
140 * a series of nibbles.
144 if ((zlen + nlen) > 74U || zlen < 10U || (nlen % 2) != 0U)
145 return (ISC_R_NOTFOUND);
148 * We assume the zone name is well formed.
152 * XXXMPA We could check the dns64 suffix here if we need to.
155 * Check that name is a series of nibbles.
156 * Compute the byte values that correspond to the nibbles as we go.
158 * Shift the final result 4 bits, by setting 'i' to 1, if we if we
159 * have a odd number of nibbles so that "must be zero" tests below
160 * are byte aligned and we correctly return ISC_R_NOTFOUND or
161 * ISC_R_SUCCESS. We will not generate a CNAME in this case.
164 i = (nlen % 4) == 2U ? 1 : 0;
166 memset(v, 0, sizeof(v));
168 INSIST((i/2) < sizeof(v));
170 return (ISC_R_NOTFOUND);
171 n = hex16[ndata[1]&0xff];
173 return (ISC_R_NOTFOUND);
174 v[i/2] = n | (v[i/2]>>4);
181 * If we get here then we know name only consisted of nibbles.
182 * Now we need to determine if the name exists or not and whether
183 * it corresponds to a empty node in the zone or there should be
186 #define ZLEN(x) (10 + (x)/2)
188 case ZLEN(32): /* prefix len 32 */
190 * The nibbles that map to this byte must be zero for 'name'
191 * to exist in the zone.
193 if (nlen > 16U && v[(nlen-1)/4 - 4] != 0)
194 return (ISC_R_NOTFOUND);
196 * If the total length is not 74 then this is a empty node
199 if (nlen + zlen != 74U)
200 return (ISC_R_SUCCESS);
201 len = dns64_rdata(v, 8, rdata);
203 case ZLEN(40): /* prefix len 40 */
205 * The nibbles that map to this byte must be zero for 'name'
206 * to exist in the zone.
208 if (nlen > 12U && v[(nlen-1)/4 - 3] != 0)
209 return (ISC_R_NOTFOUND);
211 * If the total length is not 74 then this is a empty node
214 if (nlen + zlen != 74U)
215 return (ISC_R_SUCCESS);
216 len = dns64_rdata(v, 6, rdata);
218 case ZLEN(48): /* prefix len 48 */
220 * The nibbles that map to this byte must be zero for 'name'
221 * to exist in the zone.
223 if (nlen > 8U && v[(nlen-1)/4 - 2] != 0)
224 return (ISC_R_NOTFOUND);
226 * If the total length is not 74 then this is a empty node
229 if (nlen + zlen != 74U)
230 return (ISC_R_SUCCESS);
231 len = dns64_rdata(v, 5, rdata);
233 case ZLEN(56): /* prefix len 56 */
235 * The nibbles that map to this byte must be zero for 'name'
236 * to exist in the zone.
238 if (nlen > 4U && v[(nlen-1)/4 - 1] != 0)
239 return (ISC_R_NOTFOUND);
241 * If the total length is not 74 then this is a empty node
244 if (nlen + zlen != 74U)
245 return (ISC_R_SUCCESS);
246 len = dns64_rdata(v, 4, rdata);
248 case ZLEN(64): /* prefix len 64 */
250 * The nibbles that map to this byte must be zero for 'name'
251 * to exist in the zone.
253 if (v[(nlen-1)/4] != 0)
254 return (ISC_R_NOTFOUND);
256 * If the total length is not 74 then this is a empty node
259 if (nlen + zlen != 74U)
260 return (ISC_R_SUCCESS);
261 len = dns64_rdata(v, 3, rdata);
263 case ZLEN(96): /* prefix len 96 */
265 * If the total length is not 74 then this is a empty node
268 if (nlen + zlen != 74U)
269 return (ISC_R_SUCCESS);
270 len = dns64_rdata(v, 0, rdata);
274 * This should never be reached unless someone adds a
275 * zone declaration with this internal type to named.conf.
277 return (ISC_R_NOTFOUND);
279 return (dns_sdb_putrdata(lookup, dns_rdatatype_cname, 600, rdata, len));
283 builtin_lookup(const char *zone, const char *name, void *dbdata,
284 dns_sdblookup_t *lookup)
286 builtin_t *b = (builtin_t *) dbdata;
290 if (strcmp(name, "@") == 0)
291 return (b->do_lookup(lookup));
293 return (ISC_R_NOTFOUND);
297 dns64_lookup(const dns_name_t *zone, const dns_name_t *name, void *dbdata,
298 dns_sdblookup_t *lookup)
300 builtin_t *b = (builtin_t *) dbdata;
302 if (name->labels == 0 && name->length == 0)
303 return (b->do_lookup(lookup));
305 return (dns64_cname(zone, name, lookup));
309 put_txt(dns_sdblookup_t *lookup, const char *text) {
310 unsigned char buf[256];
311 unsigned int len = strlen(text);
313 len = 255; /* Silently truncate */
315 memcpy(&buf[1], text, len);
316 return (dns_sdb_putrdata(lookup, dns_rdatatype_txt, 0, buf, len + 1));
320 do_version_lookup(dns_sdblookup_t *lookup) {
321 if (ns_g_server->version_set) {
322 if (ns_g_server->version == NULL)
323 return (ISC_R_SUCCESS);
325 return (put_txt(lookup, ns_g_server->version));
327 return (put_txt(lookup, ns_g_version));
332 do_hostname_lookup(dns_sdblookup_t *lookup) {
333 if (ns_g_server->hostname_set) {
334 if (ns_g_server->hostname == NULL)
335 return (ISC_R_SUCCESS);
337 return (put_txt(lookup, ns_g_server->hostname));
340 isc_result_t result = ns_os_gethostname(buf, sizeof(buf));
341 if (result != ISC_R_SUCCESS)
343 return (put_txt(lookup, buf));
348 do_authors_lookup(dns_sdblookup_t *lookup) {
351 static const char *authors[] = {
357 "Andreas Gustafsson",
373 * If a version string is specified, disable the authors.bind zone.
375 if (ns_g_server->version_set)
376 return (ISC_R_SUCCESS);
378 for (p = authors; *p != NULL; p++) {
379 result = put_txt(lookup, *p);
380 if (result != ISC_R_SUCCESS)
383 return (ISC_R_SUCCESS);
387 do_id_lookup(dns_sdblookup_t *lookup) {
389 if (ns_g_server->server_usehostname) {
391 isc_result_t result = ns_os_gethostname(buf, sizeof(buf));
392 if (result != ISC_R_SUCCESS)
394 return (put_txt(lookup, buf));
397 if (ns_g_server->server_id == NULL)
398 return (ISC_R_SUCCESS);
400 return (put_txt(lookup, ns_g_server->server_id));
404 do_dns64_lookup(dns_sdblookup_t *lookup) {
406 return (ISC_R_SUCCESS);
410 do_empty_lookup(dns_sdblookup_t *lookup) {
413 return (ISC_R_SUCCESS);
417 builtin_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) {
419 const char *contact = "hostmaster";
420 const char *server = "@";
421 builtin_t *b = (builtin_t *) dbdata;
426 if (b == &empty_builtin) {
430 if (b->server != NULL)
432 if (b->contact != NULL)
433 contact = b->contact;
436 result = dns_sdb_putsoa(lookup, server, contact, 0);
437 if (result != ISC_R_SUCCESS)
438 return (ISC_R_FAILURE);
440 result = dns_sdb_putrr(lookup, "ns", 0, server);
441 if (result != ISC_R_SUCCESS)
442 return (ISC_R_FAILURE);
444 return (ISC_R_SUCCESS);
448 builtin_create(const char *zone, int argc, char **argv,
449 void *driverdata, void **dbdata)
456 if (strcmp(argv[0], "empty") == 0 || strcmp(argv[0], "dns64") == 0) {
458 return (DNS_R_SYNTAX);
459 } else if (argc != 1)
460 return (DNS_R_SYNTAX);
462 if (strcmp(argv[0], "version") == 0)
463 *dbdata = &version_builtin;
464 else if (strcmp(argv[0], "hostname") == 0)
465 *dbdata = &hostname_builtin;
466 else if (strcmp(argv[0], "authors") == 0)
467 *dbdata = &authors_builtin;
468 else if (strcmp(argv[0], "id") == 0)
469 *dbdata = &id_builtin;
470 else if (strcmp(argv[0], "empty") == 0 ||
471 strcmp(argv[0], "dns64") == 0) {
476 * We don't want built-in zones to fail. Fallback to
477 * the static configuration if memory allocation fails.
479 empty = isc_mem_get(ns_g_mctx, sizeof(*empty));
480 server = isc_mem_strdup(ns_g_mctx, argv[1]);
481 contact = isc_mem_strdup(ns_g_mctx, argv[2]);
482 if (empty == NULL || server == NULL || contact == NULL) {
483 if (strcmp(argv[0], "empty") == 0)
484 *dbdata = &empty_builtin;
486 *dbdata = &dns64_builtin;
488 isc_mem_free(ns_g_mctx, server);
490 isc_mem_free(ns_g_mctx, contact);
492 isc_mem_put(ns_g_mctx, empty, sizeof (*empty));
494 if (strcmp(argv[0], "empty") == 0)
495 memcpy(empty, &empty_builtin,
496 sizeof (empty_builtin));
498 memcpy(empty, &dns64_builtin,
499 sizeof (empty_builtin));
500 empty->server = server;
501 empty->contact = contact;
505 return (ISC_R_NOTIMPLEMENTED);
506 return (ISC_R_SUCCESS);
510 builtin_destroy(const char *zone, void *driverdata, void **dbdata) {
511 builtin_t *b = (builtin_t *) *dbdata;
517 * Don't free the static versions.
519 if (*dbdata == &version_builtin || *dbdata == &hostname_builtin ||
520 *dbdata == &authors_builtin || *dbdata == &id_builtin ||
521 *dbdata == &empty_builtin || *dbdata == &dns64_builtin)
524 isc_mem_free(ns_g_mctx, b->server);
525 isc_mem_free(ns_g_mctx, b->contact);
526 isc_mem_put(ns_g_mctx, b, sizeof (*b));
529 static dns_sdbmethods_t builtin_methods = {
538 static dns_sdbmethods_t dns64_methods = {
548 ns_builtin_init(void) {
549 RUNTIME_CHECK(dns_sdb_register("_builtin", &builtin_methods, NULL,
550 DNS_SDBFLAG_RELATIVEOWNER |
551 DNS_SDBFLAG_RELATIVERDATA,
552 ns_g_mctx, &builtin_impl)
554 RUNTIME_CHECK(dns_sdb_register("_dns64", &dns64_methods, NULL,
555 DNS_SDBFLAG_RELATIVEOWNER |
556 DNS_SDBFLAG_RELATIVERDATA |
558 ns_g_mctx, &dns64_impl)
560 return (ISC_R_SUCCESS);
564 ns_builtin_deinit(void) {
565 dns_sdb_unregister(&builtin_impl);
566 dns_sdb_unregister(&dns64_impl);