2 * keydb.c - Routines for DB access that just use store/fetch.
4 * Copyright 2002-2004 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * The routines in this file are meant to be used as an initial step when
22 * adding a new db access module. They provide various functions required
23 * of the db access module using only the store and fetch functions. As
24 * they need to parse the actual OpenPGP data to work they are a lot
25 * slower than custom functions however.
30 #include "decodekey.h"
34 #include "keystructs.h"
43 * keyid2uid - Takes a keyid and returns the primary UID for it.
44 * @keyid: The keyid to lookup.
46 char *generic_keyid2uid(uint64_t keyid)
48 struct openpgp_publickey *publickey = NULL;
49 struct openpgp_signedpacket_list *curuid = NULL;
53 if (config.dbbackend->fetch_key(keyid, &publickey, false) &&
55 curuid = publickey->uids;
56 while (curuid != NULL && buf[0] == 0) {
57 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
58 snprintf(buf, 1023, "%.*s",
59 (int) curuid->packet->length,
60 curuid->packet->data);
62 curuid = curuid -> next;
64 free_publickey(publickey);
75 #ifdef NEED_GETKEYSIGS
77 * getkeysigs - Gets a linked list of the signatures on a key.
78 * @keyid: The keyid to get the sigs for.
79 * @revoked: Is the key revoked?
81 * This function gets the list of signatures on a key. Used for key
82 * indexing and doing stats bits. If revoked is non-NULL then if the key
83 * is revoked it's set to true.
85 struct ll *generic_getkeysigs(uint64_t keyid, bool *revoked)
87 struct ll *sigs = NULL;
88 struct openpgp_signedpacket_list *uids = NULL;
89 struct openpgp_publickey *publickey = NULL;
91 config.dbbackend->fetch_key(keyid, &publickey, false);
93 if (publickey != NULL) {
94 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
95 sigs = keysigs(sigs, uids->sigs);
97 if (revoked != NULL) {
98 *revoked = publickey->revoked;
100 free_publickey(publickey);
108 * cached_getkeysigs - Gets the signatures on a key.
109 * @keyid: The key we want the signatures for.
111 * This function gets the signatures on a key. It's the same as the
112 * getkeysigs function above except we use the hash module to cache the
113 * data so if we need it again it's already loaded.
115 struct ll *generic_cached_getkeysigs(uint64_t keyid)
117 struct stats_key *key = NULL;
118 struct stats_key *signedkey = NULL;
119 struct ll *cursig = NULL;
120 struct ll *sigs = NULL;
121 bool revoked = false;
127 key = findinhash(keyid);
129 if (key == NULL || key->gotsigs == false) {
130 sigs = config.dbbackend->getkeysigs(keyid, &revoked);
135 key = createandaddtohash(keyid);
138 key->revoked = revoked;
139 for (cursig = key->sigs; cursig != NULL;
140 cursig = cursig->next) {
141 signedkey = (struct stats_key *) cursig->object;
142 signedkey->signs = lladd(signedkey->signs, key);
150 #ifdef NEED_GETFULLKEYID
152 * getfullkeyid - Maps a 32bit key id to a 64bit one.
153 * @keyid: The 32bit keyid.
155 * This function maps a 32bit key id to the full 64bit one. It returns the
156 * full keyid. If the key isn't found a keyid of 0 is returned.
158 uint64_t generic_getfullkeyid(uint64_t keyid)
160 struct openpgp_publickey *publickey = NULL;
162 if (keyid < 0x100000000LL) {
163 config.dbbackend->fetch_key(keyid, &publickey, false);
164 if (publickey != NULL) {
165 get_keyid(publickey, &keyid);
166 free_publickey(publickey);
177 #ifdef NEED_UPDATEKEYS
179 * update_keys - Takes a list of public keys and updates them in the DB.
180 * @keys: The keys to update in the DB.
181 * @sendsync: Should we send a sync mail to our peers.
183 * Takes a list of keys and adds them to the database, merging them with
184 * the key in the database if it's already present there. The key list is
185 * update to contain the minimum set of updates required to get from what
186 * we had before to what we have now (ie the set of data that was added to
187 * the DB). Returns the number of entirely new keys added.
189 int generic_update_keys(struct openpgp_publickey **keys, bool sendsync)
191 struct openpgp_publickey *curkey = NULL;
192 struct openpgp_publickey *oldkey = NULL;
193 struct openpgp_publickey *prev = NULL;
198 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
199 intrans = config.dbbackend->starttrans();
200 get_keyid(curkey, &keyid);
201 logthing(LOGTHING_INFO,
202 "Fetching key 0x%" PRIX64 ", result: %d",
204 config.dbbackend->fetch_key(keyid, &oldkey,
208 * If we already have the key stored in the DB then merge it
209 * with the new one that's been supplied. Otherwise the key
210 * we've just got is the one that goes in the DB and also the
211 * one that we send out.
213 if (oldkey != NULL) {
214 merge_keys(oldkey, curkey);
215 if (curkey->sigs == NULL &&
216 curkey->uids == NULL &&
217 curkey->subkeys == NULL) {
219 *keys = curkey->next;
221 prev->next = curkey->next;
223 free_publickey(curkey);
228 logthing(LOGTHING_INFO,
229 "Merged key; storing updated key.");
230 config.dbbackend->store_key(oldkey, intrans,
233 free_publickey(oldkey);
236 logthing(LOGTHING_INFO,
237 "Storing completely new key.");
238 config.dbbackend->store_key(curkey, intrans, false);
241 config.dbbackend->endtrans();
245 if (sendsync && keys != NULL) {
251 #endif /* NEED_UPDATEKEYS */