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 unsigned char worddb_data[12];
659 struct ll *wordlist = NULL;
660 struct ll *curword = NULL;
661 bool deadlock = false;
666 db4_starttrans(dbctx);
669 if (db4_fetch_key_fp(dbctx, fp, &publickey, true) == 0) {
676 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
681 * Walk through the uids removing the words from the worddb.
683 if (publickey != NULL) {
684 uids = keyuids(publickey, &primary);
687 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
688 wordlist = makewordlist(wordlist, uids[i]);
691 privctx->worddb->cursor(privctx->worddb,
696 for (curword = wordlist; curword != NULL && !deadlock;
697 curword = curword->next) {
698 memset(&key, 0, sizeof(key));
699 memset(&data, 0, sizeof(data));
700 key.data = curword->object;
701 key.size = strlen(key.data);
702 data.data = worddb_data;
703 data.size = sizeof(worddb_data);
706 * New style uses the fingerprint as the data
707 * Old (unsupported) style was the 64 bit keyid
709 memset(&key, 0, sizeof(key));
710 memset(&data, 0, sizeof(data));
711 key.data = curword->object;
712 key.size = strlen(key.data);
714 data.size = fp->length;
716 ret = cursor->c_get(cursor,
722 ret = cursor->c_del(cursor, 0);
725 if (ret != 0 && ret != DB_NOTFOUND) {
726 logthing(LOGTHING_ERROR,
727 "Problem deleting word: %s "
728 "(0x%016" PRIX64 ")",
731 if (ret == DB_LOCK_DEADLOCK) {
736 cursor->c_close(cursor);
740 * Free our UID and word lists.
742 llfree(wordlist, NULL);
743 for (i = 0; uids[i] != NULL; i++) {
752 privctx->id32db->cursor(privctx->id32db,
756 privctx->id64db->cursor(privctx->id64db,
761 /* 32 bit short key mapping to fingerprint */
762 shortkeyid = keyid & 0xFFFFFFFF;
764 memset(&key, 0, sizeof(key));
765 memset(&data, 0, sizeof(data));
766 key.data = &shortkeyid;
767 key.size = sizeof(shortkeyid);
769 data.size = fp->length;
771 ret = cursor->c_get(cursor,
777 ret = cursor->c_del(cursor, 0);
780 if (ret != 0 && ret != DB_NOTFOUND) {
781 logthing(LOGTHING_ERROR,
782 "Problem deleting short keyid: %s "
783 "(0x%016" PRIX64 ")",
786 if (ret == DB_LOCK_DEADLOCK) {
791 /* 64 bit key mapping to fingerprint */
792 memset(&key, 0, sizeof(key));
793 memset(&data, 0, sizeof(data));
795 key.size = sizeof(keyid);
797 data.size = fp->length;
799 ret = cursor64->c_get(cursor64,
805 ret = cursor64->c_del(cursor64, 0);
808 if (ret != 0 && ret != DB_NOTFOUND) {
809 logthing(LOGTHING_ERROR,
810 "Problem deleting keyid: %s "
811 "(0x%016" PRIX64 ")",
814 if (ret == DB_LOCK_DEADLOCK) {
819 subkeyids = keysubkeys(publickey);
821 while (subkeyids != NULL && subkeyids[i].length != 0) {
822 subkeyid = fingerprint2keyid(&subkeyids[i]);
823 memset(&key, 0, sizeof(key));
824 key.data = subkeyids[i].fp;
825 key.size = subkeyids[i].length;
826 privctx->subkeydb->del(privctx->subkeydb,
827 privctx->txn, &key, 0);
828 if (ret != 0 && ret != DB_NOTFOUND) {
829 logthing(LOGTHING_ERROR,
830 "Problem deleting subkey id: %s "
831 "(0x%016" PRIX64 ")",
834 if (ret == DB_LOCK_DEADLOCK) {
839 shortkeyid = subkeyid & 0xFFFFFFFF;
841 /* Remove 32 bit keyid -> fingerprint mapping */
842 memset(&key, 0, sizeof(key));
843 memset(&data, 0, sizeof(data));
844 key.data = &shortkeyid;
845 key.size = sizeof(shortkeyid);
847 data.size = fp->length;
849 ret = cursor->c_get(cursor,
855 ret = cursor->c_del(cursor, 0);
858 if (ret != 0 && ret != DB_NOTFOUND) {
859 logthing(LOGTHING_ERROR,
860 "Problem deleting short keyid: %s "
861 "(0x%016" PRIX64 ")",
864 if (ret == DB_LOCK_DEADLOCK) {
869 /* Remove 64 bit keyid -> fingerprint mapping */
870 memset(&key, 0, sizeof(key));
871 memset(&data, 0, sizeof(data));
872 key.data = &subkeyid;
873 key.size = sizeof(subkeyid);
875 data.size = fp->length;
877 ret = cursor64->c_get(cursor64,
883 ret = cursor64->c_del(cursor64, 0);
886 if (ret != 0 && ret != DB_NOTFOUND) {
887 logthing(LOGTHING_ERROR,
888 "Problem deleting keyid: %s "
889 "(0x%016" PRIX64 ")",
892 if (ret == DB_LOCK_DEADLOCK) {
898 if (subkeyids != NULL) {
902 cursor64->c_close(cursor64);
904 cursor->c_close(cursor);
909 ret = privctx->skshashdb->cursor(privctx->skshashdb,
914 get_skshash(publickey, &hash);
916 /* Remove SKS hash -> fingerprint mapping */
917 memset(&key, 0, sizeof(key));
918 memset(&data, 0, sizeof(data));
919 key.data = hash.hash;
920 key.size = sizeof(hash.hash);
922 data.size = fp->length;
924 ret = cursor->c_get(cursor,
930 ret = cursor->c_del(cursor, 0);
933 if (ret != 0 && ret != DB_NOTFOUND) {
934 logthing(LOGTHING_ERROR,
935 "Problem deleting skshash: %s "
936 "(0x%016" PRIX64 ")",
939 if (ret == DB_LOCK_DEADLOCK) {
944 cursor->c_close(cursor);
948 free_publickey(publickey);
953 key.size = fp->length;
955 keydb_fp(privctx, fp)->del(keydb_fp(privctx, fp),
965 return deadlock ? (-1) : (ret == DB_NOTFOUND);
969 * store_key - Takes a key and stores it.
970 * @publickey: A pointer to the public key to store.
971 * @intrans: If we're already in a transaction.
972 * @update: If true the key exists and should be updated.
974 * Again we just use the hex representation of the keyid as the filename
975 * to store the key to. We flatten the public key to a list of OpenPGP
976 * packets and then use write_openpgp_stream() to write the stream out to
977 * the file. If update is true then we delete the old key first, otherwise
978 * we trust that it doesn't exist.
980 static int db4_store_key(struct onak_dbctx *dbctx,
981 struct openpgp_publickey *publickey, bool intrans,
984 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
985 struct openpgp_packet_list *packets = NULL;
986 struct openpgp_packet_list *list_end = NULL;
987 struct openpgp_publickey *next = NULL;
990 struct buffer_ctx storebuf;
994 uint32_t shortkeyid = 0;
995 struct openpgp_fingerprint *subkeyids = NULL;
997 char *primary = NULL;
998 struct ll *wordlist = NULL;
999 struct ll *curword = NULL;
1000 bool deadlock = false;
1001 struct skshash hash;
1002 struct openpgp_fingerprint fingerprint;
1004 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
1005 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
1009 if (get_fingerprint(publickey->publickey, &fingerprint) != ONAK_E_OK) {
1010 logthing(LOGTHING_ERROR, "Couldn't find fingerprint for key.");
1015 db4_starttrans(dbctx);
1019 * Delete the key if we already have it.
1021 * TODO: Can we optimize this perhaps? Possibly when other data is
1022 * involved as well? I suspect this is easiest and doesn't make a lot
1023 * of difference though - the largest chunk of data is the keydata and
1024 * it definitely needs updated.
1027 deadlock = (db4_delete_key(dbctx, &fingerprint, true) == -1);
1031 * Convert the key to a flat set of binary data.
1034 next = publickey->next;
1035 publickey->next = NULL;
1036 flatten_publickey(publickey, &packets, &list_end);
1037 publickey->next = next;
1039 storebuf.offset = 0;
1040 storebuf.size = 8192;
1041 storebuf.buffer = malloc(8192);
1043 write_openpgp_stream(buffer_putchar, &storebuf, packets);
1046 * Now we have the key data store it in the DB; the fingerprint
1049 memset(&key, 0, sizeof(key));
1050 memset(&data, 0, sizeof(data));
1051 key.data = fingerprint.fp;
1052 key.size = fingerprint.length;
1053 data.size = storebuf.offset;
1054 data.data = storebuf.buffer;
1056 ret = keydb_fp(privctx, &fingerprint)->put(
1057 keydb_fp(privctx, &fingerprint),
1063 logthing(LOGTHING_ERROR,
1064 "Problem storing key: %s",
1066 if (ret == DB_LOCK_DEADLOCK) {
1071 free(storebuf.buffer);
1072 storebuf.buffer = NULL;
1074 storebuf.offset = 0;
1076 free_packet_list(packets);
1081 * Walk through our uids storing the words into the db with the
1085 uids = keyuids(publickey, &primary);
1088 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1089 wordlist = makewordlist(wordlist, uids[i]);
1092 for (curword = wordlist; curword != NULL && !deadlock;
1093 curword = curword->next) {
1094 memset(&key, 0, sizeof(key));
1095 memset(&data, 0, sizeof(data));
1096 key.data = curword->object;
1097 key.size = strlen(key.data);
1098 data.data = fingerprint.fp;
1099 data.size = fingerprint.length;
1101 ret = privctx->worddb->put(privctx->worddb,
1107 logthing(LOGTHING_ERROR,
1108 "Problem storing word: %s",
1110 if (ret == DB_LOCK_DEADLOCK) {
1117 * Free our UID and word lists.
1119 llfree(wordlist, NULL);
1120 for (i = 0; uids[i] != NULL; i++) {
1129 * Write the truncated 32 bit keyid so we can lookup the fingerprint
1133 shortkeyid = keyid & 0xFFFFFFFF;
1135 memset(&key, 0, sizeof(key));
1136 memset(&data, 0, sizeof(data));
1137 key.data = &shortkeyid;
1138 key.size = sizeof(shortkeyid);
1139 data.data = fingerprint.fp;
1140 data.size = fingerprint.length;
1142 ret = privctx->id32db->put(privctx->id32db,
1148 logthing(LOGTHING_ERROR,
1149 "Problem storing short keyid: %s",
1151 if (ret == DB_LOCK_DEADLOCK) {
1158 * Write the 64 bit keyid so we can lookup the fingerprint for
1162 memset(&key, 0, sizeof(key));
1163 memset(&data, 0, sizeof(data));
1165 key.size = sizeof(keyid);
1166 data.data = fingerprint.fp;
1167 data.size = fingerprint.length;
1169 ret = privctx->id64db->put(privctx->id64db,
1175 logthing(LOGTHING_ERROR,
1176 "Problem storing keyid: %s",
1178 if (ret == DB_LOCK_DEADLOCK) {
1185 subkeyids = keysubkeys(publickey);
1187 while (subkeyids != NULL && subkeyids[i].length != 0) {
1188 /* Store the subkey ID -> main key fp mapping */
1189 memset(&key, 0, sizeof(key));
1190 memset(&data, 0, sizeof(data));
1191 key.data = subkeyids[i].fp;
1192 key.size = subkeyids[i].length;
1193 data.data = fingerprint.fp;
1194 data.size = fingerprint.length;
1196 ret = privctx->subkeydb->put(privctx->subkeydb,
1202 logthing(LOGTHING_ERROR,
1203 "Problem storing subkey keyid: %s",
1205 if (ret == DB_LOCK_DEADLOCK) {
1210 /* Store the 64 bit subkey ID -> main key fp mapping */
1211 memset(&key, 0, sizeof(key));
1212 memset(&data, 0, sizeof(data));
1214 keyid = fingerprint2keyid(&subkeyids[i]);
1216 key.size = sizeof(keyid);
1217 data.data = fingerprint.fp;
1218 data.size = fingerprint.length;
1220 ret = privctx->id64db->put(privctx->id64db,
1226 logthing(LOGTHING_ERROR,
1227 "Problem storing keyid: %s",
1229 if (ret == DB_LOCK_DEADLOCK) {
1234 /* Store the short subkey ID -> main key fp mapping */
1235 shortkeyid = keyid & 0xFFFFFFFF;
1237 memset(&key, 0, sizeof(key));
1238 memset(&data, 0, sizeof(data));
1239 key.data = &shortkeyid;
1240 key.size = sizeof(shortkeyid);
1241 data.data = fingerprint.fp;
1242 data.size = fingerprint.length;
1244 ret = privctx->id32db->put(privctx->id32db,
1250 logthing(LOGTHING_ERROR,
1251 "Problem storing short keyid: %s",
1253 if (ret == DB_LOCK_DEADLOCK) {
1259 if (subkeyids != NULL) {
1266 get_skshash(publickey, &hash);
1267 memset(&key, 0, sizeof(key));
1268 memset(&data, 0, sizeof(data));
1269 key.data = hash.hash;
1270 key.size = sizeof(hash.hash);
1271 data.data = fingerprint.fp;
1272 data.size = fingerprint.length;
1274 ret = privctx->skshashdb->put(privctx->skshashdb,
1280 logthing(LOGTHING_ERROR,
1281 "Problem storing SKS hash: %s",
1283 if (ret == DB_LOCK_DEADLOCK) {
1290 db4_endtrans(dbctx);
1293 return deadlock ? -1 : 0 ;
1297 * iterate_keys - call a function once for each key in the db.
1298 * @iterfunc: The function to call.
1299 * @ctx: A context pointer
1301 * Calls iterfunc once for each key in the database. ctx is passed
1302 * unaltered to iterfunc. This function is intended to aid database dumps
1303 * and statistic calculations.
1305 * Returns the number of keys we iterated over.
1307 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1308 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1311 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1317 struct buffer_ctx fetchbuf;
1318 struct openpgp_packet_list *packets = NULL;
1319 struct openpgp_publickey *key = NULL;
1321 for (i = 0; i < privctx->numdbs; i++) {
1322 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1331 memset(&dbkey, 0, sizeof(dbkey));
1332 memset(&data, 0, sizeof(data));
1333 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1335 fetchbuf.buffer = data.data;
1336 fetchbuf.offset = 0;
1337 fetchbuf.size = data.size;
1338 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1340 parse_keys(packets, &key);
1344 free_publickey(key);
1346 free_packet_list(packets);
1349 memset(&dbkey, 0, sizeof(dbkey));
1350 memset(&data, 0, sizeof(data));
1351 ret = cursor->c_get(cursor, &dbkey, &data,
1355 if (ret != DB_NOTFOUND) {
1356 logthing(LOGTHING_ERROR,
1357 "Problem reading key: %s",
1361 cursor->c_close(cursor);
1369 * Include the basic keydb routines.
1371 #define NEED_GETKEYSIGS 1
1372 #define NEED_KEYID2UID 1
1373 #define NEED_UPDATEKEYS 1
1377 * cleanupdb - De-initialize the key database.
1379 * This function should be called upon program exit to allow the DB to
1380 * cleanup after itself.
1382 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1384 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1387 if (privctx->dbenv != NULL) {
1388 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1389 if (privctx->subkeydb != NULL) {
1390 privctx->subkeydb->close(privctx->subkeydb, 0);
1391 privctx->subkeydb = NULL;
1393 if (privctx->skshashdb != NULL) {
1394 privctx->skshashdb->close(privctx->skshashdb, 0);
1395 privctx->skshashdb = NULL;
1397 if (privctx->id64db != NULL) {
1398 privctx->id64db->close(privctx->id64db, 0);
1399 privctx->id64db = NULL;
1401 if (privctx->id32db != NULL) {
1402 privctx->id32db->close(privctx->id32db, 0);
1403 privctx->id32db = NULL;
1405 if (privctx->worddb != NULL) {
1406 privctx->worddb->close(privctx->worddb, 0);
1407 privctx->worddb = NULL;
1409 for (i = 0; i < privctx->numdbs; i++) {
1410 if (privctx->dbconns[i] != NULL) {
1411 privctx->dbconns[i]->close(privctx->dbconns[i],
1413 privctx->dbconns[i] = NULL;
1416 free(privctx->dbconns);
1417 privctx->dbconns = NULL;
1418 privctx->dbenv->close(privctx->dbenv, 0);
1419 privctx->dbenv = NULL;
1428 * initdb - Initialize the key database.
1430 * This function should be called before any of the other functions in
1431 * this file are called in order to allow the DB to be initialized ready
1434 struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
1441 struct stat statbuf;
1443 struct onak_dbctx *dbctx;
1444 struct onak_db4_dbctx *privctx;
1446 dbctx = malloc(sizeof(*dbctx));
1447 if (dbctx == NULL) {
1450 dbctx->config = dbcfg;
1451 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1452 if (privctx == NULL) {
1457 /* Default to 16 key data DBs */
1458 privctx->numdbs = 16;
1460 snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
1462 ret = stat(buf, &statbuf);
1463 while ((ret == 0) || (errno != ENOENT)) {
1465 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1466 "lock file: %s (%d)", strerror(errno), ret);
1469 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1471 ret = stat(buf, &statbuf);
1475 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
1476 numdb = fopen(buf, "r");
1477 if (numdb != NULL) {
1478 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1479 privctx->numdbs = atoi(buf);
1482 } else if (!readonly) {
1483 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1485 numdb = fopen(buf, "w");
1486 if (numdb != NULL) {
1487 fprintf(numdb, "%d", privctx->numdbs);
1490 logthing(LOGTHING_ERROR,
1491 "Couldn't write num_keydb: %s",
1496 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1497 if (privctx->dbconns == NULL) {
1498 logthing(LOGTHING_CRITICAL,
1499 "Couldn't allocate memory for dbconns");
1504 ret = db_env_create(&privctx->dbenv, 0);
1506 logthing(LOGTHING_CRITICAL,
1507 "db_env_create: %s", db_strerror(ret));
1512 * Up the number of locks we're allowed at once. We base this on
1513 * the maximum number of keys we're going to return.
1516 maxlocks = config.maxkeys * 16;
1517 if (maxlocks < 1000) {
1520 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1521 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1525 * Enable deadlock detection so that we don't block indefinitely on
1526 * anything. What we really want is simple 2 state locks, but I'm not
1527 * sure how to make the standard DB functions do that yet.
1530 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1531 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1533 logthing(LOGTHING_CRITICAL,
1534 "db_env_create: %s", db_strerror(ret));
1539 ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
1540 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1544 #ifdef DB_VERSION_MISMATCH
1545 if (ret == DB_VERSION_MISMATCH) {
1546 privctx->dbenv->close(privctx->dbenv, 0);
1547 privctx->dbenv = NULL;
1548 ret = db4_upgradedb(dbctx);
1550 ret = db_env_create(&privctx->dbenv, 0);
1553 privctx->dbenv->set_errcall(privctx->dbenv,
1555 privctx->dbenv->set_lk_detect(privctx->dbenv,
1557 ret = privctx->dbenv->open(privctx->dbenv,
1559 DB_INIT_LOG | DB_INIT_MPOOL |
1560 DB_INIT_LOCK | DB_INIT_TXN |
1561 DB_CREATE | DB_RECOVER,
1565 privctx->dbenv->txn_checkpoint(
1575 logthing(LOGTHING_CRITICAL,
1576 "Error opening db environment: %s (%s)",
1579 if (privctx->dbenv != NULL) {
1580 privctx->dbenv->close(privctx->dbenv, 0);
1581 privctx->dbenv = NULL;
1587 db4_starttrans(dbctx);
1589 for (i = 0; !ret && i < privctx->numdbs; i++) {
1590 ret = db_create(&privctx->dbconns[i],
1593 logthing(LOGTHING_CRITICAL,
1594 "db_create: %s", db_strerror(ret));
1598 snprintf(buf, 1023, "keydb.%d.db", i);
1603 ret = privctx->dbconns[i]->open(
1604 privctx->dbconns[i],
1612 logthing(LOGTHING_CRITICAL,
1613 "Error opening key database:"
1623 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1625 logthing(LOGTHING_CRITICAL, "db_create: %s",
1631 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1635 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1636 "worddb", "worddb", DB_BTREE,
1640 logthing(LOGTHING_CRITICAL,
1641 "Error opening word database: %s (%s)",
1648 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1650 logthing(LOGTHING_CRITICAL, "db_create: %s",
1656 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1660 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1661 "id32db", "id32db", DB_HASH,
1665 logthing(LOGTHING_CRITICAL,
1666 "Error opening id32 database: %s (%s)",
1673 ret = db_create(&privctx->id64db, privctx->dbenv, 0);
1675 logthing(LOGTHING_CRITICAL, "db_create: %s",
1681 ret = privctx->id64db->set_flags(privctx->id64db, DB_DUP);
1685 ret = privctx->id64db->open(privctx->id64db, privctx->txn,
1686 "id64db", "id64db", DB_HASH,
1690 logthing(LOGTHING_CRITICAL,
1691 "Error opening id64 database: %s (%s)",
1698 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1700 logthing(LOGTHING_CRITICAL, "db_create: %s",
1706 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1708 "skshashdb", DB_HASH,
1712 logthing(LOGTHING_CRITICAL,
1713 "Error opening skshash database: %s (%s)",
1720 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1722 logthing(LOGTHING_CRITICAL, "db_create: %s",
1728 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1729 "subkeydb", "subkeydb",
1734 logthing(LOGTHING_CRITICAL,
1735 "Error opening subkey database: %s (%s)",
1741 if (privctx->txn != NULL) {
1742 db4_endtrans(dbctx);
1746 db4_cleanupdb(dbctx);
1747 logthing(LOGTHING_CRITICAL,
1748 "Error opening database; exiting");
1752 dbctx->cleanupdb = db4_cleanupdb;
1753 dbctx->starttrans = db4_starttrans;
1754 dbctx->endtrans = db4_endtrans;
1755 dbctx->fetch_key_id = db4_fetch_key_id;
1756 dbctx->fetch_key_fp = db4_fetch_key_fp;
1757 dbctx->fetch_key_text = db4_fetch_key_text;
1758 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1759 dbctx->store_key = db4_store_key;
1760 dbctx->update_keys = generic_update_keys;
1761 dbctx->delete_key = db4_delete_key;
1762 dbctx->getkeysigs = generic_getkeysigs;
1763 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1764 dbctx->keyid2uid = generic_keyid2uid;
1765 dbctx->iterate_keys = db4_iterate_keys;