commands.c 37.8 KB
Newer Older
1
/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
Daniel Salzman's avatar
Daniel Salzman committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    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
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Daniel Salzman's avatar
Daniel Salzman committed
17
#include <string.h>
18
#include <unistd.h>
Daniel Salzman's avatar
Daniel Salzman committed
19 20

#include "knot/common/log.h"
21
#include "knot/common/stats.h"
Daniel Salzman's avatar
Daniel Salzman committed
22 23
#include "knot/conf/confio.h"
#include "knot/ctl/commands.h"
24
#include "knot/events/handlers.h"
25
#include "knot/nameserver/query_module.h"
26
#include "knot/updates/zone-update.h"
27
#include "knot/zone/timers.h"
Daniel Salzman's avatar
Daniel Salzman committed
28
#include "libknot/libknot.h"
29
#include "libknot/yparser/yptrafo.h"
Daniel Salzman's avatar
Daniel Salzman committed
30
#include "contrib/macros.h"
31
#include "contrib/mempattern.h"
Daniel Salzman's avatar
Daniel Salzman committed
32
#include "contrib/string.h"
33
#include "zscanner/scanner.h"
34
#include "contrib/strtonum.h"
Daniel Salzman's avatar
Daniel Salzman committed
35

36 37 38 39 40 41 42 43 44 45 46
void ctl_log_data(knot_ctl_data_t *data)
{
	if (data == NULL) {
		return;
	}

	const char *zone = (*data)[KNOT_CTL_IDX_ZONE];
	const char *section = (*data)[KNOT_CTL_IDX_SECTION];
	const char *item = (*data)[KNOT_CTL_IDX_ITEM];
	const char *id = (*data)[KNOT_CTL_IDX_ID];

47 48 49 50
	if (section == NULL) {
		return;
	}

51
	if (zone != NULL) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65
		log_ctl_zone_str_debug(zone,
		              "control, item '%s%s%s%s%s%s'", section,
		              (id   != NULL ? "["  : ""),
		              (id   != NULL ? id   : ""),
		              (id   != NULL ? "]"  : ""),
		              (item != NULL ? "."  : ""),
		              (item != NULL ? item : ""));
	} else {
		log_ctl_debug("control, item '%s%s%s%s%s%s'", section,
		              (id   != NULL ? "["  : ""),
		              (id   != NULL ? id   : ""),
		              (id   != NULL ? "]"  : ""),
		              (item != NULL ? "."  : ""),
		              (item != NULL ? item : ""));
66 67 68
	}
}

69
static void send_error(ctl_args_t *args, const char *msg)
Daniel Salzman's avatar
Daniel Salzman committed
70
{
71 72
	knot_ctl_data_t data;
	memcpy(&data, args->data, sizeof(data));
Daniel Salzman's avatar
Daniel Salzman committed
73

74
	data[KNOT_CTL_IDX_ERROR] = msg;
Daniel Salzman's avatar
Daniel Salzman committed
75

76 77
	int ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &data);
	if (ret != KNOT_EOK) {
78
		log_ctl_debug("control, failed to send error (%s)", knot_strerror(ret));
79
	}
Daniel Salzman's avatar
Daniel Salzman committed
80 81
}

82
static int get_zone(ctl_args_t *args, zone_t **zone)
Daniel Salzman's avatar
Daniel Salzman committed
83
{
84 85 86 87
	const char *name = args->data[KNOT_CTL_IDX_ZONE];
	assert(name != NULL);

	uint8_t buff[KNOT_DNAME_MAXLEN];
Daniel Salzman's avatar
Daniel Salzman committed
88

89 90 91
	knot_dname_t *dname = knot_dname_from_str(buff, name, sizeof(buff));
	if (dname == NULL) {
		return KNOT_EINVAL;
Daniel Salzman's avatar
Daniel Salzman committed
92 93
	}

94
	int ret = knot_dname_to_lower(dname);
Daniel Salzman's avatar
Daniel Salzman committed
95
	if (ret != KNOT_EOK) {
96 97
		return ret;
	}
Daniel Salzman's avatar
Daniel Salzman committed
98

99 100 101
	*zone = knot_zonedb_find(args->server->zone_db, dname);
	if (*zone == NULL) {
		return KNOT_ENOZONE;
Daniel Salzman's avatar
Daniel Salzman committed
102
	}
103 104

	return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
105 106
}

107
static int zones_apply(ctl_args_t *args, int (*fcn)(zone_t *, ctl_args_t *))
Daniel Salzman's avatar
Daniel Salzman committed
108
{
109 110 111 112 113 114 115
	// Process all configured zones if none is specified.
	if (args->data[KNOT_CTL_IDX_ZONE] == NULL) {
		knot_zonedb_foreach(args->server->zone_db, fcn, args);
		return KNOT_EOK;
	}

	int ret = KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
116

117 118
	while (true) {
		zone_t *zone;
119
		ret = get_zone(args, &zone);
120 121 122 123
		if (ret == KNOT_EOK) {
			ret = fcn(zone, args);
		}
		if (ret != KNOT_EOK) {
124 125
			log_ctl_zone_str_error(args->data[KNOT_CTL_IDX_ZONE],
			                       "control, error (%s)", knot_strerror(ret));
126 127
			send_error(args, knot_strerror(ret));
		}
Daniel Salzman's avatar
Daniel Salzman committed
128

129 130 131 132
		// Get next zone name.
		ret = knot_ctl_receive(args->ctl, &args->type, &args->data);
		if (ret != KNOT_EOK || args->type != KNOT_CTL_TYPE_DATA) {
			break;
Daniel Salzman's avatar
Daniel Salzman committed
133
		}
134
		ctl_log_data(&args->data);
Daniel Salzman's avatar
Daniel Salzman committed
135 136
	}

137
	return ret;
Daniel Salzman's avatar
Daniel Salzman committed
138 139
}

140
static int zone_status(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
141
{
142 143 144 145 146
	// Zone name.
	char name[KNOT_DNAME_TXT_MAXLEN + 1];
	if (knot_dname_to_str(name, zone->name, sizeof(name)) == NULL) {
		return KNOT_EINVAL;
	}
Daniel Salzman's avatar
Daniel Salzman committed
147

148 149 150
	knot_ctl_data_t data = {
		[KNOT_CTL_IDX_ZONE] = name
	};
Daniel Salzman's avatar
Daniel Salzman committed
151

152 153 154 155 156 157 158
	// Zone type.
	data[KNOT_CTL_IDX_TYPE] = "type";

	if (zone_is_slave(conf(), zone)) {
		data[KNOT_CTL_IDX_DATA] = "slave";
	} else {
		data[KNOT_CTL_IDX_DATA] = "master";
Daniel Salzman's avatar
Daniel Salzman committed
159 160
	}

161 162 163 164 165 166 167
	int ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &data);
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Zone serial.
	data[KNOT_CTL_IDX_TYPE] = "serial";
Daniel Salzman's avatar
Daniel Salzman committed
168

Daniel Salzman's avatar
Daniel Salzman committed
169
	char buff[128];
170
	if (zone->contents != NULL) {
171 172 173 174 175
		knot_rdataset_t *soa = node_rdataset(zone->contents->apex,
		                                     KNOT_RRTYPE_SOA);
		ret = snprintf(buff, sizeof(buff), "%u", knot_soa_serial(soa));
	} else {
		ret = snprintf(buff, sizeof(buff), "none");
Daniel Salzman's avatar
Daniel Salzman committed
176
	}
177 178 179 180 181 182 183 184 185 186 187 188 189
	if (ret < 0 || ret >= sizeof(buff)) {
		return KNOT_ESPACE;
	}

	data[KNOT_CTL_IDX_DATA] = buff;

	ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_EXTRA, &data);
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Next event.
	data[KNOT_CTL_IDX_TYPE] = "next-event";
Daniel Salzman's avatar
Daniel Salzman committed
190 191

	zone_event_type_t next_type = ZONE_EVENT_INVALID;
192
	time_t next_time = zone_events_get_next(zone, &next_type);
Daniel Salzman's avatar
Daniel Salzman committed
193
	if (next_type != ZONE_EVENT_INVALID) {
194 195
		const char *next_name = zone_events_get_name(next_type);

Daniel Salzman's avatar
Daniel Salzman committed
196 197
		next_time = next_time - time(NULL);
		if (next_time < 0) {
198 199 200 201 202 203 204 205 206 207
			ret = snprintf(buff, sizeof(buff), "%s pending",
			               next_name);
		} else {
			ret = snprintf(buff, sizeof(buff), "%s in %lldh%lldm%llds",
			               next_name,
			               (long long)(next_time / 3600),
			               (long long)(next_time % 3600) / 60,
			               (long long)(next_time % 60));
		}
		if (ret < 0 || ret >= sizeof(buff)) {
Daniel Salzman's avatar
Daniel Salzman committed
208 209
			return KNOT_ESPACE;
		}
210 211

		data[KNOT_CTL_IDX_DATA] = buff;
Daniel Salzman's avatar
Daniel Salzman committed
212
	} else {
213 214 215 216 217 218
		data[KNOT_CTL_IDX_DATA] = "idle";
	}

	ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_EXTRA, &data);
	if (ret != KNOT_EOK) {
		return ret;
Daniel Salzman's avatar
Daniel Salzman committed
219 220
	}

221 222
	// DNSSEC re-signing time.
	data[KNOT_CTL_IDX_TYPE] = "auto-dnssec";
Daniel Salzman's avatar
Daniel Salzman committed
223 224

	conf_val_t val = conf_zone_get(conf(), C_DNSSEC_SIGNING, zone->name);
225 226 227 228 229 230 231 232 233 234 235 236 237 238
	if (conf_bool(&val)) {
		struct tm time_gm = { 0 };
		time_t refresh_at = zone_events_get_time(zone, ZONE_EVENT_DNSSEC);
		gmtime_r(&refresh_at, &time_gm);

		size_t written = strftime(buff, sizeof(buff), KNOT_LOG_TIME_FORMAT,
		                          &time_gm);
		if (written == 0) {
			return KNOT_ESPACE;
		}

		data[KNOT_CTL_IDX_DATA] = buff;
	} else {
		data[KNOT_CTL_IDX_DATA] = "disabled";
Daniel Salzman's avatar
Daniel Salzman committed
239 240
	}

241 242 243 244 245 246 247 248 249
	ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_EXTRA, &data);
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Zone transaction.
	data[KNOT_CTL_IDX_TYPE] = "transaction";
	data[KNOT_CTL_IDX_DATA] = (zone->control_update != NULL) ? "open" : "none";

250
	return knot_ctl_send(args->ctl, KNOT_CTL_TYPE_EXTRA, &data);
Daniel Salzman's avatar
Daniel Salzman committed
251 252
}

253
static int zone_reload(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
254
{
255
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
256

257
	if (zone_expired(zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
258 259 260
		return KNOT_ENOTSUP;
	}

261
	zone_events_schedule_user(zone, ZONE_EVENT_LOAD);
Daniel Salzman's avatar
Daniel Salzman committed
262 263 264 265

	return KNOT_EOK;
}

266
static int zone_refresh(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
267
{
268
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
269

270
	if (!zone_is_slave(conf(), zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
271 272 273
		return KNOT_ENOTSUP;
	}

274
	zone_events_schedule_user(zone, ZONE_EVENT_REFRESH);
Daniel Salzman's avatar
Daniel Salzman committed
275 276 277 278

	return KNOT_EOK;
}

279
static int zone_retransfer(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
280
{
281
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
282

283
	if (!zone_is_slave(conf(), zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
284 285 286 287
		return KNOT_ENOTSUP;
	}

	zone->flags |= ZONE_FORCE_AXFR;
288
	zone_events_schedule_user(zone, ZONE_EVENT_REFRESH);
Daniel Salzman's avatar
Daniel Salzman committed
289 290 291 292

	return KNOT_EOK;
}

293
static int zone_flush(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
294
{
295
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
296

297 298 299 300
	if (ctl_has_flag(args->data[KNOT_CTL_IDX_FLAGS], CTL_FLAG_FORCE)) {
		zone->flags |= ZONE_FORCE_FLUSH;
	}

301
	zone_events_schedule_user(zone, ZONE_EVENT_FLUSH);
Daniel Salzman's avatar
Daniel Salzman committed
302 303 304 305

	return KNOT_EOK;
}

306
static int zone_sign(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
307
{
308
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
309 310 311 312 313 314 315

	conf_val_t val = conf_zone_get(conf(), C_DNSSEC_SIGNING, zone->name);
	if (!conf_bool(&val)) {
		return KNOT_ENOTSUP;
	}

	zone->flags |= ZONE_FORCE_RESIGN;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	zone_events_schedule_user(zone, ZONE_EVENT_DNSSEC);

	return KNOT_EOK;
}

static int zone_freeze(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

	zone_events_schedule_now(zone, ZONE_EVENT_UFREEZE);

	return KNOT_EOK;
}

static int zone_thaw(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

	zone_events_schedule_now(zone, ZONE_EVENT_UTHAW);
Daniel Salzman's avatar
Daniel Salzman committed
335 336 337 338

	return KNOT_EOK;
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352
static int zone_txn_begin(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

	if (zone->control_update != NULL) {
		return KNOT_TXN_EEXISTS;
	}

	zone->control_update = malloc(sizeof(zone_update_t));
	if (zone->control_update == NULL) {
		return KNOT_ENOMEM;
	}

	zone_update_flags_t type = (zone->contents == NULL) ? UPDATE_FULL : UPDATE_INCREMENTAL;
353
	int ret = zone_update_init(zone->control_update, zone, type | UPDATE_SIGN | UPDATE_STRICT);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	if (ret != KNOT_EOK) {
		free(zone->control_update);
		zone->control_update = NULL;
		return ret;
	}

	return KNOT_EOK;
}

static int zone_txn_commit(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

	if (zone->control_update == NULL) {
		return KNOT_TXN_ENOTEXISTS;
	}

	int ret = zone_update_commit(conf(), zone->control_update);
	if (ret != KNOT_EOK) {
373 374 375 376 377
		/* Invalidate the transaction if aborted. */
		if (zone->control_update->zone == NULL) {
			free(zone->control_update);
			zone->control_update = NULL;
		}
378 379 380 381 382 383 384
		return ret;
	}

	zone_update_clear(zone->control_update);
	free(zone->control_update);
	zone->control_update = NULL;

385 386 387
	/* Sync zonefile immediately if configured. */
	conf_val_t val = conf_zone_get(conf(), C_ZONEFILE_SYNC, zone->name);
	if (conf_int(&val) == 0) {
388
		zone_events_schedule_now(zone, ZONE_EVENT_FLUSH);
389 390
	}

391
	zone_events_schedule_now(zone, ZONE_EVENT_NOTIFY);
392

393 394 395 396 397 398 399 400 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 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 534 535 536 537 538 539 540 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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
	return KNOT_EOK;
}

static int zone_txn_abort(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

	if (zone->control_update == NULL) {
		return KNOT_TXN_ENOTEXISTS;
	}

	zone_update_clear(zone->control_update);
	free(zone->control_update);
	zone->control_update = NULL;

	return KNOT_EOK;
}

typedef struct {
	ctl_args_t *args;
	int type_filter; // -1: no specific type, [0, 2^16]: specific type.
	knot_dump_style_t style;
	knot_ctl_data_t data;
	char zone[KNOT_DNAME_TXT_MAXLEN + 1];
	char owner[KNOT_DNAME_TXT_MAXLEN + 1];
	char ttl[16];
	char type[32];
	char rdata[2 * 65536];
} send_ctx_t;

static send_ctx_t *create_send_ctx(const knot_dname_t *zone_name, ctl_args_t *args)
{
	send_ctx_t *ctx = mm_alloc(&args->mm, sizeof(*ctx));
	if (ctx == NULL) {
		return NULL;
	}
	memset(ctx, 0, sizeof(*ctx));

	ctx->args = args;

	// Set the dump style.
	ctx->style.show_ttl = true;
	ctx->style.human_tmstamp = true;

	// Set the output data buffers.
	ctx->data[KNOT_CTL_IDX_ZONE]  = ctx->zone;
	ctx->data[KNOT_CTL_IDX_OWNER] = ctx->owner;
	ctx->data[KNOT_CTL_IDX_TTL]   = ctx->ttl;
	ctx->data[KNOT_CTL_IDX_TYPE]  = ctx->type;
	ctx->data[KNOT_CTL_IDX_DATA]  = ctx->rdata;

	// Set the ZONE.
	if (knot_dname_to_str(ctx->zone, zone_name, sizeof(ctx->zone)) == NULL) {
		mm_free(&args->mm, ctx);
		return NULL;
	}

	// Set the TYPE filter.
	if (args->data[KNOT_CTL_IDX_TYPE] != NULL) {
		uint16_t type;
		if (knot_rrtype_from_string(args->data[KNOT_CTL_IDX_TYPE], &type) != 0) {
			mm_free(&args->mm, ctx);
			return NULL;
		}
		ctx->type_filter = type;
	} else {
		ctx->type_filter = -1;
	}

	return ctx;
}

static int send_rrset(knot_rrset_t *rrset, send_ctx_t *ctx)
{
	int ret = snprintf(ctx->ttl, sizeof(ctx->ttl), "%u", knot_rrset_ttl(rrset));
	if (ret <= 0 || ret >= sizeof(ctx->ttl)) {
		return KNOT_ESPACE;
	}

	if (knot_rrtype_to_string(rrset->type, ctx->type, sizeof(ctx->type)) < 0) {
		return KNOT_ESPACE;
	}

	for (size_t i = 0; i < rrset->rrs.rr_count; ++i) {
		ret = knot_rrset_txt_dump_data(rrset, i, ctx->rdata,
		                               sizeof(ctx->rdata), &ctx->style);
		if (ret < 0) {
			return ret;
		}

		ret = knot_ctl_send(ctx->args->ctl, KNOT_CTL_TYPE_DATA, &ctx->data);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

static int send_node(zone_node_t *node, void *ctx_void)
{
	send_ctx_t *ctx = ctx_void;
	if (knot_dname_to_str(ctx->owner, node->owner, sizeof(ctx->owner)) == NULL) {
		return KNOT_EINVAL;
	}

	for (size_t i = 0; i < node->rrset_count; ++i) {
		knot_rrset_t rrset = node_rrset_at(node, i);

		// Check for requested TYPE.
		if (ctx->type_filter != -1 && rrset.type != ctx->type_filter) {
			continue;
		}

		int ret = send_rrset(&rrset, ctx);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin,
                     ctl_args_t *args)
{
	const char *owner = args->data[KNOT_CTL_IDX_OWNER];
	assert(owner != NULL);

	bool fqdn = false;
	int prefix_len = 0;

	size_t owner_len = strlen(owner);
	if (owner_len > 0 && (owner_len != 1 || owner[0] != '@')) {
		// Check if the owner is FQDN.
		if (owner[owner_len - 1] == '.') {
			fqdn = true;
		}

		knot_dname_t *dname = knot_dname_from_str(out, owner, out_len);
		if (dname == NULL) {
			return KNOT_EINVAL;
		}

		int ret = knot_dname_to_lower(dname);
		if (ret != KNOT_EOK) {
			return ret;
		}

		prefix_len = knot_dname_size(out);
		if (prefix_len <= 0) {
			return KNOT_EINVAL;
		}

		// Ignore trailing dot.
		prefix_len--;
	}

	// Append the origin.
	if (!fqdn) {
		int origin_len = knot_dname_size(origin);
		if (origin_len <= 0 || origin_len > out_len - prefix_len) {
			return KNOT_EINVAL;
		}
		memcpy(out + prefix_len, origin, origin_len);
	}

	return KNOT_EOK;
}

static int zone_read(zone_t *zone, ctl_args_t *args)
{
	send_ctx_t *ctx = create_send_ctx(zone->name, args);
	if (ctx == NULL) {
		return KNOT_ENOMEM;
	}

	int ret = KNOT_EOK;

	if (args->data[KNOT_CTL_IDX_OWNER] != NULL) {
		uint8_t owner[KNOT_DNAME_MAXLEN];

		ret = get_owner(owner, sizeof(owner), zone->name, args);
		if (ret != KNOT_EOK) {
			goto zone_read_failed;
		}

		const zone_node_t *node = zone_contents_find_node(zone->contents, owner);
		if (node == NULL) {
			ret = KNOT_ENONODE;
			goto zone_read_failed;
		}

		ret = send_node((zone_node_t *)node, ctx);
	} else if (zone->contents != NULL) {
588
		ret = zone_contents_apply(zone->contents, send_node, ctx);
589 590 591 592 593 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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	}

zone_read_failed:
	mm_free(&args->mm, ctx);

	return ret;
}

static int zone_flag_txn_get(zone_t *zone, ctl_args_t *args, const char *flag)
{
	if (zone->control_update == NULL) {
		return KNOT_TXN_ENOTEXISTS;
	}

	send_ctx_t *ctx = create_send_ctx(zone->name, args);
	if (ctx == NULL) {
		return KNOT_ENOMEM;
	}
	ctx->data[KNOT_CTL_IDX_FLAGS] = flag;

	int ret = KNOT_EOK;

	if (args->data[KNOT_CTL_IDX_OWNER] != NULL) {
		uint8_t owner[KNOT_DNAME_MAXLEN];

		ret = get_owner(owner, sizeof(owner), zone->name, args);
		if (ret != KNOT_EOK) {
			goto zone_txn_get_failed;
		}

		const zone_node_t *node = zone_update_get_node(zone->control_update, owner);
		if (node == NULL) {
			ret = KNOT_ENONODE;
			goto zone_txn_get_failed;
		}

		ret = send_node((zone_node_t *)node, ctx);
	} else {
		zone_update_iter_t it;
		ret = zone_update_iter(&it, zone->control_update);
		if (ret != KNOT_EOK) {
			goto zone_txn_get_failed;
		}

		const zone_node_t *iter_node = zone_update_iter_val(&it);
		while (iter_node != NULL) {
			ret = send_node((zone_node_t *)iter_node, ctx);
			if (ret != KNOT_EOK) {
				zone_update_iter_finish(&it);
				goto zone_txn_get_failed;
			}

			ret = zone_update_iter_next(&it);
			if (ret != KNOT_EOK) {
				zone_update_iter_finish(&it);
				goto zone_txn_get_failed;
			}

			iter_node = zone_update_iter_val(&it);
		}
		zone_update_iter_finish(&it);
	}

zone_txn_get_failed:
	mm_free(&args->mm, ctx);

	return ret;
}

static int zone_txn_get(zone_t *zone, ctl_args_t *args)
{
	return zone_flag_txn_get(zone, args, NULL);
}

static int send_changeset_part(changeset_t *ch, send_ctx_t *ctx, bool from)
{
	ctx->data[KNOT_CTL_IDX_FLAGS] = from ? CTL_FLAG_REM : CTL_FLAG_ADD;

	// Send SOA only if explicitly changed.
	if (ch->soa_to != NULL) {
		knot_rrset_t *soa = from ? ch->soa_from : ch->soa_to;
		assert(soa);

		char *owner = knot_dname_to_str(ctx->owner, soa->owner, sizeof(ctx->owner));
		if (owner == NULL) {
			return KNOT_EINVAL;
		}

		int ret = send_rrset(soa, ctx);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	// Send other records.
	changeset_iter_t it;
685
	int ret = from ? changeset_iter_rem(&it, ch) : changeset_iter_add(&it, ch);
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
	if (ret != KNOT_EOK) {
		return ret;
	}

	knot_rrset_t rrset = changeset_iter_next(&it);
	while (!knot_rrset_empty(&rrset)) {
		char *owner = knot_dname_to_str(ctx->owner, rrset.owner, sizeof(ctx->owner));
		if (owner == NULL) {
			changeset_iter_clear(&it);
			return KNOT_EINVAL;
		}

		ret = send_rrset(&rrset, ctx);
		if (ret != KNOT_EOK) {
			changeset_iter_clear(&it);
			return ret;
		}

		rrset = changeset_iter_next(&it);
	}
	changeset_iter_clear(&it);

	return KNOT_EOK;
}

static int send_changeset(changeset_t *ch, send_ctx_t *ctx)
{
	// First send 'from' changeset part.
	int ret = send_changeset_part(ch, ctx, true);
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Second send 'to' changeset part.
	ret = send_changeset_part(ch, ctx, false);
	if (ret != KNOT_EOK) {
		return ret;
	}

	return KNOT_EOK;
}

static int zone_txn_diff(zone_t *zone, ctl_args_t *args)
{
	if (zone->control_update == NULL) {
		return KNOT_TXN_ENOTEXISTS;
	}

	// FULL update has no changeset to print, do a 'get' instead.
	if (zone->control_update->flags & UPDATE_FULL) {
		return zone_flag_txn_get(zone, args, CTL_FLAG_ADD);
	}

	send_ctx_t *ctx = create_send_ctx(zone->name, args);
	if (ctx == NULL) {
		return KNOT_ENOMEM;
	}

	int ret = send_changeset(&zone->control_update->change, ctx);
	mm_free(&args->mm, ctx);
	return ret;
}

static int get_ttl(zone_t *zone, ctl_args_t *args, uint32_t *ttl)
{
	uint8_t owner[KNOT_DNAME_MAXLEN];

	int ret = get_owner(owner, sizeof(owner), zone->name, args);
	if (ret != KNOT_EOK) {
		return ret;
	}

	const zone_node_t *node = zone_update_get_node(zone->control_update, owner);
	if (node == NULL) {
		return KNOT_ETTL;
	}

	uint16_t type;
	if (knot_rrtype_from_string(args->data[KNOT_CTL_IDX_TYPE], &type) != 0) {
		return KNOT_EINVAL;
	}

	knot_rdataset_t *rdataset = node_rdataset(node, type);
	if (rdataset == NULL) {
		return KNOT_ETTL;
	}

	*ttl = knot_rdataset_ttl(rdataset);

	return KNOT_EOK;
}

static int create_rrset(knot_rrset_t **rrset, zone_t *zone, ctl_args_t *args,
                        bool need_ttl)
{
	char origin_buff[KNOT_DNAME_TXT_MAXLEN + 1];
	char *origin = knot_dname_to_str(origin_buff, zone->name, sizeof(origin_buff));
	if (origin == NULL) {
		return KNOT_EINVAL;
	}

	const char *owner = args->data[KNOT_CTL_IDX_OWNER];
	const char *type  = args->data[KNOT_CTL_IDX_TYPE];
	const char *data  = args->data[KNOT_CTL_IDX_DATA];
	const char *ttl   = need_ttl ? args->data[KNOT_CTL_IDX_TTL] : NULL;

	// Prepare a buffer for a reconstructed record.
	const size_t buff_len = sizeof(((send_ctx_t *)0)->owner) +
	                        sizeof(((send_ctx_t *)0)->ttl) +
	                        sizeof(((send_ctx_t *)0)->type) +
	                        sizeof(((send_ctx_t *)0)->rdata);
	char *buff = mm_alloc(&args->mm, buff_len);
	if (buff == NULL) {
		return KNOT_ENOMEM;
	}

	uint32_t default_ttl = 0;
	if (ttl == NULL) {
		int ret = get_ttl(zone, args, &default_ttl);
		if (need_ttl && ret != KNOT_EOK) {
			mm_free(&args->mm, buff);
			return ret;
		}
	}

	// Reconstruct the record.
	int ret = snprintf(buff, buff_len, "%s %s %s %s\n",
	                   (owner != NULL ? owner : ""),
	                   (ttl   != NULL ? ttl   : ""),
	                   (type  != NULL ? type  : ""),
	                   (data  != NULL ? data  : ""));
	if (ret <= 0 || ret >= buff_len) {
		mm_free(&args->mm, buff);
		return KNOT_ESPACE;
	}
	size_t rdata_len = ret;

	// Initialize RR parser.
	zs_scanner_t *scanner = mm_alloc(&args->mm, sizeof(*scanner));
	if (scanner == NULL) {
		ret = KNOT_ENOMEM;
		goto parser_failed;
	}

	// Parse the record.
	if (zs_init(scanner, origin, KNOT_CLASS_IN, default_ttl) != 0 ||
	    zs_set_input_string(scanner, buff, rdata_len) != 0 ||
	    zs_parse_record(scanner) != 0 ||
	    scanner->state != ZS_STATE_DATA) {
		ret = KNOT_EPARSEFAIL;
		goto parser_failed;
	}

	// Create output rrset.
	*rrset = knot_rrset_new(scanner->r_owner, scanner->r_type,
	                        scanner->r_class, NULL);
	if (*rrset == NULL) {
		ret = KNOT_ENOMEM;
		goto parser_failed;
	}

	ret = knot_rrset_add_rdata(*rrset, scanner->r_data, scanner->r_data_length,
	                           scanner->r_ttl, NULL);
parser_failed:
	zs_deinit(scanner);
	mm_free(&args->mm, scanner);
	mm_free(&args->mm, buff);

	return ret;
}

static int zone_txn_set(zone_t *zone, ctl_args_t *args)
{
	if (zone->control_update == NULL) {
		return KNOT_TXN_ENOTEXISTS;
	}

	if (args->data[KNOT_CTL_IDX_OWNER] == NULL ||
	    args->data[KNOT_CTL_IDX_TYPE]  == NULL) {
		return KNOT_EINVAL;
	}

	knot_rrset_t *rrset;
	int ret = create_rrset(&rrset, zone, args, true);
	if (ret != KNOT_EOK) {
		return ret;
	}

	ret = zone_update_add(zone->control_update, rrset);
	knot_rrset_free(&rrset, NULL);

	// Silently update TTL.
	if (ret == KNOT_ETTL) {
		ret = KNOT_EOK;
	}

	return ret;
}

static int zone_txn_unset(zone_t *zone, ctl_args_t *args)
{
	if (zone->control_update == NULL) {
		return KNOT_TXN_ENOTEXISTS;
	}

	if (args->data[KNOT_CTL_IDX_OWNER] == NULL) {
		return KNOT_EINVAL;
	}

	// Remove specific record.
	if (args->data[KNOT_CTL_IDX_DATA] != NULL) {
		if (args->data[KNOT_CTL_IDX_TYPE] == NULL) {
			return KNOT_EINVAL;
		}

		knot_rrset_t *rrset;
		int ret = create_rrset(&rrset, zone, args, false);
		if (ret != KNOT_EOK) {
			return ret;
		}

		ret = zone_update_remove(zone->control_update, rrset);
		knot_rrset_free(&rrset, NULL);
		return ret;
	} else {
		uint8_t owner[KNOT_DNAME_MAXLEN];

		int ret = get_owner(owner, sizeof(owner), zone->name, args);
		if (ret != KNOT_EOK) {
			return ret;
		}

		// Remove whole rrset.
		if (args->data[KNOT_CTL_IDX_TYPE] != NULL) {
			uint16_t type;
			if (knot_rrtype_from_string(args->data[KNOT_CTL_IDX_TYPE],
			                            &type) != 0) {
				return KNOT_EINVAL;
			}

			return zone_update_remove_rrset(zone->control_update, owner, type);
		// Remove whole node.
		} else {
			return zone_update_remove_node(zone->control_update, owner);
		}
	}

}

935 936 937 938 939 940 941
static int zone_purge(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

	// Abort possible editing transaction.
	(void)zone_txn_abort(zone, args);

942 943 944
	// Purge the zone timers.
	memset(&zone->timers, 0, sizeof(zone->timers));

945 946 947 948 949 950 951 952 953
	// Expire the zone.
	event_expire(conf(), zone);

	// Purge the zone file.
	char *zonefile = conf_zonefile(conf(), zone->name);
	(void)unlink(zonefile);
	free(zonefile);

	// Purge the zone journal.
954 955 956
	if (journal_open(zone->journal, zone->journal_db, zone->name) == KNOT_EOK) {
		(void)scrape_journal(zone->journal);
	}
957 958 959 960

	return KNOT_EOK;
}

961 962 963 964 965 966 967 968
static int send_stats_ctr(mod_ctr_t *ctr, ctl_args_t *args, knot_ctl_data_t *data)
{
	char index[128];
	char value[32];

	if (ctr->count == 1) {
		int ret = snprintf(value, sizeof(value), "%"PRIu64, ctr->counter);
		if (ret <= 0 || ret >= sizeof(value)) {
969
			return KNOT_ESPACE;
970
		}
971

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
		(*data)[KNOT_CTL_IDX_ID] = NULL;
		(*data)[KNOT_CTL_IDX_DATA] = value;

		ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, data);
		if (ret != KNOT_EOK) {
			return ret;
		}
	} else {
		bool force = ctl_has_flag(args->data[KNOT_CTL_IDX_FLAGS],
		                          CTL_FLAG_FORCE);

		for (uint32_t i = 0; i < ctr->count; i++) {
			// Skip empty counters.
			if (ctr->counters[i] == 0 && !force) {
				continue;
			}

			int ret;
			if (ctr->idx_to_str) {
				char *str = ctr->idx_to_str(i, ctr->count);
				if (str == NULL) {
					continue;
				}
				ret = snprintf(index, sizeof(index), "%s", str);
				free(str);
			} else {
				ret = snprintf(index, sizeof(index), "%u", i);
			}
			if (ret <= 0 || ret >= sizeof(index)) {
1001
				return KNOT_ESPACE;
1002 1003
			}

1004
			ret = snprintf(value, sizeof(value), "%"PRIu64,
1005 1006
			               ctr->counters[i]);
			if (ret <= 0 || ret >= sizeof(value)) {
1007
				return KNOT_ESPACE;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
			}

			(*data)[KNOT_CTL_IDX_ID] = index;
			(*data)[KNOT_CTL_IDX_DATA] = value;

			knot_ctl_type_t type = (i == 0) ? KNOT_CTL_TYPE_DATA :
			                                  KNOT_CTL_TYPE_EXTRA;
			ret = knot_ctl_send(args->ctl, type, data);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
	}
1021 1022 1023 1024

	return KNOT_EOK;
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
static int modules_stats(list_t *query_modules, ctl_args_t *args, knot_dname_t *zone)
{
	if (query_modules == NULL) {
		return KNOT_EOK;
	}

	const char *section = args->data[KNOT_CTL_IDX_SECTION];
	const char *item = args->data[KNOT_CTL_IDX_ITEM];

	char name[KNOT_DNAME_TXT_MAXLEN + 1] = { 0 };
	knot_ctl_data_t data = { 0 };

	bool section_found = (section == NULL) ? true : false;
	bool item_found = (item == NULL) ? true : false;

	struct query_module *mod = NULL;
	WALK_LIST(mod, *query_modules) {
		// Skip modules without statistics.
		if (mod->stats_count == 0) {
			continue;
		}

		// Check for specific module.
		if (section != NULL) {
			if (section_found) {
				break;
			} else if (strcasecmp(mod->id->name + 1, section) == 0) {
				section_found = true;
			} else {
				continue;
			}
		}

		data[KNOT_CTL_IDX_SECTION] = mod->id->name + 1;

		for (int i = 0; i < mod->stats_count; i++) {
			mod_ctr_t *ctr = mod->stats + i;

			// Skip empty counter.
			if (ctr->name == NULL) {
				continue;
			}

			// Check for specific counter.
			if (item != NULL) {
				if (item_found) {
					break;
				} else if (strcasecmp(ctr->name, item) == 0) {
					item_found = true;
				} else {
					continue;
				}
			}

			// Prepare zone name if not already prepared.
			if (zone != NULL && name[0] == '\0') {
				if (knot_dname_to_str(name, zone, sizeof(name)) == NULL) {
					return KNOT_EINVAL;
				}
				data[KNOT_CTL_IDX_ZONE] = name;
			}

			data[KNOT_CTL_IDX_ITEM] = ctr->name;

			// Send the counters.
			int ret = send_stats_ctr(ctr, args, &data);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
	}

	return (section_found && item_found) ? KNOT_EOK : KNOT_ENOENT;
}

static int zone_stats(zone_t *zone, ctl_args_t *args)
{
	return modules_stats(&zone->query_modules, args, zone->name);
}

1105
static int ctl_zone(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1106
{
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	switch (cmd) {
	case CTL_ZONE_STATUS:
		return zones_apply(args, zone_status);
	case CTL_ZONE_RELOAD:
		return zones_apply(args, zone_reload);
	case CTL_ZONE_REFRESH:
		return zones_apply(args, zone_refresh);
	case CTL_ZONE_RETRANSFER:
		return zones_apply(args, zone_retransfer);
	case CTL_ZONE_FLUSH:
		return zones_apply(args, zone_flush);
	case CTL_ZONE_SIGN:
		return zones_apply(args, zone_sign);
1120 1121 1122 1123
	case CTL_ZONE_FREEZE:
		return zones_apply(args, zone_freeze);
	case CTL_ZONE_THAW:
		return zones_apply(args, zone_thaw);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	case CTL_ZONE_READ:
		return zones_apply(args, zone_read);
	case CTL_ZONE_BEGIN:
		return zones_apply(args, zone_txn_begin);
	case CTL_ZONE_COMMIT:
		return zones_apply(args, zone_txn_commit);
	case CTL_ZONE_ABORT:
		return zones_apply(args, zone_txn_abort);
	case CTL_ZONE_DIFF:
		return zones_apply(args, zone_txn_diff);
	case CTL_ZONE_GET:
		return zones_apply(args, zone_txn_get);
	case CTL_ZONE_SET:
		return zones_apply(args, zone_txn_set);
	case CTL_ZONE_UNSET:
		return zones_apply(args, zone_txn_unset);
1140 1141
	case CTL_ZONE_PURGE:
		return zones_apply(args, zone_purge);
1142 1143
	case CTL_ZONE_STATS:
		return zones_apply(args, zone_stats);
1144 1145
	default:
		assert(0);
1146
		return KNOT_EINVAL;
1147
	}
Daniel Salzman's avatar
Daniel Salzman committed
1148 1149
}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
static int server_status(ctl_args_t *args)
{
	const char *type = args->data[KNOT_CTL_IDX_TYPE];

	if (type == NULL || strlen(type) == 0) {
		return KNOT_EOK;
	}

	char buff[2048] = "";

	int ret;
	if (strcasecmp(type, "version") == 0) {
		ret = snprintf(buff, sizeof(buff), "Version: %s", PACKAGE_VERSION);
	} else if (strcasecmp(type, "workers") == 0) {
		ret = snprintf(buff, sizeof(buff), "UDP workers: %zu, TCP workers %zu, "
		               "background workers: %zu", conf_udp_threads(conf()),
		               conf_tcp_threads(conf()), conf_bg_threads(conf()));
	} else if (strcasecmp(type, "configure") == 0) {
		ret = snprintf(buff, sizeof(buff), "%s", CONFIGURE_SUMMARY);
	} else {
		return KNOT_EINVAL;
	}
	if (ret <= 0 || ret >= sizeof(buff)) {
		return KNOT_ESPACE;
	}

	args->data[KNOT_CTL_IDX_DATA] = buff;

	return knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &args->data);
}

1181
static int ctl_server(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1182
{
1183 1184 1185 1186
	int ret = KNOT_EOK;

	switch (cmd) {
	case CTL_STATUS:
1187 1188 1189
		ret = server_status(args);
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
1190
		}
1191 1192 1193 1194 1195
		break;
	case CTL_STOP:
		ret = KNOT_CTL_ESTOP;
		break;
	case CTL_RELOAD:
1196
		ret = server_reload(args->server);
1197 1198 1199 1200 1201 1202
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
		}
		break;
	default:
		assert(0);
1203
		ret = KNOT_EINVAL;
1204
	}
Daniel Salzman's avatar
Daniel Salzman committed
1205

1206
	return ret;
Daniel Salzman's avatar
Daniel Salzman committed
1207 1208
}

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
static int ctl_stats(ctl_args_t *args, ctl_cmd_t cmd)
{
	const char *section = args->data[KNOT_CTL_IDX_SECTION];
	const char *item = args->data[KNOT_CTL_IDX_ITEM];

	bool found = (section == NULL) ? true : false;

	// Process server metrics.
	if (section == NULL || strcasecmp(section, "server") == 0) {
		char value[32];
		knot_ctl_data_t data = {
			[KNOT_CTL_IDX_SECTION] = "server",
			[KNOT_CTL_IDX_DATA] = value
		};

		for (const stats_item_t *i = server_stats; i->name != NULL; i++) {
			if (item != NULL) {
				if (found) {
					break;
				} else if (strcmp(i->name, item) == 0) {
					found = true;
				} else {
					continue;
				}
			} else {
				found = true;
			}

			data[KNOT_CTL_IDX_ITEM] = i->name;
			int ret = snprintf(value, sizeof(value), "%"PRIu64,
			                   i->val(args->server));
			if (ret <= 0 || ret >= sizeof(value)) {
1241
				ret = KNOT_ESPACE;
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
				send_error(args, knot_strerror(ret));
				return ret;
			}

			ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &data);
			if (ret != KNOT_EOK) {
				send_error(args, knot_strerror(ret));
				return ret;
			}
		}
	}

	// Process modules metrics.
	if (section == NULL || strncasecmp(section, "mod-", strlen("mod-")) == 0) {
		int ret = modules_stats(&conf()->query_modules, args, NULL);
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
			return ret;
		}

		found = true;
	}

	if (!found) {
		send_error(args, knot_strerror(KNOT_EINVAL));
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
}

1273
static int send_block_data(conf_io_t *io, knot_ctl_data_t *data)
Daniel Salzman's avatar
Daniel Salzman committed
1274
{
1275
	knot_ctl_t *ctl = (knot_ctl_t *)io->misc;
Daniel Salzman's avatar
Daniel Salzman committed
1276

1277
	const yp_item_t *item = (io->key1 != NULL) ? io->key1 : io->key0;
Daniel Salzman's avatar
Daniel Salzman committed
1278
	assert(item != NULL);
Daniel Salzman's avatar
Daniel Salzman committed
1279

1280
	char buff[YP_MAX_TXT_DATA_LEN + 1] = "\0";
Daniel Salzman's avatar
Daniel Salzman committed
1281

1282
	(*data)[KNOT_CTL_IDX_DATA] = buff;
Daniel Salzman's avatar
Daniel Salzman committed
1283

1284 1285 1286 1287 1288 1289 1290
	// Format explicit binary data value.
	if (io->data.bin != NULL) {
		size_t buff_len = sizeof(buff);
		int ret = yp_item_to_txt(item, io->data.bin, io->data.bin_len, buff,
		                         &buff_len, YP_SNOQUOTE);
		if (ret != KNOT_EOK) {
			return ret;
Daniel Salzman's avatar
Daniel Salzman committed
1291
		}
1292
		return knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, data);
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	// Format all multivalued item data if no specified index.
	} else if ((item->flags & YP_FMULTI) && io->data.index == 0) {
		size_t values = conf_val_count(io->data.val);
		for (size_t i = 0; i < values; i++) {
			conf_val(io->data.val);
			size_t buff_len = sizeof(buff);
			int ret = yp_item_to_txt(item, io->data.val->data,
			                         io->data.val->len, buff,&buff_len,
			                         YP_SNOQUOTE);
			if (ret != KNOT_EOK) {
				return ret;
			}
Daniel Salzman's avatar
Daniel Salzman committed
1305

1306 1307
			knot_ctl_type_t type = (i == 0) ? KNOT_CTL_TYPE_DATA :
			                                  KNOT_CTL_TYPE_EXTRA;
1308
			ret = knot_ctl_send(ctl, type, data);
1309 1310 1311
			if (ret != KNOT_EOK) {
				return ret;
			}
Daniel Salzman's avatar
Daniel Salzman committed
1312

1313
			conf_val_next(io->data.val);
Daniel Salzman's avatar
Daniel Salzman committed
1314
		}
1315 1316 1317 1318 1319 1320 1321 1322 1323
		return KNOT_EOK;
	// Format singlevalued item data or a specified one from multivalued.
	} else {
		conf_val(io->data.val);
		size_t buff_len = sizeof(buff);
		int ret = yp_item_to_txt(item, io->data.val->data, io->data.val->len,
		                         buff, &buff_len, YP_SNOQUOTE);
		if (ret != KNOT_EOK) {
			return ret;
Daniel Salzman's avatar
Daniel Salzman committed
1324
		}
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
		return knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, data);
	}
}

static int send_block(conf_io_t *io)
{
	knot_ctl_t *ctl = (knot_ctl_t *)io->misc;

	// Get possible error message.
	const char *err = io->error.str;
	if (err == NULL && io->error.code != KNOT_EOK) {
		err = knot_strerror(io->error.code);
	}

	knot_ctl_data_t data = {
		[KNOT_CTL_IDX_ERROR] = err,
	};

	if (io->key0 != NULL) {
		data[KNOT_CTL_IDX_SECTION] = io->key0->name + 1;
	}
	if (io->key1 != NULL) {
		data[KNOT_CTL_IDX_ITEM] = io->key1->name + 1;
	}

	// Get the item prefix.
	switch (io->type) {
	case NEW: data[KNOT_CTL_IDX_FLAGS] = CTL_FLAG_ADD; break;
	case OLD: data[KNOT_CTL_IDX_FLAGS] = CTL_FLAG_REM; break;
	default: break;
	}

	char id[KNOT_DNAME_TXT_MAXLEN + 1] = "\0";

	// Get the textual item id.
	if (io->id_len > 0 && io->key0 != NULL) {
		size_t id_len = sizeof(id);
		int ret = yp_item_to_txt(io->key0->var.g.id, io->id, io->id_len,
		                         id, &id_len, YP_SNOQUOTE);
		if (ret != KNOT_EOK) {
			return ret;
		}
		if (io->id_as_data) {
			data[KNOT_CTL_IDX_DATA] = id;
		} else {
			data[KNOT_CTL_IDX_ID] = id;
		}
	}

	if (io->data.val == NULL && io->data.bin == NULL) {
1375
		return knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, &data);
1376 1377
	} else {
		return send_block_data(io, &data);
Daniel Salzman's avatar
Daniel Salzman committed
1378 1379 1380
	}
}

1381
static int ctl_conf_txn(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1382 1383
{
	conf_io_t io = {
1384 1385
		.fcn = send_block,
		.misc = args->ctl
Daniel Salzman's avatar
Daniel Salzman committed
1386 1387
	};

1388
	int ret = KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
1389

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
	switch (cmd) {
	case CTL_CONF_BEGIN:
		ret = conf_io_begin(false);
		break;
	case CTL_CONF_ABORT:
		conf_io_abort(false);
		ret = KNOT_EOK;
		break;
	case CTL_CONF_COMMIT:
		// First check the database.
		ret = conf_io_check(&io);
		if (ret != KNOT_EOK) {
			// No transaction abort!
1403
			break;
1404
		}
Daniel Salzman's avatar
Daniel Salzman committed
1405

1406 1407 1408 1409 1410
		ret = conf_io_commit(false);
		if (ret != KNOT_EOK) {
			conf_io_abort(false);
			break;
		}
Daniel Salzman's avatar
Daniel Salzman committed
1411

1412
		ret = server_reload(args->server);
1413 1414 1415
		break;
	default:
		assert(0);
1416
		ret = KNOT_EINVAL;
Daniel Salzman's avatar
Daniel Salzman committed
1417 1418 1419
	}

	if (ret != KNOT_EOK) {
1420
		send_error(args, knot_strerror(ret));
Daniel Salzman's avatar
Daniel Salzman committed
1421 1422 1423 1424 1425
	}

	return ret;
}

1426
static int ctl_conf_read(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1427 1428
{
	conf_io_t io = {
1429 1430
		.fcn = send_block,
		.misc = args->ctl
Daniel Salzman's avatar
Daniel Salzman committed
1431 1432
	};

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	int ret = KNOT_EOK;

	while (true) {
		const char *key0 = args->data[KNOT_CTL_IDX_SECTION];
		const char *key1 = args->data[KNOT_CTL_IDX_ITEM];
		const char *id   = args->data[KNOT_CTL_IDX_ID];

		switch (cmd) {
		case CTL_CONF_LIST:
			ret = conf_io_list(key0, &io);
			break;
		case CTL_CONF_READ:
			ret = conf_io_get(key0, key1, id, true, &io);
			break;
		case CTL_CONF_DIFF:
			ret = conf_io_diff(key0, key1, id, &io);
			break;
		case CTL_CONF_GET:
			ret = conf_io_get(key0, key1, id, false, &io);
			break;
		default:
			assert(0);
1455
			ret = KNOT_EINVAL;
1456 1457 1458 1459 1460
		}
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
			break;
		}
Daniel Salzman's avatar
Daniel Salzman committed
1461

1462 1463 1464 1465 1466
		// Get next data unit.
		ret = knot_ctl_receive(args->ctl, &args->type, &args->data);
		if (ret != KNOT_EOK || args->type != KNOT_CTL_TYPE_DATA) {
			break;
		}
1467
		ctl_log_data(&args->data);
1468
	}
Daniel Salzman's avatar
Daniel Salzman committed
1469 1470 1471 1472

	return ret;
}

1473
static int ctl_conf_modify(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1474 1475
{
	// Start child transaction.
1476
	int ret = conf_io_begin(true);
Daniel Salzman's avatar
Daniel Salzman committed
1477
	if (ret != KNOT_EOK) {
1478
		send_error(args, knot_strerror(ret));
Daniel Salzman's avatar
Daniel Salzman committed
1479 1480 1481
		return ret;
	}

1482 1483 1484 1485 1486 1487 1488 1489
	while (true) {
		const char *key0 = args->data[KNOT_CTL_IDX_SECTION];
		const char *key1 = args->data[KNOT_CTL_IDX_ITEM];
		const char *id   = args->data[KNOT_CTL_IDX_ID];
		const char *data = args->data[KNOT_CTL_IDX_DATA];

		switch (cmd) {
		case CTL_CONF_SET:
1490
			ret = conf_io_set(key0, key1, id, data);
1491 1492 1493 1494 1495 1496
			break;
		case CTL_CONF_UNSET:
			ret = conf_io_unset(key0, key1, id, data);
			break;
		default:
			assert(0);
1497
			ret = KNOT_EINVAL;
1498 1499 1500 1501
		}
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
			break;
Daniel Salzman's avatar
Daniel Salzman committed
1502 1503
		}

1504 1505 1506 1507 1508
		// Get next data unit.
		ret = knot_ctl_receive(args->ctl, &args->type, &args->data);
		if (ret != KNOT_EOK || args->type != KNOT_CTL_TYPE_DATA) {
			break;
		}
1509
		ctl_log_data(&args->data);
1510
	}
Daniel Salzman's avatar
Daniel Salzman committed
1511 1512 1513

	// Finish child transaction.
	if (ret == KNOT_EOK) {
1514 1515 1516 1517
		ret = conf_io_commit(true);
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
		}
Daniel Salzman's avatar
Daniel Salzman committed
1518
	} else {
1519
		conf_io_abort(true);
Daniel Salzman's avatar
Daniel Salzman committed
1520 1521
	}

1522 1523
	return ret;
}
Daniel Salzman's avatar
Daniel Salzman committed
1524

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
typedef struct {
	const char *name;
	int (*fcn)(ctl_args_t *, ctl_cmd_t);
} desc_t;

static const desc_t cmd_table[] = {
	[CTL_NONE]            = { "" },

	[CTL_STATUS]          = { "status",          ctl_server },
	[CTL_STOP]            = { "stop",            ctl_server },
	[CTL_RELOAD]          = { "reload",          ctl_server },
1536
	[CTL_STATS]           = { "stats",           ctl_stats },
1537 1538 1539 1540 1541 1542 1543

	[CTL_ZONE_STATUS]     = { "zone-status",     ctl_zone },
	[CTL_ZONE_RELOAD]     = { "zone-reload",     ctl_zone },
	[CTL_ZONE_REFRESH]    = { "zone-refresh",    ctl_zone },
	[CTL_ZONE_RETRANSFER] = { "zone-retransfer", ctl_zone },
	[CTL_ZONE_FLUSH]      = { "zone-flush",      ctl_zone },
	[CTL_ZONE_SIGN]       = { "zone-sign",       ctl_zone },
1544 1545
	[CTL_ZONE_FREEZE]     = { "zone-freeze",     ctl_zone },
	[CTL_ZONE_THAW]       = { "zone-thaw",       ctl_zone },
1546

1547 1548 1549 1550 1551 1552 1553 1554
	[CTL_ZONE_READ]       = { "zone-read",       ctl_zone },
	[CTL_ZONE_BEGIN]      = { "zone-begin",      ctl_zone },
	[CTL_ZONE_COMMIT]     = { "zone-commit",     ctl_zone },
	[CTL_ZONE_ABORT]      = { "zone-abort",      ctl_zone },
	[CTL_ZONE_DIFF]       = { "zone-diff",       ctl_zone },
	[CTL_ZONE_GET]        = { "zone-get",        ctl_zone },
	[CTL_ZONE_SET]        = { "zone-set",        ctl_zone },
	[CTL_ZONE_UNSET]      = { "zone-unset",      ctl_zone },
1555
	[CTL_ZONE_PURGE]      = { "zone-purge",      ctl_zone },
1556
	[CTL_ZONE_STATS]      = { "zone-stats",	     ctl_zone },
1557

1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
	[CTL_CONF_LIST]       = { "conf-list",       ctl_conf_read },
	[CTL_CONF_READ]       = { "conf-read",       ctl_conf_read },
	[CTL_CONF_BEGIN]      = { "conf-begin",      ctl_conf_txn },
	[CTL_CONF_COMMIT]     = { "conf-commit",     ctl_conf_txn },
	[CTL_CONF_ABORT]      = { "conf-abort",      ctl_conf_txn },
	[CTL_CONF_DIFF]       = { "conf-diff",       ctl_conf_read },
	[CTL_CONF_GET]        = { "conf-get",        ctl_conf_read },
	[CTL_CONF_SET]        = { "conf-set",        ctl_conf_modify },
	[CTL_CONF_UNSET]      = { "conf-unset",      ctl_conf_modify },
};
Daniel Salzman's avatar
Daniel Salzman committed
1568

Daniel Salzman's avatar
Daniel Salzman committed
1569 1570 1571
#define MAX_CTL_CODE (sizeof(cmd_table) / sizeof(desc_t) - 1)

const char *ctl_cmd_to_str(ctl_cmd_t cmd)
1572
{
Daniel Salzman's avatar
Daniel Salzman committed
1573
	if (cmd <= CTL_NONE || cmd > MAX_CTL_CODE) {
1574
		return NULL;
Daniel Salzman's avatar
Daniel Salzman committed
1575 1576
	}

1577 1578 1579 1580 1581 1582 1583
	return cmd_table[cmd].name;
}

ctl_cmd_t ctl_str_to_cmd(const char *cmd_str)
{
	if (cmd_str == NULL) {
		return CTL_NONE;
Daniel Salzman's avatar
Daniel Salzman committed
1584 1585
	}

Daniel Salzman's avatar
Daniel Salzman committed
1586
	for (ctl_cmd_t cmd = CTL_NONE + 1; cmd <= MAX_CTL_CODE; cmd++) {
1587 1588
		if (strcmp(cmd_str, cmd_table[cmd].name) == 0) {
			return cmd;
Daniel Salzman's avatar
Daniel Salzman committed
1589 1590 1591
		}
	}

1592 1593
	return CTL_NONE;
}
Daniel Salzman's avatar
Daniel Salzman committed
1594

1595 1596 1597 1598
int ctl_exec(ctl_cmd_t cmd, ctl_args_t *args)
{
	if (args == NULL) {
		return KNOT_EINVAL;
Daniel Salzman's avatar
Daniel Salzman committed
1599 1600
	}

1601 1602
	return cmd_table[cmd].fcn(args, cmd);
}
Daniel Salzman's avatar
Daniel Salzman committed
1603 1604 1605 1606 1607 1608 1609 1610 1611

bool ctl_has_flag(const char *flags, const char *flag)
{
	if (flags == NULL || flag == NULL) {
		return false;
	}

	return strstr(flags, flag) != NULL;
}