]> the.earth.li Git - onak.git/blobdiff - keydb.c
Extend database backends to support fetching by key fingerprint
[onak.git] / keydb.c
diff --git a/keydb.c b/keydb.c
index 2abfa02bae51ae8a9d8d7b3f587844b66c4b29ca..ac70640e26df6bfedcfd7f506a09e30155a75abf 100644 (file)
--- a/keydb.c
+++ b/keydb.c
@@ -1,9 +1,20 @@
 /*
  * keydb.c - Routines for DB access that just use store/fetch.
  *
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
  *
- * Copyright 2002-2004 Project Purple
+ * 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 the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -39,7 +50,7 @@ char *generic_keyid2uid(uint64_t keyid)
        char buf[1024];
 
        buf[0]=0;
-       if (config.dbbackend->fetch_key(keyid, &publickey, false) &&
+       if (config.dbbackend->fetch_key_id(keyid, &publickey, false) &&
                        publickey != NULL) {
                curuid = publickey->uids;
                while (curuid != NULL && buf[0] == 0) {
@@ -77,7 +88,7 @@ struct ll *generic_getkeysigs(uint64_t keyid, bool *revoked)
        struct openpgp_signedpacket_list *uids = NULL;
        struct openpgp_publickey *publickey = NULL;
 
-       config.dbbackend->fetch_key(keyid, &publickey, false);
+       config.dbbackend->fetch_key_id(keyid, &publickey, false);
        
        if (publickey != NULL) {
                for (uids = publickey->uids; uids != NULL; uids = uids->next) {
@@ -149,9 +160,9 @@ uint64_t generic_getfullkeyid(uint64_t keyid)
        struct openpgp_publickey *publickey = NULL;
 
        if (keyid < 0x100000000LL) {
-               config.dbbackend->fetch_key(keyid, &publickey, false);
+               config.dbbackend->fetch_key_id(keyid, &publickey, false);
                if (publickey != NULL) {
-                       keyid = get_keyid(publickey);
+                       get_keyid(publickey, &keyid);
                        free_publickey(publickey);
                        publickey = NULL;
                } else {
@@ -182,13 +193,15 @@ int generic_update_keys(struct openpgp_publickey **keys, bool sendsync)
        struct openpgp_publickey *prev = NULL;
        int newkeys = 0;
        bool intrans;
+       uint64_t keyid;
 
        for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
                intrans = config.dbbackend->starttrans();
+               get_keyid(curkey, &keyid);
                logthing(LOGTHING_INFO,
                        "Fetching key 0x%" PRIX64 ", result: %d",
-                       get_keyid(curkey),
-                       config.dbbackend->fetch_key(get_keyid(curkey), &oldkey,
+                       keyid,
+                       config.dbbackend->fetch_key_id(keyid, &oldkey,
                                        intrans));
 
                /*
@@ -236,3 +249,30 @@ int generic_update_keys(struct openpgp_publickey **keys, bool sendsync)
        return newkeys;
 }
 #endif /* NEED_UPDATEKEYS */
+
+#ifdef NEED_GET_FP
+static int generic_fetch_key_fp(uint8_t *fp, size_t fpsize,
+               struct openpgp_publickey **publickey, bool intrans)
+{
+       uint64_t keyid;
+       int i;
+
+       if (fpsize > MAX_FINGERPRINT_LEN) {
+               return 0;
+       }
+
+       /*
+        * We assume if the backend is using this function it's not storing
+        * anything bigger than the 64 bit key ID and just truncate the
+        * fingerprint to get that value. This doesn't work for v3 keys,
+        * but there's no way to map from v3 fingerprint to v3 key ID so
+        * if the backend can't do it we're going to fail anyway.
+        */
+       keyid = 0;
+       for (i = (fpsize - 8); i < fpsize; i++) {
+               keyid = (keyid << 8) + fp[i];
+       }
+
+       return config.dbbackend->fetch_key_id(keyid, publickey, intrans);
+}
+#endif