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, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <sys/types.h>
33 #include "charfuncs.h"
37 #include "decodekey.h"
38 #include "keystructs.h"
41 #include "onak-conf.h"
45 #define DB4_UPGRADE_FILE "db_upgrade.lck"
47 struct onak_db4_dbctx {
48 DB_ENV *dbenv; /* The database environment context */
49 int numdbs; /* Number of data databases in use */
50 DB **dbconns; /* Connections to the key data databases */
51 DB *worddb; /* Connection to the word lookup database */
52 DB *id32db; /* Connection to the 32 bit ID lookup database */
53 DB *id64db; /* Connection to the 64 bit ID lookup database */
54 DB *skshashdb; /* Connection to the SKS hash database */
55 DB *subkeydb; /* Connection to the subkey ID lookup database */
56 DB_TXN *txn; /* Our current transaction ID */
59 DB *keydb_id(struct onak_db4_dbctx *privctx, uint64_t keyid)
65 return(privctx->dbconns[keytrun % privctx->numdbs]);
68 DB *keydb_fp(struct onak_db4_dbctx *privctx, struct openpgp_fingerprint *fp)
72 keytrun = (fp->fp[4] << 24) |
77 return(privctx->dbconns[keytrun % privctx->numdbs]);
81 * db4_errfunc - Direct DB errors to logfile
83 * Basic function to take errors from the DB library and output them to
84 * the logfile rather than stderr.
86 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 3)
87 static void db4_errfunc(const char *errpfx, const char *errmsg)
89 static void db4_errfunc(const DB_ENV *edbenv, const char *errpfx,
94 logthing(LOGTHING_DEBUG, "db4 error: %s:%s", errpfx, errmsg);
96 logthing(LOGTHING_DEBUG, "db4 error: %s", errmsg);
103 * starttrans - Start a transaction.
105 * Start a transaction. Intended to be used if we're about to perform many
106 * operations on the database to help speed it all up, or if we want
107 * something to only succeed if all relevant operations are successful.
109 static bool db4_starttrans(struct onak_dbctx *dbctx)
111 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
114 log_assert(privctx->dbenv != NULL);
115 log_assert(privctx->txn == NULL);
117 ret = privctx->dbenv->txn_begin(privctx->dbenv,
118 NULL, /* No parent transaction */
122 logthing(LOGTHING_CRITICAL,
123 "Error starting transaction: %s",
132 * endtrans - End a transaction.
134 * Ends a transaction.
136 static void db4_endtrans(struct onak_dbctx *dbctx)
138 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
141 log_assert(privctx->dbenv != NULL);
142 log_assert(privctx->txn != NULL);
144 ret = privctx->txn->commit(privctx->txn,
147 logthing(LOGTHING_CRITICAL,
148 "Error ending transaction: %s",
158 * db4_upgradedb - Upgrade a DB4 database
160 * Called if we discover we need to upgrade our DB4 database; ie if
161 * we're running with a newer version of db4 than the database was
164 static int db4_upgradedb(struct onak_db4_dbctx *privctx)
173 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
175 lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
176 if (lockfile_fd < 0) {
177 if (errno == EEXIST) {
178 while (stat(buf, &statbuf) == 0) ;
181 logthing(LOGTHING_CRITICAL, "Couldn't open database "
182 "update lock file: %s", strerror(errno));
186 snprintf(buf, sizeof(buf) - 1, "%d", getpid());
187 write(lockfile_fd, buf, strlen(buf));
190 logthing(LOGTHING_NOTICE, "Upgrading DB4 database");
191 ret = db_env_create(&privctx->dbenv, 0);
193 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
194 privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0);
195 privctx->dbenv = NULL;
197 for (i = 0; i < privctx->numdbs; i++) {
198 ret = db_create(&curdb, NULL, 0);
200 snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
202 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
203 curdb->upgrade(curdb, buf, 0);
204 curdb->close(curdb, 0);
206 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
212 ret = db_create(&curdb, NULL, 0);
214 snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
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",
224 ret = db_create(&curdb, NULL, 0);
226 snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
227 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
228 curdb->upgrade(curdb, buf, 0);
229 curdb->close(curdb, 0);
231 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
236 ret = db_create(&curdb, NULL, 0);
238 snprintf(buf, sizeof(buf) - 1, "%s/id64db", config.db_dir);
239 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
240 curdb->upgrade(curdb, buf, 0);
241 curdb->close(curdb, 0);
243 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
248 ret = db_create(&curdb, NULL, 0);
250 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
251 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
252 curdb->upgrade(curdb, buf, 0);
253 curdb->close(curdb, 0);
255 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
260 ret = db_create(&curdb, NULL, 0);
262 snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
263 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
264 curdb->upgrade(curdb, buf, 0);
265 curdb->close(curdb, 0);
267 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
272 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
280 * getfullkeyid - Maps a 32bit key id to a 64bit one.
281 * @keyid: The 32bit keyid.
283 * This function maps a 32bit key id to the full 64bit one. It returns the
284 * full keyid. If the key isn't found a keyid of 0 is returned.
286 static uint64_t db4_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
288 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
291 uint32_t shortkeyid = 0;
295 if (keyid < 0x100000000LL) {
296 ret = privctx->id32db->cursor(privctx->id32db,
305 shortkeyid = keyid & 0xFFFFFFFF;
307 memset(&key, 0, sizeof(key));
308 memset(&data, 0, sizeof(data));
309 key.data = &shortkeyid;
310 key.size = sizeof(shortkeyid);
311 data.flags = DB_DBT_MALLOC;
313 ret = cursor->c_get(cursor,
319 if (data.size == 8) {
320 keyid = * (uint64_t *) data.data;
323 for (i = 12; i < 20; i++) {
325 keyid |= ((uint8_t *) data.data)[i];
329 if (data.data != NULL) {
335 cursor->c_close(cursor);
343 * fetch_key_fp - Given a fingerprint fetch the key from storage.
345 static int db4_fetch_key_fp(struct onak_dbctx *dbctx,
346 struct openpgp_fingerprint *fingerprint,
347 struct openpgp_publickey **publickey,
350 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
351 struct openpgp_packet_list *packets = NULL;
355 struct buffer_ctx fetchbuf;
356 struct openpgp_fingerprint subfp;
358 memset(&key, 0, sizeof(key));
359 memset(&data, 0, sizeof(data));
364 key.size = fingerprint->length;
365 key.data = fingerprint->fp;
368 db4_starttrans(dbctx);
371 ret = keydb_fp(privctx, fingerprint)->get(keydb_fp(privctx,
378 if (ret == DB_NOTFOUND) {
379 /* If we didn't find the key ID see if it's a subkey ID */
380 memset(&key, 0, sizeof(key));
381 memset(&data, 0, sizeof(data));
382 data.data = subfp.fp;
383 data.ulen = MAX_FINGERPRINT_LEN;
384 data.flags = DB_DBT_USERMEM;
385 key.data = fingerprint->fp;
386 key.size = fingerprint->length;
388 ret = privctx->subkeydb->get(privctx->subkeydb,
395 /* We got a subkey match; retrieve the actual key */
396 memset(&key, 0, sizeof(key));
397 key.size = subfp.length = data.size;
400 memset(&data, 0, sizeof(data));
404 ret = keydb_fp(privctx, &subfp)->get(
405 keydb_fp(privctx, &subfp),
414 fetchbuf.buffer = data.data;
416 fetchbuf.size = data.size;
417 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
419 parse_keys(packets, publickey);
420 free_packet_list(packets);
423 } else if (ret != DB_NOTFOUND) {
424 logthing(LOGTHING_ERROR,
425 "Problem retrieving key: %s",
437 * fetch_key_id - Given a keyid fetch the key from storage.
438 * @keyid: The keyid to fetch.
439 * @publickey: A pointer to a structure to return the key in.
440 * @intrans: If we're already in a transaction.
442 * We use the hex representation of the keyid as the filename to fetch the
443 * key from. The key is stored in the file as a binary OpenPGP stream of
444 * packets, so we can just use read_openpgp_stream() to read the packets
445 * in and then parse_keys() to parse the packets into a publickey
448 static int db4_fetch_key_id(struct onak_dbctx *dbctx, uint64_t keyid,
449 struct openpgp_publickey **publickey,
452 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
453 struct openpgp_packet_list *packets = NULL;
457 struct buffer_ctx fetchbuf;
458 struct openpgp_fingerprint fingerprint;
460 if (keyid < 0x100000000LL) {
461 keyid = db4_getfullkeyid(dbctx, keyid);
464 memset(&key, 0, sizeof(key));
465 memset(&data, 0, sizeof(data));
470 key.size = sizeof(keyid);
474 db4_starttrans(dbctx);
478 * First we try a legacy stored key where we used the 64 bit key ID
481 ret = keydb_id(privctx, keyid)->get(keydb_id(privctx, keyid),
487 if (ret == DB_NOTFOUND) {
488 /* If we didn't find the key ID try the 64 bit map DB */
489 memset(&key, 0, sizeof(key));
490 memset(&data, 0, sizeof(data));
491 data.ulen = MAX_FINGERPRINT_LEN;
492 data.data = fingerprint.fp;
493 data.flags = DB_DBT_USERMEM;
494 key.size = sizeof(keyid);
497 ret = privctx->id64db->get(privctx->id64db,
504 /* We got a match; retrieve the actual key */
505 fingerprint.length = data.size;
507 memset(&key, 0, sizeof(key));
508 memset(&data, 0, sizeof(data));
509 key.size = fingerprint.length;
510 key.data = fingerprint.fp;
512 ret = keydb_fp(privctx, &fingerprint)->get(
513 keydb_fp(privctx, &fingerprint),
522 fetchbuf.buffer = data.data;
524 fetchbuf.size = data.size;
525 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
527 parse_keys(packets, publickey);
528 free_packet_list(packets);
531 } else if (ret != DB_NOTFOUND) {
532 logthing(LOGTHING_ERROR,
533 "Problem retrieving key: %s",
545 int worddb_cmp(const void *d1, const void *d2)
547 return memcmp(d1, d2, 12);
551 * fetch_key_text - Trys to find the keys that contain the supplied text.
552 * @search: The text to search for.
553 * @publickey: A pointer to a structure to return the key in.
555 * This function searches for the supplied text and returns the keys that
558 static int db4_fetch_key_text(struct onak_dbctx *dbctx, const char *search,
559 struct openpgp_publickey **publickey)
561 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
568 char *searchtext = NULL;
569 struct ll *wordlist = NULL;
570 struct ll *curword = NULL;
571 struct keyarray keylist = { NULL, 0, 0 };
572 struct keyarray newkeylist = { NULL, 0, 0 };
574 struct openpgp_fingerprint fingerprint;
577 searchtext = strdup(search);
578 wordlist = makewordlist(wordlist, searchtext);
580 for (curword = wordlist; curword != NULL; curword = curword->next) {
581 db4_starttrans(dbctx);
583 ret = privctx->worddb->cursor(privctx->worddb,
593 memset(&key, 0, sizeof(key));
594 memset(&data, 0, sizeof(data));
595 key.data = curword->object;
596 key.size = strlen(curword->object);
597 data.flags = DB_DBT_MALLOC;
598 ret = cursor->c_get(cursor,
602 while (ret == 0 && strncmp(key.data, curword->object,
604 ((char *) curword->object)[key.size] == 0) {
605 if (data.size == 12) {
606 /* Old style creation + key id */
607 fingerprint.length = 8;
608 for (i = 4; i < 12; i++) {
609 fingerprint.fp[i - 4] =
614 fingerprint.length = data.size;
615 memcpy(fingerprint.fp, data.data, data.size);
619 * Only add the keys containing this word if this is
620 * our first pass (ie we have no existing key list),
621 * or the key contained a previous word.
623 if (firstpass || array_find(&keylist, &fingerprint)) {
624 array_add(&newkeylist, &fingerprint);
630 ret = cursor->c_get(cursor,
635 array_free(&keylist);
636 keylist.keys = newkeylist.keys;
637 keylist.count = newkeylist.count;
638 keylist.size = newkeylist.size;
639 newkeylist.keys = NULL;
640 newkeylist.count = newkeylist.size = 0;
641 if (data.data != NULL) {
645 cursor->c_close(cursor);
650 llfree(wordlist, NULL);
653 if (keylist.count > config.maxkeys) {
654 keylist.count = config.maxkeys;
657 db4_starttrans(dbctx);
658 for (i = 0; i < keylist.count; i++) {
659 if (keylist.keys[i].length == 8) {
661 for (int j = 0; j < 8; j++) {
663 keyid |= keylist.keys[i].fp[j];
665 numkeys += db4_fetch_key_id(dbctx, keyid,
669 numkeys += db4_fetch_key_fp(dbctx, &keylist.keys[i],
674 array_free(&keylist);
683 static int db4_fetch_key_skshash(struct onak_dbctx *dbctx,
684 const struct skshash *hash,
685 struct openpgp_publickey **publickey)
687 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
693 struct openpgp_fingerprint fingerprint;
695 ret = privctx->skshashdb->cursor(privctx->skshashdb,
704 memset(&key, 0, sizeof(key));
705 memset(&data, 0, sizeof(data));
706 key.data = (void *) hash->hash;
707 key.size = sizeof(hash->hash);
708 data.flags = DB_DBT_MALLOC;
710 ret = cursor->c_get(cursor,
716 if (data.size == 8) {
717 /* Legacy key ID record */
718 keyid = *(uint64_t *) data.data;
719 count = db4_fetch_key_id(dbctx, keyid, publickey,
722 fingerprint.length = data.size;
723 memcpy(fingerprint.fp, data.data, data.size);
724 count = db4_fetch_key_fp(dbctx, &fingerprint,
728 if (data.data != NULL) {
734 cursor->c_close(cursor);
741 * delete_key - Given a keyid delete the key from storage.
742 * @keyid: The keyid to delete.
743 * @intrans: If we're already in a transaction.
745 * This function deletes a public key from whatever storage mechanism we
746 * are using. Returns 0 if the key existed.
748 static int db4_delete_key(struct onak_dbctx *dbctx,
749 uint64_t keyid, bool intrans)
751 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
752 struct openpgp_publickey *publickey = NULL;
755 DBC *cursor64 = NULL;
756 uint32_t shortkeyid = 0;
757 uint64_t subkeyid = 0;
758 struct openpgp_fingerprint *subkeyids = NULL;
762 char *primary = NULL;
763 unsigned char worddb_data[12];
764 struct ll *wordlist = NULL;
765 struct ll *curword = NULL;
766 bool deadlock = false;
768 struct openpgp_fingerprint fingerprint;
771 db4_starttrans(dbctx);
774 if (db4_fetch_key_id(dbctx, keyid, &publickey, true) == 0) {
779 get_fingerprint(publickey->publickey, &fingerprint);
782 * Walk through the uids removing the words from the worddb.
784 if (publickey != NULL) {
785 uids = keyuids(publickey, &primary);
788 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
789 wordlist = makewordlist(wordlist, uids[i]);
792 privctx->worddb->cursor(privctx->worddb,
797 for (curword = wordlist; curword != NULL && !deadlock;
798 curword = curword->next) {
799 memset(&key, 0, sizeof(key));
800 memset(&data, 0, sizeof(data));
801 key.data = curword->object;
802 key.size = strlen(key.data);
803 data.data = worddb_data;
804 data.size = sizeof(worddb_data);
807 * Old format word db data was the key creation time
808 * followed by the 64 bit key id.
810 worddb_data[ 0] = publickey->publickey->data[1];
811 worddb_data[ 1] = publickey->publickey->data[2];
812 worddb_data[ 2] = publickey->publickey->data[3];
813 worddb_data[ 3] = publickey->publickey->data[4];
814 worddb_data[ 4] = (keyid >> 56) & 0xFF;
815 worddb_data[ 5] = (keyid >> 48) & 0xFF;
816 worddb_data[ 6] = (keyid >> 40) & 0xFF;
817 worddb_data[ 7] = (keyid >> 32) & 0xFF;
818 worddb_data[ 8] = (keyid >> 24) & 0xFF;
819 worddb_data[ 9] = (keyid >> 16) & 0xFF;
820 worddb_data[10] = (keyid >> 8) & 0xFF;
821 worddb_data[11] = keyid & 0xFF;
823 ret = cursor->c_get(cursor,
829 ret = cursor->c_del(cursor, 0);
832 /* New style just uses the fingerprint as the data */
833 memset(&key, 0, sizeof(key));
834 memset(&data, 0, sizeof(data));
835 key.data = curword->object;
836 key.size = strlen(key.data);
837 data.data = fingerprint.fp;
838 data.size = fingerprint.length;
840 ret = cursor->c_get(cursor,
846 ret = cursor->c_del(cursor, 0);
849 if (ret != 0 && ret != DB_NOTFOUND) {
850 logthing(LOGTHING_ERROR,
851 "Problem deleting word: %s "
852 "(0x%016" PRIX64 ")",
855 if (ret == DB_LOCK_DEADLOCK) {
860 cursor->c_close(cursor);
864 * Free our UID and word lists.
866 llfree(wordlist, NULL);
867 for (i = 0; uids[i] != NULL; i++) {
876 privctx->id32db->cursor(privctx->id32db,
880 privctx->id64db->cursor(privctx->id64db,
885 shortkeyid = keyid & 0xFFFFFFFF;
887 /* Old style mapping to 64 bit key id */
888 memset(&key, 0, sizeof(key));
889 memset(&data, 0, sizeof(data));
890 key.data = &shortkeyid;
891 key.size = sizeof(shortkeyid);
893 data.size = sizeof(keyid);
895 ret = cursor->c_get(cursor,
901 ret = cursor->c_del(cursor, 0);
904 /* New style mapping to fingerprint */
905 memset(&key, 0, sizeof(key));
906 memset(&data, 0, sizeof(data));
907 key.data = &shortkeyid;
908 key.size = sizeof(shortkeyid);
909 data.data = fingerprint.fp;
910 data.size = fingerprint.length;
912 ret = cursor->c_get(cursor,
918 ret = cursor->c_del(cursor, 0);
921 if (ret != 0 && ret != DB_NOTFOUND) {
922 logthing(LOGTHING_ERROR,
923 "Problem deleting short keyid: %s "
924 "(0x%016" PRIX64 ")",
927 if (ret == DB_LOCK_DEADLOCK) {
932 /* 64 bit key mapping to fingerprint */
933 memset(&key, 0, sizeof(key));
934 memset(&data, 0, sizeof(data));
936 key.size = sizeof(keyid);
937 data.data = fingerprint.fp;
938 data.size = fingerprint.length;
940 ret = cursor64->c_get(cursor64,
946 ret = cursor64->c_del(cursor64, 0);
949 if (ret != 0 && ret != DB_NOTFOUND) {
950 logthing(LOGTHING_ERROR,
951 "Problem deleting keyid: %s "
952 "(0x%016" PRIX64 ")",
955 if (ret == DB_LOCK_DEADLOCK) {
960 subkeyids = keysubkeys(publickey);
962 while (subkeyids != NULL && subkeyids[i].length != 0) {
963 subkeyid = fingerprint2keyid(&subkeyids[i]);
964 memset(&key, 0, sizeof(key));
965 key.data = subkeyids[i].fp;
966 key.size = subkeyids[i].length;
967 privctx->subkeydb->del(privctx->subkeydb,
968 privctx->txn, &key, 0);
969 if (ret != 0 && ret != DB_NOTFOUND) {
970 logthing(LOGTHING_ERROR,
971 "Problem deleting subkey id: %s "
972 "(0x%016" PRIX64 ")",
975 if (ret == DB_LOCK_DEADLOCK) {
980 shortkeyid = subkeyid & 0xFFFFFFFF;
982 /* Remove 32 bit keyid -> 64 bit keyid mapping */
983 memset(&key, 0, sizeof(key));
984 memset(&data, 0, sizeof(data));
985 key.data = &shortkeyid;
986 key.size = sizeof(shortkeyid);
988 data.size = sizeof(keyid);
990 ret = cursor->c_get(cursor,
996 ret = cursor->c_del(cursor, 0);
999 /* Remove 32 bit keyid -> fingerprint mapping */
1000 memset(&key, 0, sizeof(key));
1001 memset(&data, 0, sizeof(data));
1002 key.data = &shortkeyid;
1003 key.size = sizeof(shortkeyid);
1004 data.data = fingerprint.fp;
1005 data.size = fingerprint.length;
1007 ret = cursor->c_get(cursor,
1013 ret = cursor->c_del(cursor, 0);
1016 if (ret != 0 && ret != DB_NOTFOUND) {
1017 logthing(LOGTHING_ERROR,
1018 "Problem deleting short keyid: %s "
1019 "(0x%016" PRIX64 ")",
1022 if (ret == DB_LOCK_DEADLOCK) {
1027 /* Remove 64 bit keyid -> fingerprint mapping */
1028 memset(&key, 0, sizeof(key));
1029 memset(&data, 0, sizeof(data));
1030 key.data = &subkeyid;
1031 key.size = sizeof(subkeyid);
1032 data.data = fingerprint.fp;
1033 data.size = fingerprint.length;
1035 ret = cursor64->c_get(cursor64,
1041 ret = cursor64->c_del(cursor64, 0);
1044 if (ret != 0 && ret != DB_NOTFOUND) {
1045 logthing(LOGTHING_ERROR,
1046 "Problem deleting keyid: %s "
1047 "(0x%016" PRIX64 ")",
1050 if (ret == DB_LOCK_DEADLOCK) {
1056 if (subkeyids != NULL) {
1060 cursor64->c_close(cursor64);
1062 cursor->c_close(cursor);
1067 ret = privctx->skshashdb->cursor(privctx->skshashdb,
1072 get_skshash(publickey, &hash);
1074 /* First delete old style keyid mapping */
1075 memset(&key, 0, sizeof(key));
1076 memset(&data, 0, sizeof(data));
1077 key.data = hash.hash;
1078 key.size = sizeof(hash.hash);
1080 data.size = sizeof(keyid);
1082 ret = cursor->c_get(cursor,
1088 ret = cursor->c_del(cursor, 0);
1091 /* Then delete new style fingerprint mapping */
1092 memset(&key, 0, sizeof(key));
1093 memset(&data, 0, sizeof(data));
1094 key.data = hash.hash;
1095 key.size = sizeof(hash.hash);
1096 data.data = fingerprint.fp;
1097 data.size = fingerprint.length;
1099 ret = cursor->c_get(cursor,
1105 ret = cursor->c_del(cursor, 0);
1108 if (ret != 0 && ret != DB_NOTFOUND) {
1109 logthing(LOGTHING_ERROR,
1110 "Problem deleting skshash: %s "
1111 "(0x%016" PRIX64 ")",
1114 if (ret == DB_LOCK_DEADLOCK) {
1119 cursor->c_close(cursor);
1123 free_publickey(publickey);
1128 key.size = sizeof(keyid);
1130 keydb_id(privctx, keyid)->del(keydb_id(privctx, keyid),
1137 db4_endtrans(dbctx);
1140 return deadlock ? (-1) : (ret == DB_NOTFOUND);
1144 * store_key - Takes a key and stores it.
1145 * @publickey: A pointer to the public key to store.
1146 * @intrans: If we're already in a transaction.
1147 * @update: If true the key exists and should be updated.
1149 * Again we just use the hex representation of the keyid as the filename
1150 * to store the key to. We flatten the public key to a list of OpenPGP
1151 * packets and then use write_openpgp_stream() to write the stream out to
1152 * the file. If update is true then we delete the old key first, otherwise
1153 * we trust that it doesn't exist.
1155 static int db4_store_key(struct onak_dbctx *dbctx,
1156 struct openpgp_publickey *publickey, bool intrans,
1159 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1160 struct openpgp_packet_list *packets = NULL;
1161 struct openpgp_packet_list *list_end = NULL;
1162 struct openpgp_publickey *next = NULL;
1165 struct buffer_ctx storebuf;
1169 uint32_t shortkeyid = 0;
1170 struct openpgp_fingerprint *subkeyids = NULL;
1172 char *primary = NULL;
1173 struct ll *wordlist = NULL;
1174 struct ll *curword = NULL;
1175 bool deadlock = false;
1176 struct skshash hash;
1177 struct openpgp_fingerprint fingerprint;
1179 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
1180 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
1184 if (get_fingerprint(publickey->publickey, &fingerprint) != ONAK_E_OK) {
1185 logthing(LOGTHING_ERROR, "Couldn't find fingerprint for key.");
1190 db4_starttrans(dbctx);
1194 * Delete the key if we already have it.
1196 * TODO: Can we optimize this perhaps? Possibly when other data is
1197 * involved as well? I suspect this is easiest and doesn't make a lot
1198 * of difference though - the largest chunk of data is the keydata and
1199 * it definitely needs updated.
1202 deadlock = (db4_delete_key(dbctx, keyid, true) == -1);
1206 * Convert the key to a flat set of binary data.
1209 next = publickey->next;
1210 publickey->next = NULL;
1211 flatten_publickey(publickey, &packets, &list_end);
1212 publickey->next = next;
1214 storebuf.offset = 0;
1215 storebuf.size = 8192;
1216 storebuf.buffer = malloc(8192);
1218 write_openpgp_stream(buffer_putchar, &storebuf, packets);
1221 * Now we have the key data store it in the DB; the keyid is
1224 memset(&key, 0, sizeof(key));
1225 memset(&data, 0, sizeof(data));
1226 key.data = fingerprint.fp;
1227 key.size = fingerprint.length;
1228 data.size = storebuf.offset;
1229 data.data = storebuf.buffer;
1231 ret = keydb_fp(privctx, &fingerprint)->put(
1232 keydb_fp(privctx, &fingerprint),
1238 logthing(LOGTHING_ERROR,
1239 "Problem storing key: %s",
1241 if (ret == DB_LOCK_DEADLOCK) {
1246 free(storebuf.buffer);
1247 storebuf.buffer = NULL;
1249 storebuf.offset = 0;
1251 free_packet_list(packets);
1256 * Walk through our uids storing the words into the db with the keyid.
1259 uids = keyuids(publickey, &primary);
1262 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1263 wordlist = makewordlist(wordlist, uids[i]);
1266 for (curword = wordlist; curword != NULL && !deadlock;
1267 curword = curword->next) {
1268 memset(&key, 0, sizeof(key));
1269 memset(&data, 0, sizeof(data));
1270 key.data = curword->object;
1271 key.size = strlen(key.data);
1272 data.data = fingerprint.fp;
1273 data.size = fingerprint.length;
1275 ret = privctx->worddb->put(privctx->worddb,
1281 logthing(LOGTHING_ERROR,
1282 "Problem storing word: %s",
1284 if (ret == DB_LOCK_DEADLOCK) {
1291 * Free our UID and word lists.
1293 llfree(wordlist, NULL);
1294 for (i = 0; uids[i] != NULL; i++) {
1303 * Write the truncated 32 bit keyid so we can lookup the fingerprint
1307 shortkeyid = keyid & 0xFFFFFFFF;
1309 memset(&key, 0, sizeof(key));
1310 memset(&data, 0, sizeof(data));
1311 key.data = &shortkeyid;
1312 key.size = sizeof(shortkeyid);
1313 data.data = fingerprint.fp;
1314 data.size = fingerprint.length;
1316 ret = privctx->id32db->put(privctx->id32db,
1322 logthing(LOGTHING_ERROR,
1323 "Problem storing short keyid: %s",
1325 if (ret == DB_LOCK_DEADLOCK) {
1332 * Write the 64 bit keyid so we can lookup the fingerprint for
1336 memset(&key, 0, sizeof(key));
1337 memset(&data, 0, sizeof(data));
1339 key.size = sizeof(keyid);
1340 data.data = fingerprint.fp;
1341 data.size = fingerprint.length;
1343 ret = privctx->id64db->put(privctx->id64db,
1349 logthing(LOGTHING_ERROR,
1350 "Problem storing keyid: %s",
1352 if (ret == DB_LOCK_DEADLOCK) {
1359 subkeyids = keysubkeys(publickey);
1361 while (subkeyids != NULL && subkeyids[i].length != 0) {
1362 /* Store the subkey ID -> main key fp mapping */
1363 memset(&key, 0, sizeof(key));
1364 memset(&data, 0, sizeof(data));
1365 key.data = subkeyids[i].fp;
1366 key.size = subkeyids[i].length;
1367 data.data = fingerprint.fp;
1368 data.size = fingerprint.length;
1370 ret = privctx->subkeydb->put(privctx->subkeydb,
1376 logthing(LOGTHING_ERROR,
1377 "Problem storing subkey keyid: %s",
1379 if (ret == DB_LOCK_DEADLOCK) {
1384 /* Store the 64 bit subkey ID -> main key fp mapping */
1385 memset(&key, 0, sizeof(key));
1386 memset(&data, 0, sizeof(data));
1388 keyid = fingerprint2keyid(&subkeyids[i]);
1390 key.size = sizeof(keyid);
1391 data.data = fingerprint.fp;
1392 data.size = fingerprint.length;
1394 ret = privctx->id64db->put(privctx->id64db,
1400 logthing(LOGTHING_ERROR,
1401 "Problem storing keyid: %s",
1403 if (ret == DB_LOCK_DEADLOCK) {
1408 /* Store the short subkey ID -> main key fp mapping */
1409 shortkeyid = keyid & 0xFFFFFFFF;
1411 memset(&key, 0, sizeof(key));
1412 memset(&data, 0, sizeof(data));
1413 key.data = &shortkeyid;
1414 key.size = sizeof(shortkeyid);
1415 data.data = fingerprint.fp;
1416 data.size = fingerprint.length;
1418 ret = privctx->id32db->put(privctx->id32db,
1424 logthing(LOGTHING_ERROR,
1425 "Problem storing short keyid: %s",
1427 if (ret == DB_LOCK_DEADLOCK) {
1433 if (subkeyids != NULL) {
1440 get_skshash(publickey, &hash);
1441 memset(&key, 0, sizeof(key));
1442 memset(&data, 0, sizeof(data));
1443 key.data = hash.hash;
1444 key.size = sizeof(hash.hash);
1445 data.data = fingerprint.fp;
1446 data.size = fingerprint.length;
1448 ret = privctx->skshashdb->put(privctx->skshashdb,
1454 logthing(LOGTHING_ERROR,
1455 "Problem storing SKS hash: %s",
1457 if (ret == DB_LOCK_DEADLOCK) {
1464 db4_endtrans(dbctx);
1467 return deadlock ? -1 : 0 ;
1471 * iterate_keys - call a function once for each key in the db.
1472 * @iterfunc: The function to call.
1473 * @ctx: A context pointer
1475 * Calls iterfunc once for each key in the database. ctx is passed
1476 * unaltered to iterfunc. This function is intended to aid database dumps
1477 * and statistic calculations.
1479 * Returns the number of keys we iterated over.
1481 static int db4_iterate_keys(struct onak_dbctx *dbctx,
1482 void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
1485 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1491 struct buffer_ctx fetchbuf;
1492 struct openpgp_packet_list *packets = NULL;
1493 struct openpgp_publickey *key = NULL;
1495 for (i = 0; i < privctx->numdbs; i++) {
1496 ret = privctx->dbconns[i]->cursor(privctx->dbconns[i],
1505 memset(&dbkey, 0, sizeof(dbkey));
1506 memset(&data, 0, sizeof(data));
1507 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1509 fetchbuf.buffer = data.data;
1510 fetchbuf.offset = 0;
1511 fetchbuf.size = data.size;
1512 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1514 parse_keys(packets, &key);
1518 free_publickey(key);
1520 free_packet_list(packets);
1523 memset(&dbkey, 0, sizeof(dbkey));
1524 memset(&data, 0, sizeof(data));
1525 ret = cursor->c_get(cursor, &dbkey, &data,
1529 if (ret != DB_NOTFOUND) {
1530 logthing(LOGTHING_ERROR,
1531 "Problem reading key: %s",
1535 cursor->c_close(cursor);
1543 * Include the basic keydb routines.
1545 #define NEED_GETKEYSIGS 1
1546 #define NEED_KEYID2UID 1
1547 #define NEED_UPDATEKEYS 1
1551 * cleanupdb - De-initialize the key database.
1553 * This function should be called upon program exit to allow the DB to
1554 * cleanup after itself.
1556 static void db4_cleanupdb(struct onak_dbctx *dbctx)
1558 struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
1561 if (privctx->dbenv != NULL) {
1562 privctx->dbenv->txn_checkpoint(privctx->dbenv, 0, 0, 0);
1563 if (privctx->subkeydb != NULL) {
1564 privctx->subkeydb->close(privctx->subkeydb, 0);
1565 privctx->subkeydb = NULL;
1567 if (privctx->skshashdb != NULL) {
1568 privctx->skshashdb->close(privctx->skshashdb, 0);
1569 privctx->skshashdb = NULL;
1571 if (privctx->id64db != NULL) {
1572 privctx->id64db->close(privctx->id64db, 0);
1573 privctx->id64db = NULL;
1575 if (privctx->id32db != NULL) {
1576 privctx->id32db->close(privctx->id32db, 0);
1577 privctx->id32db = NULL;
1579 if (privctx->worddb != NULL) {
1580 privctx->worddb->close(privctx->worddb, 0);
1581 privctx->worddb = NULL;
1583 for (i = 0; i < privctx->numdbs; i++) {
1584 if (privctx->dbconns[i] != NULL) {
1585 privctx->dbconns[i]->close(privctx->dbconns[i],
1587 privctx->dbconns[i] = NULL;
1590 free(privctx->dbconns);
1591 privctx->dbconns = NULL;
1592 privctx->dbenv->close(privctx->dbenv, 0);
1593 privctx->dbenv = NULL;
1602 * initdb - Initialize the key database.
1604 * This function should be called before any of the other functions in
1605 * this file are called in order to allow the DB to be initialized ready
1608 struct onak_dbctx *keydb_db4_init(bool readonly)
1615 struct stat statbuf;
1617 struct onak_dbctx *dbctx;
1618 struct onak_db4_dbctx *privctx;
1620 dbctx = malloc(sizeof(*dbctx));
1621 if (dbctx == NULL) {
1624 dbctx->priv = privctx = calloc(1, sizeof(*privctx));
1625 if (privctx == NULL) {
1630 /* Default to 16 key data DBs */
1631 privctx->numdbs = 16;
1633 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
1635 ret = stat(buf, &statbuf);
1636 while ((ret == 0) || (errno != ENOENT)) {
1638 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
1639 "lock file: %s (%d)", strerror(errno), ret);
1642 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
1644 ret = stat(buf, &statbuf);
1648 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
1649 numdb = fopen(buf, "r");
1650 if (numdb != NULL) {
1651 if (fgets(buf, sizeof(buf), numdb) != NULL) {
1652 privctx->numdbs = atoi(buf);
1655 } else if (!readonly) {
1656 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
1658 numdb = fopen(buf, "w");
1659 if (numdb != NULL) {
1660 fprintf(numdb, "%d", privctx->numdbs);
1663 logthing(LOGTHING_ERROR,
1664 "Couldn't write num_keydb: %s",
1669 privctx->dbconns = calloc(privctx->numdbs, sizeof (DB *));
1670 if (privctx->dbconns == NULL) {
1671 logthing(LOGTHING_CRITICAL,
1672 "Couldn't allocate memory for dbconns");
1677 ret = db_env_create(&privctx->dbenv, 0);
1679 logthing(LOGTHING_CRITICAL,
1680 "db_env_create: %s", db_strerror(ret));
1685 * Up the number of locks we're allowed at once. We base this on
1686 * the maximum number of keys we're going to return.
1688 maxlocks = config.maxkeys * 16;
1689 if (maxlocks < 1000) {
1692 privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
1693 privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
1696 * Enable deadlock detection so that we don't block indefinitely on
1697 * anything. What we really want is simple 2 state locks, but I'm not
1698 * sure how to make the standard DB functions do that yet.
1701 privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
1702 ret = privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT);
1704 logthing(LOGTHING_CRITICAL,
1705 "db_env_create: %s", db_strerror(ret));
1710 ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
1711 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
1715 #ifdef DB_VERSION_MISMATCH
1716 if (ret == DB_VERSION_MISMATCH) {
1717 privctx->dbenv->close(privctx->dbenv, 0);
1718 privctx->dbenv = NULL;
1719 ret = db4_upgradedb(privctx);
1721 ret = db_env_create(&privctx->dbenv, 0);
1724 privctx->dbenv->set_errcall(privctx->dbenv,
1726 privctx->dbenv->set_lk_detect(privctx->dbenv,
1728 ret = privctx->dbenv->open(privctx->dbenv,
1730 DB_INIT_LOG | DB_INIT_MPOOL |
1731 DB_INIT_LOCK | DB_INIT_TXN |
1732 DB_CREATE | DB_RECOVER,
1736 privctx->dbenv->txn_checkpoint(
1746 logthing(LOGTHING_CRITICAL,
1747 "Error opening db environment: %s (%s)",
1750 if (privctx->dbenv != NULL) {
1751 privctx->dbenv->close(privctx->dbenv, 0);
1752 privctx->dbenv = NULL;
1758 db4_starttrans(dbctx);
1760 for (i = 0; !ret && i < privctx->numdbs; i++) {
1761 ret = db_create(&privctx->dbconns[i],
1764 logthing(LOGTHING_CRITICAL,
1765 "db_create: %s", db_strerror(ret));
1769 snprintf(buf, 1023, "keydb.%d.db", i);
1774 ret = privctx->dbconns[i]->open(
1775 privctx->dbconns[i],
1783 logthing(LOGTHING_CRITICAL,
1784 "Error opening key database:"
1794 ret = db_create(&privctx->worddb, privctx->dbenv, 0);
1796 logthing(LOGTHING_CRITICAL, "db_create: %s",
1802 ret = privctx->worddb->set_flags(privctx->worddb, DB_DUP);
1806 ret = privctx->worddb->open(privctx->worddb, privctx->txn,
1807 "worddb", "worddb", DB_BTREE,
1811 logthing(LOGTHING_CRITICAL,
1812 "Error opening word database: %s (%s)",
1819 ret = db_create(&privctx->id32db, privctx->dbenv, 0);
1821 logthing(LOGTHING_CRITICAL, "db_create: %s",
1827 ret = privctx->id32db->set_flags(privctx->id32db, DB_DUP);
1831 ret = privctx->id32db->open(privctx->id32db, privctx->txn,
1832 "id32db", "id32db", DB_HASH,
1836 logthing(LOGTHING_CRITICAL,
1837 "Error opening id32 database: %s (%s)",
1844 ret = db_create(&privctx->id64db, privctx->dbenv, 0);
1846 logthing(LOGTHING_CRITICAL, "db_create: %s",
1852 ret = privctx->id64db->set_flags(privctx->id64db, DB_DUP);
1856 ret = privctx->id64db->open(privctx->id64db, privctx->txn,
1857 "id64db", "id64db", DB_HASH,
1861 logthing(LOGTHING_CRITICAL,
1862 "Error opening id64 database: %s (%s)",
1869 ret = db_create(&privctx->skshashdb, privctx->dbenv, 0);
1871 logthing(LOGTHING_CRITICAL, "db_create: %s",
1877 ret = privctx->skshashdb->open(privctx->skshashdb, privctx->txn,
1879 "skshashdb", DB_HASH,
1883 logthing(LOGTHING_CRITICAL,
1884 "Error opening skshash database: %s (%s)",
1891 ret = db_create(&privctx->subkeydb, privctx->dbenv, 0);
1893 logthing(LOGTHING_CRITICAL, "db_create: %s",
1899 ret = privctx->subkeydb->open(privctx->subkeydb, privctx->txn,
1900 "subkeydb", "subkeydb",
1905 logthing(LOGTHING_CRITICAL,
1906 "Error opening subkey database: %s (%s)",
1912 if (privctx->txn != NULL) {
1913 db4_endtrans(dbctx);
1917 db4_cleanupdb(dbctx);
1918 logthing(LOGTHING_CRITICAL,
1919 "Error opening database; exiting");
1923 dbctx->cleanupdb = db4_cleanupdb;
1924 dbctx->starttrans = db4_starttrans;
1925 dbctx->endtrans = db4_endtrans;
1926 dbctx->fetch_key_id = db4_fetch_key_id;
1927 dbctx->fetch_key_fp = db4_fetch_key_fp;
1928 dbctx->fetch_key_text = db4_fetch_key_text;
1929 dbctx->fetch_key_skshash = db4_fetch_key_skshash;
1930 dbctx->store_key = db4_store_key;
1931 dbctx->update_keys = generic_update_keys;
1932 dbctx->delete_key = db4_delete_key;
1933 dbctx->getkeysigs = generic_getkeysigs;
1934 dbctx->cached_getkeysigs = generic_cached_getkeysigs;
1935 dbctx->keyid2uid = generic_keyid2uid;
1936 dbctx->getfullkeyid = db4_getfullkeyid;
1937 dbctx->iterate_keys = db4_iterate_keys;