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, see <https://www.gnu.org/licenses/>.
19 #include <sys/types.h>
34 #include "charfuncs.h"
38 #include "decodekey.h"
39 #include "keystructs.h"
44 #include "onak-conf.h"
48 #define DB4_UPGRADE_FILE "db_upgrade.lck"
50 struct onak_db4_dbctx {
51 DB_ENV *dbenv; /* The database environment context */
52 int numdbs; /* Number of data databases in use */
53 DB **dbconns; /* Connections to the key data databases */
54 DB *worddb; /* Connection to the word lookup database */
55 DB *id32db; /* Connection to the 32 bit ID lookup database */
56 DB *id64db; /* Connection to the 64 bit ID lookup database */
57 DB *skshashdb; /* Connection to the SKS hash database */
58 DB *subkeydb; /* Connection to the subkey ID lookup database */
59 DB_TXN *txn; /* Our current transaction ID */
62 DB *keydb_id(struct onak_db4_dbctx *privctx, uint64_t keyid)
68 return(privctx->dbconns[keytrun % privctx->numdbs]);
71 DB *keydb_fp(struct onak_db4_dbctx *privctx, struct openpgp_fingerprint *fp)
75 keytrun = (fp->fp[4] << 24) |
80 return(privctx->dbconns[keytrun % privctx->numdbs]);
84 * db4_errfunc - Direct DB errors to logfile
86 * Basic function to take errors from the DB library and output them to
87 * the logfile rather than stderr.
89 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 3)
90 static void db4_errfunc(const char *errpfx, const char *errmsg)
92 static void db4_errfunc(const DB_ENV *edbenv, const char *errpfx,
97 logthing(LOGTHING_DEBUG, "db4 error: %s:%s", errpfx, errmsg);
99 logthing(LOGTHING_DEBUG, "db4 error: %s", errmsg);
106 * starttrans - Start a transaction.
108 * Start a transaction. Intended to be used if we're about to perform many
109 * operations on the database to help speed it all up, or if we want
110 * something to only succeed if all relevant operations are successful.
112 static bool db4_starttrans(struct onak_dbctx *dbctx)
114 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
117 log_assert(privctx->dbenv != NULL);
118 log_assert(privctx->txn == NULL);
120 ret = privctx->dbenv->txn_begin(privctx->dbenv,
121 NULL, /* No parent transaction */
125 logthing(LOGTHING_CRITICAL,
126 "Error starting transaction: %s",
135 * endtrans - End a transaction.
137 * Ends a transaction.
139 static void db4_endtrans(struct onak_dbctx *dbctx)
141 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
144 log_assert(privctx->dbenv != NULL);
145 log_assert(privctx->txn != NULL);
147 ret = privctx->txn->commit(privctx->txn,
150 logthing(LOGTHING_CRITICAL,
151 "Error ending transaction: %s",
161 * db4_upgradedb - Upgrade a DB4 database
163 * Called if we discover we need to upgrade our DB4 database; ie if
164 * we're running with a newer version of db4 than the database was
167 static int db4_upgradedb(struct onak_dbctx *dbctx)
169 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
178 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
180 lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
181 if (lockfile_fd < 0) {
182 if (errno == EEXIST) {
183 while (stat(buf, &statbuf) == 0) ;
186 logthing(LOGTHING_CRITICAL, "Couldn't open database "
187 "update lock file: %s", strerror(errno));
191 snprintf(buf, sizeof(buf) - 1, "%d", getpid());
192 written = write(lockfile_fd, buf, strlen(buf));
194 if (written != strlen(buf)) {
195 logthing(LOGTHING_CRITICAL, "Couldn't write PID to lockfile: "
196 "%s", strerror(errno));
197 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
203 logthing(LOGTHING_NOTICE, "Upgrading DB4 database");
204 ret = db_env_create(&privctx->dbenv, 0);
206 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
207 privctx->dbenv->remove(privctx->dbenv, dbctx->config->location, 0);
208 privctx->dbenv = NULL;
210 for (i = 0; i < privctx->numdbs; i++) {
211 ret = db_create(&curdb, NULL, 0);
213 snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
214 dbctx->config->location, i);
215 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
216 curdb->upgrade(curdb, buf, 0);
217 curdb->close(curdb, 0);
219 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
225 ret = db_create(&curdb, NULL, 0);
227 snprintf(buf, sizeof(buf) - 1, "%s/worddb", dbctx->config->location);
228 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
229 curdb->upgrade(curdb, buf, 0);
230 curdb->close(curdb, 0);
232 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
237 ret = db_create(&curdb, NULL, 0);
239 snprintf(buf, sizeof(buf) - 1, "%s/id32db", dbctx->config->location);
240 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
241 curdb->upgrade(curdb, buf, 0);
242 curdb->close(curdb, 0);
244 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
249 ret = db_create(&curdb, NULL, 0);
251 snprintf(buf, sizeof(buf) - 1, "%s/id64db", dbctx->config->location);
252 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
253 curdb->upgrade(curdb, buf, 0);
254 curdb->close(curdb, 0);
256 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
261 ret = db_create(&curdb, NULL, 0);
263 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", dbctx->config->location);
264 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
265 curdb->upgrade(curdb, buf, 0);
266 curdb->close(curdb, 0);
268 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
273 ret = db_create(&curdb, NULL, 0);
275 snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", dbctx->config->location);
276 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
277 curdb->upgrade(curdb, buf, 0);
278 curdb->close(curdb, 0);
280 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
285 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
293 * getfullkeyid - Maps a 32bit key id to a 64bit one.
294 * @keyid: The 32bit keyid.
296 * This function maps a 32bit key id to the full 64bit one. It returns the
297 * first full keyid that has this short keyid. If the key isn't found a
298 * keyid of 0 is returned.
300 * FIXME: This should either return the fingerprint or ideally go away
303 static uint64_t db4_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
305 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
308 uint32_t shortkeyid = 0;
312 if (keyid < 0x100000000LL) {
313 ret = privctx->id32db->cursor(privctx->id32db,
322 shortkeyid = keyid & 0xFFFFFFFF;
324 memset(&key, 0, sizeof(key));
325 memset(&data, 0, sizeof(data));
326 key.data = &shortkeyid;
327 key.size = sizeof(shortkeyid);
328 data.flags = DB_DBT_MALLOC;
330 ret = cursor->c_get(cursor,
337 /* Only works for v4, not v3 or v5 */
338 for (i = 12; i < 20; i++) {
340 keyid |= ((uint8_t *) data.data)[i];
343 if (data.data != NULL) {
349 cursor->c_close(cursor);
357 * fetch_key_fp - Given a fingerprint fetch the key from storage.
359 static int db4_fetch_key_fp(struct onak_dbctx *dbctx,
360 struct openpgp_fingerprint *fingerprint,
361 struct openpgp_publickey **publickey,
364 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
365 struct openpgp_packet_list *packets = NULL;
369 struct buffer_ctx fetchbuf;
370 struct openpgp_fingerprint subfp;
372 memset(&key, 0, sizeof(key));
373 memset(&data, 0, sizeof(data));
378 key.size = fingerprint->length;
379 key.data = fingerprint->fp;
382 db4_starttrans(dbctx);
385 ret = keydb_fp(privctx, fingerprint)->get(keydb_fp(privctx,
392 if (ret == DB_NOTFOUND) {
393 /* If we didn't find the key ID see if it's a subkey ID */
394 memset(&key, 0, sizeof(key));
395 memset(&data, 0, sizeof(data));
396 data.data = subfp.fp;
397 data.ulen = MAX_FINGERPRINT_LEN;
398 data.flags = DB_DBT_USERMEM;
399 key.data = fingerprint->fp;
400 key.size = fingerprint->length;
402 ret = privctx->subkeydb->get(privctx->subkeydb,
409 /* We got a subkey match; retrieve the actual key */
410 memset(&key, 0, sizeof(key));
411 key.size = subfp.length = data.size;
414 memset(&data, 0, sizeof(data));
418 ret = keydb_fp(privctx, &subfp)->get(
419 keydb_fp(privctx, &subfp),
428 fetchbuf.buffer = data.data;
430 fetchbuf.size = data.size;
431 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
433 parse_keys(packets, publickey);
434 free_packet_list(packets);
437 } else if (ret != DB_NOTFOUND) {
438 logthing(LOGTHING_ERROR,
439 "Problem retrieving key: %s",
451 * fetch_key_id - Given a keyid fetch the key from storage.
452 * @keyid: The keyid to fetch.
453 * @publickey: A pointer to a structure to return the key in.
454 * @intrans: If we're already in a transaction.
456 * We use the hex representation of the keyid as the filename to fetch the
457 * key from. The key is stored in the file as a binary OpenPGP stream of
458 * packets, so we can just use read_openpgp_stream() to read the packets
459 * in and then parse_keys() to parse the packets into a publickey
462 static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
463 struct openpgp_publickey **publickey,
466 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
467 struct openpgp_packet_list *packets = NULL;
472 uint32_t shortkeyid = 0;
473 struct openpgp_fingerprint fingerprint;
477 db4_starttrans(dbctx);
480 /* If the key ID fits in 32 bits assume it's a short key id */
481 if (keyid < 0x100000000LL) {
482 ret = privctx->id32db->cursor(privctx->id32db,
487 shortkeyid = keyid & 0xFFFFFFFF;
488 memset(&key, 0, sizeof(key));
489 memset(&data, 0, sizeof(data));
490 key.data = &shortkeyid;
491 key.size = sizeof(shortkeyid);
493 ret = privctx->id64db->cursor(privctx->id64db,
498 memset(&key, 0, sizeof(key));
499 memset(&data, 0, sizeof(data));
501 key.size = sizeof(keyid);
508 memset(&data, 0, sizeof(data));
509 data.ulen = MAX_FINGERPRINT_LEN;
510 data.data = fingerprint.fp;
511 data.flags = DB_DBT_USERMEM;
514 while (cursor->c_get(cursor, &key, &data,
515 first ? DB_SET : DB_NEXT_DUP) == 0) {
516 /* We got a match; retrieve the actual key */
517 fingerprint.length = data.size;
519 if (db4_fetch_key_fp(dbctx, &fingerprint,
523 memset(&data, 0, sizeof(data));
524 data.ulen = MAX_FINGERPRINT_LEN;
525 data.data = fingerprint.fp;
526 data.flags = DB_DBT_USERMEM;
529 cursor->c_close(cursor);
540 int worddb_cmp(const void *d1, const void *d2)
542 return memcmp(d1, d2, 12);
546 * fetch_key_text - Trys to find the keys that contain the supplied text.
547 * @search: The text to search for.
548 * @publickey: A pointer to a structure to return the key in.
550 * This function searches for the supplied text and returns the keys that
553 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
554 struct openpgp_publickey **publickey)
556 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
563 char *searchtext = NULL;
564 struct ll *wordlist = NULL;
565 struct ll *curword = NULL;
566 struct keyarray keylist = { NULL, 0, 0 };
567 struct keyarray newkeylist = { NULL, 0, 0 };
569 struct openpgp_fingerprint fingerprint;
572 searchtext = strdup(search);
573 wordlist = makewordlist(wordlist, searchtext);
575 for (curword = wordlist; curword != NULL; curword = curword->next) {
576 db4_starttrans(dbctx);
578 ret = privctx->worddb->cursor(privctx->worddb,
588 memset(&key, 0, sizeof(key));
589 memset(&data, 0, sizeof(data));
590 key.data = curword->object;
591 key.size = strlen(curword->object);
592 data.flags = DB_DBT_MALLOC;
593 ret = cursor->c_get(cursor,
597 while (ret == 0 && strncmp(key.data, curword->object,
599 ((char *) curword->object)[key.size] == 0) {
601 fingerprint.length = data.size;
602 memcpy(fingerprint.fp, data.data, data.size);
605 * Only add the keys containing this word if this is
606 * our first pass (ie we have no existing key list),
607 * or the key contained a previous word.
609 if (firstpass || array_find(&keylist, &fingerprint)) {
610 array_add(&newkeylist, &fingerprint);
616 ret = cursor->c_get(cursor,
621 array_free(&keylist);
622 keylist.keys = newkeylist.keys;
623 keylist.count = newkeylist.count;
624 keylist.size = newkeylist.size;
625 newkeylist.keys = NULL;
626 newkeylist.count = newkeylist.size = 0;
627 if (data.data != NULL) {
631 cursor->c_close(cursor);
636 llfree(wordlist, NULL);
639 if (keylist.count > config.maxkeys) {
640 keylist.count = config.maxkeys;
643 db4_starttrans(dbctx);
644 for (i = 0; i < keylist.count; i++) {
645 numkeys += db4_fetch_key_fp(dbctx, &keylist.keys[i],
649 array_free(&keylist);
658 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
659 const struct skshash *hash,
660 struct openpgp_publickey **publickey)
662 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
668 struct openpgp_fingerprint fingerprint;
670 ret = privctx->skshashdb->cursor(privctx->skshashdb,
679 memset(&key, 0, sizeof(key));
680 memset(&data, 0, sizeof(data));
681 key.data = (void *) hash->hash;
682 key.size = sizeof(hash->hash);
683 data.ulen = MAX_FINGERPRINT_LEN;
684 data.data = fingerprint.fp;
685 data.flags = DB_DBT_USERMEM;
687 ret = cursor->c_get(cursor,
693 fingerprint.length = data.size;
694 count = db4_fetch_key_fp(dbctx, &fingerprint,
698 cursor->c_close(cursor);
705 * delete_key - Given a keyid delete the key from storage.
706 * @fp: The fingerprint of the key to delete.
707 * @intrans: If we're already in a transaction.
709 * This function deletes a public key from whatever storage mechanism we
710 * are using. Returns 0 if the key existed.
712 static int db4_delete_key(struct onak_dbctx *dbctx,
713 struct openpgp_fingerprint *fp,
716 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
717 struct openpgp_publickey *publickey = NULL;
720 DBC *cursor64 = NULL;
721 uint32_t shortkeyid = 0;
722 uint64_t subkeyid = 0;
723 struct openpgp_fingerprint *subkeyids = NULL;
727 char *primary = NULL;
728 unsigned char worddb_data[12];
729 struct ll *wordlist = NULL;
730 struct ll *curword = NULL;
731 bool deadlock = false;
736 db4_starttrans(dbctx);
739 if (db4_fetch_key_fp(dbctx, fp, &publickey, true) == 0) {
746 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
751 * Walk through the uids removing the words from the worddb.
753 if (publickey != NULL) {
754 uids = keyuids(publickey, &primary);
757 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
758 wordlist = makewordlist(wordlist, uids[i]);
761 privctx->worddb->cursor(privctx->worddb,
766 for (curword = wordlist; curword != NULL && !deadlock;
767 curword = curword->next) {
768 memset(&key, 0, sizeof(key));
769 memset(&data, 0, sizeof(data));
770 key.data = curword->object;
771 key.size = strlen(key.data);
772 data.data = worddb_data;
773 data.size = sizeof(worddb_data);
776 * New style uses the fingerprint as the data
777 * Old (unsupported) style was the 64 bit keyid
779 memset(&key, 0, sizeof(key));
780 memset(&data, 0, sizeof(data));
781 key.data = curword->object;
782 key.size = strlen(key.data);
784 data.size = fp->length;
786 ret = cursor->c_get(cursor,
792 ret = cursor->c_del(cursor, 0);
795 if (ret != 0 && ret != DB_NOTFOUND) {
796 logthing(LOGTHING_ERROR,
797 "Problem deleting word: %s "
798 "(0x%016" PRIX64 ")",
801 if (ret == DB_LOCK_DEADLOCK) {
806 cursor->c_close(cursor);
810 * Free our UID and word lists.
812 llfree(wordlist, NULL);
813 for (i = 0; uids[i] != NULL; i++) {
822 privctx->id32db->cursor(privctx->id32db,
826 privctx->id64db->cursor(privctx->id64db,
831 /* 32 bit short key mapping to fingerprint */
832 shortkeyid = keyid & 0xFFFFFFFF;
834 memset(&key, 0, sizeof(key));
835 memset(&data, 0, sizeof(data));
836 key.data = &shortkeyid;
837 key.size = sizeof(shortkeyid);
839 data.size = fp->length;
841 ret = cursor->c_get(cursor,
847 ret = cursor->c_del(cursor, 0);
850 if (ret != 0 && ret != DB_NOTFOUND) {
851 logthing(LOGTHING_ERROR,
852 "Problem deleting short keyid: %s "
853 "(0x%016" PRIX64 ")",
856 if (ret == DB_LOCK_DEADLOCK) {
861 /* 64 bit key mapping to fingerprint */
862 memset(&key, 0, sizeof(key));
863 memset(&data, 0, sizeof(data));
865 key.size = sizeof(keyid);
867 data.size = fp->length;
869 ret = cursor64->c_get(cursor64,
875 ret = cursor64->c_del(cursor64, 0);
878 if (ret != 0 && ret != DB_NOTFOUND) {
879 logthing(LOGTHING_ERROR,
880 "Problem deleting keyid: %s "
881 "(0x%016" PRIX64 ")",
884 if (ret == DB_LOCK_DEADLOCK) {
889 subkeyids = keysubkeys(publickey);
891 while (subkeyids != NULL && subkeyids[i].length != 0) {
892 subkeyid = fingerprint2keyid(&subkeyids[i]);
893 memset(&key, 0, sizeof(key));
894 key.data = subkeyids[i].fp;
895 key.size = subkeyids[i].length;
896 privctx->subkeydb->del(privctx->subkeydb,
897 privctx->txn, &key, 0);
898 if (ret != 0 && ret != DB_NOTFOUND) {
899 logthing(LOGTHING_ERROR,
900 "Problem deleting subkey id: %s "
901 "(0x%016" PRIX64 ")",
904 if (ret == DB_LOCK_DEADLOCK) {
909 shortkeyid = subkeyid & 0xFFFFFFFF;
911 /* Remove 32 bit keyid -> fingerprint mapping */
912 memset(&key, 0, sizeof(key));
913 memset(&data, 0, sizeof(data));
914 key.data = &shortkeyid;
915 key.size = sizeof(shortkeyid);
917 data.size = fp->length;
919 ret = cursor->c_get(cursor,
925 ret = cursor->c_del(cursor, 0);
928 if (ret != 0 && ret != DB_NOTFOUND) {
929 logthing(LOGTHING_ERROR,
930 "Problem deleting short keyid: %s "
931 "(0x%016" PRIX64 ")",
934 if (ret == DB_LOCK_DEADLOCK) {
939 /* Remove 64 bit keyid -> fingerprint mapping */
940 memset(&key, 0, sizeof(key));
941 memset(&data, 0, sizeof(data));
942 key.data = &subkeyid;
943 key.size = sizeof(subkeyid);
945 data.size = fp->length;
947 ret = cursor64->c_get(cursor64,
953 ret = cursor64->c_del(cursor64, 0);
956 if (ret != 0 && ret != DB_NOTFOUND) {
957 logthing(LOGTHING_ERROR,
958 "Problem deleting keyid: %s "
959 "(0x%016" PRIX64 ")",
962 if (ret == DB_LOCK_DEADLOCK) {
968 if (subkeyids != NULL) {
972 cursor64->c_close(cursor64);
974 cursor->c_close(cursor);
979 ret = privctx->skshashdb->cursor(privctx->skshashdb,
984 get_skshash(publickey, &hash);
986 /* Remove SKS hash -> fingerprint mapping */
987 memset(&key, 0, sizeof(key));
988 memset(&data, 0, sizeof(data));
989 key.data = hash.hash;
990 key.size = sizeof(hash.hash);
992 data.size = fp->length;
994 ret = cursor->c_get(cursor,
1000 ret = cursor->c_del(cursor, 0);
1003 if (ret != 0 && ret != DB_NOTFOUND) {
1004 logthing(LOGTHING_ERROR,
1005 "Problem deleting skshash: %s "
1006 "(0x%016" PRIX64 ")",
1009 if (ret == DB_LOCK_DEADLOCK) {
1014 cursor->c_close(cursor);
1018 free_publickey(publickey);
1023 key.size = fp->length;
1025 keydb_fp(privctx, fp)->del(keydb_fp(privctx, fp),
1032 db4_endtrans(dbctx);
1035 return deadlock ? (-1) : (ret == DB_NOTFOUND);
1039 * store_key - Takes a key and stores it.
1040 * @publickey: A pointer to the public key to store.
1041 * @intrans: If we're already in a transaction.
1042 * @update: If true the key exists and should be updated.
1044 * Again we just use the hex representation of the keyid as the filename
1045 * to store the key to. We flatten the public key to a list of OpenPGP
1046 * packets and then use write_openpgp_stream() to write the stream out to
1047 * the file. If update is true then we delete the old key first, otherwise
1048 * we trust that it doesn't exist.
1050 static int db4_store_key(struct onak_dbctx *dbctx,
1051 struct openpgp_publickey *publickey, bool intrans,
1054 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1055 struct openpgp_packet_list *packets = NULL;
1056 struct openpgp_packet_list *list_end = NULL;
1057 struct openpgp_publickey *next = NULL;
1060 struct buffer_ctx storebuf;
1064 uint32_t shortkeyid = 0;
1065 struct openpgp_fingerprint *subkeyids = NULL;
1067 char *primary = NULL;
1068 struct ll *wordlist = NULL;
1069 struct ll *curword = NULL;
1070 bool deadlock = false;
1071 struct skshash hash;
1072 struct openpgp_fingerprint fingerprint;
1074 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
1075 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
1079 if (get_fingerprint(publickey->publickey, &fingerprint) != ONAK_E_OK) {
1080 logthing(LOGTHING_ERROR, "Couldn't find fingerprint for key.");
1085 db4_starttrans(dbctx);
1089 * Delete the key if we already have it.
1091 * TODO: Can we optimize this perhaps? Possibly when other data is
1092 * involved as well? I suspect this is easiest and doesn't make a lot
1093 * of difference though - the largest chunk of data is the keydata and
1094 * it definitely needs updated.
1097 deadlock = (db4_delete_key(dbctx, &fingerprint, true) == -1);
1101 * Convert the key to a flat set of binary data.
1104 next = publickey->next;
1105 publickey->next = NULL;
1106 flatten_publickey(publickey, &packets, &list_end);
1107 publickey->next = next;
1109 storebuf.offset = 0;
1110 storebuf.size = 8192;
1111 storebuf.buffer = malloc(8192);
1113 write_openpgp_stream(buffer_putchar, &storebuf, packets);
1116 * Now we have the key data store it in the DB; the fingerprint
1119 memset(&key, 0, sizeof(key));
1120 memset(&data, 0, sizeof(data));
1121 key.data = fingerprint.fp;
1122 key.size = fingerprint.length;
1123 data.size = storebuf.offset;
1124 data.data = storebuf.buffer;
1126 ret = keydb_fp(privctx, &fingerprint)->put(
1127 keydb_fp(privctx, &fingerprint),
1133 logthing(LOGTHING_ERROR,
1134 "Problem storing key: %s",
1136 if (ret == DB_LOCK_DEADLOCK) {
1141 free(storebuf.buffer);
1142 storebuf.buffer = NULL;
1144 storebuf.offset = 0;
1146 free_packet_list(packets);
1151 * Walk through our uids storing the words into the db with the
1155 uids = keyuids(publickey, &primary);
1158 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1159 wordlist = makewordlist(wordlist, uids[i]);
1162 for (curword = wordlist; curword != NULL && !deadlock;
1163 curword = curword->next) {
1164 memset(&key, 0, sizeof(key));
1165 memset(&data, 0, sizeof(data));
1166 key.data = curword->object;
1167 key.size = strlen(key.data);
1168 data.data = fingerprint.fp;
1169 data.size = fingerprint.length;
1171 ret = privctx->worddb->put(privctx->worddb,
1177 logthing(LOGTHING_ERROR,
1178 "Problem storing word: %s",
1180 if (ret == DB_LOCK_DEADLOCK) {
1187 * Free our UID and word lists.
1189 llfree(wordlist, NULL);
1190 for (i = 0; uids[i] != NULL; i++) {
1199 * Write the truncated 32 bit keyid so we can lookup the fingerprint
1203 shortkeyid = keyid & 0xFFFFFFFF;
1205 memset(&key, 0, sizeof(key));
1206 memset(&data, 0, sizeof(data));
1207 key.data = &shortkeyid;
1208 key.size = sizeof(shortkeyid);
1209 data.data = fingerprint.fp;
1210 data.size = fingerprint.length;
1212 ret = privctx->id32db->put(privctx->id32db,
1218 logthing(LOGTHING_ERROR,
1219 "Problem storing short keyid: %s",
1221 if (ret == DB_LOCK_DEADLOCK) {
1228 * Write the 64 bit keyid so we can lookup the fingerprint for
1232 memset(&key, 0, sizeof(key));
1233 memset(&data, 0, sizeof(data));
1235 key.size = sizeof(keyid);
1236 data.data = fingerprint.fp;
1237 data.size = fingerprint.length;
1239 ret = privctx->id64db->put(privctx->id64db,
1245 logthing(LOGTHING_ERROR,
1246 "Problem storing keyid: %s",
1248 if (ret == DB_LOCK_DEADLOCK) {
1255 subkeyids = keysubkeys(publickey);
1257 while (subkeyids != NULL && subkeyids[i].length != 0) {
1258 /* Store the subkey ID -> main key fp mapping */
1259 memset(&key, 0, sizeof(key));
1260 memset(&data, 0, sizeof(data));
1261 key.data = subkeyids[i].fp;
1262 key.size = subkeyids[i].length;
1263 data.data = fingerprint.fp;
1264 data.size = fingerprint.length;
1266 ret = privctx->subkeydb->put(privctx->subkeydb,
1272 logthing(LOGTHING_ERROR,
1273 "Problem storing subkey keyid: %s",
1275 if (ret == DB_LOCK_DEADLOCK) {
1280 /* Store the 64 bit subkey ID -> main key fp mapping */
1281 memset(&key, 0, sizeof(key));
1282 memset(&data, 0, sizeof(data));
1284 keyid = fingerprint2keyid(&subkeyids[i]);
1286 key.size = sizeof(keyid);
1287 data.data = fingerprint.fp;
1288 data.size = fingerprint.length;
1290 ret = privctx->id64db->put(privctx->id64db,
1296 logthing(LOGTHING_ERROR,
1297 "Problem storing keyid: %s",
1299 if (ret == DB_LOCK_DEADLOCK) {
1304 /* Store the short subkey ID -> main key fp mapping */
1305 shortkeyid = keyid & 0xFFFFFFFF;
1307 memset(&key, 0, sizeof(key));
1308 memset(&data, 0, sizeof(data));
1309 key.data = &shortkeyid;
1310 key.size = sizeof(shortkeyid);
1311 data.data = fingerprint.fp;
1312 data.size = fingerprint.length;
1314 ret = privctx->id32db->put(privctx->id32db,
1320 logthing(LOGTHING_ERROR,
1321 "Problem storing short keyid: %s",
1323 if (ret == DB_LOCK_DEADLOCK) {
1329 if (subkeyids != NULL) {
1336 get_skshash(publickey, &hash);
1337 memset(&key, 0, sizeof(key));
1338 memset(&data, 0, sizeof(data));
1339 key.data = hash.hash;
1340 key.size = sizeof(hash.hash);
1341 data.data = fingerprint.fp;
1342 data.size = fingerprint.length;
1344 ret = privctx->skshashdb->put(privctx->skshashdb,
1350 logthing(LOGTHING_ERROR,
1351 "Problem storing SKS hash: %s",
1353 if (ret == DB_LOCK_DEADLOCK) {
1360 db4_endtrans(dbctx);
1363 return deadlock ? -1 : 0 ;
1367 * iterate_keys - call a function once for each key in the db.
1368 * @iterfunc: The function to call.
1369 * @ctx: A context pointer
1371 * Calls iterfunc once for each key in the database. ctx is passed
1372 * unaltered to iterfunc. This function is intended to aid database dumps
1373 * and statistic calculations.
1375 * Returns the number of keys we iterated over.
1377 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1378 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1381 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1387 struct buffer_ctx fetchbuf;
1388 struct openpgp_packet_list *packets = NULL;
1389 struct openpgp_publickey *key = NULL;
1391 for (i = 0; i < privctx->numdbs; i++) {
1392 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1401 memset(&dbkey, 0, sizeof(dbkey));
1402 memset(&data, 0, sizeof(data));
1403 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1405 fetchbuf.buffer = data.data;
1406 fetchbuf.offset = 0;
1407 fetchbuf.size = data.size;
1408 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1410 parse_keys(packets, &key);
1414 free_publickey(key);
1416 free_packet_list(packets);
1419 memset(&dbkey, 0, sizeof(dbkey));
1420 memset(&data, 0, sizeof(data));
1421 ret = cursor->c_get(cursor, &dbkey, &data,
1425 if (ret != DB_NOTFOUND) {
1426 logthing(LOGTHING_ERROR,
1427 "Problem reading key: %s",
1431 cursor->c_close(cursor);
1439 * Include the basic keydb routines.
1441 #define NEED_GETKEYSIGS 1
1442 #define NEED_KEYID2UID 1
1443 #define NEED_UPDATEKEYS 1
1447 * cleanupdb - De-initialize the key database.
1449 * This function should be called upon program exit to allow the DB to
1450 * cleanup after itself.
1452 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1454 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1457 if (privctx->dbenv != NULL) {
1458 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1459 if (privctx->subkeydb != NULL) {
1460 privctx->subkeydb->close(privctx->subkeydb, 0);
1461 privctx->subkeydb = NULL;
1463 if (privctx->skshashdb != NULL) {
1464 privctx->skshashdb->close(privctx->skshashdb, 0);
1465 privctx->skshashdb = NULL;
1467 if (privctx->id64db != NULL) {
1468 privctx->id64db->close(privctx->id64db, 0);
1469 privctx->id64db = NULL;
1471 if (privctx->id32db != NULL) {
1472 privctx->id32db->close(privctx->id32db, 0);
1473 privctx->id32db = NULL;
1475 if (privctx->worddb != NULL) {
1476 privctx->worddb->close(privctx->worddb, 0);
1477 privctx->worddb = NULL;
1479 for (i = 0; i < privctx->numdbs; i++) {
1480 if (privctx->dbconns[i] != NULL) {
1481 privctx->dbconns[i]->close(privctx->dbconns[i],
1483 privctx->dbconns[i] = NULL;
1486 free(privctx->dbconns);
1487 privctx->dbconns = NULL;
1488 privctx->dbenv->close(privctx->dbenv, 0);
1489 privctx->dbenv = NULL;
1498 * initdb - Initialize the key database.
1500 * This function should be called before any of the other functions in
1501 * this file are called in order to allow the DB to be initialized ready
1504 struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
1511 struct stat statbuf;
1513 struct onak_dbctx *dbctx;
1514 struct onak_db4_dbctx *privctx;
1516 dbctx = malloc(sizeof(*dbctx));
1517 if (dbctx == NULL) {
1520 dbctx->config = dbcfg;
1521 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1522 if (privctx == NULL) {
1527 /* Default to 16 key data DBs */
1528 privctx->numdbs = 16;
1530 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
1532 ret = stat(buf, &statbuf);
1533 while ((ret == 0) || (errno != ENOENT)) {
1535 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1536 "lock file: %s (%d)", strerror(errno), ret);
1539 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1541 ret = stat(buf, &statbuf);
1545 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
1546 numdb = fopen(buf, "r");
1547 if (numdb != NULL) {
1548 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1549 privctx->numdbs = atoi(buf);
1552 } else if (!readonly) {
1553 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1555 numdb = fopen(buf, "w");
1556 if (numdb != NULL) {
1557 fprintf(numdb, "%d", privctx->numdbs);
1560 logthing(LOGTHING_ERROR,
1561 "Couldn't write num_keydb: %s",
1566 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1567 if (privctx->dbconns == NULL) {
1568 logthing(LOGTHING_CRITICAL,
1569 "Couldn't allocate memory for dbconns");
1574 ret = db_env_create(&privctx->dbenv, 0);
1576 logthing(LOGTHING_CRITICAL,
1577 "db_env_create: %s", db_strerror(ret));
1582 * Up the number of locks we're allowed at once. We base this on
1583 * the maximum number of keys we're going to return.
1586 maxlocks = config.maxkeys * 16;
1587 if (maxlocks < 1000) {
1590 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1591 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1595 * Enable deadlock detection so that we don't block indefinitely on
1596 * anything. What we really want is simple 2 state locks, but I'm not
1597 * sure how to make the standard DB functions do that yet.
1600 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1601 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1603 logthing(LOGTHING_CRITICAL,
1604 "db_env_create: %s", db_strerror(ret));
1609 ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
1610 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1614 #ifdef DB_VERSION_MISMATCH
1615 if (ret == DB_VERSION_MISMATCH) {
1616 privctx->dbenv->close(privctx->dbenv, 0);
1617 privctx->dbenv = NULL;
1618 ret = db4_upgradedb(dbctx);
1620 ret = db_env_create(&privctx->dbenv, 0);
1623 privctx->dbenv->set_errcall(privctx->dbenv,
1625 privctx->dbenv->set_lk_detect(privctx->dbenv,
1627 ret = privctx->dbenv->open(privctx->dbenv,
1629 DB_INIT_LOG | DB_INIT_MPOOL |
1630 DB_INIT_LOCK | DB_INIT_TXN |
1631 DB_CREATE | DB_RECOVER,
1635 privctx->dbenv->txn_checkpoint(
1645 logthing(LOGTHING_CRITICAL,
1646 "Error opening db environment: %s (%s)",
1649 if (privctx->dbenv != NULL) {
1650 privctx->dbenv->close(privctx->dbenv, 0);
1651 privctx->dbenv = NULL;
1657 db4_starttrans(dbctx);
1659 for (i = 0; !ret && i < privctx->numdbs; i++) {
1660 ret = db_create(&privctx->dbconns[i],
1663 logthing(LOGTHING_CRITICAL,
1664 "db_create: %s", db_strerror(ret));
1668 snprintf(buf, 1023, "keydb.%d.db", i);
1673 ret = privctx->dbconns[i]->open(
1674 privctx->dbconns[i],
1682 logthing(LOGTHING_CRITICAL,
1683 "Error opening key database:"
1693 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1695 logthing(LOGTHING_CRITICAL, "db_create: %s",
1701 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1705 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1706 "worddb", "worddb", DB_BTREE,
1710 logthing(LOGTHING_CRITICAL,
1711 "Error opening word database: %s (%s)",
1718 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1720 logthing(LOGTHING_CRITICAL, "db_create: %s",
1726 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1730 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1731 "id32db", "id32db", DB_HASH,
1735 logthing(LOGTHING_CRITICAL,
1736 "Error opening id32 database: %s (%s)",
1743 ret = db_create(&privctx->id64db, privctx->dbenv, 0);
1745 logthing(LOGTHING_CRITICAL, "db_create: %s",
1751 ret = privctx->id64db->set_flags(privctx->id64db, DB_DUP);
1755 ret = privctx->id64db->open(privctx->id64db, privctx->txn,
1756 "id64db", "id64db", DB_HASH,
1760 logthing(LOGTHING_CRITICAL,
1761 "Error opening id64 database: %s (%s)",
1768 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1770 logthing(LOGTHING_CRITICAL, "db_create: %s",
1776 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1778 "skshashdb", DB_HASH,
1782 logthing(LOGTHING_CRITICAL,
1783 "Error opening skshash database: %s (%s)",
1790 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1792 logthing(LOGTHING_CRITICAL, "db_create: %s",
1798 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1799 "subkeydb", "subkeydb",
1804 logthing(LOGTHING_CRITICAL,
1805 "Error opening subkey database: %s (%s)",
1811 if (privctx->txn != NULL) {
1812 db4_endtrans(dbctx);
1816 db4_cleanupdb(dbctx);
1817 logthing(LOGTHING_CRITICAL,
1818 "Error opening database; exiting");
1822 dbctx->cleanupdb = db4_cleanupdb;
1823 dbctx->starttrans = db4_starttrans;
1824 dbctx->endtrans = db4_endtrans;
1825 dbctx->fetch_key_id = db4_fetch_key_id;
1826 dbctx->fetch_key_fp = db4_fetch_key_fp;
1827 dbctx->fetch_key_text = db4_fetch_key_text;
1828 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1829 dbctx->store_key = db4_store_key;
1830 dbctx->update_keys = generic_update_keys;
1831 dbctx->delete_key = db4_delete_key;
1832 dbctx->getkeysigs = generic_getkeysigs;
1833 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1834 dbctx->keyid2uid = generic_keyid2uid;
1835 dbctx->getfullkeyid = db4_getfullkeyid;
1836 dbctx->iterate_keys = db4_iterate_keys;