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 * fetch_key_fp - Given a fingerprint fetch the key from storage.
295 static int db4_fetch_key_fp(struct onak_dbctx *dbctx,
296 struct openpgp_fingerprint *fingerprint,
297 struct openpgp_publickey **publickey,
300 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
301 struct openpgp_packet_list *packets = NULL;
305 struct buffer_ctx fetchbuf;
306 struct openpgp_fingerprint subfp;
308 memset(&key, 0, sizeof(key));
309 memset(&data, 0, sizeof(data));
314 key.size = fingerprint->length;
315 key.data = fingerprint->fp;
318 db4_starttrans(dbctx);
321 ret = keydb_fp(privctx, fingerprint)->get(keydb_fp(privctx,
328 if (ret == DB_NOTFOUND) {
329 /* If we didn't find the key ID see if it's a subkey ID */
330 memset(&key, 0, sizeof(key));
331 memset(&data, 0, sizeof(data));
332 data.data = subfp.fp;
333 data.ulen = MAX_FINGERPRINT_LEN;
334 data.flags = DB_DBT_USERMEM;
335 key.data = fingerprint->fp;
336 key.size = fingerprint->length;
338 ret = privctx->subkeydb->get(privctx->subkeydb,
345 /* We got a subkey match; retrieve the actual key */
346 memset(&key, 0, sizeof(key));
347 key.size = subfp.length = data.size;
350 memset(&data, 0, sizeof(data));
354 ret = keydb_fp(privctx, &subfp)->get(
355 keydb_fp(privctx, &subfp),
364 fetchbuf.buffer = data.data;
366 fetchbuf.size = data.size;
367 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
369 parse_keys(packets, publickey);
370 free_packet_list(packets);
373 } else if (ret != DB_NOTFOUND) {
374 logthing(LOGTHING_ERROR,
375 "Problem retrieving key: %s",
387 * fetch_key_id - Given a keyid fetch the key from storage.
388 * @keyid: The keyid to fetch.
389 * @publickey: A pointer to a structure to return the key in.
390 * @intrans: If we're already in a transaction.
392 * We use the hex representation of the keyid as the filename to fetch the
393 * key from. The key is stored in the file as a binary OpenPGP stream of
394 * packets, so we can just use read_openpgp_stream() to read the packets
395 * in and then parse_keys() to parse the packets into a publickey
398 static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
399 struct openpgp_publickey **publickey,
402 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
403 struct openpgp_packet_list *packets = NULL;
408 uint32_t shortkeyid = 0;
409 struct openpgp_fingerprint fingerprint;
413 db4_starttrans(dbctx);
416 /* If the key ID fits in 32 bits assume it's a short key id */
417 if (keyid < 0x100000000LL) {
418 ret = privctx->id32db->cursor(privctx->id32db,
423 shortkeyid = keyid & 0xFFFFFFFF;
424 memset(&key, 0, sizeof(key));
425 memset(&data, 0, sizeof(data));
426 key.data = &shortkeyid;
427 key.size = sizeof(shortkeyid);
429 ret = privctx->id64db->cursor(privctx->id64db,
434 memset(&key, 0, sizeof(key));
435 memset(&data, 0, sizeof(data));
437 key.size = sizeof(keyid);
444 memset(&data, 0, sizeof(data));
445 data.ulen = MAX_FINGERPRINT_LEN;
446 data.data = fingerprint.fp;
447 data.flags = DB_DBT_USERMEM;
450 while (cursor->c_get(cursor, &key, &data,
451 first ? DB_SET : DB_NEXT_DUP) == 0) {
452 /* We got a match; retrieve the actual key */
453 fingerprint.length = data.size;
455 if (db4_fetch_key_fp(dbctx, &fingerprint,
459 memset(&data, 0, sizeof(data));
460 data.ulen = MAX_FINGERPRINT_LEN;
461 data.data = fingerprint.fp;
462 data.flags = DB_DBT_USERMEM;
465 cursor->c_close(cursor);
476 * fetch_key_text - Trys to find the keys that contain the supplied text.
477 * @search: The text to search for.
478 * @publickey: A pointer to a structure to return the key in.
480 * This function searches for the supplied text and returns the keys that
483 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
484 struct openpgp_publickey **publickey)
486 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
493 char *searchtext = NULL;
494 struct ll *wordlist = NULL;
495 struct ll *curword = NULL;
496 struct keyarray keylist = { NULL, 0, 0 };
497 struct keyarray newkeylist = { NULL, 0, 0 };
499 struct openpgp_fingerprint fingerprint;
502 searchtext = strdup(search);
503 wordlist = makewordlist(wordlist, searchtext);
505 for (curword = wordlist; curword != NULL; curword = curword->next) {
506 db4_starttrans(dbctx);
508 ret = privctx->worddb->cursor(privctx->worddb,
518 memset(&key, 0, sizeof(key));
519 memset(&data, 0, sizeof(data));
520 key.data = curword->object;
521 key.size = strlen(curword->object);
522 data.flags = DB_DBT_MALLOC;
523 ret = cursor->c_get(cursor,
527 while (ret == 0 && strncmp(key.data, curword->object,
529 ((char *) curword->object)[key.size] == 0) {
531 fingerprint.length = data.size;
532 memcpy(fingerprint.fp, data.data, data.size);
535 * Only add the keys containing this word if this is
536 * our first pass (ie we have no existing key list),
537 * or the key contained a previous word.
539 if (firstpass || array_find(&keylist, &fingerprint)) {
540 array_add(&newkeylist, &fingerprint);
546 ret = cursor->c_get(cursor,
551 array_free(&keylist);
552 keylist.keys = newkeylist.keys;
553 keylist.count = newkeylist.count;
554 keylist.size = newkeylist.size;
555 newkeylist.keys = NULL;
556 newkeylist.count = newkeylist.size = 0;
557 if (data.data != NULL) {
561 cursor->c_close(cursor);
566 llfree(wordlist, NULL);
569 if (keylist.count > config.maxkeys) {
570 keylist.count = config.maxkeys;
573 db4_starttrans(dbctx);
574 for (i = 0; i < keylist.count; i++) {
575 numkeys += db4_fetch_key_fp(dbctx, &keylist.keys[i],
579 array_free(&keylist);
588 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
589 const struct skshash *hash,
590 struct openpgp_publickey **publickey)
592 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;