commands.c 40.2 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>
Daniel Salzman's avatar
Daniel Salzman committed
18
#include <unistd.h>
19
#include <sys/time.h>
Daniel Salzman's avatar
Daniel Salzman committed
20 21

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

Daniel Salzman's avatar
Daniel Salzman committed
40 41 42 43 44
#define MATCH_OR_FILTER(args, code) ((args)->data[KNOT_CTL_IDX_FILTER] == NULL || \
                                     strchr((args)->data[KNOT_CTL_IDX_FILTER], (code)) != NULL)

#define MATCH_AND_FILTER(args, code) ((args)->data[KNOT_CTL_IDX_FILTER] != NULL && \
                                      strchr((args)->data[KNOT_CTL_IDX_FILTER], (code)) != NULL)
45

46 47 48 49 50 51 52 53 54 55 56
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];

57 58 59 60
	if (section == NULL) {
		return;
	}

61
	if (zone != NULL) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75
		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 : ""));
76 77 78
	}
}

79
static void send_error(ctl_args_t *args, const char *msg)
Daniel Salzman's avatar
Daniel Salzman committed
80
{
81 82
	knot_ctl_data_t data;
	memcpy(&data, args->data, sizeof(data));
Daniel Salzman's avatar
Daniel Salzman committed
83

84
	data[KNOT_CTL_IDX_ERROR] = msg;
Daniel Salzman's avatar
Daniel Salzman committed
85

86 87
	int ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &data);
	if (ret != KNOT_EOK) {
88
		log_ctl_debug("control, failed to send error (%s)", knot_strerror(ret));
89
	}
Daniel Salzman's avatar
Daniel Salzman committed
90 91
}

92
static int get_zone(ctl_args_t *args, zone_t **zone)
Daniel Salzman's avatar
Daniel Salzman committed
93
{
94 95 96 97
	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
98

99 100 101
	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
102 103
	}

104
	int ret = knot_dname_to_lower(dname);
Daniel Salzman's avatar
Daniel Salzman committed
105
	if (ret != KNOT_EOK) {
106 107
		return ret;
	}
Daniel Salzman's avatar
Daniel Salzman committed
108

109 110 111
	*zone = knot_zonedb_find(args->server->zone_db, dname);
	if (*zone == NULL) {
		return KNOT_ENOZONE;
Daniel Salzman's avatar
Daniel Salzman committed
112
	}
113 114

	return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
115 116
}

117
static int zones_apply(ctl_args_t *args, int (*fcn)(zone_t *, ctl_args_t *))
Daniel Salzman's avatar
Daniel Salzman committed
118
{
119 120 121 122 123 124 125
	// 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
126

127 128
	while (true) {
		zone_t *zone;
Daniel Salzman's avatar
Daniel Salzman committed
129
		ret = get_zone(args, &zone);
130 131 132 133
		if (ret == KNOT_EOK) {
			ret = fcn(zone, args);
		}
		if (ret != KNOT_EOK) {
134 135
			log_ctl_zone_str_error(args->data[KNOT_CTL_IDX_ZONE],
			                       "control, error (%s)", knot_strerror(ret));
136 137
			send_error(args, knot_strerror(ret));
		}
Daniel Salzman's avatar
Daniel Salzman committed
138

139 140 141 142
		// 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
143
		}
144
		ctl_log_data(&args->data);
Daniel Salzman's avatar
Daniel Salzman committed
145 146
	}

147
	return ret;
Daniel Salzman's avatar
Daniel Salzman committed
148 149
}

150
static int zone_status(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
151
{
152 153 154 155
	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
156

157
	knot_ctl_data_t data = {
158
		[KNOT_CTL_IDX_ZONE] = name
159
	};
Daniel Salzman's avatar
Daniel Salzman committed
160

161
	int ret;
Daniel Salzman's avatar
Daniel Salzman committed
162
	char buff[128];
163
	knot_ctl_type_t type = KNOT_CTL_TYPE_DATA;
164

Daniel Salzman's avatar
Daniel Salzman committed
165
	if (MATCH_OR_FILTER(args, CTL_FILTER_STATUS_ROLE)) {
166
		data[KNOT_CTL_IDX_TYPE] = "role";
167

168 169 170 171 172
		if (zone_is_slave(conf(), zone)) {
			data[KNOT_CTL_IDX_DATA] = "slave";
		} else {
			data[KNOT_CTL_IDX_DATA] = "master";
		}
173

174 175 176 177 178 179
		ret = knot_ctl_send(args->ctl, type, &data);
		if (ret != KNOT_EOK) {
			return ret;
		} else {
			type = KNOT_CTL_TYPE_EXTRA;
		}
180
	}
181

Daniel Salzman's avatar
Daniel Salzman committed
182
	if (MATCH_OR_FILTER(args, CTL_FILTER_STATUS_SERIAL)) {
183
		data[KNOT_CTL_IDX_TYPE] = "serial";
184

185 186
		if (zone->contents != NULL) {
			knot_rdataset_t *soa = node_rdataset(zone->contents->apex,
187
			                                     KNOT_RRTYPE_SOA);
188
			ret = snprintf(buff, sizeof(buff), "%u", knot_soa_serial(soa));
189
		} else {
190
			ret = snprintf(buff, sizeof(buff), "none");
191 192
		}
		if (ret < 0 || ret >= sizeof(buff)) {
Daniel Salzman's avatar
Daniel Salzman committed
193 194
			return KNOT_ESPACE;
		}
195 196 197

		data[KNOT_CTL_IDX_DATA] = buff;

198 199 200 201 202 203
		ret = knot_ctl_send(args->ctl, type, &data);
		if (ret != KNOT_EOK) {
			return ret;
		} else {
			type = KNOT_CTL_TYPE_EXTRA;
		}
Daniel Salzman's avatar
Daniel Salzman committed
204
	}
205

Daniel Salzman's avatar
Daniel Salzman committed
206
	if (MATCH_OR_FILTER(args, CTL_FILTER_STATUS_TRANSACTION)) {
207 208
		data[KNOT_CTL_IDX_TYPE] = "transaction";
		data[KNOT_CTL_IDX_DATA] = (zone->control_update != NULL) ? "open" : "none";
209 210 211
		ret = knot_ctl_send(args->ctl, type, &data);
		if (ret != KNOT_EOK) {
			return ret;
212 213
		} else {
			type = KNOT_CTL_TYPE_EXTRA;
214
		}
Daniel Salzman's avatar
Daniel Salzman committed
215
	}
216

217
	bool ufrozen = zone->events.ufrozen;
Daniel Salzman's avatar
Daniel Salzman committed
218
	if (MATCH_OR_FILTER(args, CTL_FILTER_STATUS_FREEZE)) {
219 220
		data[KNOT_CTL_IDX_TYPE] = "freeze";
		if (ufrozen) {
221
			if (zone_events_get_time(zone, ZONE_EVENT_UTHAW) < time(NULL)) {
222 223
				data[KNOT_CTL_IDX_DATA] = "yes";
			} else {
224
				data[KNOT_CTL_IDX_DATA] = "thawing";
225
			}
226
		} else {
227
			if (zone_events_get_time(zone, ZONE_EVENT_UFREEZE) < time(NULL)) {
228 229
				data[KNOT_CTL_IDX_DATA] = "no";
			} else {
230 231
				data[KNOT_CTL_IDX_DATA] = "freezing";

232
			}
233
		}
234
		ret = knot_ctl_send(args->ctl, type, &data);
235 236 237 238
		if (ret != KNOT_EOK) {
			return ret;
		}
	}
239

Daniel Salzman's avatar
Daniel Salzman committed
240
	if (MATCH_OR_FILTER(args, CTL_FILTER_STATUS_EVENTS)) {
241
		for (zone_event_type_t i = 0; i < ZONE_EVENT_COUNT; i++) {
242 243 244 245 246 247 248 249 250 251 252 253 254
			// Events not worth showing or used elsewhere.
			if (i == ZONE_EVENT_LOAD || i == ZONE_EVENT_UFREEZE ||
			    i == ZONE_EVENT_UTHAW) {
				continue;
			}

			// Skip events affected by freeze.
			if (ufrozen && ufreeze_applies(i)) {
				continue;
			}

			data[KNOT_CTL_IDX_TYPE] = zone_events_get_name(i);
			time_t ev_time = zone_events_get_time(zone, i);
255
			if (ev_time <= 0) {
256
				ret = snprintf(buff, sizeof(buff), "not scheduled");
257 258
			} else if (ev_time <= time(NULL)) {
				ret = snprintf(buff, sizeof(buff), "pending");
259
			} else {
260 261
				ret = knot_time_print(TIME_PRINT_HUMAN_MIXED,
				                      ev_time, buff, sizeof(buff));
262 263 264 265 266 267 268 269 270
			}
			if (ret < 0 || ret >= sizeof(buff)) {
				return KNOT_ESPACE;
			}
			data[KNOT_CTL_IDX_DATA] = buff;

			ret = knot_ctl_send(args->ctl, type, &data);
			if (ret != KNOT_EOK) {
				return ret;
271
			}
272
			type = KNOT_CTL_TYPE_EXTRA;
273
		}
274 275
	}

276
	return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
277 278
}

279
static int zone_reload(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_expired(zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
284 285 286
		return KNOT_ENOTSUP;
	}

287
	zone_events_schedule_user(zone, ZONE_EVENT_LOAD);
Daniel Salzman's avatar
Daniel Salzman committed
288 289 290 291

	return KNOT_EOK;
}

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

296
	if (!zone_is_slave(conf(), zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
297 298 299
		return KNOT_ENOTSUP;
	}

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

	return KNOT_EOK;
}

305
static int zone_retransfer(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
306
{
307
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
308

309
	if (!zone_is_slave(conf(), zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
310 311 312 313
		return KNOT_ENOTSUP;
	}

	zone->flags |= ZONE_FORCE_AXFR;
314
	zone_events_schedule_user(zone, ZONE_EVENT_REFRESH);
Daniel Salzman's avatar
Daniel Salzman committed
315 316 317 318

	return KNOT_EOK;
}

319
static int zone_flush(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
320
{
Daniel Salzman's avatar
Daniel Salzman committed
321
	if (MATCH_AND_FILTER(args, CTL_FILTER_FLUSH_OUTDIR)) {
322 323
		return zone_dump_to_dir(conf(), zone, args->data[KNOT_CTL_IDX_DATA]);
	}
Daniel Salzman's avatar
Daniel Salzman committed
324

325 326 327 328
	if (ctl_has_flag(args->data[KNOT_CTL_IDX_FLAGS], CTL_FLAG_FORCE)) {
		zone->flags |= ZONE_FORCE_FLUSH;
	}

329
	zone_events_schedule_user(zone, ZONE_EVENT_FLUSH);
Daniel Salzman's avatar
Daniel Salzman committed
330 331 332 333

	return KNOT_EOK;
}

334
static int zone_sign(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
335
{
336
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
337 338 339 340 341 342 343

	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;
344 345 346 347 348
	zone_events_schedule_user(zone, ZONE_EVENT_DNSSEC);

	return KNOT_EOK;
}

349
static int zone_ksk_sbm_confirm(zone_t *zone, ctl_args_t *args)
Libor Peltan's avatar
Libor Peltan committed
350
{
351 352
	UNUSED(args);

Libor Peltan's avatar
Libor Peltan committed
353 354
	kdnssec_ctx_t ctx = { 0 };

355
	int ret = kdnssec_ctx_init(conf(), &ctx, zone->name, NULL);
Libor Peltan's avatar
Libor Peltan committed
356 357 358 359
	if (ret != KNOT_EOK) {
		return ret;
	}

360
	ret = knot_dnssec_ksk_sbm_confirm(&ctx);
Libor Peltan's avatar
Libor Peltan committed
361
	kdnssec_ctx_deinit(&ctx);
362 363 364 365
	if (ret == KNOT_EOK) {
		// NOT zone_events_schedule_user(), intentionally!
		zone_events_schedule_now(zone, ZONE_EVENT_DNSSEC);
	}
Libor Peltan's avatar
Libor Peltan committed
366 367 368 369

	return ret;
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383
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
384 385 386 387

	return KNOT_EOK;
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401
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;
402
	int ret = zone_update_init(zone->control_update, zone, type | UPDATE_SIGN | UPDATE_STRICT);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	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;
	}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	// Sign update.
	conf_val_t val = conf_zone_get(conf(), C_DNSSEC_SIGNING, zone->name);
	bool dnssec_enable = (zone->control_update->flags & UPDATE_SIGN) && conf_bool(&val);
	if (dnssec_enable) {
		zone_sign_reschedule_t resch = { 0 };
		int ret = knot_dnssec_sign_update(zone->control_update, &resch);
		if (ret != KNOT_EOK) {
			zone_update_clear(zone->control_update);
			free(zone->control_update);
			zone->control_update = NULL;
			return ret;
		}
		log_dnssec_next(zone->name, (time_t)resch.next_sign);
		zone_events_schedule_at(zone, ZONE_EVENT_DNSSEC, (time_t)resch.next_sign);
	}

436 437
	int ret = zone_update_commit(conf(), zone->control_update);
	if (ret != KNOT_EOK) {
438 439 440 441 442
		/* Invalidate the transaction if aborted. */
		if (zone->control_update->zone == NULL) {
			free(zone->control_update);
			zone->control_update = NULL;
		}
443 444 445 446 447 448 449
		return ret;
	}

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

450
	zone_events_schedule_now(zone, ZONE_EVENT_NOTIFY);
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 588 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
	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) {
647
		ret = zone_contents_apply(zone->contents, send_node, ctx);
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 685 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
	}

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;
744
	int ret = from ? changeset_iter_rem(&it, ch) : changeset_iter_add(&it, ch);
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 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	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);
		}
	}

}

Daniel Salzman's avatar
Daniel Salzman committed
994 995 996
static int zone_purge(zone_t *zone, ctl_args_t *args)
{
	// Abort possible editing transaction.
Daniel Salzman's avatar
Daniel Salzman committed
997
	if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_EXPIRE)) {
998 999
		(void)zone_txn_abort(zone, args);
	}
Daniel Salzman's avatar
Daniel Salzman committed
1000

1001
	// Purge the zone timers.
Daniel Salzman's avatar
Daniel Salzman committed
1002
	if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_TIMERS)) {
1003 1004
		memset(&zone->timers, 0, sizeof(zone->timers));
	}
1005

Daniel Salzman's avatar
Daniel Salzman committed
1006
	// Expire the zone.
Daniel Salzman's avatar
Daniel Salzman committed
1007
	if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_EXPIRE)) {
1008 1009
		(void)event_expire(conf(), zone);
	}
Daniel Salzman's avatar
Daniel Salzman committed
1010 1011

	// Purge the zone file.
Daniel Salzman's avatar
Daniel Salzman committed
1012
	if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_ZONEFILE)) {
1013 1014 1015 1016
		char *zonefile = conf_zonefile(conf(), zone->name);
		(void)unlink(zonefile);
		free(zonefile);
	}
Daniel Salzman's avatar
Daniel Salzman committed
1017 1018

	// Purge the zone journal.
Daniel Salzman's avatar
Daniel Salzman committed
1019
	if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_JOURNAL)) {
1020 1021 1022
		if (journal_open(zone->journal, zone->journal_db, zone->name) == KNOT_EOK) {
			(void)journal_scrape(zone->journal);
		}
1023
	}
Daniel Salzman's avatar
Daniel Salzman committed
1024

1025
	// Purge KASP DB.
Daniel Salzman's avatar
Daniel Salzman committed
1026
	if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_KASPDB)) {
1027 1028 1029
		if (kasp_db_open(*kaspdb()) == KNOT_EOK) {
			(void)kasp_db_delete_all(*kaspdb(), zone->name);
		}
1030 1031
	}

Daniel Salzman's avatar
Daniel Salzman committed
1032 1033 1034
	return KNOT_EOK;
}

1035 1036 1037 1038 1039 1040 1041 1042
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)) {
1043
			return KNOT_ESPACE;
1044
		}
Daniel Salzman's avatar
Daniel Salzman committed
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
		(*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)) {
1075
				return KNOT_ESPACE;
1076 1077
			}

1078
			ret = snprintf(value, sizeof(value), "%"PRIu64,
1079 1080
			               ctr->counters[i]);
			if (ret <= 0 || ret >= sizeof(value)) {
1081
				return KNOT_ESPACE;
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
			}

			(*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;
			}
		}
	}
Daniel Salzman's avatar
Daniel Salzman committed
1095 1096 1097 1098

	return KNOT_EOK;
}

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
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;

1114
	knotd_mod_t *mod = NULL;
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 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
	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);
}

1179
static int ctl_zone(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1180
{
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
	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);
1194
	case CTL_ZONE_KSK_SBM:
1195
		return zones_apply(args, zone_ksk_sbm_confirm);
1196 1197 1198 1199
	case CTL_ZONE_FREEZE:
		return zones_apply(args, zone_freeze);
	case CTL_ZONE_THAW:
		return zones_apply(args, zone_thaw);
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	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);
Daniel Salzman's avatar
Daniel Salzman committed
1216 1217
	case CTL_ZONE_PURGE:
		return zones_apply(args, zone_purge);
1218 1219
	case CTL_ZONE_STATS:
		return zones_apply(args, zone_stats);
1220 1221
	default:
		assert(0);
1222
		return KNOT_EINVAL;
1223
	}
Daniel Salzman's avatar
Daniel Salzman committed
1224 1225
}

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
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);
}

1257
static int ctl_server(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1258
{
1259 1260 1261 1262
	int ret = KNOT_EOK;

	switch (cmd) {
	case CTL_STATUS:
1263 1264 1265
		ret = server_status(args);
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
1266
		}
1267 1268 1269 1270 1271
		break;
	case CTL_STOP:
		ret = KNOT_CTL_ESTOP;
		break;
	case CTL_RELOAD:
1272
		ret = server_reload(args->server);
1273 1274 1275 1276 1277 1278
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
		}
		break;
	default:
		assert(0);
1279
		ret = KNOT_EINVAL;
1280
	}
Daniel Salzman's avatar
Daniel Salzman committed
1281

1282
	return ret;
Daniel Salzman's avatar
Daniel Salzman committed
1283 1284
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
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)) {
1317
				ret = KNOT_ESPACE;
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
				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) {
1332
		int ret = modules_stats(conf()->query_modules, args, NULL);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		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;
}

Daniel Salzman's avatar
Daniel Salzman committed
1349
static int send_block_data(conf_io_t *io, knot_ctl_data_t *data)
Daniel Salzman's avatar
Daniel Salzman committed
1350
{
1351
	knot_ctl_t *ctl = (knot_ctl_t *)io->misc;
Daniel Salzman's avatar
Daniel Salzman committed
1352

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

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

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

1360 1361 1362 1363 1364 1365 1366
	// 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
1367
		}
Daniel Salzman's avatar
Daniel Salzman committed
1368
		return knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, data);
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	// 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
1381