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.21 2003/10/03 23:02:04 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)
65 return(dbconns[keytrun % numdbs]);
69 * makewordlist - Takes a string and splits it into a set of unique words.
70 * @wordlist: The current word list.
71 * @words: The string to split and add.
73 * We take words and split it on non alpha numeric characters. These get
74 * added to the word list if they're not already present. If the wordlist
75 * is NULL then we start a new list, otherwise it's search for already
76 * added words. Note that words is modified in the process of scanning.
78 * Returns the new word list.
80 struct ll *makewordlist(struct ll *wordlist, char *word)
86 * Walk through the words string, spliting on non alphanumerics and
87 * then checking if the word already exists in the list. If not then
91 while (end != NULL && *end != 0) {
93 while (*start != 0 && !isalnum(*start)) {
97 while (*end != 0 && isalnum(*end)) {
101 if (end - start > 1) {
107 if (llfind(wordlist, start,
109 wordlist = lladd(wordlist,
119 * initdb - Initialize the key database.
121 * This function should be called before any of the other functions in
122 * this file are called in order to allow the DB to be initialized ready
132 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
133 numdb = fopen(buf, "r");
135 if (fgets(buf, sizeof(buf), numdb) != NULL) {
140 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
144 dbconns = malloc(sizeof (DB *) * numdbs);
145 if (dbconns == NULL) {
146 logthing(LOGTHING_CRITICAL,
147 "Couldn't allocate memory for dbconns");
151 ret = db_env_create(&dbenv, 0);
153 logthing(LOGTHING_CRITICAL,
154 "db_env_create: %s", db_strerror(ret));
159 * Enable deadlock detection so that we don't block indefinitely on
160 * anything. What we really want is simple 2 state locks, but I'm not
161 * sure how to make the standard DB functions do that yet.
163 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
165 logthing(LOGTHING_CRITICAL,
166 "db_env_create: %s", db_strerror(ret));
170 ret = dbenv->open(dbenv, config.db_dir,
171 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
176 logthing(LOGTHING_CRITICAL,
177 "Error opening db environment: %s (%s)",
183 for (i = 0; i < numdbs; i++) {
184 ret = db_create(&dbconns[i], dbenv, 0);
186 logthing(LOGTHING_CRITICAL,
187 "db_create: %s", db_strerror(ret));
191 snprintf(buf, 1023, "keydb.%d.db", i);
192 ret = dbconns[i]->open(dbconns[i], buf,
198 logthing(LOGTHING_CRITICAL,
199 "Error opening key database: %s (%s)",
206 ret = db_create(&worddb, dbenv, 0);
208 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
211 ret = worddb->set_flags(worddb, DB_DUP);
213 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
217 logthing(LOGTHING_CRITICAL,
218 "Error opening word database: %s (%s)",
228 * cleanupdb - De-initialize the key database.
230 * This function should be called upon program exit to allow the DB to
231 * cleanup after itself.
237 txn_checkpoint(dbenv, 0, 0, 0);
238 worddb->close(worddb, 0);
240 for (i = 0; i < numdbs; i++) {
241 dbconns[i]->close(dbconns[i], 0);
244 dbenv->close(dbenv, 0);
249 * starttrans - Start a transaction.
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.
255 bool starttrans(void)
259 assert(dbenv != NULL);
262 ret = txn_begin(dbenv,
263 NULL, /* No parent transaction */
267 logthing(LOGTHING_CRITICAL,
268 "Error starting transaction: %s",
277 * endtrans - End a transaction.
279 * Ends a transaction.
285 assert(dbenv != NULL);
288 ret = txn_commit(txn,
291 logthing(LOGTHING_CRITICAL,
292 "Error ending transaction: %s",
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.
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
313 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
316 struct openpgp_packet_list *packets = NULL;
320 struct buffer_ctx fetchbuf;
322 memset(&key, 0, sizeof(key));
323 memset(&data, 0, sizeof(data));
328 key.size = sizeof(keyid);
336 ret = keydb(keyid)->get(keydb(keyid),
343 fetchbuf.buffer = data.data;
345 fetchbuf.size = data.size;
346 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
348 parse_keys(packets, publickey);
349 free_packet_list(packets);
352 } else if (ret != DB_NOTFOUND) {
353 logthing(LOGTHING_ERROR,
354 "Problem retrieving key: %s",
365 int worddb_cmp(const char *d1, const char *d2)
367 return memcmp(d1, d2, 12);
371 * fetch_key_text - Trys to find the keys that contain the supplied text.
372 * @search: The text to search for.
373 * @publickey: A pointer to a structure to return the key in.
375 * This function searches for the supplied text and returns the keys that
378 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
386 char *searchtext = NULL;
387 struct ll *wordlist = NULL;
388 struct ll *curword = NULL;
389 struct ll *keylist = NULL;
390 struct ll *newkeylist = NULL;
393 searchtext = strdup(search);
394 wordlist = makewordlist(wordlist, searchtext);
398 ret = worddb->cursor(worddb,
403 for (curword = wordlist; curword != NULL; curword = curword->next) {
404 memset(&key, 0, sizeof(key));
405 memset(&data, 0, sizeof(data));
406 key.data = curword->object;
407 key.size = strlen(curword->object);
408 data.flags = DB_DBT_MALLOC;
409 ret = cursor->c_get(cursor,
413 while (ret == 0 && strncmp(key.data, curword->object,
415 ((char *) curword->object)[key.size] == 0) {
417 for (i = 4; i < 12; i++) {
419 keyid += ((unsigned char *)
423 if (keylist == NULL ||
424 llfind(keylist, data.data,
425 worddb_cmp) != NULL) {
426 newkeylist = lladd(newkeylist, data.data);
432 ret = cursor->c_get(cursor,
437 llfree(keylist, free);
438 keylist = newkeylist;
440 if (data.data != NULL) {
445 llfree(wordlist, NULL);
448 for (newkeylist = keylist;
449 newkeylist != NULL && numkeys < config.maxkeys;
450 newkeylist = newkeylist->next) {
453 for (i = 4; i < 12; i++) {
455 keyid += ((unsigned char *)
456 newkeylist->object)[i];
459 numkeys += fetch_key(keyid,
463 llfree(keylist, free);
468 ret = cursor->c_close(cursor);
477 * store_key - Takes a key and stores it.
478 * @publickey: A pointer to the public key to store.
479 * @intrans: If we're already in a transaction.
480 * @update: If true the key exists and should be updated.
482 * Again we just use the hex representation of the keyid as the filename
483 * to store the key to. We flatten the public key to a list of OpenPGP
484 * packets and then use write_openpgp_stream() to write the stream out to
485 * the file. If update is true then we delete the old key first, otherwise
486 * we trust that it doesn't exist.
488 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
490 struct openpgp_packet_list *packets = NULL;
491 struct openpgp_packet_list *list_end = NULL;
492 struct openpgp_publickey *next = NULL;
495 struct buffer_ctx storebuf;
500 char *primary = NULL;
501 unsigned char worddb_data[12];
502 struct ll *wordlist = NULL;
503 struct ll *curword = NULL;
504 bool deadlock = false;
506 keyid = get_keyid(publickey);
513 * Delete the key if we already have it.
515 * TODO: Can we optimize this perhaps? Possibly when other data is
516 * involved as well? I suspect this is easiest and doesn't make a lot
517 * of difference though - the largest chunk of data is the keydata and
518 * it definitely needs updated.
521 deadlock = (delete_key(keyid, true) == -1);
525 * Convert the key to a flat set of binary data.
528 next = publickey->next;
529 publickey->next = NULL;
530 flatten_publickey(publickey, &packets, &list_end);
531 publickey->next = next;
534 storebuf.size = 8192;
535 storebuf.buffer = malloc(8192);
537 write_openpgp_stream(buffer_putchar, &storebuf, packets);
540 * Now we have the key data store it in the DB; the keyid is
543 memset(&key, 0, sizeof(key));
544 memset(&data, 0, sizeof(data));
546 key.size = sizeof(keyid);
548 data.size = storebuf.offset;
549 data.data = storebuf.buffer;
551 ret = keydb(keyid)->put(keydb(keyid),
557 logthing(LOGTHING_ERROR,
558 "Problem storing key: %s",
560 if (ret == DB_LOCK_DEADLOCK) {
565 free(storebuf.buffer);
566 storebuf.buffer = NULL;
570 free_packet_list(packets);
575 * Walk through our uids storing the words into the db with the keyid.
578 uids = keyuids(publickey, &primary);
581 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
582 wordlist = makewordlist(wordlist, uids[i]);
585 for (curword = wordlist; curword != NULL && !deadlock;
586 curword = curword->next) {
587 memset(&key, 0, sizeof(key));
588 memset(&data, 0, sizeof(data));
589 key.data = curword->object;
590 key.size = strlen(key.data);
591 data.data = worddb_data;
592 data.size = sizeof(worddb_data);
595 * Our data is the key creation time followed by the
598 worddb_data[ 0] = publickey->publickey->data[1];
599 worddb_data[ 1] = publickey->publickey->data[2];
600 worddb_data[ 2] = publickey->publickey->data[3];
601 worddb_data[ 3] = publickey->publickey->data[4];
602 worddb_data[ 4] = (keyid >> 56) & 0xFF;
603 worddb_data[ 5] = (keyid >> 48) & 0xFF;
604 worddb_data[ 6] = (keyid >> 40) & 0xFF;
605 worddb_data[ 7] = (keyid >> 32) & 0xFF;
606 worddb_data[ 8] = (keyid >> 24) & 0xFF;
607 worddb_data[ 9] = (keyid >> 16) & 0xFF;
608 worddb_data[10] = (keyid >> 8) & 0xFF;
609 worddb_data[11] = keyid & 0xFF;
610 ret = worddb->put(worddb,
616 logthing(LOGTHING_ERROR,
617 "Problem storing word: %s",
619 if (ret == DB_LOCK_DEADLOCK) {
626 * Free our UID and word lists.
628 llfree(wordlist, NULL);
629 for (i = 0; uids[i] != NULL; i++) {
641 return deadlock ? -1 : 0 ;
645 * delete_key - Given a keyid delete the key from storage.
646 * @keyid: The keyid to delete.
647 * @intrans: If we're already in a transaction.
649 * This function deletes a public key from whatever storage mechanism we
650 * are using. Returns 0 if the key existed.
652 int delete_key(uint64_t keyid, bool intrans)
654 struct openpgp_publickey *publickey = NULL;
660 char *primary = NULL;
661 unsigned char worddb_data[12];
662 struct ll *wordlist = NULL;
663 struct ll *curword = NULL;
664 bool deadlock = false;
672 fetch_key(keyid, &publickey, true);
675 * Walk through the uids removing the words from the worddb.
677 if (publickey != NULL) {
678 uids = keyuids(publickey, &primary);
681 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
682 wordlist = makewordlist(wordlist, uids[i]);
685 ret = worddb->cursor(worddb,
690 for (curword = wordlist; curword != NULL && !deadlock;
691 curword = curword->next) {
692 memset(&key, 0, sizeof(key));
693 memset(&data, 0, sizeof(data));
694 key.data = curword->object;
695 key.size = strlen(key.data);
696 data.data = worddb_data;
697 data.size = sizeof(worddb_data);
700 * Our data is the key creation time followed by the
703 worddb_data[ 0] = publickey->publickey->data[1];
704 worddb_data[ 1] = publickey->publickey->data[2];
705 worddb_data[ 2] = publickey->publickey->data[3];
706 worddb_data[ 3] = publickey->publickey->data[4];
707 worddb_data[ 4] = (keyid >> 56) & 0xFF;
708 worddb_data[ 5] = (keyid >> 48) & 0xFF;
709 worddb_data[ 6] = (keyid >> 40) & 0xFF;
710 worddb_data[ 7] = (keyid >> 32) & 0xFF;
711 worddb_data[ 8] = (keyid >> 24) & 0xFF;
712 worddb_data[ 9] = (keyid >> 16) & 0xFF;
713 worddb_data[10] = (keyid >> 8) & 0xFF;
714 worddb_data[11] = keyid & 0xFF;
716 ret = cursor->c_get(cursor,
722 ret = cursor->c_del(cursor, 0);
724 logthing(LOGTHING_ERROR,
725 "Problem deleting word: %s",
731 logthing(LOGTHING_ERROR,
732 "Problem deleting word: %s",
734 if (ret == DB_LOCK_DEADLOCK) {
739 ret = cursor->c_close(cursor);
743 * Free our UID and word lists.
745 llfree(wordlist, NULL);
746 for (i = 0; uids[i] != NULL; i++) {
752 free_publickey(publickey);
758 key.size = sizeof(keyid);
760 keydb(keyid)->del(keydb(keyid),
770 return deadlock ? (-1) : (ret == DB_NOTFOUND);
774 * dumpdb - dump the key database
775 * @filenamebase: The base filename to use for the dump.
777 * Dumps the database into one or more files, which contain pure OpenPGP
778 * that can be reimported into onak or gpg. filenamebase provides a base
779 * file name for the dump; several files may be created, all of which will
780 * begin with this string and then have a unique number and a .pgp
783 int dumpdb(char *filenamebase)
793 for (i = 0; i < numdbs; i++) {
794 ret = dbconns[i]->cursor(dbconns[i],
799 snprintf(filename, 1023, "%s.%d.pgp", filenamebase, i);
800 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
802 logthing(LOGTHING_ERROR,
803 "Error opening keydump file (%s): %s",
807 memset(&key, 0, sizeof(key));
808 memset(&data, 0, sizeof(data));
809 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
811 write(fd, data.data, data.size);
812 memset(&key, 0, sizeof(key));
813 memset(&data, 0, sizeof(data));
814 ret = cursor->c_get(cursor, &key, &data,
817 if (ret != DB_NOTFOUND) {
818 logthing(LOGTHING_ERROR,
819 "Problem reading key: %s",
825 ret = cursor->c_close(cursor);
833 * Include the basic keydb routines.
835 #define NEED_GETFULLKEYID 1
836 #define NEED_GETKEYSIGS 1
837 #define NEED_KEYID2UID 1