confdb.c 20.1 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

    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/>.
*/

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

#include "knot/conf/confdb.h"
#include "libknot/errcode.h"
#include "libknot/yparser/yptrafo.h"
31
#include "contrib/openbsd/strlcpy.h"
32
#include "contrib/wire_ctx.h"
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * A simple configuration:
 *
 * server.identity: "knot"
 * server.version: "version"
 * template[tpl1].storage: "directory1"
 * template[tpl2].storage: "directory2"
 * template[tpl2].master: [ "master1", "master2" ]
 *
 * And the corresponding configuration DB content:
 *
 * # DB structure version.
 * [00][FF]:         [02]
 * # Sections codes.
 * [00][00]server:   [02]
 * [00][00]template: [03]
 * # Server section items codes.
 * [02][00]identity: [02]
 * [02][00]version:  [03]
 * # Server items values.
 * [02][02]:         knot\0
 * [02][03]:         version\0
 * # Template section items codes.
 * [03][00]master:   [03]
 * [03][00]storage:  [02]
 * # Template identificators.
 * [03][01]tpl1\0
 * [03][01]tpl2\0
 * # Template items values.
 * [03][02]tpl1\0:   directory1\0
 * [03][02]tpl2\0:   directory2\0
 * [03][03]tpl2\0:   [00][08]master1\0 [00][08]master2\0
 */

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
typedef enum {
	KEY0_ROOT    =   0,
	KEY1_ITEMS   =   0,
	KEY1_ID      =   1,
	KEY1_FIRST   =   2,
	KEY1_LAST    = 200,
	KEY1_VERSION = 255
} db_code_t;

typedef enum {
	KEY0_POS = 0,
	KEY1_POS = 1,
	NAME_POS = 2
} db_code_pos_t;

typedef enum {
	DB_GET,
	DB_SET,
	DB_DEL
} db_action_t;

static int db_check_version(
	conf_t *conf,
91
	knot_db_txn_t *txn)
92 93
{
	uint8_t k[2] = { KEY0_ROOT, KEY1_VERSION };
94 95
	knot_db_val_t key = { k, sizeof(k) };
	knot_db_val_t data;
96 97 98 99 100 101 102 103 104 105 106

	// Get conf-DB version.
	int ret = conf->api->find(txn, &key, &data, 0);
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Check conf-DB version.
	if (data.len != 1 || ((uint8_t *)data.data)[0] != CONF_DB_VERSION) {
		return KNOT_CONF_EVERSION;
	}
107

108 109 110
	return KNOT_EOK;
}

111 112
int conf_db_init(
	conf_t *conf,
113 114
	knot_db_txn_t *txn,
	bool purge)
115 116 117 118 119
{
	if (conf == NULL || txn == NULL) {
		return KNOT_EINVAL;
	}

120
	uint8_t k[2] = { KEY0_ROOT, KEY1_VERSION };
121
	knot_db_val_t key = { k, sizeof(k) };
122 123 124 125

	int ret = conf->api->count(txn);
	if (ret == 0) { // Initialize empty DB with DB version.
		uint8_t d[1] = { CONF_DB_VERSION };
126
		knot_db_val_t data = { d, sizeof(d) };
127
		return conf->api->insert(txn, &key, &data, 0);
128 129 130 131 132 133 134 135 136 137
	} else if (ret > 0) { // Non-empty DB.
		if (purge) {
			// Purge the DB.
			ret = conf->api->clear(txn);
			if (ret != KNOT_EOK) {
				return ret;
			}
			return conf_db_init(conf, txn, false);
		}
		return KNOT_EOK;
138 139 140 141 142 143 144 145 146 147
	} else { // DB error.
		return ret;
	}
}

int conf_db_check(
	conf_t *conf,
	knot_db_txn_t *txn)
{
	int ret = conf->api->count(txn);
148 149 150 151 152 153 154 155 156 157 158 159 160
	if (ret == 0) { // Not initialized DB.
		return KNOT_CONF_ENOTINIT;
	} else if (ret > 0) { // Check the DB.
		int count = ret;

		ret = db_check_version(conf, txn);
		if (ret != KNOT_EOK) {
			return ret;
		} else if (count == 1) {
			return KNOT_EOK; // Empty but initialized DB.
		} else {
			return count - 1; // Non-empty DB.
		}
161 162 163 164 165
	} else { // DB error.
		return ret;
	}
}

166
static int db_code(
167
	conf_t *conf,
168
	knot_db_txn_t *txn,
169 170
	uint8_t section_code,
	const yp_name_t *name,
171 172
	db_action_t action,
	uint8_t *code)
173
{
174
	if (name == NULL) {
175 176 177
		return KNOT_EINVAL;
	}

178
	knot_db_val_t key;
179 180
	uint8_t k[CONF_MIN_KEY_LEN + YP_MAX_ITEM_NAME_LEN];
	k[KEY0_POS] = section_code;
181
	k[KEY1_POS] = KEY1_ITEMS;
182 183 184 185 186
	memcpy(k + NAME_POS, name + 1, name[0]);
	key.data = k;
	key.len = CONF_MIN_KEY_LEN + name[0];

	// Check if the item is already registered.
187
	knot_db_val_t data;
188
	int ret = conf->api->find(txn, &key, &data, 0);
189 190 191 192 193 194 195 196
	switch (ret) {
	case KNOT_EOK:
		if (action == DB_DEL) {
			return conf->api->del(txn, &key);
		}
		if (code != NULL) {
			*code = ((uint8_t *)data.data)[0];
		}
197
		return KNOT_EOK;
198 199 200 201 202 203 204
	case KNOT_ENOENT:
		if (action != DB_SET) {
			return KNOT_ENOENT;
		}
		break;
	default:
		return ret;
205 206 207 208 209
	}

	// Reduce the key to common prefix only.
	key.len = CONF_MIN_KEY_LEN;

210 211 212
	bool codes[KEY1_LAST + 1] = { false };

	// Find all used item codes.
213 214
	knot_db_iter_t *it = conf->api->iter_begin(txn, KNOT_DB_NOOP);
	it = conf->api->iter_seek(it, &key, KNOT_DB_GEQ);
215
	while (it != NULL) {
216
		knot_db_val_t iter_key;
217 218 219 220 221 222 223 224 225 226 227 228 229
		ret = conf->api->iter_key(it, &iter_key);
		if (ret != KNOT_EOK) {
			conf->api->iter_finish(it);
			return ret;
		}
		uint8_t *key_data = (uint8_t *)iter_key.data;

		// Check for database prefix end.
		if (key_data[KEY0_POS] != k[KEY0_POS] ||
		    key_data[KEY1_POS] != k[KEY1_POS]) {
			break;
		}

230
		knot_db_val_t iter_val;
231 232 233 234 235 236
		ret = conf->api->iter_val(it, &iter_val);
		if (ret != KNOT_EOK) {
			conf->api->iter_finish(it);
			return ret;
		}
		uint8_t code = ((uint8_t *)iter_val.data)[0];
237
		codes[code] = true;
238 239 240 241 242

		it = conf->api->iter_next(it);
	}
	conf->api->iter_finish(it);

243 244 245 246 247 248 249 250 251
	// Find the smallest unused item code.
	uint8_t new_code = KEY1_FIRST;
	while (codes[new_code]) {
		new_code++;
		if (new_code > KEY1_LAST) {
			return KNOT_ESPACE;
		}
	}

252 253 254 255 256 257 258 259 260 261 262 263 264
	// Restore the full key.
	key.len = CONF_MIN_KEY_LEN + name[0];

	// Fill the data with a new code.
	data.data = &new_code;
	data.len = sizeof(new_code);

	// Register new item code.
	ret = conf->api->insert(txn, &key, &data, 0);
	if (ret != KNOT_EOK) {
		return ret;
	}

265 266 267
	if (code != NULL) {
		*code = new_code;
	}
268 269 270 271

	return KNOT_EOK;
}

272
static uint8_t *find_data(
273 274
	const knot_db_val_t *value,
	const knot_db_val_t *current)
275
{
276
	wire_ctx_t ctx = wire_ctx_init_const(current->data, current->len);
277

278 279 280
	// Loop over the data array. Each item has 2B length prefix.
	while (wire_ctx_available(&ctx) > 0) {
		uint16_t len = wire_ctx_read_u16(&ctx);
281
		assert(ctx.error == KNOT_EOK);
282 283

		// Check for the same data.
284 285
		if (len == value->len &&
		    memcmp(ctx.position, value->data, value->len) == 0) {
286 287 288
			wire_ctx_skip(&ctx, -sizeof(uint16_t));
			assert(ctx.error == KNOT_EOK);
			return ctx.position;
289
		}
290
		wire_ctx_skip(&ctx, len);
291 292
	}

293 294
	assert(ctx.error == KNOT_EOK && wire_ctx_available(&ctx) == 0);

295
	return NULL;
296 297
}

298
static int db_set(
299
	conf_t *conf,
300 301 302
	knot_db_txn_t *txn,
	knot_db_val_t *key,
	knot_db_val_t *data,
303 304
	bool multi)
{
305 306
	if (!multi) {
		if (data->len > CONF_MAX_DATA_LEN) {
307 308 309
			return KNOT_ERANGE;
		}

310 311 312
		// Insert new (overwrite old) data.
		return conf->api->insert(txn, key, data, 0);
	}
313

314
	knot_db_val_t d;
315

316 317 318 319 320 321 322 323 324 325 326
	if (data->len > UINT16_MAX) {
		return KNOT_ERANGE;
	}

	int ret = conf->api->find(txn, key, &d, 0);
	if (ret == KNOT_ENOENT) {
		d.len = 0;
	} else if (ret == KNOT_EOK) {
		// Check for duplicate data.
		if (find_data(data, &d) != NULL) {
			return KNOT_EOK;
327
		}
328 329 330
	} else {
		return ret;
	}
331

332 333 334 335 336
	// Prepare buffer for all data.
	size_t new_len = d.len + sizeof(uint16_t) + data->len;
	if (new_len > CONF_MAX_DATA_LEN) {
		return KNOT_ESPACE;
	}
337

338 339 340 341
	uint8_t *new_data = malloc(new_len);
	if (new_data == NULL) {
		return KNOT_ENOMEM;
	}
342

343
	wire_ctx_t ctx = wire_ctx_init(new_data, new_len);
344

345 346 347 348 349 350
	// Copy current data array.
	wire_ctx_write(&ctx, d.data, d.len);
	// Copy length prefix for the new data item.
	wire_ctx_write_u16(&ctx, data->len);
	// Copy the new data item.
	wire_ctx_write(&ctx, data->data, data->len);
351

352
	assert(ctx.error == KNOT_EOK && wire_ctx_available(&ctx) == 0);
353

354 355
	d.data = new_data;
	d.len = new_len;
356

357 358
	// Insert new (or append) data.
	ret = conf->api->insert(txn, key, &d, 0);
359

360 361 362
	free(new_data);

	return ret;
363 364 365 366
}

int conf_db_set(
	conf_t *conf,
367
	knot_db_txn_t *txn,
368 369 370 371 372 373
	const yp_name_t *key0,
	const yp_name_t *key1,
	const uint8_t *id,
	size_t id_len,
	const uint8_t *data,
	size_t data_len)
374
{
375 376
	if (conf == NULL || txn == NULL || key0 == NULL ||
	    (id == NULL && id_len > 0) || (data == NULL && data_len > 0)) {
377 378 379
		return KNOT_EINVAL;
	}

380
	// Check for valid keys.
381
	const yp_item_t *item = yp_schema_find(key1 != NULL ? key1 : key0,
382
	                                       key1 != NULL ? key0 : NULL,
383
	                                       conf->schema);
384 385 386
	if (item == NULL) {
		return KNOT_YP_EINVAL_ITEM;
	}
387 388

	// Ignore alone key0 insertion.
389
	if (key1 == NULL && id_len == 0) {
390 391 392
		return KNOT_EOK;
	}

393 394 395 396 397 398 399
	// Ignore group id as a key1.
	if (item->parent != NULL && (item->parent->flags & YP_FMULTI) != 0 &&
	    item->parent->var.g.id == item) {
		key1 = NULL;
	}

	uint8_t k[CONF_MAX_KEY_LEN] = { 0 };
400
	knot_db_val_t key = { k, CONF_MIN_KEY_LEN };
401

402
	// Set key0 code.
403
	int ret = db_code(conf, txn, KEY0_ROOT, key0, DB_SET, &k[KEY0_POS]);
404 405 406 407 408
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Set id part.
409 410 411 412 413 414
	if (id_len > 0) {
		if (id_len > YP_MAX_ID_LEN) {
			return KNOT_YP_EINVAL_ID;
		}
		memcpy(k + CONF_MIN_KEY_LEN, id, id_len);
		key.len += id_len;
415

416
		k[KEY1_POS] = KEY1_ID;
417
		knot_db_val_t val = { NULL };
418

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		// Insert id.
		if (key1 == NULL) {
			ret = conf->api->find(txn, &key, &val, 0);
			if (ret == KNOT_EOK) {
				return KNOT_CONF_EREDEFINE;
			}
			ret = db_set(conf, txn, &key, &val, false);
			if (ret != KNOT_EOK) {
				return ret;
			}
		// Check for existing id.
		} else {
			ret = conf->api->find(txn, &key, &val, 0);
			if (ret != KNOT_EOK) {
				return KNOT_YP_EINVAL_ID;
			}
435
		}
436
	}
437

438 439
	// Insert key1 data.
	if (key1 != NULL) {
440
		// Set key1 code.
441
		ret = db_code(conf, txn, k[KEY0_POS], key1, DB_SET, &k[KEY1_POS]);
442 443 444 445
		if (ret != KNOT_EOK) {
			return ret;
		}

446
		knot_db_val_t val = { (uint8_t *)data, data_len };
447 448 449 450
		ret = db_set(conf, txn, &key, &val, item->flags & YP_FMULTI);
		if (ret != KNOT_EOK) {
			return ret;
		}
451
	}
452 453

	return KNOT_EOK;
454 455
}

456 457
static int db_unset(
	conf_t *conf,
458 459 460
	knot_db_txn_t *txn,
	knot_db_val_t *key,
	knot_db_val_t *data,
461 462 463 464 465 466 467
	bool multi)
{
	// No item data can be zero length.
	if (data->len == 0) {
		return conf->api->del(txn, key);
	}

468
	knot_db_val_t d;
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

	int ret = conf->api->find(txn, key, &d, 0);
	if (ret != KNOT_EOK) {
		return ret;
	}

	// Process singlevalued data.
	if (!multi) {
		if (d.len != data->len ||
		    memcmp((uint8_t *)d.data, data->data, d.len) != 0) {
			return KNOT_ENOENT;
		}
		return conf->api->del(txn, key);
	}

	// Check if the data exists.
	uint8_t *pos = find_data(data, &d);
	if (pos == NULL) {
		return KNOT_ENOENT;
	}

	// Prepare buffer for reduced data.
	size_t total_len = d.len - sizeof(uint16_t) - data->len;
	if (total_len  == 0) {
		return conf->api->del(txn, key);
	}

	uint8_t *new_data = malloc(total_len);
	if (new_data == NULL) {
		return KNOT_ENOMEM;
	}

	size_t new_len = 0;

	// Copy leading data block.
	assert(pos >= (uint8_t *)d.data);
	size_t head_len = pos - (uint8_t *)d.data;
	if (head_len > 0) {
		memcpy(new_data, d.data, head_len);
		new_len += head_len;
	}

	pos += sizeof(uint16_t) + data->len;

	// Copy trailing data block.
	assert(pos <= (uint8_t *)d.data + d.len);
	size_t tail_len = (uint8_t *)d.data + d.len - pos;
	if (tail_len > 0) {
		memcpy(new_data + new_len, pos, tail_len);
		new_len += tail_len;
	}

	d.data = new_data;
	d.len = new_len;

	// Insert reduced data.
	ret = conf->api->insert(txn, key, &d, 0);

	free(new_data);

	return ret;
}

int conf_db_unset(
533
	conf_t *conf,
534
	knot_db_txn_t *txn,
535 536 537 538
	const yp_name_t *key0,
	const yp_name_t *key1,
	const uint8_t *id,
	size_t id_len,
539 540 541
	const uint8_t *data,
	size_t data_len,
	bool delete_key1)
542
{
543 544
	if (conf == NULL || txn == NULL || key0 == NULL ||
	    (id == NULL && id_len > 0) || (data == NULL && data_len > 0)) {
545 546 547
		return KNOT_EINVAL;
	}

548
	// Check for valid keys.
549
	const yp_item_t *item = yp_schema_find(key1 != NULL ? key1 : key0,
550
	                                       key1 != NULL ? key0 : NULL,
551
	                                       conf->schema);
552 553 554 555 556 557 558
	if (item == NULL) {
		return KNOT_YP_EINVAL_ITEM;
	}

	// Delete the key0.
	if (key1 == NULL && id_len == 0) {
		return db_code(conf, txn, KEY0_ROOT, key0, DB_DEL, NULL);
559 560
	}

561 562 563 564 565 566 567
	// Ignore group id as a key1.
	if (item->parent != NULL && (item->parent->flags & YP_FMULTI) != 0 &&
	    item->parent->var.g.id == item) {
		key1 = NULL;
	}

	uint8_t k[CONF_MAX_KEY_LEN] = { 0 };
568
	knot_db_val_t key = { k, CONF_MIN_KEY_LEN };
569

570 571
	// Set the key0 code.
	int ret = db_code(conf, txn, KEY0_ROOT, key0, DB_GET, &k[KEY0_POS]);
572 573 574 575
	if (ret != KNOT_EOK) {
		return ret;
	}

576 577
	// Set the id part.
	if (id_len > 0) {
578
		if (id_len > YP_MAX_ID_LEN) {
579
			return KNOT_YP_EINVAL_ID;
580 581 582 583
		}
		memcpy(k + CONF_MIN_KEY_LEN, id, id_len);
		key.len += id_len;

584
		k[KEY1_POS] = KEY1_ID;
585
		knot_db_val_t val = { NULL };
586 587 588 589 590 591 592 593 594 595 596

		// Delete the id.
		if (key1 == NULL) {
			return conf->api->del(txn, &key);
		// Check for existing id.
		} else {
			ret = conf->api->find(txn, &key, &val, 0);
			if (ret != KNOT_EOK) {
				return KNOT_YP_EINVAL_ID;
			}
		}
597 598
	}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	if (key1 != NULL) {
		// Set the key1 code.
		ret = db_code(conf, txn, k[KEY0_POS], key1, DB_GET, &k[KEY1_POS]);
		if (ret != KNOT_EOK) {
			return ret;
		}

		// Delete the key1.
		if (data_len == 0 && delete_key1) {
			ret = db_code(conf, txn, k[KEY0_POS], key1, DB_DEL, NULL);
			if (ret != KNOT_EOK) {
				return ret;
			}
		// Delete the item data.
		} else {
614
			knot_db_val_t val = { (uint8_t *)data, data_len };
615 616 617 618 619
			ret = db_unset(conf, txn, &key, &val, item->flags & YP_FMULTI);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
620 621 622 623 624
	}

	return KNOT_EOK;
}

625 626
int conf_db_get(
	conf_t *conf,
627
	knot_db_txn_t *txn,
628 629 630 631 632
	const yp_name_t *key0,
	const yp_name_t *key1,
	const uint8_t *id,
	size_t id_len,
	conf_val_t *data)
633
{
634 635 636 637 638 639 640 641 642
	conf_val_t out = { NULL };

	if (conf == NULL || txn == NULL || key0 == NULL ||
	    (id == NULL && id_len > 0)) {
		out.code = KNOT_EINVAL;
		goto get_error;
	}

	// Check for valid keys.
643
	out.item = yp_schema_find(key1 != NULL ? key1 : key0,
644
	                          key1 != NULL ? key0 : NULL,
645
	                          conf->schema);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
	if (out.item == NULL) {
		out.code = KNOT_YP_EINVAL_ITEM;
		goto get_error;
	}

	// At least key1 or id must be specified.
	if (key1 == NULL && id_len == 0) {
		out.code = KNOT_EINVAL;
		goto get_error;
	}

	// Ignore group id as a key1.
	if (out.item->parent != NULL && (out.item->parent->flags & YP_FMULTI) != 0 &&
	    out.item->parent->var.g.id == out.item) {
		key1 = NULL;
	}
662

663
	uint8_t k[CONF_MAX_KEY_LEN] = { 0 };
664 665
	knot_db_val_t key = { k, CONF_MIN_KEY_LEN };
	knot_db_val_t val = { NULL };
666 667 668 669

	// Set the key0 code.
	out.code = db_code(conf, txn, KEY0_ROOT, key0, DB_GET, &k[KEY0_POS]);
	if (out.code != KNOT_EOK) {
670 671 672
		if (id_len > 0) {
			out.code = KNOT_YP_EINVAL_ID;
		}
673 674 675 676 677 678 679 680
		goto get_error;
	}

	// Set the id part.
	if (id_len > 0) {
		if (id_len > YP_MAX_ID_LEN) {
			out.code = KNOT_YP_EINVAL_ID;
			goto get_error;
681
		}
682 683
		memcpy(k + CONF_MIN_KEY_LEN, id, id_len);
		key.len += id_len;
684

685
		k[KEY1_POS] = KEY1_ID;
686

687 688 689 690 691
		// Check for existing id.
		out.code = conf->api->find(txn, &key, &val, 0);
		if (out.code != KNOT_EOK) {
			out.code = KNOT_YP_EINVAL_ID;
			goto get_error;
692 693
		}
	}
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

	// Set the key1 code.
	if (key1 != NULL) {
		out.code = db_code(conf, txn, k[KEY0_POS], key1, DB_GET, &k[KEY1_POS]);
		if (out.code != KNOT_EOK) {
			goto get_error;
		}
	}

	// Get the data.
	out.code = conf->api->find(txn, &key, &val, 0);
	if (out.code == KNOT_EOK) {
		out.blob = val.data;
		out.blob_len = val.len;
	}
get_error:
	// Set the output.
	if (data != NULL) {
		*data = out;
	}

	return out.code;
716 717
}

718 719 720
static int check_iter(
	conf_t *conf,
	conf_iter_t *iter)
721
{
722
	knot_db_val_t key;
723

724 725 726 727
	// Get the current key.
	int ret = conf->api->iter_key(iter->iter, &key);
	if (ret != KNOT_EOK) {
		return KNOT_ENOENT;
728
	}
729
	uint8_t *key_data = (uint8_t *)key.data;
730

731 732 733
	// Check for key overflow.
	if (key_data[KEY0_POS] != iter->key0_code || key_data[KEY1_POS] != KEY1_ID) {
		return KNOT_EOF;
734
	}
735 736

	return KNOT_EOK;
737 738 739 740
}

int conf_db_iter_begin(
	conf_t *conf,
741
	knot_db_txn_t *txn,
742 743 744
	const yp_name_t *key0,
	conf_iter_t *iter)
{
745 746
	conf_iter_t out = { NULL };

747
	if (conf == NULL || txn == NULL || key0 == NULL || iter == NULL) {
748 749
		out.code = KNOT_EINVAL;
		goto iter_begin_error;
750 751
	}

752 753
	// Look-up group id item in the schema.
	const yp_item_t *grp = yp_schema_find(key0, NULL, conf->schema);
754
	if (grp == NULL) {
755 756
		out.code = KNOT_YP_EINVAL_ITEM;
		goto iter_begin_error;
757
	}
758 759 760 761 762
	if (grp->type != YP_TGRP || (grp->flags & YP_FMULTI) == 0) {
		out.code = KNOT_ENOTSUP;
		goto iter_begin_error;
	}
	out.item = grp->var.g.id;
763 764

	// Get key0 code.
765 766 767
	out.code = db_code(conf, txn, KEY0_ROOT, key0, DB_GET, &out.key0_code);
	if (out.code != KNOT_EOK) {
		goto iter_begin_error;
768 769 770
	}

	// Prepare key prefix.
771
	uint8_t k[2] = { out.key0_code, KEY1_ID };
772
	knot_db_val_t key = { k, sizeof(k) };
773 774

	// Get the data.
775 776
	out.iter = conf->api->iter_begin(txn, KNOT_DB_NOOP);
	out.iter = conf->api->iter_seek(out.iter, &key, KNOT_DB_GEQ);
777

778 779 780
	// Check for no section id.
	out.code = check_iter(conf, &out);
	if (out.code != KNOT_EOK) {
781
		out.code = KNOT_ENOENT; // Treat all errors as no entry.
782
		conf_db_iter_finish(conf, &out);
783 784 785 786
		goto iter_begin_error;
	}

iter_begin_error:
787 788 789 790
	// Set the output.
	if (iter != NULL) {
		*iter = out;
	}
791 792

	return out.code;
793 794 795 796 797 798 799 800 801 802
}

int conf_db_iter_next(
	conf_t *conf,
	conf_iter_t *iter)
{
	if (conf == NULL || iter == NULL) {
		return KNOT_EINVAL;
	}

803 804
	if (iter->code != KNOT_EOK) {
		return iter->code;
805
	}
806
	assert(iter->iter != NULL);
807 808 809 810

	// Move to the next key-value.
	iter->iter = conf->api->iter_next(iter->iter);
	if (iter->iter == NULL) {
811 812 813
		conf_db_iter_finish(conf, iter);
		iter->code = KNOT_EOF;
		return iter->code;
814 815 816
	}

	// Check for key overflow.
817 818 819 820
	iter->code = check_iter(conf, iter);
	if (iter->code != KNOT_EOK) {
		conf_db_iter_finish(conf, iter);
		return iter->code;
821 822 823 824 825 826 827 828
	}

	return KNOT_EOK;
}

int conf_db_iter_id(
	conf_t *conf,
	conf_iter_t *iter,
829
	const uint8_t **data,
830 831
	size_t *data_len)
{
832 833
	if (conf == NULL || iter == NULL || iter->iter == NULL ||
	    data == NULL || data_len == NULL) {
834 835 836
		return KNOT_EINVAL;
	}

837
	knot_db_val_t key;
838 839 840 841 842
	int ret = conf->api->iter_key(iter->iter, &key);
	if (ret != KNOT_EOK) {
		return ret;
	}

843
	*data = (uint8_t *)key.data + CONF_MIN_KEY_LEN;
844 845 846 847 848
	*data_len = key.len - CONF_MIN_KEY_LEN;

	return KNOT_EOK;
}

849 850 851 852 853 854 855 856
int conf_db_iter_del(
	conf_t *conf,
	conf_iter_t *iter)
{
	if (conf == NULL || iter == NULL || iter->iter == NULL) {
		return KNOT_EINVAL;
	}

857
	return knot_db_lmdb_iter_del(iter->iter);
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
}

void conf_db_iter_finish(
	conf_t *conf,
	conf_iter_t *iter)
{
	if (conf == NULL || iter == NULL) {
		return;
	}

	if (iter->iter != NULL) {
		conf->api->iter_finish(iter->iter);
		iter->iter = NULL;
	}
}

874 875
int conf_db_raw_dump(
	conf_t *conf,
876
	knot_db_txn_t *txn,
877 878 879 880 881 882
	const char *file_name)
{
	if (conf == NULL) {
		return KNOT_EINVAL;
	}

883 884 885 886 887
	// Use the current config read transaction if not specified.
	if (txn == NULL) {
		txn = &conf->read_txn;
	}

888 889 890 891 892 893 894 895 896 897
	FILE *fp = stdout;
	if (file_name != NULL) {
		fp = fopen(file_name, "w");
		if (fp == NULL) {
			return KNOT_ERROR;
		}
	}

	int ret = KNOT_EOK;

898
	knot_db_iter_t *it = conf->api->iter_begin(txn, KNOT_DB_FIRST);
899
	while (it != NULL) {
900
		knot_db_val_t key;
901 902 903 904 905
		ret = conf->api->iter_key(it, &key);
		if (ret != KNOT_EOK) {
			break;
		}

906
		knot_db_val_t data;
907 908 909 910 911 912 913
		ret = conf->api->iter_val(it, &data);
		if (ret != KNOT_EOK) {
			break;
		}

		uint8_t *k = (uint8_t *)key.data;
		uint8_t *d = (uint8_t *)data.data;
914
		if (k[1] == KEY1_ITEMS) {
915 916 917
			fprintf(fp, "[%i][%i]%.*s", k[0], k[1],
			        (int)key.len - 2, k + 2);
			fprintf(fp, ": %u\n", d[0]);
918
		} else if (k[1] == KEY1_ID) {
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
			fprintf(fp, "[%i][%i](%zu){", k[0], k[1], key.len - 2);
			for (size_t i = 2; i < key.len; i++) {
				fprintf(fp, "%02x", (uint8_t)k[i]);
			}
			fprintf(fp, "}\n");
		} else {
			fprintf(fp, "[%i][%i]", k[0], k[1]);
			if (key.len > 2) {
				fprintf(fp, "(%zu){", key.len - 2);
				for (size_t i = 2; i < key.len; i++) {
					fprintf(fp, "%02x", (uint8_t)k[i]);
				}
				fprintf(fp, "}");
			}
			fprintf(fp, ": (%zu)<", data.len);
			for (size_t i = 0; i < data.len; i++) {
				fprintf(fp, "%02x", (uint8_t)d[i]);
			}
			fprintf(fp, ">\n");
		}

		it = conf->api->iter_next(it);
	}
	conf->api->iter_finish(it);

	if (file_name != NULL) {
		fclose(fp);
	} else {
		fflush(fp);
	}

	return ret;
}