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