kasp_db.c 20.1 KB
Newer Older
1 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 31 32
/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 "knot/dnssec/kasp/kasp_db.h"

#include <stdarg.h> // just for va_free()
#include <pthread.h>
#include <sys/stat.h>

#include "contrib/files.h"
#include "contrib/wire_ctx.h"

struct kasp_db {
	knot_db_t *keys_db;
	char *db_path;
	size_t db_mapsize;
	pthread_mutex_t opening_mutex;
};

33 34 35 36 37
typedef enum {
	KASPDBKEY_PARAMS = 0x1,
	KASPDBKEY_POLICYLAST = 0x2,
	KASPDBKEY_NSEC3SALT = 0x3,
	KASPDBKEY_NSEC3TIME = 0x4,
38
	KASPDBKEY_MASTERSERIAL = 0x5,
39
	KASPDBKEY_LASTSIGNEDSERIAL = 0x6,
40 41
} keyclass_t;

42 43 44 45
static const knot_db_api_t *db_api = NULL;

static kasp_db_t *global_kasp_db = NULL;

46
kasp_db_t **kaspdb(void)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
	return &global_kasp_db;
}

int kasp_db_init(kasp_db_t **db, const char *path, size_t mapsize)
{
	if (db == NULL || path == NULL || *db != NULL) {
		return KNOT_EINVAL;
	}

	db_api = knot_db_lmdb_api();

	*db = calloc(1, sizeof(**db));
	if (*db == NULL) {
		return KNOT_ENOMEM;
	}

	(*db)->db_path = strdup(path);
	if ((*db)->db_path == NULL) {
		free(*db);
		return KNOT_ENOMEM;
	}

	(*db)->db_mapsize = mapsize;

	pthread_mutex_init(&(*db)->opening_mutex, NULL);
	return KNOT_EOK;
}

int kasp_db_reconfigure(kasp_db_t **db, const char *new_path, size_t new_mapsize)
{
	if (db == NULL || new_path == NULL || *db == NULL || (*db)->db_path == NULL) {
		return KNOT_EINVAL;
	}

	pthread_mutex_lock(&(*db)->opening_mutex);

	bool changed_path = (strcmp(new_path, (*db)->db_path) != 0);
	bool changed_mapsize = (new_mapsize != (*db)->db_mapsize);

	if ((*db)->keys_db != NULL) {
		pthread_mutex_unlock(&(*db)->opening_mutex);
		if (changed_path) {
			return KNOT_EBUSY;
		} else if (changed_mapsize) {
			return KNOT_EEXIST;
		} else {
			return KNOT_ENODIFF;
		}
	}

	free((*db)->db_path);
	(*db)->db_path = strdup(new_path);
	if ((*db)->db_path == NULL) {
		pthread_mutex_unlock(&(*db)->opening_mutex);
		return KNOT_ENOMEM;
	}
	(*db)->db_mapsize = new_mapsize;

	pthread_mutex_unlock(&(*db)->opening_mutex);
	return KNOT_EOK;
}

110 111 112 113 114 115 116 117 118 119 120
bool kasp_db_exists(kasp_db_t *db)
{
	if (db->keys_db == NULL) {
		struct stat st;
		if (stat(db->db_path, &st) != 0) {
			return false;
		}
	}
	return true;
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
int kasp_db_open(kasp_db_t *db)
{
	if (db == NULL || db->db_path == NULL) {
		return KNOT_EINVAL;
	}

	pthread_mutex_lock(&db->opening_mutex);

	if (db->keys_db != NULL) {
		pthread_mutex_unlock(&db->opening_mutex);
		return KNOT_EOK; // already open
	}

	int ret = make_dir(db->db_path, S_IRWXU | S_IRGRP | S_IXGRP, true);
	if (ret != KNOT_EOK) {
		pthread_mutex_unlock(&db->opening_mutex);
		return ret;
	}

	struct knot_db_lmdb_opts opts = KNOT_DB_LMDB_OPTS_INITIALIZER;
	opts.path = db->db_path;
	opts.mapsize = db->db_mapsize;
143
	opts.maxdbs = 1;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	opts.dbname = "keys_db";

	ret = db_api->init(&db->keys_db, NULL, &opts);
	if (ret != KNOT_EOK) {
		pthread_mutex_unlock(&db->opening_mutex);
		return ret;
	}

	pthread_mutex_unlock(&db->opening_mutex);

	return ret;
}

void kasp_db_close(kasp_db_t **db)
{
	if (db != NULL && *db != NULL) {
		pthread_mutex_lock(&(*db)->opening_mutex);
		db_api->deinit((*db)->keys_db);
162
		(*db)->keys_db = NULL;
163 164 165 166 167 168 169 170
		pthread_mutex_unlock(&(*db)->opening_mutex);
		free((*db)->db_path);
		pthread_mutex_destroy(&(*db)->opening_mutex);
		free(*db);
		*db = NULL;
	}
}

171
static knot_db_val_t make_key(keyclass_t kclass, const knot_dname_t *dname, const char *str)
172
{
173 174 175
	size_t dnlen = (dname == NULL ? 0 : knot_dname_size((const knot_dname_t *)dname));
	size_t slen = (str == NULL ? 0 : strlen(str) + 1);
	knot_db_val_t res = { .len = 1 + dnlen + slen, .data = malloc(1 + dnlen + slen) };
176
	if (res.data != NULL) {
177 178 179 180 181 182
		wire_ctx_t wire = wire_ctx_init(res.data, res.len);
		wire_ctx_write_u8(&wire, (uint8_t)kclass);
		wire_ctx_write(&wire, dname, dnlen);
		wire_ctx_write(&wire, str, slen);
	} else {
		res.len = 0;
183 184 185 186
	}
	return res;
}

187
static void free_key(knot_db_val_t *key)
188
{
189 190
	free(key->data);
	memset(key, 0, sizeof(*key));
191 192
}

193
static char *keyid_fromkey(const knot_db_val_t *key)
194
{
195
	if (key->len < 2 || *(uint8_t *)key->data != KASPDBKEY_PARAMS) {
196 197
		return NULL;
	}
198 199
	size_t skip = knot_dname_size((const knot_dname_t *)key->data + 1);
	return (key->len < skip + 2 ? NULL : strdup(key->data + skip + 1));
200 201
}

202
static bool check_key_zone(const knot_db_val_t *key, const knot_dname_t *zone_name)
203
{
204 205
	if (key->len < 2 || *(uint8_t *)key->data == KASPDBKEY_POLICYLAST) {
		return false;
206
	}
207
	return (knot_dname_cmp(key->data + 1, zone_name) == 0);
208 209
}

210
static int serialize_key_params(const key_params_t *params, const knot_dname_t *dname, knot_db_val_t *key, knot_db_val_t *val)
211
{
212 213 214 215 216 217
	assert(params != NULL);
	assert(dname != NULL);
	assert(key != NULL);
	assert(val != NULL);

	*key = make_key(KASPDBKEY_PARAMS, dname, params->id);
218
	val->len = sizeof(uint16_t) + 2 * sizeof(uint8_t) + 11 * sizeof(uint64_t) +
219 220 221
	            params->public_key.size;
	val->data = malloc(val->len);
	if (val->data == NULL) {
222 223 224
		free(key->data);
		key->data = NULL;
		key->len = 0;
225 226 227 228 229
		return KNOT_ENOMEM;
	}
	wire_ctx_t wire = wire_ctx_init(val->data, val->len);

	wire_ctx_write_u64(&wire, params->public_key.size);
230
	wire_ctx_write_u64(&wire, 0); // length of Unused-future block at the end
231 232
	wire_ctx_write_u16(&wire, params->keytag);
	wire_ctx_write_u8(&wire, params->algorithm);
233 234 235 236
	uint8_t flags = 0x02;
	flags |= (params->is_ksk ? 0x01 : 0);
	flags |= (params->is_pub_only ? 0x04 : 0);
	wire_ctx_write_u8(&wire, flags);
237
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.created);
238
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.pre_active);
239
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.publish);
240
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.ready);
241
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.active);
242
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.retire_active);
243
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.retire);
244
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.post_active);
245 246 247 248
	wire_ctx_write_u64(&wire, (uint64_t)params->timing.remove);
	wire_ctx_write(&wire, params->public_key.data, params->public_key.size);

	if (wire.error != KNOT_EOK) {
249
		free(key->data);
250
		free(val->data);
251 252
		key->data = NULL;
		key->len = 0;
253 254 255 256 257 258 259 260 261
		val->data = NULL;
		val->len = 0;
		return KNOT_ERROR;
	}
	return KNOT_EOK;
}

static int deserialize_key_params(key_params_t *params, const knot_db_val_t *key, const knot_db_val_t *val)
{
262 263 264 265 266 267 268 269
	assert(params != NULL);
	assert(key != NULL);
	assert(val != NULL);
	assert(key->data != NULL);
	assert(val->data != NULL);
	assert(val->len >= sizeof(uint64_t));

	wire_ctx_t wire = wire_ctx_init_const(val->data, val->len);
270
	params->public_key.size = wire_ctx_read_u64(&wire);
271
	uint64_t unused_future_length = wire_ctx_read_u64(&wire);
272 273
	params->keytag = wire_ctx_read_u16(&wire);
	params->algorithm = wire_ctx_read_u8(&wire);
274 275
	uint8_t isksk_plus_flags = wire_ctx_read_u8(&wire);
	params->is_ksk = ((isksk_plus_flags & (uint8_t)0x01) != (uint8_t)0x00);
276
	params->is_pub_only = ((isksk_plus_flags & (uint8_t)0x04) != (uint8_t)0x00);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if ((isksk_plus_flags & (uint8_t)0x02) != (uint8_t)0x00) {
		params->timing.created = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.pre_active = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.publish = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.ready = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.active = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.retire_active = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.retire = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.post_active = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.remove = (knot_time_t)wire_ctx_read_u64(&wire);
	} else {
		// import of old kasp db format missing some timers
		params->timing.created = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.pre_active = 0;
		params->timing.publish = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.ready = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.active = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.retire_active = 0;
		params->timing.retire = (knot_time_t)wire_ctx_read_u64(&wire);
		params->timing.post_active = 0;
		params->timing.remove = (knot_time_t)wire_ctx_read_u64(&wire);
	}
299 300 301 302 303 304 305 306 307 308 309 310
	if (wire.error != KNOT_EOK) {
		return KNOT_ERROR;
	}

	free(params->public_key.data);
	params->public_key.data = malloc(params->public_key.size);
	if (params->public_key.data == NULL) {
		return KNOT_ENOMEM;
	}
	wire_ctx_read(&wire, params->public_key.data, params->public_key.size);

	free(params->id);
311
	params->id = keyid_fromkey(key);
312 313 314 315
	if (params->id == NULL) {
		wire.error = KNOT_EMALF;
	}

316
	if (wire.error != KNOT_EOK || wire_ctx_available(&wire) != unused_future_length) {
317 318 319 320 321 322 323 324 325 326
		free(params->id);
		free(params->public_key.data);
		params->id = NULL;
		params->public_key.data = NULL;
		params->public_key.size = 0;
		return KNOT_EMALF;
	}
	return KNOT_EOK;
}

327 328 329 330 331 332 333 334 335 336 337 338
static key_params_t *keyval2params(const knot_db_val_t *key, const knot_db_val_t *val)
{
	key_params_t *res = calloc(1, sizeof(*res));
	if (res != NULL) {
		if (deserialize_key_params(res, key, val) != KNOT_EOK) {
			free(res);
			return NULL;
		}
	}
	return res;
}

339 340 341 342 343 344 345 346 347 348
#define txn_check(...) \
	if (ret != KNOT_EOK) { \
		db_api->txn_abort(txn); \
		va_free(NULL, __VA_ARGS__); \
		return ret; \
	} \

#define with_txn(what, ...) \
	int ret = KNOT_EOK; \
	knot_db_txn_t local_txn, *txn = &local_txn; \
349
	ret = db_api->txn_begin(db->keys_db, txn, (what & 0x1) ? 0 : KNOT_DB_RDONLY); \
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
	txn_check(__VA_ARGS__); \

#define with_txn_end(...) \
	txn_check(__VA_ARGS__); \
	ret = db_api->txn_commit(txn); \
	if (ret != KNOT_EOK) { \
		db_api->txn_abort(txn); \
	} \

#define KEYS_RO 0x0
#define KEYS_RW 0x1

// TODO move elsewhere
static void va_free(void *p, ...)
{
	va_list args;
	va_start(args, p);
	for (void *f = p; f != NULL; f = va_arg(args, void *)) {
		free(f);
	}
	va_end(args);
}

int kasp_db_list_keys(kasp_db_t *db, const knot_dname_t *zone_name, list_t *dst)
{
375
	if (db == NULL || db->keys_db == NULL || zone_name == NULL || dst == NULL) {
376 377 378
		return KNOT_ENOENT;
	}

379 380 381
	knot_db_val_t key = make_key(KASPDBKEY_PARAMS, zone_name, NULL), val = { 0 };

	with_txn(KEYS_RO, NULL);
382 383
	knot_db_iter_t *iter = db_api->iter_begin(txn, KNOT_DB_NOOP);
	if (iter != NULL) {
384
		iter = db_api->iter_seek(iter, &key, KNOT_DB_GEQ);
385
	}
386
	free_key(&key);
387 388 389

	init_list(dst);
	while (iter != NULL && ret == KNOT_EOK) {
390 391
		ret = db_api->iter_key(iter, &key);
		if (ret != KNOT_EOK || *(uint8_t *)key.data != KASPDBKEY_PARAMS || !check_key_zone(&key, zone_name)) {
392 393 394 395
			break;
		}
		ret = db_api->iter_val(iter, &val);
		if (ret == KNOT_EOK) {
396 397 398 399
			key_params_t *parm = keyval2params(&key, &val);
			if (parm != NULL) {
				ptrlist_add(dst, parm, NULL);
			}
400 401 402 403 404 405 406
			iter = db_api->iter_next(iter);
		}
	}
	db_api->iter_finish(iter);
	db_api->txn_abort(txn);

	if (ret != KNOT_EOK) {
407
		ptrlist_deep_free(dst, NULL);
408 409 410 411 412
		return ret;
	}
	return (EMPTY_LIST(*dst) ? KNOT_ENOENT : KNOT_EOK);
}

413
static bool keyid_inuse(knot_db_txn_t *txn, const char *key_id, key_params_t **optional)
414
{
415 416 417 418 419 420 421 422 423 424 425 426 427 428
	knot_db_iter_t *iter = db_api->iter_begin(txn, KNOT_DB_FIRST);
	while (iter != NULL) {
		knot_db_val_t key, val;
		if (db_api->iter_key(iter, &key) == KNOT_EOK && *(uint8_t *)key.data == KASPDBKEY_PARAMS) {
			char *keyid = keyid_fromkey(&key);
			if (keyid != NULL && strcmp(keyid, key_id) == 0) {
				if (optional != NULL && db_api->iter_val(iter, &val) == KNOT_EOK) {
					*optional = keyval2params(&key, &val);
				}
				db_api->iter_finish(iter);
				free(keyid);
				return true;
			}
			free(keyid);
429
		}
430
		iter = db_api->iter_next(iter);
431 432
	}
	db_api->iter_finish(iter);
433
	return false;
434 435 436 437
}

int kasp_db_delete_key(kasp_db_t *db, const knot_dname_t *zone_name, const char *key_id, bool *still_used)
{
438
	if (db == NULL || db->keys_db == NULL || zone_name == NULL || key_id == NULL) {
439 440 441
		return KNOT_EINVAL;
	}

442
	knot_db_val_t key = make_key(KASPDBKEY_PARAMS, zone_name, key_id);
443 444

	with_txn(KEYS_RW, key.data, NULL);
445 446 447 448 449
	ret = db_api->del(txn, &key);
	free_key(&key);
	if (still_used != NULL) {
		*still_used = keyid_inuse(txn, key_id, NULL);
	}
450 451 452 453
	with_txn_end(NULL, NULL);
	return ret;
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
int kasp_db_delete_all(kasp_db_t *db, const knot_dname_t *zone_name)
{
	list_t allkeys;
	init_list(&allkeys);
	int r = kasp_db_list_keys(db, zone_name, &allkeys);
	if (r != KNOT_EOK) {
		return r;
	}

	with_txn(KEYS_RW, NULL);

	ptrnode_t *n;
	WALK_LIST(n, allkeys) {
		key_params_t *parm = n->d;
		knot_db_val_t key = make_key(KASPDBKEY_PARAMS, zone_name, parm->id);
		ret = db_api->del(txn, &key);
		free_key(&key);
		free(parm->id);
		free(parm->public_key.data);
		memset(parm, 0, sizeof(*parm));
		if (ret != KNOT_EOK) {
			break;
		}
	}
478
	ptrlist_deep_free(&allkeys, NULL);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

	if (ret == KNOT_EOK) {
		knot_db_val_t key = make_key(KASPDBKEY_NSEC3SALT, zone_name, NULL);
		ret = db_api->del(txn, &key);
		free_key(&key);
		if (ret == KNOT_EOK) {
			key = make_key(KASPDBKEY_NSEC3TIME, zone_name, NULL);
			ret = db_api->del(txn, &key);
			free_key(&key);
		}
	}

	with_txn_end(NULL, NULL);
	return ret;
}

495 496
int kasp_db_add_key(kasp_db_t *db, const knot_dname_t *zone_name, const key_params_t *params)
{
497
	if (db == NULL || db->keys_db == NULL || zone_name == NULL || params == NULL) {
498 499 500
		return KNOT_EINVAL;
	}

501
	knot_db_val_t key = { 0 }, val = { 0 };
502

503 504 505 506 507 508
	with_txn(KEYS_RW, NULL);
	ret = serialize_key_params(params, zone_name, &key, &val);
	txn_check(NULL);
	ret = db_api->insert(txn, &key, &val, 0);
	free_key(&key);
	free_key(&val);
509 510 511 512
	with_txn_end(NULL, NULL);
	return ret;
}

513
int kasp_db_share_key(kasp_db_t *db, const knot_dname_t *zone_from, const knot_dname_t *zone_to, const char *key_id)
514
{
515 516
	if (db == NULL || db->keys_db == NULL || zone_from == NULL ||
	    zone_to == NULL || key_id == NULL) {
517 518 519
		return KNOT_EINVAL;
	}

520 521
	knot_db_val_t key_from = make_key(KASPDBKEY_PARAMS, zone_from, key_id),
	              key_to = make_key(KASPDBKEY_PARAMS, zone_to, key_id), val = { 0 };
522

523 524 525 526 527 528
	with_txn(KEYS_RW, NULL);
	ret = db_api->find(txn, &key_from, &val, 0);
	txn_check(txn, key_from.data, key_to.data, NULL);
	ret = db_api->insert(txn, &key_to, &val, 0);
	free_key(&key_from);
	free_key(&key_to);
529 530 531 532 533
	with_txn_end(NULL);
	return ret;
}

int kasp_db_store_nsec3salt(kasp_db_t *db, const knot_dname_t *zone_name,
534
			    const dnssec_binary_t *nsec3salt, knot_time_t salt_created)
535
{
536
	if (db == NULL || db->keys_db == NULL ||
537
	    zone_name == NULL || nsec3salt == NULL || salt_created <= 0) {
538 539 540 541
		return KNOT_EINVAL;
	}

	with_txn(KEYS_RW, NULL);
542
	knot_db_val_t key = make_key(KASPDBKEY_NSEC3SALT, zone_name, NULL);
543 544
	knot_db_val_t val = { .len = nsec3salt->size, .data = nsec3salt->data };
	ret = db_api->insert(txn, &key, &val, 0);
545
	free_key(&key);
546
	txn_check(NULL);
547
	key = make_key(KASPDBKEY_NSEC3TIME, zone_name, NULL);
548
	uint64_t tmp = htobe64((uint64_t)salt_created);
549 550 551
	val.len = sizeof(tmp);
	val.data = &tmp;
	ret = db_api->insert(txn, &key, &val, 0);
552
	free_key(&key);
553 554 555 556 557
	with_txn_end(NULL);
	return ret;
}

int kasp_db_load_nsec3salt(kasp_db_t *db, const knot_dname_t *zone_name,
558
			   dnssec_binary_t *nsec3salt, knot_time_t *salt_created)
559
{
560
	if (db == NULL || db->keys_db == NULL ||
561 562 563 564
	    zone_name == NULL || nsec3salt == NULL || salt_created == NULL) {
		return KNOT_EINVAL;
	}

565 566
	with_txn(KEYS_RW, NULL);
	knot_db_val_t key = make_key(KASPDBKEY_NSEC3TIME, zone_name, NULL), val = { 0 };
567
	ret = db_api->find(txn, &key, &val, 0);
568
	free_key(&key);
569 570
	if (ret == KNOT_EOK) {
		if (val.len == sizeof(uint64_t)) {
571
			*salt_created = (knot_time_t)be64toh(*(uint64_t *)val.data);
572 573 574 575 576 577
		}
		else {
			ret = KNOT_EMALF;
		}
	}
	txn_check(NULL);
578
	key = make_key(KASPDBKEY_NSEC3SALT, zone_name, NULL);
579
	ret = db_api->find(txn, &key, &val, 0);
580
	free_key(&key);
581 582 583 584 585 586 587 588 589 590 591 592
	if (ret == KNOT_EOK) {
		nsec3salt->data = malloc(val.len);
		if (nsec3salt->data == NULL) {
			ret = KNOT_ENOMEM;
		} else {
			nsec3salt->size = val.len;
			memcpy(nsec3salt->data, val.data, val.len);
		}
	}
	with_txn_end(NULL);
	return ret;
}
593

594 595
int kasp_db_store_serial(kasp_db_t *db, const knot_dname_t *zone_name,
			 kaspdb_serial_t serial_type, uint32_t serial)
596 597 598 599
{
	if (db == NULL || db->keys_db == NULL || zone_name == NULL) {
		return KNOT_EINVAL;
	}
600 601

	uint32_t be = htobe32(serial);
602
	with_txn(KEYS_RW, NULL);
603 604
	knot_db_val_t key = make_key((keyclass_t)serial_type, zone_name, NULL);
	knot_db_val_t val = { .len = sizeof(uint32_t), .data = &be };
605 606 607 608 609 610
	ret = db_api->insert(txn, &key, &val, 0);
	free_key(&key);
	with_txn_end(NULL);
	return ret;
}

611 612
int kasp_db_load_serial(kasp_db_t *db, const knot_dname_t *zone_name,
			kaspdb_serial_t serial_type, uint32_t *serial)
613
{
614
	if (db == NULL || db->keys_db == NULL || zone_name == NULL || serial == NULL) {
615 616 617 618
		return KNOT_EINVAL;
	}

	with_txn(KEYS_RO, NULL);
619
	knot_db_val_t key = make_key((keyclass_t)serial_type, zone_name, NULL), val = { 0 };
620 621 622 623
	ret = db_api->find(txn, &key, &val, 0);
	free_key(&key);
	if (ret == KNOT_EOK) {
		if (val.len == sizeof(uint32_t)) {
624 625
			*serial = be32toh(*(uint32_t *)val.data);
		} else {
626 627 628 629 630 631 632
			ret = KNOT_EMALF;
		}
	}
	with_txn_end(NULL);
	return ret;
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
int kasp_db_get_policy_last(kasp_db_t *db, const char *policy_string, knot_dname_t **lp_zone,
			    char **lp_keyid)
{
	if (db == NULL || db->keys_db == NULL || policy_string == NULL ||
	    lp_zone == NULL || lp_keyid == NULL) {
		return KNOT_EINVAL;
	}

	with_txn(KEYS_RO, NULL);
	knot_db_val_t key = make_key(KASPDBKEY_POLICYLAST, NULL, policy_string), val = { 0 };
	ret = db_api->find(txn, &key, &val, 0);
	free_key(&key);
	if (ret == KNOT_EOK) {
		if (*(uint8_t *)val.data != KASPDBKEY_PARAMS) {
			ret = KNOT_EMALF;
		} else {
			*lp_zone = knot_dname_copy((knot_dname_t *)(val.data + 1), NULL);
			*lp_keyid = keyid_fromkey(&val);
			if (*lp_zone == NULL || *lp_keyid == NULL) {
				free(*lp_zone);
				free(*lp_keyid);
				ret = KNOT_ENOMEM;
			}
		}
	}
	with_txn_end(NULL);
	return ret;
}

int kasp_db_set_policy_last(kasp_db_t *db, const char *policy_string, const char *last_lp_keyid,
			    const knot_dname_t *new_lp_zone, const char *new_lp_keyid)
{
	if (db == NULL || db->keys_db == NULL ||
	    new_lp_zone == NULL || new_lp_keyid == NULL) {
		return KNOT_EINVAL;
	}
	with_txn(KEYS_RW, NULL);
	knot_db_val_t key = make_key(KASPDBKEY_POLICYLAST, NULL, policy_string), val = { 0 };
	ret = db_api->find(txn, &key, &val, 0);
	switch (ret) {
	case KNOT_EOK:
		if (*(uint8_t *)val.data != KASPDBKEY_PARAMS) {
			ret = KNOT_EMALF;
		} else {
			char *real_last = keyid_fromkey(&val);
			if (real_last == NULL) {
				ret = KNOT_ENOMEM;
			} else {
				if (last_lp_keyid == NULL || strcmp(real_last, last_lp_keyid) != 0) {
					ret = KNOT_ESEMCHECK;
				}
				free(real_last);
			}
		}
		break;
	case KNOT_ENOENT:
		ret = KNOT_EOK;
		break;
	}
	if (ret == KNOT_EOK) {
		val = make_key(KASPDBKEY_PARAMS, new_lp_zone, new_lp_keyid);
		ret = db_api->insert(txn, &key, &val, 0);
		free(val.data);
	}
	free(key.data);
	with_txn_end(NULL);
	return ret;
}
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

int kasp_db_list_zones(kasp_db_t *db, list_t *dst)
{
	if (db == NULL || db->keys_db == NULL || dst == NULL) {
		return KNOT_EINVAL;
	}

	with_txn(KEYS_RO, NULL);
	knot_db_iter_t *iter = db_api->iter_begin(txn, KNOT_DB_FIRST);
	while (iter != NULL) {
		knot_db_val_t key;
		if (db_api->iter_key(iter, &key) == KNOT_EOK && key.len > 1 &&
		    *(uint8_t *)key.data != KASPDBKEY_POLICYLAST) {
			// obtain a domain name of a record in KASP db
			knot_dname_t *key_dn = (knot_dname_t *)(key.data + 1);
			// check if not already in dst
			ptrnode_t *n;
			WALK_LIST(n, *dst) {
				knot_dname_t *exist_dn = (knot_dname_t *)n->d;
				if (knot_dname_cmp(key_dn, exist_dn) == 0) {
					key_dn = NULL;
					break;
				}
			}
			// copy it from txn and add to dst
			if (key_dn != NULL) {
				knot_dname_t *add_dn = knot_dname_copy(key_dn, NULL);
Daniel Salzman's avatar
Daniel Salzman committed
728 729 730 731
				if (add_dn == NULL) {
					ret = KNOT_ENOMEM;
					break;
				}
732 733 734 735 736 737 738 739 740 741 742 743 744 745
				ptrlist_add(dst, add_dn, NULL);
			}
		}
		iter = db_api->iter_next(iter);
	}
	db_api->iter_finish(iter);
	db_api->txn_abort(txn);

	if (ret != KNOT_EOK) {
		ptrlist_deep_free(dst, NULL);
		return ret;
	}
	return (EMPTY_LIST(*dst) ? KNOT_ENOENT : KNOT_EOK);
}