builtin.c 15.3 KB
Newer Older
1
/*
Tinderbox User's avatar
Tinderbox User committed
2
 * Copyright (C) 2004, 2005, 2007, 2009-2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2001-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7 8
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17
 */

Evan Hunt's avatar
Evan Hunt committed
18
/* $Id: builtin.c,v 1.26 2012/01/21 19:44:18 each Exp $ */
19

20 21
/*! \file
 * \brief
22
 * The built-in "version", "hostname", "id", "authors" and "empty" databases.
23 24 25 26 27 28 29
 */

#include <config.h>

#include <string.h>
#include <stdio.h>

30
#include <isc/mem.h>
31 32 33 34 35
#include <isc/print.h>
#include <isc/result.h>
#include <isc/util.h>

#include <dns/result.h>
36
#include <dns/sdb.h>
37 38 39 40 41 42 43 44 45 46 47

#include <named/builtin.h>
#include <named/globals.h>
#include <named/server.h>
#include <named/os.h>

typedef struct builtin builtin_t;

static isc_result_t do_version_lookup(dns_sdblookup_t *lookup);
static isc_result_t do_hostname_lookup(dns_sdblookup_t *lookup);
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);
51 52 53 54 55 56 57 58 59

/*
 * We can't use function pointers as the db_data directly
 * because ANSI C does not guarantee that function pointers
 * can safely be cast to void pointers and back.
 */

struct builtin {
	isc_result_t (*do_lookup)(dns_sdblookup_t *lookup);
60 61
	char *server;
	char *contact;
62 63
};

64 65 66 67 68
static builtin_t version_builtin = { do_version_lookup,  NULL, NULL };
static builtin_t hostname_builtin = { do_hostname_lookup, NULL, NULL };
static builtin_t authors_builtin = { do_authors_lookup, NULL, NULL };
static builtin_t id_builtin = { do_id_lookup, NULL, NULL };
static builtin_t empty_builtin = { do_empty_lookup, NULL, NULL };
69
static builtin_t dns64_builtin = { do_dns64_lookup, NULL, NULL };
70 71

static dns_sdbimplementation_t *builtin_impl;
72
static dns_sdbimplementation_t *dns64_impl;
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/*
 * Pre computed HEX * 16 or 1 table.
 */
static const unsigned char hex16[256] = {
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*00*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,	/*10*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*20*/
	 0, 16, 32, 48, 64, 80, 96,112,128,144,  1,  1,  1,  1,  1,  1,	/*30*/
	 1,160,176,192,208,224,240,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*40*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*50*/
	 1,160,176,192,208,224,240,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*60*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*70*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*80*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*90*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*A0*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*B0*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*C0*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*D0*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, /*E0*/
	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1  /*F0*/
};

const unsigned char decimal[] = "0123456789";

static size_t
dns64_rdata(unsigned char *v, size_t start, unsigned char *rdata) {
	size_t i, j = 0;

Mark Andrews's avatar
Mark Andrews committed
102
	for (i = 0; i < 4U; i++) {
103
		unsigned char c = v[start++];
Mark Andrews's avatar
Mark Andrews committed
104
		if (start == 7U)
105 106 107 108 109 110 111 112 113 114 115 116 117 118
			start++;
		if (c > 99) {
			rdata[j++] = 3;
			rdata[j++] = decimal[c/100]; c = c % 100;
			rdata[j++] = decimal[c/10]; c = c % 10;
			rdata[j++] = decimal[c];
		} else if (c > 9) {
			rdata[j++] = 2;
			rdata[j++] = decimal[c/10]; c = c % 10;
			rdata[j++] = decimal[c];
		} else {
			rdata[j++] = 1;
			rdata[j++] = decimal[c];
		}
Tinderbox User's avatar
Tinderbox User committed
119
	}
120
	memmove(&rdata[j], "\07in-addr\04arpa", 14);
121 122
	return (j + 14);
}
123 124

static isc_result_t
125 126 127 128 129
dns64_cname(const dns_name_t *zone, const dns_name_t *name,
	    dns_sdblookup_t *lookup)
{
	size_t zlen, nlen, j, len;
	unsigned char v[16], n;
130
	unsigned int i;
131 132
	unsigned char rdata[sizeof("123.123.123.123.in-addr.arpa.")];
	unsigned char *ndata;
133 134

	/*
135
	 * The combined length of the zone and name is 74.
136
	 *
137
	 * The minimum zone length is 10 ((3)ip6(4)arpa(0)).
138
	 *
139
	 * The length of name should always be even as we are expecting
140 141
	 * a series of nibbles.
	 */
142 143 144
	zlen = zone->length;
	nlen = name->length;
	if ((zlen + nlen) > 74U || zlen < 10U || (nlen % 2) != 0U)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		return (ISC_R_NOTFOUND);

	/*
	 * We assume the zone name is well formed.
	 */

	/*
	 * XXXMPA We could check the dns64 suffix here if we need to.
	 */
	/*
	 * Check that name is a series of nibbles.
	 * Compute the byte values that correspond to the nibbles as we go.
	 *
	 * Shift the final result 4 bits, by setting 'i' to 1, if we if we
	 * have a odd number of nibbles so that "must be zero" tests below
	 * are byte aligned and we correctly return ISC_R_NOTFOUND or
	 * ISC_R_SUCCESS.  We will not generate a CNAME in this case.
	 */
163 164
	ndata = name->ndata;
	i = (nlen % 4) == 2U ? 1 : 0;
165 166
	j = nlen;
	memset(v, 0, sizeof(v));
Mark Andrews's avatar
Mark Andrews committed
167
	while (j != 0U) {
168
		INSIST((i/2) < sizeof(v));
169
		if (ndata[0] != 1)
170
			return (ISC_R_NOTFOUND);
171 172
		n = hex16[ndata[1]&0xff];
		if (n == 1)
173
			return (ISC_R_NOTFOUND);
174 175 176
		v[i/2] = n | (v[i/2]>>4);
		j -= 2;
		ndata += 2;
177 178 179 180
		i++;
	}

	/*
Automatic Updater's avatar
Automatic Updater committed
181
	 * If we get here then we know name only consisted of nibbles.
182 183 184 185
	 * Now we need to determine if the name exists or not and whether
	 * it corresponds to a empty node in the zone or there should be
	 * a CNAME.
	 */
186
#define ZLEN(x) (10 + (x)/2)
187
	switch (zlen) {
188
	case ZLEN(32):	/* prefix len 32 */
189
		/*
190 191 192 193 194 195 196
		 * The nibbles that map to this byte must be zero for 'name'
		 * to exist in the zone.
		 */
		if (nlen > 16U && v[(nlen-1)/4 - 4] != 0)
			return (ISC_R_NOTFOUND);
		/*
		 * If the total length is not 74 then this is a empty node
197 198
		 * so return success.
		 */
199
		if (nlen + zlen != 74U)
200
			return (ISC_R_SUCCESS);
201
		len = dns64_rdata(v, 8, rdata);
202
		break;
203
	case ZLEN(40):	/* prefix len 40 */
204 205 206 207
		/*
		 * The nibbles that map to this byte must be zero for 'name'
		 * to exist in the zone.
		 */
208
		if (nlen > 12U && v[(nlen-1)/4 - 3] != 0)
209 210
			return (ISC_R_NOTFOUND);
		/*
211
		 * If the total length is not 74 then this is a empty node
212 213
		 * so return success.
		 */
214
		if (nlen + zlen != 74U)
215
			return (ISC_R_SUCCESS);
216
		len = dns64_rdata(v, 6, rdata);
217
		break;
218
	case ZLEN(48):	/* prefix len 48 */
219 220 221 222
		/*
		 * The nibbles that map to this byte must be zero for 'name'
		 * to exist in the zone.
		 */
223
		if (nlen > 8U && v[(nlen-1)/4 - 2] != 0)
224 225
			return (ISC_R_NOTFOUND);
		/*
226
		 * If the total length is not 74 then this is a empty node
227 228
		 * so return success.
		 */
229
		if (nlen + zlen != 74U)
230
			return (ISC_R_SUCCESS);
231
		len = dns64_rdata(v, 5, rdata);
232
		break;
233
	case ZLEN(56):	/* prefix len 56 */
234 235 236 237
		/*
		 * The nibbles that map to this byte must be zero for 'name'
		 * to exist in the zone.
		 */
238
		if (nlen > 4U && v[(nlen-1)/4 - 1] != 0)
239 240
			return (ISC_R_NOTFOUND);
		/*
241
		 * If the total length is not 74 then this is a empty node
242 243
		 * so return success.
		 */
244
		if (nlen + zlen != 74U)
245
			return (ISC_R_SUCCESS);
246
		len = dns64_rdata(v, 4, rdata);
247
		break;
248
	case ZLEN(64):	/* prefix len 64 */
249 250 251 252
		/*
		 * The nibbles that map to this byte must be zero for 'name'
		 * to exist in the zone.
		 */
253
		if (v[(nlen-1)/4] != 0)
254 255
			return (ISC_R_NOTFOUND);
		/*
256
		 * If the total length is not 74 then this is a empty node
257 258
		 * so return success.
		 */
259
		if (nlen + zlen != 74U)
260
			return (ISC_R_SUCCESS);
261
		len = dns64_rdata(v, 3, rdata);
262
		break;
263
	case ZLEN(96):	/* prefix len 96 */
264
		/*
265
		 * If the total length is not 74 then this is a empty node
266 267
		 * so return success.
		 */
268
		if (nlen + zlen != 74U)
269
			return (ISC_R_SUCCESS);
270
		len = dns64_rdata(v, 0, rdata);
271 272 273 274 275 276 277 278
		break;
	default:
		/*
		 * This should never be reached unless someone adds a
		 * zone declaration with this internal type to named.conf.
		 */
		return (ISC_R_NOTFOUND);
	}
279 280
	return (dns_sdb_putrdata(lookup, dns_rdatatype_cname, 600,
				 rdata, (unsigned int)len));
281 282
}

283 284
static isc_result_t
builtin_lookup(const char *zone, const char *name, void *dbdata,
285 286
	       dns_sdblookup_t *lookup, dns_clientinfomethods_t *methods,
	       dns_clientinfo_t *clientinfo)
287 288 289 290
{
	builtin_t *b = (builtin_t *) dbdata;

	UNUSED(zone);
291 292
	UNUSED(methods);
	UNUSED(clientinfo);
293 294 295 296 297 298 299

	if (strcmp(name, "@") == 0)
		return (b->do_lookup(lookup));
	else
		return (ISC_R_NOTFOUND);
}

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static isc_result_t
dns64_lookup(const dns_name_t *zone, const dns_name_t *name, void *dbdata,
	     dns_sdblookup_t *lookup, dns_clientinfomethods_t *methods,
	     dns_clientinfo_t *clientinfo)
{
	builtin_t *b = (builtin_t *) dbdata;

	UNUSED(methods);
	UNUSED(clientinfo);

	if (name->labels == 0 && name->length == 0)
		return (b->do_lookup(lookup));
	else
		return (dns64_cname(zone, name, lookup));
}

316 317 318 319 320 321 322
static isc_result_t
put_txt(dns_sdblookup_t *lookup, const char *text) {
	unsigned char buf[256];
	unsigned int len = strlen(text);
	if (len > 255)
		len = 255; /* Silently truncate */
	buf[0] = len;
323
	memmove(&buf[1], text, len);
324 325 326 327 328
	return (dns_sdb_putrdata(lookup, dns_rdatatype_txt, 0, buf, len + 1));
}

static isc_result_t
do_version_lookup(dns_sdblookup_t *lookup) {
Automatic Updater's avatar
Automatic Updater committed
329
	if (ns_g_server->version_set) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		if (ns_g_server->version == NULL)
			return (ISC_R_SUCCESS);
		else
			return (put_txt(lookup, ns_g_server->version));
	} else {
		return (put_txt(lookup, ns_g_version));
	}
}

static isc_result_t
do_hostname_lookup(dns_sdblookup_t *lookup) {
	if (ns_g_server->hostname_set) {
		if (ns_g_server->hostname == NULL)
			return (ISC_R_SUCCESS);
		else
			return (put_txt(lookup, ns_g_server->hostname));
	} else {
Mark Andrews's avatar
Mark Andrews committed
347
		char buf[256];
348 349 350 351 352 353 354 355 356 357 358 359 360
		isc_result_t result = ns_os_gethostname(buf, sizeof(buf));
		if (result != ISC_R_SUCCESS)
			return (result);
		return (put_txt(lookup, buf));
	}
}

static isc_result_t
do_authors_lookup(dns_sdblookup_t *lookup) {
	isc_result_t result;
	const char **p;
	static const char *authors[] = {
		"Mark Andrews",
361
		"Curtis Blackburn",
362 363
		"James Brister",
		"Ben Cottrell",
Evan Hunt's avatar
Evan Hunt committed
364
		"John H. DuBois III",
Francis Dupont's avatar
Francis Dupont committed
365
		"Francis Dupont",
366 367 368
		"Michael Graff",
		"Andreas Gustafsson",
		"Bob Halley",
369
		"Evan Hunt",
370
		"JINMEI Tatuya",
371
		"Witold Krecicki",
372
		"David Lawrence",
Scott Mann's avatar
Scott Mann committed
373
		"Scott Mann",
374 375 376
		"Danny Mayer",
		"Damien Neil",
		"Matt Nelson",
377
		"Jeremy C. Reed",
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		"Michael Sawyer",
		"Brian Wellington",
		NULL
	};

	/*
	 * If a version string is specified, disable the authors.bind zone.
	 */
	if (ns_g_server->version_set)
		return (ISC_R_SUCCESS);

	for (p = authors; *p != NULL; p++) {
		result = put_txt(lookup, *p);
		if (result != ISC_R_SUCCESS)
			return (result);
	}
	return (ISC_R_SUCCESS);
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
static isc_result_t
do_id_lookup(dns_sdblookup_t *lookup) {

	if (ns_g_server->server_usehostname) {
		char buf[256];
		isc_result_t result = ns_os_gethostname(buf, sizeof(buf));
		if (result != ISC_R_SUCCESS)
			return (result);
		return (put_txt(lookup, buf));
	}

	if (ns_g_server->server_id == NULL)
		return (ISC_R_SUCCESS);
	else
		return (put_txt(lookup, ns_g_server->server_id));
}

414 415 416 417 418 419
static isc_result_t
do_dns64_lookup(dns_sdblookup_t *lookup) {
	UNUSED(lookup);
	return (ISC_R_SUCCESS);
}

420 421 422 423 424 425 426
static isc_result_t
do_empty_lookup(dns_sdblookup_t *lookup) {

	UNUSED(lookup);
	return (ISC_R_SUCCESS);
}

427 428 429
static isc_result_t
builtin_authority(const char *zone, void *dbdata, dns_sdblookup_t *lookup) {
	isc_result_t result;
430 431 432
	const char *contact = "hostmaster";
	const char *server = "@";
	builtin_t *b = (builtin_t *) dbdata;
433 434 435 436

	UNUSED(zone);
	UNUSED(dbdata);

437 438 439 440 441 442 443 444 445
	if (b == &empty_builtin) {
		server = ".";
		contact = ".";
	} else {
		if (b->server != NULL)
			server = b->server;
		if (b->contact != NULL)
			contact = b->contact;
	}
Automatic Updater's avatar
Automatic Updater committed
446

447
	result = dns_sdb_putsoa(lookup, server, contact, 0);
448 449
	if (result != ISC_R_SUCCESS)
		return (ISC_R_FAILURE);
450 451

	result = dns_sdb_putrr(lookup, "ns", 0, server);
452 453 454 455 456 457 458 459 460 461
	if (result != ISC_R_SUCCESS)
		return (ISC_R_FAILURE);

	return (ISC_R_SUCCESS);
}

static isc_result_t
builtin_create(const char *zone, int argc, char **argv,
	       void *driverdata, void **dbdata)
{
462 463
	REQUIRE(argc >= 1);

464 465
	UNUSED(zone);
	UNUSED(driverdata);
466

467
	if (strcmp(argv[0], "empty") == 0 || strcmp(argv[0], "dns64") == 0) {
468 469 470
		if (argc != 3)
			return (DNS_R_SYNTAX);
	} else if (argc != 1)
471
		return (DNS_R_SYNTAX);
472

473 474 475 476 477 478
	if (strcmp(argv[0], "version") == 0)
		*dbdata = &version_builtin;
	else if (strcmp(argv[0], "hostname") == 0)
		*dbdata = &hostname_builtin;
	else if (strcmp(argv[0], "authors") == 0)
		*dbdata = &authors_builtin;
479 480
	else if (strcmp(argv[0], "id") == 0)
		*dbdata = &id_builtin;
481 482
	else if (strcmp(argv[0], "empty") == 0 ||
		 strcmp(argv[0], "dns64") == 0) {
483 484 485 486 487
		builtin_t *empty;
		char *server;
		char *contact;
		/*
		 * We don't want built-in zones to fail.  Fallback to
Mark Andrews's avatar
Mark Andrews committed
488
		 * the static configuration if memory allocation fails.
489 490 491 492 493
		 */
		empty = isc_mem_get(ns_g_mctx, sizeof(*empty));
		server = isc_mem_strdup(ns_g_mctx, argv[1]);
		contact = isc_mem_strdup(ns_g_mctx, argv[2]);
		if (empty == NULL || server == NULL || contact == NULL) {
494 495 496 497
			if (strcmp(argv[0], "empty") == 0)
				*dbdata = &empty_builtin;
			else
				*dbdata = &dns64_builtin;
498 499 500 501 502 503 504
			if (server != NULL)
				isc_mem_free(ns_g_mctx, server);
			if (contact != NULL)
				isc_mem_free(ns_g_mctx, contact);
			if (empty != NULL)
				isc_mem_put(ns_g_mctx, empty, sizeof (*empty));
		} else {
505
			if (strcmp(argv[0], "empty") == 0)
506 507
				memmove(empty, &empty_builtin,
					sizeof (empty_builtin));
508
			else
509 510
				memmove(empty, &dns64_builtin,
					sizeof (empty_builtin));
511 512 513 514 515
			empty->server = server;
			empty->contact = contact;
			*dbdata = empty;
		}
	} else
516 517 518 519
		return (ISC_R_NOTIMPLEMENTED);
	return (ISC_R_SUCCESS);
}

520 521 522 523 524 525 526 527 528 529 530 531
static void
builtin_destroy(const char *zone, void *driverdata, void **dbdata) {
	builtin_t *b = (builtin_t *) *dbdata;

	UNUSED(zone);
	UNUSED(driverdata);

	/*
	 * Don't free the static versions.
	 */
	if (*dbdata == &version_builtin || *dbdata == &hostname_builtin ||
	    *dbdata == &authors_builtin || *dbdata == &id_builtin ||
532
	    *dbdata == &empty_builtin || *dbdata == &dns64_builtin)
533 534 535 536 537 538 539
		return;

	isc_mem_free(ns_g_mctx, b->server);
	isc_mem_free(ns_g_mctx, b->contact);
	isc_mem_put(ns_g_mctx, b, sizeof (*b));
}

540 541 542 543 544
static dns_sdbmethods_t builtin_methods = {
	builtin_lookup,
	builtin_authority,
	NULL,		/* allnodes */
	builtin_create,
545 546 547 548 549 550 551 552 553 554 555
	builtin_destroy,
	NULL
};

static dns_sdbmethods_t dns64_methods = {
	NULL,
	builtin_authority,
	NULL,		/* allnodes */
	builtin_create,
	builtin_destroy,
	dns64_lookup,
556 557 558 559 560 561 562 563 564
};

isc_result_t
ns_builtin_init(void) {
	RUNTIME_CHECK(dns_sdb_register("_builtin", &builtin_methods, NULL,
				       DNS_SDBFLAG_RELATIVEOWNER |
				       DNS_SDBFLAG_RELATIVERDATA,
				       ns_g_mctx, &builtin_impl)
		      == ISC_R_SUCCESS);
565 566 567 568 569 570
	RUNTIME_CHECK(dns_sdb_register("_dns64", &dns64_methods, NULL,
				       DNS_SDBFLAG_RELATIVEOWNER |
				       DNS_SDBFLAG_RELATIVERDATA |
				       DNS_SDBFLAG_DNS64,
				       ns_g_mctx, &dns64_impl)
		      == ISC_R_SUCCESS);
571 572 573 574 575 576
	return (ISC_R_SUCCESS);
}

void
ns_builtin_deinit(void) {
	dns_sdb_unregister(&builtin_impl);
577
	dns_sdb_unregister(&dns64_impl);
578
}