]> git.sommitrealweird.co.uk Git - onak.git/blob - keydb_dynamic.c
ae2b8056870dba7a2a27c75c37ac5193fd275490
[onak.git] / keydb_dynamic.c
1 /*
2  * keydb_dynamic.c - backend that can load the other backends
3  *
4  * Brett Parker <iDunno@sommitrealweird.co.uk>
5  *
6  * Copyright 2005 Project Purple
7  */
8
9 #include <dlfcn.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "decodekey.h"
14 #include "hash.h"
15 #include "keydb.h"
16 #include "keyid.h"
17 #include "keystructs.h"
18 #include "log.h"
19 #include "mem.h"
20 #include "merge.h"
21 #include "onak-conf.h"
22 #include "openpgp.h"
23 #include "parsekey.h"
24 #include "sendsync.h"
25
26 static struct dbfuncs *loaded_backend = NULL;
27 static void *backend_handle;
28
29 static bool close_backend(void)
30 {
31         loaded_backend = NULL;
32         dlclose(backend_handle);
33         backend_handle = NULL;
34
35         return true;
36 }
37
38 static bool load_backend(void)
39 {
40         char *soname = NULL;
41         char *funcsname = NULL;
42
43         if (loaded_backend != NULL) {
44                 close_backend();
45                 loaded_backend = NULL;
46         }
47
48         if (config.use_keyd) {
49                 free(config.db_backend);
50                 config.db_backend = strdup("keyd");
51         }
52
53         if (!config.db_backend) {
54                 logthing(LOGTHING_CRITICAL, "No database backend defined.");
55                 exit(EXIT_FAILURE);
56         }
57
58         if (config.backends_dir == NULL) {
59                 soname = malloc(strlen(config.db_backend)
60                         + strlen("./libkeydb_")
61                         + strlen(".so")
62                         + 1);
63
64                 sprintf(soname, "./libkeydb_%s.so", config.db_backend);
65         } else {
66                 soname = malloc(strlen(config.db_backend)
67                         + strlen("/libkeydb_")
68                         + strlen(".so")
69                         + strlen(config.backends_dir)
70                         + 1);
71
72                 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
73                         config.db_backend);
74         }
75                 
76         logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
77
78         backend_handle = dlopen(soname, RTLD_LAZY);
79         if (backend_handle == NULL) {
80                 logthing(LOGTHING_CRITICAL,
81                                 "Failed to open handle to library '%s': %s",
82                                 soname, dlerror());
83                 free(soname);
84                 soname = NULL;
85                 exit(EXIT_FAILURE);
86         }
87
88         funcsname = malloc(strlen(config.db_backend)
89                         + strlen("keydb_")
90                         + strlen("_funcs")
91                         + 1);
92         sprintf(funcsname, "keydb_%s_funcs", config.db_backend);
93
94         loaded_backend = dlsym(backend_handle, funcsname);
95         free(funcsname);
96
97         if (loaded_backend == NULL) {
98                 logthing(LOGTHING_CRITICAL,
99                                 "Failed to find dbfuncs structure in library "
100                                 "'%s' : %s", soname, dlerror());
101                 free(soname);
102                 soname = NULL;
103                 exit(EXIT_FAILURE);
104         }
105         free(soname);
106         soname = NULL;
107
108         return true;
109 }
110
111 static bool dynamic_starttrans()
112 {
113         if (loaded_backend == NULL) {
114                 load_backend();
115         }
116         
117         if (loaded_backend != NULL) {
118                 if (loaded_backend->starttrans != NULL) {
119                         return loaded_backend->starttrans();
120                 }
121         }
122
123         return false;
124 }
125
126 static void dynamic_endtrans()
127 {
128         if (loaded_backend == NULL) {
129                 load_backend();
130         }
131         
132         if (loaded_backend != NULL) {
133                 if (loaded_backend->endtrans != NULL) {
134                         loaded_backend->endtrans();
135                 }
136         }
137 }
138
139 static int dynamic_fetch_key(uint64_t keyid,
140                 struct openpgp_publickey **publickey, bool intrans)
141 {
142         if (loaded_backend == NULL) {
143                 load_backend();
144         }
145         
146         if (loaded_backend != NULL) {
147                 if (loaded_backend->fetch_key != NULL) {
148                         return loaded_backend->fetch_key(keyid,publickey,intrans);
149                 }
150         }
151
152         return -1;
153 }
154
155 static int dynamic_store_key(struct openpgp_publickey *publickey, bool intrans,
156                 bool update)
157 {
158         if (loaded_backend == NULL) {
159                 load_backend();
160         }
161         
162         if (loaded_backend != NULL) {
163                 if (loaded_backend->store_key != NULL) {
164                         return loaded_backend->store_key(publickey,intrans,update);
165                 }
166         }
167
168         return -1;
169 }
170
171 static int dynamic_delete_key(uint64_t keyid, bool intrans)
172 {
173         if (loaded_backend == NULL) {
174                 load_backend();
175         }
176         
177         if (loaded_backend != NULL) {
178                 if (loaded_backend->delete_key != NULL) {
179                         return loaded_backend->delete_key(keyid, intrans);
180                 }
181         }
182
183         return -1;
184 }
185
186 static int dynamic_fetch_key_text(const char *search,
187                 struct openpgp_publickey **publickey)
188 {
189         if (loaded_backend == NULL) {
190                 load_backend();
191         }
192         
193         if (loaded_backend != NULL) {
194                 if (loaded_backend->fetch_key_text != NULL) {
195                         return loaded_backend->fetch_key_text(search, publickey);
196                 }
197         }
198
199         return -1;
200 }
201
202 static int dynamic_fetch_key_skshash(const struct skshash *hash,
203                 struct openpgp_publickey **publickey)
204 {
205         if (loaded_backend == NULL) {
206                 load_backend();
207         }
208         
209         if (loaded_backend != NULL) {
210                 if (loaded_backend->fetch_key_skshash != NULL) {
211                         return loaded_backend->fetch_key_skshash(hash,
212                                                                 publickey);
213                 }
214         }
215
216         return -1;
217 }
218
219 static int dynamic_iterate_keys(void (*iterfunc)(void *ctx,
220                 struct openpgp_publickey *key), void *ctx)
221 {
222         if (loaded_backend == NULL) {
223                 load_backend();
224         }
225         
226         if (loaded_backend != NULL) {
227                 if (loaded_backend->iterate_keys != NULL) {
228                         return loaded_backend->iterate_keys(iterfunc, ctx);
229                 }
230         }
231
232         return -1;
233 }
234
235 /**
236  *      keyid2uid - Takes a keyid and returns the primary UID for it.
237  *      @keyid: The keyid to lookup.
238  */
239 static char *dynamic_keyid2uid(uint64_t keyid)
240 {
241         struct openpgp_publickey *publickey = NULL;
242         struct openpgp_signedpacket_list *curuid = NULL;
243         char buf[1024];
244
245         if (loaded_backend == NULL) {
246                 load_backend();
247         }
248         
249         if (loaded_backend != NULL) {
250                 if (loaded_backend->keyid2uid != NULL) {
251                         return loaded_backend->keyid2uid(keyid);
252                 }
253         }
254         
255         buf[0]=0;
256         if (dynamic_fetch_key(keyid, &publickey, false) && publickey != NULL) {
257                 curuid = publickey->uids;
258                 while (curuid != NULL && buf[0] == 0) {
259                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
260                                 snprintf(buf, 1023, "%.*s",
261                                                 (int) curuid->packet->length,
262                                                 curuid->packet->data);
263                         }
264                         curuid = curuid -> next;
265                 }
266                 free_publickey(publickey);
267         }
268
269         if (buf[0] == 0) {
270                 return NULL;
271         } else {
272                 return strdup(buf);
273         }
274 }
275
276 /**
277  *      getkeysigs - Gets a linked list of the signatures on a key.
278  *      @keyid: The keyid to get the sigs for.
279  *      @revoked: Is the key revoked?
280  *
281  *      This function gets the list of signatures on a key. Used for key 
282  *      indexing and doing stats bits. If revoked is non-NULL then if the key
283  *      is revoked it's set to true.
284  */
285 static struct ll *dynamic_getkeysigs(uint64_t keyid, bool *revoked)
286 {
287         struct ll *sigs = NULL;
288         struct openpgp_signedpacket_list *uids = NULL;
289         struct openpgp_publickey *publickey = NULL;
290         
291         if ( loaded_backend == NULL ) {
292                 load_backend();
293         }
294         
295         if (loaded_backend != NULL) {
296                 if (loaded_backend->getkeysigs != NULL) {
297                         return loaded_backend->getkeysigs(keyid,revoked);
298                 }
299         }
300
301         dynamic_fetch_key(keyid, &publickey, false);
302         
303         if (publickey != NULL) {
304                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
305                         sigs = keysigs(sigs, uids->sigs);
306                 }
307                 if (revoked != NULL) {
308                         *revoked = publickey->revoked;
309                 }
310                 free_publickey(publickey);
311         }
312
313         return sigs;
314 }
315
316 /**
317  *      cached_getkeysigs - Gets the signatures on a key.
318  *      @keyid: The key we want the signatures for.
319  *      
320  *      This function gets the signatures on a key. It's the same as the
321  *      getkeysigs function above except we use the hash module to cache the
322  *      data so if we need it again it's already loaded.
323  */
324 static struct ll *dynamic_cached_getkeysigs(uint64_t keyid)
325 {
326         struct stats_key *key = NULL;
327         struct stats_key *signedkey = NULL;
328         struct ll        *cursig = NULL;
329         bool              revoked = false;
330
331         if (keyid == 0)  {
332                 return NULL;
333         }
334         
335         if (loaded_backend == NULL) {
336                 load_backend();
337         }
338         
339         if (loaded_backend != NULL) {
340                 if (loaded_backend->cached_getkeysigs != NULL) {
341                         return loaded_backend->cached_getkeysigs(keyid);
342                 }
343         }
344
345         key = createandaddtohash(keyid);
346
347         if (key->gotsigs == false) {
348                 key->sigs = dynamic_getkeysigs(key->keyid, &revoked);
349                 key->revoked = revoked;
350                 for (cursig = key->sigs; cursig != NULL;
351                                 cursig = cursig->next) {
352                         signedkey = (struct stats_key *) cursig->object;
353                         signedkey->signs = lladd(signedkey->signs, key);
354                 }
355                 key->gotsigs = true;
356         }
357
358         return key->sigs;
359 }
360
361 /**
362  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
363  *      @keyid: The 32bit keyid.
364  *
365  *      This function maps a 32bit key id to the full 64bit one. It returns the
366  *      full keyid. If the key isn't found a keyid of 0 is returned.
367  */
368 static uint64_t dynamic_getfullkeyid(uint64_t keyid)
369 {
370         struct openpgp_publickey *publickey = NULL;
371
372         if (loaded_backend == NULL) {
373                 load_backend();
374         }
375         
376         if (loaded_backend != NULL) {
377                 if (loaded_backend->getfullkeyid != NULL) {
378                         return loaded_backend->getfullkeyid(keyid);
379                 }
380         }
381
382         if (keyid < 0x100000000LL) {
383                 dynamic_fetch_key(keyid, &publickey, false);
384                 if (publickey != NULL) {
385                         keyid = get_keyid(publickey);
386                         free_publickey(publickey);
387                         publickey = NULL;
388                 } else {
389                         keyid = 0;
390                 }
391         }
392         
393         return keyid;
394 }
395
396 /**
397  *      update_keys - Takes a list of public keys and updates them in the DB.
398  *      @keys: The keys to update in the DB.
399  *      @sendsync: Should we send a sync mail to our peers.
400  *
401  *      Takes a list of keys and adds them to the database, merging them with
402  *      the key in the database if it's already present there. The key list is
403  *      update to contain the minimum set of updates required to get from what
404  *      we had before to what we have now (ie the set of data that was added to
405  *      the DB). Returns the number of entirely new keys added.
406  */
407 static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
408 {
409         struct openpgp_publickey *curkey = NULL;
410         struct openpgp_publickey *oldkey = NULL;
411         struct openpgp_publickey *prev = NULL;
412         int newkeys = 0;
413         bool intrans;
414         
415         if (loaded_backend == NULL) {
416                 load_backend();
417         }
418         
419         if (loaded_backend != NULL) {
420                 if (loaded_backend->update_keys != NULL) {
421                         return loaded_backend->update_keys(keys, sendsync);
422                 }
423         }
424
425         for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
426                 intrans = dynamic_starttrans();
427                 logthing(LOGTHING_INFO,
428                         "Fetching key 0x%" PRIX64 ", result: %d",
429                         get_keyid(curkey),
430                         dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
431
432                 /*
433                  * If we already have the key stored in the DB then merge it
434                  * with the new one that's been supplied. Otherwise the key
435                  * we've just got is the one that goes in the DB and also the
436                  * one that we send out.
437                  */
438                 if (oldkey != NULL) {
439                         merge_keys(oldkey, curkey);
440                         if (curkey->sigs == NULL &&
441                                         curkey->uids == NULL &&
442                                         curkey->subkeys == NULL) {
443                                 if (prev == NULL) {
444                                         *keys = curkey->next;
445                                 } else {
446                                         prev->next = curkey->next;
447                                         curkey->next = NULL;
448                                         free_publickey(curkey);
449                                         curkey = prev;
450                                 }
451                         } else {
452                                 prev = curkey;
453                                 logthing(LOGTHING_INFO,
454                                         "Merged key; storing updated key.");
455                                 dynamic_store_key(oldkey, intrans, true);
456                         }
457                         free_publickey(oldkey);
458                         oldkey = NULL;
459                 
460                 } else {
461                         logthing(LOGTHING_INFO,
462                                 "Storing completely new key.");
463                         dynamic_store_key(curkey, intrans, false);
464                         newkeys++;
465                 }
466                 dynamic_endtrans();
467                 intrans = false;
468         }
469
470         if (sendsync && keys != NULL) {
471                 sendkeysync(*keys);
472         }
473
474         return newkeys;
475 }
476
477 static void dynamic_initdb(bool readonly)
478 {
479         if (loaded_backend == NULL) {
480                 load_backend();
481         }
482
483         if (loaded_backend != NULL) {
484                 if (loaded_backend->initdb != NULL) {
485                         loaded_backend->initdb(readonly);
486                 }
487         }
488 }
489
490 static void dynamic_cleanupdb(void)
491 {
492         if (loaded_backend != NULL) {
493                 if (loaded_backend->cleanupdb != NULL) {
494                         loaded_backend->cleanupdb();
495                 }
496         }
497
498         close_backend();
499 }
500
501 struct dbfuncs keydb_dynamic_funcs = {
502         .initdb                 = dynamic_initdb,
503         .cleanupdb              = dynamic_cleanupdb,
504         .starttrans             = dynamic_starttrans,
505         .endtrans               = dynamic_endtrans,
506         .fetch_key              = dynamic_fetch_key,
507         .fetch_key_text         = dynamic_fetch_key_text,
508         .fetch_key_skshash      = dynamic_fetch_key_skshash,
509         .store_key              = dynamic_store_key,
510         .update_keys            = dynamic_update_keys,
511         .delete_key             = dynamic_delete_key,
512         .getkeysigs             = dynamic_getkeysigs,
513         .cached_getkeysigs      = dynamic_cached_getkeysigs,
514         .keyid2uid              = dynamic_keyid2uid,
515         .getfullkeyid           = dynamic_getfullkeyid,
516         .iterate_keys           = dynamic_iterate_keys,
517 };