]> git.sommitrealweird.co.uk Git - onak.git/blobdiff - keydb_db3.c
Add subkey support to keydb_fs backend.
[onak.git] / keydb_db3.c
index 27b090e0ee6c133dc2239d396c29c5955506c78e..7cc3b1ef78d45227262e85744f95a520c2dda5fc 100644 (file)
@@ -4,8 +4,6 @@
  * Jonathan McDowell <noodles@earth.li>
  *
  * Copyright 2002 Project Purple
- *
- * $Id: keydb_db3.c,v 1.26 2004/05/27 03:33:24 noodles Exp $
  */
 
 #include <assert.h>
@@ -355,7 +353,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        return (numkeys);
 }
 
-int worddb_cmp(const char *d1, const char *d2)
+int worddb_cmp(const void *d1, const void *d2)
 {
        return memcmp(d1, d2, 12);
 }
@@ -490,6 +488,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
        DBT        data;
        uint64_t   keyid = 0;
        uint32_t   shortkeyid = 0;
+       uint64_t  *subkeyids = NULL;
        char     **uids = NULL;
        char      *primary = NULL;
        unsigned char worddb_data[12];
@@ -660,6 +659,39 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                }
        }
 
+       if (!deadlock) {
+               subkeyids = keysubkeys(publickey);
+               i = 0;
+               while (subkeyids != NULL && subkeyids[i] != 0) {
+                       shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
+
+                       memset(&key, 0, sizeof(key));
+                       memset(&data, 0, sizeof(data));
+                       key.data = &shortkeyid;
+                       key.size = sizeof(shortkeyid);
+                       data.data = &keyid;
+                       data.size = sizeof(keyid);
+
+                       ret = id32db->put(id32db,
+                               txn,
+                               &key,
+                               &data,
+                               0);
+                       if (ret != 0) {
+                               logthing(LOGTHING_ERROR,
+                                       "Problem storing short keyid: %s",
+                                       db_strerror(ret));
+                               if (ret == DB_LOCK_DEADLOCK) {
+                                       deadlock = true;
+                               }
+                       }
+               }
+               if (subkeyids != NULL) {
+                       free(subkeyids);
+                       subkeyids = NULL;
+               }
+       }
+
        return deadlock ? -1 : 0 ;
 }
 
@@ -677,6 +709,7 @@ int delete_key(uint64_t keyid, bool intrans)
        DBT key, data;
        DBC *cursor = NULL;
        uint32_t   shortkeyid = 0;
+       uint64_t  *subkeyids = NULL;
        int ret = 0;
        int i;
        char **uids = NULL;
@@ -811,6 +844,48 @@ int delete_key(uint64_t keyid, bool intrans)
                                deadlock = true;
                        }
                }
+
+               subkeyids = keysubkeys(publickey);
+               i = 0;
+               while (subkeyids != NULL && subkeyids[i] != 0) {
+                       shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
+
+                       memset(&key, 0, sizeof(key));
+                       memset(&data, 0, sizeof(data));
+                       key.data = &shortkeyid;
+                       key.size = sizeof(shortkeyid);
+                       data.data = &keyid;
+                       data.size = sizeof(keyid);
+
+                       ret = cursor->c_get(cursor,
+                               &key,
+                               &data,
+                               DB_GET_BOTH);
+
+                       if (ret == 0) {
+                               ret = cursor->c_del(cursor, 0);
+                               if (ret != 0) {
+                                       logthing(LOGTHING_ERROR,
+                                               "Problem deleting short"
+                                               " keyid: %s",
+                                               db_strerror(ret));
+                               }
+                       }
+
+                       if (ret != 0) {
+                               logthing(LOGTHING_ERROR,
+                                       "Problem deleting short keyid: %s",
+                                       db_strerror(ret));
+                               if (ret == DB_LOCK_DEADLOCK) {
+                                       deadlock = true;
+                               }
+                       }
+               }
+               if (subkeyids != NULL) {
+                       free(subkeyids);
+                       subkeyids = NULL;
+               }
+
                ret = cursor->c_close(cursor);
                cursor = NULL;
        }