2 * keydb_db4.c - Routines to store and fetch keys in a DB4 database.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002-2004 Project Purple
21 #include "charfuncs.h"
25 #include "decodekey.h"
26 #include "keystructs.h"
29 #include "onak-conf.h"
34 * dbenv - our database environment.
36 static DB_ENV *dbenv = NULL;
39 * numdb - The number of database files we have.
41 static int numdbs = 16;
44 * dbconn - our connections to the key database files.
46 static DB **dbconns = NULL;
49 * worddb - our connection to the word database.
51 static DB *worddb = NULL;
54 * id32db - our connection to the 32bit ID database.
56 static DB *id32db = NULL;
59 * txn - our current transaction id.
61 static DB_TXN *txn = NULL;
63 DB *keydb(uint64_t keyid)
69 return(dbconns[keytrun % numdbs]);
73 * starttrans - Start a transaction.
75 * Start a transaction. Intended to be used if we're about to perform many
76 * operations on the database to help speed it all up, or if we want
77 * something to only succeed if all relevant operations are successful.
79 static bool db4_starttrans(void)
83 log_assert(dbenv != NULL);
84 log_assert(txn == NULL);
86 ret = dbenv->txn_begin(dbenv,
87 NULL, /* No parent transaction */
91 logthing(LOGTHING_CRITICAL,
92 "Error starting transaction: %s",
101 * endtrans - End a transaction.
103 * Ends a transaction.
105 static void db4_endtrans(void)
109 log_assert(dbenv != NULL);
110 log_assert(txn != NULL);
112 ret = txn->commit(txn,
115 logthing(LOGTHING_CRITICAL,
116 "Error ending transaction: %s",
126 * initdb - Initialize the key database.
128 * This function should be called before any of the other functions in
129 * this file are called in order to allow the DB to be initialized ready
132 static void db4_initdb(bool readonly)
140 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
141 numdb = fopen(buf, "r");
143 if (fgets(buf, sizeof(buf), numdb) != NULL) {
147 } else if (!readonly) {
148 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
150 numdb = fopen(buf, "w");
152 fprintf(numdb, "%d", numdbs);
155 logthing(LOGTHING_ERROR,
156 "Couldn't write num_keydb: %s",
161 dbconns = malloc(sizeof (DB *) * numdbs);
162 if (dbconns == NULL) {
163 logthing(LOGTHING_CRITICAL,
164 "Couldn't allocate memory for dbconns");
169 ret = db_env_create(&dbenv, 0);
171 logthing(LOGTHING_CRITICAL,
172 "db_env_create: %s", db_strerror(ret));
177 * Enable deadlock detection so that we don't block indefinitely on
178 * anything. What we really want is simple 2 state locks, but I'm not
179 * sure how to make the standard DB functions do that yet.
182 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
184 logthing(LOGTHING_CRITICAL,
185 "db_env_create: %s", db_strerror(ret));
190 ret = dbenv->open(dbenv, config.db_dir,
191 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
196 logthing(LOGTHING_CRITICAL,
197 "Error opening db environment: %s (%s)",
200 dbenv->close(dbenv, 0);
208 for (i = 0; !ret && i < numdbs; i++) {
209 ret = db_create(&dbconns[i], dbenv, 0);
211 logthing(LOGTHING_CRITICAL,
212 "db_create: %s", db_strerror(ret));
216 snprintf(buf, 1023, "keydb.%d.db", i);
221 ret = dbconns[i]->open(dbconns[i],
229 logthing(LOGTHING_CRITICAL,
230 "Error opening key database:"
241 ret = db_create(&worddb, dbenv, 0);
243 logthing(LOGTHING_CRITICAL, "db_create: %s",
249 ret = worddb->set_flags(worddb, DB_DUP);
253 ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
257 logthing(LOGTHING_CRITICAL,
258 "Error opening word database: %s (%s)",
265 ret = db_create(&id32db, dbenv, 0);
267 logthing(LOGTHING_CRITICAL, "db_create: %s",
273 ret = id32db->set_flags(id32db, DB_DUP);
277 ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
281 logthing(LOGTHING_CRITICAL,
282 "Error opening id32 database: %s (%s)",
294 logthing(LOGTHING_CRITICAL,
295 "Error opening database; exiting");
303 * cleanupdb - De-initialize the key database.
305 * This function should be called upon program exit to allow the DB to
306 * cleanup after itself.
308 static void db4_cleanupdb(void)
313 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
314 if (id32db != NULL) {
315 id32db->close(id32db, 0);
318 if (worddb != NULL) {
319 worddb->close(worddb, 0);
322 for (i = 0; i < numdbs; i++) {
323 if (dbconns[i] != NULL) {
324 dbconns[i]->close(dbconns[i], 0);
330 dbenv->close(dbenv, 0);
336 * fetch_key - Given a keyid fetch the key from storage.
337 * @keyid: The keyid to fetch.
338 * @publickey: A pointer to a structure to return the key in.
339 * @intrans: If we're already in a transaction.
341 * We use the hex representation of the keyid as the filename to fetch the
342 * key from. The key is stored in the file as a binary OpenPGP stream of
343 * packets, so we can just use read_openpgp_stream() to read the packets
344 * in and then parse_keys() to parse the packets into a publickey
347 static int db4_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
350 struct openpgp_packet_list *packets = NULL;
354 struct buffer_ctx fetchbuf;
356 if (keyid < 0x100000000LL) {
357 keyid = getfullkeyid(keyid);
360 memset(&key, 0, sizeof(key));
361 memset(&data, 0, sizeof(data));
366 key.size = sizeof(keyid);
373 ret = keydb(keyid)->get(keydb(keyid),
380 fetchbuf.buffer = data.data;
382 fetchbuf.size = data.size;
383 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
385 parse_keys(packets, publickey);
386 free_packet_list(packets);
389 } else if (ret != DB_NOTFOUND) {
390 logthing(LOGTHING_ERROR,
391 "Problem retrieving key: %s",
402 int worddb_cmp(const void *d1, const void *d2)
404 return memcmp(d1, d2, 12);
408 * fetch_key_text - Trys to find the keys that contain the supplied text.
409 * @search: The text to search for.
410 * @publickey: A pointer to a structure to return the key in.
412 * This function searches for the supplied text and returns the keys that
415 static int db4_fetch_key_text(const char *search,
416 struct openpgp_publickey **publickey)
424 char *searchtext = NULL;
425 struct ll *wordlist = NULL;
426 struct ll *curword = NULL;
427 struct keyarray keylist = { NULL, 0, 0 };
428 struct keyarray newkeylist = { NULL, 0, 0 };
431 searchtext = strdup(search);
432 wordlist = makewordlist(wordlist, searchtext);
434 for (curword = wordlist; curword != NULL; curword = curword->next) {
437 ret = worddb->cursor(worddb,
442 memset(&key, 0, sizeof(key));
443 memset(&data, 0, sizeof(data));
444 key.data = curword->object;
445 key.size = strlen(curword->object);
446 data.flags = DB_DBT_MALLOC;
447 ret = cursor->c_get(cursor,
451 while (ret == 0 && strncmp(key.data, curword->object,
453 ((char *) curword->object)[key.size] == 0) {
455 for (i = 4; i < 12; i++) {
457 keyid += ((unsigned char *)
461 if (keylist.count == 0 ||
462 array_find(&keylist, keyid)) {
463 array_add(&newkeylist, keyid);
469 ret = cursor->c_get(cursor,
474 array_free(&keylist);
475 keylist = newkeylist;
476 newkeylist.keys = NULL;
477 newkeylist.count = newkeylist.size = 0;
478 if (data.data != NULL) {
482 ret = cursor->c_close(cursor);
486 llfree(wordlist, NULL);
490 for (i = 0; i < keylist.count; i++) {
491 numkeys += fetch_key(keylist.keys[i],
495 array_free(&keylist);
505 * store_key - Takes a key and stores it.
506 * @publickey: A pointer to the public key to store.
507 * @intrans: If we're already in a transaction.
508 * @update: If true the key exists and should be updated.
510 * Again we just use the hex representation of the keyid as the filename
511 * to store the key to. We flatten the public key to a list of OpenPGP
512 * packets and then use write_openpgp_stream() to write the stream out to
513 * the file. If update is true then we delete the old key first, otherwise
514 * we trust that it doesn't exist.
516 static int db4_store_key(struct openpgp_publickey *publickey, bool intrans,
519 struct openpgp_packet_list *packets = NULL;
520 struct openpgp_packet_list *list_end = NULL;
521 struct openpgp_publickey *next = NULL;
524 struct buffer_ctx storebuf;
528 uint32_t shortkeyid = 0;
529 uint64_t *subkeyids = NULL;
531 char *primary = NULL;
532 unsigned char worddb_data[12];
533 struct ll *wordlist = NULL;
534 struct ll *curword = NULL;
535 bool deadlock = false;
537 keyid = get_keyid(publickey);
544 * Delete the key if we already have it.
546 * TODO: Can we optimize this perhaps? Possibly when other data is
547 * involved as well? I suspect this is easiest and doesn't make a lot
548 * of difference though - the largest chunk of data is the keydata and
549 * it definitely needs updated.
552 deadlock = (delete_key(keyid, true) == -1);
556 * Convert the key to a flat set of binary data.
559 next = publickey->next;
560 publickey->next = NULL;
561 flatten_publickey(publickey, &packets, &list_end);
562 publickey->next = next;
565 storebuf.size = 8192;
566 storebuf.buffer = malloc(8192);
568 write_openpgp_stream(buffer_putchar, &storebuf, packets);
571 * Now we have the key data store it in the DB; the keyid is
574 memset(&key, 0, sizeof(key));
575 memset(&data, 0, sizeof(data));
577 key.size = sizeof(keyid);
578 data.size = storebuf.offset;
579 data.data = storebuf.buffer;
581 ret = keydb(keyid)->put(keydb(keyid),
587 logthing(LOGTHING_ERROR,
588 "Problem storing key: %s",
590 if (ret == DB_LOCK_DEADLOCK) {
595 free(storebuf.buffer);
596 storebuf.buffer = NULL;
600 free_packet_list(packets);
605 * Walk through our uids storing the words into the db with the keyid.
608 uids = keyuids(publickey, &primary);
611 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
612 wordlist = makewordlist(wordlist, uids[i]);
615 for (curword = wordlist; curword != NULL && !deadlock;
616 curword = curword->next) {
617 memset(&key, 0, sizeof(key));
618 memset(&data, 0, sizeof(data));
619 key.data = curword->object;
620 key.size = strlen(key.data);
621 data.data = worddb_data;
622 data.size = sizeof(worddb_data);
625 * Our data is the key creation time followed by the
628 worddb_data[ 0] = publickey->publickey->data[1];
629 worddb_data[ 1] = publickey->publickey->data[2];
630 worddb_data[ 2] = publickey->publickey->data[3];
631 worddb_data[ 3] = publickey->publickey->data[4];
632 worddb_data[ 4] = (keyid >> 56) & 0xFF;
633 worddb_data[ 5] = (keyid >> 48) & 0xFF;
634 worddb_data[ 6] = (keyid >> 40) & 0xFF;
635 worddb_data[ 7] = (keyid >> 32) & 0xFF;
636 worddb_data[ 8] = (keyid >> 24) & 0xFF;
637 worddb_data[ 9] = (keyid >> 16) & 0xFF;
638 worddb_data[10] = (keyid >> 8) & 0xFF;
639 worddb_data[11] = keyid & 0xFF;
640 ret = worddb->put(worddb,
646 logthing(LOGTHING_ERROR,
647 "Problem storing word: %s",
649 if (ret == DB_LOCK_DEADLOCK) {
656 * Free our UID and word lists.
658 llfree(wordlist, NULL);
659 for (i = 0; uids[i] != NULL; i++) {
668 * Write the truncated 32 bit keyid so we can lookup the full id for
672 shortkeyid = keyid & 0xFFFFFFFF;
674 memset(&key, 0, sizeof(key));
675 memset(&data, 0, sizeof(data));
676 key.data = &shortkeyid;
677 key.size = sizeof(shortkeyid);
679 data.size = sizeof(keyid);
681 ret = id32db->put(id32db,
687 logthing(LOGTHING_ERROR,
688 "Problem storing short keyid: %s",
690 if (ret == DB_LOCK_DEADLOCK) {
697 subkeyids = keysubkeys(publickey);
699 while (subkeyids != NULL && subkeyids[i] != 0) {
700 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
702 memset(&key, 0, sizeof(key));
703 memset(&data, 0, sizeof(data));
704 key.data = &shortkeyid;
705 key.size = sizeof(shortkeyid);
707 data.size = sizeof(keyid);
709 ret = id32db->put(id32db,
715 logthing(LOGTHING_ERROR,
716 "Problem storing short keyid: %s",
718 if (ret == DB_LOCK_DEADLOCK) {
723 if (subkeyids != NULL) {
733 return deadlock ? -1 : 0 ;
737 * delete_key - Given a keyid delete the key from storage.
738 * @keyid: The keyid to delete.
739 * @intrans: If we're already in a transaction.
741 * This function deletes a public key from whatever storage mechanism we
742 * are using. Returns 0 if the key existed.
744 static int db4_delete_key(uint64_t keyid, bool intrans)
746 struct openpgp_publickey *publickey = NULL;
749 uint32_t shortkeyid = 0;
750 uint64_t *subkeyids = NULL;
754 char *primary = NULL;
755 unsigned char worddb_data[12];
756 struct ll *wordlist = NULL;
757 struct ll *curword = NULL;
758 bool deadlock = false;
764 fetch_key(keyid, &publickey, true);
767 * Walk through the uids removing the words from the worddb.
769 if (publickey != NULL) {
770 uids = keyuids(publickey, &primary);
773 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
774 wordlist = makewordlist(wordlist, uids[i]);
777 ret = worddb->cursor(worddb,
782 for (curword = wordlist; curword != NULL && !deadlock;
783 curword = curword->next) {
784 memset(&key, 0, sizeof(key));
785 memset(&data, 0, sizeof(data));
786 key.data = curword->object;
787 key.size = strlen(key.data);
788 data.data = worddb_data;
789 data.size = sizeof(worddb_data);
792 * Our data is the key creation time followed by the
795 worddb_data[ 0] = publickey->publickey->data[1];
796 worddb_data[ 1] = publickey->publickey->data[2];
797 worddb_data[ 2] = publickey->publickey->data[3];
798 worddb_data[ 3] = publickey->publickey->data[4];
799 worddb_data[ 4] = (keyid >> 56) & 0xFF;
800 worddb_data[ 5] = (keyid >> 48) & 0xFF;
801 worddb_data[ 6] = (keyid >> 40) & 0xFF;
802 worddb_data[ 7] = (keyid >> 32) & 0xFF;
803 worddb_data[ 8] = (keyid >> 24) & 0xFF;
804 worddb_data[ 9] = (keyid >> 16) & 0xFF;
805 worddb_data[10] = (keyid >> 8) & 0xFF;
806 worddb_data[11] = keyid & 0xFF;
808 ret = cursor->c_get(cursor,
814 ret = cursor->c_del(cursor, 0);
816 logthing(LOGTHING_ERROR,
817 "Problem deleting word: %s",
823 logthing(LOGTHING_ERROR,
824 "Problem deleting word: %s",
826 if (ret == DB_LOCK_DEADLOCK) {
831 ret = cursor->c_close(cursor);
835 * Free our UID and word lists.
837 llfree(wordlist, NULL);
838 for (i = 0; uids[i] != NULL; i++) {
844 free_publickey(publickey);
849 ret = id32db->cursor(id32db,
854 shortkeyid = keyid & 0xFFFFFFFF;
856 memset(&key, 0, sizeof(key));
857 memset(&data, 0, sizeof(data));
858 key.data = &shortkeyid;
859 key.size = sizeof(shortkeyid);
861 data.size = sizeof(keyid);
863 ret = cursor->c_get(cursor,
869 ret = cursor->c_del(cursor, 0);
871 logthing(LOGTHING_ERROR,
872 "Problem deleting short keyid: %s",
878 logthing(LOGTHING_ERROR,
879 "Problem deleting short keyid: %s",
881 if (ret == DB_LOCK_DEADLOCK) {
886 subkeyids = keysubkeys(publickey);
888 while (subkeyids != NULL && subkeyids[i] != 0) {
889 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
891 memset(&key, 0, sizeof(key));
892 memset(&data, 0, sizeof(data));
893 key.data = &shortkeyid;
894 key.size = sizeof(shortkeyid);
896 data.size = sizeof(keyid);
898 ret = cursor->c_get(cursor,
904 ret = cursor->c_del(cursor, 0);
906 logthing(LOGTHING_ERROR,
907 "Problem deleting short"
914 logthing(LOGTHING_ERROR,
915 "Problem deleting short keyid: %s",
917 if (ret == DB_LOCK_DEADLOCK) {
922 if (subkeyids != NULL) {
927 ret = cursor->c_close(cursor);
933 key.size = sizeof(keyid);
935 keydb(keyid)->del(keydb(keyid),
945 return deadlock ? (-1) : (ret == DB_NOTFOUND);
949 * iterate_keys - call a function once for each key in the db.
950 * @iterfunc: The function to call.
951 * @ctx: A context pointer
953 * Calls iterfunc once for each key in the database. ctx is passed
954 * unaltered to iterfunc. This function is intended to aid database dumps
955 * and statistic calculations.
957 * Returns the number of keys we iterated over.
959 static int db4_iterate_keys(void (*iterfunc)(void *ctx,
960 struct openpgp_publickey *key), void *ctx)
967 struct buffer_ctx fetchbuf;
968 struct openpgp_packet_list *packets = NULL;
969 struct openpgp_publickey *key = NULL;
971 for (i = 0; i < numdbs; i++) {
972 ret = dbconns[i]->cursor(dbconns[i],
977 memset(&dbkey, 0, sizeof(dbkey));
978 memset(&data, 0, sizeof(data));
979 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
981 fetchbuf.buffer = data.data;
983 fetchbuf.size = data.size;
984 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
986 parse_keys(packets, &key);
992 free_packet_list(packets);
995 memset(&dbkey, 0, sizeof(dbkey));
996 memset(&data, 0, sizeof(data));
997 ret = cursor->c_get(cursor, &dbkey, &data,
1001 if (ret != DB_NOTFOUND) {
1002 logthing(LOGTHING_ERROR,
1003 "Problem reading key: %s",
1007 ret = cursor->c_close(cursor);
1015 * getfullkeyid - Maps a 32bit key id to a 64bit one.
1016 * @keyid: The 32bit keyid.
1018 * This function maps a 32bit key id to the full 64bit one. It returns the
1019 * full keyid. If the key isn't found a keyid of 0 is returned.
1021 static uint64_t db4_getfullkeyid(uint64_t keyid)
1025 uint32_t shortkeyid = 0;
1028 if (keyid < 0x100000000LL) {
1029 ret = id32db->cursor(id32db,
1034 shortkeyid = keyid & 0xFFFFFFFF;
1036 memset(&key, 0, sizeof(key));
1037 memset(&data, 0, sizeof(data));
1038 key.data = &shortkeyid;
1039 key.size = sizeof(shortkeyid);
1040 data.flags = DB_DBT_MALLOC;
1042 ret = cursor->c_get(cursor,
1048 keyid = *(uint64_t *) data.data;
1050 if (data.data != NULL) {
1056 ret = cursor->c_close(cursor);
1064 * Include the basic keydb routines.
1066 #define NEED_GETKEYSIGS 1
1067 #define NEED_KEYID2UID 1
1068 #define NEED_UPDATEKEYS 1