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 struct openpgp_fingerprint *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;
603 db4_starttrans(dbctx);
606 db4_fetch_key_id(dbctx, keyid, &publickey, true);
609 * Walk through the uids removing the words from the worddb.
611 if (publickey != NULL) {
612 uids = keyuids(publickey, &primary);
615 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
616 wordlist = makewordlist(wordlist, uids[i]);
619 privctx->worddb->cursor(privctx->worddb,
624 for (curword = wordlist; curword != NULL && !deadlock;
625 curword = curword->next) {
626 memset(&key, 0, sizeof(key));
627 memset(&data, 0, sizeof(data));
628 key.data = curword->object;
629 key.size = strlen(key.data);
630 data.data = worddb_data;
631 data.size = sizeof(worddb_data);
634 * Our data is the key creation time followed by the
637 worddb_data[ 0] = publickey->publickey->data[1];
638 worddb_data[ 1] = publickey->publickey->data[2];
639 worddb_data[ 2] = publickey->publickey->data[3];
640 worddb_data[ 3] = publickey->publickey->data[4];
641 worddb_data[ 4] = (keyid >> 56) & 0xFF;
642 worddb_data[ 5] = (keyid >> 48) & 0xFF;
643 worddb_data[ 6] = (keyid >> 40) & 0xFF;
644 worddb_data[ 7] = (keyid >> 32) & 0xFF;
645 worddb_data[ 8] = (keyid >> 24) & 0xFF;
646 worddb_data[ 9] = (keyid >> 16) & 0xFF;
647 worddb_data[10] = (keyid >> 8) & 0xFF;
648 worddb_data[11] = keyid & 0xFF;
650 ret = cursor->c_get(cursor,
656 ret = cursor->c_del(cursor, 0);
660 logthing(LOGTHING_ERROR,
661 "Problem deleting word: %s "
662 "(0x%016" PRIX64 ")",
665 if (ret == DB_LOCK_DEADLOCK) {
670 cursor->c_close(cursor);
673 ret = privctx->skshashdb->cursor(privctx->skshashdb,
678 get_skshash(publickey, &hash);
680 memset(&key, 0, sizeof(key));
681 memset(&data, 0, sizeof(data));
682 key.data = hash.hash;
683 key.size = sizeof(hash.hash);
685 data.size = sizeof(keyid);
687 ret = cursor->c_get(cursor,
693 ret = cursor->c_del(cursor, 0);
697 logthing(LOGTHING_ERROR,
698 "Problem deleting skshash: %s "
699 "(0x%016" PRIX64 ")",
702 if (ret == DB_LOCK_DEADLOCK) {
707 cursor->c_close(cursor);
712 * Free our UID and word lists.
714 llfree(wordlist, NULL);
715 for (i = 0; uids[i] != NULL; i++) {
721 free_publickey(publickey);
726 privctx->id32db->cursor(privctx->id32db,
731 shortkeyid = keyid & 0xFFFFFFFF;
733 memset(&key, 0, sizeof(key));
734 memset(&data, 0, sizeof(data));
735 key.data = &shortkeyid;
736 key.size = sizeof(shortkeyid);
738 data.size = sizeof(keyid);
740 ret = cursor->c_get(cursor,
746 ret = cursor->c_del(cursor, 0);
750 logthing(LOGTHING_ERROR,
751 "Problem deleting short keyid: %s "
752 "(0x%016" PRIX64 ")",
755 if (ret == DB_LOCK_DEADLOCK) {
760 subkeyids = keysubkeys(publickey);
762 while (subkeyids != NULL && subkeyids[i].length != 0) {
763 subkeyid = fingerprint2keyid(&subkeyids[i]);
764 memset(&key, 0, sizeof(key));
765 key.data = &subkeyid;
766 key.size = sizeof(subkeyid);
767 privctx->subkeydb->del(privctx->subkeydb,
768 privctx->txn, &key, 0);
770 logthing(LOGTHING_ERROR,
771 "Problem deleting subkey id: %s "
772 "(0x%016" PRIX64 ")",
775 if (ret == DB_LOCK_DEADLOCK) {
780 shortkeyid = subkeyid & 0xFFFFFFFF;
782 memset(&key, 0, sizeof(key));
783 memset(&data, 0, sizeof(data));
784 key.data = &shortkeyid;
785 key.size = sizeof(shortkeyid);
787 data.size = sizeof(keyid);
789 ret = cursor->c_get(cursor,
795 ret = cursor->c_del(cursor, 0);
799 logthing(LOGTHING_ERROR,
800 "Problem deleting short keyid: %s "
801 "(0x%016" PRIX64 ")",
804 if (ret == DB_LOCK_DEADLOCK) {
810 if (subkeyids != NULL) {
814 cursor->c_close(cursor);
820 key.size = sizeof(keyid);
822 keydb(privctx, keyid)->del(keydb(privctx, keyid),
832 return deadlock ? (-1) : (ret == DB_NOTFOUND);
836 * store_key - Takes a key and stores it.
837 * @publickey: A pointer to the public key to store.
838 * @intrans: If we're already in a transaction.
839 * @update: If true the key exists and should be updated.
841 * Again we just use the hex representation of the keyid as the filename
842 * to store the key to. We flatten the public key to a list of OpenPGP
843 * packets and then use write_openpgp_stream() to write the stream out to
844 * the file. If update is true then we delete the old key first, otherwise
845 * we trust that it doesn't exist.
847 static int db4_store_key(struct onak_dbctx *dbctx,
848 struct openpgp_publickey *publickey, bool intrans,
851 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
852 struct openpgp_packet_list *packets = NULL;
853 struct openpgp_packet_list *list_end = NULL;
854 struct openpgp_publickey *next = NULL;
857 struct buffer_ctx storebuf;
861 uint32_t shortkeyid = 0;
862 struct openpgp_fingerprint *subkeyids = NULL;
864 char *primary = NULL;
865 unsigned char worddb_data[12];
866 struct ll *wordlist = NULL;
867 struct ll *curword = NULL;
868 bool deadlock = false;
872 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
873 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
878 db4_starttrans(dbctx);
882 * Delete the key if we already have it.
884 * TODO: Can we optimize this perhaps? Possibly when other data is
885 * involved as well? I suspect this is easiest and doesn't make a lot
886 * of difference though - the largest chunk of data is the keydata and
887 * it definitely needs updated.
890 deadlock = (db4_delete_key(dbctx, keyid, true) == -1);
894 * Convert the key to a flat set of binary data.
897 next = publickey->next;
898 publickey->next = NULL;
899 flatten_publickey(publickey, &packets, &list_end);
900 publickey->next = next;
903 storebuf.size = 8192;
904 storebuf.buffer = malloc(8192);
906 write_openpgp_stream(buffer_putchar, &storebuf, packets);
909 * Now we have the key data store it in the DB; the keyid is
912 memset(&key, 0, sizeof(key));
913 memset(&data, 0, sizeof(data));
915 key.size = sizeof(keyid);
916 data.size = storebuf.offset;
917 data.data = storebuf.buffer;
919 ret = keydb(privctx, keyid)->put(keydb(privctx, keyid),
925 logthing(LOGTHING_ERROR,
926 "Problem storing key: %s",
928 if (ret == DB_LOCK_DEADLOCK) {
933 free(storebuf.buffer);
934 storebuf.buffer = NULL;
938 free_packet_list(packets);
943 * Walk through our uids storing the words into the db with the keyid.
946 uids = keyuids(publickey, &primary);
949 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
950 wordlist = makewordlist(wordlist, uids[i]);
953 for (curword = wordlist; curword != NULL && !deadlock;
954 curword = curword->next) {
955 memset(&key, 0, sizeof(key));
956 memset(&data, 0, sizeof(data));
957 key.data = curword->object;
958 key.size = strlen(key.data);
959 data.data = worddb_data;
960 data.size = sizeof(worddb_data);
963 * Our data is the key creation time followed by the
966 worddb_data[ 0] = publickey->publickey->data[1];
967 worddb_data[ 1] = publickey->publickey->data[2];
968 worddb_data[ 2] = publickey->publickey->data[3];
969 worddb_data[ 3] = publickey->publickey->data[4];
970 worddb_data[ 4] = (keyid >> 56) & 0xFF;
971 worddb_data[ 5] = (keyid >> 48) & 0xFF;
972 worddb_data[ 6] = (keyid >> 40) & 0xFF;
973 worddb_data[ 7] = (keyid >> 32) & 0xFF;
974 worddb_data[ 8] = (keyid >> 24) & 0xFF;
975 worddb_data[ 9] = (keyid >> 16) & 0xFF;
976 worddb_data[10] = (keyid >> 8) & 0xFF;
977 worddb_data[11] = keyid & 0xFF;
978 ret = privctx->worddb->put(privctx->worddb,
984 logthing(LOGTHING_ERROR,
985 "Problem storing word: %s",
987 if (ret == DB_LOCK_DEADLOCK) {
994 * Free our UID and word lists.
996 llfree(wordlist, NULL);
997 for (i = 0; uids[i] != NULL; i++) {
1006 * Write the truncated 32 bit keyid so we can lookup the full id for
1010 shortkeyid = keyid & 0xFFFFFFFF;
1012 memset(&key, 0, sizeof(key));
1013 memset(&data, 0, sizeof(data));
1014 key.data = &shortkeyid;
1015 key.size = sizeof(shortkeyid);
1017 data.size = sizeof(keyid);
1019 ret = privctx->id32db->put(privctx->id32db,
1025 logthing(LOGTHING_ERROR,
1026 "Problem storing short keyid: %s",
1028 if (ret == DB_LOCK_DEADLOCK) {
1035 subkeyids = keysubkeys(publickey);
1037 while (subkeyids != NULL && subkeyids[i].length != 0) {
1038 subkeyid = fingerprint2keyid(&subkeyids[i]);
1039 /* Store the subkey ID -> main key ID mapping */
1040 memset(&key, 0, sizeof(key));
1041 memset(&data, 0, sizeof(data));
1042 key.data = &subkeyid;
1043 key.size = sizeof(subkeyid);
1045 data.size = sizeof(keyid);
1047 ret = privctx->subkeydb->put(privctx->subkeydb,
1053 logthing(LOGTHING_ERROR,
1054 "Problem storing subkey keyid: %s",
1056 if (ret == DB_LOCK_DEADLOCK) {
1061 /* Store the short subkey ID -> main key ID mapping */
1062 shortkeyid = subkeyid & 0xFFFFFFFF;
1064 memset(&key, 0, sizeof(key));
1065 memset(&data, 0, sizeof(data));
1066 key.data = &shortkeyid;
1067 key.size = sizeof(shortkeyid);
1069 data.size = sizeof(keyid);
1071 ret = privctx->id32db->put(privctx->id32db,
1077 logthing(LOGTHING_ERROR,
1078 "Problem storing short keyid: %s",
1080 if (ret == DB_LOCK_DEADLOCK) {
1086 if (subkeyids != NULL) {
1093 get_skshash(publickey, &hash);
1094 memset(&key, 0, sizeof(key));
1095 memset(&data, 0, sizeof(data));
1096 key.data = hash.hash;
1097 key.size = sizeof(hash.hash);
1099 data.size = sizeof(keyid);
1101 ret = privctx->skshashdb->put(privctx->skshashdb,
1107 logthing(LOGTHING_ERROR,
1108 "Problem storing SKS hash: %s",
1110 if (ret == DB_LOCK_DEADLOCK) {
1117 db4_endtrans(dbctx);
1120 return deadlock ? -1 : 0 ;
1124 * iterate_keys - call a function once for each key in the db.
1125 * @iterfunc: The function to call.
1126 * @ctx: A context pointer
1128 * Calls iterfunc once for each key in the database. ctx is passed
1129 * unaltered to iterfunc. This function is intended to aid database dumps
1130 * and statistic calculations.
1132 * Returns the number of keys we iterated over.
1134 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1135 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1138 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1144 struct buffer_ctx fetchbuf;
1145 struct openpgp_packet_list *packets = NULL;
1146 struct openpgp_publickey *key = NULL;
1148 for (i = 0; i < privctx->numdbs; i++) {
1149 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1158 memset(&dbkey, 0, sizeof(dbkey));
1159 memset(&data, 0, sizeof(data));
1160 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1162 fetchbuf.buffer = data.data;
1163 fetchbuf.offset = 0;
1164 fetchbuf.size = data.size;
1165 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1167 parse_keys(packets, &key);
1171 free_publickey(key);
1173 free_packet_list(packets);
1176 memset(&dbkey, 0, sizeof(dbkey));
1177 memset(&data, 0, sizeof(data));
1178 ret = cursor->c_get(cursor, &dbkey, &data,
1182 if (ret != DB_NOTFOUND) {
1183 logthing(LOGTHING_ERROR,
1184 "Problem reading key: %s",
1188 cursor->c_close(cursor);
1196 * Include the basic keydb routines.
1198 #define NEED_GETKEYSIGS 1
1199 #define NEED_KEYID2UID 1
1200 #define NEED_UPDATEKEYS 1
1201 #define NEED_GET_FP 1
1205 * cleanupdb - De-initialize the key database.
1207 * This function should be called upon program exit to allow the DB to
1208 * cleanup after itself.
1210 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1212 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1215 if (privctx->dbenv != NULL) {
1216 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1217 if (privctx->subkeydb != NULL) {
1218 privctx->subkeydb->close(privctx->subkeydb, 0);
1219 privctx->subkeydb = NULL;
1221 if (privctx->skshashdb != NULL) {
1222 privctx->skshashdb->close(privctx->skshashdb, 0);
1223 privctx->skshashdb = NULL;
1225 if (privctx->id32db != NULL) {
1226 privctx->id32db->close(privctx->id32db, 0);
1227 privctx->id32db = NULL;
1229 if (privctx->worddb != NULL) {
1230 privctx->worddb->close(privctx->worddb, 0);
1231 privctx->worddb = NULL;
1233 for (i = 0; i < privctx->numdbs; i++) {
1234 if (privctx->dbconns[i] != NULL) {
1235 privctx->dbconns[i]->close(privctx->dbconns[i],
1237 privctx->dbconns[i] = NULL;
1240 free(privctx->dbconns);
1241 privctx->dbconns = NULL;
1242 privctx->dbenv->close(privctx->dbenv, 0);
1243 privctx->dbenv = NULL;
1252 * initdb - Initialize the key database.
1254 * This function should be called before any of the other functions in
1255 * this file are called in order to allow the DB to be initialized ready
1258 struct onak_dbctx *keydb_db4_init(bool readonly)
1265 struct stat statbuf;
1267 struct onak_dbctx *dbctx;
1268 struct onak_db4_dbctx *privctx;
1270 dbctx = malloc(sizeof(*dbctx));
1271 if (dbctx == NULL) {
1274 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1275 if (privctx == NULL) {
1280 /* Default to 16 key data DBs */
1281 privctx->numdbs = 16;
1283 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
1285 ret = stat(buf, &statbuf);
1286 while ((ret == 0) || (errno != ENOENT)) {
1288 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1289 "lock file: %s (%d)", strerror(errno), ret);
1292 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1294 ret = stat(buf, &statbuf);
1298 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
1299 numdb = fopen(buf, "r");
1300 if (numdb != NULL) {
1301 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1302 privctx->numdbs = atoi(buf);
1305 } else if (!readonly) {
1306 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1308 numdb = fopen(buf, "w");
1309 if (numdb != NULL) {
1310 fprintf(numdb, "%d", privctx->numdbs);
1313 logthing(LOGTHING_ERROR,
1314 "Couldn't write num_keydb: %s",
1319 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1320 if (privctx->dbconns == NULL) {
1321 logthing(LOGTHING_CRITICAL,
1322 "Couldn't allocate memory for dbconns");
1327 ret = db_env_create(&privctx->dbenv, 0);
1329 logthing(LOGTHING_CRITICAL,
1330 "db_env_create: %s", db_strerror(ret));
1335 * Up the number of locks we're allowed at once. We base this on
1336 * the maximum number of keys we're going to return.
1338 maxlocks = config.maxkeys * 16;
1339 if (maxlocks < 1000) {
1342 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1343 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1346 * Enable deadlock detection so that we don't block indefinitely on
1347 * anything. What we really want is simple 2 state locks, but I'm not
1348 * sure how to make the standard DB functions do that yet.
1351 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1352 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1354 logthing(LOGTHING_CRITICAL,
1355 "db_env_create: %s", db_strerror(ret));
1360 ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
1361 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1365 #ifdef DB_VERSION_MISMATCH
1366 if (ret == DB_VERSION_MISMATCH) {
1367 privctx->dbenv->close(privctx->dbenv, 0);
1368 privctx->dbenv = NULL;
1369 ret = db4_upgradedb(privctx);
1371 ret = db_env_create(&privctx->dbenv, 0);
1374 privctx->dbenv->set_errcall(privctx->dbenv,
1376 privctx->dbenv->set_lk_detect(privctx->dbenv,
1378 ret = privctx->dbenv->open(privctx->dbenv,
1380 DB_INIT_LOG | DB_INIT_MPOOL |
1381 DB_INIT_LOCK | DB_INIT_TXN |
1382 DB_CREATE | DB_RECOVER,
1386 privctx->dbenv->txn_checkpoint(
1396 logthing(LOGTHING_CRITICAL,
1397 "Error opening db environment: %s (%s)",
1400 if (privctx->dbenv != NULL) {
1401 privctx->dbenv->close(privctx->dbenv, 0);
1402 privctx->dbenv = NULL;
1408 db4_starttrans(dbctx);
1410 for (i = 0; !ret && i < privctx->numdbs; i++) {
1411 ret = db_create(&privctx->dbconns[i],
1414 logthing(LOGTHING_CRITICAL,
1415 "db_create: %s", db_strerror(ret));
1419 snprintf(buf, 1023, "keydb.%d.db", i);
1424 ret = privctx->dbconns[i]->open(
1425 privctx->dbconns[i],
1433 logthing(LOGTHING_CRITICAL,
1434 "Error opening key database:"
1444 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1446 logthing(LOGTHING_CRITICAL, "db_create: %s",
1452 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1456 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1457 "worddb", "worddb", DB_BTREE,
1461 logthing(LOGTHING_CRITICAL,
1462 "Error opening word database: %s (%s)",
1469 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1471 logthing(LOGTHING_CRITICAL, "db_create: %s",
1477 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1481 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1482 "id32db", "id32db", DB_HASH,
1486 logthing(LOGTHING_CRITICAL,
1487 "Error opening id32 database: %s (%s)",
1494 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1496 logthing(LOGTHING_CRITICAL, "db_create: %s",
1502 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1504 "skshashdb", DB_HASH,
1508 logthing(LOGTHING_CRITICAL,
1509 "Error opening skshash database: %s (%s)",
1516 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1518 logthing(LOGTHING_CRITICAL, "db_create: %s",
1524 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1525 "subkeydb", "subkeydb",
1530 logthing(LOGTHING_CRITICAL,
1531 "Error opening subkey database: %s (%s)",
1537 if (privctx->txn != NULL) {
1538 db4_endtrans(dbctx);
1542 db4_cleanupdb(dbctx);
1543 logthing(LOGTHING_CRITICAL,
1544 "Error opening database; exiting");
1548 dbctx->cleanupdb = db4_cleanupdb;
1549 dbctx->starttrans = db4_starttrans;
1550 dbctx->endtrans = db4_endtrans;
1551 dbctx->fetch_key_id = db4_fetch_key_id;
1552 dbctx->fetch_key_fp = generic_fetch_key_fp;
1553 dbctx->fetch_key_text = db4_fetch_key_text;
1554 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1555 dbctx->store_key = db4_store_key;
1556 dbctx->update_keys = generic_update_keys;
1557 dbctx->delete_key = db4_delete_key;
1558 dbctx->getkeysigs = generic_getkeysigs;
1559 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1560 dbctx->keyid2uid = generic_keyid2uid;
1561 dbctx->getfullkeyid = db4_getfullkeyid;
1562 dbctx->iterate_keys = db4_iterate_keys;