query_module.c 14.6 KB
Newer Older
1
/*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16
 */

17
#include <assert.h>
18 19 20 21 22
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

23
#include "contrib/sockaddr.h"
24 25 26 27
#include "libknot/attribute.h"
#include "knot/common/log.h"
#include "knot/conf/module.h"
#include "knot/conf/tools.h"
28 29
#include "knot/dnssec/rrset-sign.h"
#include "knot/dnssec/zone-sign.h"
30
#include "knot/nameserver/query_module.h"
31
#include "knot/nameserver/process_query.h"
32

33
#ifdef HAVE_ATOMIC
34 35 36
 #define ATOMIC_ADD(dst, val) __atomic_add_fetch(&(dst), (val), __ATOMIC_RELAXED)
 #define ATOMIC_SUB(dst, val) __atomic_sub_fetch(&(dst), (val), __ATOMIC_RELAXED)
 #define ATOMIC_SET(dst, val) __atomic_store_n(&(dst), (val), __ATOMIC_RELAXED)
37
#else
38 39 40 41
 #warning "Statistics data can be inaccurate if configured with multiple udp/tcp workers"
 #define ATOMIC_ADD(dst, val) ((dst) += (val))
 #define ATOMIC_SUB(dst, val) ((dst) -= (val))
 #define ATOMIC_SET(dst, val) ((dst) = (val))
42
#endif
43 44 45 46 47 48

_public_
int knotd_conf_check_ref(knotd_conf_check_args_t *args)
{
	return check_ref(args);
}
49

Daniel Salzman's avatar
Daniel Salzman committed
50
struct query_plan *query_plan_create(void)
51
{
Daniel Salzman's avatar
Daniel Salzman committed
52
	struct query_plan *plan = malloc(sizeof(struct query_plan));
53 54 55 56
	if (plan == NULL) {
		return NULL;
	}

57
	for (unsigned i = 0; i < KNOTD_STAGES; ++i) {
58 59 60 61 62 63 64 65 66 67 68 69
		init_list(&plan->stage[i]);
	}

	return plan;
}

void query_plan_free(struct query_plan *plan)
{
	if (plan == NULL) {
		return;
	}

70
	for (unsigned i = 0; i < KNOTD_STAGES; ++i) {
71 72
		struct query_step *step = NULL, *next = NULL;
		WALK_LIST_DELSAFE(step, next, plan->stage[i]) {
Daniel Salzman's avatar
Daniel Salzman committed
73
			free(step);
74 75 76
		}
	}

Daniel Salzman's avatar
Daniel Salzman committed
77
	free(plan);
78 79
}

Daniel Salzman's avatar
Daniel Salzman committed
80
static struct query_step *make_step(query_step_process_f process, void *ctx)
81
{
Daniel Salzman's avatar
Daniel Salzman committed
82
	struct query_step *step = calloc(1, sizeof(struct query_step));
83 84 85 86 87 88
	if (step == NULL) {
		return NULL;
	}

	step->process = process;
	step->ctx = ctx;
89

90 91 92
	return step;
}

93 94
int query_plan_step(struct query_plan *plan, knotd_stage_t stage,
                    query_step_process_f process, void *ctx)
95
{
Daniel Salzman's avatar
Daniel Salzman committed
96
	struct query_step *step = make_step(process, ctx);
97 98 99 100 101
	if (step == NULL) {
		return KNOT_ENOMEM;
	}

	add_tail(&plan->stage[stage], &step->node);
102

103 104 105
	return KNOT_EOK;
}

106 107
_public_
int knotd_mod_hook(knotd_mod_t *mod, knotd_stage_t stage, knotd_mod_hook_f hook)
108
{
109 110 111 112 113
	if (stage != KNOTD_STAGE_BEGIN && stage != KNOTD_STAGE_END) {
		return KNOT_EINVAL;
	}

	return query_plan_step(mod->plan, stage, hook, mod);
114 115
}

116 117
_public_
int knotd_mod_in_hook(knotd_mod_t *mod, knotd_stage_t stage, knotd_mod_in_hook_f hook)
118
{
119 120 121 122 123 124 125 126
	if (stage == KNOTD_STAGE_BEGIN || stage == KNOTD_STAGE_END) {
		return KNOT_EINVAL;
	}

	return query_plan_step(mod->plan, stage, hook, mod);
}

knotd_mod_t *query_module_open(conf_t *conf, conf_mod_id_t *mod_id,
127
                               struct query_plan *plan, const knot_dname_t *zone)
128 129 130 131 132 133 134 135 136 137 138 139 140
{
	if (conf == NULL || mod_id == NULL || plan == NULL) {
		return NULL;
	}

	/* Locate the module. */
	const module_t *mod = conf_mod_find(conf, mod_id->name + 1,
	                                    mod_id->name[0], false);
	if (mod == NULL) {
		return NULL;
	}

	/* Create query module. */
141
	knotd_mod_t *module = calloc(1, sizeof(knotd_mod_t));
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	if (module == NULL) {
		return NULL;
	}

	module->plan = plan;
	module->config = conf;
	module->zone = zone;
	module->id = mod_id;
	module->api = mod->api;

	return module;
}

void query_module_close(knotd_mod_t *module)
{
	if (module == NULL) {
		return;
	}

	knotd_mod_stats_free(module);
	conf_free_mod_id(module->id);
163

164
	zone_sign_ctx_free(module->sign_ctx);
165 166
	free_zone_keys(module->keyset);
	free(module->keyset);
167 168 169 170 171
	if (module->dnssec != NULL) {
		knot_lmdb_deinit(module->dnssec->kasp_db);
		kdnssec_ctx_deinit(module->dnssec);
		free(module->dnssec);
	}
172

173
	free(module);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
}

_public_
void *knotd_mod_ctx(knotd_mod_t *mod)
{
	return (mod != NULL) ? mod->ctx : NULL;
}

_public_
void knotd_mod_ctx_set(knotd_mod_t *mod, void *ctx)
{
	if (mod != NULL) mod->ctx = ctx;
}

_public_
const knot_dname_t *knotd_mod_zone(knotd_mod_t *mod)
{
	return (mod != NULL) ? mod->zone : NULL;
}

_public_
void knotd_mod_log(knotd_mod_t *mod, int priority, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	knotd_mod_vlog(mod, priority, fmt, args);
	va_end(args);
}

_public_
void knotd_mod_vlog(knotd_mod_t *mod, int priority, const char *fmt, va_list args)
{
	if (mod == NULL || fmt == NULL) {
		return;
	}

	char msg[512];

	if (vsnprintf(msg, sizeof(msg), fmt, args) < 0) {
		msg[0] = '\0';
	}

	#define LOG_ARGS(mod_id, msg) "module '%s%s%.*s', %s", \
		mod_id->name + 1, (mod_id->len > 0) ? "/" : "", (int)mod_id->len, \
		mod_id->data, msg

	if (mod->zone == NULL) {
		log_fmt(priority, LOG_SOURCE_SERVER, LOG_ARGS(mod->id, msg));
	} else {
223
		log_fmt_zone(priority, LOG_SOURCE_ZONE, mod->zone, NULL,
224 225 226 227 228 229 230 231 232 233 234
		             LOG_ARGS(mod->id, msg));
	}

	#undef LOG_ARGS
}

_public_
int knotd_mod_stats_add(knotd_mod_t *mod, const char *ctr_name, uint32_t idx_count,
                        knotd_mod_idx_to_str_f idx_to_str)
{
	if (mod == NULL || idx_count == 0) {
235 236 237 238
		return KNOT_EINVAL;
	}

	mod_ctr_t *stats = NULL;
239 240
	if (mod->stats == NULL) {
		assert(mod->stats_count == 0);
241
		stats = malloc(sizeof(*stats));
242 243 244
		if (stats == NULL) {
			return KNOT_ENOMEM;
		}
245
		mod->stats = stats;
246
	} else {
247 248
		assert(mod->stats_count > 0);
		size_t old_size = mod->stats_count * sizeof(*stats);
249
		size_t new_size = old_size + sizeof(*stats);
250
		stats = realloc(mod->stats, new_size);
251
		if (stats == NULL) {
252
			knotd_mod_stats_free(mod);
253 254
			return KNOT_ENOMEM;
		}
255 256
		mod->stats = stats;
		stats += mod->stats_count;
257 258
	}

259
	mod->stats_count++;
260

261 262 263
	if (idx_count == 1) {
		stats->counter = 0;
	} else {
264
		size_t size = idx_count * sizeof(((mod_ctr_t *)0)->counter);
265
		stats->counters = calloc(1, size);
266
		if (stats->counters == NULL) {
267
			knotd_mod_stats_free(mod);
268 269
			return KNOT_ENOMEM;
		}
270
		stats->idx_to_str = idx_to_str;
271
	}
272 273
	stats->name = ctr_name;
	stats->count = idx_count;
274 275 276 277

	return KNOT_EOK;
}

278 279
_public_
void knotd_mod_stats_free(knotd_mod_t *mod)
280
{
281
	if (mod == NULL || mod->stats == NULL) {
282 283 284
		return;
	}

285 286
	for (int i = 0; i < mod->stats_count; i++) {
		if (mod->stats[i].count > 1) {
287
			free(mod->stats[i].counters);
288 289 290
		}
	}

291
	free(mod->stats);
292 293 294 295 296 297 298 299
}

#define STATS_BODY(OPERATION) { \
	if (mod == NULL) return; \
	\
	mod_ctr_t *ctr = mod->stats + ctr_id; \
	if (ctr->count == 1) { \
		assert(idx == 0); \
300
		OPERATION(ctr->counter, val); \
301 302
	} else { \
		assert(idx < ctr->count); \
303
		OPERATION(ctr->counters[idx], val); \
304
	} \
305 306
}

307 308
_public_
void knotd_mod_stats_incr(knotd_mod_t *mod, uint32_t ctr_id, uint32_t idx, uint64_t val)
309
{
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	STATS_BODY(ATOMIC_ADD)
}

_public_
void knotd_mod_stats_decr(knotd_mod_t *mod, uint32_t ctr_id, uint32_t idx, uint64_t val)
{
	STATS_BODY(ATOMIC_SUB)
}

_public_
void knotd_mod_stats_store(knotd_mod_t *mod, uint32_t ctr_id, uint32_t idx, uint64_t val)
{
	STATS_BODY(ATOMIC_SET)
}

_public_
knotd_conf_t knotd_conf_env(knotd_mod_t *mod, knotd_conf_env_t env)
{
	static const char *version = "Knot DNS " PACKAGE_VERSION;

	knotd_conf_t out = { { 0 } };

	if (mod == NULL) {
		return out;
	}

	conf_t *config = (mod->config != NULL) ? mod->config : conf();

	switch (env) {
	case KNOTD_CONF_ENV_VERSION:
		out.single.string = version;
		break;
	case KNOTD_CONF_ENV_HOSTNAME:
		out.single.string = config->hostname;
		break;
	case KNOTD_CONF_ENV_WORKERS_UDP:
		out.single.integer = conf_udp_threads(config);
		break;
	case KNOTD_CONF_ENV_WORKERS_TCP:
		out.single.integer = conf_tcp_threads(config);
		break;
	default:
		return out;
	}

	out.count = 1;

	return out;
}

static void set_val(yp_type_t type, knotd_conf_val_t *item, conf_val_t *val)
{
	switch (type) {
	case YP_TINT:
		item->integer = conf_int(val);
		break;
	case YP_TBOOL:
		item->boolean = conf_bool(val);
		break;
	case YP_TOPT:
		item->option = conf_opt(val);
		break;
	case YP_TSTR:
		item->string = conf_str(val);
		break;
	case YP_TDNAME:
		item->dname = conf_dname(val);
		break;
	case YP_TADDR:
		item->addr = conf_addr(val, NULL);
		break;
	case YP_TNET:
		item->addr = conf_addr_range(val, &item->addr_max,
		                             &item->addr_mask);
		break;
	case YP_TREF:
		if (val->code == KNOT_EOK) {
			conf_val(val);
			item->data_len = val->len;
			item->data = val->data;
390
		}
391 392 393 394 395 396 397 398 399 400
		break;
	case YP_THEX:
	case YP_TB64:
		item->data = conf_bin(val, &item->data_len);
		break;
	case YP_TDATA:
		item->data = conf_data(val, &item->data_len);
		break;
	default:
		return;
401
	}
402
}
403

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
static void set_conf_out(knotd_conf_t *out, conf_val_t *val)
{
	if (!(val->item->flags & YP_FMULTI)) {
		out->count = (val->code == KNOT_EOK) ? 1 : 0;
		set_val(val->item->type, &out->single, val);
	} else {
		size_t count = conf_val_count(val);
		if (count == 0) {
			return;
		}

		out->multi = malloc(count * sizeof(*out->multi));
		if (out->multi == NULL) {
			return;
		}
		memset(out->multi, 0, count * sizeof(*out->multi));

		for (size_t i = 0; i < count; i++) {
			set_val(val->item->type, &out->multi[i], val);
			conf_val_next(val);
		}
		out->count = count;
	}
427 428
}

429 430 431
_public_
knotd_conf_t knotd_conf(knotd_mod_t *mod, const yp_name_t *section_name,
                        const yp_name_t *item_name, const knotd_conf_t *id)
432
{
433 434 435 436
	knotd_conf_t out = { { 0 } };

	if (mod == NULL || section_name == NULL || item_name == NULL) {
		return out;
437 438
	}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	conf_t *config = (mod->config != NULL) ? mod->config : conf();

	const uint8_t *raw_id = (id != NULL) ? id->single.data : NULL;
	size_t raw_id_len = (id != NULL) ? id->single.data_len : 0;
	conf_val_t val = conf_rawid_get(config, section_name, item_name,
	                                raw_id, raw_id_len);

	set_conf_out(&out, &val);

	return out;
}

_public_
knotd_conf_t knotd_conf_mod(knotd_mod_t *mod, const yp_name_t *item_name)
{
	knotd_conf_t out = { { 0 } };

	if (mod == NULL || item_name == NULL) {
		return out;
458 459
	}

460 461 462 463 464
	conf_t *config = (mod->config != NULL) ? mod->config : conf();

	conf_val_t val = conf_mod_get(config, item_name, mod->id);
	if (val.item == NULL) {
		return out;
465
	}
466

467
	set_conf_out(&out, &val);
468

469
	return out;
470 471
}

472 473 474
_public_
knotd_conf_t knotd_conf_zone(knotd_mod_t *mod, const yp_name_t *item_name,
                             const knot_dname_t *zone)
475
{
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	knotd_conf_t out = { { 0 } };

	if (mod == NULL || item_name == NULL || zone == NULL) {
		return out;
	}

	conf_t *config = (mod->config != NULL) ? mod->config : conf();

	conf_val_t val = conf_zone_get(config, item_name, zone);

	set_conf_out(&out, &val);

	return out;
}

_public_
knotd_conf_t knotd_conf_check_item(knotd_conf_check_args_t *args,
                                   const yp_name_t *item_name)
{
	knotd_conf_t out = { { 0 } };

	conf_val_t val = conf_rawid_get_txn(args->extra->conf, args->extra->txn,
498 499
	                                    args->item->name, item_name,
	                                    args->id, args->id_len);
500 501 502 503 504 505

	set_conf_out(&out, &val);

	return out;
}

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
_public_
bool knotd_conf_addr_range_match(const knotd_conf_t *range,
                                 const struct sockaddr_storage *addr)
{
	if (range == NULL || addr == NULL) {
		return false;
	}

	for (size_t i = 0; i < range->count; i++) {
		knotd_conf_val_t *val = &range->multi[i];
		if (val->addr_max.ss_family == AF_UNSPEC) {
			if (sockaddr_net_match((struct sockaddr *)addr,
			                       (struct sockaddr *)&val->addr,
			                       val->addr_mask)) {
				return true;
			}
		} else {
			if (sockaddr_range_match((struct sockaddr *)addr,
			                         (struct sockaddr *)&val->addr,
			                         (struct sockaddr *)&val->addr_max)) {
				return true;
			}
		}
	}

	return false;
}

534 535 536 537
_public_
void knotd_conf_free(knotd_conf_t *conf)
{
	if (conf == NULL) {
538 539 540
		return;
	}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	if (conf->count > 0 && conf->multi != NULL) {
		memset(conf->multi, 0, conf->count * sizeof(*conf->multi));
		free(conf->multi);
	}
	memset(conf, 0, sizeof(*conf));
}

_public_
const knot_dname_t *knotd_qdata_zone_name(knotd_qdata_t *qdata)
{
	if (qdata == NULL || qdata->extra->zone == NULL) {
		return NULL;
	}

	return qdata->extra->zone->name;
}

_public_
knot_rrset_t knotd_qdata_zone_apex_rrset(knotd_qdata_t *qdata, uint16_t type)
{
	if (qdata == NULL || qdata->extra->zone == NULL ||
	    qdata->extra->zone->contents == NULL) {
		return node_rrset(NULL, type);
	}

	return node_rrset(qdata->extra->zone->contents->apex, type);
567
}
568 569 570 571

_public_
int knotd_mod_dnssec_init(knotd_mod_t *mod)
{
572
	if (mod == NULL || mod->dnssec != NULL) {
573 574 575
		return KNOT_EINVAL;
	}

576 577 578
	knot_lmdb_db_t *kaspdb;

	mod->dnssec = calloc(1, sizeof(*(mod->dnssec)) + sizeof(*kaspdb));
579 580 581
	if (mod->dnssec == NULL) {
		return KNOT_ENOMEM;
	}
582 583
	kaspdb = (knot_lmdb_db_t *)(mod->dnssec + 1);

584
	char *kasp_dir = conf_db(mod->config, C_KASP_DB);
585 586 587
	conf_val_t kasp_size = conf_default_get(mod->config, C_MAX_KASP_DB_SIZE);
	knot_lmdb_init(kaspdb, kasp_dir, conf_int(&kasp_size), 0, "keys_db");
	free(kasp_dir);
588 589

	conf_val_t conf = conf_zone_get(mod->config, C_DNSSEC_SIGNING, mod->zone);
590
	int ret = kdnssec_ctx_init(mod->config, mod->dnssec, mod->zone, kaspdb,
591 592
	                           conf_bool(&conf) ? NULL : mod->id);
	if (ret != KNOT_EOK) {
593
		knot_lmdb_deinit(kaspdb);
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
		free(mod->dnssec);
		return ret;
	}

	return KNOT_EOK;
}

_public_
int knotd_mod_dnssec_load_keyset(knotd_mod_t *mod, bool verbose)
{
	if (mod == NULL || mod->dnssec == NULL) {
		return KNOT_EINVAL;
	}

	mod->keyset = calloc(1, sizeof(*(mod->keyset)));
	if (mod->keyset == NULL) {
		return KNOT_ENOMEM;
	}

	int ret = load_zone_keys(mod->dnssec, mod->keyset, verbose);
	if (ret != KNOT_EOK) {
		free(mod->keyset);
		mod->keyset = NULL;
		return ret;
	}

620 621 622 623 624 625 626 627
	mod->sign_ctx = zone_sign_ctx(mod->keyset, mod->dnssec);
	if (mod->sign_ctx == NULL) {
		free_zone_keys(mod->keyset);
		free(mod->keyset);
		mod->keyset = NULL;
		return KNOT_ENOMEM;
	}

628 629 630
	return KNOT_EOK;
}

631 632 633 634 635 636 637 638 639 640 641 642 643
_public_
void knotd_mod_dnssec_unload_keyset(knotd_mod_t *mod)
{
	if (mod != NULL && mod->keyset != NULL) {
		zone_sign_ctx_free(mod->sign_ctx);
		mod->sign_ctx = NULL;

		free_zone_keys(mod->keyset);
		free(mod->keyset);
		mod->keyset = NULL;
	}
}

644 645 646 647
_public_
int knotd_mod_dnssec_sign_rrset(knotd_mod_t *mod, knot_rrset_t *rrsigs,
                                const knot_rrset_t *rrset, knot_mm_t *mm)
{
648
	if (mod == NULL || rrsigs == NULL || rrset == NULL) {
649 650 651
		return KNOT_EINVAL;
	}

652
	return knot_sign_rrset2(rrsigs, rrset, mod->sign_ctx, mm);
653
}