]> the.earth.li Git - onak.git/commitdiff
Change delete_key to use a full fingerprint
authorJonathan McDowell <noodles@earth.li>
Thu, 1 Aug 2019 21:18:24 +0000 (22:18 +0100)
committerJonathan McDowell <noodles@earth.li>
Thu, 1 Aug 2019 21:18:24 +0000 (22:18 +0100)
delete_key was deleting a key based on the keyid, which is
insufficiently precise. Move over to the full fingerprint (even though
with some backends this is an effective no-op for now).

12 files changed:
keyd.c
keydb.h
keydb_db4.c
keydb_dynamic.c
keydb_file.c
keydb_fs.c
keydb_hkp.c
keydb_keyd.c
keydb_keyring.c
keydb_pg.c
keydb_stacked.c
onak.c

diff --git a/keyd.c b/keyd.c
index 9994dea303b812588fe00ff9d18cec5424ce8668..4520b14e99bfa43873801dac89e79eec1530da57 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -444,8 +444,9 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
                                ret = 1;
                        }
                        if (ret == 0) {
-                               bytes = read(fd, &keyid, sizeof(keyid));
-                               if (bytes != sizeof(keyid)) {
+                               bytes = read(fd, &fingerprint,
+                                               sizeof(fingerprint));
+                               if (bytes != sizeof(fingerprint)) {
                                        ret = 1;
                                }
                        }
@@ -455,7 +456,7 @@ static int sock_do(struct onak_dbctx *dbctx, int fd)
                                                ", result: %d",
                                                keyid,
                                                dbctx->delete_key(dbctx,
-                                                       keyid, false));
+                                                       &fingerprint, false));
                        }
                        break;
                case KEYD_CMD_GETFULLKEYID:
diff --git a/keydb.h b/keydb.h
index 05405c334f8b06eca398cf7d19b977d8c897d622..f448215bd3a35719e544a6f74af1b0406edb6915 100644 (file)
--- a/keydb.h
+++ b/keydb.h
@@ -104,13 +104,14 @@ struct onak_dbctx {
 
 /**
  * @brief Given a keyid delete the key from storage.
- * @param keyid The keyid to delete.
+ * @param fp The fingerprint of the key to delete.
  * @param intrans If we're already in a transaction.
  *
  * This function deletes a public key from whatever storage mechanism we
  * are using. Returns 0 if the key existed.
  */
-       int (*delete_key)(struct onak_dbctx *, uint64_t keyid, bool intrans);
+       int (*delete_key)(struct onak_dbctx *, struct openpgp_fingerprint *fp,
+                       bool intrans);
 
 /**
  * @brief Trys to find the keys that contain the supplied text.
index aa2f229c01c3c4fc5f3e6d7dd1bd69b6e3045c18..d2db2c5a39923f6bd7a9c4fe32f415919beb7f02 100644 (file)
@@ -703,14 +703,15 @@ static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
 
 /**
  *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
  *     @intrans: If we're already in a transaction.
  *
  *     This function deletes a public key from whatever storage mechanism we
  *     are using. Returns 0 if the key existed.
  */
 static int db4_delete_key(struct onak_dbctx *dbctx,
-               uint64_t keyid, bool intrans)
+               struct openpgp_fingerprint *fp,
+               bool intrans)
 {
        struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
        struct openpgp_publickey *publickey = NULL;
@@ -729,20 +730,22 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
        struct ll *curword  = NULL;
        bool deadlock = false;
        struct skshash hash;
-       struct openpgp_fingerprint fingerprint;
+       uint64_t keyid;
 
        if (!intrans) {
                db4_starttrans(dbctx);
        }
 
-       if (db4_fetch_key_id(dbctx, keyid, &publickey, true) == 0) {
+       if (db4_fetch_key_fp(dbctx, fp, &publickey, true) == 0) {
                if (!intrans) {
                        db4_endtrans(dbctx);
                }
                return 1;
        }
 
-       get_fingerprint(publickey->publickey, &fingerprint);
+       if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
+               return 1;
+       }
 
        /*
         * Walk through the uids removing the words from the worddb.
@@ -777,8 +780,8 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                        memset(&data, 0, sizeof(data));
                        key.data = curword->object;
                        key.size = strlen(key.data);
-                       data.data = fingerprint.fp;
-                       data.size = fingerprint.length;
+                       data.data = fp->fp;
+                       data.size = fp->length;
 
                        ret = cursor->c_get(cursor,
                                &key,
@@ -832,8 +835,8 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                memset(&data, 0, sizeof(data));
                key.data = &shortkeyid;
                key.size = sizeof(shortkeyid);
-               data.data = fingerprint.fp;
-               data.size = fingerprint.length;
+               data.data = fp->fp;
+               data.size = fp->length;
 
                ret = cursor->c_get(cursor,
                        &key,
@@ -860,8 +863,8 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                memset(&data, 0, sizeof(data));
                key.data = &keyid;
                key.size = sizeof(keyid);
-               data.data = fingerprint.fp;
-               data.size = fingerprint.length;
+               data.data = fp->fp;
+               data.size = fp->length;
 
                ret = cursor64->c_get(cursor64,
                        &key,
@@ -910,8 +913,8 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                        memset(&data, 0, sizeof(data));
                        key.data = &shortkeyid;
                        key.size = sizeof(shortkeyid);
-                       data.data = fingerprint.fp;
-                       data.size = fingerprint.length;
+                       data.data = fp->fp;
+                       data.size = fp->length;
 
                        ret = cursor->c_get(cursor,
                                &key,
@@ -938,8 +941,8 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                        memset(&data, 0, sizeof(data));
                        key.data = &subkeyid;
                        key.size = sizeof(subkeyid);
-                       data.data = fingerprint.fp;
-                       data.size = fingerprint.length;
+                       data.data = fp->fp;
+                       data.size = fp->length;
 
                        ret = cursor64->c_get(cursor64,
                                &key,
@@ -985,8 +988,8 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                        memset(&data, 0, sizeof(data));
                        key.data = hash.hash;
                        key.size = sizeof(hash.hash);
-                       data.data = fingerprint.fp;
-                       data.size = fingerprint.length;
+                       data.data = fp->fp;
+                       data.size = fp->length;
 
                        ret = cursor->c_get(cursor,
                                &key,
@@ -1016,20 +1019,10 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
        publickey = NULL;
 
        if (!deadlock) {
-               key.data = fingerprint.fp;
-               key.size = fingerprint.length;
-
-               keydb_fp(privctx, &fingerprint)->del(keydb_fp(privctx,
-                                       &fingerprint),
-                               privctx->txn,
-                               &key,
-                               0); /* flags */
-
-               /* Delete old style 64 bit keyid */
-               key.data = &keyid;
-               key.size = sizeof(keyid);
+               key.data = fp->fp;
+               key.size = fp->length;
 
-               keydb_id(privctx, keyid)->del(keydb_id(privctx, keyid),
+               keydb_fp(privctx, fp)->del(keydb_fp(privctx, fp),
                                privctx->txn,
                                &key,
                                0); /* flags */
@@ -1101,7 +1094,7 @@ static int db4_store_key(struct onak_dbctx *dbctx,
         * it definitely needs updated.
         */
        if (update) {
-               deadlock = (db4_delete_key(dbctx, keyid, true) == -1);
+               deadlock = (db4_delete_key(dbctx, &fingerprint, true) == -1);
        }
 
        /*
index ef42fbe35406977deb152610eceab0032bda850c..b302efbb0c4a0e6d2a2b0c5947d31195d8060de1 100644 (file)
@@ -108,14 +108,15 @@ static int dynamic_store_key(struct onak_dbctx *dbctx,
                        publickey, intrans, update);
 }
 
-static int dynamic_delete_key(struct onak_dbctx *dbctx, uint64_t keyid,
+static int dynamic_delete_key(struct onak_dbctx *dbctx,
+               struct openpgp_fingerprint *fp,
                bool intrans)
 {
        struct onak_dynamic_dbctx *privctx =
                        (struct onak_dynamic_dbctx *) dbctx->priv;
 
        return privctx->loadeddbctx->delete_key(privctx->loadeddbctx,
-                       keyid, intrans);
+                       fp, intrans);
 }
 
 static int dynamic_update_keys(struct onak_dbctx *dbctx,
index bc3c578667a0a36e205b69567ea3e2dd953f43e2..c5d3ece2cae2a17aebcb3774968302d264f0159c 100644 (file)
@@ -143,20 +143,20 @@ static int file_store_key(struct onak_dbctx *dbctx,
 
 /**
  *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
  *     @intrans: If we're already in a transaction.
  *
  *     This function deletes a public key from whatever storage mechanism we
  *     are using. Returns 0 if the key existed.
  */
 static int file_delete_key(struct onak_dbctx *dbctx,
-               uint64_t keyid, bool intrans)
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        char *db_dir = (char *) dbctx->priv;
        char keyfile[1024];
 
        snprintf(keyfile, 1023, "%s/0x%" PRIX64, db_dir,
-                       keyid & 0xFFFFFFFF);
+                       fingerprint2keyid(fp) & 0xFFFFFFFF);
 
        return unlink(keyfile);
 }
index 0bf922f1475a419781226b33dc22e154e4234c63..926960f04cbe9a2f7aec564f094e2d26201d05d2 100644 (file)
@@ -394,10 +394,11 @@ static int fs_store_key(struct onak_dbctx *dbctx,
 
 /**
  *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
  *     @intrans: If we're already in a transaction.
  */
-static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
+static int fs_delete_key(struct onak_dbctx *dbctx,
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        static char buffer[PATH_MAX];
        int ret;
@@ -407,9 +408,11 @@ static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
        struct openpgp_fingerprint *subkeyids = NULL;
        uint64_t subkeyid;
        int i = 0;
+       uint64_t keyid;
 
-       if ((keyid >> 32) == 0)
-               keyid = fs_getfullkeyid(dbctx, keyid);
+       keyid = fingerprint2keyid(fp);
+       if (keyid == 0)
+               return 1;
 
        if (!intrans)
                fs_starttrans(dbctx);
index 3ec46a4f2cf7b9e9bb40ca1e806961727fa40370..46fceb8dc230811a9c320ba44cead2d722fbf34f 100644 (file)
@@ -271,13 +271,13 @@ static int hkp_store_key(struct onak_dbctx *dbctx,
 
 /**
  *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
  *     @intrans: If we're already in a transaction.
  *
  *     No op for HKP.
  */
 static int hkp_delete_key(struct onak_dbctx *dbctx,
-               uint64_t keyid, bool intrans)
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        return -1;
 }
index cd1a2819d98eefc04b53a060d718e7c1fc58ab19..597507e5e78d3ecb41b8043c9be162dfe65a9d62 100644 (file)
@@ -187,19 +187,19 @@ static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
 
 /**
 *      delete_key - Given a keyid delete the key from storage.
-*      @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
 *      @intrans: If we're already in a transaction.
 *
 *      This function deletes a public key from whatever storage mechanism we
 *      are using. Returns 0 if the key existed.
 */
 static int keyd_delete_key(struct onak_dbctx *dbctx,
-               uint64_t keyid, bool intrans)
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        int keyd_fd = (intptr_t) dbctx->priv;
 
        if (keyd_send_cmd(keyd_fd, KEYD_CMD_DELETE)) {
-               write(keyd_fd, &keyid, sizeof(keyid));
+               write(keyd_fd, fp, sizeof(*fp));
        }
 
        return 0;
index 62371aedce9a6e9918127acaecd72fff361485b9..e8c056b7ccdf19f1cc4c1eec7e0cef8cfeac531c 100644 (file)
@@ -161,13 +161,13 @@ static int keyring_store_key(struct onak_dbctx *dbctx,
 
 /**
  *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
  *     @intrans: If we're already in a transaction.
  *
  *     We don't support removing keys from a keyring file.
  */
 static int keyring_delete_key(struct onak_dbctx *dbctx,
-               uint64_t keyid, bool intrans)
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        return 1;
 }
@@ -254,7 +254,7 @@ static int keyring_parse_keys(struct onak_keyring_dbctx *privctx)
         * Walk the keyring file, noting the start of each public key and the
         * total length of packets associated with it.
         */
-       pos = start = totlen = 0;
+       len = pos = start = totlen = 0;
        while (((privctx->length - pos) > 5) && (privctx->file[pos] & 0x80)) {
                if (privctx->file[pos] & 0x40) {
                        tag = privctx->file[pos] & 0x3F;
index 0b58d28f6e753e4d1563db777105da0b8a9b99b3..7663c3db8799ce518994649901d48d557e95068c 100644 (file)
@@ -247,13 +247,14 @@ static int pg_fetch_key_text(struct onak_dbctx *dbctx,
 
 /**
  *     delete_key - Given a keyid delete the key from storage.
- *     @keyid: The keyid to delete.
+ *     @fp: The fingerprint of the key to delete.
  *     @intrans: If we're already in a transaction.
  *
  *     This function deletes a public key from whatever storage mechanism we
  *     are using. Returns 0 if the key existed.
  */
-static int pg_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
+static int pg_delete_key(struct onak_dbctx *dbctx,
+               struct openpgp_fingerprint *fp, bool intrans)
 {
        PGconn *dbconn = (PGconn *) dbctx->priv;
        PGresult *result = NULL;
@@ -262,11 +263,14 @@ static int pg_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans)
        int found = 1;
        int i;
        Oid key_oid;
+       uint64_t keyid;
 
        if (!intrans) {
                result = PQexec(dbconn, "BEGIN");
                PQclear(result);
        }
+
+       keyid = fingerprint2keyid(fp);
        
        snprintf(statement, 1023,
                        "SELECT keydata FROM onak_keys WHERE keyid = '%"
@@ -347,6 +351,7 @@ static int pg_store_key(struct onak_dbctx *dbctx,
        int i;
        uint64_t keyid;
        struct pg_fc_ctx fcctx;
+       struct openpgp_fingerprint fp;
 
        if (!intrans) {
                result = PQexec(dbconn, "BEGIN");
@@ -367,7 +372,8 @@ static int pg_store_key(struct onak_dbctx *dbctx,
         * it definitely needs updated.
         */
        if (update) {
-               pg_delete_key(dbctx, keyid, true);
+               get_fingerprint(publickey->publickey, &fp);
+               pg_delete_key(dbctx, &fp, true);
        }
 
        next = publickey->next;
index 59c988da9172f0cd862df64a251fde0fc03541a0..c715c63172d2449cacaea01882785cb7936016d4 100644 (file)
@@ -71,7 +71,8 @@ static int stacked_store_key(struct onak_dbctx *dbctx,
                        publickey, intrans, update);
 }
 
-static int stacked_delete_key(struct onak_dbctx *dbctx, uint64_t keyid,
+static int stacked_delete_key(struct onak_dbctx *dbctx,
+               struct openpgp_fingerprint *fp,
                bool intrans)
 {
        struct onak_stacked_dbctx *privctx =
@@ -80,7 +81,7 @@ static int stacked_delete_key(struct onak_dbctx *dbctx, uint64_t keyid,
                        (struct onak_dbctx *) privctx->backends->object;
 
        return backend->delete_key(backend,
-                       keyid, intrans);
+                       fp, intrans);
 }
 
 static int stacked_update_keys(struct onak_dbctx *dbctx,
diff --git a/onak.c b/onak.c
index b7cfc80205a55e89184f738f12332267f2701f41..384b6749f61c372c0d0077f6d8fa5602fff2f247 100644 (file)
--- a/onak.c
+++ b/onak.c
@@ -387,9 +387,16 @@ int main(int argc, char *argv[])
                                puts("Key not found");
                        }
                } else if (!strcmp("delete", argv[optind])) {
-                       dbctx->delete_key(dbctx,
-                                       dbctx->getfullkeyid(dbctx, keyid),
-                                       false);
+                       if (!isfp) {
+                               if (dbctx->fetch_key_id(dbctx, keyid, &keys,
+                                                       true)) {
+                                       get_fingerprint(keys->publickey,
+                                                       &fingerprint);
+                                       dbctx->delete_key(dbctx, &fingerprint,
+                                                       true);
+                               }
+                       } else
+                               dbctx->delete_key(dbctx, &fingerprint, false);
                } else if (!strcmp("get", argv[optind])) {
                        if (!(ishex || isfp)) {
                                puts("Can't get a key on uid text."
@@ -448,7 +455,8 @@ int main(int argc, char *argv[])
                } else if (!strcmp("reindex", argv[optind])) {
                        dbctx->starttrans(dbctx);
                        if (dbctx->fetch_key_id(dbctx, keyid, &keys, true)) {
-                               dbctx->delete_key(dbctx, keyid, true);
+                               get_fingerprint(keys->publickey, &fingerprint);
+                               dbctx->delete_key(dbctx, &fingerprint, true);
                                cleankeys(&keys, config.clean_policies);
                                dbctx->store_key(dbctx, keys, true, false);
                        } else {