]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
kaspdb: when adding a key, always remove all trash records of the key
authorDavid Vašek <david.vasek@nic.cz>
Fri, 23 Jan 2026 17:29:35 +0000 (18:29 +0100)
committerDavid Vašek <david.vasek@nic.cz>
Wed, 20 May 2026 07:10:38 +0000 (09:10 +0200)
src/knot/dnssec/kasp/kasp_db.c
src/knot/dnssec/kasp/kasp_db.h
src/knot/dnssec/kasp/kasp_zone.c

index 9386d11d5700ee677c653a6dff8a60aecc60a8f3..997b3ae22f7a1b15fcda63b9b84bd85fd83cbab3 100644 (file)
@@ -549,9 +549,20 @@ int kasp_db_list_zones(knot_lmdb_db_t *db, list_t *zones)
 
 int kasp_db_add_key(knot_lmdb_db_t *db, const knot_dname_t *zone_name, const key_params_t *params)
 {
-       MDB_val v = params_serialize(params);
-       MDB_val k = make_key_str(KASPDBKEY_PARAMS, zone_name, params->id);
-       return knot_lmdb_quick_insert(db, k, v);
+       MDB_val val = params_serialize(params);
+       MDB_val key = make_key_str(KASPDBKEY_PARAMS, zone_name, params->id);
+       MDB_val del_prefix = make_key_str(KASPDBKEY_TRASH, NULL, params->id);
+       knot_lmdb_txn_t txn = { 0 };
+       knot_lmdb_begin(db, &txn, true);
+       knot_lmdb_insert(&txn, &key, &val);
+       // More than one KASPDBKEY_TRASH aren't expected, but none must remain.
+       knot_lmdb_del_prefix(&txn, &del_prefix);
+       free(key.mv_data);
+       free(val.mv_data);
+       free(del_prefix.mv_data);
+       knot_lmdb_commit(&txn);
+
+       return txn.ret;
 }
 
 int kasp_db_share_key(knot_lmdb_db_t *db, const knot_dname_t *zone_from,
index 83312a11de8f1009997c6f2b185d301b42a9689c..f4a1a6f0d949e5222686c0a55b092abeaac01406 100644 (file)
@@ -118,10 +118,11 @@ int kasp_db_list_zones(knot_lmdb_db_t *db, list_t *zones);
 
 /*!
  * \brief Add a key to the DB (possibly overwrite) and link it to a zone.
+ *        Remove all trash records related to the key, if there are any.
  *
  * Stores new key with given params into KASP db. If a key with the same ID had been present
  * in KASP db already, its params get silently overwritten by those new params.
- * Moreover, the key ID is linked to the zone.
+ * Moreover, the key ID is linked to the zone and all related trash records are removed.
  *
  * \param db            KASP db
  * \param zone_name     name of the zone the new key shall belong to
index 19765770700fc8123c9eba964188e76d9b114c9f..7f0fa9d2bf90e0e04b04b201346d7345cdb69eb8 100644 (file)
@@ -264,7 +264,8 @@ int kasp_zone_save(const knot_kasp_zone_t *zone,
        for (size_t i = 0; i < zone->num_keys; i++) {
                kaspkey2params(&zone->keys[i], &parm);
 
-               // Force overwrite already existing key-val pairs.
+               // Force overwrite already existing key-val pairs
+               // and remove related trash records.
                int ret = kasp_db_add_key(kdb, zone_name, &parm);
                if (ret != KNOT_EOK) {
                        return ret;