X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;f=keydb_db4.c;h=f3d20dbcafd3ff8521981b455a05c110418f8dec;hb=adc800dbc424a1e246dd4a82a0c2e88eeda25531;hp=3e4a039e1cc0058a5512ecf17a37654fdbab4335;hpb=b6390a543c22add446f24ca6e4a28d9c5b7f49e8;p=onak.git diff --git a/keydb_db4.c b/keydb_db4.c index 3e4a039..f3d20db 100644 --- a/keydb_db4.c +++ b/keydb_db4.c @@ -13,8 +13,7 @@ * more details. * * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * this program. If not, see . */ #include @@ -23,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -37,7 +38,9 @@ #include "decodekey.h" #include "keystructs.h" #include "mem.h" +#include "ll.h" #include "log.h" +#include "onak.h" #include "onak-conf.h" #include "parsekey.h" #include "wordlist.h" @@ -161,16 +164,18 @@ static void db4_endtrans(struct onak_dbctx *dbctx) * we're running with a newer version of db4 than the database was * created with. */ -static int db4_upgradedb(struct onak_db4_dbctx *privctx) +static int db4_upgradedb(struct onak_dbctx *dbctx) { + struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv; DB *curdb = NULL; int ret; int i; char buf[1024]; int lockfile_fd; struct stat statbuf; + ssize_t written; - snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir, + snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location, DB4_UPGRADE_FILE); lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600); if (lockfile_fd < 0) { @@ -184,21 +189,29 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) } } snprintf(buf, sizeof(buf) - 1, "%d", getpid()); - write(lockfile_fd, buf, strlen(buf)); + written = write(lockfile_fd, buf, strlen(buf)); close(lockfile_fd); + if (written != strlen(buf)) { + logthing(LOGTHING_CRITICAL, "Couldn't write PID to lockfile: " + "%s", strerror(errno)); + snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location, + DB4_UPGRADE_FILE); + unlink(buf); + return -1; + } logthing(LOGTHING_NOTICE, "Upgrading DB4 database"); ret = db_env_create(&privctx->dbenv, 0); if (ret == 0) { privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc); - privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0); + privctx->dbenv->remove(privctx->dbenv, dbctx->config->location, 0); privctx->dbenv = NULL; } for (i = 0; i < privctx->numdbs; i++) { ret = db_create(&curdb, NULL, 0); if (ret == 0) { snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db", - config.db_dir, i); + dbctx->config->location, i); logthing(LOGTHING_DEBUG, "Upgrading %s", buf); curdb->upgrade(curdb, buf, 0); curdb->close(curdb, 0); @@ -211,7 +224,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) ret = db_create(&curdb, NULL, 0); if (ret == 0) { - snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir); + snprintf(buf, sizeof(buf) - 1, "%s/worddb", dbctx->config->location); logthing(LOGTHING_DEBUG, "Upgrading %s", buf); curdb->upgrade(curdb, buf, 0); curdb->close(curdb, 0); @@ -223,7 +236,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) ret = db_create(&curdb, NULL, 0); if (ret == 0) { - snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir); + snprintf(buf, sizeof(buf) - 1, "%s/id32db", dbctx->config->location); logthing(LOGTHING_DEBUG, "Upgrading %s", buf); curdb->upgrade(curdb, buf, 0); curdb->close(curdb, 0); @@ -235,7 +248,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) ret = db_create(&curdb, NULL, 0); if (ret == 0) { - snprintf(buf, sizeof(buf) - 1, "%s/id64db", config.db_dir); + snprintf(buf, sizeof(buf) - 1, "%s/id64db", dbctx->config->location); logthing(LOGTHING_DEBUG, "Upgrading %s", buf); curdb->upgrade(curdb, buf, 0); curdb->close(curdb, 0); @@ -247,7 +260,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) ret = db_create(&curdb, NULL, 0); if (ret == 0) { - snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir); + snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", dbctx->config->location); logthing(LOGTHING_DEBUG, "Upgrading %s", buf); curdb->upgrade(curdb, buf, 0); curdb->close(curdb, 0); @@ -259,7 +272,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) ret = db_create(&curdb, NULL, 0); if (ret == 0) { - snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir); + snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", dbctx->config->location); logthing(LOGTHING_DEBUG, "Upgrading %s", buf); curdb->upgrade(curdb, buf, 0); curdb->close(curdb, 0); @@ -269,7 +282,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx) db_strerror(ret)); } - snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir, + snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location, DB4_UPGRADE_FILE); unlink(buf); @@ -772,7 +785,9 @@ static int db4_delete_key(struct onak_dbctx *dbctx, } if (db4_fetch_key_id(dbctx, keyid, &publickey, true) == 0) { - db4_endtrans(dbctx); + if (!intrans) { + db4_endtrans(dbctx); + } return 1; } @@ -826,7 +841,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx, DB_GET_BOTH); if (ret == 0) { - ret = cursor->c_del(cursor, 0); + cursor->c_del(cursor, 0); } /* New style just uses the fingerprint as the data */ @@ -898,7 +913,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx, DB_GET_BOTH); if (ret == 0) { - ret = cursor->c_del(cursor, 0); + cursor->c_del(cursor, 0); } /* New style mapping to fingerprint */ @@ -993,7 +1008,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx, DB_GET_BOTH); if (ret == 0) { - ret = cursor->c_del(cursor, 0); + cursor->c_del(cursor, 0); } /* Remove 32 bit keyid -> fingerprint mapping */ @@ -1085,7 +1100,7 @@ static int db4_delete_key(struct onak_dbctx *dbctx, DB_GET_BOTH); if (ret == 0) { - ret = cursor->c_del(cursor, 0); + cursor->c_del(cursor, 0); } /* Then delete new style fingerprint mapping */ @@ -1124,6 +1139,16 @@ static int db4_delete_key(struct onak_dbctx *dbctx, publickey = NULL; if (!deadlock) { + key.data = fingerprint.fp; + key.size = fingerprint.length; + + keydb_fp(privctx, &fingerprint)->del(keydb_fp(privctx, + &fingerprint), + privctx->txn, + &key, + 0); /* flags */ + + /* Delete old style 64 bit keyid */ key.data = &keyid; key.size = sizeof(keyid); @@ -1605,7 +1630,7 @@ static void db4_cleanupdb(struct onak_dbctx *dbctx) * this file are called in order to allow the DB to be initialized ready * for access. */ -struct onak_dbctx *keydb_db4_init(bool readonly) +struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly) { char buf[1024]; FILE *numdb = NULL; @@ -1621,6 +1646,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) if (dbctx == NULL) { return NULL; } + dbctx->config = dbcfg; dbctx->priv = privctx = calloc(1, sizeof(*privctx)); if (privctx == NULL) { free(dbctx); @@ -1630,7 +1656,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) /* Default to 16 key data DBs */ privctx->numdbs = 16; - snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir, + snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location, DB4_UPGRADE_FILE); ret = stat(buf, &statbuf); while ((ret == 0) || (errno != ENOENT)) { @@ -1645,7 +1671,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) } ret = 0; - snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir); + snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location); numdb = fopen(buf, "r"); if (numdb != NULL) { if (fgets(buf, sizeof(buf), numdb) != NULL) { @@ -1685,12 +1711,14 @@ struct onak_dbctx *keydb_db4_init(bool readonly) * 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; + if (ret == 0) { + maxlocks = config.maxkeys * 16; + if (maxlocks < 1000) { + maxlocks = 1000; + } + privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks); + privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks); } - privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks); - privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks); /* * Enable deadlock detection so that we don't block indefinitely on @@ -1707,7 +1735,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) } if (ret == 0) { - ret = privctx->dbenv->open(privctx->dbenv, config.db_dir, + ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location, DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_CREATE, @@ -1716,7 +1744,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) if (ret == DB_VERSION_MISMATCH) { privctx->dbenv->close(privctx->dbenv, 0); privctx->dbenv = NULL; - ret = db4_upgradedb(privctx); + ret = db4_upgradedb(dbctx); if (ret == 0) { ret = db_env_create(&privctx->dbenv, 0); } @@ -1726,7 +1754,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) privctx->dbenv->set_lk_detect(privctx->dbenv, DB_LOCK_DEFAULT); ret = privctx->dbenv->open(privctx->dbenv, - config.db_dir, + dbcfg->location, DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_CREATE | DB_RECOVER, @@ -1745,7 +1773,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly) if (ret != 0) { logthing(LOGTHING_CRITICAL, "Error opening db environment: %s (%s)", - config.db_dir, + dbcfg->location, db_strerror(ret)); if (privctx->dbenv != NULL) { privctx->dbenv->close(privctx->dbenv, 0);