2 * keydb_db4.c - Routines to store and fetch keys in a DB3 database.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002-2004 Project Purple
21 #include "charfuncs.h"
24 #include "decodekey.h"
25 #include "keystructs.h"
28 #include "onak-conf.h"
33 * dbenv - our database environment.
35 static DB_ENV *dbenv = NULL;
38 * numdb - The number of database files we have.
40 static int numdbs = 16;
43 * dbconn - our connections to the key database files.
45 static DB **dbconns = NULL;
48 * worddb - our connection to the word database.
50 static DB *worddb = NULL;
53 * id32db - our connection to the 32bit ID database.
55 static DB *id32db = NULL;
58 * txn - our current transaction id.
60 static DB_TXN *txn = NULL;
62 DB *keydb(uint64_t keyid)
68 return(dbconns[keytrun % numdbs]);
72 * initdb - Initialize the key database.
74 * This function should be called before any of the other functions in
75 * this file are called in order to allow the DB to be initialized ready
78 void initdb(bool readonly)
86 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
87 numdb = fopen(buf, "r");
89 if (fgets(buf, sizeof(buf), numdb) != NULL) {
93 } else if (!readonly) {
94 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
96 numdb = fopen(buf, "w");
98 fprintf(numdb, "%d", numdbs);
101 logthing(LOGTHING_ERROR,
102 "Couldn't write num_keydb: %s",
107 dbconns = malloc(sizeof (DB *) * numdbs);
108 if (dbconns == NULL) {
109 logthing(LOGTHING_CRITICAL,
110 "Couldn't allocate memory for dbconns");
114 ret = db_env_create(&dbenv, 0);
116 logthing(LOGTHING_CRITICAL,
117 "db_env_create: %s", db_strerror(ret));
122 * Enable deadlock detection so that we don't block indefinitely on
123 * anything. What we really want is simple 2 state locks, but I'm not
124 * sure how to make the standard DB functions do that yet.
126 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
128 logthing(LOGTHING_CRITICAL,
129 "db_env_create: %s", db_strerror(ret));
133 ret = dbenv->open(dbenv, config.db_dir,
134 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
139 logthing(LOGTHING_CRITICAL,
140 "Error opening db environment: %s (%s)",
148 for (i = 0; i < numdbs; i++) {
149 ret = db_create(&dbconns[i], dbenv, 0);
151 logthing(LOGTHING_CRITICAL,
152 "db_create: %s", db_strerror(ret));
156 snprintf(buf, 1023, "keydb.%d.db", i);
161 ret = dbconns[i]->open(dbconns[i],
169 logthing(LOGTHING_CRITICAL,
170 "Error opening key database: %s (%s)",
177 ret = db_create(&worddb, dbenv, 0);
179 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
182 ret = worddb->set_flags(worddb, DB_DUP);
184 ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
188 logthing(LOGTHING_CRITICAL,
189 "Error opening word database: %s (%s)",
195 ret = db_create(&id32db, dbenv, 0);
197 logthing(LOGTHING_CRITICAL, "db_create: %s", db_strerror(ret));
200 ret = id32db->set_flags(id32db, DB_DUP);
202 ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
206 logthing(LOGTHING_CRITICAL,
207 "Error opening id32 database: %s (%s)",
218 * cleanupdb - De-initialize the key database.
220 * This function should be called upon program exit to allow the DB to
221 * cleanup after itself.
227 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
228 id32db->close(id32db, 0);
230 worddb->close(worddb, 0);
232 for (i = 0; i < numdbs; i++) {
233 dbconns[i]->close(dbconns[i], 0);
236 dbenv->close(dbenv, 0);
241 * starttrans - Start a transaction.
243 * Start a transaction. Intended to be used if we're about to perform many
244 * operations on the database to help speed it all up, or if we want
245 * something to only succeed if all relevant operations are successful.
247 bool starttrans(void)
251 log_assert(dbenv != NULL);
252 log_assert(txn == NULL);
254 ret = dbenv->txn_begin(dbenv,
255 NULL, /* No parent transaction */
259 logthing(LOGTHING_CRITICAL,
260 "Error starting transaction: %s",
269 * endtrans - End a transaction.
271 * Ends a transaction.
277 log_assert(dbenv != NULL);
278 log_assert(txn != NULL);
280 ret = txn->commit(txn,
283 logthing(LOGTHING_CRITICAL,
284 "Error ending transaction: %s",
294 * fetch_key - Given a keyid fetch the key from storage.
295 * @keyid: The keyid to fetch.
296 * @publickey: A pointer to a structure to return the key in.
297 * @intrans: If we're already in a transaction.
299 * We use the hex representation of the keyid as the filename to fetch the
300 * key from. The key is stored in the file as a binary OpenPGP stream of
301 * packets, so we can just use read_openpgp_stream() to read the packets
302 * in and then parse_keys() to parse the packets into a publickey
305 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
308 struct openpgp_packet_list *packets = NULL;
312 struct buffer_ctx fetchbuf;
314 if (keyid < 0x100000000LL) {
315 keyid = getfullkeyid(keyid);
318 memset(&key, 0, sizeof(key));
319 memset(&data, 0, sizeof(data));
324 key.size = sizeof(keyid);
331 ret = keydb(keyid)->get(keydb(keyid),
338 fetchbuf.buffer = data.data;
340 fetchbuf.size = data.size;
341 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
343 parse_keys(packets, publickey);
344 free_packet_list(packets);
347 } else if (ret != DB_NOTFOUND) {
348 logthing(LOGTHING_ERROR,
349 "Problem retrieving key: %s",
360 int worddb_cmp(const void *d1, const void *d2)
362 return memcmp(d1, d2, 12);
366 * fetch_key_text - Trys to find the keys that contain the supplied text.
367 * @search: The text to search for.
368 * @publickey: A pointer to a structure to return the key in.
370 * This function searches for the supplied text and returns the keys that
373 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
381 char *searchtext = NULL;
382 struct ll *wordlist = NULL;
383 struct ll *curword = NULL;
384 struct ll *keylist = NULL;
385 struct ll *newkeylist = NULL;
388 searchtext = strdup(search);
389 wordlist = makewordlist(wordlist, searchtext);
393 ret = worddb->cursor(worddb,
398 for (curword = wordlist; curword != NULL; curword = curword->next) {
399 memset(&key, 0, sizeof(key));
400 memset(&data, 0, sizeof(data));
401 key.data = curword->object;
402 key.size = strlen(curword->object);
403 data.flags = DB_DBT_MALLOC;
404 ret = cursor->c_get(cursor,
408 while (ret == 0 && strncmp(key.data, curword->object,
410 ((char *) curword->object)[key.size] == 0) {
412 for (i = 4; i < 12; i++) {
414 keyid += ((unsigned char *)
418 if (keylist == NULL ||
419 llfind(keylist, data.data,
420 worddb_cmp) != NULL) {
421 newkeylist = lladd(newkeylist, data.data);
427 ret = cursor->c_get(cursor,
432 llfree(keylist, free);
433 keylist = newkeylist;
435 if (data.data != NULL) {
440 llfree(wordlist, NULL);
443 for (newkeylist = keylist;
444 newkeylist != NULL && numkeys < config.maxkeys;
445 newkeylist = newkeylist->next) {
448 for (i = 4; i < 12; i++) {
450 keyid += ((unsigned char *)
451 newkeylist->object)[i];
454 numkeys += fetch_key(keyid,
458 llfree(keylist, free);
463 ret = cursor->c_close(cursor);
472 * store_key - Takes a key and stores it.
473 * @publickey: A pointer to the public key to store.
474 * @intrans: If we're already in a transaction.
475 * @update: If true the key exists and should be updated.
477 * Again we just use the hex representation of the keyid as the filename
478 * to store the key to. We flatten the public key to a list of OpenPGP
479 * packets and then use write_openpgp_stream() to write the stream out to
480 * the file. If update is true then we delete the old key first, otherwise
481 * we trust that it doesn't exist.
483 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
485 struct openpgp_packet_list *packets = NULL;
486 struct openpgp_packet_list *list_end = NULL;
487 struct openpgp_publickey *next = NULL;
490 struct buffer_ctx storebuf;
494 uint32_t shortkeyid = 0;
495 uint64_t *subkeyids = NULL;
497 char *primary = NULL;
498 unsigned char worddb_data[12];
499 struct ll *wordlist = NULL;
500 struct ll *curword = NULL;
501 bool deadlock = false;
503 keyid = get_keyid(publickey);
510 * Delete the key if we already have it.
512 * TODO: Can we optimize this perhaps? Possibly when other data is
513 * involved as well? I suspect this is easiest and doesn't make a lot
514 * of difference though - the largest chunk of data is the keydata and
515 * it definitely needs updated.
518 deadlock = (delete_key(keyid, true) == -1);
522 * Convert the key to a flat set of binary data.
525 next = publickey->next;
526 publickey->next = NULL;
527 flatten_publickey(publickey, &packets, &list_end);
528 publickey->next = next;
531 storebuf.size = 8192;
532 storebuf.buffer = malloc(8192);
534 write_openpgp_stream(buffer_putchar, &storebuf, packets);
537 * Now we have the key data store it in the DB; the keyid is
540 memset(&key, 0, sizeof(key));
541 memset(&data, 0, sizeof(data));
543 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++) {
638 * Write the truncated 32 bit keyid so we can lookup the full id for
642 shortkeyid = keyid & 0xFFFFFFFF;
644 memset(&key, 0, sizeof(key));
645 memset(&data, 0, sizeof(data));
646 key.data = &shortkeyid;
647 key.size = sizeof(shortkeyid);
649 data.size = sizeof(keyid);
651 ret = id32db->put(id32db,
657 logthing(LOGTHING_ERROR,
658 "Problem storing short keyid: %s",
660 if (ret == DB_LOCK_DEADLOCK) {
667 subkeyids = keysubkeys(publickey);
669 while (subkeyids != NULL && subkeyids[i] != 0) {
670 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
672 memset(&key, 0, sizeof(key));
673 memset(&data, 0, sizeof(data));
674 key.data = &shortkeyid;
675 key.size = sizeof(shortkeyid);
677 data.size = sizeof(keyid);
679 ret = id32db->put(id32db,
685 logthing(LOGTHING_ERROR,
686 "Problem storing short keyid: %s",
688 if (ret == DB_LOCK_DEADLOCK) {
693 if (subkeyids != NULL) {
699 return deadlock ? -1 : 0 ;
703 * delete_key - Given a keyid delete the key from storage.
704 * @keyid: The keyid to delete.
705 * @intrans: If we're already in a transaction.
707 * This function deletes a public key from whatever storage mechanism we
708 * are using. Returns 0 if the key existed.
710 int delete_key(uint64_t keyid, bool intrans)
712 struct openpgp_publickey *publickey = NULL;
715 uint32_t shortkeyid = 0;
716 uint64_t *subkeyids = NULL;
720 char *primary = NULL;
721 unsigned char worddb_data[12];
722 struct ll *wordlist = NULL;
723 struct ll *curword = NULL;
724 bool deadlock = false;
730 fetch_key(keyid, &publickey, true);
733 * Walk through the uids removing the words from the worddb.
735 if (publickey != NULL) {
736 uids = keyuids(publickey, &primary);
739 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
740 wordlist = makewordlist(wordlist, uids[i]);
743 ret = worddb->cursor(worddb,
748 for (curword = wordlist; curword != NULL && !deadlock;
749 curword = curword->next) {
750 memset(&key, 0, sizeof(key));
751 memset(&data, 0, sizeof(data));
752 key.data = curword->object;
753 key.size = strlen(key.data);
754 data.data = worddb_data;
755 data.size = sizeof(worddb_data);
758 * Our data is the key creation time followed by the
761 worddb_data[ 0] = publickey->publickey->data[1];
762 worddb_data[ 1] = publickey->publickey->data[2];
763 worddb_data[ 2] = publickey->publickey->data[3];
764 worddb_data[ 3] = publickey->publickey->data[4];
765 worddb_data[ 4] = (keyid >> 56) & 0xFF;
766 worddb_data[ 5] = (keyid >> 48) & 0xFF;
767 worddb_data[ 6] = (keyid >> 40) & 0xFF;
768 worddb_data[ 7] = (keyid >> 32) & 0xFF;
769 worddb_data[ 8] = (keyid >> 24) & 0xFF;
770 worddb_data[ 9] = (keyid >> 16) & 0xFF;
771 worddb_data[10] = (keyid >> 8) & 0xFF;
772 worddb_data[11] = keyid & 0xFF;
774 ret = cursor->c_get(cursor,
780 ret = cursor->c_del(cursor, 0);
782 logthing(LOGTHING_ERROR,
783 "Problem deleting word: %s",
789 logthing(LOGTHING_ERROR,
790 "Problem deleting word: %s",
792 if (ret == DB_LOCK_DEADLOCK) {
797 ret = cursor->c_close(cursor);
801 * Free our UID and word lists.
803 llfree(wordlist, NULL);
804 for (i = 0; uids[i] != NULL; i++) {
810 free_publickey(publickey);
815 ret = id32db->cursor(id32db,
820 shortkeyid = keyid & 0xFFFFFFFF;
822 memset(&key, 0, sizeof(key));
823 memset(&data, 0, sizeof(data));
824 key.data = &shortkeyid;
825 key.size = sizeof(shortkeyid);
827 data.size = sizeof(keyid);
829 ret = cursor->c_get(cursor,
835 ret = cursor->c_del(cursor, 0);
837 logthing(LOGTHING_ERROR,
838 "Problem deleting short keyid: %s",
844 logthing(LOGTHING_ERROR,
845 "Problem deleting short keyid: %s",
847 if (ret == DB_LOCK_DEADLOCK) {
852 subkeyids = keysubkeys(publickey);
854 while (subkeyids != NULL && subkeyids[i] != 0) {
855 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
857 memset(&key, 0, sizeof(key));
858 memset(&data, 0, sizeof(data));
859 key.data = &shortkeyid;
860 key.size = sizeof(shortkeyid);
862 data.size = sizeof(keyid);
864 ret = cursor->c_get(cursor,
870 ret = cursor->c_del(cursor, 0);
872 logthing(LOGTHING_ERROR,
873 "Problem deleting short"
880 logthing(LOGTHING_ERROR,
881 "Problem deleting short keyid: %s",
883 if (ret == DB_LOCK_DEADLOCK) {
888 if (subkeyids != NULL) {
893 ret = cursor->c_close(cursor);
899 key.size = sizeof(keyid);
901 keydb(keyid)->del(keydb(keyid),
911 return deadlock ? (-1) : (ret == DB_NOTFOUND);
915 * dumpdb - dump the key database
916 * @filenamebase: The base filename to use for the dump.
918 * Dumps the database into one or more files, which contain pure OpenPGP
919 * that can be reimported into onak or gpg. filenamebase provides a base
920 * file name for the dump; several files may be created, all of which will
921 * begin with this string and then have a unique number and a .pgp
924 int dumpdb(char *filenamebase)
934 for (i = 0; i < numdbs; i++) {
935 ret = dbconns[i]->cursor(dbconns[i],
940 snprintf(filename, 1023, "%s.%d.pgp", filenamebase, i);
941 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0640);
943 logthing(LOGTHING_ERROR,
944 "Error opening keydump file (%s): %s",
948 memset(&key, 0, sizeof(key));
949 memset(&data, 0, sizeof(data));
950 ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
952 write(fd, data.data, data.size);
953 memset(&key, 0, sizeof(key));
954 memset(&data, 0, sizeof(data));
955 ret = cursor->c_get(cursor, &key, &data,
958 if (ret != DB_NOTFOUND) {
959 logthing(LOGTHING_ERROR,
960 "Problem reading key: %s",
966 ret = cursor->c_close(cursor);
974 * getfullkeyid - Maps a 32bit key id to a 64bit one.
975 * @keyid: The 32bit keyid.
977 * This function maps a 32bit key id to the full 64bit one. It returns the
978 * full keyid. If the key isn't found a keyid of 0 is returned.
980 uint64_t getfullkeyid(uint64_t keyid)
984 uint32_t shortkeyid = 0;
987 if (keyid < 0x100000000LL) {
988 ret = id32db->cursor(id32db,
993 shortkeyid = keyid & 0xFFFFFFFF;
995 memset(&key, 0, sizeof(key));
996 memset(&data, 0, sizeof(data));
997 key.data = &shortkeyid;
998 key.size = sizeof(shortkeyid);
999 data.flags = DB_DBT_MALLOC;
1001 ret = cursor->c_get(cursor,
1007 keyid = *(uint64_t *) data.data;
1009 if (data.data != NULL) {
1015 ret = cursor->c_close(cursor);
1023 * Include the basic keydb routines.
1025 #define NEED_GETKEYSIGS 1
1026 #define NEED_KEYID2UID 1