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_id - 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_id(uint64_t keyid,
656 struct openpgp_publickey **publickey,
659 struct openpgp_packet_list *packets = NULL;
663 struct buffer_ctx fetchbuf;
665 if (keyid < 0x100000000LL) {
666 keyid = db4_getfullkeyid(keyid);
669 memset(&key, 0, sizeof(key));
670 memset(&data, 0, sizeof(data));
675 key.size = sizeof(keyid);
682 ret = keydb(keyid)->get(keydb(keyid),
688 if (ret == DB_NOTFOUND) {
689 /* If we didn't find the key ID see if it's a subkey ID */
690 memset(&key, 0, sizeof(key));
691 memset(&data, 0, sizeof(data));
694 key.size = sizeof(keyid);
697 ret = subkeydb->get(subkeydb,
704 /* We got a subkey match; retrieve the actual key */
705 keyid = *(uint64_t *) data.data;
707 memset(&key, 0, sizeof(key));
708 memset(&data, 0, sizeof(data));
711 key.size = sizeof(keyid);
714 ret = keydb(keyid)->get(keydb(keyid),
723 fetchbuf.buffer = data.data;
725 fetchbuf.size = data.size;
726 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
728 parse_keys(packets, publickey);
729 free_packet_list(packets);
732 } else if (ret != DB_NOTFOUND) {
733 logthing(LOGTHING_ERROR,
734 "Problem retrieving key: %s",
745 int worddb_cmp(const void *d1, const void *d2)
747 return memcmp(d1, d2, 12);
751 * fetch_key_text - Trys to find the keys that contain the supplied text.
752 * @search: The text to search for.
753 * @publickey: A pointer to a structure to return the key in.
755 * This function searches for the supplied text and returns the keys that
758 static int db4_fetch_key_text(const char *search,
759 struct openpgp_publickey **publickey)
767 char *searchtext = NULL;
768 struct ll *wordlist = NULL;
769 struct ll *curword = NULL;
770 struct keyarray keylist = { NULL, 0, 0 };
771 struct keyarray newkeylist = { NULL, 0, 0 };
775 searchtext = strdup(search);
776 wordlist = makewordlist(wordlist, searchtext);
778 for (curword = wordlist; curword != NULL; curword = curword->next) {
781 ret = worddb->cursor(worddb,
786 memset(&key, 0, sizeof(key));
787 memset(&data, 0, sizeof(data));
788 key.data = curword->object;
789 key.size = strlen(curword->object);
790 data.flags = DB_DBT_MALLOC;
791 ret = cursor->c_get(cursor,
795 while (ret == 0 && strncmp(key.data, curword->object,
797 ((char *) curword->object)[key.size] == 0) {
799 for (i = 4; i < 12; i++) {
801 keyid += ((unsigned char *)
806 * Only add the keys containing this word if this is
807 * our first pass (ie we have no existing key list),
808 * or the key contained a previous word.
810 if (firstpass || array_find(&keylist, keyid)) {
811 array_add(&newkeylist, keyid);
817 ret = cursor->c_get(cursor,
822 array_free(&keylist);
823 keylist = newkeylist;
824 newkeylist.keys = NULL;
825 newkeylist.count = newkeylist.size = 0;
826 if (data.data != NULL) {
830 ret = cursor->c_close(cursor);
835 llfree(wordlist, NULL);
838 if (keylist.count > config.maxkeys) {
839 keylist.count = config.maxkeys;
843 for (i = 0; i < keylist.count; i++) {
844 numkeys += db4_fetch_key_id(keylist.keys[i],
848 array_free(&keylist);
857 static int db4_fetch_key_skshash(const struct skshash *hash,
858 struct openpgp_publickey **publickey)
865 ret = skshashdb->cursor(skshashdb,
870 memset(&key, 0, sizeof(key));
871 memset(&data, 0, sizeof(data));
872 key.data = (void *) hash->hash;
873 key.size = sizeof(hash->hash);
874 data.flags = DB_DBT_MALLOC;
876 ret = cursor->c_get(cursor,
882 keyid = *(uint64_t *) data.data;
884 if (data.data != NULL) {
890 ret = cursor->c_close(cursor);
893 return db4_fetch_key_id(keyid, publickey, false);
897 * delete_key - Given a keyid delete the key from storage.
898 * @keyid: The keyid to delete.
899 * @intrans: If we're already in a transaction.
901 * This function deletes a public key from whatever storage mechanism we
902 * are using. Returns 0 if the key existed.
904 static int db4_delete_key(uint64_t keyid, bool intrans)
906 struct openpgp_publickey *publickey = NULL;
909 uint32_t shortkeyid = 0;
910 uint64_t *subkeyids = NULL;
914 char *primary = NULL;
915 unsigned char worddb_data[12];
916 struct ll *wordlist = NULL;
917 struct ll *curword = NULL;
918 bool deadlock = false;
925 db4_fetch_key_id(keyid, &publickey, true);
928 * Walk through the uids removing the words from the worddb.
930 if (publickey != NULL) {
931 uids = keyuids(publickey, &primary);
934 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
935 wordlist = makewordlist(wordlist, uids[i]);
938 ret = worddb->cursor(worddb,
943 for (curword = wordlist; curword != NULL && !deadlock;
944 curword = curword->next) {
945 memset(&key, 0, sizeof(key));
946 memset(&data, 0, sizeof(data));
947 key.data = curword->object;
948 key.size = strlen(key.data);
949 data.data = worddb_data;
950 data.size = sizeof(worddb_data);
953 * Our data is the key creation time followed by the
956 worddb_data[ 0] = publickey->publickey->data[1];
957 worddb_data[ 1] = publickey->publickey->data[2];
958 worddb_data[ 2] = publickey->publickey->data[3];
959 worddb_data[ 3] = publickey->publickey->data[4];
960 worddb_data[ 4] = (keyid >> 56) & 0xFF;
961 worddb_data[ 5] = (keyid >> 48) & 0xFF;
962 worddb_data[ 6] = (keyid >> 40) & 0xFF;
963 worddb_data[ 7] = (keyid >> 32) & 0xFF;
964 worddb_data[ 8] = (keyid >> 24) & 0xFF;
965 worddb_data[ 9] = (keyid >> 16) & 0xFF;
966 worddb_data[10] = (keyid >> 8) & 0xFF;
967 worddb_data[11] = keyid & 0xFF;
969 ret = cursor->c_get(cursor,
975 ret = cursor->c_del(cursor, 0);
979 logthing(LOGTHING_ERROR,
980 "Problem deleting word: %s "
981 "(0x%016" PRIX64 ")",
984 if (ret == DB_LOCK_DEADLOCK) {
989 ret = cursor->c_close(cursor);
992 ret = skshashdb->cursor(skshashdb,
996 get_skshash(publickey, &hash);
998 memset(&key, 0, sizeof(key));
999 memset(&data, 0, sizeof(data));
1000 key.data = hash.hash;
1001 key.size = sizeof(hash.hash);
1003 data.size = sizeof(keyid);
1005 ret = cursor->c_get(cursor,
1011 ret = cursor->c_del(cursor, 0);
1015 logthing(LOGTHING_ERROR,
1016 "Problem deleting skshash: %s "
1017 "(0x%016" PRIX64 ")",
1020 if (ret == DB_LOCK_DEADLOCK) {
1025 ret = cursor->c_close(cursor);
1029 * Free our UID and word lists.
1031 llfree(wordlist, NULL);
1032 for (i = 0; uids[i] != NULL; i++) {
1038 free_publickey(publickey);
1043 ret = id32db->cursor(id32db,
1048 shortkeyid = keyid & 0xFFFFFFFF;
1050 memset(&key, 0, sizeof(key));
1051 memset(&data, 0, sizeof(data));
1052 key.data = &shortkeyid;
1053 key.size = sizeof(shortkeyid);
1055 data.size = sizeof(keyid);
1057 ret = cursor->c_get(cursor,
1063 ret = cursor->c_del(cursor, 0);
1067 logthing(LOGTHING_ERROR,
1068 "Problem deleting short keyid: %s "
1069 "(0x%016" PRIX64 ")",
1072 if (ret == DB_LOCK_DEADLOCK) {
1077 subkeyids = keysubkeys(publickey);
1079 while (subkeyids != NULL && subkeyids[i] != 0) {
1080 memset(&key, 0, sizeof(key));
1081 key.data = &subkeyids[i];
1082 key.size = sizeof(subkeyids[i]);
1083 subkeydb->del(subkeydb, txn, &key, 0);
1085 logthing(LOGTHING_ERROR,
1086 "Problem deleting subkey id: %s "
1087 "(0x%016" PRIX64 ")",
1090 if (ret == DB_LOCK_DEADLOCK) {
1095 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
1097 memset(&key, 0, sizeof(key));
1098 memset(&data, 0, sizeof(data));
1099 key.data = &shortkeyid;
1100 key.size = sizeof(shortkeyid);
1102 data.size = sizeof(keyid);
1104 ret = cursor->c_get(cursor,
1110 ret = cursor->c_del(cursor, 0);
1114 logthing(LOGTHING_ERROR,
1115 "Problem deleting short keyid: %s "
1116 "(0x%016" PRIX64 ")",
1119 if (ret == DB_LOCK_DEADLOCK) {
1124 if (subkeyids != NULL) {
1128 ret = cursor->c_close(cursor);
1135 key.size = sizeof(keyid);
1137 keydb(keyid)->del(keydb(keyid),
1147 return deadlock ? (-1) : (ret == DB_NOTFOUND);
1151 * store_key - Takes a key and stores it.
1152 * @publickey: A pointer to the public key to store.
1153 * @intrans: If we're already in a transaction.
1154 * @update: If true the key exists and should be updated.
1156 * Again we just use the hex representation of the keyid as the filename
1157 * to store the key to. We flatten the public key to a list of OpenPGP
1158 * packets and then use write_openpgp_stream() to write the stream out to
1159 * the file. If update is true then we delete the old key first, otherwise
1160 * we trust that it doesn't exist.
1162 static int db4_store_key(struct openpgp_publickey *publickey, bool intrans,
1165 struct openpgp_packet_list *packets = NULL;
1166 struct openpgp_packet_list *list_end = NULL;
1167 struct openpgp_publickey *next = NULL;
1170 struct buffer_ctx storebuf;
1174 uint32_t shortkeyid = 0;
1175 uint64_t *subkeyids = NULL;
1177 char *primary = NULL;
1178 unsigned char worddb_data[12];
1179 struct ll *wordlist = NULL;
1180 struct ll *curword = NULL;
1181 bool deadlock = false;
1182 struct skshash hash;
1184 if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
1185 logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
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(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));
1227 key.size = sizeof(keyid);
1228 data.size = storebuf.offset;
1229 data.data = storebuf.buffer;
1231 ret = keydb(keyid)->put(keydb(keyid),
1237 logthing(LOGTHING_ERROR,
1238 "Problem storing key: %s",
1240 if (ret == DB_LOCK_DEADLOCK) {
1245 free(storebuf.buffer);
1246 storebuf.buffer = NULL;
1248 storebuf.offset = 0;
1250 free_packet_list(packets);
1255 * Walk through our uids storing the words into the db with the keyid.
1258 uids = keyuids(publickey, &primary);
1261 for (i = 0; ret == 0 && uids[i] != NULL; i++) {
1262 wordlist = makewordlist(wordlist, uids[i]);
1265 for (curword = wordlist; curword != NULL && !deadlock;
1266 curword = curword->next) {
1267 memset(&key, 0, sizeof(key));
1268 memset(&data, 0, sizeof(data));
1269 key.data = curword->object;
1270 key.size = strlen(key.data);
1271 data.data = worddb_data;
1272 data.size = sizeof(worddb_data);
1275 * Our data is the key creation time followed by the
1278 worddb_data[ 0] = publickey->publickey->data[1];
1279 worddb_data[ 1] = publickey->publickey->data[2];
1280 worddb_data[ 2] = publickey->publickey->data[3];
1281 worddb_data[ 3] = publickey->publickey->data[4];
1282 worddb_data[ 4] = (keyid >> 56) & 0xFF;
1283 worddb_data[ 5] = (keyid >> 48) & 0xFF;
1284 worddb_data[ 6] = (keyid >> 40) & 0xFF;
1285 worddb_data[ 7] = (keyid >> 32) & 0xFF;
1286 worddb_data[ 8] = (keyid >> 24) & 0xFF;
1287 worddb_data[ 9] = (keyid >> 16) & 0xFF;
1288 worddb_data[10] = (keyid >> 8) & 0xFF;
1289 worddb_data[11] = keyid & 0xFF;
1290 ret = worddb->put(worddb,
1296 logthing(LOGTHING_ERROR,
1297 "Problem storing word: %s",
1299 if (ret == DB_LOCK_DEADLOCK) {
1306 * Free our UID and word lists.
1308 llfree(wordlist, NULL);
1309 for (i = 0; uids[i] != NULL; i++) {
1318 * Write the truncated 32 bit keyid so we can lookup the full id for
1322 shortkeyid = keyid & 0xFFFFFFFF;
1324 memset(&key, 0, sizeof(key));
1325 memset(&data, 0, sizeof(data));
1326 key.data = &shortkeyid;
1327 key.size = sizeof(shortkeyid);
1329 data.size = sizeof(keyid);
1331 ret = id32db->put(id32db,
1337 logthing(LOGTHING_ERROR,
1338 "Problem storing short keyid: %s",
1340 if (ret == DB_LOCK_DEADLOCK) {
1347 subkeyids = keysubkeys(publickey);
1349 while (subkeyids != NULL && subkeyids[i] != 0) {
1350 /* Store the subkey ID -> main key ID mapping */
1351 memset(&key, 0, sizeof(key));
1352 memset(&data, 0, sizeof(data));
1353 key.data = &subkeyids[i];
1354 key.size = sizeof(subkeyids[i]);
1356 data.size = sizeof(keyid);
1358 ret = subkeydb->put(subkeydb,
1364 logthing(LOGTHING_ERROR,
1365 "Problem storing subkey keyid: %s",
1367 if (ret == DB_LOCK_DEADLOCK) {
1372 /* Store the short subkey ID -> main key ID mapping */
1373 shortkeyid = subkeyids[i++] & 0xFFFFFFFF;
1375 memset(&key, 0, sizeof(key));
1376 memset(&data, 0, sizeof(data));
1377 key.data = &shortkeyid;
1378 key.size = sizeof(shortkeyid);
1380 data.size = sizeof(keyid);
1382 ret = id32db->put(id32db,
1388 logthing(LOGTHING_ERROR,
1389 "Problem storing short keyid: %s",
1391 if (ret == DB_LOCK_DEADLOCK) {
1396 if (subkeyids != NULL) {
1403 get_skshash(publickey, &hash);
1404 memset(&key, 0, sizeof(key));
1405 memset(&data, 0, sizeof(data));
1406 key.data = hash.hash;
1407 key.size = sizeof(hash.hash);
1409 data.size = sizeof(keyid);
1411 ret = skshashdb->put(skshashdb,
1417 logthing(LOGTHING_ERROR,
1418 "Problem storing SKS hash: %s",
1420 if (ret == DB_LOCK_DEADLOCK) {
1430 return deadlock ? -1 : 0 ;
1434 * iterate_keys - call a function once for each key in the db.
1435 * @iterfunc: The function to call.
1436 * @ctx: A context pointer
1438 * Calls iterfunc once for each key in the database. ctx is passed
1439 * unaltered to iterfunc. This function is intended to aid database dumps
1440 * and statistic calculations.
1442 * Returns the number of keys we iterated over.
1444 static int db4_iterate_keys(void (*iterfunc)(void *ctx,
1445 struct openpgp_publickey *key), void *ctx)
1452 struct buffer_ctx fetchbuf;
1453 struct openpgp_packet_list *packets = NULL;
1454 struct openpgp_publickey *key = NULL;
1456 for (i = 0; i < numdbs; i++) {
1457 ret = dbconns[i]->cursor(dbconns[i],
1462 memset(&dbkey, 0, sizeof(dbkey));
1463 memset(&data, 0, sizeof(data));
1464 ret = cursor->c_get(cursor, &dbkey, &data, DB_NEXT);
1466 fetchbuf.buffer = data.data;
1467 fetchbuf.offset = 0;
1468 fetchbuf.size = data.size;
1469 read_openpgp_stream(buffer_fetchchar, &fetchbuf,
1471 parse_keys(packets, &key);
1475 free_publickey(key);
1477 free_packet_list(packets);
1480 memset(&dbkey, 0, sizeof(dbkey));
1481 memset(&data, 0, sizeof(data));
1482 ret = cursor->c_get(cursor, &dbkey, &data,
1486 if (ret != DB_NOTFOUND) {
1487 logthing(LOGTHING_ERROR,
1488 "Problem reading key: %s",
1492 ret = cursor->c_close(cursor);
1500 * Include the basic keydb routines.
1502 #define NEED_GETKEYSIGS 1
1503 #define NEED_KEYID2UID 1
1504 #define NEED_UPDATEKEYS 1
1505 #define NEED_GET_FP 1
1508 struct dbfuncs keydb_db4_funcs = {
1509 .initdb = db4_initdb,
1510 .cleanupdb = db4_cleanupdb,
1511 .starttrans = db4_starttrans,
1512 .endtrans = db4_endtrans,
1513 .fetch_key_id = db4_fetch_key_id,
1514 .fetch_key_fp = generic_fetch_key_fp,
1515 .fetch_key_text = db4_fetch_key_text,
1516 .fetch_key_skshash = db4_fetch_key_skshash,
1517 .store_key = db4_store_key,
1518 .update_keys = generic_update_keys,
1519 .delete_key = db4_delete_key,
1520 .getkeysigs = generic_getkeysigs,
1521 .cached_getkeysigs = generic_cached_getkeysigs,
1522 .keyid2uid = generic_keyid2uid,
1523 .getfullkeyid = db4_getfullkeyid,
1524 .iterate_keys = db4_iterate_keys,