commands.c 38.5 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"
Daniel Salzman's avatar
Daniel Salzman committed
25
#include "knot/events/handlers.h"
26
#include "knot/nameserver/query_module.h"
27
#include "knot/updates/zone-update.h"
28
#include "knot/zone/timers.h"
Daniel Salzman's avatar
Daniel Salzman committed
29
#include "libknot/libknot.h"
30
#include "libknot/yparser/yptrafo.h"
Daniel Salzman's avatar
Daniel Salzman committed
31
#include "contrib/macros.h"
32
#include "contrib/mempattern.h"
Daniel Salzman's avatar
Daniel Salzman committed
33
#include "contrib/string.h"
34
#include "zscanner/scanner.h"
35
#include "contrib/strtonum.h"
Daniel Salzman's avatar
Daniel Salzman committed
36

37 38 39 40 41 42 43 44 45 46 47
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];

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

52
	if (zone != NULL) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66
		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 : ""));
67 68 69
	}
}

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

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

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

83
static int get_zone(ctl_args_t *args, zone_t **zone)
Daniel Salzman's avatar
Daniel Salzman committed
84
{
85 86 87 88
	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
89

90 91 92
	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
93 94
	}

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

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

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

108
static int zones_apply(ctl_args_t *args, int (*fcn)(zone_t *, ctl_args_t *))
Daniel Salzman's avatar
Daniel Salzman committed
109
{
110 111 112 113 114 115 116
	// 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
117

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

130 131 132 133
		// 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
134
		}
135
		ctl_log_data(&args->data);
Daniel Salzman's avatar
Daniel Salzman committed
136 137
	}

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

141 142 143 144 145 146 147
typedef enum {
	ZONE_STATUS_INVALID = -1,
	ZONE_STATUS_NONE = 0,
	ZONE_STATUS_TYPE = 1,
	ZONE_STATUS_SERIAL,
	ZONE_STATUS_TRANSACTION,
	ZONE_STATUS_EVENT_TIMERS,
148 149
	ZONE_STATUS_EVENT_STATUS,
	ZONE_STATUS_FREEZE,
150 151 152 153 154 155 156
} zone_status_param ;

static zone_status_param zone_status_param_check(const char *param)
{
	if (param == NULL) {
		return ZONE_STATUS_NONE;
	}
157
	if (strcmp(param, "+type") == 0) {
158 159
		return ZONE_STATUS_TYPE;
	}
160
	if (strcmp(param, "+serial") == 0) {
161 162
		return ZONE_STATUS_SERIAL;
	}
163
	if (strcmp(param, "+transaction") == 0) {
164 165
		return ZONE_STATUS_TRANSACTION;
	}
166
	if (strcmp(param, "+event-timers") == 0) {
167 168
		return ZONE_STATUS_EVENT_TIMERS;
	}
169
	if (strcmp(param, "+freeze") == 0) {
170 171
		return ZONE_STATUS_FREEZE;
	}
172 173 174
	return ZONE_STATUS_INVALID;
}

175
static int zone_status(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
176
{
177 178 179 180 181
	// 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
182

183
	knot_ctl_data_t data = {
184
	    [KNOT_CTL_IDX_ZONE] = name
185
	};
Daniel Salzman's avatar
Daniel Salzman committed
186

187 188 189 190 191 192 193 194
	zone_status_param param = zone_status_param_check(args->data[KNOT_CTL_IDX_OWNER]);
	if (param == ZONE_STATUS_INVALID) {
		data[KNOT_CTL_IDX_ERROR] = "Invalid parameters.";
		int ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &data);
		if (ret != KNOT_EOK) {
			return ret;
		}
		return KNOT_EINVAL;
195 196
	}

197
	int ret;
Daniel Salzman's avatar
Daniel Salzman committed
198
	char buff[128];
199 200 201 202
	knot_ctl_type_t type = KNOT_CTL_TYPE_DATA;
	// Zone type.
	if (param == ZONE_STATUS_TYPE || param == ZONE_STATUS_NONE) {
		data[KNOT_CTL_IDX_TYPE] = "type";
203

204 205 206 207 208
		if (zone_is_slave(conf(), zone)) {
			data[KNOT_CTL_IDX_DATA] = "slave";
		} else {
			data[KNOT_CTL_IDX_DATA] = "master";
		}
209

210 211 212 213 214 215
		ret = knot_ctl_send(args->ctl, type, &data);
		if (ret != KNOT_EOK) {
			return ret;
		} else {
			type = KNOT_CTL_TYPE_EXTRA;
		}
216
	}
217 218 219
	// Zone serial.
	if (param == ZONE_STATUS_SERIAL || param == ZONE_STATUS_NONE) {
		data[KNOT_CTL_IDX_TYPE] = "serial";
220

221 222 223 224
		if (zone->contents != NULL) {
			knot_rdataset_t *soa = node_rdataset(zone->contents->apex,
							     KNOT_RRTYPE_SOA);
			ret = snprintf(buff, sizeof(buff), "%u", knot_soa_serial(soa));
225
		} else {
226
			ret = snprintf(buff, sizeof(buff), "none");
227 228
		}
		if (ret < 0 || ret >= sizeof(buff)) {
Daniel Salzman's avatar
Daniel Salzman committed
229 230
			return KNOT_ESPACE;
		}
231 232 233

		data[KNOT_CTL_IDX_DATA] = buff;

234 235 236 237 238 239
		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
240
	}
241 242 243 244
	// Zone transaction.
	if (param == ZONE_STATUS_TRANSACTION || param == ZONE_STATUS_NONE) {
		data[KNOT_CTL_IDX_TYPE] = "transaction";
		data[KNOT_CTL_IDX_DATA] = (zone->control_update != NULL) ? "open" : "none";
245 246 247
		ret = knot_ctl_send(args->ctl, type, &data);
		if (ret != KNOT_EOK) {
			return ret;
248 249
		} else {
			type = KNOT_CTL_TYPE_EXTRA;
250
		}
Daniel Salzman's avatar
Daniel Salzman committed
251
	}
252 253 254
	// frozen
	bool ufrozen = zone->events.ufrozen;
	if (param == ZONE_STATUS_FREEZE || param == ZONE_STATUS_NONE) {
255 256
		time_t freeze = zone_events_get_time(zone, ZONE_EVENT_UFREEZE) - time(NULL);
		time_t thaw = zone_events_get_time(zone, ZONE_EVENT_UTHAW) - time(NULL);
257 258
		data[KNOT_CTL_IDX_TYPE] = "freeze";
		if (ufrozen) {
259 260 261 262 263
			if (thaw < 0) {
				data[KNOT_CTL_IDX_DATA] = "yes";
			} else {
				data[KNOT_CTL_IDX_DATA] = "thaw queued";
			}
264
		} else {
265 266 267 268 269
			if (freeze < 0) {
				data[KNOT_CTL_IDX_DATA] = "no";
			} else {
				data[KNOT_CTL_IDX_DATA] = "freeze queued";
			}
270
		}
271
		ret = knot_ctl_send(args->ctl, type, &data);
272 273 274 275 276 277 278 279
		if (ret != KNOT_EOK) {
			return ret;
		}
	}
	// list modules
	if (param == ZONE_STATUS_EVENT_TIMERS || param == ZONE_STATUS_NONE) {
		time_t ev_time;
		for (zone_event_type_t i = 0; i < ZONE_EVENT_COUNT; i++) {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
			// Events not worth showing or used elsewhere
			if (!(i == ZONE_EVENT_LOAD || i == ZONE_EVENT_UFREEZE
			      || i == ZONE_EVENT_UTHAW)) {
				// Skip events affected by freeze
				if (!ufrozen || !ufreeze_applies(i)) {
					data[KNOT_CTL_IDX_TYPE] = zone_events_get_name(i);
					ev_time = zone_events_get_time(zone, i);
					ev_time = ev_time - time(NULL);
					if (ev_time < 0) {
						ret = snprintf(buff, sizeof(buff), "not scheduled");
					} else {
						ret = snprintf(buff, sizeof(buff), "in %lldh%lldm%llds",
							       (long long)(ev_time / 3600),
							       (long long)(ev_time % 3600) / 60,
							       (long long)(ev_time % 60));
					}
					if (ret < 0 || ret >= sizeof(buff)) {
						return KNOT_ESPACE;
					}
					data[KNOT_CTL_IDX_DATA] = buff;

Filip Široký's avatar
Filip Široký committed
301
					ret = knot_ctl_send(args->ctl, type,
302 303 304 305 306
							    &data);
					if (ret != KNOT_EOK) {
						return ret;
					}
					type = KNOT_CTL_TYPE_EXTRA;
307
				}
308 309 310
			}
		}
	}
311

312
	return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
313 314
}

315
static int zone_reload(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
316
{
317
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
318

319
	if (zone_expired(zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
320 321 322
		return KNOT_ENOTSUP;
	}

323
	zone_events_schedule_user(zone, ZONE_EVENT_LOAD);
Daniel Salzman's avatar
Daniel Salzman committed
324 325 326 327

	return KNOT_EOK;
}

328
static int zone_refresh(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
329
{
330
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
331

332
	if (!zone_is_slave(conf(), zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
333 334 335
		return KNOT_ENOTSUP;
	}

336
	zone_events_schedule_user(zone, ZONE_EVENT_REFRESH);
Daniel Salzman's avatar
Daniel Salzman committed
337 338 339 340

	return KNOT_EOK;
}

341
static int zone_retransfer(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
342
{
343
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
344

345
	if (!zone_is_slave(conf(), zone)) {
Daniel Salzman's avatar
Daniel Salzman committed
346 347 348 349
		return KNOT_ENOTSUP;
	}

	zone->flags |= ZONE_FORCE_AXFR;
350
	zone_events_schedule_user(zone, ZONE_EVENT_REFRESH);
Daniel Salzman's avatar
Daniel Salzman committed
351 352 353 354

	return KNOT_EOK;
}

355
static int zone_flush(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
356
{
357
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
358

359 360 361 362
	if (ctl_has_flag(args->data[KNOT_CTL_IDX_FLAGS], CTL_FLAG_FORCE)) {
		zone->flags |= ZONE_FORCE_FLUSH;
	}

363
	zone_events_schedule_user(zone, ZONE_EVENT_FLUSH);
Daniel Salzman's avatar
Daniel Salzman committed
364 365 366 367

	return KNOT_EOK;
}

368
static int zone_sign(zone_t *zone, ctl_args_t *args)
Daniel Salzman's avatar
Daniel Salzman committed
369
{
370
	UNUSED(args);
Daniel Salzman's avatar
Daniel Salzman committed
371 372 373 374 375 376 377

	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;
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	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
397 398 399 400

	return KNOT_EOK;
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414
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;
415
	int ret = zone_update_init(zone->control_update, zone, type | UPDATE_SIGN | UPDATE_STRICT);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	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) {
435 436 437 438 439
		/* Invalidate the transaction if aborted. */
		if (zone->control_update->zone == NULL) {
			free(zone->control_update);
			zone->control_update = NULL;
		}
440 441 442 443 444 445 446
		return ret;
	}

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

447 448 449
	/* Sync zonefile immediately if configured. */
	conf_val_t val = conf_zone_get(conf(), C_ZONEFILE_SYNC, zone->name);
	if (conf_int(&val) == 0) {
450
		zone_events_schedule_now(zone, ZONE_EVENT_FLUSH);
451 452
	}

453
	zone_events_schedule_now(zone, ZONE_EVENT_NOTIFY);
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 647 648 649
	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) {
650
		ret = zone_contents_apply(zone->contents, send_node, ctx);
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 744 745 746
	}

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;
747
	int ret = from ? changeset_iter_rem(&it, ch) : changeset_iter_add(&it, ch);
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 994 995 996
	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
997 998 999 1000 1001 1002 1003
static int zone_purge(zone_t *zone, ctl_args_t *args)
{
	UNUSED(args);

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

1004 1005 1006
	// Purge the zone timers.
	memset(&zone->timers, 0, sizeof(zone->timers));

Daniel Salzman's avatar
Daniel Salzman committed
1007 1008 1009 1010 1011 1012 1013 1014 1015
	// 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.
1016 1017 1018
	if (journal_open(zone->journal, zone->journal_db, zone->name) == KNOT_EOK) {
		(void)scrape_journal(zone->journal);
	}
Daniel Salzman's avatar
Daniel Salzman committed
1019 1020 1021 1022

	return KNOT_EOK;
}

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
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)) {
			return ret;
		}
Daniel Salzman's avatar
Daniel Salzman committed
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
		(*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)) {
				return ret;
			}

			ret = snprintf(value, sizeof(value),  "%"PRIu64,
			               ctr->counters[i]);
			if (ret <= 0 || ret >= sizeof(value)) {
				return ret;
			}

			(*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
1083 1084 1085 1086

	return KNOT_EOK;
}

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 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
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);
}

1167
static int ctl_zone(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1168
{
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	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);
1182 1183 1184 1185
	case CTL_ZONE_FREEZE:
		return zones_apply(args, zone_freeze);
	case CTL_ZONE_THAW:
		return zones_apply(args, zone_thaw);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	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
1202 1203
	case CTL_ZONE_PURGE:
		return zones_apply(args, zone_purge);
1204 1205
	case CTL_ZONE_STATS:
		return zones_apply(args, zone_stats);
1206 1207
	default:
		assert(0);
1208
		return KNOT_EINVAL;
1209
	}
Daniel Salzman's avatar
Daniel Salzman committed
1210 1211
}

1212
static int ctl_server(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1213
{
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
	int ret = KNOT_EOK;

	switch (cmd) {
	case CTL_STATUS:
		ret = KNOT_EOK;
		break;
	case CTL_STOP:
		ret = KNOT_CTL_ESTOP;
		break;
	case CTL_RELOAD:
1224
		ret = server_reload(args->server);
1225 1226 1227 1228 1229 1230
		if (ret != KNOT_EOK) {
			send_error(args, knot_strerror(ret));
		}
		break;
	default:
		assert(0);
1231
		ret = KNOT_EINVAL;
1232
	}
Daniel Salzman's avatar
Daniel Salzman committed
1233

1234
	return ret;
Daniel Salzman's avatar
Daniel Salzman committed
1235 1236
}

1237 1238 1239 1240 1241 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 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
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)) {
				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;
}

Daniel Salzman's avatar
Daniel Salzman committed
1300
static int send_block_data(conf_io_t *io, knot_ctl_data_t *data)
Daniel Salzman's avatar
Daniel Salzman committed
1301
{
1302
	knot_ctl_t *ctl = (knot_ctl_t *)io->misc;
Daniel Salzman's avatar
Daniel Salzman committed
1303

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

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

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

1311 1312 1313 1314 1315 1316 1317
	// 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
1318
		}
Daniel Salzman's avatar
Daniel Salzman committed
1319
		return knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, data);
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	// 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
1332

1333 1334
			knot_ctl_type_t type = (i == 0) ? KNOT_CTL_TYPE_DATA :
			                                  KNOT_CTL_TYPE_EXTRA;
Daniel Salzman's avatar
Daniel Salzman committed
1335
			ret = knot_ctl_send(ctl, type, data);
1336 1337 1338
			if (ret != KNOT_EOK) {
				return ret;
			}
Daniel Salzman's avatar
Daniel Salzman committed
1339

1340
			conf_val_next(io->data.val);
Daniel Salzman's avatar
Daniel Salzman committed
1341
		}
1342 1343 1344 1345 1346 1347 1348 1349 1350
		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
1351
		}
Daniel Salzman's avatar
Daniel Salzman committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
		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) {
1402
		return knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, &data);
Daniel Salzman's avatar
Daniel Salzman committed
1403 1404
	} else {
		return send_block_data(io, &data);
Daniel Salzman's avatar
Daniel Salzman committed
1405 1406 1407
	}
}

1408
static int ctl_conf_txn(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1409 1410
{
	conf_io_t io = {
1411 1412
		.fcn = send_block,
		.misc = args->ctl
Daniel Salzman's avatar
Daniel Salzman committed
1413 1414
	};

1415
	int ret = KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
1416

1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	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) {
1429
			// Error response is already sent by the check function.
1430
			// No transaction abort!
1431
			return KNOT_EOK;
1432
		}
Daniel Salzman's avatar
Daniel Salzman committed
1433

1434 1435 1436 1437 1438
		ret = conf_io_commit(false);
		if (ret != KNOT_EOK) {
			conf_io_abort(false);
			break;
		}
Daniel Salzman's avatar
Daniel Salzman committed
1439

1440
		ret = server_reload(args->server);
1441 1442 1443
		break;
	default:
		assert(0);
1444
		ret = KNOT_EINVAL;
Daniel Salzman's avatar
Daniel Salzman committed
1445 1446 1447
	}

	if (ret != KNOT_EOK) {
1448
		send_error(args, knot_strerror(ret));
Daniel Salzman's avatar
Daniel Salzman committed
1449 1450 1451 1452 1453
	}

	return ret;
}

1454
static int ctl_conf_read(ctl_args_t *args, ctl_cmd_t cmd)
Daniel Salzman's avatar
Daniel Salzman committed
1455 1456
{
	conf_io_t io = {
1457 1458
		.fcn = send_block,
		.misc = args->ctl
Daniel Salzman's avatar
Daniel Salzman committed
1459 1460
	};

1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	int ret = KNOT_EOK;

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