2 * keydb_dynamic.c - backend that can load the other backends
4 * Copyright 2005 Brett Parker <iDunno@sommitrealweird.co.uk>
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.
24 #include "decodekey.h"
28 #include "keystructs.h"
32 #include "onak-conf.h"
37 static struct dbfuncs *loaded_backend = NULL;
38 static void *backend_handle;
40 static bool close_backend(void)
42 loaded_backend = NULL;
43 dlclose(backend_handle);
44 backend_handle = NULL;
49 static bool load_backend(void)
52 char *funcsname = NULL;
54 if (loaded_backend != NULL) {
56 loaded_backend = NULL;
59 if (config.use_keyd) {
60 free(config.db_backend);
61 config.db_backend = strdup("keyd");
64 if (!config.db_backend) {
65 logthing(LOGTHING_CRITICAL, "No database backend defined.");
69 if (config.backends_dir == NULL) {
70 soname = malloc(strlen(config.db_backend)
71 + strlen("./libkeydb_")
75 sprintf(soname, "./libkeydb_%s.so", config.db_backend);
77 soname = malloc(strlen(config.db_backend)
78 + strlen("/libkeydb_")
80 + strlen(config.backends_dir)
83 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
87 logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
89 backend_handle = dlopen(soname, RTLD_LAZY);
90 if (backend_handle == NULL) {
91 logthing(LOGTHING_CRITICAL,
92 "Failed to open handle to library '%s': %s",
99 funcsname = malloc(strlen(config.db_backend)
103 sprintf(funcsname, "keydb_%s_funcs", config.db_backend);
105 loaded_backend = dlsym(backend_handle, funcsname);
108 if (loaded_backend == NULL) {
109 logthing(LOGTHING_CRITICAL,
110 "Failed to find dbfuncs structure in library "
111 "'%s' : %s", soname, dlerror());
122 static bool dynamic_starttrans()
124 if (loaded_backend == NULL) {
128 if (loaded_backend != NULL) {
129 if (loaded_backend->starttrans != NULL) {
130 return loaded_backend->starttrans();
137 static void dynamic_endtrans()
139 if (loaded_backend == NULL) {
143 if (loaded_backend != NULL) {
144 if (loaded_backend->endtrans != NULL) {
145 loaded_backend->endtrans();
150 static int dynamic_fetch_key(uint64_t keyid,
151 struct openpgp_publickey **publickey, bool intrans)
153 if (loaded_backend == NULL) {
157 if (loaded_backend != NULL) {
158 if (loaded_backend->fetch_key != NULL) {
159 return loaded_backend->fetch_key(keyid,publickey,intrans);
166 static int dynamic_store_key(struct openpgp_publickey *publickey, bool intrans,
169 if (loaded_backend == NULL) {
173 if (loaded_backend != NULL) {
174 if (loaded_backend->store_key != NULL) {
175 return loaded_backend->store_key(publickey,intrans,update);
182 static int dynamic_delete_key(uint64_t keyid, bool intrans)
184 if (loaded_backend == NULL) {
188 if (loaded_backend != NULL) {
189 if (loaded_backend->delete_key != NULL) {
190 return loaded_backend->delete_key(keyid, intrans);
197 static int dynamic_fetch_key_text(const char *search,
198 struct openpgp_publickey **publickey)
200 if (loaded_backend == NULL) {
204 if (loaded_backend != NULL) {
205 if (loaded_backend->fetch_key_text != NULL) {
206 return loaded_backend->fetch_key_text(search, publickey);
213 static int dynamic_fetch_key_skshash(const struct skshash *hash,
214 struct openpgp_publickey **publickey)
216 if (loaded_backend == NULL) {
220 if (loaded_backend != NULL) {
221 if (loaded_backend->fetch_key_skshash != NULL) {
222 return loaded_backend->fetch_key_skshash(hash,
230 static int dynamic_iterate_keys(void (*iterfunc)(void *ctx,
231 struct openpgp_publickey *key), void *ctx)
233 if (loaded_backend == NULL) {
237 if (loaded_backend != NULL) {
238 if (loaded_backend->iterate_keys != NULL) {
239 return loaded_backend->iterate_keys(iterfunc, ctx);
247 * keyid2uid - Takes a keyid and returns the primary UID for it.
248 * @keyid: The keyid to lookup.
250 static char *dynamic_keyid2uid(uint64_t keyid)
252 struct openpgp_publickey *publickey = NULL;
253 struct openpgp_signedpacket_list *curuid = NULL;
256 if (loaded_backend == NULL) {
260 if (loaded_backend != NULL) {
261 if (loaded_backend->keyid2uid != NULL) {
262 return loaded_backend->keyid2uid(keyid);
267 if (dynamic_fetch_key(keyid, &publickey, false) && publickey != NULL) {
268 curuid = publickey->uids;
269 while (curuid != NULL && buf[0] == 0) {
270 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
271 snprintf(buf, 1023, "%.*s",
272 (int) curuid->packet->length,
273 curuid->packet->data);
275 curuid = curuid -> next;
277 free_publickey(publickey);
288 * getkeysigs - Gets a linked list of the signatures on a key.
289 * @keyid: The keyid to get the sigs for.
290 * @revoked: Is the key revoked?
292 * This function gets the list of signatures on a key. Used for key
293 * indexing and doing stats bits. If revoked is non-NULL then if the key
294 * is revoked it's set to true.
296 static struct ll *dynamic_getkeysigs(uint64_t keyid, bool *revoked)
298 struct ll *sigs = NULL;
299 struct openpgp_signedpacket_list *uids = NULL;
300 struct openpgp_publickey *publickey = NULL;
302 if ( loaded_backend == NULL ) {
306 if (loaded_backend != NULL) {
307 if (loaded_backend->getkeysigs != NULL) {
308 return loaded_backend->getkeysigs(keyid,revoked);
312 dynamic_fetch_key(keyid, &publickey, false);
314 if (publickey != NULL) {
315 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
316 sigs = keysigs(sigs, uids->sigs);
318 if (revoked != NULL) {
319 *revoked = publickey->revoked;
321 free_publickey(publickey);
328 * cached_getkeysigs - Gets the signatures on a key.
329 * @keyid: The key we want the signatures for.
331 * This function gets the signatures on a key. It's the same as the
332 * getkeysigs function above except we use the hash module to cache the
333 * data so if we need it again it's already loaded.
335 static struct ll *dynamic_cached_getkeysigs(uint64_t keyid)
337 struct stats_key *key = NULL;
338 struct stats_key *signedkey = NULL;
339 struct ll *cursig = NULL;
340 bool revoked = false;
346 if (loaded_backend == NULL) {
350 if (loaded_backend != NULL) {
351 if (loaded_backend->cached_getkeysigs != NULL) {
352 return loaded_backend->cached_getkeysigs(keyid);
356 key = createandaddtohash(keyid);
358 if (key->gotsigs == false) {
359 key->sigs = dynamic_getkeysigs(key->keyid, &revoked);
360 key->revoked = revoked;
361 for (cursig = key->sigs; cursig != NULL;
362 cursig = cursig->next) {
363 signedkey = (struct stats_key *) cursig->object;
364 signedkey->signs = lladd(signedkey->signs, key);
373 * getfullkeyid - Maps a 32bit key id to a 64bit one.
374 * @keyid: The 32bit keyid.
376 * This function maps a 32bit key id to the full 64bit one. It returns the
377 * full keyid. If the key isn't found a keyid of 0 is returned.
379 static uint64_t dynamic_getfullkeyid(uint64_t keyid)
381 struct openpgp_publickey *publickey = NULL;
383 if (loaded_backend == NULL) {
387 if (loaded_backend != NULL) {
388 if (loaded_backend->getfullkeyid != NULL) {
389 return loaded_backend->getfullkeyid(keyid);
393 if (keyid < 0x100000000LL) {
394 dynamic_fetch_key(keyid, &publickey, false);
395 if (publickey != NULL) {
396 keyid = get_keyid(publickey);
397 free_publickey(publickey);
408 * update_keys - Takes a list of public keys and updates them in the DB.
409 * @keys: The keys to update in the DB.
410 * @sendsync: Should we send a sync mail to our peers.
412 * Takes a list of keys and adds them to the database, merging them with
413 * the key in the database if it's already present there. The key list is
414 * update to contain the minimum set of updates required to get from what
415 * we had before to what we have now (ie the set of data that was added to
416 * the DB). Returns the number of entirely new keys added.
418 static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
420 struct openpgp_publickey *curkey = NULL;
421 struct openpgp_publickey *oldkey = NULL;
422 struct openpgp_publickey *prev = NULL;
426 if (loaded_backend == NULL) {
430 if (loaded_backend != NULL) {
431 if (loaded_backend->update_keys != NULL) {
432 return loaded_backend->update_keys(keys, sendsync);
436 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
437 intrans = dynamic_starttrans();
438 logthing(LOGTHING_INFO,
439 "Fetching key 0x%" PRIX64 ", result: %d",
441 dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
444 * If we already have the key stored in the DB then merge it
445 * with the new one that's been supplied. Otherwise the key
446 * we've just got is the one that goes in the DB and also the
447 * one that we send out.
449 if (oldkey != NULL) {
450 merge_keys(oldkey, curkey);
451 if (curkey->sigs == NULL &&
452 curkey->uids == NULL &&
453 curkey->subkeys == NULL) {
455 *keys = curkey->next;
457 prev->next = curkey->next;
459 free_publickey(curkey);
464 logthing(LOGTHING_INFO,
465 "Merged key; storing updated key.");
466 dynamic_store_key(oldkey, intrans, true);
468 free_publickey(oldkey);
472 logthing(LOGTHING_INFO,
473 "Storing completely new key.");
474 dynamic_store_key(curkey, intrans, false);
481 if (sendsync && keys != NULL) {
488 static void dynamic_initdb(bool readonly)
490 if (loaded_backend == NULL) {
494 if (loaded_backend != NULL) {
495 if (loaded_backend->initdb != NULL) {
496 loaded_backend->initdb(readonly);
501 static void dynamic_cleanupdb(void)
503 if (loaded_backend != NULL) {
504 if (loaded_backend->cleanupdb != NULL) {
505 loaded_backend->cleanupdb();
512 struct dbfuncs keydb_dynamic_funcs = {
513 .initdb = dynamic_initdb,
514 .cleanupdb = dynamic_cleanupdb,
515 .starttrans = dynamic_starttrans,
516 .endtrans = dynamic_endtrans,
517 .fetch_key = dynamic_fetch_key,
518 .fetch_key_text = dynamic_fetch_key_text,
519 .fetch_key_skshash = dynamic_fetch_key_skshash,
520 .store_key = dynamic_store_key,
521 .update_keys = dynamic_update_keys,
522 .delete_key = dynamic_delete_key,
523 .getkeysigs = dynamic_getkeysigs,
524 .cached_getkeysigs = dynamic_cached_getkeysigs,
525 .keyid2uid = dynamic_keyid2uid,
526 .getfullkeyid = dynamic_getfullkeyid,
527 .iterate_keys = dynamic_iterate_keys,