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