2 * keydb_db3.c - Routines to store and fetch keys in a DB3 database.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
8 * $Id: keydb_db3.c,v 1.20 2003/09/30 20:40:10 noodles Exp $
12 #include <sys/types.h>
24 #include "charfuncs.h"
27 #include "decodekey.h"
28 #include "keystructs.h"
31 #include "onak-conf.h"
35 * dbenv - our database environment.
37 static DB_ENV *dbenv = NULL;
40 * numdb - The number of database files we have.
42 static int numdbs = 16;
45 * dbconn - our connections to the key database files.
47 static DB **dbconns = NULL;
50 * worddb - our connection to the word database.
52 static DB *worddb = NULL;
55 * txn - our current transaction id.
57 static DB_TXN *txn = NULL;
59 DB *keydb(uint64_t keyid)
61 return(dbconns[keyid % numdbs]);
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.
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.
74 * Returns the new word list.
76 struct ll *makewordlist(struct ll *wordlist, char *word)
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
87 while (end != NULL && *end != 0) {
89 while (*start != 0 && !isalnum(*start)) {
93 while (*end != 0 && isalnum(*end)) {
97 if (end - start > 1) {
103 if (llfind(wordlist, start,
105 wordlist = lladd(wordlist,
115 * initdb - Initialize the key database.
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
128 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
129 numdb = fopen(buf, "r");
131 if (fgets(buf, sizeof(buf), numdb) != NULL) {
136 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
140 dbconns = malloc(sizeof (DB *) * numdbs);
141 if (dbconns == NULL) {
142 logthing(LOGTHING_CRITICAL,
143 "Couldn't allocate memory for dbconns");
147 ret = db_env_create(&dbenv, 0);
149 logthing(LOGTHING_CRITICAL,
150 "db_env_create: %s", db_strerror(ret));
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.
159 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
161 logthing(LOGTHING_CRITICAL,
162 "db_env_create: %s", db_strerror(ret));
166 ret = dbenv->open(dbenv, config.db_dir,
167 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
172 logthing(LOGTHING_CRITICAL,
173 "Erroring opening db environment: %s (%s)",
179 for (i = 0; i < numdbs; i++) {
180 ret = db_create(&dbconns[i], dbenv, 0);
182 logthing(LOGTHING_CRITICAL,
183 "db_create: %s", db_strerror(ret));
187 snprintf(buf, 1023, "keydb.%d.db", i);
188 ret = dbconns[i]->open(dbconns[i], buf,
194 logthing(LOGTHING_CRITICAL,
195 "Error opening key database: %s (%s)",
202 ret = db_create(&worddb, dbenv, 0);
204 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
207 ret = worddb->set_flags(worddb, DB_DUP);
209 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
213 logthing(LOGTHING_CRITICAL,
214 "Error opening word database: %s (%s)",
224 * cleanupdb - De-initialize the key database.
226 * This function should be called upon program exit to allow the DB to
227 * cleanup after itself.
233 txn_checkpoint(dbenv, 0, 0, 0);
234 worddb->close(worddb, 0);
236 for (i = 0; i < numdbs; i++) {
237 dbconns[i]->close(dbconns[i], 0);
240 dbenv->close(dbenv, 0);
245 * starttrans - Start a transaction.
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.
251 bool starttrans(void)
255 assert(dbenv != NULL);
258 ret = txn_begin(dbenv,
259 NULL, /* No parent transaction */
263 logthing(LOGTHING_CRITICAL,
264 "Error starting transaction: %s",
273 * endtrans - End a transaction.
275 * Ends a transaction.
281 assert(dbenv != NULL);
284 ret = txn_commit(txn,
287 logthing(LOGTHING_CRITICAL,
288 "Error ending transaction: %s",
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.
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
309 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
312 struct openpgp_packet_list *packets = NULL;
316 struct buffer_ctx fetchbuf;
318 memset(&key, 0, sizeof(key));
319 memset(&data, 0, sizeof(data));
324 key.size = sizeof(keyid);
332 ret = keydb(keyid)->get(keydb(keyid),
339 fetchbuf.buffer = data.data;
341 fetchbuf.size = data.size;
342 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
344 parse_keys(packets, publickey);
345 free_packet_list(packets);
348 } else if (ret != DB_NOTFOUND) {
349 logthing(LOGTHING_ERROR,
350 "Problem retrieving key: %s",
361 int worddb_cmp(const char *d1, const char *d2)
363 return memcmp(d1, d2, 12);
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.
371 * This function searches for the supplied text and returns the keys that
374 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
382 char *searchtext = NULL;
383 struct ll *wordlist = NULL;
384 struct ll *curword = NULL;
385 struct ll *keylist = NULL;
386 struct ll *newkeylist = NULL;
389 searchtext = strdup(search);
390 wordlist = makewordlist(wordlist, searchtext);
394 ret = worddb->cursor(worddb,
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,
409 while (ret == 0 && strncmp(key.data, curword->object,
411 ((char *) curword->object)[key.size] == 0) {
413 for (i = 4; i < 12; i++) {
415 keyid += ((unsigned char *)
419 if (keylist == NULL ||
420 llfind(keylist, data.data,
421 worddb_cmp) != NULL) {
422 newkeylist = lladd(newkeylist, data.data);
428 ret = cursor->c_get(cursor,
433 llfree(keylist, free);
434 keylist = newkeylist;
436 if (data.data != NULL) {
441 llfree(wordlist, NULL);
444 for (newkeylist = keylist;
445 newkeylist != NULL && numkeys < config.maxkeys;
446 newkeylist = newkeylist->next) {
449 for (i = 4; i < 12; i++) {
451 keyid += ((unsigned char *)
452 newkeylist->object)[i];
455 numkeys += fetch_key(keyid,
459 llfree(keylist, free);
464 ret = cursor->c_close(cursor);
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.
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.
484 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
486 struct openpgp_packet_list *packets = NULL;
487 struct openpgp_packet_list *list_end = NULL;
488 struct openpgp_publickey *next = NULL;
491 struct buffer_ctx storebuf;
496 char *primary = NULL;
497 unsigned char worddb_data[12];
498 struct ll *wordlist = NULL;
499 struct ll *curword = NULL;
500 bool deadlock = false;
502 keyid = get_keyid(publickey);
509 * Delete the key if we already have it.
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.
517 deadlock = (delete_key(keyid, true) == -1);
521 * Convert the key to a flat set of binary data.
524 next = publickey->next;
525 publickey->next = NULL;
526 flatten_publickey(publickey, &packets, &list_end);
527 publickey->next = next;
530 storebuf.size = 8192;
531 storebuf.buffer = malloc(8192);
533 write_openpgp_stream(buffer_putchar, &storebuf, packets);
536 * Now we have the key data store it in the DB; the keyid is
539 memset(&key, 0, sizeof(key));
540 memset(&data, 0, sizeof(data));
542 key.size = sizeof(keyid);
544 data.size = storebuf.offset;
545 data.data = storebuf.buffer;
547 ret = keydb(keyid)->put(keydb(keyid),
553 logthing(LOGTHING_ERROR,
554 "Problem storing key: %s",
556 if (ret == DB_LOCK_DEADLOCK) {
561 free(storebuf.buffer);
562 storebuf.buffer = NULL;
566 free_packet_list(packets);
571 * Walk through our uids storing the words into the db with the keyid.
574 uids = keyuids(publickey, &primary);
577 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
578 wordlist = makewordlist(wordlist, uids[i]);
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);
591 * Our data is the key creation time followed by the
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,
612 logthing(LOGTHING_ERROR,
613 "Problem storing word: %s",
615 if (ret == DB_LOCK_DEADLOCK) {
622 * Free our UID and word lists.
624 llfree(wordlist, NULL);
625 for (i = 0; uids[i] != NULL; i++) {
637 return deadlock ? -1 : 0 ;
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.
645 * This function deletes a public key from whatever storage mechanism we
646 * are using. Returns 0 if the key existed.
648 int delete_key(uint64_t keyid, bool intrans)
650 struct openpgp_publickey *publickey = 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;
668 fetch_key(keyid, &publickey, true);
671 * Walk through the uids removing the words from the worddb.
673 if (publickey != NULL) {
674 uids = keyuids(publickey, &primary);
677 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
678 wordlist = makewordlist(wordlist, uids[i]);
681 ret = worddb->cursor(worddb,
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);
696 * Our data is the key creation time followed by the
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;
712 ret = cursor->c_get(cursor,
718 ret = cursor->c_del(cursor, 0);
720 logthing(LOGTHING_ERROR,
721 "Problem deleting word: %s",
727 logthing(LOGTHING_ERROR,
728 "Problem deleting word: %s",
730 if (ret == DB_LOCK_DEADLOCK) {
735 ret = cursor->c_close(cursor);
739 * Free our UID and word lists.
741 llfree(wordlist, NULL);
742 for (i = 0; uids[i] != NULL; i++) {
748 free_publickey(publickey);
754 key.size = sizeof(keyid);
756 keydb(keyid)->del(keydb(keyid),
766 return deadlock ? (-1) : (ret == DB_NOTFOUND);
770 * dumpdb - dump the key database
771 * @filenamebase: The base filename to use for the dump.
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
779 int dumpdb(char *filenamebase)
789 for (i = 0; i < numdbs; i++) {
790 ret = dbconns[i]->cursor(dbconns[i],
795 snprintf(filename, 1023, "%s.%d.pgp", filenamebase, i);
796 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
798 logthing(LOGTHING_ERROR,
799 "Error opening keydump file (%s): %s",
803 memset(&key, 0, sizeof(key));
804 memset(&data, 0, sizeof(data));
805 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
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,
813 if (ret != DB_NOTFOUND) {
814 logthing(LOGTHING_ERROR,
815 "Problem reading key: %s",
821 ret = cursor->c_close(cursor);
829 * Include the basic keydb routines.
831 #define NEED_GETFULLKEYID 1
832 #define NEED_GETKEYSIGS 1
833 #define NEED_KEYID2UID 1