Commit f821295a authored by Libor Peltan's avatar Libor Peltan Committed by Daniel Salzman

kasp_db: use new LMDB interlayer + improvements

parent 83688ddd
......@@ -379,7 +379,7 @@ static int zone_ksk_sbm_confirm(zone_t *zone, ctl_args_t *args)
kdnssec_ctx_t ctx = { 0 };
int ret = kdnssec_ctx_init(conf(), &ctx, zone->name, NULL);
int ret = kdnssec_ctx_init(conf(), &ctx, zone->name, zone->kaspdb, NULL);
if (ret != KNOT_EOK) {
return ret;
}
......@@ -1065,14 +1065,13 @@ static int orphans_purge(ctl_args_t *args)
// Purge KASP DB.
if (only_orphan || MATCH_AND_FILTER(args, CTL_FILTER_PURGE_KASPDB)) {
list_t zones;
init_list(&zones);
if (kasp_db_open(*kaspdb()) == KNOT_EOK &&
kasp_db_list_zones(*kaspdb(), &zones) == KNOT_EOK) {
if (knot_lmdb_open(&args->server->kaspdb) == KNOT_EOK &&
kasp_db_list_zones(&args->server->kaspdb, &zones) == KNOT_EOK) {
ptrnode_t *zn;
WALK_LIST(zn, zones) {
knot_dname_t *zone_name = (knot_dname_t *)zn->d;
if (!zone_exists(zone_name, args->server->zone_db)) {
(void)kasp_db_delete_all(*kaspdb(), zone_name);
if (!zone_exists(zone_name, &args->server->zone_db)) {
(void)kasp_db_delete_all(&args->server->kaspdb, zone_name);
}
knot_dname_free(zone_name, NULL);
}
......@@ -1108,8 +1107,8 @@ static int orphans_purge(ctl_args_t *args)
if (!zone_exists(zone_name, args->server->zone_db)) {
// Purge KASP DB.
if (only_orphan || MATCH_AND_FILTER(args, CTL_FILTER_PURGE_KASPDB)) {
if (kasp_db_open(*kaspdb()) == KNOT_EOK) {
(void) kasp_db_delete_all(*kaspdb(), zone_name);
if (knot_lmdb_open(&args->server->kaspdb) == KNOT_EOK) {
(void)kasp_db_delete_all(&args->server->kaspdb, zone_name);
}
}
......@@ -1171,8 +1170,8 @@ static int zone_purge(zone_t *zone, ctl_args_t *args)
// Purge KASP DB.
if (MATCH_OR_FILTER(args, CTL_FILTER_PURGE_KASPDB)) {
if (kasp_db_open(*kaspdb()) == KNOT_EOK) {
(void)kasp_db_delete_all(*kaspdb(), zone->name);
if (knot_lmdb_open(zone->kaspdb) == KNOT_EOK) {
(void)kasp_db_delete_all(zone->kaspdb, zone->name);
}
}
......
......@@ -126,7 +126,7 @@ static void policy_load(knot_kasp_policy_t *policy, conf_val_t *id)
}
int kdnssec_ctx_init(conf_t *conf, kdnssec_ctx_t *ctx, const knot_dname_t *zone_name,
const conf_mod_id_t *from_module)
knot_lmdb_db_t *kaspdb, const conf_mod_id_t *from_module)
{
if (ctx == NULL || zone_name == NULL) {
return KNOT_EINVAL;
......@@ -141,14 +141,14 @@ int kdnssec_ctx_init(conf_t *conf, kdnssec_ctx_t *ctx, const knot_dname_t *zone_
ret = KNOT_ENOMEM;
goto init_error;
}
ctx->kasp_db = kaspdb();
ret = kasp_db_open(*ctx->kasp_db);
ctx->kasp_db = kaspdb;
ret = knot_lmdb_open(ctx->kasp_db);
if (ret != KNOT_EOK) {
goto init_error;
}
ret = kasp_zone_load(ctx->zone, zone_name, *ctx->kasp_db);
ret = kasp_zone_load(ctx->zone, zone_name, ctx->kasp_db);
if (ret != KNOT_EOK) {
goto init_error;
}
......@@ -204,7 +204,7 @@ int kdnssec_ctx_commit(kdnssec_ctx_t *ctx)
// do something with keytore? Probably not..
return kasp_zone_save(ctx->zone, ctx->zone->dname, *ctx->kasp_db);
return kasp_zone_save(ctx->zone, ctx->zone->dname, ctx->kasp_db);
}
void kdnssec_ctx_deinit(kdnssec_ctx_t *ctx)
......
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -30,7 +30,7 @@
typedef struct {
knot_time_t now;
kasp_db_t **kasp_db;
knot_lmdb_db_t *kasp_db;
knot_kasp_zone_t *zone;
knot_kasp_policy_t *policy;
dnssec_keystore_t *keystore;
......@@ -49,10 +49,11 @@ typedef struct {
* \param conf Configuration.
* \param ctx Signing context to be initialized.
* \param zone_name Name of the zone.
* \param kaspdb Key and signature policy database.
* \param from_module Module identifier if initialized from a module.
*/
int kdnssec_ctx_init(conf_t *conf, kdnssec_ctx_t *ctx, const knot_dname_t *zone_name,
const conf_mod_id_t *from_module);
knot_lmdb_db_t *kaspdb, const conf_mod_id_t *from_module);
/*!
* \brief Save the changes in ctx (in kasp zone).
......
This diff is collapsed.
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -23,6 +23,7 @@
#include "libknot/db/db_lmdb.h"
#include "libknot/dname.h"
#include "knot/dnssec/kasp/policy.h"
#include "knot/journal/knot_lmdb.h"
typedef struct kasp_db kasp_db_t;
......@@ -31,56 +32,6 @@ typedef enum { // the enum values MUST match those from keyclass_t !!
KASPDB_SERIAL_LASTSIGNED = 0x6,
} kaspdb_serial_t;
/*!
* \brief Returns kasp_db_t singleton, to be used for signing all zones.
*
* De/initialized with server_t, used in zone contents signing context.
*/
kasp_db_t **kaspdb(void);
/*!
* \brief Initialize kasp_db_t, prepare to simple open on-demand.
*
* \param db structure to initialize
* \param path path to the LMDB directory (will be created)
* \param mapsize LMDB map size
*
* \return KNOT_E*
*/
int kasp_db_init(kasp_db_t **db, const char *path, size_t mapsize);
/*!
* \brief Re-initialize kasp_db_t if not already open.
*
* \param db structure to initialize
* \param new_path new path to LMDB
* \param new_mapsize new LMDB map size
*
* \retval KNOT_EBUSY can't reconfigure DB path because already open
* \retval KNOT_EEXIST can't reconfigure mapsize because already open
* \retval KNOT_ENODIFF already open, but no change needed => OK
* \retval KNOT_EINVAL, KNOT_ENOMEM, etc. standard errors
* \return KNOT_EOK reconfigured successfully
*/
int kasp_db_reconfigure(kasp_db_t **db, const char *new_path, size_t new_mapsize);
/*!
* \brief Determine if kasp_db possibly exists at all.
*
* This is useful to avoid creating kasp_db by opening it just to check if anything is there.
*/
bool kasp_db_exists(kasp_db_t *db);
/*!
* \brief Perform real ctreate/open of KASP db.
*/
int kasp_db_open(kasp_db_t *db);
/*!
* \brief Close KASP db if open and free the structure.
*/
void kasp_db_close(kasp_db_t **db);
/*!
* \brief For given zone, list all keys (their IDs) belonging to it.
*
......@@ -90,7 +41,7 @@ void kasp_db_close(kasp_db_t **db);
*
* \return KNOT_E* (KNOT_ENOENT if no keys)
*/
int kasp_db_list_keys(kasp_db_t *db, const knot_dname_t *zone_name, list_t *dst);
int kasp_db_list_keys(knot_lmdb_db_t *db, const knot_dname_t *zone_name, list_t *dst);
/*!
* \brief Remove a key from zone. Delete the key if no zone has it anymore.
......@@ -102,7 +53,7 @@ int kasp_db_list_keys(kasp_db_t *db, const knot_dname_t *zone_name, list_t *dst)
*
* \return KNOT_E*
*/
int kasp_db_delete_key(kasp_db_t *db, const knot_dname_t *zone_name, const char *key_id, bool *still_used);
int kasp_db_delete_key(knot_lmdb_db_t *db, const knot_dname_t *zone_name, const char *key_id, bool *still_used);
/*!
* \brief Remove all zone's keys from DB, including nsec3param
......@@ -111,7 +62,7 @@ int kasp_db_delete_key(kasp_db_t *db, const knot_dname_t *zone_name, const char
*
* \return KNOT_E*
*/
int kasp_db_delete_all(kasp_db_t *db, const knot_dname_t *zone_name);
int kasp_db_delete_all(knot_lmdb_db_t *db, const knot_dname_t *zone_name);
/*!
* \brief Add a key to the DB (possibly overwrite) and link it to a zone.
......@@ -126,7 +77,7 @@ int kasp_db_delete_all(kasp_db_t *db, const knot_dname_t *zone_name);
*
* \return KNOT_E*
*/
int kasp_db_add_key(kasp_db_t *db, const knot_dname_t *zone_name, const key_params_t *params);
int kasp_db_add_key(knot_lmdb_db_t *db, const knot_dname_t *zone_name, const key_params_t *params);
/*!
* \brief Link a key from another zone.
......@@ -138,7 +89,7 @@ int kasp_db_add_key(kasp_db_t *db, const knot_dname_t *zone_name, const key_para
*
* \return KNOT_E*
*/
int kasp_db_share_key(kasp_db_t *db, const knot_dname_t *zone_from,
int kasp_db_share_key(knot_lmdb_db_t *db, const knot_dname_t *zone_from,
const knot_dname_t *zone_to, const char *key_id);
/*!
......@@ -151,7 +102,7 @@ int kasp_db_share_key(kasp_db_t *db, const knot_dname_t *zone_from,
*
* \return KNOT_E*
*/
int kasp_db_store_nsec3salt(kasp_db_t *db, const knot_dname_t *zone_name,
int kasp_db_store_nsec3salt(knot_lmdb_db_t *db, const knot_dname_t *zone_name,
const dnssec_binary_t *nsec3salt, knot_time_t salt_created);
/*!
......@@ -164,7 +115,7 @@ int kasp_db_store_nsec3salt(kasp_db_t *db, const knot_dname_t *zone_name,
*
* \return KNOT_E* (KNOT_ENOENT if not stored before)
*/
int kasp_db_load_nsec3salt(kasp_db_t *db, const knot_dname_t *zone_name,
int kasp_db_load_nsec3salt(knot_lmdb_db_t *db, const knot_dname_t *zone_name,
dnssec_binary_t *nsec3salt, knot_time_t *salt_created);
/*!
......@@ -177,7 +128,7 @@ int kasp_db_load_nsec3salt(kasp_db_t *db, const knot_dname_t *zone_name,
*
* \return KNOT_E*
*/
int kasp_db_store_serial(kasp_db_t *db, const knot_dname_t *zone_name,
int kasp_db_store_serial(knot_lmdb_db_t *db, const knot_dname_t *zone_name,
kaspdb_serial_t serial_type, uint32_t serial);
/*!
......@@ -190,7 +141,7 @@ int kasp_db_store_serial(kasp_db_t *db, const knot_dname_t *zone_name,
*
* \return KNOT_E* (KNOT_ENOENT if not stored before)
*/
int kasp_db_load_serial(kasp_db_t *db, const knot_dname_t *zone_name,
int kasp_db_load_serial(knot_lmdb_db_t *db, const knot_dname_t *zone_name,
kaspdb_serial_t serial_type, uint32_t *serial);
/*!
......@@ -203,8 +154,8 @@ int kasp_db_load_serial(kasp_db_t *db, const knot_dname_t *zone_name,
*
* \return KNOT_E*
*/
int kasp_db_get_policy_last(kasp_db_t *db, const char *policy_string, knot_dname_t **lp_zone,
char **lp_keyid);
int kasp_db_get_policy_last(knot_lmdb_db_t *db, const char *policy_string,
knot_dname_t **lp_zone, char **lp_keyid);
/*!
* \brief For given policy name, try to reset last generated key.
......@@ -220,7 +171,7 @@ int kasp_db_get_policy_last(kasp_db_t *db, const char *policy_string, knot_dname
* \retval KNOT_EOK policy-last key set up successfully to given zone/ID
* \return KNOT_E* common error
*/
int kasp_db_set_policy_last(kasp_db_t *db, const char *policy_string, const char *last_lp_keyid,
int kasp_db_set_policy_last(knot_lmdb_db_t *db, const char *policy_string, const char *last_lp_keyid,
const knot_dname_t *new_lp_zone, const char *new_lp_keyid);
/*!
......@@ -233,7 +184,7 @@ int kasp_db_set_policy_last(kasp_db_t *db, const char *policy_string, const char
*
* \return KNOT_E*
*/
int kasp_db_list_zones(kasp_db_t *db, list_t *dst);
int kasp_db_list_zones(knot_lmdb_db_t *db, list_t *dst);
/*!
* \brief Store pre-generated records for offline KSK usage.
......@@ -244,7 +195,7 @@ int kasp_db_list_zones(kasp_db_t *db, list_t *dst);
*
* \return KNOT_E*
*/
int kasp_db_store_offline_records(kasp_db_t *db, knot_time_t for_time, const key_records_t *r);
int kasp_db_store_offline_records(knot_lmdb_db_t *db, knot_time_t for_time, const key_records_t *r);
/*!
* \brief Load pregenerated records for offline signing.
......@@ -257,7 +208,7 @@ int kasp_db_store_offline_records(kasp_db_t *db, knot_time_t for_time, const key
*
* \return KNOT_E*
*/
int kasp_db_load_offline_records(kasp_db_t *db, const knot_dname_t *for_dname,
int kasp_db_load_offline_records(knot_lmdb_db_t *db, const knot_dname_t *for_dname,
knot_time_t for_time, knot_time_t *next_time,
key_records_t *r);
......@@ -271,5 +222,5 @@ int kasp_db_load_offline_records(kasp_db_t *db, const knot_dname_t *for_dname,
*
* \return KNOT_E*
*/
int kasp_db_delete_offline_records(kasp_db_t *db, const knot_dname_t *zone,
int kasp_db_delete_offline_records(knot_lmdb_db_t *db, const knot_dname_t *zone,
knot_time_t from_time, knot_time_t to_time);
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -140,7 +140,7 @@ static void kaspkey2params(knot_kasp_key_t *key, key_params_t *params)
int kasp_zone_load(knot_kasp_zone_t *zone,
const knot_dname_t *zone_name,
kasp_db_t *kdb)
knot_lmdb_db_t *kdb)
{
if (zone == NULL || zone_name == NULL || kdb == NULL) {
return KNOT_EINVAL;
......@@ -225,7 +225,7 @@ int kasp_zone_append(knot_kasp_zone_t *zone, const knot_kasp_key_t *appkey)
int kasp_zone_save(const knot_kasp_zone_t *zone,
const knot_dname_t *zone_name,
kasp_db_t *kdb)
knot_lmdb_db_t *kdb)
{
if (zone == NULL || zone_name == NULL || kdb == NULL) {
return KNOT_EINVAL;
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -30,11 +30,11 @@ typedef struct {
int kasp_zone_load(knot_kasp_zone_t *zone,
const knot_dname_t *zone_name,
kasp_db_t *kdb);
knot_lmdb_db_t *kdb);
int kasp_zone_save(const knot_kasp_zone_t *zone,
const knot_dname_t *zone_name,
kasp_db_t *kdb);
knot_lmdb_db_t *kdb);
int kasp_zone_append(knot_kasp_zone_t *zone,
const knot_kasp_key_t *appkey);
......
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -121,7 +121,7 @@ static int share_or_generate_key(kdnssec_ctx_t *ctx, kdnssec_generate_flags_t fl
return KNOT_EINVAL;
} // for now not designed for rotating shared ZSK
int ret = kasp_db_get_policy_last(*ctx->kasp_db, ctx->policy->string,
int ret = kasp_db_get_policy_last(ctx->kasp_db, ctx->policy->string,
&borrow_zone, &borrow_key);
if (ret != KNOT_EOK && ret != KNOT_ENOENT) {
return ret;
......@@ -146,7 +146,7 @@ static int share_or_generate_key(kdnssec_ctx_t *ctx, kdnssec_generate_flags_t fl
return ret;
}
ret = kasp_db_set_policy_last(*ctx->kasp_db, ctx->policy->string,
ret = kasp_db_set_policy_last(ctx->kasp_db, ctx->policy->string,
borrow_key, ctx->zone->dname, key->id);
free(borrow_zone);
free(borrow_key);
......@@ -165,7 +165,7 @@ static int share_or_generate_key(kdnssec_ctx_t *ctx, kdnssec_generate_flags_t fl
return ret;
}
ret = kasp_db_get_policy_last(*ctx->kasp_db, ctx->policy->string,
ret = kasp_db_get_policy_last(ctx->kasp_db, ctx->policy->string,
&borrow_zone, &borrow_key);
}
}
......
......@@ -30,14 +30,14 @@
#include "knot/zone/adjust.h"
static int sign_init(zone_contents_t *zone, zone_sign_flags_t flags, zone_sign_roll_flags_t roll_flags,
kdnssec_ctx_t *ctx, zone_sign_reschedule_t *reschedule)
knot_lmdb_db_t *kaspdb, kdnssec_ctx_t *ctx, zone_sign_reschedule_t *reschedule)
{
assert(zone);
assert(ctx);
const knot_dname_t *zone_name = zone->apex->owner;
int r = kdnssec_ctx_init(conf(), ctx, zone_name, NULL);
int r = kdnssec_ctx_init(conf(), ctx, zone_name, kaspdb, NULL);
if (r != KNOT_EOK) {
return r;
}
......@@ -155,7 +155,7 @@ int knot_dnssec_zone_sign(zone_update_t *update,
// signing pipeline
result = sign_init(update->new_cont, flags, roll_flags, &ctx, reschedule);
result = sign_init(update->new_cont, flags, roll_flags, update->zone->kaspdb, &ctx, reschedule);
if (result != KNOT_EOK) {
log_zone_error(zone_name, "DNSSEC, failed to initialize (%s)",
knot_strerror(result));
......@@ -246,7 +246,7 @@ int knot_dnssec_sign_update(zone_update_t *update, zone_sign_reschedule_t *resch
// signing pipeline
result = sign_init(update->new_cont, 0, 0, &ctx, reschedule);
result = sign_init(update->new_cont, 0, 0, update->zone->kaspdb, &ctx, reschedule);
if (result != KNOT_EOK) {
log_zone_error(zone_name, "DNSSEC, failed to initialize (%s)",
knot_strerror(result));
......
......@@ -129,14 +129,14 @@ int kdnssec_share_key(kdnssec_ctx_t *ctx, const knot_dname_t *from_zone, const c
return ret;
}
ret = kasp_db_share_key(*ctx->kasp_db, from_zone, ctx->zone->dname, key_id);
ret = kasp_db_share_key(ctx->kasp_db, from_zone, ctx->zone->dname, key_id);
if (ret != KNOT_EOK) {
free(to_zone);
return ret;
}
kasp_zone_clear(ctx->zone);
ret = kasp_zone_load(ctx->zone, to_zone, *ctx->kasp_db);
ret = kasp_zone_load(ctx->zone, to_zone, ctx->kasp_db);
free(to_zone);
return ret;
}
......@@ -155,7 +155,7 @@ int kdnssec_delete_key(kdnssec_ctx_t *ctx, knot_kasp_key_t *key_ptr)
}
bool key_still_used_in_keystore = false;
int ret = kasp_db_delete_key(*ctx->kasp_db, ctx->zone->dname, key_ptr->id, &key_still_used_in_keystore);
int ret = kasp_db_delete_key(ctx->kasp_db, ctx->zone->dname, key_ptr->id, &key_still_used_in_keystore);
if (ret != KNOT_EOK) {
return ret;
}
......
......@@ -904,7 +904,7 @@ int knot_zone_sign_update_dnskeys(zone_update_t *update,
key_records_init(dnssec_ctx, &add_r);
if (dnssec_ctx->policy->offline_ksk) {
ret = kasp_db_load_offline_records(*dnssec_ctx->kasp_db, apex->owner, dnssec_ctx->now, next_resign, &add_r);
ret = kasp_db_load_offline_records(dnssec_ctx->kasp_db, apex->owner, dnssec_ctx->now, next_resign, &add_r);
if (ret == KNOT_EOK) {
log_zone_info(dnssec_ctx->zone->dname, "DNSSEC, using offline DNSKEY RRSIG");
} else {
......
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -24,7 +24,7 @@ int event_nsec3resalt(conf_t *conf, zone_t *zone)
kdnssec_ctx_t kctx = { 0 };
int ret = kdnssec_ctx_init(conf, &kctx, zone->name, NULL);
int ret = kdnssec_ctx_init(conf, &kctx, zone->name, zone->kaspdb, NULL);
if (ret != KNOT_EOK) {
return ret;
}
......
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2019 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
......@@ -21,7 +21,7 @@ int event_parent_ds_q(conf_t *conf, zone_t *zone)
{
kdnssec_ctx_t ctx = { 0 };
int ret = kdnssec_ctx_init(conf, &ctx, zone->name, NULL);
int ret = kdnssec_ctx_init(conf, &ctx, zone->name, zone->kaspdb, NULL);
if (ret != KNOT_EOK) {
return ret;
}
......
......@@ -164,8 +164,11 @@ void query_module_close(knotd_mod_t *module)
zone_sign_ctx_free(module->sign_ctx);
free_zone_keys(module->keyset);
free(module->keyset);
kdnssec_ctx_deinit(module->dnssec);
free(module->dnssec);
if (module->dnssec != NULL) {
knot_lmdb_deinit(module->dnssec->kasp_db);
kdnssec_ctx_deinit(module->dnssec);
free(module->dnssec);
}
free(module);
}
......@@ -566,17 +569,25 @@ knot_rrset_t knotd_qdata_zone_apex_rrset(knotd_qdata_t *qdata, uint16_t type)
_public_
int knotd_mod_dnssec_init(knotd_mod_t *mod)
{
if (mod == NULL) {
if (mod == NULL || mod->dnssec != NULL) {
return KNOT_EINVAL;
}
mod->dnssec = calloc(1, sizeof(*(mod->dnssec)));
knot_lmdb_db_t *kaspdb;
mod->dnssec = calloc(1, sizeof(*(mod->dnssec)) + sizeof(*kaspdb));
if (mod->dnssec == NULL) {
return KNOT_ENOMEM;
}
kaspdb = (knot_lmdb_db_t *)(mod->dnssec + 1);
char *kasp_dir = conf_kaspdir(mod->config);
conf_val_t kasp_size = conf_default_get(mod->config, C_MAX_KASP_DB_SIZE);
knot_lmdb_init(kaspdb, kasp_dir, conf_int(&kasp_size), 0, "keys_db");
free(kasp_dir);
conf_val_t conf = conf_zone_get(mod->config, C_DNSSEC_SIGNING, mod->zone);
int ret = kdnssec_ctx_init(mod->config, mod->dnssec, mod->zone,
int ret = kdnssec_ctx_init(mod->config, mod->dnssec, mod->zone, kaspdb,
conf_bool(&conf) ? NULL : mod->id);
if (ret != KNOT_EOK) {
free(mod->dnssec);
......
......@@ -421,18 +421,13 @@ int server_init(server_t *server, int bg_workers)
char *journal_dir = conf_journalfile(conf());
conf_val_t journal_size = conf_default_get(conf(), C_MAX_JOURNAL_DB_SIZE);
conf_val_t journal_mode = conf_default_get(conf(), C_JOURNAL_DB_MODE);
knot_lmdb_init(&server->journaldb, journal_dir, conf_int(&journal_size), journal_env_flags(conf_opt(&journal_mode)));
knot_lmdb_init(&server->journaldb, journal_dir, conf_int(&journal_size), journal_env_flags(conf_opt(&journal_mode)), NULL);
free(journal_dir);
char *kasp_dir = conf_kaspdir(conf());
conf_val_t kasp_size = conf_default_get(conf(), C_MAX_KASP_DB_SIZE);
int ret = kasp_db_init(kaspdb(), kasp_dir, conf_int(&kasp_size));
knot_lmdb_init(&server->kaspdb, kasp_dir, conf_int(&kasp_size), 0, "keys_db");
free(kasp_dir);
if (ret != KNOT_EOK) {
worker_pool_destroy(server->workers);
evsched_deinit(&server->sched);
return ret;
}
return KNOT_EOK;
}
......@@ -462,7 +457,7 @@ void server_deinit(server_t *server)
evsched_deinit(&server->sched);
/* Close kasp_db. */
kasp_db_close(kaspdb());
knot_lmdb_deinit(&server->kaspdb);
/* Close journal database if open. */
knot_lmdb_deinit(&server->journaldb);
......@@ -765,25 +760,13 @@ static int reconfigure_kasp_db(conf_t *conf, server_t *server)
{
char *kasp_dir = conf_kaspdir(conf);
conf_val_t kasp_size = conf_default_get(conf, C_MAX_KASP_DB_SIZE);
int ret = kasp_db_reconfigure(kaspdb(), kasp_dir, conf_int(&kasp_size));
switch (ret) {
case KNOT_EBUSY:
log_warning("ignored reconfiguration of KASP DB path (already open)");
break;
case KNOT_EEXIST:
ret = KNOT_EBUSY;
log_warning("ignored reconfiguration of KASP DB max size (already open)");
break;
case KNOT_ENODIFF:
case KNOT_EOK:
ret = KNOT_EOK;
break;
default:
break;
int ret = knot_lmdb_reinit(&server->kaspdb, kasp_dir, conf_int(&kasp_size), 0);
if (ret != KNOT_EOK) {
log_warning("ignored reconfiguration of KASP DB (%s)", knot_strerror(ret));
}
free(kasp_dir);
return ret;
return KNOT_EOK; // not "ret"
}
void server_reconfigure(conf_t *conf, server_t *server)
......
......@@ -86,6 +86,7 @@ typedef struct server {
knot_zonedb_t *zone_db;
knot_db_t *timers_db;
knot_lmdb_db_t journaldb;
knot_lmdb_db_t kaspdb;
/*! \brief I/O handlers. */
struct {
......
......@@ -490,24 +490,12 @@ int zone_dump_to_dir(conf_t *conf, zone_t *zone, const char *dir)
int zone_set_master_serial(zone_t *zone, uint32_t serial)
{
int ret = kasp_db_open(*kaspdb());
if (ret == KNOT_EOK) {
ret = kasp_db_store_serial(*kaspdb(), zone->name, KASPDB_SERIAL_MASTER, serial);
}
return ret;
return kasp_db_store_serial(zone->kaspdb, zone->name, KASPDB_SERIAL_MASTER, serial);
}
int zone_get_master_serial(zone_t *zone, uint32_t *serial)
{
if (!kasp_db_exists(*kaspdb())) {
*serial = zone_contents_serial(zone->contents);
return KNOT_EOK;
}
int ret = kasp_db_open(*kaspdb());
if (ret != KNOT_EOK) {
return ret;
}
ret = kasp_db_load_serial(*kaspdb(), zone->name, KASPDB_SERIAL_MASTER, serial);
int ret = kasp_db_load_serial(zone->kaspdb, zone->name, KASPDB_SERIAL_MASTER, serial);
if (ret == KNOT_ENOENT) {
*serial = zone_contents_serial(zone->contents);
return KNOT_EOK;
......@@ -517,21 +505,10 @@ int zone_get_master_serial(zone_t *zone, uint32_t *serial)
int zone_set_lastsigned_serial(zone_t *zone, uint32_t serial)
{
int ret = kasp_db_open(*kaspdb());
if (ret == KNOT_EOK) {
ret = kasp_db_store_serial(*kaspdb(), zone->name, KASPDB_SERIAL_LASTSIGNED, serial);
}
return ret;
return kasp_db_store_serial(zone->kaspdb, zone->name, KASPDB_SERIAL_LASTSIGNED, serial);
}
bool zone_get_lastsigned_serial(zone_t *zone, uint32_t *serial)
{
if (!kasp_db_exists(*kaspdb())) {
return false;
}
int ret = kasp_db_open(*kaspdb());
if (ret == KNOT_EOK) {
ret = kasp_db_load_serial(*kaspdb(), zone->name, KASPDB_SERIAL_LASTSIGNED, serial);
}
return (ret == KNOT_EOK);
return kasp_db_load_serial(zone->kaspdb, zone->name, KASPDB_SERIAL_LASTSIGNED, serial) == KNOT_EOK;
}
......@@ -77,6 +77,9 @@ typedef struct zone
/*! \brief Ptr to journal DB (in struct server) */
knot_lmdb_db_t *journaldb;
/*! \brief Ptr to journal DB (in struct server) */
knot_lmdb_db_t *kaspdb;
/*! \brief Preferred master lock. */
pthread_mutex_t preferred_lock;
/*! \brief Preferred master for remote operation. */
......
......@@ -51,6 +51,7 @@ static zone_t *create_zone_from(const knot_dname_t *name, server_t *server)
}
zone->journaldb = &server->journaldb;
zone->kaspdb = &server->kaspdb;
int result = zone_events_setup(zone, server->workers, &server->sched,
server->timers_db);
......
......@@ -243,14 +243,14 @@ int keymgr_generate_key(kdnssec_ctx_t *ctx, int argc, char *argv[])
char *last_policy_last = NULL;
knot_dname_t *unused = NULL;
ret = kasp_db_get_policy_last(*ctx->kasp_db, addtopolicy, &unused,
ret = kasp_db_get_policy_last(ctx->kasp_db, addtopolicy, &unused,
&last_policy_last);
knot_dname_free(unused, NULL);
if (ret != KNOT_EOK && ret != KNOT_ENOENT) {
return ret;
}
ret = kasp_db_set_policy_last(*ctx->kasp_db, addtopolicy, last_policy_last,
ret = kasp_db_set_policy_last(ctx->kasp_db, addtopolicy, last_policy_last,
ctx->zone->dname, key->id);
free(last_policy_last);
if (ret != KNOT_EOK) {
......@@ -570,7 +570,7 @@ int keymgr_nsec3_salt_print(kdnssec_ctx_t *ctx)
{
dnssec_binary_t salt_bin;
knot_time_t created;
int ret = kasp_db_load_nsec3salt(*ctx->kasp_db, ctx->zone->dname,
int ret = kasp_db_load_nsec3salt(ctx->kasp_db, ctx->zone->dname,
&salt_bin, &created);
switch (ret) {
case KNOT_EOK:
......@@ -608,7 +608,7 @@ int keymgr_nsec3_salt_set(kdnssec_ctx_t *ctx, const char *new_salt)
"salt length (%d).\n",
(int)ctx->policy->nsec3_salt_length);
}
int ret = kasp_db_store_nsec3salt(*ctx->kasp_db, ctx->zone->dname,
int ret = kasp_db_store_nsec3salt(ctx->kasp_db, ctx->zone->dname,
&salt_bin, knot_time());
if (salt_bin.size > 0) {
free(salt_bin.data);
......@@ -747,7 +747,7 @@ int keymgr_get_key(kdnssec_ctx_t *ctx, const char *key_spec, knot_kasp_key_t **k
return KNOT_EOK;
}
int keymgr_foreign_key_id(char *argv[], knot_dname_t **key_zone, char **key_id)
int keymgr_foreign_key_id(char *argv[], knot_lmdb_db_t *kaspdb, knot_dname_t **key_zone, char **key_id)
{
*key_zone = knot_dname_from_str_alloc(argv[0]);
if (*key_zone == NULL) {
......@@ -756,7 +756,7 @@ int keymgr_foreign_key_id(char *argv[], knot_dname_t **key_zone, char **key_id)
knot_dname_to_lower(*key_zone);
kdnssec_ctx_t kctx = { 0 };
int ret = kdnssec_ctx_init(conf(), &kctx, *key_zone, NULL);
int ret = kdnssec_ctx_init(conf(), &kctx, *key_zone, kaspdb, NULL);
if (ret != KNOT_EOK) {
printf("Failed to initialize zone %s (%s)\n", argv[0], knot_strerror(ret));
free(*key_zone);
......