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
10 #include <sys/types.h>
22 #include "charfuncs.h"
25 #include "decodekey.h"
26 #include "keystructs.h"
29 #include "onak-conf.h"
33 * dbenv - our database environment.
35 static DB_ENV *dbenv = NULL;
38 * dbconn - our connection to the key database.
40 static DB *dbconn = NULL;
43 * worddb - our connection to the word database.
45 static DB *worddb = NULL;
48 * txn - our current transaction id.
50 static DB_TXN *txn = NULL;
53 * makewordlist - Takes a string and splits it into a set of unique words.
54 * @wordlist: The current word list.
55 * @words: The string to split and add.
57 * We take words and split it on non alpha numeric characters. These get
58 * added to the word list if they're not already present. If the wordlist
59 * is NULL then we start a new list, otherwise it's search for already
60 * added words. Note that words is modified in the process of scanning.
62 * Returns the new word list.
64 struct ll *makewordlist(struct ll *wordlist, char *word)
70 * Walk through the words string, spliting on non alphanumerics and
71 * then checking if the word already exists in the list. If not then
75 while (end != NULL && *end != 0) {
77 while (*start != 0 && !isalnum(*start)) {
81 while (*end != 0 && isalnum(*end)) {
85 if (end - start > 1) {
91 if (llfind(wordlist, start,
93 wordlist = lladd(wordlist,
103 * initdb - Initialize the key database.
105 * This function should be called before any of the other functions in
106 * this file are called in order to allow the DB to be initialized ready
113 ret = db_env_create(&dbenv, 0);
115 logthing(LOGTHING_CRITICAL,
116 "db_env_create: %s", db_strerror(ret));
121 * Enable deadlock detection so that we don't block indefinitely on
122 * anything. What we really want is simple 2 state locks, but I'm not
123 * sure how to make the standard DB functions do that yet.
125 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
127 logthing(LOGTHING_CRITICAL,
128 "db_env_create: %s", db_strerror(ret));
132 ret = dbenv->open(dbenv, config.db_dir,
133 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
138 logthing(LOGTHING_CRITICAL,
139 "Erroring opening db environment: %s (%s)",
145 ret = db_create(&dbconn, dbenv, 0);
147 logthing(LOGTHING_CRITICAL,
148 "db_create: %s", db_strerror(ret));
152 ret = dbconn->open(dbconn, "keydb.db",
158 logthing(LOGTHING_CRITICAL,
159 "Error opening key database: %s (%s)",
165 ret = db_create(&worddb, dbenv, 0);
167 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
170 ret = worddb->set_flags(worddb, DB_DUP);
172 ret = worddb->open(worddb, "worddb", NULL, DB_BTREE,
176 logthing(LOGTHING_CRITICAL,
177 "Error opening word database: %s (%s)",
187 * cleanupdb - De-initialize the key database.
189 * This function should be called upon program exit to allow the DB to
190 * cleanup after itself.
194 txn_checkpoint(dbenv, 0, 0, 0);
195 worddb->close(worddb, 0);
197 dbconn->close(dbconn, 0);
199 dbenv->close(dbenv, 0);
204 * starttrans - Start a transaction.
206 * Start a transaction. Intended to be used if we're about to perform many
207 * operations on the database to help speed it all up, or if we want
208 * something to only succeed if all relevant operations are successful.
210 bool starttrans(void)
214 assert(dbenv != NULL);
217 ret = txn_begin(dbenv,
218 NULL, /* No parent transaction */
222 logthing(LOGTHING_CRITICAL,
223 "Error starting transaction: %s",
232 * endtrans - End a transaction.
234 * Ends a transaction.
240 assert(dbenv != NULL);
243 ret = txn_commit(txn,
246 logthing(LOGTHING_CRITICAL,
247 "Error ending transaction: %s",
257 * fetch_key - Given a keyid fetch the key from storage.
258 * @keyid: The keyid to fetch.
259 * @publickey: A pointer to a structure to return the key in.
260 * @intrans: If we're already in a transaction.
262 * We use the hex representation of the keyid as the filename to fetch the
263 * key from. The key is stored in the file as a binary OpenPGP stream of
264 * packets, so we can just use read_openpgp_stream() to read the packets
265 * in and then parse_keys() to parse the packets into a publickey
268 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
271 struct openpgp_packet_list *packets = NULL;
275 struct buffer_ctx fetchbuf;
277 memset(&key, 0, sizeof(key));
278 memset(&data, 0, sizeof(data));
283 key.size = sizeof(keyid);
291 ret = dbconn->get(dbconn,
298 fetchbuf.buffer = data.data;
300 fetchbuf.size = data.size;
301 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
303 parse_keys(packets, publickey);
304 free_packet_list(packets);
307 } else if (ret != DB_NOTFOUND) {
308 logthing(LOGTHING_ERROR,
309 "Problem retrieving key: %s",
320 int worddb_cmp(const char *d1, const char *d2)
322 return memcmp(d1, d2, 12);
326 * fetch_key_text - Trys to find the keys that contain the supplied text.
327 * @search: The text to search for.
328 * @publickey: A pointer to a structure to return the key in.
330 * This function searches for the supplied text and returns the keys that
333 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
341 char *searchtext = NULL;
342 struct ll *wordlist = NULL;
343 struct ll *curword = NULL;
344 struct ll *keylist = NULL;
345 struct ll *newkeylist = NULL;
348 searchtext = strdup(search);
349 wordlist = makewordlist(wordlist, searchtext);
353 ret = worddb->cursor(worddb,
358 for (curword = wordlist; curword != NULL; curword = curword->next) {
359 memset(&key, 0, sizeof(key));
360 memset(&data, 0, sizeof(data));
361 key.data = curword->object;
362 key.size = strlen(curword->object);
363 data.flags = DB_DBT_MALLOC;
364 ret = cursor->c_get(cursor,
368 while (ret == 0 && strncmp(key.data, curword->object,
370 ((char *) curword->object)[key.size] == 0) {
372 for (i = 4; i < 12; i++) {
374 keyid += ((unsigned char *)
378 if (keylist == NULL ||
379 llfind(keylist, data.data,
380 worddb_cmp) != NULL) {
381 newkeylist = lladd(newkeylist, data.data);
387 ret = cursor->c_get(cursor,
392 llfree(keylist, free);
393 keylist = newkeylist;
395 if (data.data != NULL) {
400 llfree(wordlist, NULL);
403 for (newkeylist = keylist;
404 newkeylist != NULL && numkeys < config.maxkeys;
405 newkeylist = newkeylist->next) {
408 for (i = 4; i < 12; i++) {
410 keyid += ((unsigned char *)
411 newkeylist->object)[i];
414 numkeys += fetch_key(keyid,
418 llfree(keylist, free);
423 ret = cursor->c_close(cursor);
432 * store_key - Takes a key and stores it.
433 * @publickey: A pointer to the public key to store.
434 * @intrans: If we're already in a transaction.
435 * @update: If true the key exists and should be updated.
437 * Again we just use the hex representation of the keyid as the filename
438 * to store the key to. We flatten the public key to a list of OpenPGP
439 * packets and then use write_openpgp_stream() to write the stream out to
440 * the file. If update is true then we delete the old key first, otherwise
441 * we trust that it doesn't exist.
443 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
445 struct openpgp_packet_list *packets = NULL;
446 struct openpgp_packet_list *list_end = NULL;
447 struct openpgp_publickey *next = NULL;
450 struct buffer_ctx storebuf;
455 char *primary = NULL;
456 unsigned char worddb_data[12];
457 struct ll *wordlist = NULL;
458 struct ll *curword = NULL;
459 bool deadlock = false;
461 keyid = get_keyid(publickey);
468 * Delete the key if we already have it.
470 * TODO: Can we optimize this perhaps? Possibly when other data is
471 * involved as well? I suspect this is easiest and doesn't make a lot
472 * of difference though - the largest chunk of data is the keydata and
473 * it definitely needs updated.
476 deadlock = (delete_key(keyid, true) == -1);
480 * Convert the key to a flat set of binary data.
483 next = publickey->next;
484 publickey->next = NULL;
485 flatten_publickey(publickey, &packets, &list_end);
486 publickey->next = next;
489 storebuf.size = 8192;
490 storebuf.buffer = malloc(8192);
492 write_openpgp_stream(buffer_putchar, &storebuf, packets);
495 * Now we have the key data store it in the DB; the keyid is
498 memset(&key, 0, sizeof(key));
499 memset(&data, 0, sizeof(data));
501 key.size = sizeof(keyid);
503 data.size = storebuf.offset;
504 data.data = storebuf.buffer;
506 ret = dbconn->put(dbconn,
512 logthing(LOGTHING_ERROR,
513 "Problem storing key: %s",
515 if (ret == DB_LOCK_DEADLOCK) {
520 free(storebuf.buffer);
521 storebuf.buffer = NULL;
525 free_packet_list(packets);
530 * Walk through our uids storing the words into the db with the keyid.
533 uids = keyuids(publickey, &primary);
536 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
537 wordlist = makewordlist(wordlist, uids[i]);
540 for (curword = wordlist; curword != NULL && !deadlock;
541 curword = curword->next) {
542 memset(&key, 0, sizeof(key));
543 memset(&data, 0, sizeof(data));
544 key.data = curword->object;
545 key.size = strlen(key.data);
546 data.data = worddb_data;
547 data.size = sizeof(worddb_data);
550 * Our data is the key creation time followed by the
553 worddb_data[ 0] = publickey->publickey->data[1];
554 worddb_data[ 1] = publickey->publickey->data[2];
555 worddb_data[ 2] = publickey->publickey->data[3];
556 worddb_data[ 3] = publickey->publickey->data[4];
557 worddb_data[ 4] = (keyid >> 56) & 0xFF;
558 worddb_data[ 5] = (keyid >> 48) & 0xFF;
559 worddb_data[ 6] = (keyid >> 40) & 0xFF;
560 worddb_data[ 7] = (keyid >> 32) & 0xFF;
561 worddb_data[ 8] = (keyid >> 24) & 0xFF;
562 worddb_data[ 9] = (keyid >> 16) & 0xFF;
563 worddb_data[10] = (keyid >> 8) & 0xFF;
564 worddb_data[11] = keyid & 0xFF;
565 ret = worddb->put(worddb,
571 logthing(LOGTHING_ERROR,
572 "Problem storing word: %s",
574 if (ret == DB_LOCK_DEADLOCK) {
581 * Free our UID and word lists.
583 llfree(wordlist, NULL);
584 for (i = 0; uids[i] != NULL; i++) {
596 return deadlock ? -1 : 0 ;
600 * delete_key - Given a keyid delete the key from storage.
601 * @keyid: The keyid to delete.
602 * @intrans: If we're already in a transaction.
604 * This function deletes a public key from whatever storage mechanism we
605 * are using. Returns 0 if the key existed.
607 int delete_key(uint64_t keyid, bool intrans)
609 struct openpgp_publickey *publickey = NULL;
615 char *primary = NULL;
616 unsigned char worddb_data[12];
617 struct ll *wordlist = NULL;
618 struct ll *curword = NULL;
619 bool deadlock = false;
627 fetch_key(keyid, &publickey, true);
630 * Walk through the uids removing the words from the worddb.
632 if (publickey != NULL) {
633 uids = keyuids(publickey, &primary);
636 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
637 wordlist = makewordlist(wordlist, uids[i]);
640 ret = worddb->cursor(worddb,
645 for (curword = wordlist; curword != NULL && !deadlock;
646 curword = curword->next) {
647 memset(&key, 0, sizeof(key));
648 memset(&data, 0, sizeof(data));
649 key.data = curword->object;
650 key.size = strlen(key.data);
651 data.data = worddb_data;
652 data.size = sizeof(worddb_data);
655 * Our data is the key creation time followed by the
658 worddb_data[ 0] = publickey->publickey->data[1];
659 worddb_data[ 1] = publickey->publickey->data[2];
660 worddb_data[ 2] = publickey->publickey->data[3];
661 worddb_data[ 3] = publickey->publickey->data[4];
662 worddb_data[ 4] = (keyid >> 56) & 0xFF;
663 worddb_data[ 5] = (keyid >> 48) & 0xFF;
664 worddb_data[ 6] = (keyid >> 40) & 0xFF;
665 worddb_data[ 7] = (keyid >> 32) & 0xFF;
666 worddb_data[ 8] = (keyid >> 24) & 0xFF;
667 worddb_data[ 9] = (keyid >> 16) & 0xFF;
668 worddb_data[10] = (keyid >> 8) & 0xFF;
669 worddb_data[11] = keyid & 0xFF;
671 ret = cursor->c_get(cursor,
677 ret = cursor->c_del(cursor, 0);
679 logthing(LOGTHING_ERROR,
680 "Problem deleting word: %s",
686 logthing(LOGTHING_ERROR,
687 "Problem deleting word: %s",
689 if (ret == DB_LOCK_DEADLOCK) {
694 ret = cursor->c_close(cursor);
698 * Free our UID and word lists.
700 llfree(wordlist, NULL);
701 for (i = 0; uids[i] != NULL; i++) {
707 free_publickey(publickey);
713 key.size = sizeof(keyid);
725 return deadlock ? (-1) : (ret == DB_NOTFOUND);
729 * dumpdb - dump the key database
730 * @filenamebase: The base filename to use for the dump.
732 * Dumps the database into one or more files, which contain pure OpenPGP
733 * that can be reimported into onak or gpg. filenamebase provides a base
734 * file name for the dump; several files may be created, all of which will
735 * begin with this string and then have a unique number and a .pgp
738 int dumpdb(char *filenamebase)
747 ret = dbconn->cursor(dbconn,
752 fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640);
753 memset(&key, 0, sizeof(key));
754 memset(&data, 0, sizeof(data));
755 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
757 write(fd, data.data, data.size);
758 memset(&key, 0, sizeof(key));
759 memset(&data, 0, sizeof(data));
760 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
762 if (ret != DB_NOTFOUND) {
763 logthing(LOGTHING_ERROR, "Problem reading key: %s",
769 ret = cursor->c_close(cursor);
778 * Include the basic keydb routines.
780 #define NEED_GETFULLKEYID 1
781 #define NEED_GETKEYSIGS 1
782 #define NEED_KEYID2UID 1