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"
48 * dbenv - our database environment.
50 static DB_ENV *dbenv = NULL;
53 * numdb - The number of database files we have.
55 static int numdbs = 16;
58 * dbconn - our connections to the key database files.
60 static DB **dbconns = NULL;
63 * worddb - our connection to the word database.
65 static DB *worddb = NULL;
68 * id32db - our connection to the 32bit ID database.
70 static DB *id32db = NULL;
73 * skshashdb - our connection to the SKS hash database.
75 static DB *skshashdb = NULL;
78 * subkeydb - our connection to the subkey ID lookup database.
80 static DB *subkeydb = NULL;
83 * txn - our current transaction id.
85 static DB_TXN *txn = NULL;
87 DB *keydb(uint64_t keyid)
93 return(dbconns[keytrun % numdbs]);
97 * db4_errfunc - Direct DB errors to logfile
99 * Basic function to take errors from the DB library and output them to
100 * the logfile rather than stderr.
102 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR < 3)
103 static void db4_errfunc(const char *errpfx, const char *errmsg)
105 static void db4_errfunc(const DB_ENV *edbenv, const char *errpfx,
110 logthing(LOGTHING_DEBUG, "db4 error: %s:%s", errpfx, errmsg);
112 logthing(LOGTHING_DEBUG, "db4 error: %s", errmsg);
119 * starttrans - Start a transaction.
121 * Start a transaction. Intended to be used if we're about to perform many
122 * operations on the database to help speed it all up, or if we want
123 * something to only succeed if all relevant operations are successful.
125 static bool db4_starttrans(void)
129 log_assert(dbenv != NULL);
130 log_assert(txn == NULL);
132 ret = dbenv->txn_begin(dbenv,
133 NULL, /* No parent transaction */
137 logthing(LOGTHING_CRITICAL,
138 "Error starting transaction: %s",
147 * endtrans - End a transaction.
149 * Ends a transaction.
151 static void db4_endtrans(void)
155 log_assert(dbenv != NULL);
156 log_assert(txn != NULL);
158 ret = txn->commit(txn,
161 logthing(LOGTHING_CRITICAL,
162 "Error ending transaction: %s",
172 * cleanupdb - De-initialize the key database.
174 * This function should be called upon program exit to allow the DB to
175 * cleanup after itself.
177 static void db4_cleanupdb(void)
182 dbenv->txn_checkpoint(dbenv, 0, 0, 0);
183 if (subkeydb != NULL) {
184 subkeydb->close(subkeydb, 0);
187 if (skshashdb != NULL) {
188 skshashdb->close(skshashdb, 0);
191 if (id32db != NULL) {
192 id32db->close(id32db, 0);
195 if (worddb != NULL) {
196 worddb->close(worddb, 0);
199 for (i = 0; i < numdbs; i++) {
200 if (dbconns[i] != NULL) {
201 dbconns[i]->close(dbconns[i], 0);
207 dbenv->close(dbenv, 0);
213 * db4_upgradedb - Upgrade a DB4 database
215 * Called if we discover we need to upgrade our DB4 database; ie if
216 * we're running with a newer version of db4 than the database was
219 static int db4_upgradedb(int numdb)
228 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
230 lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
231 if (lockfile_fd < 0) {
232 if (errno == EEXIST) {
233 while (stat(buf, &statbuf) == 0) ;
236 logthing(LOGTHING_CRITICAL, "Couldn't open database "
237 "update lock file: %s", strerror(errno));
241 snprintf(buf, sizeof(buf) - 1, "%d", getpid());
242 write(lockfile_fd, buf, strlen(buf));
245 logthing(LOGTHING_NOTICE, "Upgrading DB4 database");
246 ret = db_env_create(&dbenv, 0);
247 dbenv->set_errcall(dbenv, &db4_errfunc);
248 dbenv->remove(dbenv, config.db_dir, 0);
250 for (i = 0; i < numdb; i++) {
251 ret = db_create(&curdb, NULL, 0);
253 snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
255 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
256 ret = curdb->upgrade(curdb, buf, 0);
257 curdb->close(curdb, 0);
259 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
265 ret = db_create(&curdb, NULL, 0);
267 snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
268 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
269 ret = curdb->upgrade(curdb, buf, 0);
270 curdb->close(curdb, 0);
272 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
277 ret = db_create(&curdb, NULL, 0);
279 snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
280 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
281 ret = curdb->upgrade(curdb, buf, 0);
282 curdb->close(curdb, 0);
284 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
289 ret = db_create(&curdb, NULL, 0);
291 snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
292 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
293 ret = curdb->upgrade(curdb, buf, 0);
294 curdb->close(curdb, 0);
296 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
301 ret = db_create(&curdb, NULL, 0);
303 snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
304 logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
305 ret = curdb->upgrade(curdb, buf, 0);
306 curdb->close(curdb, 0);
308 logthing(LOGTHING_ERROR, "Error upgrading DB %s : %s",
313 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
321 * initdb - Initialize the key database.
323 * This function should be called before any of the other functions in
324 * this file are called in order to allow the DB to be initialized ready
327 static void db4_initdb(bool readonly)
337 snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
339 ret = stat(buf, &statbuf);
340 while ((ret == 0) || (errno != ENOENT)) {
342 logthing(LOGTHING_CRITICAL, "Couldn't stat upgrade "
343 "lock file: %s (%d)", strerror(errno), ret);
346 logthing(LOGTHING_DEBUG, "DB4 upgrade in progress; waiting.");
348 ret = stat(buf, &statbuf);
352 snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
353 numdb = fopen(buf, "r");
355 if (fgets(buf, sizeof(buf), numdb) != NULL) {
359 } else if (!readonly) {
360 logthing(LOGTHING_ERROR, "Couldn't open num_keydb: %s",
362 numdb = fopen(buf, "w");
364 fprintf(numdb, "%d", numdbs);
367 logthing(LOGTHING_ERROR,
368 "Couldn't write num_keydb: %s",
373 dbconns = calloc(numdbs, sizeof (DB *));
374 if (dbconns == NULL) {
375 logthing(LOGTHING_CRITICAL,
376 "Couldn't allocate memory for dbconns");
381 ret = db_env_create(&dbenv, 0);
383 logthing(LOGTHING_CRITICAL,
384 "db_env_create: %s", db_strerror(ret));
389 * Up the number of locks we're allowed at once. We base this on
390 * the maximum number of keys we're going to return.
392 maxlocks = config.maxkeys * 16;
393 if (maxlocks < 1000) {
396 dbenv->set_lk_max_locks(dbenv, maxlocks);
397 dbenv->set_lk_max_objects(dbenv, maxlocks);
400 * Enable deadlock detection so that we don't block indefinitely on
401 * anything. What we really want is simple 2 state locks, but I'm not
402 * sure how to make the standard DB functions do that yet.
405 dbenv->set_errcall(dbenv, &db4_errfunc);
406 ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
408 logthing(LOGTHING_CRITICAL,
409 "db_env_create: %s", db_strerror(ret));
414 ret = dbenv->open(dbenv, config.db_dir,
415 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
419 #ifdef DB_VERSION_MISMATCH
420 if (ret == DB_VERSION_MISMATCH) {
421 dbenv->close(dbenv, 0);
423 ret = db4_upgradedb(numdbs);
425 ret = db_env_create(&dbenv, 0);
428 dbenv->set_errcall(dbenv, &db4_errfunc);
429 dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
430 ret = dbenv->open(dbenv, config.db_dir,
431 DB_INIT_LOG | DB_INIT_MPOOL |
432 DB_INIT_LOCK | DB_INIT_TXN |
433 DB_CREATE | DB_RECOVER,
437 dbenv->txn_checkpoint(dbenv,
446 logthing(LOGTHING_CRITICAL,
447 "Error opening db environment: %s (%s)",
450 dbenv->close(dbenv, 0);
458 for (i = 0; !ret && i < numdbs; i++) {
459 ret = db_create(&dbconns[i], dbenv, 0);
461 logthing(LOGTHING_CRITICAL,
462 "db_create: %s", db_strerror(ret));
466 snprintf(buf, 1023, "keydb.%d.db", i);
471 ret = dbconns[i]->open(dbconns[i],
479 logthing(LOGTHING_CRITICAL,
480 "Error opening key database:"
491 ret = db_create(&worddb, dbenv, 0);
493 logthing(LOGTHING_CRITICAL, "db_create: %s",
499 ret = worddb->set_flags(worddb, DB_DUP);
503 ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
507 logthing(LOGTHING_CRITICAL,
508 "Error opening word database: %s (%s)",
515 ret = db_create(&id32db, dbenv, 0);
517 logthing(LOGTHING_CRITICAL, "db_create: %s",
523 ret = id32db->set_flags(id32db, DB_DUP);
527 ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
531 logthing(LOGTHING_CRITICAL,
532 "Error opening id32 database: %s (%s)",
539 ret = db_create(&skshashdb, dbenv, 0);
541 logthing(LOGTHING_CRITICAL, "db_create: %s",
547 ret = skshashdb->open(skshashdb, txn, "skshashdb",
548 "skshashdb", DB_HASH,
552 logthing(LOGTHING_CRITICAL,
553 "Error opening skshash database: %s (%s)",
560 ret = db_create(&subkeydb, dbenv, 0);
562 logthing(LOGTHING_CRITICAL, "db_create: %s",
568 ret = subkeydb->open(subkeydb, txn, "subkeydb", "subkeydb",
573 logthing(LOGTHING_CRITICAL,
574 "Error opening subkey database: %s (%s)",
586 logthing(LOGTHING_CRITICAL,
587 "Error opening database; exiting");
595 * getfullkeyid - Maps a 32bit key id to a 64bit one.
596 * @keyid: The 32bit keyid.
598 * This function maps a 32bit key id to the full 64bit one. It returns the
599 * full keyid. If the key isn't found a keyid of 0 is returned.
601 static uint64_t db4_getfullkeyid(uint64_t keyid)
605 uint32_t shortkeyid = 0;
608 if (keyid < 0x100000000LL) {
609 ret = id32db->cursor(id32db,
614 shortkeyid = keyid & 0xFFFFFFFF;
616 memset(&key, 0, sizeof(key));
617 memset(&data, 0, sizeof(data));
618 key.data = &shortkeyid;
619 key.size = sizeof(shortkeyid);
620 data.flags = DB_DBT_MALLOC;
622 ret = cursor->c_get(cursor,
628 keyid = *(uint64_t *) data.data;
630 if (data.data != NULL) {
636 ret = cursor->c_close(cursor);
644 * fetch_key - Given a keyid fetch the key from storage.
645 * @keyid: The keyid to fetch.
646 * @publickey: A pointer to a structure to return the key in.
647 * @intrans: If we're already in a transaction.
649 * We use the hex representation of the keyid as the filename to fetch the
650 * key from. The key is stored in the file as a binary OpenPGP stream of
651 * packets, so we can just use read_openpgp_stream() to read the packets
652 * in and then parse_keys() to parse the packets into a publickey
655 static int db4_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
658 struct openpgp_packet_list *packets = NULL;
662 struct buffer_ctx fetchbuf;
664 if (keyid < 0x100000000LL) {
665 keyid = db4_getfullkeyid(keyid);
668 memset(&key, 0, sizeof(key));
669 memset(&data, 0, sizeof(data));
674 key.size = sizeof(keyid);
681 ret = keydb(keyid)->get(keydb(keyid),
687 if (ret == DB_NOTFOUND) {
688 /* If we didn't find the key ID see if it's a subkey ID */
689 memset(&key, 0, sizeof(key));
690 memset(&data, 0, sizeof(data));
693 key.size = sizeof(keyid);
696 ret = subkeydb->get(subkeydb,
703 /* We got a subkey match; retrieve the actual key */
704 keyid = *(uint64_t *) data.data;
706 memset(&key, 0, sizeof(key));
707 memset(&data, 0, sizeof(data));
710 key.size = sizeof(keyid);
713 ret = keydb(keyid)->get(keydb(keyid),
722 fetchbuf.buffer = data.data;
724 fetchbuf.size = data.size;
725 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
727 parse_keys(packets, publickey);
728 free_packet_list(packets);
731 } else if (ret != DB_NOTFOUND) {
732 logthing(LOGTHING_ERROR,
733 "Problem retrieving key: %s",
744 int worddb_cmp(const void *d1, const void *d2)
746 return memcmp(d1, d2, 12);
750 * fetch_key_text - Trys to find the keys that contain the supplied text.
751 * @search: The text to search for.
752 * @publickey: A pointer to a structure to return the key in.
754 * This function searches for the supplied text and returns the keys that
757 static int db4_fetch_key_text(const char *search,
758 struct openpgp_publickey **publickey)
766 char *searchtext = NULL;
767 struct ll *wordlist = NULL;
768 struct ll *curword = NULL;
769 struct keyarray keylist = { NULL, 0, 0 };
770 struct keyarray newkeylist = { NULL, 0, 0 };
774 searchtext = strdup(search);
775 wordlist = makewordlist(wordlist, searchtext);
777 for (curword = wordlist; curword != NULL; curword = curword->next) {
780 ret = worddb->cursor(worddb,
785 memset(&key, 0, sizeof(key));
786 memset(&data, 0, sizeof(data));
787 key.data = curword->object;
788 key.size = strlen(curword->object);
789 data.flags = DB_DBT_MALLOC;
790 ret = cursor->c_get(cursor,
794 while (ret == 0 && strncmp(key.data, curword->object,
796 ((char *) curword->object)[key.size] == 0) {
798 for (i = 4; i < 12; i++) {
800 keyid += ((unsigned char *)
805 * Only add the keys containing this word if this is
806 * our first pass (ie we have no existing key list),
807 * or the key contained a previous word.
809 if (firstpass || array_find(&keylist, keyid)) {
810 array_add(&newkeylist, keyid);
816 ret = cursor->c_get(cursor,
821 array_free(&keylist);
822 keylist = newkeylist;
823 newkeylist.keys = NULL;
824 newkeylist.count = newkeylist.size = 0;
825 if (data.data != NULL) {
829 ret = cursor->c_close(cursor);
834 llfree(wordlist, NULL);
837 if (keylist.count > config.maxkeys) {
838 keylist.count = config.maxkeys;
842 for (i = 0; i < keylist.count; i++) {
843 numkeys += db4_fetch_key(keylist.keys[i],
847 array_free(&keylist);
856 static int db4_fetch_key_skshash(const struct skshash *hash,
857 struct openpgp_publickey **publickey)
864 ret = skshashdb->cursor(skshashdb,
869 memset(&key, 0, sizeof(key));
870 memset(&data, 0, sizeof(data));
871 key.data = (void *) hash->hash;
872 key.size = sizeof(hash->hash);
873 data.flags = DB_DBT_MALLOC;
875 ret = cursor->c_get(cursor,
881 keyid = *(uint64_t *) data.data;
883 if (data.data != NULL) {
889 ret = cursor->c_close(cursor);
892 return db4_fetch_key(keyid, publickey, false);
896 * delete_key - Given a keyid delete the key from storage.
897 * @keyid: The keyid to delete.
898 * @intrans: If we're already in a transaction.
900 * This function deletes a public key from whatever storage mechanism we
901 * are using. Returns 0 if the key existed.
903 static int db4_delete_key(uint64_t keyid, bool intrans)
905 struct openpgp_publickey *publickey = NULL;
908 uint32_t shortkeyid = 0;
909 uint64_t *subkeyids = NULL;
913 char *primary = NULL;
914 unsigned char worddb_data[12];
915 struct ll *wordlist = NULL;
916 struct ll *curword = NULL;
917 bool deadlock = false;
924 db4_fetch_key(keyid, &publickey, true);
927 * Walk through the uids removing the words from the worddb.
929 if (publickey != NULL) {
930 uids = keyuids(publickey, &primary);
933 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
934 wordlist = makewordlist(wordlist, uids[i]);
937 ret = worddb->cursor(worddb,
942 for (curword = wordlist; curword != NULL && !deadlock;
943 curword = curword->next) {
944 memset(&key, 0, sizeof(key));
945 memset(&data, 0, sizeof(data));
946 key.data = curword->object;
947 key.size = strlen(key.data);
948 data.data = worddb_data;
949 data.size = sizeof(worddb_data);
952 * Our data is the key creation time followed by the
955 worddb_data[ 0] = publickey->publickey->data[1];
956 worddb_data[ 1] = publickey->publickey->data[2];
957 worddb_data[ 2] = publickey->publickey->data[3];
958 worddb_data[ 3] = publickey->publickey->data[4];
959 worddb_data[ 4] = (keyid >> 56) & 0xFF;
960 worddb_data[ 5] = (keyid >> 48) & 0xFF;
961 worddb_data[ 6] = (keyid >> 40) & 0xFF;
962 worddb_data[ 7] = (keyid >> 32) & 0xFF;
963 worddb_data[ 8] = (keyid >> 24) & 0xFF;
964 worddb_data[ 9] = (keyid >> 16) & 0xFF;
965 worddb_data[10] = (keyid >> 8) & 0xFF;
966 worddb_data[11] = keyid & 0xFF;
968 ret = cursor->c_get(cursor,
974 ret = cursor->c_del(cursor, 0);
978 logthing(LOGTHING_ERROR,
979 "Problem deleting word: %s "
980 "(0x%016" PRIX64 ")",
983 if (ret == DB_LOCK_DEADLOCK) {
988 ret = cursor->c_close(cursor);
991 ret = skshashdb->cursor(skshashdb,
995 get_skshash(publickey, &hash);
997 memset(&key, 0, sizeof(key));
998 memset(&data, 0, sizeof(data));
999 key.data = hash.hash;
1000 key.size = sizeof(hash.hash);
1002 data.size = sizeof(keyid);
1004 ret = cursor->c_get(cursor,
1010 ret = cursor->c_del(cursor, 0);
1014 logthing(LOGTHING_ERROR,
1015 "Problem deleting skshash: %s "
1016 "(0x%016" PRIX64 ")",
1019 if (ret == DB_LOCK_DEADLOCK) {
1024 ret = cursor->c_close(cursor);
1028 * Free our UID and word lists.
1030 llfree(wordlist, NULL);
1031 for (i = 0; uids[i] != NULL; i++) {
1037 free_publickey(publickey);
1042 ret = id32db->cursor(id32db,
1047 shortkeyid = keyid & 0xFFFFFFFF;
1049 memset(&key, 0, sizeof(key));
1050 memset(&data, 0, sizeof(data));
1051 key.data = &shortkeyid;
1052 key.size = sizeof(shortkeyid);
1054 data.size = sizeof(keyid);
1056 ret = cursor->c_get(cursor,
1062 ret = cursor->c_del(cursor, 0);
1066 logthing(LOGTHING_ERROR,
1067 "Problem deleting short keyid: %s "
1068 "(0x%016" PRIX64 ")",
1071 if (ret == DB_LOCK_DEADLOCK) {
1076 subkeyids = keysubkeys(publickey);
1078 while (subkeyids != NULL && subkeyids[i] != 0) {
1079 memset(&key, 0, sizeof(key));
1080 key.data = &subkeyids[i];
1081 key.size = sizeof(subkeyids[i]);
1082 subkeydb->del(subkeydb, txn, &key, 0);
1084 logthing(LOGTHING_ERROR,
1085 "Problem deleting subkey id: %s "
1086 "(0x%016" PRIX64 ")",
1089 if (ret == DB_LOCK_DEADLOCK) {
1094 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
1096 memset(&key, 0, sizeof(key));
1097 memset(&data, 0, sizeof(data));
1098 key.data = &shortkeyid;
1099 key.size = sizeof(shortkeyid);
1101 data.size = sizeof(keyid);
1103 ret = cursor->c_get(cursor,
1109 ret = cursor->c_del(cursor, 0);
1113 logthing(LOGTHING_ERROR,
1114 "Problem deleting short keyid: %s "
1115 "(0x%016" PRIX64 ")",
1118 if (ret == DB_LOCK_DEADLOCK) {
1123 if (subkeyids != NULL) {
1127 ret = cursor->c_close(cursor);
1134 key.size = sizeof(keyid);
1136 keydb(keyid)->del(keydb(keyid),
1146 return deadlock ? (-1) : (ret == DB_NOTFOUND);
1150 * store_key - Takes a key and stores it.
1151 * @publickey: A pointer to the public key to store.
1152 * @intrans: If we're already in a transaction.
1153 * @update: If true the key exists and should be updated.
1155 * Again we just use the hex representation of the keyid as the filename
1156 * to store the key to. We flatten the public key to a list of OpenPGP
1157 * packets and then use write_openpgp_stream() to write the stream out to
1158 * the file. If update is true then we delete the old key first, otherwise
1159 * we trust that it doesn't exist.
1161 static int db4_store_key(struct openpgp_publickey *publickey, bool intrans,
1164 struct openpgp_packet_list *packets = NULL;
1165 struct openpgp_packet_list *list_end = NULL;
1166 struct openpgp_publickey *next = NULL;
1169 struct buffer_ctx storebuf;
1173 uint32_t shortkeyid = 0;
1174 uint64_t *subkeyids = NULL;
1176 char *primary = NULL;
1177 unsigned char worddb_data[12];
1178 struct ll *wordlist = NULL;
1179 struct ll *curword = NULL;
1180 bool deadlock = false;
1181 struct skshash hash;
1183 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
1184 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
1193 * Delete the key if we already have it.
1195 * TODO: Can we optimize this perhaps? Possibly when other data is
1196 * involved as well? I suspect this is easiest and doesn't make a lot
1197 * of difference though - the largest chunk of data is the keydata and
1198 * it definitely needs updated.
1201 deadlock = (db4_delete_key(keyid, true) == -1);
1205 * Convert the key to a flat set of binary data.
1208 next = publickey->next;
1209 publickey->next = NULL;
1210 flatten_publickey(publickey, &packets, &list_end);
1211 publickey->next = next;
1213 storebuf.offset = 0;
1214 storebuf.size = 8192;
1215 storebuf.buffer = malloc(8192);
1217 write_openpgp_stream(buffer_putchar, &storebuf, packets);
1220 * Now we have the key data store it in the DB; the keyid is
1223 memset(&key, 0, sizeof(key));
1224 memset(&data, 0, sizeof(data));
1226 key.size = sizeof(keyid);
1227 data.size = storebuf.offset;
1228 data.data = storebuf.buffer;
1230 ret = keydb(keyid)->put(keydb(keyid),
1236 logthing(LOGTHING_ERROR,
1237 "Problem storing key: %s",
1239 if (ret == DB_LOCK_DEADLOCK) {
1244 free(storebuf.buffer);
1245 storebuf.buffer = NULL;
1247 storebuf.offset = 0;
1249 free_packet_list(packets);
1254 * Walk through our uids storing the words into the db with the keyid.
1257 uids = keyuids(publickey, &primary);
1260 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1261 wordlist = makewordlist(wordlist, uids[i]);
1264 for (curword = wordlist; curword != NULL && !deadlock;
1265 curword = curword->next) {
1266 memset(&key, 0, sizeof(key));
1267 memset(&data, 0, sizeof(data));
1268 key.data = curword->object;
1269 key.size = strlen(key.data);
1270 data.data = worddb_data;
1271 data.size = sizeof(worddb_data);
1274 * Our data is the key creation time followed by the
1277 worddb_data[ 0] = publickey->publickey->data[1];
1278 worddb_data[ 1] = publickey->publickey->data[2];
1279 worddb_data[ 2] = publickey->publickey->data[3];
1280 worddb_data[ 3] = publickey->publickey->data[4];
1281 worddb_data[ 4] = (keyid >> 56) & 0xFF;
1282 worddb_data[ 5] = (keyid >> 48) & 0xFF;
1283 worddb_data[ 6] = (keyid >> 40) & 0xFF;
1284 worddb_data[ 7] = (keyid >> 32) & 0xFF;
1285 worddb_data[ 8] = (keyid >> 24) & 0xFF;
1286 worddb_data[ 9] = (keyid >> 16) & 0xFF;
1287 worddb_data[10] = (keyid >> 8) & 0xFF;
1288 worddb_data[11] = keyid & 0xFF;
1289 ret = worddb->put(worddb,
1295 logthing(LOGTHING_ERROR,
1296 "Problem storing word: %s",
1298 if (ret == DB_LOCK_DEADLOCK) {
1305 * Free our UID and word lists.
1307 llfree(wordlist, NULL);
1308 for (i = 0; uids[i] != NULL; i++) {
1317 * Write the truncated 32 bit keyid so we can lookup the full id for
1321 shortkeyid = keyid & 0xFFFFFFFF;
1323 memset(&key, 0, sizeof(key));
1324 memset(&data, 0, sizeof(data));
1325 key.data = &shortkeyid;
1326 key.size = sizeof(shortkeyid);
1328 data.size = sizeof(keyid);
1330 ret = id32db->put(id32db,
1336 logthing(LOGTHING_ERROR,
1337 "Problem storing short keyid: %s",
1339 if (ret == DB_LOCK_DEADLOCK) {
1346 subkeyids = keysubkeys(publickey);
1348 while (subkeyids != NULL && subkeyids[i] != 0) {
1349 /* Store the subkey ID -> main key ID mapping */
1350 memset(&key, 0, sizeof(key));
1351 memset(&data, 0, sizeof(data));
1352 key.data = &subkeyids[i];
1353 key.size = sizeof(subkeyids[i]);
1355 data.size = sizeof(keyid);
1357 ret = subkeydb->put(subkeydb,
1363 logthing(LOGTHING_ERROR,
1364 "Problem storing subkey keyid: %s",
1366 if (ret == DB_LOCK_DEADLOCK) {
1371 /* Store the short subkey ID -> main key ID mapping */
1372 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
1374 memset(&key, 0, sizeof(key));
1375 memset(&data, 0, sizeof(data));
1376 key.data = &shortkeyid;
1377 key.size = sizeof(shortkeyid);
1379 data.size = sizeof(keyid);
1381 ret = id32db->put(id32db,
1387 logthing(LOGTHING_ERROR,
1388 "Problem storing short keyid: %s",
1390 if (ret == DB_LOCK_DEADLOCK) {
1395 if (subkeyids != NULL) {
1402 get_skshash(publickey, &hash);
1403 memset(&key, 0, sizeof(key));
1404 memset(&data, 0, sizeof(data));
1405 key.data = hash.hash;
1406 key.size = sizeof(hash.hash);
1408 data.size = sizeof(keyid);
1410 ret = skshashdb->put(skshashdb,
1416 logthing(LOGTHING_ERROR,
1417 "Problem storing SKS hash: %s",
1419 if (ret == DB_LOCK_DEADLOCK) {
1429 return deadlock ? -1 : 0 ;
1433 * iterate_keys - call a function once for each key in the db.
1434 * @iterfunc: The function to call.
1435 * @ctx: A context pointer
1437 * Calls iterfunc once for each key in the database. ctx is passed
1438 * unaltered to iterfunc. This function is intended to aid database dumps
1439 * and statistic calculations.
1441 * Returns the number of keys we iterated over.
1443 static int db4_iterate_keys(void (*iterfunc)(void *ctx,
1444 struct openpgp_publickey *key), void *ctx)
1451 struct buffer_ctx fetchbuf;
1452 struct openpgp_packet_list *packets = NULL;
1453 struct openpgp_publickey *key = NULL;
1455 for (i = 0; i < numdbs; i++) {
1456 ret = dbconns[i]->cursor(dbconns[i],
1461 memset(&dbkey, 0, sizeof(dbkey));
1462 memset(&data, 0, sizeof(data));
1463 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1465 fetchbuf.buffer = data.data;
1466 fetchbuf.offset = 0;
1467 fetchbuf.size = data.size;
1468 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1470 parse_keys(packets, &key);
1474 free_publickey(key);
1476 free_packet_list(packets);
1479 memset(&dbkey, 0, sizeof(dbkey));
1480 memset(&data, 0, sizeof(data));
1481 ret = cursor->c_get(cursor, &dbkey, &data,
1485 if (ret != DB_NOTFOUND) {
1486 logthing(LOGTHING_ERROR,
1487 "Problem reading key: %s",
1491 ret = cursor->c_close(cursor);
1499 * Include the basic keydb routines.
1501 #define NEED_GETKEYSIGS 1
1502 #define NEED_KEYID2UID 1
1503 #define NEED_UPDATEKEYS 1
1506 struct dbfuncs keydb_db4_funcs = {
1507 .initdb = db4_initdb,
1508 .cleanupdb = db4_cleanupdb,
1509 .starttrans = db4_starttrans,
1510 .endtrans = db4_endtrans,
1511 .fetch_key = db4_fetch_key,
1512 .fetch_key_text = db4_fetch_key_text,
1513 .fetch_key_skshash = db4_fetch_key_skshash,
1514 .store_key = db4_store_key,
1515 .update_keys = generic_update_keys,
1516 .delete_key = db4_delete_key,
1517 .getkeysigs = generic_getkeysigs,
1518 .cached_getkeysigs = generic_cached_getkeysigs,
1519 .keyid2uid = generic_keyid2uid,
1520 .getfullkeyid = db4_getfullkeyid,
1521 .iterate_keys = db4_iterate_keys,