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 * initdb - Initialize the key database.
75 * This function should be called before any of the other functions in
76 * this file are called in order to allow the DB to be initialized ready
79 void initdb(bool readonly)
87 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
88 numdb = fopen(buf, "r");
90 if (fgets(buf, sizeof(buf), numdb) != NULL) {
94 } else if (!readonly) {
95 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
97 numdb = fopen(buf, "w");
99 fprintf(numdb, "%d", numdbs);
102 logthing(LOGTHING_ERROR,
103 "Couldn't write num_keydb: %s",
108 dbconns = malloc(sizeof (DB *) * numdbs);
109 if (dbconns == NULL) {
110 logthing(LOGTHING_CRITICAL,
111 "Couldn't allocate memory for dbconns");
116 ret = db_env_create(&dbenv, 0);
118 logthing(LOGTHING_CRITICAL,
119 "db_env_create: %s", db_strerror(ret));
124 * Enable deadlock detection so that we don't block indefinitely on
125 * anything. What we really want is simple 2 state locks, but I'm not
126 * sure how to make the standard DB functions do that yet.
129 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
131 logthing(LOGTHING_CRITICAL,
132 "db_env_create: %s", db_strerror(ret));
137 ret = dbenv->open(dbenv, config.db_dir,
138 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
143 logthing(LOGTHING_CRITICAL,
144 "Error opening db environment: %s (%s)",
153 for (i = 0; !ret && i < numdbs; i++) {
154 ret = db_create(&dbconns[i], dbenv, 0);
156 logthing(LOGTHING_CRITICAL,
157 "db_create: %s", db_strerror(ret));
161 snprintf(buf, 1023, "keydb.%d.db", i);
166 ret = dbconns[i]->open(dbconns[i],
174 logthing(LOGTHING_CRITICAL,
175 "Error opening key database:"
186 ret = db_create(&worddb, dbenv, 0);
188 logthing(LOGTHING_CRITICAL, "db_create: %s",
194 ret = worddb->set_flags(worddb, DB_DUP);
198 ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
202 logthing(LOGTHING_CRITICAL,
203 "Error opening word database: %s (%s)",
210 ret = db_create(&id32db, dbenv, 0);
212 logthing(LOGTHING_CRITICAL, "db_create: %s",
218 ret = id32db->set_flags(id32db, DB_DUP);
222 ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
226 logthing(LOGTHING_CRITICAL,
227 "Error opening id32 database: %s (%s)",
239 logthing(LOGTHING_CRITICAL,
240 "Error opening database; exiting");
248 * cleanupdb - De-initialize the key database.
250 * This function should be called upon program exit to allow the DB to
251 * cleanup after itself.
258 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
259 if (id32db != NULL) {
260 id32db->close(id32db, 0);
263 if (worddb != NULL) {
264 worddb->close(worddb, 0);
267 for (i = 0; i < numdbs; i++) {
268 if (dbconns[i] != NULL) {
269 dbconns[i]->close(dbconns[i], 0);
275 dbenv->close(dbenv, 0);
281 * starttrans - Start a transaction.
283 * Start a transaction. Intended to be used if we're about to perform many
284 * operations on the database to help speed it all up, or if we want
285 * something to only succeed if all relevant operations are successful.
287 bool starttrans(void)
291 log_assert(dbenv != NULL);
292 log_assert(txn == NULL);
294 ret = dbenv->txn_begin(dbenv,
295 NULL, /* No parent transaction */
299 logthing(LOGTHING_CRITICAL,
300 "Error starting transaction: %s",
309 * endtrans - End a transaction.
311 * Ends a transaction.
317 log_assert(dbenv != NULL);
318 log_assert(txn != NULL);
320 ret = txn->commit(txn,
323 logthing(LOGTHING_CRITICAL,
324 "Error ending transaction: %s",
334 * fetch_key - Given a keyid fetch the key from storage.
335 * @keyid: The keyid to fetch.
336 * @publickey: A pointer to a structure to return the key in.
337 * @intrans: If we're already in a transaction.
339 * We use the hex representation of the keyid as the filename to fetch the
340 * key from. The key is stored in the file as a binary OpenPGP stream of
341 * packets, so we can just use read_openpgp_stream() to read the packets
342 * in and then parse_keys() to parse the packets into a publickey
345 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
348 struct openpgp_packet_list *packets = NULL;
352 struct buffer_ctx fetchbuf;
354 if (keyid < 0x100000000LL) {
355 keyid = getfullkeyid(keyid);
358 memset(&key, 0, sizeof(key));
359 memset(&data, 0, sizeof(data));
364 key.size = sizeof(keyid);
371 ret = keydb(keyid)->get(keydb(keyid),
378 fetchbuf.buffer = data.data;
380 fetchbuf.size = data.size;
381 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
383 parse_keys(packets, publickey);
384 free_packet_list(packets);
387 } else if (ret != DB_NOTFOUND) {
388 logthing(LOGTHING_ERROR,
389 "Problem retrieving key: %s",
400 int worddb_cmp(const void *d1, const void *d2)
402 return memcmp(d1, d2, 12);
406 * fetch_key_text - Trys to find the keys that contain the supplied text.
407 * @search: The text to search for.
408 * @publickey: A pointer to a structure to return the key in.
410 * This function searches for the supplied text and returns the keys that
413 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
421 char *searchtext = NULL;
422 struct ll *wordlist = NULL;
423 struct ll *curword = NULL;
424 struct keyarray keylist = { NULL, 0, 0 };
425 struct keyarray newkeylist = { NULL, 0, 0 };
428 searchtext = strdup(search);
429 wordlist = makewordlist(wordlist, searchtext);
431 for (curword = wordlist; curword != NULL; curword = curword->next) {
434 ret = worddb->cursor(worddb,
439 memset(&key, 0, sizeof(key));
440 memset(&data, 0, sizeof(data));
441 key.data = curword->object;
442 key.size = strlen(curword->object);
443 data.flags = DB_DBT_MALLOC;
444 ret = cursor->c_get(cursor,
448 while (ret == 0 && strncmp(key.data, curword->object,
450 ((char *) curword->object)[key.size] == 0) {
452 for (i = 4; i < 12; i++) {
454 keyid += ((unsigned char *)
458 if (keylist.count == 0 ||
459 array_find(&keylist, keyid)) {
460 array_add(&newkeylist, keyid);
466 ret = cursor->c_get(cursor,
471 array_free(&keylist);
472 keylist = newkeylist;
473 newkeylist.keys = NULL;
474 newkeylist.count = newkeylist.size = 0;
475 if (data.data != NULL) {
479 ret = cursor->c_close(cursor);
483 llfree(wordlist, NULL);
487 for (i = 0; i < keylist.count; i++) {
488 numkeys += fetch_key(keylist.keys[i],
492 array_free(&keylist);
502 * store_key - Takes a key and stores it.
503 * @publickey: A pointer to the public key to store.
504 * @intrans: If we're already in a transaction.
505 * @update: If true the key exists and should be updated.
507 * Again we just use the hex representation of the keyid as the filename
508 * to store the key to. We flatten the public key to a list of OpenPGP
509 * packets and then use write_openpgp_stream() to write the stream out to
510 * the file. If update is true then we delete the old key first, otherwise
511 * we trust that it doesn't exist.
513 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
515 struct openpgp_packet_list *packets = NULL;
516 struct openpgp_packet_list *list_end = NULL;
517 struct openpgp_publickey *next = NULL;
520 struct buffer_ctx storebuf;
524 uint32_t shortkeyid = 0;
525 uint64_t *subkeyids = NULL;
527 char *primary = NULL;
528 unsigned char worddb_data[12];
529 struct ll *wordlist = NULL;
530 struct ll *curword = NULL;
531 bool deadlock = false;
533 keyid = get_keyid(publickey);
540 * Delete the key if we already have it.
542 * TODO: Can we optimize this perhaps? Possibly when other data is
543 * involved as well? I suspect this is easiest and doesn't make a lot
544 * of difference though - the largest chunk of data is the keydata and
545 * it definitely needs updated.
548 deadlock = (delete_key(keyid, true) == -1);
552 * Convert the key to a flat set of binary data.
555 next = publickey->next;
556 publickey->next = NULL;
557 flatten_publickey(publickey, &packets, &list_end);
558 publickey->next = next;
561 storebuf.size = 8192;
562 storebuf.buffer = malloc(8192);
564 write_openpgp_stream(buffer_putchar, &storebuf, packets);
567 * Now we have the key data store it in the DB; the keyid is
570 memset(&key, 0, sizeof(key));
571 memset(&data, 0, sizeof(data));
573 key.size = sizeof(keyid);
574 data.size = storebuf.offset;
575 data.data = storebuf.buffer;
577 ret = keydb(keyid)->put(keydb(keyid),
583 logthing(LOGTHING_ERROR,
584 "Problem storing key: %s",
586 if (ret == DB_LOCK_DEADLOCK) {
591 free(storebuf.buffer);
592 storebuf.buffer = NULL;
596 free_packet_list(packets);
601 * Walk through our uids storing the words into the db with the keyid.
604 uids = keyuids(publickey, &primary);
607 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
608 wordlist = makewordlist(wordlist, uids[i]);
611 for (curword = wordlist; curword != NULL && !deadlock;
612 curword = curword->next) {
613 memset(&key, 0, sizeof(key));
614 memset(&data, 0, sizeof(data));
615 key.data = curword->object;
616 key.size = strlen(key.data);
617 data.data = worddb_data;
618 data.size = sizeof(worddb_data);
621 * Our data is the key creation time followed by the
624 worddb_data[ 0] = publickey->publickey->data[1];
625 worddb_data[ 1] = publickey->publickey->data[2];
626 worddb_data[ 2] = publickey->publickey->data[3];
627 worddb_data[ 3] = publickey->publickey->data[4];
628 worddb_data[ 4] = (keyid >> 56) & 0xFF;
629 worddb_data[ 5] = (keyid >> 48) & 0xFF;
630 worddb_data[ 6] = (keyid >> 40) & 0xFF;
631 worddb_data[ 7] = (keyid >> 32) & 0xFF;
632 worddb_data[ 8] = (keyid >> 24) & 0xFF;
633 worddb_data[ 9] = (keyid >> 16) & 0xFF;
634 worddb_data[10] = (keyid >> 8) & 0xFF;
635 worddb_data[11] = keyid & 0xFF;
636 ret = worddb->put(worddb,
642 logthing(LOGTHING_ERROR,
643 "Problem storing word: %s",
645 if (ret == DB_LOCK_DEADLOCK) {
652 * Free our UID and word lists.
654 llfree(wordlist, NULL);
655 for (i = 0; uids[i] != NULL; i++) {
664 * Write the truncated 32 bit keyid so we can lookup the full id for
668 shortkeyid = keyid & 0xFFFFFFFF;
670 memset(&key, 0, sizeof(key));
671 memset(&data, 0, sizeof(data));
672 key.data = &shortkeyid;
673 key.size = sizeof(shortkeyid);
675 data.size = sizeof(keyid);
677 ret = id32db->put(id32db,
683 logthing(LOGTHING_ERROR,
684 "Problem storing short keyid: %s",
686 if (ret == DB_LOCK_DEADLOCK) {
693 subkeyids = keysubkeys(publickey);
695 while (subkeyids != NULL && subkeyids[i] != 0) {
696 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
698 memset(&key, 0, sizeof(key));
699 memset(&data, 0, sizeof(data));
700 key.data = &shortkeyid;
701 key.size = sizeof(shortkeyid);
703 data.size = sizeof(keyid);
705 ret = id32db->put(id32db,
711 logthing(LOGTHING_ERROR,
712 "Problem storing short keyid: %s",
714 if (ret == DB_LOCK_DEADLOCK) {
719 if (subkeyids != NULL) {
729 return deadlock ? -1 : 0 ;
733 * delete_key - Given a keyid delete the key from storage.
734 * @keyid: The keyid to delete.
735 * @intrans: If we're already in a transaction.
737 * This function deletes a public key from whatever storage mechanism we
738 * are using. Returns 0 if the key existed.
740 int delete_key(uint64_t keyid, bool intrans)
742 struct openpgp_publickey *publickey = NULL;
745 uint32_t shortkeyid = 0;
746 uint64_t *subkeyids = NULL;
750 char *primary = NULL;
751 unsigned char worddb_data[12];
752 struct ll *wordlist = NULL;
753 struct ll *curword = NULL;
754 bool deadlock = false;
760 fetch_key(keyid, &publickey, true);
763 * Walk through the uids removing the words from the worddb.
765 if (publickey != NULL) {
766 uids = keyuids(publickey, &primary);
769 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
770 wordlist = makewordlist(wordlist, uids[i]);
773 ret = worddb->cursor(worddb,
778 for (curword = wordlist; curword != NULL && !deadlock;
779 curword = curword->next) {
780 memset(&key, 0, sizeof(key));
781 memset(&data, 0, sizeof(data));
782 key.data = curword->object;
783 key.size = strlen(key.data);
784 data.data = worddb_data;
785 data.size = sizeof(worddb_data);
788 * Our data is the key creation time followed by the
791 worddb_data[ 0] = publickey->publickey->data[1];
792 worddb_data[ 1] = publickey->publickey->data[2];
793 worddb_data[ 2] = publickey->publickey->data[3];
794 worddb_data[ 3] = publickey->publickey->data[4];
795 worddb_data[ 4] = (keyid >> 56) & 0xFF;
796 worddb_data[ 5] = (keyid >> 48) & 0xFF;
797 worddb_data[ 6] = (keyid >> 40) & 0xFF;
798 worddb_data[ 7] = (keyid >> 32) & 0xFF;
799 worddb_data[ 8] = (keyid >> 24) & 0xFF;
800 worddb_data[ 9] = (keyid >> 16) & 0xFF;
801 worddb_data[10] = (keyid >> 8) & 0xFF;
802 worddb_data[11] = keyid & 0xFF;
804 ret = cursor->c_get(cursor,
810 ret = cursor->c_del(cursor, 0);
812 logthing(LOGTHING_ERROR,
813 "Problem deleting word: %s",
819 logthing(LOGTHING_ERROR,
820 "Problem deleting word: %s",
822 if (ret == DB_LOCK_DEADLOCK) {
827 ret = cursor->c_close(cursor);
831 * Free our UID and word lists.
833 llfree(wordlist, NULL);
834 for (i = 0; uids[i] != NULL; i++) {
840 free_publickey(publickey);
845 ret = id32db->cursor(id32db,
850 shortkeyid = keyid & 0xFFFFFFFF;
852 memset(&key, 0, sizeof(key));
853 memset(&data, 0, sizeof(data));
854 key.data = &shortkeyid;
855 key.size = sizeof(shortkeyid);
857 data.size = sizeof(keyid);
859 ret = cursor->c_get(cursor,
865 ret = cursor->c_del(cursor, 0);
867 logthing(LOGTHING_ERROR,
868 "Problem deleting short keyid: %s",
874 logthing(LOGTHING_ERROR,
875 "Problem deleting short keyid: %s",
877 if (ret == DB_LOCK_DEADLOCK) {
882 subkeyids = keysubkeys(publickey);
884 while (subkeyids != NULL && subkeyids[i] != 0) {
885 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
887 memset(&key, 0, sizeof(key));
888 memset(&data, 0, sizeof(data));
889 key.data = &shortkeyid;
890 key.size = sizeof(shortkeyid);
892 data.size = sizeof(keyid);
894 ret = cursor->c_get(cursor,
900 ret = cursor->c_del(cursor, 0);
902 logthing(LOGTHING_ERROR,
903 "Problem deleting short"
910 logthing(LOGTHING_ERROR,
911 "Problem deleting short keyid: %s",
913 if (ret == DB_LOCK_DEADLOCK) {
918 if (subkeyids != NULL) {
923 ret = cursor->c_close(cursor);
929 key.size = sizeof(keyid);
931 keydb(keyid)->del(keydb(keyid),
941 return deadlock ? (-1) : (ret == DB_NOTFOUND);
945 * iterate_keys - call a function once for each key in the db.
946 * @iterfunc: The function to call.
947 * @ctx: A context pointer
949 * Calls iterfunc once for each key in the database. ctx is passed
950 * unaltered to iterfunc. This function is intended to aid database dumps
951 * and statistic calculations.
953 * Returns the number of keys we iterated over.
955 int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
963 struct buffer_ctx fetchbuf;
964 struct openpgp_packet_list *packets = NULL;
965 struct openpgp_publickey *key = NULL;
967 for (i = 0; i < numdbs; i++) {
968 ret = dbconns[i]->cursor(dbconns[i],
973 memset(&dbkey, 0, sizeof(dbkey));
974 memset(&data, 0, sizeof(data));
975 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
977 fetchbuf.buffer = data.data;
979 fetchbuf.size = data.size;
980 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
982 parse_keys(packets, &key);
988 free_packet_list(packets);
991 memset(&dbkey, 0, sizeof(dbkey));
992 memset(&data, 0, sizeof(data));
993 ret = cursor->c_get(cursor, &dbkey, &data,
997 if (ret != DB_NOTFOUND) {
998 logthing(LOGTHING_ERROR,
999 "Problem reading key: %s",
1003 ret = cursor->c_close(cursor);
1011 * getfullkeyid - Maps a 32bit key id to a 64bit one.
1012 * @keyid: The 32bit keyid.
1014 * This function maps a 32bit key id to the full 64bit one. It returns the
1015 * full keyid. If the key isn't found a keyid of 0 is returned.
1017 uint64_t getfullkeyid(uint64_t keyid)
1021 uint32_t shortkeyid = 0;
1024 if (keyid < 0x100000000LL) {
1025 ret = id32db->cursor(id32db,
1030 shortkeyid = keyid & 0xFFFFFFFF;
1032 memset(&key, 0, sizeof(key));
1033 memset(&data, 0, sizeof(data));
1034 key.data = &shortkeyid;
1035 key.size = sizeof(shortkeyid);
1036 data.flags = DB_DBT_MALLOC;
1038 ret = cursor->c_get(cursor,
1044 keyid = *(uint64_t *) data.data;
1046 if (data.data != NULL) {
1052 ret = cursor->c_close(cursor);
1060 * Include the basic keydb routines.
1062 #define NEED_GETKEYSIGS 1
1063 #define NEED_KEYID2UID 1
1064 #define NEED_UPDATEKEYS 1