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)
83 return(privctx->dbconns[keytrun % privctx->numdbs]);
87 * db4_errfunc - Direct DB errors to logfile
89 * Basic function to take errors from the DB library and output them to
90 * the logfile rather than stderr.
92 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 3)
93 static void db4_errfunc(const char *errpfx, const char *errmsg)
95 static void db4_errfunc(const DB_ENV *edbenv, const char *errpfx,
100 logthing(LOGTHING_DEBUG, "db4 error: %s:%s", errpfx, errmsg);
102 logthing(LOGTHING_DEBUG, "db4 error: %s", errmsg);
109 * starttrans - Start a transaction.
111 * Start a transaction. Intended to be used if we're about to perform many
112 * operations on the database to help speed it all up, or if we want
113 * something to only succeed if all relevant operations are successful.
115 static bool db4_starttrans(struct onak_dbctx *dbctx)
117 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
120 log_assert(privctx->dbenv != NULL);
121 log_assert(privctx->txn == NULL);
123 ret = privctx->dbenv->txn_begin(privctx->dbenv,
124 NULL, /* No parent transaction */
128 logthing(LOGTHING_CRITICAL,
129 "Error starting transaction: %s",
138 * endtrans - End a transaction.
140 * Ends a transaction.
142 static void db4_endtrans(struct onak_dbctx *dbctx)
144 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
147 log_assert(privctx->dbenv != NULL);
148 log_assert(privctx->txn != NULL);
150 ret = privctx->txn->commit(privctx->txn,
153 logthing(LOGTHING_CRITICAL,
154 "Error ending transaction: %s",
164 * db4_upgradedb - Upgrade a DB4 database
166 * Called if we discover we need to upgrade our DB4 database; ie if
167 * we're running with a newer version of db4 than the database was
170 static int db4_upgradedb(struct onak_dbctx *dbctx)
172 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
181 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
183 lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
184 if (lockfile_fd < 0) {
185 if (errno == EEXIST) {
186 while (stat(buf, &statbuf) == 0) ;
189 logthing(LOGTHING_CRITICAL, "Couldn't open database "
190 "update lock file: %s", strerror(errno));
194 snprintf(buf, sizeof(buf) - 1, "%d", getpid());
195 written = write(lockfile_fd, buf, strlen(buf));
197 if (written != strlen(buf)) {
198 logthing(LOGTHING_CRITICAL, "Couldn't write PID to lockfile: "
199 "%s", strerror(errno));
200 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
206 logthing(LOGTHING_NOTICE, "Upgrading DB4 database");
207 ret = db_env_create(&privctx->dbenv, 0);
209 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
210 privctx->dbenv->remove(privctx->dbenv, dbctx->config->location, 0);
211 privctx->dbenv = NULL;
213 for (i = 0; i < privctx->numdbs; i++) {
214 ret = db_create(&curdb, NULL, 0);
216 snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
217 dbctx->config->location, i);
218 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
219 curdb->upgrade(curdb, buf, 0);
220 curdb->close(curdb, 0);
222 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
228 ret = db_create(&curdb, NULL, 0);
230 snprintf(buf, sizeof(buf) - 1, "%s/worddb", dbctx->config->location);
231 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
232 curdb->upgrade(curdb, buf, 0);
233 curdb->close(curdb, 0);
235 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
240 ret = db_create(&curdb, NULL, 0);
242 snprintf(buf, sizeof(buf) - 1, "%s/id32db", dbctx->config->location);
243 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
244 curdb->upgrade(curdb, buf, 0);
245 curdb->close(curdb, 0);
247 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
252 ret = db_create(&curdb, NULL, 0);
254 snprintf(buf, sizeof(buf) - 1, "%s/id64db", dbctx->config->location);
255 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
256 curdb->upgrade(curdb, buf, 0);
257 curdb->close(curdb, 0);
259 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
264 ret = db_create(&curdb, NULL, 0);
266 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", dbctx->config->location);
267 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
268 curdb->upgrade(curdb, buf, 0);
269 curdb->close(curdb, 0);
271 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
276 ret = db_create(&curdb, NULL, 0);
278 snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", dbctx->config->location);
279 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
280 curdb->upgrade(curdb, buf, 0);
281 curdb->close(curdb, 0);
283 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
288 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
296 * fetch_key_fp - Given a fingerprint fetch the key from storage.
298 static int db4_fetch_key_fp(struct onak_dbctx *dbctx,
299 struct openpgp_fingerprint *fingerprint,
300 struct openpgp_publickey **publickey,
303 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
304 struct openpgp_packet_list *packets = NULL;
308 struct buffer_ctx fetchbuf;
309 struct openpgp_fingerprint subfp;
311 memset(&key, 0, sizeof(key));
312 memset(&data, 0, sizeof(data));
317 key.size = fingerprint->length;
318 key.data = fingerprint->fp;
321 db4_starttrans(dbctx);
324 ret = keydb_fp(privctx, fingerprint)->get(keydb_fp(privctx,
331 if (ret == DB_NOTFOUND) {
332 /* If we didn't find the key ID see if it's a subkey ID */
333 memset(&key, 0, sizeof(key));
334 memset(&data, 0, sizeof(data));
335 data.data = subfp.fp;
336 data.ulen = MAX_FINGERPRINT_LEN;
337 data.flags = DB_DBT_USERMEM;
338 key.data = fingerprint->fp;
339 key.size = fingerprint->length;
341 ret = privctx->subkeydb->get(privctx->subkeydb,
348 /* We got a subkey match; retrieve the actual key */
349 memset(&key, 0, sizeof(key));
350 key.size = subfp.length = data.size;
353 memset(&data, 0, sizeof(data));
357 ret = keydb_fp(privctx, &subfp)->get(
358 keydb_fp(privctx, &subfp),
367 fetchbuf.buffer = data.data;
369 fetchbuf.size = data.size;
370 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
372 parse_keys(packets, publickey);
373 free_packet_list(packets);
376 } else if (ret != DB_NOTFOUND) {
377 logthing(LOGTHING_ERROR,
378 "Problem retrieving key: %s",
390 * fetch_key_id - Given a keyid fetch the key from storage.
391 * @keyid: The keyid to fetch.
392 * @publickey: A pointer to a structure to return the key in.
393 * @intrans: If we're already in a transaction.
395 * We use the hex representation of the keyid as the filename to fetch the
396 * key from. The key is stored in the file as a binary OpenPGP stream of
397 * packets, so we can just use read_openpgp_stream() to read the packets
398 * in and then parse_keys() to parse the packets into a publickey
401 static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
402 struct openpgp_publickey **publickey,
405 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
406 struct openpgp_packet_list *packets = NULL;
411 uint32_t shortkeyid = 0;
412 struct openpgp_fingerprint fingerprint;
416 db4_starttrans(dbctx);
419 /* If the key ID fits in 32 bits assume it's a short key id */
420 if (keyid < 0x100000000LL) {
421 ret = privctx->id32db->cursor(privctx->id32db,
426 shortkeyid = keyid & 0xFFFFFFFF;
427 memset(&key, 0, sizeof(key));
428 memset(&data, 0, sizeof(data));
429 key.data = &shortkeyid;
430 key.size = sizeof(shortkeyid);
432 ret = privctx->id64db->cursor(privctx->id64db,
437 memset(&key, 0, sizeof(key));
438 memset(&data, 0, sizeof(data));
440 key.size = sizeof(keyid);
447 memset(&data, 0, sizeof(data));
448 data.ulen = MAX_FINGERPRINT_LEN;
449 data.data = fingerprint.fp;
450 data.flags = DB_DBT_USERMEM;
453 while (cursor->c_get(cursor, &key, &data,
454 first ? DB_SET : DB_NEXT_DUP) == 0) {
455 /* We got a match; retrieve the actual key */
456 fingerprint.length = data.size;
458 if (db4_fetch_key_fp(dbctx, &fingerprint,
462 memset(&data, 0, sizeof(data));
463 data.ulen = MAX_FINGERPRINT_LEN;
464 data.data = fingerprint.fp;
465 data.flags = DB_DBT_USERMEM;
468 cursor->c_close(cursor);
479 * fetch_key_text - Trys to find the keys that contain the supplied text.
480 * @search: The text to search for.
481 * @publickey: A pointer to a structure to return the key in.
483 * This function searches for the supplied text and returns the keys that
486 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
487 struct openpgp_publickey **publickey)
489 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
496 char *searchtext = NULL;
497 struct ll *wordlist = NULL;
498 struct ll *curword = NULL;
499 struct keyarray keylist = { NULL, 0, 0 };
500 struct keyarray newkeylist = { NULL, 0, 0 };
502 struct openpgp_fingerprint fingerprint;
505 searchtext = strdup(search);
506 wordlist = makewordlist(wordlist, searchtext);
508 for (curword = wordlist; curword != NULL; curword = curword->next) {
509 db4_starttrans(dbctx);
511 ret = privctx->worddb->cursor(privctx->worddb,
521 memset(&key, 0, sizeof(key));
522 memset(&data, 0, sizeof(data));
523 key.data = curword->object;
524 key.size = strlen(curword->object);
525 data.flags = DB_DBT_MALLOC;
526 ret = cursor->c_get(cursor,
530 while (ret == 0 && strncmp(key.data, curword->object,
532 ((char *) curword->object)[key.size] == 0) {
534 fingerprint.length = data.size;
535 memcpy(fingerprint.fp, data.data, data.size);
538 * Only add the keys containing this word if this is
539 * our first pass (ie we have no existing key list),
540 * or the key contained a previous word.
542 if (firstpass || array_find(&keylist, &fingerprint)) {
543 array_add(&newkeylist, &fingerprint);
549 ret = cursor->c_get(cursor,
554 array_free(&keylist);
555 keylist.keys = newkeylist.keys;
556 keylist.count = newkeylist.count;
557 keylist.size = newkeylist.size;
558 newkeylist.keys = NULL;
559 newkeylist.count = newkeylist.size = 0;
560 if (data.data != NULL) {
564 cursor->c_close(cursor);
569 llfree(wordlist, NULL);
572 if (keylist.count > config.maxkeys) {
573 keylist.count = config.maxkeys;
576 db4_starttrans(dbctx);
577 for (i = 0; i < keylist.count; i++) {
578 numkeys += db4_fetch_key_fp(dbctx, &keylist.keys[i],
582 array_free(&keylist);
591 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
592 const struct skshash *hash,
593 struct openpgp_publickey **publickey)
595 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
601 struct openpgp_fingerprint fingerprint;
603 ret = privctx->skshashdb->cursor(privctx->skshashdb,
612 memset(&key, 0, sizeof(key));
613 memset(&data, 0, sizeof(data));
614 key.data = (void *) hash->hash;
615 key.size = sizeof(hash->hash);
616 data.ulen = MAX_FINGERPRINT_LEN;
617 data.data = fingerprint.fp;
618 data.flags = DB_DBT_USERMEM;
620 ret = cursor->c_get(cursor,
626 fingerprint.length = data.size;
627 count = db4_fetch_key_fp(dbctx, &fingerprint,
631 cursor->c_close(cursor);
638 * delete_key - Given a keyid delete the key from storage.
639 * @fp: The fingerprint of the key to delete.
640 * @intrans: If we're already in a transaction.
642 * This function deletes a public key from whatever storage mechanism we
643 * are using. Returns 0 if the key existed.
645 static int db4_delete_key(struct onak_dbctx *dbctx,
646 struct openpgp_fingerprint *fp,
649 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
650 struct openpgp_publickey *publickey = NULL;
653 DBC *cursor64 = NULL;
654 uint32_t shortkeyid = 0;
655 uint64_t subkeyid = 0;
656 struct openpgp_fingerprint *subkeyids = NULL;
660 char *primary = NULL;
661 struct ll *wordlist = NULL;
662 struct ll *curword = NULL;
663 bool deadlock = false;
668 db4_starttrans(dbctx);
671 if (db4_fetch_key_fp(dbctx, fp, &publickey, true) == 0) {
678 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
683 * Walk through the uids removing the words from the worddb.
685 if (publickey != NULL) {
686 uids = keyuids(publickey, &primary);
689 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
690 wordlist = makewordlist(wordlist, uids[i]);
693 privctx->worddb->cursor(privctx->worddb,
698 for (curword = wordlist; curword != NULL && !deadlock;
699 curword = curword->next) {
701 * New style uses the fingerprint as the data
702 * Old (unsupported) style was the 64 bit keyid
704 memset(&key, 0, sizeof(key));
705 memset(&data, 0, sizeof(data));
706 key.data = curword->object;
707 key.size = strlen(key.data);
709 data.size = fp->length;
711 ret = cursor->c_get(cursor,
717 ret = cursor->c_del(cursor, 0);
720 if (ret != 0 && ret != DB_NOTFOUND) {
721 logthing(LOGTHING_ERROR,
722 "Problem deleting word: %s "
723 "(0x%016" PRIX64 ")",
726 if (ret == DB_LOCK_DEADLOCK) {
731 cursor->c_close(cursor);
735 * Free our UID and word lists.
737 llfree(wordlist, NULL);
738 for (i = 0; uids[i] != NULL; i++) {
747 privctx->id32db->cursor(privctx->id32db,
751 privctx->id64db->cursor(privctx->id64db,
756 /* 32 bit short key mapping to fingerprint */
757 shortkeyid = keyid & 0xFFFFFFFF;
759 memset(&key, 0, sizeof(key));
760 memset(&data, 0, sizeof(data));
761 key.data = &shortkeyid;
762 key.size = sizeof(shortkeyid);
764 data.size = fp->length;
766 ret = cursor->c_get(cursor,
772 ret = cursor->c_del(cursor, 0);
775 if (ret != 0 && ret != DB_NOTFOUND) {
776 logthing(LOGTHING_ERROR,
777 "Problem deleting short keyid: %s "
778 "(0x%016" PRIX64 ")",
781 if (ret == DB_LOCK_DEADLOCK) {
786 /* 64 bit key mapping to fingerprint */
787 memset(&key, 0, sizeof(key));
788 memset(&data, 0, sizeof(data));
790 key.size = sizeof(keyid);
792 data.size = fp->length;
794 ret = cursor64->c_get(cursor64,
800 ret = cursor64->c_del(cursor64, 0);
803 if (ret != 0 && ret != DB_NOTFOUND) {
804 logthing(LOGTHING_ERROR,
805 "Problem deleting keyid: %s "
806 "(0x%016" PRIX64 ")",
809 if (ret == DB_LOCK_DEADLOCK) {
814 subkeyids = keysubkeys(publickey);
816 while (subkeyids != NULL && subkeyids[i].length != 0) {
817 subkeyid = fingerprint2keyid(&subkeyids[i]);
818 memset(&key, 0, sizeof(key));
819 key.data = subkeyids[i].fp;
820 key.size = subkeyids[i].length;
821 ret = privctx->subkeydb->del(privctx->subkeydb,
822 privctx->txn, &key, 0);
823 if (ret != 0 && ret != DB_NOTFOUND) {
824 logthing(LOGTHING_ERROR,
825 "Problem deleting subkey id: %s "
826 "(0x%016" PRIX64 ")",
829 if (ret == DB_LOCK_DEADLOCK) {
834 shortkeyid = subkeyid & 0xFFFFFFFF;
836 /* Remove 32 bit keyid -> fingerprint mapping */
837 memset(&key, 0, sizeof(key));
838 memset(&data, 0, sizeof(data));
839 key.data = &shortkeyid;
840 key.size = sizeof(shortkeyid);
842 data.size = fp->length;
844 ret = cursor->c_get(cursor,
850 ret = cursor->c_del(cursor, 0);
853 if (ret != 0 && ret != DB_NOTFOUND) {
854 logthing(LOGTHING_ERROR,
855 "Problem deleting short keyid: %s "
856 "(0x%016" PRIX64 ")",
859 if (ret == DB_LOCK_DEADLOCK) {
864 /* Remove 64 bit keyid -> fingerprint mapping */
865 memset(&key, 0, sizeof(key));
866 memset(&data, 0, sizeof(data));
867 key.data = &subkeyid;
868 key.size = sizeof(subkeyid);
870 data.size = fp->length;
872 ret = cursor64->c_get(cursor64,
878 ret = cursor64->c_del(cursor64, 0);
881 if (ret != 0 && ret != DB_NOTFOUND) {
882 logthing(LOGTHING_ERROR,
883 "Problem deleting keyid: %s "
884 "(0x%016" PRIX64 ")",
887 if (ret == DB_LOCK_DEADLOCK) {
893 if (subkeyids != NULL) {
897 cursor64->c_close(cursor64);
899 cursor->c_close(cursor);
904 ret = privctx->skshashdb->cursor(privctx->skshashdb,
909 get_skshash(publickey, &hash);
911 /* Remove SKS hash -> fingerprint mapping */
912 memset(&key, 0, sizeof(key));
913 memset(&data, 0, sizeof(data));
914 key.data = hash.hash;
915 key.size = sizeof(hash.hash);
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 skshash: %s "
931 "(0x%016" PRIX64 ")",
934 if (ret == DB_LOCK_DEADLOCK) {
939 cursor->c_close(cursor);
943 free_publickey(publickey);
948 key.size = fp->length;
950 keydb_fp(privctx, fp)->del(keydb_fp(privctx, fp),
960 return deadlock ? (-1) : (ret == DB_NOTFOUND);
964 * store_key - Takes a key and stores it.
965 * @publickey: A pointer to the public key to store.
966 * @intrans: If we're already in a transaction.
967 * @update: If true the key exists and should be updated.
969 * Again we just use the hex representation of the keyid as the filename
970 * to store the key to. We flatten the public key to a list of OpenPGP
971 * packets and then use write_openpgp_stream() to write the stream out to
972 * the file. If update is true then we delete the old key first, otherwise
973 * we trust that it doesn't exist.
975 static int db4_store_key(struct onak_dbctx *dbctx,
976 struct openpgp_publickey *publickey, bool intrans,
979 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
980 struct openpgp_packet_list *packets = NULL;
981 struct openpgp_packet_list *list_end = NULL;
982 struct openpgp_publickey *next = NULL;
985 struct buffer_ctx storebuf;
989 uint32_t shortkeyid = 0;
990 struct openpgp_fingerprint *subkeyids = NULL;
992 char *primary = NULL;
993 struct ll *wordlist = NULL;
994 struct ll *curword = NULL;
995 bool deadlock = false;
997 struct openpgp_fingerprint fingerprint;
999 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
1000 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
1004 if (get_fingerprint(publickey->publickey, &fingerprint) != ONAK_E_OK) {
1005 logthing(LOGTHING_ERROR, "Couldn't find fingerprint for key.");
1010 db4_starttrans(dbctx);
1014 * Delete the key if we already have it.
1016 * TODO: Can we optimize this perhaps? Possibly when other data is
1017 * involved as well? I suspect this is easiest and doesn't make a lot
1018 * of difference though - the largest chunk of data is the keydata and
1019 * it definitely needs updated.
1022 deadlock = (db4_delete_key(dbctx, &fingerprint, true) == -1);
1026 * Convert the key to a flat set of binary data.
1029 next = publickey->next;
1030 publickey->next = NULL;
1031 flatten_publickey(publickey, &packets, &list_end);
1032 publickey->next = next;
1034 storebuf.offset = 0;
1035 storebuf.size = 8192;
1036 storebuf.buffer = malloc(8192);
1038 write_openpgp_stream(buffer_putchar, &storebuf, packets);
1041 * Now we have the key data store it in the DB; the fingerprint
1044 memset(&key, 0, sizeof(key));
1045 memset(&data, 0, sizeof(data));
1046 key.data = fingerprint.fp;
1047 key.size = fingerprint.length;
1048 data.size = storebuf.offset;
1049 data.data = storebuf.buffer;
1051 ret = keydb_fp(privctx, &fingerprint)->put(
1052 keydb_fp(privctx, &fingerprint),
1058 logthing(LOGTHING_ERROR,
1059 "Problem storing key: %s",
1061 if (ret == DB_LOCK_DEADLOCK) {
1066 free(storebuf.buffer);
1067 storebuf.buffer = NULL;
1069 storebuf.offset = 0;
1071 free_packet_list(packets);
1076 * Walk through our uids storing the words into the db with the
1080 uids = keyuids(publickey, &primary);
1083 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1084 wordlist = makewordlist(wordlist, uids[i]);
1087 for (curword = wordlist; curword != NULL && !deadlock;
1088 curword = curword->next) {
1089 memset(&key, 0, sizeof(key));
1090 memset(&data, 0, sizeof(data));
1091 key.data = curword->object;
1092 key.size = strlen(key.data);
1093 data.data = fingerprint.fp;
1094 data.size = fingerprint.length;
1096 ret = privctx->worddb->put(privctx->worddb,
1102 logthing(LOGTHING_ERROR,
1103 "Problem storing word: %s",
1105 if (ret == DB_LOCK_DEADLOCK) {
1112 * Free our UID and word lists.
1114 llfree(wordlist, NULL);
1115 for (i = 0; uids[i] != NULL; i++) {
1124 * Write the truncated 32 bit keyid so we can lookup the fingerprint
1128 shortkeyid = keyid & 0xFFFFFFFF;
1130 memset(&key, 0, sizeof(key));
1131 memset(&data, 0, sizeof(data));
1132 key.data = &shortkeyid;
1133 key.size = sizeof(shortkeyid);
1134 data.data = fingerprint.fp;
1135 data.size = fingerprint.length;
1137 ret = privctx->id32db->put(privctx->id32db,
1143 logthing(LOGTHING_ERROR,
1144 "Problem storing short keyid: %s",
1146 if (ret == DB_LOCK_DEADLOCK) {
1153 * Write the 64 bit keyid so we can lookup the fingerprint for
1157 memset(&key, 0, sizeof(key));
1158 memset(&data, 0, sizeof(data));
1160 key.size = sizeof(keyid);
1161 data.data = fingerprint.fp;
1162 data.size = fingerprint.length;
1164 ret = privctx->id64db->put(privctx->id64db,
1170 logthing(LOGTHING_ERROR,
1171 "Problem storing keyid: %s",
1173 if (ret == DB_LOCK_DEADLOCK) {
1180 subkeyids = keysubkeys(publickey);
1182 while (subkeyids != NULL && subkeyids[i].length != 0) {
1183 /* Store the subkey ID -> main key fp mapping */
1184 memset(&key, 0, sizeof(key));
1185 memset(&data, 0, sizeof(data));
1186 key.data = subkeyids[i].fp;
1187 key.size = subkeyids[i].length;
1188 data.data = fingerprint.fp;
1189 data.size = fingerprint.length;
1191 ret = privctx->subkeydb->put(privctx->subkeydb,
1197 logthing(LOGTHING_ERROR,
1198 "Problem storing subkey keyid: %s",
1200 if (ret == DB_LOCK_DEADLOCK) {
1205 /* Store the 64 bit subkey ID -> main key fp mapping */
1206 memset(&key, 0, sizeof(key));
1207 memset(&data, 0, sizeof(data));
1209 keyid = fingerprint2keyid(&subkeyids[i]);
1211 key.size = sizeof(keyid);
1212 data.data = fingerprint.fp;
1213 data.size = fingerprint.length;
1215 ret = privctx->id64db->put(privctx->id64db,
1221 logthing(LOGTHING_ERROR,
1222 "Problem storing keyid: %s",
1224 if (ret == DB_LOCK_DEADLOCK) {
1229 /* Store the short subkey ID -> main key fp mapping */
1230 shortkeyid = keyid & 0xFFFFFFFF;
1232 memset(&key, 0, sizeof(key));
1233 memset(&data, 0, sizeof(data));
1234 key.data = &shortkeyid;
1235 key.size = sizeof(shortkeyid);
1236 data.data = fingerprint.fp;
1237 data.size = fingerprint.length;
1239 ret = privctx->id32db->put(privctx->id32db,
1245 logthing(LOGTHING_ERROR,
1246 "Problem storing short keyid: %s",
1248 if (ret == DB_LOCK_DEADLOCK) {
1254 if (subkeyids != NULL) {
1261 get_skshash(publickey, &hash);
1262 memset(&key, 0, sizeof(key));
1263 memset(&data, 0, sizeof(data));
1264 key.data = hash.hash;
1265 key.size = sizeof(hash.hash);
1266 data.data = fingerprint.fp;
1267 data.size = fingerprint.length;
1269 ret = privctx->skshashdb->put(privctx->skshashdb,
1275 logthing(LOGTHING_ERROR,
1276 "Problem storing SKS hash: %s",
1278 if (ret == DB_LOCK_DEADLOCK) {
1285 db4_endtrans(dbctx);
1288 return deadlock ? -1 : 0 ;
1292 * iterate_keys - call a function once for each key in the db.
1293 * @iterfunc: The function to call.
1294 * @ctx: A context pointer
1296 * Calls iterfunc once for each key in the database. ctx is passed
1297 * unaltered to iterfunc. This function is intended to aid database dumps
1298 * and statistic calculations.
1300 * Returns the number of keys we iterated over.
1302 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1303 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1306 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1312 struct buffer_ctx fetchbuf;
1313 struct openpgp_packet_list *packets = NULL;
1314 struct openpgp_publickey *key = NULL;
1316 for (i = 0; i < privctx->numdbs; i++) {
1317 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1326 memset(&dbkey, 0, sizeof(dbkey));
1327 memset(&data, 0, sizeof(data));
1328 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1330 fetchbuf.buffer = data.data;
1331 fetchbuf.offset = 0;
1332 fetchbuf.size = data.size;
1333 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1335 parse_keys(packets, &key);
1339 free_publickey(key);
1341 free_packet_list(packets);
1344 memset(&dbkey, 0, sizeof(dbkey));
1345 memset(&data, 0, sizeof(data));
1346 ret = cursor->c_get(cursor, &dbkey, &data,
1350 if (ret != DB_NOTFOUND) {
1351 logthing(LOGTHING_ERROR,
1352 "Problem reading key: %s",
1356 cursor->c_close(cursor);
1364 * Include the basic keydb routines.
1366 #define NEED_GETKEYSIGS 1
1367 #define NEED_KEYID2UID 1
1368 #define NEED_UPDATEKEYS 1
1372 * cleanupdb - De-initialize the key database.
1374 * This function should be called upon program exit to allow the DB to
1375 * cleanup after itself.
1377 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1379 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1382 if (privctx->dbenv != NULL) {
1383 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1384 if (privctx->subkeydb != NULL) {
1385 privctx->subkeydb->close(privctx->subkeydb, 0);
1386 privctx->subkeydb = NULL;
1388 if (privctx->skshashdb != NULL) {
1389 privctx->skshashdb->close(privctx->skshashdb, 0);
1390 privctx->skshashdb = NULL;
1392 if (privctx->id64db != NULL) {
1393 privctx->id64db->close(privctx->id64db, 0);
1394 privctx->id64db = NULL;
1396 if (privctx->id32db != NULL) {
1397 privctx->id32db->close(privctx->id32db, 0);
1398 privctx->id32db = NULL;
1400 if (privctx->worddb != NULL) {
1401 privctx->worddb->close(privctx->worddb, 0);
1402 privctx->worddb = NULL;
1404 for (i = 0; i < privctx->numdbs; i++) {
1405 if (privctx->dbconns[i] != NULL) {
1406 privctx->dbconns[i]->close(privctx->dbconns[i],
1408 privctx->dbconns[i] = NULL;
1411 free(privctx->dbconns);
1412 privctx->dbconns = NULL;
1413 privctx->dbenv->close(privctx->dbenv, 0);
1414 privctx->dbenv = NULL;
1423 * initdb - Initialize the key database.
1425 * This function should be called before any of the other functions in
1426 * this file are called in order to allow the DB to be initialized ready
1429 struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
1436 struct stat statbuf;
1438 struct onak_dbctx *dbctx;
1439 struct onak_db4_dbctx *privctx;
1441 dbctx = malloc(sizeof(*dbctx));
1442 if (dbctx == NULL) {
1445 dbctx->config = dbcfg;
1446 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1447 if (privctx == NULL) {
1452 /* Default to 16 key data DBs */
1453 privctx->numdbs = 16;
1455 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
1457 ret = stat(buf, &statbuf);
1458 while ((ret == 0) || (errno != ENOENT)) {
1460 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1461 "lock file: %s (%d)", strerror(errno), ret);
1464 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1466 ret = stat(buf, &statbuf);
1470 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
1471 numdb = fopen(buf, "r");
1472 if (numdb != NULL) {
1473 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1474 privctx->numdbs = atoi(buf);
1477 } else if (!readonly) {
1478 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1480 numdb = fopen(buf, "w");
1481 if (numdb != NULL) {
1482 fprintf(numdb, "%d", privctx->numdbs);
1485 logthing(LOGTHING_ERROR,
1486 "Couldn't write num_keydb: %s",
1491 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1492 if (privctx->dbconns == NULL) {
1493 logthing(LOGTHING_CRITICAL,
1494 "Couldn't allocate memory for dbconns");
1499 ret = db_env_create(&privctx->dbenv, 0);
1501 logthing(LOGTHING_CRITICAL,
1502 "db_env_create: %s", db_strerror(ret));
1507 * Up the number of locks we're allowed at once. We base this on
1508 * the maximum number of keys we're going to return.
1511 maxlocks = config.maxkeys * 16;
1512 if (maxlocks < 1000) {
1515 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1516 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1520 * Enable deadlock detection so that we don't block indefinitely on
1521 * anything. What we really want is simple 2 state locks, but I'm not
1522 * sure how to make the standard DB functions do that yet.
1525 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1526 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1528 logthing(LOGTHING_CRITICAL,
1529 "db_env_create: %s", db_strerror(ret));
1534 ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
1535 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1539 #ifdef DB_VERSION_MISMATCH
1540 if (ret == DB_VERSION_MISMATCH) {
1541 privctx->dbenv->close(privctx->dbenv, 0);
1542 privctx->dbenv = NULL;
1543 ret = db4_upgradedb(dbctx);
1545 ret = db_env_create(&privctx->dbenv, 0);
1548 privctx->dbenv->set_errcall(privctx->dbenv,
1550 privctx->dbenv->set_lk_detect(privctx->dbenv,
1552 ret = privctx->dbenv->open(privctx->dbenv,
1554 DB_INIT_LOG | DB_INIT_MPOOL |
1555 DB_INIT_LOCK | DB_INIT_TXN |
1556 DB_CREATE | DB_RECOVER,
1560 privctx->dbenv->txn_checkpoint(
1570 logthing(LOGTHING_CRITICAL,
1571 "Error opening db environment: %s (%s)",
1574 if (privctx->dbenv != NULL) {
1575 privctx->dbenv->close(privctx->dbenv, 0);
1576 privctx->dbenv = NULL;
1582 db4_starttrans(dbctx);
1584 for (i = 0; !ret && i < privctx->numdbs; i++) {
1585 ret = db_create(&privctx->dbconns[i],
1588 logthing(LOGTHING_CRITICAL,
1589 "db_create: %s", db_strerror(ret));
1593 snprintf(buf, 1023, "keydb.%d.db", i);
1598 ret = privctx->dbconns[i]->open(
1599 privctx->dbconns[i],
1607 logthing(LOGTHING_CRITICAL,
1608 "Error opening key database:"
1618 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1620 logthing(LOGTHING_CRITICAL, "db_create: %s",
1626 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1630 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1631 "worddb", "worddb", DB_BTREE,
1635 logthing(LOGTHING_CRITICAL,
1636 "Error opening word database: %s (%s)",
1643 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1645 logthing(LOGTHING_CRITICAL, "db_create: %s",
1651 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1655 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1656 "id32db", "id32db", DB_HASH,
1660 logthing(LOGTHING_CRITICAL,
1661 "Error opening id32 database: %s (%s)",
1668 ret = db_create(&privctx->id64db, privctx->dbenv, 0);
1670 logthing(LOGTHING_CRITICAL, "db_create: %s",
1676 ret = privctx->id64db->set_flags(privctx->id64db, DB_DUP);
1680 ret = privctx->id64db->open(privctx->id64db, privctx->txn,
1681 "id64db", "id64db", DB_HASH,
1685 logthing(LOGTHING_CRITICAL,
1686 "Error opening id64 database: %s (%s)",
1693 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1695 logthing(LOGTHING_CRITICAL, "db_create: %s",
1701 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1703 "skshashdb", DB_HASH,
1707 logthing(LOGTHING_CRITICAL,
1708 "Error opening skshash database: %s (%s)",
1715 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1717 logthing(LOGTHING_CRITICAL, "db_create: %s",
1723 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1724 "subkeydb", "subkeydb",
1729 logthing(LOGTHING_CRITICAL,
1730 "Error opening subkey database: %s (%s)",
1736 if (privctx->txn != NULL) {
1737 db4_endtrans(dbctx);
1741 db4_cleanupdb(dbctx);
1742 logthing(LOGTHING_CRITICAL,
1743 "Error opening database; exiting");
1747 dbctx->cleanupdb = db4_cleanupdb;
1748 dbctx->starttrans = db4_starttrans;
1749 dbctx->endtrans = db4_endtrans;
1750 dbctx->fetch_key_id = db4_fetch_key_id;
1751 dbctx->fetch_key_fp = db4_fetch_key_fp;
1752 dbctx->fetch_key_text = db4_fetch_key_text;
1753 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1754 dbctx->store_key = db4_store_key;
1755 dbctx->update_keys = generic_update_keys;
1756 dbctx->delete_key = db4_delete_key;
1757 dbctx->getkeysigs = generic_getkeysigs;
1758 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1759 dbctx->keyid2uid = generic_keyid2uid;
1760 dbctx->iterate_keys = db4_iterate_keys;