X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keydb_fs.c;h=277929734c633ab14bb2fd00b2fea1dfe19c7db6;hb=5e40e676a7c4f640c9f850a379300c01a5fca69c;hp=9e072bab0f240e9205668c03769032711627e2a6;hpb=8e0907be1d73011075a99a0c029c56664e12843e;p=onak.git diff --git a/keydb_fs.c b/keydb_fs.c index 9e072ba..2779297 100644 --- a/keydb_fs.c +++ b/keydb_fs.c @@ -128,15 +128,23 @@ static void worddir(char *buffer, size_t length, char *word, uint32_t hash) (uint8_t) ((hash >> 16) & 0xFF), hash, word); } -static void subkeypath(char *buffer, size_t length, uint64_t subkey, - uint64_t keyid) +static void subkeypath(char *buffer, size_t length, uint64_t subkey) { snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64, config.db_dir, (uint8_t) ((subkey >> 24) & 0xFF), (uint8_t) ((subkey >> 16) & 0xFF), (uint32_t) (subkey & 0xFFFFFFFF), - keyid); + subkey); +} + +static void subkeydir(char *buffer, size_t length, uint64_t subkey) +{ + snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X", + config.db_dir, + (uint8_t) ((subkey >> 24) & 0xFF), + (uint8_t) ((subkey >> 16) & 0xFF), + (uint32_t) (subkey & 0xFFFFFFFF)); } static void skshashpath(char *buffer, size_t length, @@ -152,14 +160,6 @@ static void skshashpath(char *buffer, size_t length, hash->hash[12], hash->hash[13], hash->hash[14], hash->hash[15]); } -static void subkeydir(char *buffer, size_t length, uint64_t subkey) -{ - snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X", - config.db_dir, - (uint8_t) ((subkey >> 24) & 0xFF), - (uint8_t) ((subkey >> 16) & 0xFF), - (uint32_t) (subkey & 0xFFFFFFFF)); -} /*****************************************************************************/ @@ -260,7 +260,13 @@ static int fs_fetch_key_id(struct onak_dbctx *dbctx, keyid = fs_getfullkeyid(dbctx, keyid); keypath(buffer, sizeof(buffer), keyid); - if ((fd = open(buffer, O_RDONLY)) != -1) { + fd = open(buffer, O_RDONLY); + if (fd == -1 && errno == ENOENT) { + subkeypath(buffer, sizeof(buffer), keyid); + fd = open(buffer, O_RDONLY); + } + + if (fd != -1) { /* File is present, load it in... */ read_openpgp_stream(file_fetchchar, &fd, &packets, 0); parse_keys(packets, publickey); @@ -294,7 +300,7 @@ static int fs_store_key(struct onak_dbctx *dbctx, uint64_t keyid; struct ll *wordlist = NULL, *wl = NULL; struct skshash hash; - uint64_t *subkeyids = NULL; + struct openpgp_fingerprint *subkeyids = NULL; uint32_t hashid; int i = 0; @@ -342,13 +348,14 @@ static int fs_store_key(struct onak_dbctx *dbctx, subkeyids = keysubkeys(publickey); i = 0; - while (subkeyids != NULL && subkeyids[i] != 0) { - prove_path_to(subkeyids[i], "subkeys"); + while (subkeyids != NULL && subkeyids[i].length != 0) { + keyid = fingerprint2keyid(&subkeyids[i]); + + prove_path_to(keyid, "subkeys"); - subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]); + subkeydir(wbuffer, sizeof(wbuffer), keyid); mkdir(wbuffer, 0777); - subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i], - keyid); + subkeypath(wbuffer, sizeof(wbuffer), keyid); link(buffer, wbuffer); i++; @@ -383,7 +390,8 @@ static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans) struct openpgp_publickey *pk = NULL; struct skshash hash; struct ll *wordlist = NULL, *wl = NULL; - uint64_t *subkeyids = NULL; + struct openpgp_fingerprint *subkeyids = NULL; + uint64_t subkeyid; int i = 0; if ((keyid >> 32) == 0) @@ -413,11 +421,11 @@ static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans) subkeyids = keysubkeys(pk); i = 0; - while (subkeyids != NULL && subkeyids[i] != 0) { - prove_path_to(subkeyids[i], "subkeys"); + while (subkeyids != NULL && subkeyids[i].length != 0) { + subkeyid = fingerprint2keyid(&subkeyids[i]); + prove_path_to(subkeyid, "subkeys"); - subkeypath(buffer, sizeof(buffer), subkeyids[i], - keyid); + subkeypath(buffer, sizeof(buffer), subkeyid); unlink(buffer); i++; @@ -452,7 +460,7 @@ static struct ll *internal_get_key_by_word(char *word, struct ll *mct) d = opendir(buffer); logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word, buffer); - if (d) + if (d) { do { de = readdir(d); if (de && de->d_name[0] != '.') { @@ -470,7 +478,8 @@ static struct ll *internal_get_key_by_word(char *word, struct ll *mct) } } } while (de); - closedir(d); + closedir(d); + } return keys; } @@ -639,11 +648,18 @@ struct onak_dbctx *keydb_fs_init(bool readonly) mkdir(config.db_dir, 0777); privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600); } - chdir(config.db_dir); - if (privctx->lockfile_fd == -1) - privctx->lockfile_fd = open(buffer, - (privctx->lockfile_readonly) ? - O_RDONLY : O_RDWR); + if (chdir(config.db_dir) == -1) { + /* Shouldn't happen after the above */ + logthing(LOGTHING_CRITICAL, + "Couldn't change to database directory: %s", + strerror(errno)); + free(dbctx->priv); + free(dbctx); + return NULL; + } + privctx->lockfile_fd = open(buffer, + (privctx->lockfile_readonly) ? + O_RDONLY : O_RDWR); if (privctx->lockfile_fd == -1) privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600); if (privctx->lockfile_fd == -1) {