+ if (ret == 0) {
+ ret = db_env_create(&dbenv, 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "db_env_create: %s", db_strerror(ret));
+ }
+ }
+
+ /*
+ * Up the number of locks we're allowed at once. We base this on
+ * the maximum number of keys we're going to return.
+ */
+ maxlocks = config.maxkeys * 16;
+ if (maxlocks < 1000) {
+ maxlocks = 1000;
+ }
+ dbenv->set_lk_max_locks(dbenv, maxlocks);
+ dbenv->set_lk_max_objects(dbenv, maxlocks);
+
+ /*
+ * Enable deadlock detection so that we don't block indefinitely on
+ * anything. What we really want is simple 2 state locks, but I'm not
+ * sure how to make the standard DB functions do that yet.
+ */
+ if (ret == 0) {
+ dbenv->set_errcall(dbenv, &db4_errfunc);
+ ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "db_env_create: %s", db_strerror(ret));
+ }
+ }
+
+ if (ret == 0) {
+ ret = dbenv->open(dbenv, config.db_dir,
+ DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
+ DB_INIT_TXN |
+ DB_CREATE,
+ 0);
+#ifdef DB_VERSION_MISMATCH
+ if (ret == DB_VERSION_MISMATCH) {
+ dbenv->close(dbenv, 0);
+ dbenv = NULL;
+ ret = db4_upgradedb(numdbs);
+ if (ret == 0) {
+ ret = db_env_create(&dbenv, 0);
+ }
+ if (ret == 0) {
+ dbenv->set_errcall(dbenv, &db4_errfunc);
+ dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
+ ret = dbenv->open(dbenv, config.db_dir,
+ DB_INIT_LOG | DB_INIT_MPOOL |
+ DB_INIT_LOCK | DB_INIT_TXN |
+ DB_CREATE | DB_RECOVER,
+ 0);
+
+ if (ret == 0) {
+ dbenv->txn_checkpoint(dbenv,
+ 0,
+ 0,
+ DB_FORCE);
+ }
+ }
+ }
+#endif
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error opening db environment: %s (%s)",
+ config.db_dir,
+ db_strerror(ret));
+ dbenv->close(dbenv, 0);
+ dbenv = NULL;
+ }
+ }
+
+ if (ret == 0) {
+ db4_starttrans();
+
+ for (i = 0; !ret && i < numdbs; i++) {
+ ret = db_create(&dbconns[i], dbenv, 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "db_create: %s", db_strerror(ret));
+ }
+
+ if (ret == 0) {
+ snprintf(buf, 1023, "keydb.%d.db", i);
+ flags = DB_CREATE;
+ if (readonly) {
+ flags = DB_RDONLY;
+ }
+ ret = dbconns[i]->open(dbconns[i],
+ txn,
+ buf,
+ "keydb",
+ DB_HASH,
+ flags,
+ 0664);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error opening key database:"
+ " %s (%s)",
+ buf,
+ db_strerror(ret));
+ }
+ }
+ }
+
+ }
+
+ if (ret == 0) {
+ ret = db_create(&worddb, dbenv, 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL, "db_create: %s",
+ db_strerror(ret));
+ }
+ }
+
+ if (ret == 0) {
+ ret = worddb->set_flags(worddb, DB_DUP);
+ }
+
+ if (ret == 0) {
+ ret = worddb->open(worddb, txn, "worddb", "worddb", DB_BTREE,
+ flags,
+ 0664);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error opening word database: %s (%s)",
+ "worddb",
+ db_strerror(ret));
+ }
+ }
+
+ if (ret == 0) {
+ ret = db_create(&id32db, dbenv, 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL, "db_create: %s",
+ db_strerror(ret));
+ }
+ }
+
+ if (ret == 0) {
+ ret = id32db->set_flags(id32db, DB_DUP);
+ }
+
+ if (ret == 0) {
+ ret = id32db->open(id32db, txn, "id32db", "id32db", DB_HASH,
+ flags,
+ 0664);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error opening id32 database: %s (%s)",
+ "id32db",
+ db_strerror(ret));
+ }
+ }
+
+ if (ret == 0) {
+ ret = db_create(&skshashdb, dbenv, 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL, "db_create: %s",
+ db_strerror(ret));
+ }
+ }
+
+ if (ret == 0) {
+ ret = skshashdb->open(skshashdb, txn, "skshashdb",
+ "skshashdb", DB_HASH,
+ flags,
+ 0664);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error opening skshash database: %s (%s)",
+ "skshashdb",
+ db_strerror(ret));
+ }
+ }
+
+ if (txn != NULL) {
+ db4_endtrans();
+ }
+
+ if (ret != 0) {
+ db4_cleanupdb();
+ logthing(LOGTHING_CRITICAL,
+ "Error opening database; exiting");
+ exit(EXIT_FAILURE);
+ }
+
+ return;
+}
+
+/**
+ * getfullkeyid - Maps a 32bit key id to a 64bit one.
+ * @keyid: The 32bit keyid.
+ *
+ * This function maps a 32bit key id to the full 64bit one. It returns the
+ * full keyid. If the key isn't found a keyid of 0 is returned.
+ */
+static uint64_t db4_getfullkeyid(uint64_t keyid)
+{
+ DBT key, data;
+ DBC *cursor = NULL;
+ uint32_t shortkeyid = 0;
+ int ret = 0;
+
+ if (keyid < 0x100000000LL) {
+ ret = id32db->cursor(id32db,
+ txn,
+ &cursor,
+ 0); /* flags */
+
+ shortkeyid = keyid & 0xFFFFFFFF;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+ key.data = &shortkeyid;
+ key.size = sizeof(shortkeyid);
+ data.flags = DB_DBT_MALLOC;
+
+ ret = cursor->c_get(cursor,
+ &key,
+ &data,
+ DB_SET);
+
+ if (ret == 0) {
+ keyid = *(uint64_t *) data.data;
+
+ if (data.data != NULL) {
+ free(data.data);
+ data.data = NULL;
+ }
+ }
+
+ ret = cursor->c_close(cursor);
+ cursor = NULL;
+ }
+
+ return keyid;
+}
+
+/**
+ * fetch_key - Given a keyid fetch the key from storage.
+ * @keyid: The keyid to fetch.
+ * @publickey: A pointer to a structure to return the key in.
+ * @intrans: If we're already in a transaction.
+ *
+ * We use the hex representation of the keyid as the filename to fetch the
+ * key from. The key is stored in the file as a binary OpenPGP stream of
+ * packets, so we can just use read_openpgp_stream() to read the packets
+ * in and then parse_keys() to parse the packets into a publickey
+ * structure.
+ */
+static int db4_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+ bool intrans)
+{
+ struct openpgp_packet_list *packets = NULL;
+ DBT key, data;
+ int ret = 0;
+ int numkeys = 0;
+ struct buffer_ctx fetchbuf;
+
+ if (keyid < 0x100000000LL) {
+ keyid = db4_getfullkeyid(keyid);
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ data.size = 0;
+ data.data = NULL;
+
+ key.size = sizeof(keyid);
+ key.data = &keyid;
+
+ if (!intrans) {
+ db4_starttrans();
+ }
+
+ ret = keydb(keyid)->get(keydb(keyid),
+ txn,
+ &key,
+ &data,
+ 0); /* flags*/
+
+ if (ret == 0) {
+ fetchbuf.buffer = data.data;
+ fetchbuf.offset = 0;
+ fetchbuf.size = data.size;
+ read_openpgp_stream(buffer_fetchchar, &fetchbuf,
+ &packets, 0);
+ parse_keys(packets, publickey);
+ free_packet_list(packets);
+ packets = NULL;
+ numkeys++;
+ } else if (ret != DB_NOTFOUND) {
+ logthing(LOGTHING_ERROR,
+ "Problem retrieving key: %s",
+ db_strerror(ret));
+ }
+
+ if (!intrans) {
+ db4_endtrans();
+ }
+
+ return (numkeys);
+}
+
+int worddb_cmp(const void *d1, const void *d2)
+{
+ return memcmp(d1, d2, 12);
+}
+
+/**
+ * fetch_key_text - Trys to find the keys that contain the supplied text.
+ * @search: The text to search for.
+ * @publickey: A pointer to a structure to return the key in.
+ *
+ * This function searches for the supplied text and returns the keys that
+ * contain it.
+ */
+static int db4_fetch_key_text(const char *search,
+ struct openpgp_publickey **publickey)
+{
+ DBC *cursor = NULL;
+ DBT key, data;
+ int ret;
+ uint64_t keyid;
+ int i;
+ int numkeys;
+ char *searchtext = NULL;
+ struct ll *wordlist = NULL;
+ struct ll *curword = NULL;
+ struct keyarray keylist = { NULL, 0, 0 };
+ struct keyarray newkeylist = { NULL, 0, 0 };
+ int firstpass = 1;
+
+ numkeys = 0;
+ searchtext = strdup(search);
+ wordlist = makewordlist(wordlist, searchtext);