2 * keydb_dynamic.c - backend that can load the other backends
4 * Brett Parker <iDunno@sommitrealweird.co.uk>
6 * Copyright 2005 Project Purple
11 #include "decodekey.h"
15 #include "keystructs.h"
20 #include "keydb_dynamic.h"
22 struct dynamic_backend *get_backend(void)
24 return &__dynamicdb_backend__;
27 bool backend_loaded(void)
29 return __dynamicdb_backend__.loaded;
32 bool load_backend(void)
36 struct dynamic_backend *backend = get_backend();
38 if (backend->loaded) {
42 if (config.backends_dir == NULL) {
43 soname = malloc(strlen(config.db_backend)
44 + strlen("/libkeydb_")
48 sprintf(soname, "libkeydb_%s.so", config.db_backend);
50 soname = malloc(strlen(config.db_backend)
51 + strlen("/libkeydb_")
53 + strlen(config.backends_dir)
56 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
60 logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
62 handle = dlopen(soname, RTLD_LAZY);
64 logthing(LOGTHING_ERROR,
65 "Failed to open handle to library '%s': %s",
74 backend->initdb = (initdbfunc_t) dlsym(handle, "initdb");
75 backend->cleanupdb = (cleanupdbfunc_t) dlsym(handle, "cleanupdb");
76 backend->starttrans = (starttransfunc_t) dlsym(handle, "starttrans");
77 backend->endtrans = (endtransfunc_t) dlsym(handle, "endtrans");
78 backend->fetch_key = (fetch_keyfunc_t) dlsym(handle, "fetch_key");
79 backend->store_key = (store_keyfunc_t) dlsym(handle, "store_key");
80 backend->delete_key = (delete_keyfunc_t) dlsym(handle, "delete_key");
81 backend->fetch_key_text = (fetch_key_textfunc_t)
82 dlsym (handle, "fetch_key_text");
83 backend->update_keys = (update_keysfunc_t)
84 dlsym(handle, "update_keys");
85 backend->keyid2uid = (keyid2uidfunc_t) dlsym(handle, "keyid2uid");
86 backend->cached_getkeysigs = (cached_getkeysigsfunc_t)
87 dlsym(handle, "cached_getkeysigs");
88 backend->getfullkeyid = (getfullkeyidfunc_t)
89 dlsym(handle, "getfullkeyid");
90 backend->iterate_keys = (iterate_keysfunc_t)
91 dlsym(handle, "iterate_keys");
93 backend->handle = handle;
94 backend->loaded = true;
99 bool close_backend(void)
101 struct dynamic_backend *backend;
102 backend = get_backend();
104 backend->initdb = NULL;
105 backend->cleanupdb = NULL;
106 backend->starttrans = NULL;
107 backend->endtrans = NULL;
108 backend->fetch_key = NULL;
109 backend->store_key = NULL;
110 backend->delete_key = NULL;
111 backend->fetch_key_text = NULL;
112 backend->update_keys = NULL;
113 backend->keyid2uid = NULL;
114 backend->cached_getkeysigs = NULL;
115 backend->getfullkeyid = NULL;
116 backend->iterate_keys = NULL;
117 backend->loaded = false;
118 dlclose(backend->handle);
119 backend->handle = NULL;
125 * keyid2uid - Takes a keyid and returns the primary UID for it.
126 * @keyid: The keyid to lookup.
128 char *keyid2uid(uint64_t keyid)
130 struct openpgp_publickey *publickey = NULL;
131 struct openpgp_signedpacket_list *curuid = NULL;
134 struct dynamic_backend *backend;
135 if (!backend_loaded()) {
139 if (backend_loaded()) {
140 backend = get_backend();
141 if (backend->keyid2uid != NULL) {
142 return backend->keyid2uid(keyid);
147 if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
148 curuid = publickey->uids;
149 while (curuid != NULL && buf[0] == 0) {
150 if (curuid->packet->tag == 13) {
151 snprintf(buf, 1023, "%.*s",
152 (int) curuid->packet->length,
153 curuid->packet->data);
155 curuid = curuid -> next;
157 free_publickey(publickey);
168 * getkeysigs - Gets a linked list of the signatures on a key.
169 * @keyid: The keyid to get the sigs for.
170 * @revoked: Is the key revoked?
172 * This function gets the list of signatures on a key. Used for key
173 * indexing and doing stats bits. If revoked is non-NULL then if the key
174 * is revoked it's set to true.
176 struct ll *getkeysigs(uint64_t keyid, bool *revoked)
178 struct ll *sigs = NULL;
179 struct openpgp_signedpacket_list *uids = NULL;
180 struct openpgp_publickey *publickey = NULL;
182 struct dynamic_backend *backend;
183 if ( !backend_loaded() ) {
187 if (backend_loaded()) {
188 backend = get_backend();
189 if (backend->getkeysigs != NULL) {
190 return backend->getkeysigs(keyid,revoked);
194 fetch_key(keyid, &publickey, false);
196 if (publickey != NULL) {
197 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
198 sigs = keysigs(sigs, uids->sigs);
200 if (revoked != NULL) {
201 *revoked = (publickey->revocations != NULL);
203 free_publickey(publickey);
210 * cached_getkeysigs - Gets the signatures on a key.
211 * @keyid: The key we want the signatures for.
213 * This function gets the signatures on a key. It's the same as the
214 * getkeysigs function above except we use the hash module to cache the
215 * data so if we need it again it's already loaded.
217 struct ll *cached_getkeysigs(uint64_t keyid)
219 struct stats_key *key = NULL;
220 struct stats_key *signedkey = NULL;
221 struct ll *cursig = NULL;
222 bool revoked = false;
224 struct dynamic_backend *backend;
230 if (!backend_loaded()) {
234 if (backend_loaded()) {
235 backend = get_backend();
236 if (backend->cached_getkeysigs != NULL) {
237 return backend->cached_getkeysigs(keyid);
241 key = createandaddtohash(keyid);
243 if (key->gotsigs == false) {
244 key->sigs = getkeysigs(key->keyid, &revoked);
245 key->revoked = revoked;
246 for (cursig = key->sigs; cursig != NULL;
247 cursig = cursig->next) {
248 signedkey = (struct stats_key *) cursig->object;
249 signedkey->signs = lladd(signedkey->signs, key);
258 * getfullkeyid - Maps a 32bit key id to a 64bit one.
259 * @keyid: The 32bit keyid.
261 * This function maps a 32bit key id to the full 64bit one. It returns the
262 * full keyid. If the key isn't found a keyid of 0 is returned.
264 uint64_t getfullkeyid(uint64_t keyid)
266 struct openpgp_publickey *publickey = NULL;
267 struct dynamic_backend *backend;
269 if (!backend_loaded()) {
273 if (backend_loaded()) {
274 backend = get_backend();
275 if (backend->getfullkeyid != NULL) {
276 return backend->getfullkeyid(keyid);
280 if (keyid < 0x100000000LL) {
281 fetch_key(keyid, &publickey, false);
282 if (publickey != NULL) {
283 keyid = get_keyid(publickey);
284 free_publickey(publickey);
295 * update_keys - Takes a list of public keys and updates them in the DB.
296 * @keys: The keys to update in the DB.
297 * @sendsync: Should we send a sync mail to our peers.
299 * Takes a list of keys and adds them to the database, merging them with
300 * the key in the database if it's already present there. The key list is
301 * update to contain the minimum set of updates required to get from what
302 * we had before to what we have now (ie the set of data that was added to
303 * the DB). Returns the number of entirely new keys added.
305 int update_keys(struct openpgp_publickey **keys, bool sendsync)
307 struct openpgp_publickey *curkey = NULL;
308 struct openpgp_publickey *oldkey = NULL;
309 struct openpgp_publickey *prev = NULL;
310 struct dynamic_backend *backend;
314 if (!backend_loaded()) {
318 if (backend_loaded()) {
319 backend = get_backend();
320 if (backend->update_keys != NULL) {
321 return backend->update_keys(keys, sendsync);
325 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
326 intrans = starttrans();
327 logthing(LOGTHING_INFO,
328 "Fetching key 0x%llX, result: %d",
330 fetch_key(get_keyid(curkey), &oldkey, intrans));
333 * If we already have the key stored in the DB then merge it
334 * with the new one that's been supplied. Otherwise the key
335 * we've just got is the one that goes in the DB and also the
336 * one that we send out.
338 if (oldkey != NULL) {
339 merge_keys(oldkey, curkey);
340 if (curkey->revocations == NULL &&
341 curkey->uids == NULL &&
342 curkey->subkeys == NULL) {
344 *keys = curkey->next;
346 prev->next = curkey->next;
348 free_publickey(curkey);
353 logthing(LOGTHING_INFO,
354 "Merged key; storing updated key.");
355 store_key(oldkey, intrans, true);
357 free_publickey(oldkey);
361 logthing(LOGTHING_INFO,
362 "Storing completely new key.");
363 store_key(curkey, intrans, false);
370 if (sendsync && keys != NULL) {
377 void initdb(bool readonly)
379 struct dynamic_backend *backend;
380 backend = get_backend();
382 if (!backend_loaded()) {
386 if (backend->loaded) {
387 if (backend->initdb != NULL) {
388 backend->initdb(readonly);
395 struct dynamic_backend *backend;
396 backend = get_backend();
398 if (backend->loaded) {
399 if (backend->cleanupdb != NULL) {
400 backend->cleanupdb();
409 struct dynamic_backend *backend;
410 backend = get_backend();
412 if (!backend_loaded()) {
416 if (backend->loaded) {
417 if (backend->starttrans != NULL) {
418 return backend->starttrans();
427 struct dynamic_backend *backend;
428 backend = get_backend();
430 if (!backend_loaded()) {
434 if (backend->loaded) {
435 if (backend->endtrans != NULL) {
441 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
444 struct dynamic_backend *backend;
445 backend = get_backend();
447 if (!backend_loaded()) {
451 if (backend->loaded) {
452 if (backend->fetch_key != NULL) {
453 return backend->fetch_key(keyid,publickey,intrans);
460 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
462 struct dynamic_backend *backend;
463 backend = get_backend();
465 if (!backend_loaded()) {
469 if (backend->loaded) {
470 if (backend->store_key != NULL) {
471 return backend->store_key(publickey,intrans,update);
478 int delete_key(uint64_t keyid, bool intrans)
480 struct dynamic_backend *backend;
481 backend = get_backend();
483 if (!backend_loaded()) {
487 if (backend->loaded) {
488 if (backend->delete_key != NULL) {
489 return backend->delete_key(keyid, intrans);
496 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
498 struct dynamic_backend *backend;
499 backend = get_backend();
501 if (!backend_loaded()) {
505 if (backend->loaded) {
506 if (backend->fetch_key_text != NULL) {
507 return backend->fetch_key_text(search, publickey);
514 int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
517 struct dynamic_backend *backend;
518 backend = get_backend();
520 if (!backend_loaded()) {
524 if (backend->loaded) {
525 if (backend->iterate_keys != NULL) {
526 return backend->iterate_keys(iterfunc, ctx);