]> the.earth.li Git - onak.git/blob - keydb_db4.c
Move mailsync functionality to the database backends.
[onak.git] / keydb_db4.c
1 /*
2  * keydb_db4.c - Routines to store and fetch keys in a DB3 database.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002-2004 Project Purple
7  */
8
9 #include <sys/types.h>
10 #include <sys/uio.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include <db.h>
20
21 #include "charfuncs.h"
22 #include "keydb.h"
23 #include "keyid.h"
24 #include "decodekey.h"
25 #include "keystructs.h"
26 #include "mem.h"
27 #include "log.h"
28 #include "onak-conf.h"
29 #include "parsekey.h"
30 #include "wordlist.h"
31
32 /**
33  *      dbenv - our database environment.
34  */
35 static DB_ENV *dbenv = NULL;
36
37 /**
38  *      numdb - The number of database files we have.
39  */
40 static int numdbs = 16;
41
42 /**
43  *      dbconn - our connections to the key database files.
44  */
45 static DB **dbconns = NULL;
46
47 /**
48  *      worddb - our connection to the word database.
49  */
50 static DB *worddb = NULL;
51
52 /**
53  *      id32db - our connection to the 32bit ID database.
54  */
55 static DB *id32db = NULL;
56
57 /**
58  *      txn - our current transaction id.
59  */
60 static DB_TXN *txn = NULL;
61
62 DB *keydb(uint64_t keyid)
63 {
64         uint64_t keytrun;
65
66         keytrun = keyid >> 8;
67
68         return(dbconns[keytrun % numdbs]);
69 }
70
71 /**
72  *      initdb - Initialize the key database.
73  *
74  *      This function should be called before any of the other functions in
75  *      this file are called in order to allow the DB to be initialized ready
76  *      for access.
77  */
78 void initdb(bool readonly)
79 {
80         char       buf[1024];
81         FILE      *numdb = NULL;
82         int        ret = 0;
83         int        i = 0;
84         u_int32_t  flags = 0;
85
86         snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
87         numdb = fopen(buf, "r");
88         if (numdb != NULL) {
89                 if (fgets(buf, sizeof(buf), numdb) != NULL) {
90                         numdbs = atoi(buf);
91                 }
92                 fclose(numdb);
93         } else if (!readonly) {
94                 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
95                                 strerror(errno));
96                 numdb = fopen(buf, "w");
97                 if (numdb != NULL) {
98                         fprintf(numdb, "%d", numdbs);
99                         fclose(numdb);
100                 } else {
101                         logthing(LOGTHING_ERROR,
102                                 "Couldn't write num_keydb: %s",
103                                 strerror(errno));
104                 }
105         }
106
107         dbconns = malloc(sizeof (DB *) * numdbs);
108         if (dbconns == NULL) {
109                 logthing(LOGTHING_CRITICAL,
110                                 "Couldn't allocate memory for dbconns");
111                 exit(1);
112         }
113
114         ret = db_env_create(&dbenv, 0);
115         if (ret != 0) {
116                 logthing(LOGTHING_CRITICAL,
117                         "db_env_create: %s", db_strerror(ret));
118                 exit(1);
119         }
120
121         /*
122          * Enable deadlock detection so that we don't block indefinitely on
123          * anything. What we really want is simple 2 state locks, but I'm not
124          * sure how to make the standard DB functions do that yet.
125          */
126         ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
127         if (ret != 0) {
128                 logthing(LOGTHING_CRITICAL,
129                         "db_env_create: %s", db_strerror(ret));
130                 exit(1);
131         }
132
133         ret = dbenv->open(dbenv, config.db_dir,
134                         DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
135                         DB_INIT_TXN |
136                         DB_CREATE,
137                         0);
138         if (ret != 0) {
139                 logthing(LOGTHING_CRITICAL,
140                                 "Error opening db environment: %s (%s)",
141                                 config.db_dir,
142                                 db_strerror(ret));
143                 exit(1);
144         }
145
146         starttrans();
147
148         for (i = 0; i < numdbs; i++) {
149                 ret = db_create(&dbconns[i], dbenv, 0);
150                 if (ret != 0) {
151                         logthing(LOGTHING_CRITICAL,
152                                 "db_create: %s", db_strerror(ret));
153                         exit(1);
154                 }
155
156                 snprintf(buf, 1023, "keydb.%d.db", i);
157                 flags = DB_CREATE;
158                 if (readonly) {
159                         flags = DB_RDONLY;
160                 }
161                 ret = dbconns[i]->open(dbconns[i],
162                                 txn,
163                                 buf,
164                                 "keydb",
165                                 DB_HASH,
166                                 flags,
167                                 0664);
168                 if (ret != 0) {
169                         logthing(LOGTHING_CRITICAL,
170                                 "Error opening key database: %s (%s)",
171                                 buf,
172                                 db_strerror(ret));
173                         exit(1);
174                 }
175         }
176
177         ret = db_create(&worddb, dbenv, 0);
178         if (ret != 0) {
179                 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
180                 exit(1);
181         }
182         ret = worddb->set_flags(worddb, DB_DUP);
183
184         ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
185                         flags,
186                         0664);
187         if (ret != 0) {
188                 logthing(LOGTHING_CRITICAL,
189                                 "Error opening word database: %s (%s)",
190                                 "worddb",
191                                 db_strerror(ret));
192                 exit(1);
193         }
194
195         ret = db_create(&id32db, dbenv, 0);
196         if (ret != 0) {
197                 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
198                 exit(1);
199         }
200         ret = id32db->set_flags(id32db, DB_DUP);
201
202         ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
203                         flags,
204                         0664);
205         if (ret != 0) {
206                 logthing(LOGTHING_CRITICAL,
207                                 "Error opening id32 database: %s (%s)",
208                                 "id32db",
209                                 db_strerror(ret));
210                 exit(1);
211         }
212         endtrans();
213         
214         return;
215 }
216
217 /**
218  *      cleanupdb - De-initialize the key database.
219  *
220  *      This function should be called upon program exit to allow the DB to
221  *      cleanup after itself.
222  */
223 void cleanupdb(void)
224 {
225         int i = 0;
226
227         if (dbenv != NULL) {
228                 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
229                 if (id32db != NULL) {
230                         id32db->close(id32db, 0);
231                         id32db = NULL;
232                 }
233                 if (worddb != NULL) {
234                         worddb->close(worddb, 0);
235                         worddb = NULL;
236                 }
237                 for (i = 0; i < numdbs; i++) {
238                         if (dbconns[i] != NULL) {
239                                 dbconns[i]->close(dbconns[i], 0);
240                                 dbconns[i] = NULL;
241                         }
242                 }
243                 dbenv->close(dbenv, 0);
244                 dbenv = NULL;
245         }
246 }
247
248 /**
249  *      starttrans - Start a transaction.
250  *
251  *      Start a transaction. Intended to be used if we're about to perform many
252  *      operations on the database to help speed it all up, or if we want
253  *      something to only succeed if all relevant operations are successful.
254  */
255 bool starttrans(void)
256 {
257         int ret;
258
259         log_assert(dbenv != NULL);
260         log_assert(txn == NULL);
261
262         ret = dbenv->txn_begin(dbenv,
263                 NULL, /* No parent transaction */
264                 &txn,
265                 0);
266         if (ret != 0) {
267                 logthing(LOGTHING_CRITICAL,
268                                 "Error starting transaction: %s",
269                                 db_strerror(ret));
270                 exit(1);
271         }
272
273         return true;
274 }
275
276 /**
277  *      endtrans - End a transaction.
278  *
279  *      Ends a transaction.
280  */
281 void endtrans(void)
282 {
283         int ret;
284
285         log_assert(dbenv != NULL);
286         log_assert(txn != NULL);
287
288         ret = txn->commit(txn,
289                 0);
290         if (ret != 0) {
291                 logthing(LOGTHING_CRITICAL,
292                                 "Error ending transaction: %s",
293                                 db_strerror(ret));
294                 exit(1);
295         }
296         txn = NULL;
297
298         return;
299 }
300
301 /**
302  *      fetch_key - Given a keyid fetch the key from storage.
303  *      @keyid: The keyid to fetch.
304  *      @publickey: A pointer to a structure to return the key in.
305  *      @intrans: If we're already in a transaction.
306  *
307  *      We use the hex representation of the keyid as the filename to fetch the
308  *      key from. The key is stored in the file as a binary OpenPGP stream of
309  *      packets, so we can just use read_openpgp_stream() to read the packets
310  *      in and then parse_keys() to parse the packets into a publickey
311  *      structure.
312  */
313 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
314                 bool intrans)
315 {
316         struct openpgp_packet_list *packets = NULL;
317         DBT key, data;
318         int ret = 0;
319         int numkeys = 0;
320         struct buffer_ctx fetchbuf;
321
322         if (keyid < 0x100000000LL) {
323                 keyid = getfullkeyid(keyid);
324         }
325
326         memset(&key, 0, sizeof(key));
327         memset(&data, 0, sizeof(data));
328
329         data.size = 0;
330         data.data = NULL;
331
332         key.size = sizeof(keyid);
333         key.data = &keyid;
334
335         if (!intrans) {
336                 starttrans();
337         }
338
339         ret = keydb(keyid)->get(keydb(keyid),
340                         txn,
341                         &key,
342                         &data,
343                         0); /* flags*/
344         
345         if (ret == 0) {
346                 fetchbuf.buffer = data.data;
347                 fetchbuf.offset = 0;
348                 fetchbuf.size = data.size;
349                 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
350                                 &packets, 0);
351                 parse_keys(packets, publickey);
352                 free_packet_list(packets);
353                 packets = NULL;
354                 numkeys++;
355         } else if (ret != DB_NOTFOUND) {
356                 logthing(LOGTHING_ERROR,
357                                 "Problem retrieving key: %s",
358                                 db_strerror(ret));
359         }
360
361         if (!intrans) {
362                 endtrans();
363         }
364
365         return (numkeys);
366 }
367
368 int worddb_cmp(const void *d1, const void *d2)
369 {
370         return memcmp(d1, d2, 12);
371 }
372
373 /**
374  *      fetch_key_text - Trys to find the keys that contain the supplied text.
375  *      @search: The text to search for.
376  *      @publickey: A pointer to a structure to return the key in.
377  *
378  *      This function searches for the supplied text and returns the keys that
379  *      contain it.
380  */
381 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
382 {
383         DBC *cursor = NULL;
384         DBT key, data;
385         int ret;
386         uint64_t keyid;
387         int i;
388         int numkeys;
389         char *searchtext = NULL;
390         struct ll *wordlist = NULL;
391         struct ll *curword = NULL;
392         struct ll *keylist = NULL;
393         struct ll *newkeylist = NULL;
394
395         numkeys = 0;
396         searchtext = strdup(search);
397         wordlist = makewordlist(wordlist, searchtext);
398
399         starttrans();
400
401         ret = worddb->cursor(worddb,
402                         txn,
403                         &cursor,
404                         0);   /* flags */
405
406         for (curword = wordlist; curword != NULL; curword = curword->next) {
407                 memset(&key, 0, sizeof(key));
408                 memset(&data, 0, sizeof(data));
409                 key.data = curword->object;
410                 key.size = strlen(curword->object);
411                 data.flags = DB_DBT_MALLOC;
412                 ret = cursor->c_get(cursor,
413                                 &key,
414                                 &data,
415                                 DB_SET);
416                 while (ret == 0 && strncmp(key.data, curword->object,
417                                         key.size) == 0 &&
418                                 ((char *) curword->object)[key.size] == 0) {
419                         keyid = 0;
420                         for (i = 4; i < 12; i++) {
421                                 keyid <<= 8;
422                                 keyid += ((unsigned char *)
423                                                 data.data)[i];
424                         }
425
426                         if (keylist == NULL ||
427                                         llfind(keylist, data.data,
428                                                 worddb_cmp) != NULL) {
429                                 newkeylist = lladd(newkeylist, data.data);
430                                 data.data = NULL;
431                         } else {
432                                 free(data.data);
433                                 data.data = NULL;
434                         }
435                         ret = cursor->c_get(cursor,
436                                         &key,
437                                         &data,
438                                         DB_NEXT);
439                 }
440                 llfree(keylist, free);
441                 keylist = newkeylist;
442                 newkeylist = NULL;
443                 if (data.data != NULL) {
444                         free(data.data);
445                         data.data = NULL;
446                 }
447         }
448         llfree(wordlist, NULL);
449         wordlist = NULL;
450         
451         for (newkeylist = keylist;
452                         newkeylist != NULL && numkeys < config.maxkeys;
453                         newkeylist = newkeylist->next) {
454
455                         keyid = 0;
456                         for (i = 4; i < 12; i++) {
457                                 keyid <<= 8;
458                                 keyid += ((unsigned char *)
459                                                 newkeylist->object)[i];
460                         }
461
462                         numkeys += fetch_key(keyid,
463                                         publickey,
464                                         true);
465         }
466         llfree(keylist, free);
467         keylist = NULL;
468         free(searchtext);
469         searchtext = NULL;
470
471         ret = cursor->c_close(cursor);
472         cursor = NULL;
473
474         endtrans();
475         
476         return (numkeys);
477 }
478
479 /**
480  *      store_key - Takes a key and stores it.
481  *      @publickey: A pointer to the public key to store.
482  *      @intrans: If we're already in a transaction.
483  *      @update: If true the key exists and should be updated.
484  *
485  *      Again we just use the hex representation of the keyid as the filename
486  *      to store the key to. We flatten the public key to a list of OpenPGP
487  *      packets and then use write_openpgp_stream() to write the stream out to
488  *      the file. If update is true then we delete the old key first, otherwise
489  *      we trust that it doesn't exist.
490  */
491 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
492 {
493         struct     openpgp_packet_list *packets = NULL;
494         struct     openpgp_packet_list *list_end = NULL;
495         struct     openpgp_publickey *next = NULL;
496         int        ret = 0;
497         int        i = 0;
498         struct     buffer_ctx storebuf;
499         DBT        key;
500         DBT        data;
501         uint64_t   keyid = 0;
502         uint32_t   shortkeyid = 0;
503         uint64_t  *subkeyids = NULL;
504         char     **uids = NULL;
505         char      *primary = NULL;
506         unsigned char worddb_data[12];
507         struct ll *wordlist = NULL;
508         struct ll *curword  = NULL;
509         bool       deadlock = false;
510
511         keyid = get_keyid(publickey);
512
513         if (!intrans) {
514                 starttrans();
515         }
516
517         /*
518          * Delete the key if we already have it.
519          *
520          * TODO: Can we optimize this perhaps? Possibly when other data is
521          * involved as well? I suspect this is easiest and doesn't make a lot
522          * of difference though - the largest chunk of data is the keydata and
523          * it definitely needs updated.
524          */
525         if (update) {
526                 deadlock = (delete_key(keyid, true) == -1);
527         }
528
529         /*
530          * Convert the key to a flat set of binary data.
531          */
532         if (!deadlock) {
533                 next = publickey->next;
534                 publickey->next = NULL;
535                 flatten_publickey(publickey, &packets, &list_end);
536                 publickey->next = next;
537
538                 storebuf.offset = 0; 
539                 storebuf.size = 8192;
540                 storebuf.buffer = malloc(8192);
541         
542                 write_openpgp_stream(buffer_putchar, &storebuf, packets);
543
544                 /*
545                  * Now we have the key data store it in the DB; the keyid is
546                  * the key.
547                  */
548                 memset(&key, 0, sizeof(key));
549                 memset(&data, 0, sizeof(data));
550                 key.data = &keyid;
551                 key.size = sizeof(keyid);
552                 data.size = storebuf.offset;
553                 data.data = storebuf.buffer;
554
555                 ret = keydb(keyid)->put(keydb(keyid),
556                                 txn,
557                                 &key,
558                                 &data,
559                                 0); /* flags*/
560                 if (ret != 0) {
561                         logthing(LOGTHING_ERROR,
562                                         "Problem storing key: %s",
563                                         db_strerror(ret));
564                         if (ret == DB_LOCK_DEADLOCK) {
565                                 deadlock = true;
566                         }
567                 }
568
569                 free(storebuf.buffer);
570                 storebuf.buffer = NULL;
571                 storebuf.size = 0;
572                 storebuf.offset = 0; 
573         
574                 free_packet_list(packets);
575                 packets = NULL;
576         }
577
578         /*
579          * Walk through our uids storing the words into the db with the keyid.
580          */
581         if (!deadlock) {
582                 uids = keyuids(publickey, &primary);
583         }
584         if (uids != NULL) {
585                 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
586                         wordlist = makewordlist(wordlist, uids[i]);
587                 }
588
589                 for (curword = wordlist; curword != NULL && !deadlock;
590                                 curword = curword->next) {
591                         memset(&key, 0, sizeof(key));
592                         memset(&data, 0, sizeof(data));
593                         key.data = curword->object;
594                         key.size = strlen(key.data);
595                         data.data = worddb_data;
596                         data.size = sizeof(worddb_data);
597
598                         /*
599                          * Our data is the key creation time followed by the
600                          * key id.
601                          */
602                         worddb_data[ 0] = publickey->publickey->data[1];
603                         worddb_data[ 1] = publickey->publickey->data[2];
604                         worddb_data[ 2] = publickey->publickey->data[3];
605                         worddb_data[ 3] = publickey->publickey->data[4];
606                         worddb_data[ 4] = (keyid >> 56) & 0xFF;
607                         worddb_data[ 5] = (keyid >> 48) & 0xFF;
608                         worddb_data[ 6] = (keyid >> 40) & 0xFF;
609                         worddb_data[ 7] = (keyid >> 32) & 0xFF;
610                         worddb_data[ 8] = (keyid >> 24) & 0xFF;
611                         worddb_data[ 9] = (keyid >> 16) & 0xFF;
612                         worddb_data[10] = (keyid >>  8) & 0xFF;
613                         worddb_data[11] = keyid & 0xFF; 
614                         ret = worddb->put(worddb,
615                                 txn,
616                                 &key,
617                                 &data,
618                                 0);
619                         if (ret != 0) {
620                                 logthing(LOGTHING_ERROR,
621                                         "Problem storing word: %s",
622                                         db_strerror(ret));
623                                 if (ret == DB_LOCK_DEADLOCK) {
624                                         deadlock = true;
625                                 }
626                         }
627                 }
628
629                 /*
630                  * Free our UID and word lists.
631                  */
632                 llfree(wordlist, NULL);
633                 for (i = 0; uids[i] != NULL; i++) {
634                         free(uids[i]);
635                         uids[i] = NULL;
636                 }
637                 free(uids);
638                 uids = NULL;
639         }
640
641         /*
642          * Write the truncated 32 bit keyid so we can lookup the full id for
643          * queries.
644          */
645         if (!deadlock) {
646                 shortkeyid = keyid & 0xFFFFFFFF;
647
648                 memset(&key, 0, sizeof(key));
649                 memset(&data, 0, sizeof(data));
650                 key.data = &shortkeyid;
651                 key.size = sizeof(shortkeyid);
652                 data.data = &keyid;
653                 data.size = sizeof(keyid);
654
655                 ret = id32db->put(id32db,
656                         txn,
657                         &key,
658                         &data,
659                         0);
660                 if (ret != 0) {
661                         logthing(LOGTHING_ERROR,
662                                 "Problem storing short keyid: %s",
663                                 db_strerror(ret));
664                         if (ret == DB_LOCK_DEADLOCK) {
665                                 deadlock = true;
666                         }
667                 }
668         }
669
670         if (!deadlock) {
671                 subkeyids = keysubkeys(publickey);
672                 i = 0;
673                 while (subkeyids != NULL && subkeyids[i] != 0) {
674                         shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
675
676                         memset(&key, 0, sizeof(key));
677                         memset(&data, 0, sizeof(data));
678                         key.data = &shortkeyid;
679                         key.size = sizeof(shortkeyid);
680                         data.data = &keyid;
681                         data.size = sizeof(keyid);
682
683                         ret = id32db->put(id32db,
684                                 txn,
685                                 &key,
686                                 &data,
687                                 0);
688                         if (ret != 0) {
689                                 logthing(LOGTHING_ERROR,
690                                         "Problem storing short keyid: %s",
691                                         db_strerror(ret));
692                                 if (ret == DB_LOCK_DEADLOCK) {
693                                         deadlock = true;
694                                 }
695                         }
696                 }
697                 if (subkeyids != NULL) {
698                         free(subkeyids);
699                         subkeyids = NULL;
700                 }
701         }
702
703         if (!intrans) {
704                 endtrans();
705         }
706
707         return deadlock ? -1 : 0 ;
708 }
709
710 /**
711  *      delete_key - Given a keyid delete the key from storage.
712  *      @keyid: The keyid to delete.
713  *      @intrans: If we're already in a transaction.
714  *
715  *      This function deletes a public key from whatever storage mechanism we
716  *      are using. Returns 0 if the key existed.
717  */
718 int delete_key(uint64_t keyid, bool intrans)
719 {
720         struct openpgp_publickey *publickey = NULL;
721         DBT key, data;
722         DBC *cursor = NULL;
723         uint32_t   shortkeyid = 0;
724         uint64_t  *subkeyids = NULL;
725         int ret = 0;
726         int i;
727         char **uids = NULL;
728         char *primary = NULL;
729         unsigned char worddb_data[12];
730         struct ll *wordlist = NULL;
731         struct ll *curword  = NULL;
732         bool deadlock = false;
733
734         if (!intrans) {
735                 starttrans();
736         }
737
738         fetch_key(keyid, &publickey, true);
739
740         /*
741          * Walk through the uids removing the words from the worddb.
742          */
743         if (publickey != NULL) {
744                 uids = keyuids(publickey, &primary);
745         }
746         if (uids != NULL) {
747                 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
748                         wordlist = makewordlist(wordlist, uids[i]);
749                 }
750                                 
751                 ret = worddb->cursor(worddb,
752                         txn,
753                         &cursor,
754                         0);   /* flags */
755
756                 for (curword = wordlist; curword != NULL && !deadlock;
757                                 curword = curword->next) {
758                         memset(&key, 0, sizeof(key));
759                         memset(&data, 0, sizeof(data));
760                         key.data = curword->object;
761                         key.size = strlen(key.data);
762                         data.data = worddb_data;
763                         data.size = sizeof(worddb_data);
764
765                         /*
766                          * Our data is the key creation time followed by the
767                          * key id.
768                          */
769                         worddb_data[ 0] = publickey->publickey->data[1];
770                         worddb_data[ 1] = publickey->publickey->data[2];
771                         worddb_data[ 2] = publickey->publickey->data[3];
772                         worddb_data[ 3] = publickey->publickey->data[4];
773                         worddb_data[ 4] = (keyid >> 56) & 0xFF;
774                         worddb_data[ 5] = (keyid >> 48) & 0xFF;
775                         worddb_data[ 6] = (keyid >> 40) & 0xFF;
776                         worddb_data[ 7] = (keyid >> 32) & 0xFF;
777                         worddb_data[ 8] = (keyid >> 24) & 0xFF;
778                         worddb_data[ 9] = (keyid >> 16) & 0xFF;
779                         worddb_data[10] = (keyid >>  8) & 0xFF;
780                         worddb_data[11] = keyid & 0xFF; 
781
782                         ret = cursor->c_get(cursor,
783                                 &key,
784                                 &data,
785                                 DB_GET_BOTH);
786
787                         if (ret == 0) {
788                                 ret = cursor->c_del(cursor, 0);
789                                 if (ret != 0) {
790                                         logthing(LOGTHING_ERROR,
791                                                 "Problem deleting word: %s",
792                                                 db_strerror(ret));
793                                 }
794                         }
795
796                         if (ret != 0) {
797                                 logthing(LOGTHING_ERROR,
798                                         "Problem deleting word: %s",
799                                         db_strerror(ret));
800                                 if (ret == DB_LOCK_DEADLOCK) {
801                                         deadlock = true;
802                                 }
803                         }
804                 }
805                 ret = cursor->c_close(cursor);
806                 cursor = NULL;
807
808                 /*
809                  * Free our UID and word lists.
810                  */
811                 llfree(wordlist, NULL);
812                 for (i = 0; uids[i] != NULL; i++) {
813                         free(uids[i]);
814                         uids[i] = NULL;
815                 }
816                 free(uids);
817                 uids = NULL;
818                 free_publickey(publickey);
819                 publickey = NULL;
820         }
821
822         if (!deadlock) {
823                 ret = id32db->cursor(id32db,
824                         txn,
825                         &cursor,
826                         0);   /* flags */
827
828                 shortkeyid = keyid & 0xFFFFFFFF;
829
830                 memset(&key, 0, sizeof(key));
831                 memset(&data, 0, sizeof(data));
832                 key.data = &shortkeyid;
833                 key.size = sizeof(shortkeyid);
834                 data.data = &keyid;
835                 data.size = sizeof(keyid);
836
837                 ret = cursor->c_get(cursor,
838                         &key,
839                         &data,
840                         DB_GET_BOTH);
841
842                 if (ret == 0) {
843                         ret = cursor->c_del(cursor, 0);
844                         if (ret != 0) {
845                                 logthing(LOGTHING_ERROR,
846                                         "Problem deleting short keyid: %s",
847                                         db_strerror(ret));
848                         }
849                 }
850
851                 if (ret != 0) {
852                         logthing(LOGTHING_ERROR,
853                                 "Problem deleting short keyid: %s",
854                                 db_strerror(ret));
855                         if (ret == DB_LOCK_DEADLOCK) {
856                                 deadlock = true;
857                         }
858                 }
859
860                 subkeyids = keysubkeys(publickey);
861                 i = 0;
862                 while (subkeyids != NULL && subkeyids[i] != 0) {
863                         shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
864
865                         memset(&key, 0, sizeof(key));
866                         memset(&data, 0, sizeof(data));
867                         key.data = &shortkeyid;
868                         key.size = sizeof(shortkeyid);
869                         data.data = &keyid;
870                         data.size = sizeof(keyid);
871
872                         ret = cursor->c_get(cursor,
873                                 &key,
874                                 &data,
875                                 DB_GET_BOTH);
876
877                         if (ret == 0) {
878                                 ret = cursor->c_del(cursor, 0);
879                                 if (ret != 0) {
880                                         logthing(LOGTHING_ERROR,
881                                                 "Problem deleting short"
882                                                 " keyid: %s",
883                                                 db_strerror(ret));
884                                 }
885                         }
886
887                         if (ret != 0) {
888                                 logthing(LOGTHING_ERROR,
889                                         "Problem deleting short keyid: %s",
890                                         db_strerror(ret));
891                                 if (ret == DB_LOCK_DEADLOCK) {
892                                         deadlock = true;
893                                 }
894                         }
895                 }
896                 if (subkeyids != NULL) {
897                         free(subkeyids);
898                         subkeyids = NULL;
899                 }
900
901                 ret = cursor->c_close(cursor);
902                 cursor = NULL;
903         }
904
905         if (!deadlock) {
906                 key.data = &keyid;
907                 key.size = sizeof(keyid);
908
909                 keydb(keyid)->del(keydb(keyid),
910                                 txn,
911                                 &key,
912                                 0); /* flags */
913         }
914
915         if (!intrans) {
916                 endtrans();
917         }
918
919         return deadlock ? (-1) : (ret == DB_NOTFOUND);
920 }
921
922 /**
923  *      dumpdb - dump the key database
924  *      @filenamebase: The base filename to use for the dump.
925  *
926  *      Dumps the database into one or more files, which contain pure OpenPGP
927  *      that can be reimported into onak or gpg. filenamebase provides a base
928  *      file name for the dump; several files may be created, all of which will
929  *      begin with this string and then have a unique number and a .pgp
930  *      extension.
931  */
932 int dumpdb(char *filenamebase)
933 {
934         DBT   key, data;
935         DBC  *cursor = NULL;
936         int   ret = 0;
937         int   fd = -1;
938         int   i = 0;
939         char  filename[1024];
940
941         filename[1023] = 0;
942         for (i = 0; i < numdbs; i++) {
943                 ret = dbconns[i]->cursor(dbconns[i],
944                         NULL,
945                         &cursor,
946                         0);   /* flags */
947
948                 snprintf(filename, 1023, "%s.%d.pgp", filenamebase, i);
949                 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
950                 if (fd == -1) {
951                         logthing(LOGTHING_ERROR,
952                                 "Error opening keydump file (%s): %s",
953                                 filename,
954                                 strerror(errno));
955                 } else {
956                         memset(&key, 0, sizeof(key));
957                         memset(&data, 0, sizeof(data));
958                         ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
959                         while (ret == 0) {
960                                 write(fd, data.data, data.size);
961                                 memset(&key, 0, sizeof(key));
962                                 memset(&data, 0, sizeof(data));
963                                 ret = cursor->c_get(cursor, &key, &data,
964                                                 DB_NEXT);
965                         }
966                         if (ret != DB_NOTFOUND) {
967                                 logthing(LOGTHING_ERROR,
968                                         "Problem reading key: %s",
969                                         db_strerror(ret));
970                         }
971                         close(fd);
972                 }
973
974                 ret = cursor->c_close(cursor);
975                 cursor = NULL;
976         }
977         
978         return 0;
979 }
980
981 /**
982  *      iterate_keys - call a function once for each key in the db.
983  *      @iterfunc: The function to call.
984  *      @ctx: A context pointer
985  *
986  *      Calls iterfunc once for each key in the database. ctx is passed
987  *      unaltered to iterfunc. This function is intended to aid database dumps
988  *      and statistic calculations.
989  *
990  *      Returns the number of keys we iterated over.
991  */
992 int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
993                 void *ctx)
994 {
995         DBT                         dbkey, data;
996         DBC                        *cursor = NULL;
997         int                         ret = 0;
998         int                         i = 0;
999         int                         numkeys = 0;
1000         struct buffer_ctx           fetchbuf;
1001         struct openpgp_packet_list *packets = NULL;
1002         struct openpgp_publickey   *key = NULL;
1003
1004         for (i = 0; i < numdbs; i++) {
1005                 ret = dbconns[i]->cursor(dbconns[i],
1006                         NULL,
1007                         &cursor,
1008                         0);   /* flags */
1009
1010                 memset(&dbkey, 0, sizeof(dbkey));
1011                 memset(&data, 0, sizeof(data));
1012                 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1013                 while (ret == 0) {
1014                         fetchbuf.buffer = data.data;
1015                         fetchbuf.offset = 0;
1016                         fetchbuf.size = data.size;
1017                         read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1018                                 &packets, 0);
1019                         parse_keys(packets, &key);
1020
1021                         iterfunc(ctx, key);
1022                         
1023                         free_publickey(key);
1024                         key = NULL;
1025                         free_packet_list(packets);
1026                         packets = NULL;
1027                         
1028                         memset(&dbkey, 0, sizeof(dbkey));
1029                         memset(&data, 0, sizeof(data));
1030                         ret = cursor->c_get(cursor, &dbkey, &data,
1031                                         DB_NEXT);
1032                         numkeys++;
1033                 }
1034                 if (ret != DB_NOTFOUND) {
1035                         logthing(LOGTHING_ERROR,
1036                                 "Problem reading key: %s",
1037                                 db_strerror(ret));
1038                 }
1039
1040                 ret = cursor->c_close(cursor);
1041                 cursor = NULL;
1042         }
1043         
1044         return numkeys;
1045 }
1046
1047 /**
1048  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
1049  *      @keyid: The 32bit keyid.
1050  *
1051  *      This function maps a 32bit key id to the full 64bit one. It returns the
1052  *      full keyid. If the key isn't found a keyid of 0 is returned.
1053  */
1054 uint64_t getfullkeyid(uint64_t keyid)
1055 {
1056         DBT       key, data;
1057         DBC      *cursor = NULL;
1058         uint32_t  shortkeyid = 0;
1059         int       ret = 0;
1060
1061         if (keyid < 0x100000000LL) {
1062                 ret = id32db->cursor(id32db,
1063                                 txn,
1064                                 &cursor,
1065                                 0);   /* flags */
1066
1067                 shortkeyid = keyid & 0xFFFFFFFF;
1068
1069                 memset(&key, 0, sizeof(key));
1070                 memset(&data, 0, sizeof(data));
1071                 key.data = &shortkeyid;
1072                 key.size = sizeof(shortkeyid);
1073                 data.flags = DB_DBT_MALLOC;
1074
1075                 ret = cursor->c_get(cursor,
1076                         &key,
1077                         &data,
1078                         DB_SET);
1079
1080                 if (ret == 0) {
1081                         keyid = *(uint64_t *) data.data;
1082
1083                         if (data.data != NULL) {
1084                                 free(data.data);
1085                                 data.data = NULL;
1086                         }
1087                 }
1088
1089                 ret = cursor->c_close(cursor);
1090                 cursor = NULL;
1091         }
1092         
1093         return keyid;
1094 }
1095
1096 /*
1097  * Include the basic keydb routines.
1098  */
1099 #define NEED_GETKEYSIGS 1
1100 #define NEED_KEYID2UID 1
1101 #define NEED_UPDATEKEYS 1
1102 #include "keydb.c"