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;
410 uint32_t shortkeyid = 0;
411 struct openpgp_fingerprint fingerprint;
415 db4_starttrans(dbctx);
418 /* If the key ID fits in 32 bits assume it's a short key id */
419 if (keyid < 0x100000000LL) {
420 ret = privctx->id32db->cursor(privctx->id32db,
425 shortkeyid = keyid & 0xFFFFFFFF;
426 memset(&key, 0, sizeof(key));
427 memset(&data, 0, sizeof(data));
428 key.data = &shortkeyid;
429 key.size = sizeof(shortkeyid);
431 ret = privctx->id64db->cursor(privctx->id64db,
436 memset(&key, 0, sizeof(key));
437 memset(&data, 0, sizeof(data));
439 key.size = sizeof(keyid);
446 memset(&data, 0, sizeof(data));
447 data.ulen = MAX_FINGERPRINT_LEN;
448 data.data = fingerprint.fp;
449 data.flags = DB_DBT_USERMEM;
452 while (cursor->c_get(cursor, &key, &data,
453 first ? DB_SET : DB_NEXT_DUP) == 0) {
454 /* We got a match; retrieve the actual key */
455 fingerprint.length = data.size;
457 if (db4_fetch_key_fp(dbctx, &fingerprint,
461 memset(&data, 0, sizeof(data));
462 data.ulen = MAX_FINGERPRINT_LEN;
463 data.data = fingerprint.fp;
464 data.flags = DB_DBT_USERMEM;
467 cursor->c_close(cursor);
478 * fetch_key_text - Trys to find the keys that contain the supplied text.
479 * @search: The text to search for.
480 * @publickey: A pointer to a structure to return the key in.
482 * This function searches for the supplied text and returns the keys that
485 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
486 struct openpgp_publickey **publickey)
488 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
494 char *searchtext = NULL;
495 struct ll *wordlist = NULL;
496 struct ll *curword = NULL;
497 struct keyarray keylist = { NULL, 0, 0 };
498 struct keyarray newkeylist = { NULL, 0, 0 };
500 struct openpgp_fingerprint fingerprint;
503 searchtext = strdup(search);
504 wordlist = makewordlist(wordlist, searchtext);
506 for (curword = wordlist; curword != NULL; curword = curword->next) {
507 db4_starttrans(dbctx);
509 ret = privctx->worddb->cursor(privctx->worddb,
519 memset(&key, 0, sizeof(key));
520 memset(&data, 0, sizeof(data));
521 key.data = curword->object;
522 key.size = strlen(curword->object);
523 data.flags = DB_DBT_MALLOC;
524 ret = cursor->c_get(cursor,
528 while (ret == 0 && strncmp(key.data, curword->object,
530 ((char *) curword->object)[key.size] == 0) {
532 fingerprint.length = data.size;
533 memcpy(fingerprint.fp, data.data, data.size);
536 * Only add the keys containing this word if this is
537 * our first pass (ie we have no existing key list),
538 * or the key contained a previous word.
540 if (firstpass || array_find(&keylist, &fingerprint)) {
541 array_add(&newkeylist, &fingerprint);
547 ret = cursor->c_get(cursor,
552 array_free(&keylist);
553 keylist.keys = newkeylist.keys;
554 keylist.count = newkeylist.count;
555 keylist.size = newkeylist.size;
556 newkeylist.keys = NULL;
557 newkeylist.count = newkeylist.size = 0;
558 if (data.data != NULL) {
562 cursor->c_close(cursor);
567 llfree(wordlist, NULL);
570 if (keylist.count > config.maxkeys) {
571 keylist.count = config.maxkeys;
574 db4_starttrans(dbctx);
575 for (i = 0; i < keylist.count; i++) {
576 numkeys += db4_fetch_key_fp(dbctx, &keylist.keys[i],
580 array_free(&keylist);
589 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
590 const struct skshash *hash,
591 struct openpgp_publickey **publickey)
593 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
598 struct openpgp_fingerprint fingerprint;
600 ret = privctx->skshashdb->cursor(privctx->skshashdb,
609 memset(&key, 0, sizeof(key));
610 memset(&data, 0, sizeof(data));
611 key.data = (void *) hash->hash;
612 key.size = sizeof(hash->hash);
613 data.ulen = MAX_FINGERPRINT_LEN;
614 data.data = fingerprint.fp;
615 data.flags = DB_DBT_USERMEM;
617 ret = cursor->c_get(cursor,
623 fingerprint.length = data.size;
624 count = db4_fetch_key_fp(dbctx, &fingerprint,
628 cursor->c_close(cursor);
635 * delete_key - Given a keyid delete the key from storage.
636 * @fp: The fingerprint of the key to delete.
637 * @intrans: If we're already in a transaction.
639 * This function deletes a public key from whatever storage mechanism we
640 * are using. Returns 0 if the key existed.
642 static int db4_delete_key(struct onak_dbctx *dbctx,
643 struct openpgp_fingerprint *fp,
646 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
647 struct openpgp_publickey *publickey = NULL;
650 DBC *cursor64 = NULL;
651 uint32_t shortkeyid = 0;
652 uint64_t subkeyid = 0;
653 struct openpgp_fingerprint *subkeyids = NULL;
657 char *primary = NULL;
658 struct ll *wordlist = NULL;
659 struct ll *curword = NULL;
660 bool deadlock = false;
665 db4_starttrans(dbctx);
668 if (db4_fetch_key_fp(dbctx, fp, &publickey, true) == 0) {
675 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
680 * Walk through the uids removing the words from the worddb.
682 if (publickey != NULL) {
683 uids = keyuids(publickey, &primary);
686 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
687 wordlist = makewordlist(wordlist, uids[i]);
690 privctx->worddb->cursor(privctx->worddb,
695 for (curword = wordlist; curword != NULL && !deadlock;
696 curword = curword->next) {
698 * New style uses the fingerprint as the data
699 * Old (unsupported) style was the 64 bit keyid
701 memset(&key, 0, sizeof(key));
702 memset(&data, 0, sizeof(data));
703 key.data = curword->object;
704 key.size = strlen(key.data);
706 data.size = fp->length;
708 ret = cursor->c_get(cursor,
714 ret = cursor->c_del(cursor, 0);
717 if (ret != 0 && ret != DB_NOTFOUND) {
718 logthing(LOGTHING_ERROR,
719 "Problem deleting word: %s "
720 "(0x%016" PRIX64 ")",
723 if (ret == DB_LOCK_DEADLOCK) {
728 cursor->c_close(cursor);
732 * Free our UID and word lists.
734 llfree(wordlist, NULL);
735 for (i = 0; uids[i] != NULL; i++) {
744 privctx->id32db->cursor(privctx->id32db,
748 privctx->id64db->cursor(privctx->id64db,
753 /* 32 bit short key mapping to fingerprint */
754 shortkeyid = keyid & 0xFFFFFFFF;
756 memset(&key, 0, sizeof(key));
757 memset(&data, 0, sizeof(data));
758 key.data = &shortkeyid;
759 key.size = sizeof(shortkeyid);
761 data.size = fp->length;
763 ret = cursor->c_get(cursor,
769 ret = cursor->c_del(cursor, 0);
772 if (ret != 0 && ret != DB_NOTFOUND) {
773 logthing(LOGTHING_ERROR,
774 "Problem deleting short keyid: %s "
775 "(0x%016" PRIX64 ")",
778 if (ret == DB_LOCK_DEADLOCK) {
783 /* 64 bit key mapping to fingerprint */
784 memset(&key, 0, sizeof(key));
785 memset(&data, 0, sizeof(data));
787 key.size = sizeof(keyid);
789 data.size = fp->length;
791 ret = cursor64->c_get(cursor64,
797 ret = cursor64->c_del(cursor64, 0);
800 if (ret != 0 && ret != DB_NOTFOUND) {
801 logthing(LOGTHING_ERROR,
802 "Problem deleting keyid: %s "
803 "(0x%016" PRIX64 ")",
806 if (ret == DB_LOCK_DEADLOCK) {
811 subkeyids = keysubkeys(publickey);
813 while (subkeyids != NULL && subkeyids[i].length != 0) {
814 subkeyid = fingerprint2keyid(&subkeyids[i]);
815 memset(&key, 0, sizeof(key));
816 key.data = subkeyids[i].fp;
817 key.size = subkeyids[i].length;
818 ret = privctx->subkeydb->del(privctx->subkeydb,
819 privctx->txn, &key, 0);
820 if (ret != 0 && ret != DB_NOTFOUND) {
821 logthing(LOGTHING_ERROR,
822 "Problem deleting subkey id: %s "
823 "(0x%016" PRIX64 ")",
826 if (ret == DB_LOCK_DEADLOCK) {
831 shortkeyid = subkeyid & 0xFFFFFFFF;
833 /* Remove 32 bit keyid -> fingerprint mapping */
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 /* Remove 64 bit keyid -> fingerprint mapping */
862 memset(&key, 0, sizeof(key));
863 memset(&data, 0, sizeof(data));
864 key.data = &subkeyid;
865 key.size = sizeof(subkeyid);
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) {
890 if (subkeyids != NULL) {
894 cursor64->c_close(cursor64);
896 cursor->c_close(cursor);
901 ret = privctx->skshashdb->cursor(privctx->skshashdb,
906 get_skshash(publickey, &hash);
908 /* Remove SKS hash -> fingerprint mapping */
909 memset(&key, 0, sizeof(key));
910 memset(&data, 0, sizeof(data));
911 key.data = hash.hash;
912 key.size = sizeof(hash.hash);
914 data.size = fp->length;
916 ret = cursor->c_get(cursor,
922 ret = cursor->c_del(cursor, 0);
925 if (ret != 0 && ret != DB_NOTFOUND) {
926 logthing(LOGTHING_ERROR,
927 "Problem deleting skshash: %s "
928 "(0x%016" PRIX64 ")",
931 if (ret == DB_LOCK_DEADLOCK) {
936 cursor->c_close(cursor);
940 free_publickey(publickey);
945 key.size = fp->length;
947 keydb_fp(privctx, fp)->del(keydb_fp(privctx, fp),
957 return deadlock ? (-1) : (ret == DB_NOTFOUND);
961 * store_key - Takes a key and stores it.
962 * @publickey: A pointer to the public key to store.
963 * @intrans: If we're already in a transaction.
964 * @update: If true the key exists and should be updated.
966 * Again we just use the hex representation of the keyid as the filename
967 * to store the key to. We flatten the public key to a list of OpenPGP
968 * packets and then use write_openpgp_stream() to write the stream out to
969 * the file. If update is true then we delete the old key first, otherwise
970 * we trust that it doesn't exist.
972 static int db4_store_key(struct onak_dbctx *dbctx,
973 struct openpgp_publickey *publickey, bool intrans,
976 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
977 struct openpgp_packet_list *packets = NULL;
978 struct openpgp_packet_list *list_end = NULL;
979 struct openpgp_publickey *next = NULL;
982 struct buffer_ctx storebuf;
986 uint32_t shortkeyid = 0;
987 struct openpgp_fingerprint *subkeyids = NULL;
989 char *primary = NULL;
990 struct ll *wordlist = NULL;
991 struct ll *curword = NULL;
992 bool deadlock = false;
994 struct openpgp_fingerprint fingerprint;
996 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
997 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
1001 if (get_fingerprint(publickey->publickey, &fingerprint) != ONAK_E_OK) {
1002 logthing(LOGTHING_ERROR, "Couldn't find fingerprint for key.");
1007 db4_starttrans(dbctx);
1011 * Delete the key if we already have it.
1013 * TODO: Can we optimize this perhaps? Possibly when other data is
1014 * involved as well? I suspect this is easiest and doesn't make a lot
1015 * of difference though - the largest chunk of data is the keydata and
1016 * it definitely needs updated.
1019 deadlock = (db4_delete_key(dbctx, &fingerprint, true) == -1);
1023 * Convert the key to a flat set of binary data.
1026 next = publickey->next;
1027 publickey->next = NULL;
1028 flatten_publickey(publickey, &packets, &list_end);
1029 publickey->next = next;
1031 storebuf.offset = 0;
1032 storebuf.size = 8192;
1033 storebuf.buffer = malloc(8192);
1035 write_openpgp_stream(buffer_putchar, &storebuf, packets);
1038 * Now we have the key data store it in the DB; the fingerprint
1041 memset(&key, 0, sizeof(key));
1042 memset(&data, 0, sizeof(data));
1043 key.data = fingerprint.fp;
1044 key.size = fingerprint.length;
1045 data.size = storebuf.offset;
1046 data.data = storebuf.buffer;
1048 ret = keydb_fp(privctx, &fingerprint)->put(
1049 keydb_fp(privctx, &fingerprint),
1055 logthing(LOGTHING_ERROR,
1056 "Problem storing key: %s",
1058 if (ret == DB_LOCK_DEADLOCK) {
1063 free(storebuf.buffer);
1064 storebuf.buffer = NULL;
1066 storebuf.offset = 0;
1068 free_packet_list(packets);
1073 * Walk through our uids storing the words into the db with the
1077 uids = keyuids(publickey, &primary);
1080 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1081 wordlist = makewordlist(wordlist, uids[i]);
1084 for (curword = wordlist; curword != NULL && !deadlock;
1085 curword = curword->next) {
1086 memset(&key, 0, sizeof(key));
1087 memset(&data, 0, sizeof(data));
1088 key.data = curword->object;
1089 key.size = strlen(key.data);
1090 data.data = fingerprint.fp;
1091 data.size = fingerprint.length;
1093 ret = privctx->worddb->put(privctx->worddb,
1099 logthing(LOGTHING_ERROR,
1100 "Problem storing word: %s",
1102 if (ret == DB_LOCK_DEADLOCK) {
1109 * Free our UID and word lists.
1111 llfree(wordlist, NULL);
1112 for (i = 0; uids[i] != NULL; i++) {
1121 * Write the truncated 32 bit keyid so we can lookup the fingerprint
1125 shortkeyid = keyid & 0xFFFFFFFF;
1127 memset(&key, 0, sizeof(key));
1128 memset(&data, 0, sizeof(data));
1129 key.data = &shortkeyid;
1130 key.size = sizeof(shortkeyid);
1131 data.data = fingerprint.fp;
1132 data.size = fingerprint.length;
1134 ret = privctx->id32db->put(privctx->id32db,
1140 logthing(LOGTHING_ERROR,
1141 "Problem storing short keyid: %s",
1143 if (ret == DB_LOCK_DEADLOCK) {
1150 * Write the 64 bit keyid so we can lookup the fingerprint for
1154 memset(&key, 0, sizeof(key));
1155 memset(&data, 0, sizeof(data));
1157 key.size = sizeof(keyid);
1158 data.data = fingerprint.fp;
1159 data.size = fingerprint.length;
1161 ret = privctx->id64db->put(privctx->id64db,
1167 logthing(LOGTHING_ERROR,
1168 "Problem storing keyid: %s",
1170 if (ret == DB_LOCK_DEADLOCK) {
1177 subkeyids = keysubkeys(publickey);
1179 while (subkeyids != NULL && subkeyids[i].length != 0) {
1180 /* Store the subkey ID -> main key fp mapping */
1181 memset(&key, 0, sizeof(key));
1182 memset(&data, 0, sizeof(data));
1183 key.data = subkeyids[i].fp;
1184 key.size = subkeyids[i].length;
1185 data.data = fingerprint.fp;
1186 data.size = fingerprint.length;
1188 ret = privctx->subkeydb->put(privctx->subkeydb,
1194 logthing(LOGTHING_ERROR,
1195 "Problem storing subkey keyid: %s",
1197 if (ret == DB_LOCK_DEADLOCK) {
1202 /* Store the 64 bit subkey ID -> main key fp mapping */
1203 memset(&key, 0, sizeof(key));
1204 memset(&data, 0, sizeof(data));
1206 keyid = fingerprint2keyid(&subkeyids[i]);
1208 key.size = sizeof(keyid);
1209 data.data = fingerprint.fp;
1210 data.size = fingerprint.length;
1212 ret = privctx->id64db->put(privctx->id64db,
1218 logthing(LOGTHING_ERROR,
1219 "Problem storing keyid: %s",
1221 if (ret == DB_LOCK_DEADLOCK) {
1226 /* Store the short subkey ID -> main key fp mapping */
1227 shortkeyid = keyid & 0xFFFFFFFF;
1229 memset(&key, 0, sizeof(key));
1230 memset(&data, 0, sizeof(data));
1231 key.data = &shortkeyid;
1232 key.size = sizeof(shortkeyid);
1233 data.data = fingerprint.fp;
1234 data.size = fingerprint.length;
1236 ret = privctx->id32db->put(privctx->id32db,
1242 logthing(LOGTHING_ERROR,
1243 "Problem storing short keyid: %s",
1245 if (ret == DB_LOCK_DEADLOCK) {
1251 if (subkeyids != NULL) {
1258 get_skshash(publickey, &hash);
1259 memset(&key, 0, sizeof(key));
1260 memset(&data, 0, sizeof(data));
1261 key.data = hash.hash;
1262 key.size = sizeof(hash.hash);
1263 data.data = fingerprint.fp;
1264 data.size = fingerprint.length;
1266 ret = privctx->skshashdb->put(privctx->skshashdb,
1272 logthing(LOGTHING_ERROR,
1273 "Problem storing SKS hash: %s",
1275 if (ret == DB_LOCK_DEADLOCK) {
1282 db4_endtrans(dbctx);
1285 return deadlock ? -1 : 0 ;
1289 * iterate_keys - call a function once for each key in the db.
1290 * @iterfunc: The function to call.
1291 * @ctx: A context pointer
1293 * Calls iterfunc once for each key in the database. ctx is passed
1294 * unaltered to iterfunc. This function is intended to aid database dumps
1295 * and statistic calculations.
1297 * Returns the number of keys we iterated over.
1299 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1300 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1303 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1309 struct buffer_ctx fetchbuf;
1310 struct openpgp_packet_list *packets = NULL;
1311 struct openpgp_publickey *key = NULL;
1313 for (i = 0; i < privctx->numdbs; i++) {
1314 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1323 memset(&dbkey, 0, sizeof(dbkey));
1324 memset(&data, 0, sizeof(data));
1325 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1327 fetchbuf.buffer = data.data;
1328 fetchbuf.offset = 0;
1329 fetchbuf.size = data.size;
1330 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1332 parse_keys(packets, &key);
1336 free_publickey(key);
1338 free_packet_list(packets);
1341 memset(&dbkey, 0, sizeof(dbkey));
1342 memset(&data, 0, sizeof(data));
1343 ret = cursor->c_get(cursor, &dbkey, &data,
1347 if (ret != DB_NOTFOUND) {
1348 logthing(LOGTHING_ERROR,
1349 "Problem reading key: %s",
1353 cursor->c_close(cursor);
1361 * Include the basic keydb routines.
1363 #define NEED_GETKEYSIGS 1
1364 #define NEED_KEYID2UID 1
1365 #define NEED_UPDATEKEYS 1
1369 * cleanupdb - De-initialize the key database.
1371 * This function should be called upon program exit to allow the DB to
1372 * cleanup after itself.
1374 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1376 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1379 if (privctx->dbenv != NULL) {
1380 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1381 if (privctx->subkeydb != NULL) {
1382 privctx->subkeydb->close(privctx->subkeydb, 0);
1383 privctx->subkeydb = NULL;
1385 if (privctx->skshashdb != NULL) {
1386 privctx->skshashdb->close(privctx->skshashdb, 0);
1387 privctx->skshashdb = NULL;
1389 if (privctx->id64db != NULL) {
1390 privctx->id64db->close(privctx->id64db, 0);
1391 privctx->id64db = NULL;
1393 if (privctx->id32db != NULL) {
1394 privctx->id32db->close(privctx->id32db, 0);
1395 privctx->id32db = NULL;
1397 if (privctx->worddb != NULL) {
1398 privctx->worddb->close(privctx->worddb, 0);
1399 privctx->worddb = NULL;
1401 for (i = 0; i < privctx->numdbs; i++) {
1402 if (privctx->dbconns[i] != NULL) {
1403 privctx->dbconns[i]->close(privctx->dbconns[i],
1405 privctx->dbconns[i] = NULL;
1408 free(privctx->dbconns);
1409 privctx->dbconns = NULL;
1410 privctx->dbenv->close(privctx->dbenv, 0);
1411 privctx->dbenv = NULL;
1420 * initdb - Initialize the key database.
1422 * This function should be called before any of the other functions in
1423 * this file are called in order to allow the DB to be initialized ready
1426 struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
1433 struct stat statbuf;
1435 struct onak_dbctx *dbctx;
1436 struct onak_db4_dbctx *privctx;
1438 dbctx = malloc(sizeof(*dbctx));
1439 if (dbctx == NULL) {
1442 dbctx->config = dbcfg;
1443 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1444 if (privctx == NULL) {
1449 /* Default to 16 key data DBs */
1450 privctx->numdbs = 16;
1452 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
1454 ret = stat(buf, &statbuf);
1455 while ((ret == 0) || (errno != ENOENT)) {
1457 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1458 "lock file: %s (%d)", strerror(errno), ret);
1461 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1463 ret = stat(buf, &statbuf);
1467 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
1468 numdb = fopen(buf, "r");
1469 if (numdb != NULL) {
1470 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1471 privctx->numdbs = atoi(buf);
1474 } else if (!readonly) {
1475 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1477 numdb = fopen(buf, "w");
1478 if (numdb != NULL) {
1479 fprintf(numdb, "%d", privctx->numdbs);
1482 logthing(LOGTHING_ERROR,
1483 "Couldn't write num_keydb: %s",
1488 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1489 if (privctx->dbconns == NULL) {
1490 logthing(LOGTHING_CRITICAL,
1491 "Couldn't allocate memory for dbconns");
1496 ret = db_env_create(&privctx->dbenv, 0);
1498 logthing(LOGTHING_CRITICAL,
1499 "db_env_create: %s", db_strerror(ret));
1504 * Up the number of locks we're allowed at once. We base this on
1505 * the maximum number of keys we're going to return.
1508 maxlocks = config.maxkeys * 16;
1509 if (maxlocks < 1000) {
1512 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1513 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1517 * Enable deadlock detection so that we don't block indefinitely on
1518 * anything. What we really want is simple 2 state locks, but I'm not
1519 * sure how to make the standard DB functions do that yet.
1522 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1523 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1525 logthing(LOGTHING_CRITICAL,
1526 "db_env_create: %s", db_strerror(ret));
1531 ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
1532 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1536 #ifdef DB_VERSION_MISMATCH
1537 if (ret == DB_VERSION_MISMATCH) {
1538 privctx->dbenv->close(privctx->dbenv, 0);
1539 privctx->dbenv = NULL;
1540 ret = db4_upgradedb(dbctx);
1542 ret = db_env_create(&privctx->dbenv, 0);
1545 privctx->dbenv->set_errcall(privctx->dbenv,
1547 privctx->dbenv->set_lk_detect(privctx->dbenv,
1549 ret = privctx->dbenv->open(privctx->dbenv,
1551 DB_INIT_LOG | DB_INIT_MPOOL |
1552 DB_INIT_LOCK | DB_INIT_TXN |
1553 DB_CREATE | DB_RECOVER,
1557 privctx->dbenv->txn_checkpoint(
1567 logthing(LOGTHING_CRITICAL,
1568 "Error opening db environment: %s (%s)",
1571 if (privctx->dbenv != NULL) {
1572 privctx->dbenv->close(privctx->dbenv, 0);
1573 privctx->dbenv = NULL;
1579 db4_starttrans(dbctx);
1581 for (i = 0; !ret && i < privctx->numdbs; i++) {
1582 ret = db_create(&privctx->dbconns[i],
1585 logthing(LOGTHING_CRITICAL,
1586 "db_create: %s", db_strerror(ret));
1590 snprintf(buf, 1023, "keydb.%d.db", i);
1595 ret = privctx->dbconns[i]->open(
1596 privctx->dbconns[i],
1604 logthing(LOGTHING_CRITICAL,
1605 "Error opening key database:"
1615 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1617 logthing(LOGTHING_CRITICAL, "db_create: %s",
1623 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1627 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1628 "worddb", "worddb", DB_BTREE,
1632 logthing(LOGTHING_CRITICAL,
1633 "Error opening word database: %s (%s)",
1640 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1642 logthing(LOGTHING_CRITICAL, "db_create: %s",
1648 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1652 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1653 "id32db", "id32db", DB_HASH,
1657 logthing(LOGTHING_CRITICAL,
1658 "Error opening id32 database: %s (%s)",
1665 ret = db_create(&privctx->id64db, privctx->dbenv, 0);
1667 logthing(LOGTHING_CRITICAL, "db_create: %s",
1673 ret = privctx->id64db->set_flags(privctx->id64db, DB_DUP);
1677 ret = privctx->id64db->open(privctx->id64db, privctx->txn,
1678 "id64db", "id64db", DB_HASH,
1682 logthing(LOGTHING_CRITICAL,
1683 "Error opening id64 database: %s (%s)",
1690 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1692 logthing(LOGTHING_CRITICAL, "db_create: %s",
1698 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1700 "skshashdb", DB_HASH,
1704 logthing(LOGTHING_CRITICAL,
1705 "Error opening skshash database: %s (%s)",
1712 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1714 logthing(LOGTHING_CRITICAL, "db_create: %s",
1720 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1721 "subkeydb", "subkeydb",
1726 logthing(LOGTHING_CRITICAL,
1727 "Error opening subkey database: %s (%s)",
1733 if (privctx->txn != NULL) {
1734 db4_endtrans(dbctx);
1738 db4_cleanupdb(dbctx);
1739 logthing(LOGTHING_CRITICAL,
1740 "Error opening database; exiting");
1744 dbctx->cleanupdb = db4_cleanupdb;
1745 dbctx->starttrans = db4_starttrans;
1746 dbctx->endtrans = db4_endtrans;
1747 dbctx->fetch_key_id = db4_fetch_key_id;
1748 dbctx->fetch_key_fp = db4_fetch_key_fp;
1749 dbctx->fetch_key_text = db4_fetch_key_text;
1750 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1751 dbctx->store_key = db4_store_key;
1752 dbctx->update_keys = generic_update_keys;
1753 dbctx->delete_key = db4_delete_key;
1754 dbctx->getkeysigs = generic_getkeysigs;
1755 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1756 dbctx->keyid2uid = generic_keyid2uid;
1757 dbctx->iterate_keys = db4_iterate_keys;