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);
179 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
180 privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0);
181 privctx->dbenv = NULL;
182 for (i = 0; i < privctx->numdbs; i++) {
183 ret = db_create(&curdb, NULL, 0);
185 snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
187 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
188 ret = curdb->upgrade(curdb, buf, 0);
189 curdb->close(curdb, 0);
191 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
197 ret = db_create(&curdb, NULL, 0);
199 snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
200 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
201 ret = curdb->upgrade(curdb, buf, 0);
202 curdb->close(curdb, 0);
204 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
209 ret = db_create(&curdb, NULL, 0);
211 snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
212 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
213 ret = curdb->upgrade(curdb, buf, 0);
214 curdb->close(curdb, 0);
216 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
221 ret = db_create(&curdb, NULL, 0);
223 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
224 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
225 ret = curdb->upgrade(curdb, buf, 0);
226 curdb->close(curdb, 0);
228 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
233 ret = db_create(&curdb, NULL, 0);
235 snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
236 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
237 ret = curdb->upgrade(curdb, buf, 0);
238 curdb->close(curdb, 0);
240 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
245 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
253 * getfullkeyid - Maps a 32bit key id to a 64bit one.
254 * @keyid: The 32bit keyid.
256 * This function maps a 32bit key id to the full 64bit one. It returns the
257 * full keyid. If the key isn't found a keyid of 0 is returned.
259 static uint64_t db4_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
261 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
264 uint32_t shortkeyid = 0;
267 if (keyid < 0x100000000LL) {
268 ret = privctx->id32db->cursor(privctx->id32db,
273 shortkeyid = keyid & 0xFFFFFFFF;
275 memset(&key, 0, sizeof(key));
276 memset(&data, 0, sizeof(data));
277 key.data = &shortkeyid;
278 key.size = sizeof(shortkeyid);
279 data.flags = DB_DBT_MALLOC;
281 ret = cursor->c_get(cursor,
287 keyid = *(uint64_t *) data.data;
289 if (data.data != NULL) {
295 ret = cursor->c_close(cursor);
303 * fetch_key_id - Given a keyid fetch the key from storage.
304 * @keyid: The keyid to fetch.
305 * @publickey: A pointer to a structure to return the key in.
306 * @intrans: If we're already in a transaction.
308 * We use the hex representation of the keyid as the filename to fetch the
309 * key from. The key is stored in the file as a binary OpenPGP stream of
310 * packets, so we can just use read_openpgp_stream() to read the packets
311 * in and then parse_keys() to parse the packets into a publickey
314 static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
315 struct openpgp_publickey **publickey,
318 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
319 struct openpgp_packet_list *packets = NULL;
323 struct buffer_ctx fetchbuf;
325 if (keyid < 0x100000000LL) {
326 keyid = db4_getfullkeyid(dbctx, keyid);
329 memset(&key, 0, sizeof(key));
330 memset(&data, 0, sizeof(data));
335 key.size = sizeof(keyid);
339 db4_starttrans(dbctx);
342 ret = keydb(privctx, keyid)->get(keydb(privctx, keyid),
348 if (ret == DB_NOTFOUND) {
349 /* If we didn't find the key ID see if it's a subkey ID */
350 memset(&key, 0, sizeof(key));
351 memset(&data, 0, sizeof(data));
354 key.size = sizeof(keyid);
357 ret = privctx->subkeydb->get(privctx->subkeydb,
364 /* We got a subkey match; retrieve the actual key */
365 keyid = *(uint64_t *) data.data;
367 memset(&key, 0, sizeof(key));
368 memset(&data, 0, sizeof(data));
371 key.size = sizeof(keyid);
374 ret = keydb(privctx, keyid)->get(keydb(privctx, keyid),
383 fetchbuf.buffer = data.data;
385 fetchbuf.size = data.size;
386 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
388 parse_keys(packets, publickey);
389 free_packet_list(packets);
392 } else if (ret != DB_NOTFOUND) {
393 logthing(LOGTHING_ERROR,
394 "Problem retrieving key: %s",
405 int worddb_cmp(const void *d1, const void *d2)
407 return memcmp(d1, d2, 12);
411 * fetch_key_text - Trys to find the keys that contain the supplied text.
412 * @search: The text to search for.
413 * @publickey: A pointer to a structure to return the key in.
415 * This function searches for the supplied text and returns the keys that
418 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
419 struct openpgp_publickey **publickey)
421 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
428 char *searchtext = NULL;
429 struct ll *wordlist = NULL;
430 struct ll *curword = NULL;
431 struct keyarray keylist = { NULL, 0, 0 };
432 struct keyarray newkeylist = { NULL, 0, 0 };
436 searchtext = strdup(search);
437 wordlist = makewordlist(wordlist, searchtext);
439 for (curword = wordlist; curword != NULL; curword = curword->next) {
440 db4_starttrans(dbctx);
442 ret = privctx->worddb->cursor(privctx->worddb,
447 memset(&key, 0, sizeof(key));
448 memset(&data, 0, sizeof(data));
449 key.data = curword->object;
450 key.size = strlen(curword->object);
451 data.flags = DB_DBT_MALLOC;
452 ret = cursor->c_get(cursor,
456 while (ret == 0 && strncmp(key.data, curword->object,
458 ((char *) curword->object)[key.size] == 0) {
460 for (i = 4; i < 12; i++) {
462 keyid += ((unsigned char *)
467 * Only add the keys containing this word if this is
468 * our first pass (ie we have no existing key list),
469 * or the key contained a previous word.
471 if (firstpass || array_find(&keylist, keyid)) {
472 array_add(&newkeylist, keyid);
478 ret = cursor->c_get(cursor,
483 array_free(&keylist);
484 keylist = newkeylist;
485 newkeylist.keys = NULL;
486 newkeylist.count = newkeylist.size = 0;
487 if (data.data != NULL) {
491 ret = cursor->c_close(cursor);
496 llfree(wordlist, NULL);
499 if (keylist.count > config.maxkeys) {
500 keylist.count = config.maxkeys;
503 db4_starttrans(dbctx);
504 for (i = 0; i < keylist.count; i++) {
505 numkeys += db4_fetch_key_id(dbctx, keylist.keys[i],
509 array_free(&keylist);
518 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
519 const struct skshash *hash,
520 struct openpgp_publickey **publickey)
522 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
528 ret = privctx->skshashdb->cursor(privctx->skshashdb,
533 memset(&key, 0, sizeof(key));
534 memset(&data, 0, sizeof(data));
535 key.data = (void *) hash->hash;
536 key.size = sizeof(hash->hash);
537 data.flags = DB_DBT_MALLOC;
539 ret = cursor->c_get(cursor,
545 keyid = *(uint64_t *) data.data;
547 if (data.data != NULL) {
553 ret = cursor->c_close(cursor);
556 return db4_fetch_key_id(dbctx, keyid, publickey, false);
560 * delete_key - Given a keyid delete the key from storage.
561 * @keyid: The keyid to delete.
562 * @intrans: If we're already in a transaction.
564 * This function deletes a public key from whatever storage mechanism we
565 * are using. Returns 0 if the key existed.
567 static int db4_delete_key(struct onak_dbctx *dbctx,
568 uint64_t keyid, bool intrans)
570 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
571 struct openpgp_publickey *publickey = NULL;
574 uint32_t shortkeyid = 0;
575 uint64_t *subkeyids = NULL;
579 char *primary = NULL;
580 unsigned char worddb_data[12];
581 struct ll *wordlist = NULL;
582 struct ll *curword = NULL;
583 bool deadlock = false;
587 db4_starttrans(dbctx);
590 db4_fetch_key_id(dbctx, keyid, &publickey, true);
593 * Walk through the uids removing the words from the worddb.
595 if (publickey != NULL) {
596 uids = keyuids(publickey, &primary);
599 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
600 wordlist = makewordlist(wordlist, uids[i]);
603 ret = privctx->worddb->cursor(privctx->worddb,
608 for (curword = wordlist; curword != NULL && !deadlock;
609 curword = curword->next) {
610 memset(&key, 0, sizeof(key));
611 memset(&data, 0, sizeof(data));
612 key.data = curword->object;
613 key.size = strlen(key.data);
614 data.data = worddb_data;
615 data.size = sizeof(worddb_data);
618 * Our data is the key creation time followed by the
621 worddb_data[ 0] = publickey->publickey->data[1];
622 worddb_data[ 1] = publickey->publickey->data[2];
623 worddb_data[ 2] = publickey->publickey->data[3];
624 worddb_data[ 3] = publickey->publickey->data[4];
625 worddb_data[ 4] = (keyid >> 56) & 0xFF;
626 worddb_data[ 5] = (keyid >> 48) & 0xFF;
627 worddb_data[ 6] = (keyid >> 40) & 0xFF;
628 worddb_data[ 7] = (keyid >> 32) & 0xFF;
629 worddb_data[ 8] = (keyid >> 24) & 0xFF;
630 worddb_data[ 9] = (keyid >> 16) & 0xFF;
631 worddb_data[10] = (keyid >> 8) & 0xFF;
632 worddb_data[11] = keyid & 0xFF;
634 ret = cursor->c_get(cursor,
640 ret = cursor->c_del(cursor, 0);
644 logthing(LOGTHING_ERROR,
645 "Problem deleting word: %s "
646 "(0x%016" PRIX64 ")",
649 if (ret == DB_LOCK_DEADLOCK) {
654 ret = cursor->c_close(cursor);
657 ret = privctx->skshashdb->cursor(privctx->skshashdb,
661 get_skshash(publickey, &hash);
663 memset(&key, 0, sizeof(key));
664 memset(&data, 0, sizeof(data));
665 key.data = hash.hash;
666 key.size = sizeof(hash.hash);
668 data.size = sizeof(keyid);
670 ret = cursor->c_get(cursor,
676 ret = cursor->c_del(cursor, 0);
680 logthing(LOGTHING_ERROR,
681 "Problem deleting skshash: %s "
682 "(0x%016" PRIX64 ")",
685 if (ret == DB_LOCK_DEADLOCK) {
690 ret = cursor->c_close(cursor);
694 * Free our UID and word lists.
696 llfree(wordlist, NULL);
697 for (i = 0; uids[i] != NULL; i++) {
703 free_publickey(publickey);
708 ret = privctx->id32db->cursor(privctx->id32db,
713 shortkeyid = keyid & 0xFFFFFFFF;
715 memset(&key, 0, sizeof(key));
716 memset(&data, 0, sizeof(data));
717 key.data = &shortkeyid;
718 key.size = sizeof(shortkeyid);
720 data.size = sizeof(keyid);
722 ret = cursor->c_get(cursor,
728 ret = cursor->c_del(cursor, 0);
732 logthing(LOGTHING_ERROR,
733 "Problem deleting short keyid: %s "
734 "(0x%016" PRIX64 ")",
737 if (ret == DB_LOCK_DEADLOCK) {
742 subkeyids = keysubkeys(publickey);
744 while (subkeyids != NULL && subkeyids[i] != 0) {
745 memset(&key, 0, sizeof(key));
746 key.data = &subkeyids[i];
747 key.size = sizeof(subkeyids[i]);
748 privctx->subkeydb->del(privctx->subkeydb,
749 privctx->txn, &key, 0);
751 logthing(LOGTHING_ERROR,
752 "Problem deleting subkey id: %s "
753 "(0x%016" PRIX64 ")",
756 if (ret == DB_LOCK_DEADLOCK) {
761 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
763 memset(&key, 0, sizeof(key));
764 memset(&data, 0, sizeof(data));
765 key.data = &shortkeyid;
766 key.size = sizeof(shortkeyid);
768 data.size = sizeof(keyid);
770 ret = cursor->c_get(cursor,
776 ret = cursor->c_del(cursor, 0);
780 logthing(LOGTHING_ERROR,
781 "Problem deleting short keyid: %s "
782 "(0x%016" PRIX64 ")",
785 if (ret == DB_LOCK_DEADLOCK) {
790 if (subkeyids != NULL) {
794 ret = cursor->c_close(cursor);
801 key.size = sizeof(keyid);
803 keydb(privctx, keyid)->del(keydb(privctx, keyid),
813 return deadlock ? (-1) : (ret == DB_NOTFOUND);
817 * store_key - Takes a key and stores it.
818 * @publickey: A pointer to the public key to store.
819 * @intrans: If we're already in a transaction.
820 * @update: If true the key exists and should be updated.
822 * Again we just use the hex representation of the keyid as the filename
823 * to store the key to. We flatten the public key to a list of OpenPGP
824 * packets and then use write_openpgp_stream() to write the stream out to
825 * the file. If update is true then we delete the old key first, otherwise
826 * we trust that it doesn't exist.
828 static int db4_store_key(struct onak_dbctx *dbctx,
829 struct openpgp_publickey *publickey, bool intrans,
832 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
833 struct openpgp_packet_list *packets = NULL;
834 struct openpgp_packet_list *list_end = NULL;
835 struct openpgp_publickey *next = NULL;
838 struct buffer_ctx storebuf;
842 uint32_t shortkeyid = 0;
843 uint64_t *subkeyids = NULL;
845 char *primary = NULL;
846 unsigned char worddb_data[12];
847 struct ll *wordlist = NULL;
848 struct ll *curword = NULL;
849 bool deadlock = false;
852 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
853 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
858 db4_starttrans(dbctx);
862 * Delete the key if we already have it.
864 * TODO: Can we optimize this perhaps? Possibly when other data is
865 * involved as well? I suspect this is easiest and doesn't make a lot
866 * of difference though - the largest chunk of data is the keydata and
867 * it definitely needs updated.
870 deadlock = (db4_delete_key(dbctx, keyid, true) == -1);
874 * Convert the key to a flat set of binary data.
877 next = publickey->next;
878 publickey->next = NULL;
879 flatten_publickey(publickey, &packets, &list_end);
880 publickey->next = next;
883 storebuf.size = 8192;
884 storebuf.buffer = malloc(8192);
886 write_openpgp_stream(buffer_putchar, &storebuf, packets);
889 * Now we have the key data store it in the DB; the keyid is
892 memset(&key, 0, sizeof(key));
893 memset(&data, 0, sizeof(data));
895 key.size = sizeof(keyid);
896 data.size = storebuf.offset;
897 data.data = storebuf.buffer;
899 ret = keydb(privctx, keyid)->put(keydb(privctx, keyid),
905 logthing(LOGTHING_ERROR,
906 "Problem storing key: %s",
908 if (ret == DB_LOCK_DEADLOCK) {
913 free(storebuf.buffer);
914 storebuf.buffer = NULL;
918 free_packet_list(packets);
923 * Walk through our uids storing the words into the db with the keyid.
926 uids = keyuids(publickey, &primary);
929 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
930 wordlist = makewordlist(wordlist, uids[i]);
933 for (curword = wordlist; curword != NULL && !deadlock;
934 curword = curword->next) {
935 memset(&key, 0, sizeof(key));
936 memset(&data, 0, sizeof(data));
937 key.data = curword->object;
938 key.size = strlen(key.data);
939 data.data = worddb_data;
940 data.size = sizeof(worddb_data);
943 * Our data is the key creation time followed by the
946 worddb_data[ 0] = publickey->publickey->data[1];
947 worddb_data[ 1] = publickey->publickey->data[2];
948 worddb_data[ 2] = publickey->publickey->data[3];
949 worddb_data[ 3] = publickey->publickey->data[4];
950 worddb_data[ 4] = (keyid >> 56) & 0xFF;
951 worddb_data[ 5] = (keyid >> 48) & 0xFF;
952 worddb_data[ 6] = (keyid >> 40) & 0xFF;
953 worddb_data[ 7] = (keyid >> 32) & 0xFF;
954 worddb_data[ 8] = (keyid >> 24) & 0xFF;
955 worddb_data[ 9] = (keyid >> 16) & 0xFF;
956 worddb_data[10] = (keyid >> 8) & 0xFF;
957 worddb_data[11] = keyid & 0xFF;
958 ret = privctx->worddb->put(privctx->worddb,
964 logthing(LOGTHING_ERROR,
965 "Problem storing word: %s",
967 if (ret == DB_LOCK_DEADLOCK) {
974 * Free our UID and word lists.
976 llfree(wordlist, NULL);
977 for (i = 0; uids[i] != NULL; i++) {
986 * Write the truncated 32 bit keyid so we can lookup the full id for
990 shortkeyid = keyid & 0xFFFFFFFF;
992 memset(&key, 0, sizeof(key));
993 memset(&data, 0, sizeof(data));
994 key.data = &shortkeyid;
995 key.size = sizeof(shortkeyid);
997 data.size = sizeof(keyid);
999 ret = privctx->id32db->put(privctx->id32db,
1005 logthing(LOGTHING_ERROR,
1006 "Problem storing short keyid: %s",
1008 if (ret == DB_LOCK_DEADLOCK) {
1015 subkeyids = keysubkeys(publickey);
1017 while (subkeyids != NULL && subkeyids[i] != 0) {
1018 /* Store the subkey ID -> main key ID mapping */
1019 memset(&key, 0, sizeof(key));
1020 memset(&data, 0, sizeof(data));
1021 key.data = &subkeyids[i];
1022 key.size = sizeof(subkeyids[i]);
1024 data.size = sizeof(keyid);
1026 ret = privctx->subkeydb->put(privctx->subkeydb,
1032 logthing(LOGTHING_ERROR,
1033 "Problem storing subkey keyid: %s",
1035 if (ret == DB_LOCK_DEADLOCK) {
1040 /* Store the short subkey ID -> main key ID mapping */
1041 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
1043 memset(&key, 0, sizeof(key));
1044 memset(&data, 0, sizeof(data));
1045 key.data = &shortkeyid;
1046 key.size = sizeof(shortkeyid);
1048 data.size = sizeof(keyid);
1050 ret = privctx->id32db->put(privctx->id32db,
1056 logthing(LOGTHING_ERROR,
1057 "Problem storing short keyid: %s",
1059 if (ret == DB_LOCK_DEADLOCK) {
1064 if (subkeyids != NULL) {
1071 get_skshash(publickey, &hash);
1072 memset(&key, 0, sizeof(key));
1073 memset(&data, 0, sizeof(data));
1074 key.data = hash.hash;
1075 key.size = sizeof(hash.hash);
1077 data.size = sizeof(keyid);
1079 ret = privctx->skshashdb->put(privctx->skshashdb,
1085 logthing(LOGTHING_ERROR,
1086 "Problem storing SKS hash: %s",
1088 if (ret == DB_LOCK_DEADLOCK) {
1095 db4_endtrans(dbctx);
1098 return deadlock ? -1 : 0 ;
1102 * iterate_keys - call a function once for each key in the db.
1103 * @iterfunc: The function to call.
1104 * @ctx: A context pointer
1106 * Calls iterfunc once for each key in the database. ctx is passed
1107 * unaltered to iterfunc. This function is intended to aid database dumps
1108 * and statistic calculations.
1110 * Returns the number of keys we iterated over.
1112 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1113 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1116 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1122 struct buffer_ctx fetchbuf;
1123 struct openpgp_packet_list *packets = NULL;
1124 struct openpgp_publickey *key = NULL;
1126 for (i = 0; i < privctx->numdbs; i++) {
1127 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1132 memset(&dbkey, 0, sizeof(dbkey));
1133 memset(&data, 0, sizeof(data));
1134 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1136 fetchbuf.buffer = data.data;
1137 fetchbuf.offset = 0;
1138 fetchbuf.size = data.size;
1139 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1141 parse_keys(packets, &key);
1145 free_publickey(key);
1147 free_packet_list(packets);
1150 memset(&dbkey, 0, sizeof(dbkey));
1151 memset(&data, 0, sizeof(data));
1152 ret = cursor->c_get(cursor, &dbkey, &data,
1156 if (ret != DB_NOTFOUND) {
1157 logthing(LOGTHING_ERROR,
1158 "Problem reading key: %s",
1162 ret = cursor->c_close(cursor);
1170 * Include the basic keydb routines.
1172 #define NEED_GETKEYSIGS 1
1173 #define NEED_KEYID2UID 1
1174 #define NEED_UPDATEKEYS 1
1175 #define NEED_GET_FP 1
1179 * cleanupdb - De-initialize the key database.
1181 * This function should be called upon program exit to allow the DB to
1182 * cleanup after itself.
1184 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1186 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1189 if (privctx->dbenv != NULL) {
1190 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1191 if (privctx->subkeydb != NULL) {
1192 privctx->subkeydb->close(privctx->subkeydb, 0);
1193 privctx->subkeydb = NULL;
1195 if (privctx->skshashdb != NULL) {
1196 privctx->skshashdb->close(privctx->skshashdb, 0);
1197 privctx->skshashdb = NULL;
1199 if (privctx->id32db != NULL) {
1200 privctx->id32db->close(privctx->id32db, 0);
1201 privctx->id32db = NULL;
1203 if (privctx->worddb != NULL) {
1204 privctx->worddb->close(privctx->worddb, 0);
1205 privctx->worddb = NULL;
1207 for (i = 0; i < privctx->numdbs; i++) {
1208 if (privctx->dbconns[i] != NULL) {
1209 privctx->dbconns[i]->close(privctx->dbconns[i],
1211 privctx->dbconns[i] = NULL;
1214 free(privctx->dbconns);
1215 privctx->dbconns = NULL;
1216 privctx->dbenv->close(privctx->dbenv, 0);
1217 privctx->dbenv = NULL;
1226 * initdb - Initialize the key database.
1228 * This function should be called before any of the other functions in
1229 * this file are called in order to allow the DB to be initialized ready
1232 struct onak_dbctx *keydb_db4_init(bool readonly)
1239 struct stat statbuf;
1241 struct onak_dbctx *dbctx;
1242 struct onak_db4_dbctx *privctx;
1244 dbctx = malloc(sizeof(*dbctx));
1245 if (dbctx == NULL) {
1248 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1249 if (privctx == NULL) {
1254 /* Default to 16 key data DBs */
1255 privctx->numdbs = 16;
1257 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
1259 ret = stat(buf, &statbuf);
1260 while ((ret == 0) || (errno != ENOENT)) {
1262 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1263 "lock file: %s (%d)", strerror(errno), ret);
1266 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1268 ret = stat(buf, &statbuf);
1272 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
1273 numdb = fopen(buf, "r");
1274 if (numdb != NULL) {
1275 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1276 privctx->numdbs = atoi(buf);
1279 } else if (!readonly) {
1280 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1282 numdb = fopen(buf, "w");
1283 if (numdb != NULL) {
1284 fprintf(numdb, "%d", privctx->numdbs);
1287 logthing(LOGTHING_ERROR,
1288 "Couldn't write num_keydb: %s",
1293 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1294 if (privctx->dbconns == NULL) {
1295 logthing(LOGTHING_CRITICAL,
1296 "Couldn't allocate memory for dbconns");
1301 ret = db_env_create(&privctx->dbenv, 0);
1303 logthing(LOGTHING_CRITICAL,
1304 "db_env_create: %s", db_strerror(ret));
1309 * Up the number of locks we're allowed at once. We base this on
1310 * the maximum number of keys we're going to return.
1312 maxlocks = config.maxkeys * 16;
1313 if (maxlocks < 1000) {
1316 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1317 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1320 * Enable deadlock detection so that we don't block indefinitely on
1321 * anything. What we really want is simple 2 state locks, but I'm not
1322 * sure how to make the standard DB functions do that yet.
1325 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1326 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1328 logthing(LOGTHING_CRITICAL,
1329 "db_env_create: %s", db_strerror(ret));
1334 ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
1335 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1339 #ifdef DB_VERSION_MISMATCH
1340 if (ret == DB_VERSION_MISMATCH) {
1341 privctx->dbenv->close(privctx->dbenv, 0);
1342 privctx->dbenv = NULL;
1343 ret = db4_upgradedb(privctx);
1345 ret = db_env_create(&privctx->dbenv, 0);
1348 privctx->dbenv->set_errcall(privctx->dbenv,
1350 privctx->dbenv->set_lk_detect(privctx->dbenv,
1352 ret = privctx->dbenv->open(privctx->dbenv,
1354 DB_INIT_LOG | DB_INIT_MPOOL |
1355 DB_INIT_LOCK | DB_INIT_TXN |
1356 DB_CREATE | DB_RECOVER,
1360 privctx->dbenv->txn_checkpoint(
1370 logthing(LOGTHING_CRITICAL,
1371 "Error opening db environment: %s (%s)",
1374 privctx->dbenv->close(privctx->dbenv, 0);
1375 privctx->dbenv = NULL;
1380 db4_starttrans(dbctx);
1382 for (i = 0; !ret && i < privctx->numdbs; i++) {
1383 ret = db_create(&privctx->dbconns[i],
1386 logthing(LOGTHING_CRITICAL,
1387 "db_create: %s", db_strerror(ret));
1391 snprintf(buf, 1023, "keydb.%d.db", i);
1396 ret = privctx->dbconns[i]->open(
1397 privctx->dbconns[i],
1405 logthing(LOGTHING_CRITICAL,
1406 "Error opening key database:"
1416 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1418 logthing(LOGTHING_CRITICAL, "db_create: %s",
1424 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1428 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1429 "worddb", "worddb", DB_BTREE,
1433 logthing(LOGTHING_CRITICAL,
1434 "Error opening word database: %s (%s)",
1441 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1443 logthing(LOGTHING_CRITICAL, "db_create: %s",
1449 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1453 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1454 "id32db", "id32db", DB_HASH,
1458 logthing(LOGTHING_CRITICAL,
1459 "Error opening id32 database: %s (%s)",
1466 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1468 logthing(LOGTHING_CRITICAL, "db_create: %s",
1474 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1476 "skshashdb", DB_HASH,
1480 logthing(LOGTHING_CRITICAL,
1481 "Error opening skshash database: %s (%s)",
1488 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1490 logthing(LOGTHING_CRITICAL, "db_create: %s",
1496 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1497 "subkeydb", "subkeydb",
1502 logthing(LOGTHING_CRITICAL,
1503 "Error opening subkey database: %s (%s)",
1509 if (privctx->txn != NULL) {
1510 db4_endtrans(dbctx);
1514 db4_cleanupdb(dbctx);
1515 logthing(LOGTHING_CRITICAL,
1516 "Error opening database; exiting");
1520 dbctx->cleanupdb = db4_cleanupdb;
1521 dbctx->starttrans = db4_starttrans;
1522 dbctx->endtrans = db4_endtrans;
1523 dbctx->fetch_key_id = db4_fetch_key_id;
1524 dbctx->fetch_key_fp = generic_fetch_key_fp;
1525 dbctx->fetch_key_text = db4_fetch_key_text;
1526 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1527 dbctx->store_key = db4_store_key;
1528 dbctx->update_keys = generic_update_keys;
1529 dbctx->delete_key = db4_delete_key;
1530 dbctx->getkeysigs = generic_getkeysigs;
1531 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1532 dbctx->keyid2uid = generic_keyid2uid;
1533 dbctx->getfullkeyid = db4_getfullkeyid;
1534 dbctx->iterate_keys = db4_iterate_keys;