2 * keydb_db4.c - Routines to store and fetch keys in a DB4 database.
4 * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <sys/types.h>
33 #include "charfuncs.h"
37 #include "decodekey.h"
38 #include "keystructs.h"
41 #include "onak-conf.h"
45 #define DB4_UPGRADE_FILE "db_upgrade.lck"
47 struct onak_db4_dbctx {
48 DB_ENV *dbenv; /* The database environment context */
49 int numdbs; /* Number of data databases in use */
50 DB **dbconns; /* Connections to the key data databases */
51 DB *worddb; /* Connection to the word lookup database */
52 DB *id32db; /* Connection to the 32 bit ID lookup database */
53 DB *skshashdb; /* Connection to the SKS hash database */
54 DB *subkeydb; /* Connection to the subkey ID lookup database */
55 DB_TXN *txn; /* Our current transaction ID */
58 DB *keydb(struct onak_db4_dbctx *privctx, uint64_t keyid)
64 return(privctx->dbconns[keytrun % privctx->numdbs]);
68 * db4_errfunc - Direct DB errors to logfile
70 * Basic function to take errors from the DB library and output them to
71 * the logfile rather than stderr.
73 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 3)
74 static void db4_errfunc(const char *errpfx, const char *errmsg)
76 static void db4_errfunc(const DB_ENV *edbenv, const char *errpfx,
81 logthing(LOGTHING_DEBUG, "db4 error: %s:%s", errpfx, errmsg);
83 logthing(LOGTHING_DEBUG, "db4 error: %s", errmsg);
90 * starttrans - Start a transaction.
92 * Start a transaction. Intended to be used if we're about to perform many
93 * operations on the database to help speed it all up, or if we want
94 * something to only succeed if all relevant operations are successful.
96 static bool db4_starttrans(struct onak_dbctx *dbctx)
98 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
101 log_assert(privctx->dbenv != NULL);
102 log_assert(privctx->txn == NULL);
104 ret = privctx->dbenv->txn_begin(privctx->dbenv,
105 NULL, /* No parent transaction */
109 logthing(LOGTHING_CRITICAL,
110 "Error starting transaction: %s",
119 * endtrans - End a transaction.
121 * Ends a transaction.
123 static void db4_endtrans(struct onak_dbctx *dbctx)
125 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
128 log_assert(privctx->dbenv != NULL);
129 log_assert(privctx->txn != NULL);
131 ret = privctx->txn->commit(privctx->txn,
134 logthing(LOGTHING_CRITICAL,
135 "Error ending transaction: %s",
145 * db4_upgradedb - Upgrade a DB4 database
147 * Called if we discover we need to upgrade our DB4 database; ie if
148 * we're running with a newer version of db4 than the database was
151 static int db4_upgradedb(struct onak_db4_dbctx *privctx)
160 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
162 lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
163 if (lockfile_fd < 0) {
164 if (errno == EEXIST) {
165 while (stat(buf, &statbuf) == 0) ;
168 logthing(LOGTHING_CRITICAL, "Couldn't open database "
169 "update lock file: %s", strerror(errno));
173 snprintf(buf, sizeof(buf) - 1, "%d", getpid());
174 write(lockfile_fd, buf, strlen(buf));
177 logthing(LOGTHING_NOTICE, "Upgrading DB4 database");
178 ret = db_env_create(&privctx->dbenv, 0);
180 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
181 privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0);
182 privctx->dbenv = NULL;
184 for (i = 0; i < privctx->numdbs; i++) {
185 ret = db_create(&curdb, NULL, 0);
187 snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
189 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
190 curdb->upgrade(curdb, buf, 0);
191 curdb->close(curdb, 0);
193 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
199 ret = db_create(&curdb, NULL, 0);
201 snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
202 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
203 curdb->upgrade(curdb, buf, 0);
204 curdb->close(curdb, 0);
206 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
211 ret = db_create(&curdb, NULL, 0);
213 snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
214 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
215 curdb->upgrade(curdb, buf, 0);
216 curdb->close(curdb, 0);
218 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
223 ret = db_create(&curdb, NULL, 0);
225 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
226 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
227 curdb->upgrade(curdb, buf, 0);
228 curdb->close(curdb, 0);
230 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
235 ret = db_create(&curdb, NULL, 0);
237 snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
238 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
239 curdb->upgrade(curdb, buf, 0);
240 curdb->close(curdb, 0);
242 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
247 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
255 * getfullkeyid - Maps a 32bit key id to a 64bit one.
256 * @keyid: The 32bit keyid.
258 * This function maps a 32bit key id to the full 64bit one. It returns the
259 * full keyid. If the key isn't found a keyid of 0 is returned.
261 static uint64_t db4_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
263 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
266 uint32_t shortkeyid = 0;
269 if (keyid < 0x100000000LL) {
270 ret = privctx->id32db->cursor(privctx->id32db,
279 shortkeyid = keyid & 0xFFFFFFFF;
281 memset(&key, 0, sizeof(key));
282 memset(&data, 0, sizeof(data));
283 key.data = &shortkeyid;
284 key.size = sizeof(shortkeyid);
285 data.flags = DB_DBT_MALLOC;
287 ret = cursor->c_get(cursor,
293 keyid = *(uint64_t *) data.data;
295 if (data.data != NULL) {
301 cursor->c_close(cursor);
309 * fetch_key_id - Given a keyid fetch the key from storage.
310 * @keyid: The keyid to fetch.
311 * @publickey: A pointer to a structure to return the key in.
312 * @intrans: If we're already in a transaction.
314 * We use the hex representation of the keyid as the filename to fetch the
315 * key from. The key is stored in the file as a binary OpenPGP stream of
316 * packets, so we can just use read_openpgp_stream() to read the packets
317 * in and then parse_keys() to parse the packets into a publickey
320 static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
321 struct openpgp_publickey **publickey,
324 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
325 struct openpgp_packet_list *packets = NULL;
329 struct buffer_ctx fetchbuf;
331 if (keyid < 0x100000000LL) {
332 keyid = db4_getfullkeyid(dbctx, keyid);
335 memset(&key, 0, sizeof(key));
336 memset(&data, 0, sizeof(data));
341 key.size = sizeof(keyid);
345 db4_starttrans(dbctx);
348 ret = keydb(privctx, keyid)->get(keydb(privctx, keyid),
354 if (ret == DB_NOTFOUND) {
355 /* If we didn't find the key ID see if it's a subkey ID */
356 memset(&key, 0, sizeof(key));
357 memset(&data, 0, sizeof(data));
360 key.size = sizeof(keyid);
363 ret = privctx->subkeydb->get(privctx->subkeydb,
370 /* We got a subkey match; retrieve the actual key */
371 keyid = *(uint64_t *) data.data;
373 memset(&key, 0, sizeof(key));
374 memset(&data, 0, sizeof(data));
377 key.size = sizeof(keyid);
380 ret = keydb(privctx, keyid)->get(keydb(privctx, keyid),
389 fetchbuf.buffer = data.data;
391 fetchbuf.size = data.size;
392 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
394 parse_keys(packets, publickey);
395 free_packet_list(packets);
398 } else if (ret != DB_NOTFOUND) {
399 logthing(LOGTHING_ERROR,
400 "Problem retrieving key: %s",
411 int worddb_cmp(const void *d1, const void *d2)
413 return memcmp(d1, d2, 12);
417 * fetch_key_text - Trys to find the keys that contain the supplied text.
418 * @search: The text to search for.
419 * @publickey: A pointer to a structure to return the key in.
421 * This function searches for the supplied text and returns the keys that
424 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
425 struct openpgp_publickey **publickey)
427 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
434 char *searchtext = NULL;
435 struct ll *wordlist = NULL;
436 struct ll *curword = NULL;
437 struct keyarray keylist = { NULL, 0, 0 };
438 struct keyarray newkeylist = { NULL, 0, 0 };
442 searchtext = strdup(search);
443 wordlist = makewordlist(wordlist, searchtext);
445 for (curword = wordlist; curword != NULL; curword = curword->next) {
446 db4_starttrans(dbctx);
448 ret = privctx->worddb->cursor(privctx->worddb,
458 memset(&key, 0, sizeof(key));
459 memset(&data, 0, sizeof(data));
460 key.data = curword->object;
461 key.size = strlen(curword->object);
462 data.flags = DB_DBT_MALLOC;
463 ret = cursor->c_get(cursor,
467 while (ret == 0 && strncmp(key.data, curword->object,
469 ((char *) curword->object)[key.size] == 0) {
471 for (i = 4; i < 12; i++) {
473 keyid += ((unsigned char *)
478 * Only add the keys containing this word if this is
479 * our first pass (ie we have no existing key list),
480 * or the key contained a previous word.
482 if (firstpass || array_find(&keylist, keyid)) {
483 array_add(&newkeylist, keyid);
489 ret = cursor->c_get(cursor,
494 array_free(&keylist);
495 keylist = newkeylist;
496 newkeylist.keys = NULL;
497 newkeylist.count = newkeylist.size = 0;
498 if (data.data != NULL) {
502 cursor->c_close(cursor);
507 llfree(wordlist, NULL);
510 if (keylist.count > config.maxkeys) {
511 keylist.count = config.maxkeys;
514 db4_starttrans(dbctx);
515 for (i = 0; i < keylist.count; i++) {
516 numkeys += db4_fetch_key_id(dbctx, keylist.keys[i],
520 array_free(&keylist);
529 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
530 const struct skshash *hash,
531 struct openpgp_publickey **publickey)
533 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
539 ret = privctx->skshashdb->cursor(privctx->skshashdb,
548 memset(&key, 0, sizeof(key));
549 memset(&data, 0, sizeof(data));
550 key.data = (void *) hash->hash;
551 key.size = sizeof(hash->hash);
552 data.flags = DB_DBT_MALLOC;
554 ret = cursor->c_get(cursor,
560 keyid = *(uint64_t *) data.data;
562 if (data.data != NULL) {
568 cursor->c_close(cursor);
571 return db4_fetch_key_id(dbctx, keyid, publickey, false);
575 * delete_key - Given a keyid delete the key from storage.
576 * @keyid: The keyid to delete.
577 * @intrans: If we're already in a transaction.
579 * This function deletes a public key from whatever storage mechanism we
580 * are using. Returns 0 if the key existed.
582 static int db4_delete_key(struct onak_dbctx *dbctx,
583 uint64_t keyid, bool intrans)
585 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
586 struct openpgp_publickey *publickey = NULL;
589 uint32_t shortkeyid = 0;
590 uint64_t *subkeyids = NULL;
594 char *primary = NULL;
595 unsigned char worddb_data[12];
596 struct ll *wordlist = NULL;
597 struct ll *curword = NULL;
598 bool deadlock = false;
602 db4_starttrans(dbctx);
605 db4_fetch_key_id(dbctx, keyid, &publickey, true);
608 * Walk through the uids removing the words from the worddb.
610 if (publickey != NULL) {
611 uids = keyuids(publickey, &primary);
614 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
615 wordlist = makewordlist(wordlist, uids[i]);
618 privctx->worddb->cursor(privctx->worddb,
623 for (curword = wordlist; curword != NULL && !deadlock;
624 curword = curword->next) {
625 memset(&key, 0, sizeof(key));
626 memset(&data, 0, sizeof(data));
627 key.data = curword->object;
628 key.size = strlen(key.data);
629 data.data = worddb_data;
630 data.size = sizeof(worddb_data);
633 * Our data is the key creation time followed by the
636 worddb_data[ 0] = publickey->publickey->data[1];
637 worddb_data[ 1] = publickey->publickey->data[2];
638 worddb_data[ 2] = publickey->publickey->data[3];
639 worddb_data[ 3] = publickey->publickey->data[4];
640 worddb_data[ 4] = (keyid >> 56) & 0xFF;
641 worddb_data[ 5] = (keyid >> 48) & 0xFF;
642 worddb_data[ 6] = (keyid >> 40) & 0xFF;
643 worddb_data[ 7] = (keyid >> 32) & 0xFF;
644 worddb_data[ 8] = (keyid >> 24) & 0xFF;
645 worddb_data[ 9] = (keyid >> 16) & 0xFF;
646 worddb_data[10] = (keyid >> 8) & 0xFF;
647 worddb_data[11] = keyid & 0xFF;
649 ret = cursor->c_get(cursor,
655 ret = cursor->c_del(cursor, 0);
659 logthing(LOGTHING_ERROR,
660 "Problem deleting word: %s "
661 "(0x%016" PRIX64 ")",
664 if (ret == DB_LOCK_DEADLOCK) {
669 cursor->c_close(cursor);
672 ret = privctx->skshashdb->cursor(privctx->skshashdb,
677 get_skshash(publickey, &hash);
679 memset(&key, 0, sizeof(key));
680 memset(&data, 0, sizeof(data));
681 key.data = hash.hash;
682 key.size = sizeof(hash.hash);
684 data.size = sizeof(keyid);
686 ret = cursor->c_get(cursor,
692 ret = cursor->c_del(cursor, 0);
696 logthing(LOGTHING_ERROR,
697 "Problem deleting skshash: %s "
698 "(0x%016" PRIX64 ")",
701 if (ret == DB_LOCK_DEADLOCK) {
706 cursor->c_close(cursor);
711 * Free our UID and word lists.
713 llfree(wordlist, NULL);
714 for (i = 0; uids[i] != NULL; i++) {
720 free_publickey(publickey);
725 privctx->id32db->cursor(privctx->id32db,
730 shortkeyid = keyid & 0xFFFFFFFF;
732 memset(&key, 0, sizeof(key));
733 memset(&data, 0, sizeof(data));
734 key.data = &shortkeyid;
735 key.size = sizeof(shortkeyid);
737 data.size = sizeof(keyid);
739 ret = cursor->c_get(cursor,
745 ret = cursor->c_del(cursor, 0);
749 logthing(LOGTHING_ERROR,
750 "Problem deleting short keyid: %s "
751 "(0x%016" PRIX64 ")",
754 if (ret == DB_LOCK_DEADLOCK) {
759 subkeyids = keysubkeys(publickey);
761 while (subkeyids != NULL && subkeyids[i] != 0) {
762 memset(&key, 0, sizeof(key));
763 key.data = &subkeyids[i];
764 key.size = sizeof(subkeyids[i]);
765 privctx->subkeydb->del(privctx->subkeydb,
766 privctx->txn, &key, 0);
768 logthing(LOGTHING_ERROR,
769 "Problem deleting subkey id: %s "
770 "(0x%016" PRIX64 ")",
773 if (ret == DB_LOCK_DEADLOCK) {
778 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
780 memset(&key, 0, sizeof(key));
781 memset(&data, 0, sizeof(data));
782 key.data = &shortkeyid;
783 key.size = sizeof(shortkeyid);
785 data.size = sizeof(keyid);
787 ret = cursor->c_get(cursor,
793 ret = cursor->c_del(cursor, 0);
797 logthing(LOGTHING_ERROR,
798 "Problem deleting short keyid: %s "
799 "(0x%016" PRIX64 ")",
802 if (ret == DB_LOCK_DEADLOCK) {
807 if (subkeyids != NULL) {
811 cursor->c_close(cursor);
817 key.size = sizeof(keyid);
819 keydb(privctx, keyid)->del(keydb(privctx, keyid),
829 return deadlock ? (-1) : (ret == DB_NOTFOUND);
833 * store_key - Takes a key and stores it.
834 * @publickey: A pointer to the public key to store.
835 * @intrans: If we're already in a transaction.
836 * @update: If true the key exists and should be updated.
838 * Again we just use the hex representation of the keyid as the filename
839 * to store the key to. We flatten the public key to a list of OpenPGP
840 * packets and then use write_openpgp_stream() to write the stream out to
841 * the file. If update is true then we delete the old key first, otherwise
842 * we trust that it doesn't exist.
844 static int db4_store_key(struct onak_dbctx *dbctx,
845 struct openpgp_publickey *publickey, bool intrans,
848 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
849 struct openpgp_packet_list *packets = NULL;
850 struct openpgp_packet_list *list_end = NULL;
851 struct openpgp_publickey *next = NULL;
854 struct buffer_ctx storebuf;
858 uint32_t shortkeyid = 0;
859 uint64_t *subkeyids = NULL;
861 char *primary = NULL;
862 unsigned char worddb_data[12];
863 struct ll *wordlist = NULL;
864 struct ll *curword = NULL;
865 bool deadlock = false;
868 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
869 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
874 db4_starttrans(dbctx);
878 * Delete the key if we already have it.
880 * TODO: Can we optimize this perhaps? Possibly when other data is
881 * involved as well? I suspect this is easiest and doesn't make a lot
882 * of difference though - the largest chunk of data is the keydata and
883 * it definitely needs updated.
886 deadlock = (db4_delete_key(dbctx, keyid, true) == -1);
890 * Convert the key to a flat set of binary data.
893 next = publickey->next;
894 publickey->next = NULL;
895 flatten_publickey(publickey, &packets, &list_end);
896 publickey->next = next;
899 storebuf.size = 8192;
900 storebuf.buffer = malloc(8192);
902 write_openpgp_stream(buffer_putchar, &storebuf, packets);
905 * Now we have the key data store it in the DB; the keyid is
908 memset(&key, 0, sizeof(key));
909 memset(&data, 0, sizeof(data));
911 key.size = sizeof(keyid);
912 data.size = storebuf.offset;
913 data.data = storebuf.buffer;
915 ret = keydb(privctx, keyid)->put(keydb(privctx, keyid),
921 logthing(LOGTHING_ERROR,
922 "Problem storing key: %s",
924 if (ret == DB_LOCK_DEADLOCK) {
929 free(storebuf.buffer);
930 storebuf.buffer = NULL;
934 free_packet_list(packets);
939 * Walk through our uids storing the words into the db with the keyid.
942 uids = keyuids(publickey, &primary);
945 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
946 wordlist = makewordlist(wordlist, uids[i]);
949 for (curword = wordlist; curword != NULL && !deadlock;
950 curword = curword->next) {
951 memset(&key, 0, sizeof(key));
952 memset(&data, 0, sizeof(data));
953 key.data = curword->object;
954 key.size = strlen(key.data);
955 data.data = worddb_data;
956 data.size = sizeof(worddb_data);
959 * Our data is the key creation time followed by the
962 worddb_data[ 0] = publickey->publickey->data[1];
963 worddb_data[ 1] = publickey->publickey->data[2];
964 worddb_data[ 2] = publickey->publickey->data[3];
965 worddb_data[ 3] = publickey->publickey->data[4];
966 worddb_data[ 4] = (keyid >> 56) & 0xFF;
967 worddb_data[ 5] = (keyid >> 48) & 0xFF;
968 worddb_data[ 6] = (keyid >> 40) & 0xFF;
969 worddb_data[ 7] = (keyid >> 32) & 0xFF;
970 worddb_data[ 8] = (keyid >> 24) & 0xFF;
971 worddb_data[ 9] = (keyid >> 16) & 0xFF;
972 worddb_data[10] = (keyid >> 8) & 0xFF;
973 worddb_data[11] = keyid & 0xFF;
974 ret = privctx->worddb->put(privctx->worddb,
980 logthing(LOGTHING_ERROR,
981 "Problem storing word: %s",
983 if (ret == DB_LOCK_DEADLOCK) {
990 * Free our UID and word lists.
992 llfree(wordlist, NULL);
993 for (i = 0; uids[i] != NULL; i++) {
1002 * Write the truncated 32 bit keyid so we can lookup the full id for
1006 shortkeyid = keyid & 0xFFFFFFFF;
1008 memset(&key, 0, sizeof(key));
1009 memset(&data, 0, sizeof(data));
1010 key.data = &shortkeyid;
1011 key.size = sizeof(shortkeyid);
1013 data.size = sizeof(keyid);
1015 ret = privctx->id32db->put(privctx->id32db,
1021 logthing(LOGTHING_ERROR,
1022 "Problem storing short keyid: %s",
1024 if (ret == DB_LOCK_DEADLOCK) {
1031 subkeyids = keysubkeys(publickey);
1033 while (subkeyids != NULL && subkeyids[i] != 0) {
1034 /* Store the subkey ID -> main key ID mapping */
1035 memset(&key, 0, sizeof(key));
1036 memset(&data, 0, sizeof(data));
1037 key.data = &subkeyids[i];
1038 key.size = sizeof(subkeyids[i]);
1040 data.size = sizeof(keyid);
1042 ret = privctx->subkeydb->put(privctx->subkeydb,
1048 logthing(LOGTHING_ERROR,
1049 "Problem storing subkey keyid: %s",
1051 if (ret == DB_LOCK_DEADLOCK) {
1056 /* Store the short subkey ID -> main key ID mapping */
1057 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
1059 memset(&key, 0, sizeof(key));
1060 memset(&data, 0, sizeof(data));
1061 key.data = &shortkeyid;
1062 key.size = sizeof(shortkeyid);
1064 data.size = sizeof(keyid);
1066 ret = privctx->id32db->put(privctx->id32db,
1072 logthing(LOGTHING_ERROR,
1073 "Problem storing short keyid: %s",
1075 if (ret == DB_LOCK_DEADLOCK) {
1080 if (subkeyids != NULL) {
1087 get_skshash(publickey, &hash);
1088 memset(&key, 0, sizeof(key));
1089 memset(&data, 0, sizeof(data));
1090 key.data = hash.hash;
1091 key.size = sizeof(hash.hash);
1093 data.size = sizeof(keyid);
1095 ret = privctx->skshashdb->put(privctx->skshashdb,
1101 logthing(LOGTHING_ERROR,
1102 "Problem storing SKS hash: %s",
1104 if (ret == DB_LOCK_DEADLOCK) {
1111 db4_endtrans(dbctx);
1114 return deadlock ? -1 : 0 ;
1118 * iterate_keys - call a function once for each key in the db.
1119 * @iterfunc: The function to call.
1120 * @ctx: A context pointer
1122 * Calls iterfunc once for each key in the database. ctx is passed
1123 * unaltered to iterfunc. This function is intended to aid database dumps
1124 * and statistic calculations.
1126 * Returns the number of keys we iterated over.
1128 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1129 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1132 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1138 struct buffer_ctx fetchbuf;
1139 struct openpgp_packet_list *packets = NULL;
1140 struct openpgp_publickey *key = NULL;
1142 for (i = 0; i < privctx->numdbs; i++) {
1143 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1152 memset(&dbkey, 0, sizeof(dbkey));
1153 memset(&data, 0, sizeof(data));
1154 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1156 fetchbuf.buffer = data.data;
1157 fetchbuf.offset = 0;
1158 fetchbuf.size = data.size;
1159 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1161 parse_keys(packets, &key);
1165 free_publickey(key);
1167 free_packet_list(packets);
1170 memset(&dbkey, 0, sizeof(dbkey));
1171 memset(&data, 0, sizeof(data));
1172 ret = cursor->c_get(cursor, &dbkey, &data,
1176 if (ret != DB_NOTFOUND) {
1177 logthing(LOGTHING_ERROR,
1178 "Problem reading key: %s",
1182 cursor->c_close(cursor);
1190 * Include the basic keydb routines.
1192 #define NEED_GETKEYSIGS 1
1193 #define NEED_KEYID2UID 1
1194 #define NEED_UPDATEKEYS 1
1195 #define NEED_GET_FP 1
1199 * cleanupdb - De-initialize the key database.
1201 * This function should be called upon program exit to allow the DB to
1202 * cleanup after itself.
1204 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1206 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1209 if (privctx->dbenv != NULL) {
1210 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1211 if (privctx->subkeydb != NULL) {
1212 privctx->subkeydb->close(privctx->subkeydb, 0);
1213 privctx->subkeydb = NULL;
1215 if (privctx->skshashdb != NULL) {
1216 privctx->skshashdb->close(privctx->skshashdb, 0);
1217 privctx->skshashdb = NULL;
1219 if (privctx->id32db != NULL) {
1220 privctx->id32db->close(privctx->id32db, 0);
1221 privctx->id32db = NULL;
1223 if (privctx->worddb != NULL) {
1224 privctx->worddb->close(privctx->worddb, 0);
1225 privctx->worddb = NULL;
1227 for (i = 0; i < privctx->numdbs; i++) {
1228 if (privctx->dbconns[i] != NULL) {
1229 privctx->dbconns[i]->close(privctx->dbconns[i],
1231 privctx->dbconns[i] = NULL;
1234 free(privctx->dbconns);
1235 privctx->dbconns = NULL;
1236 privctx->dbenv->close(privctx->dbenv, 0);
1237 privctx->dbenv = NULL;
1246 * initdb - Initialize the key database.
1248 * This function should be called before any of the other functions in
1249 * this file are called in order to allow the DB to be initialized ready
1252 struct onak_dbctx *keydb_db4_init(bool readonly)
1259 struct stat statbuf;
1261 struct onak_dbctx *dbctx;
1262 struct onak_db4_dbctx *privctx;
1264 dbctx = malloc(sizeof(*dbctx));
1265 if (dbctx == NULL) {
1268 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1269 if (privctx == NULL) {
1274 /* Default to 16 key data DBs */
1275 privctx->numdbs = 16;
1277 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
1279 ret = stat(buf, &statbuf);
1280 while ((ret == 0) || (errno != ENOENT)) {
1282 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1283 "lock file: %s (%d)", strerror(errno), ret);
1286 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1288 ret = stat(buf, &statbuf);
1292 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
1293 numdb = fopen(buf, "r");
1294 if (numdb != NULL) {
1295 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1296 privctx->numdbs = atoi(buf);
1299 } else if (!readonly) {
1300 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1302 numdb = fopen(buf, "w");
1303 if (numdb != NULL) {
1304 fprintf(numdb, "%d", privctx->numdbs);
1307 logthing(LOGTHING_ERROR,
1308 "Couldn't write num_keydb: %s",
1313 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1314 if (privctx->dbconns == NULL) {
1315 logthing(LOGTHING_CRITICAL,
1316 "Couldn't allocate memory for dbconns");
1321 ret = db_env_create(&privctx->dbenv, 0);
1323 logthing(LOGTHING_CRITICAL,
1324 "db_env_create: %s", db_strerror(ret));
1329 * Up the number of locks we're allowed at once. We base this on
1330 * the maximum number of keys we're going to return.
1332 maxlocks = config.maxkeys * 16;
1333 if (maxlocks < 1000) {
1336 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1337 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1340 * Enable deadlock detection so that we don't block indefinitely on
1341 * anything. What we really want is simple 2 state locks, but I'm not
1342 * sure how to make the standard DB functions do that yet.
1345 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1346 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1348 logthing(LOGTHING_CRITICAL,
1349 "db_env_create: %s", db_strerror(ret));
1354 ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
1355 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1359 #ifdef DB_VERSION_MISMATCH
1360 if (ret == DB_VERSION_MISMATCH) {
1361 privctx->dbenv->close(privctx->dbenv, 0);
1362 privctx->dbenv = NULL;
1363 ret = db4_upgradedb(privctx);
1365 ret = db_env_create(&privctx->dbenv, 0);
1368 privctx->dbenv->set_errcall(privctx->dbenv,
1370 privctx->dbenv->set_lk_detect(privctx->dbenv,
1372 ret = privctx->dbenv->open(privctx->dbenv,
1374 DB_INIT_LOG | DB_INIT_MPOOL |
1375 DB_INIT_LOCK | DB_INIT_TXN |
1376 DB_CREATE | DB_RECOVER,
1380 privctx->dbenv->txn_checkpoint(
1390 logthing(LOGTHING_CRITICAL,
1391 "Error opening db environment: %s (%s)",
1394 if (privctx->dbenv != NULL) {
1395 privctx->dbenv->close(privctx->dbenv, 0);
1396 privctx->dbenv = NULL;
1402 db4_starttrans(dbctx);
1404 for (i = 0; !ret && i < privctx->numdbs; i++) {
1405 ret = db_create(&privctx->dbconns[i],
1408 logthing(LOGTHING_CRITICAL,
1409 "db_create: %s", db_strerror(ret));
1413 snprintf(buf, 1023, "keydb.%d.db", i);
1418 ret = privctx->dbconns[i]->open(
1419 privctx->dbconns[i],
1427 logthing(LOGTHING_CRITICAL,
1428 "Error opening key database:"
1438 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1440 logthing(LOGTHING_CRITICAL, "db_create: %s",
1446 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1450 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1451 "worddb", "worddb", DB_BTREE,
1455 logthing(LOGTHING_CRITICAL,
1456 "Error opening word database: %s (%s)",
1463 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1465 logthing(LOGTHING_CRITICAL, "db_create: %s",
1471 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1475 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1476 "id32db", "id32db", DB_HASH,
1480 logthing(LOGTHING_CRITICAL,
1481 "Error opening id32 database: %s (%s)",
1488 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1490 logthing(LOGTHING_CRITICAL, "db_create: %s",
1496 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1498 "skshashdb", DB_HASH,
1502 logthing(LOGTHING_CRITICAL,
1503 "Error opening skshash database: %s (%s)",
1510 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1512 logthing(LOGTHING_CRITICAL, "db_create: %s",
1518 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1519 "subkeydb", "subkeydb",
1524 logthing(LOGTHING_CRITICAL,
1525 "Error opening subkey database: %s (%s)",
1531 if (privctx->txn != NULL) {
1532 db4_endtrans(dbctx);
1536 db4_cleanupdb(dbctx);
1537 logthing(LOGTHING_CRITICAL,
1538 "Error opening database; exiting");
1542 dbctx->cleanupdb = db4_cleanupdb;
1543 dbctx->starttrans = db4_starttrans;
1544 dbctx->endtrans = db4_endtrans;
1545 dbctx->fetch_key_id = db4_fetch_key_id;
1546 dbctx->fetch_key_fp = generic_fetch_key_fp;
1547 dbctx->fetch_key_text = db4_fetch_key_text;
1548 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1549 dbctx->store_key = db4_store_key;
1550 dbctx->update_keys = generic_update_keys;
1551 dbctx->delete_key = db4_delete_key;
1552 dbctx->getkeysigs = generic_getkeysigs;
1553 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1554 dbctx->keyid2uid = generic_keyid2uid;
1555 dbctx->getfullkeyid = db4_getfullkeyid;
1556 dbctx->iterate_keys = db4_iterate_keys;