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.
31 #include "decodekey.h"
35 #include "keystructs.h"
45 * keyid2uid - Takes a keyid and returns the primary UID for it.
46 * @keyid: The keyid to lookup.
48 char *generic_keyid2uid(struct onak_dbctx *dbctx, uint64_t keyid)
50 struct openpgp_publickey *publickey = NULL;
51 struct openpgp_signedpacket_list *curuid = NULL;
55 if (dbctx->fetch_key_id(dbctx, keyid, &publickey, false) &&
57 curuid = publickey->uids;
58 while (curuid != NULL && buf[0] == 0) {
59 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
60 snprintf(buf, 1023, "%.*s",
61 (int) curuid->packet->length,
62 curuid->packet->data);
64 curuid = curuid -> next;
66 free_publickey(publickey);
77 #ifdef NEED_GETKEYSIGS
79 * getkeysigs - Gets a linked list of the signatures on a key.
80 * @keyid: The keyid to get the sigs for.
81 * @revoked: Is the key revoked?
83 * This function gets the list of signatures on a key. Used for key
84 * indexing and doing stats bits. If revoked is non-NULL then if the key
85 * is revoked it's set to true.
87 struct ll *generic_getkeysigs(struct onak_dbctx *dbctx,
88 uint64_t keyid, bool *revoked)
90 struct ll *sigs = NULL;
91 struct openpgp_signedpacket_list *uids = NULL;
92 struct openpgp_publickey *publickey = NULL;
94 dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
96 if (publickey != NULL) {
97 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
98 sigs = keysigs(sigs, uids->sigs);
100 if (revoked != NULL) {
101 *revoked = publickey->revoked;
103 free_publickey(publickey);
111 * cached_getkeysigs - Gets the signatures on a key.
112 * @keyid: The key we want the signatures for.
114 * This function gets the signatures on a key. It's the same as the
115 * getkeysigs function above except we use the hash module to cache the
116 * data so if we need it again it's already loaded.
118 struct ll *generic_cached_getkeysigs(struct onak_dbctx *dbctx, uint64_t keyid)
120 struct stats_key *key = NULL;
121 struct stats_key *signedkey = NULL;
122 struct ll *cursig = NULL;
123 struct ll *sigs = NULL;
124 bool revoked = false;
130 key = findinhash(keyid);
132 if (key == NULL || key->gotsigs == false) {
133 sigs = dbctx->getkeysigs(dbctx, keyid, &revoked);
138 key = createandaddtohash(keyid);
141 key->revoked = revoked;
142 for (cursig = key->sigs; cursig != NULL;
143 cursig = cursig->next) {
144 signedkey = (struct stats_key *) cursig->object;
145 signedkey->signs = lladd(signedkey->signs, key);
153 #ifdef NEED_GETFULLKEYID
155 * getfullkeyid - Maps a 32bit key id to a 64bit one.
156 * @keyid: The 32bit keyid.
158 * This function maps a 32bit key id to the full 64bit one. It returns the
159 * full keyid. If the key isn't found a keyid of 0 is returned.
161 uint64_t generic_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
163 struct openpgp_publickey *publickey = NULL;
165 if (keyid < 0x100000000LL) {
166 dbctx->fetch_key_id(dbctx, keyid, &publickey, false);
167 if (publickey != NULL) {
168 get_keyid(publickey, &keyid);
169 free_publickey(publickey);
180 #ifdef NEED_UPDATEKEYS
182 * update_keys - Takes a list of public keys and updates them in the DB.
183 * @keys: The keys to update in the DB.
184 * @sendsync: Should we send a sync mail to our peers.
186 * Takes a list of keys and adds them to the database, merging them with
187 * the key in the database if it's already present there. The key list is
188 * update to contain the minimum set of updates required to get from what
189 * we had before to what we have now (ie the set of data that was added to
190 * the DB). Returns the number of entirely new keys added.
192 int generic_update_keys(struct onak_dbctx *dbctx,
193 struct openpgp_publickey **keys, bool sendsync)
195 struct openpgp_publickey *curkey = NULL;
196 struct openpgp_publickey *oldkey = NULL;
197 struct openpgp_publickey *prev = NULL;
202 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
203 intrans = dbctx->starttrans(dbctx);
204 get_keyid(curkey, &keyid);
205 logthing(LOGTHING_INFO,
206 "Fetching key 0x%" PRIX64 ", result: %d",
208 dbctx->fetch_key_id(dbctx, keyid, &oldkey,
212 * If we already have the key stored in the DB then merge it
213 * with the new one that's been supplied. Otherwise the key
214 * we've just got is the one that goes in the DB and also the
215 * one that we send out.
217 if (oldkey != NULL) {
218 merge_keys(oldkey, curkey);
219 if (curkey->sigs == NULL &&
220 curkey->uids == NULL &&
221 curkey->subkeys == NULL) {
223 *keys = curkey->next;
225 prev->next = curkey->next;
227 free_publickey(curkey);
232 logthing(LOGTHING_INFO,
233 "Merged key; storing updated key.");
234 dbctx->store_key(dbctx, oldkey, intrans,
237 free_publickey(oldkey);
240 logthing(LOGTHING_INFO,
241 "Storing completely new key.");
242 dbctx->store_key(dbctx, curkey, intrans, false);
245 dbctx->endtrans(dbctx);
248 if (sendsync && keys != NULL) {
254 #endif /* NEED_UPDATEKEYS */
257 static int generic_fetch_key_fp(struct onak_dbctx *dbctx,
258 struct openpgp_fingerprint *fingerprint,
259 struct openpgp_publickey **publickey, bool intrans)
264 if (fingerprint->length > MAX_FINGERPRINT_LEN) {
269 * We assume if the backend is using this function it's not storing
270 * anything bigger than the 64 bit key ID and just truncate the
271 * fingerprint to get that value. This doesn't work for v3 keys,
272 * but there's no way to map from v3 fingerprint to v3 key ID so
273 * if the backend can't do it we're going to fail anyway.
276 for (i = (fingerprint->length - 8); i < fingerprint->length; i++) {
277 keyid = (keyid << 8) + fingerprint->fp[i];
280 return dbctx->fetch_key_id(dbctx, keyid, publickey, intrans);