]> the.earth.li Git - onak.git/blobdiff - keydb_db4.c
Cleanup postinst to avoid recursive chown of database
[onak.git] / keydb_db4.c
index aa2f229c01c3c4fc5f3e6d7dd1bd69b6e3045c18..1f306e267f60e9851c0dd65b0874517ba7afcb39 100644 (file)
@@ -72,10 +72,13 @@ DB *keydb_fp(struct onak_db4_dbctx *privctx, struct openpgp_fingerprint *fp)
 {
        uint64_t keytrun;
 
-       keytrun = (fp->fp[4] << 24) |
-                       (fp->fp[5] << 16) |
-                       (fp->fp[6] <<  8) |
-                       (fp->fp[7]);
+       keytrun = fp->fp[4];
+       keytrun <<= 8;
+       keytrun |= fp->fp[5];
+       keytrun <<= 8;
+       keytrun |= fp->fp[6];
+       keytrun <<= 8;
+       keytrun |= fp->fp[7];
 
        return(privctx->dbconns[keytrun % privctx->numdbs]);
 }
@@ -289,70 +292,6 @@ static int db4_upgradedb(struct onak_dbctx *dbctx)
        return ret;
 }
 
-/**
- *     getfullkeyid - Maps a 32bit key id to a 64bit one.
- *     @keyid: The 32bit keyid.
- *
- *     This function maps a 32bit key id to the full 64bit one. It returns the
- *     first full keyid that has this short keyid. If the key isn't found a
- *     keyid of 0 is returned.
- *
- *     FIXME: This should either return the fingerprint or ideally go away
- *     entirely.
- */
-static uint64_t db4_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
-{
-       struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
-       DBT       key, data;
-       DBC      *cursor = NULL;
-       uint32_t  shortkeyid = 0;
-       int       ret = 0;
-       int       i;
-
-       if (keyid < 0x100000000LL) {
-               ret = privctx->id32db->cursor(privctx->id32db,
-                               privctx->txn,
-                               &cursor,
-                               0);   /* flags */
-
-               if (ret != 0) {
-                       return 0;
-               }
-
-               shortkeyid = keyid & 0xFFFFFFFF;
-
-               memset(&key, 0, sizeof(key));
-               memset(&data, 0, sizeof(data));
-               key.data = &shortkeyid;
-               key.size = sizeof(shortkeyid);
-               data.flags = DB_DBT_MALLOC;
-
-               ret = cursor->c_get(cursor,
-                       &key,
-                       &data,
-                       DB_SET);
-
-               if (ret == 0) {
-                       keyid = 0;
-                       /* Only works for v4, not v3 or v5 */
-                       for (i = 12; i < 20; i++) {
-                               keyid <<= 8;
-                               keyid |= ((uint8_t *) data.data)[i];
-                       }
-
-                       if (data.data != NULL) {
-                               free(data.data);
-                               data.data = NULL;
-                       }
-               }
-
-               cursor->c_close(cursor);
-               cursor = NULL;
-       }
-
-       return keyid;
-}
-
 /**
  *     fetch_key_fp - Given a fingerprint fetch the key from storage.
  */
@@ -464,7 +403,6 @@ static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
                bool intrans)
 {
        struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
-       struct openpgp_packet_list *packets = NULL;
        DBT key, data;
        DBC *cursor = NULL;
        int ret = 0;
@@ -536,12 +474,6 @@ static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
        return (numkeys);
 }
 
-
-int worddb_cmp(const void *d1, const void *d2)
-{
-       return memcmp(d1, d2, 12);
-}
-
 /**
  *     fetch_key_text - Trys to find the keys that contain the supplied text.
  *     @search: The text to search for.
@@ -557,7 +489,6 @@ static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
        DBC *cursor = NULL;
        DBT key, data;
        int ret;
-       uint64_t keyid;
        int i;
        int numkeys;
        char *searchtext = NULL;
@@ -662,7 +593,6 @@ static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
        struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
        DBT       key, data;
        DBC      *cursor = NULL;
-       uint64_t  keyid = 0;
        int       ret;
        int       count = 0;
        struct openpgp_fingerprint fingerprint;
@@ -703,14 +633,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;
@@ -724,25 +655,26 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
        int i;
        char **uids = NULL;
        char *primary = NULL;
-       unsigned char worddb_data[12];
        struct ll *wordlist = NULL;
        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.
@@ -762,13 +694,6 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
 
                for (curword = wordlist; curword != NULL && !deadlock;
                                curword = curword->next) {
-                       memset(&key, 0, sizeof(key));
-                       memset(&data, 0, sizeof(data));
-                       key.data = curword->object;
-                       key.size = strlen(key.data);
-                       data.data = worddb_data;
-                       data.size = sizeof(worddb_data);
-
                        /*
                         * New style uses the fingerprint as the data
                         * Old (unsupported) style was the 64 bit keyid
@@ -777,8 +702,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 +757,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 +785,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,
@@ -890,7 +815,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx,
                        memset(&key, 0, sizeof(key));
                        key.data = subkeyids[i].fp;
                        key.size = subkeyids[i].length;
-                       privctx->subkeydb->del(privctx->subkeydb,
+                       ret = privctx->subkeydb->del(privctx->subkeydb,
                                        privctx->txn, &key, 0);
                        if (ret != 0 && ret != DB_NOTFOUND) {
                                logthing(LOGTHING_ERROR,
@@ -910,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,
@@ -938,8 +863,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 +910,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 +941,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 +1016,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);
        }
 
        /*
@@ -1839,7 +1754,6 @@ struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
        dbctx->getkeysigs               = generic_getkeysigs;
        dbctx->cached_getkeysigs        = generic_cached_getkeysigs;
        dbctx->keyid2uid                = generic_keyid2uid;
-       dbctx->getfullkeyid             = db4_getfullkeyid;
        dbctx->iterate_keys             = db4_iterate_keys;
 
        return dbctx;