X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keydb_fs.c;h=277929734c633ab14bb2fd00b2fea1dfe19c7db6;hb=e125e9a83b730771cefe08f4a50280a8fe842f86;hp=ea09c7c0dae882e0684f3174c35397e26fccadaa;hpb=5e1b22d763640c4d7a09d07920403d8d491b4410;p=onak.git diff --git a/keydb_fs.c b/keydb_fs.c index ea09c7c..2779297 100644 --- a/keydb_fs.c +++ b/keydb_fs.c @@ -46,8 +46,10 @@ #define PATH_MAX 1024 #endif -static int keydb_lockfile_fd = -1; -static bool keydb_lockfile_readonly; +struct onak_fs_dbctx { + int lockfile_fd; + bool lockfile_readonly; +}; /*****************************************************************************/ @@ -126,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, @@ -150,76 +160,24 @@ 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)); -} /*****************************************************************************/ -/** - * initdb - Initialize the key database. - */ -static void fs_initdb(bool readonly) -{ - char buffer[PATH_MAX]; - - keydb_lockfile_readonly = readonly; - - snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir); - - if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) { - if (errno != ENOENT) { - logthing(LOGTHING_CRITICAL, - "Unable to access keydb_fs root of '%s'. (%s)", - config.db_dir, strerror(errno)); - exit(1); /* Lacking rwx on the key dir */ - } - mkdir(config.db_dir, 0777); - keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600); - } - chdir(config.db_dir); - if (keydb_lockfile_fd == -1) - keydb_lockfile_fd = open(buffer, - (keydb_lockfile_readonly) ? - O_RDONLY : O_RDWR); - if (keydb_lockfile_fd == -1) - keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600); - if (keydb_lockfile_fd == -1) { - logthing(LOGTHING_CRITICAL, - "Unable to open lockfile '%s'. (%s)", - buffer, strerror(errno)); - exit(1); /* Lacking rwx on the key dir */ - } -} - -/** - * cleanupdb - De-initialize the key database. - */ -static void fs_cleanupdb(void) -{ - /* Mmmm nothing to do here? */ - close(keydb_lockfile_fd); -} - /** * starttrans - Start a transaction. */ -static bool fs_starttrans(void) +static bool fs_starttrans(struct onak_dbctx *dbctx) { + struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv; struct flock lockstruct; int remaining = 20; lockstruct.l_type = - F_RDLCK | ((keydb_lockfile_readonly) ? 0 : F_WRLCK); + F_RDLCK | ((privctx->lockfile_readonly) ? 0 : F_WRLCK); lockstruct.l_whence = SEEK_SET; lockstruct.l_start = 0; lockstruct.l_len = 1; - while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) { + while (fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct) == -1) { if (remaining-- == 0) return false; /* Hope to hell that noodles DTRT */ usleep(100); @@ -230,18 +188,19 @@ static bool fs_starttrans(void) /** * endtrans - End a transaction. */ -static void fs_endtrans(void) +static void fs_endtrans(struct onak_dbctx *dbctx) { + struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv; struct flock lockstruct; lockstruct.l_type = F_UNLCK; lockstruct.l_whence = SEEK_SET; lockstruct.l_start = 0; lockstruct.l_len = 1; - fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct); + fcntl(privctx->lockfile_fd, F_SETLK, &lockstruct); } -static uint64_t fs_getfullkeyid(uint64_t keyid) +static uint64_t fs_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid) { static char buffer[PATH_MAX]; DIR *d = NULL; @@ -285,7 +244,9 @@ static uint64_t fs_getfullkeyid(uint64_t keyid) * @publickey: A pointer to a structure to return the key in. * @intrans: If we're already in a transaction. */ -static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, +static int fs_fetch_key_id(struct onak_dbctx *dbctx, + uint64_t keyid, + struct openpgp_publickey **publickey, bool intrans) { static char buffer[PATH_MAX]; @@ -293,13 +254,19 @@ static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, struct openpgp_packet_list *packets = NULL; if (!intrans) - fs_starttrans(); + fs_starttrans(dbctx); if ((keyid >> 32) == 0) - keyid = fs_getfullkeyid(keyid); + 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); @@ -310,7 +277,7 @@ static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, } if (!intrans) - fs_endtrans(); + fs_endtrans(dbctx); return ret; } @@ -320,7 +287,8 @@ static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, * @intrans: If we're already in a transaction. * @update: If true the key exists and should be updated. */ -static int fs_store_key(struct openpgp_publickey *publickey, bool intrans, +static int fs_store_key(struct onak_dbctx *dbctx, + struct openpgp_publickey *publickey, bool intrans, bool update) { static char buffer[PATH_MAX]; @@ -329,16 +297,20 @@ static int fs_store_key(struct openpgp_publickey *publickey, bool intrans, struct openpgp_packet_list *packets = NULL; struct openpgp_packet_list *list_end = NULL; struct openpgp_publickey *next = NULL; - uint64_t keyid = get_keyid(publickey); + 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; + if (get_keyid(publickey, &keyid) != ONAK_E_OK) { + logthing(LOGTHING_ERROR, "Couldn't find key ID for key."); + return 0; + } if (!intrans) - fs_starttrans(); + fs_starttrans(dbctx); prove_path_to(keyid, "key"); keypath(buffer, sizeof(buffer), keyid); @@ -376,13 +348,14 @@ static int fs_store_key(struct openpgp_publickey *publickey, bool intrans, 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++; @@ -401,7 +374,7 @@ static int fs_store_key(struct openpgp_publickey *publickey, bool intrans, } if (!intrans) - fs_endtrans(); + fs_endtrans(dbctx); return ret; } @@ -410,23 +383,24 @@ static int fs_store_key(struct openpgp_publickey *publickey, bool intrans, * @keyid: The keyid to delete. * @intrans: If we're already in a transaction. */ -static int fs_delete_key(uint64_t keyid, bool intrans) +static int fs_delete_key(struct onak_dbctx *dbctx, uint64_t keyid, bool intrans) { static char buffer[PATH_MAX]; int ret; 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) - keyid = fs_getfullkeyid(keyid); + keyid = fs_getfullkeyid(dbctx, keyid); if (!intrans) - fs_starttrans(); + fs_starttrans(dbctx); - ret = fs_fetch_key(keyid, &pk, true); + ret = fs_fetch_key_id(dbctx, keyid, &pk, true); if (ret) { logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64, @@ -447,11 +421,11 @@ static int fs_delete_key(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++; @@ -470,7 +444,7 @@ static int fs_delete_key(uint64_t keyid, bool intrans) unlink(buffer); if (!intrans) - fs_endtrans(); + fs_endtrans(dbctx); return 1; } @@ -486,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] != '.') { @@ -504,7 +478,8 @@ static struct ll *internal_get_key_by_word(char *word, struct ll *mct) } } } while (de); - closedir(d); + closedir(d); + } return keys; } @@ -514,7 +489,8 @@ static struct ll *internal_get_key_by_word(char *word, struct ll *mct) * @search: The text to search for. * @publickey: A pointer to a structure to return the key in. */ -static int fs_fetch_key_text(const char *search, +static int fs_fetch_key_text(struct onak_dbctx *dbctx, + const char *search, struct openpgp_publickey **publickey) { struct ll *wordlist = NULL, *wl = NULL; @@ -559,7 +535,8 @@ static int fs_fetch_key_text(const char *search, while (wl) { logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object); addedkeys += - fs_fetch_key(strtoull(wl->object, NULL, 16), publickey, + fs_fetch_key_id(dbctx, + strtoull(wl->object, NULL, 16), publickey, false); if (addedkeys >= config.maxkeys) break; @@ -579,7 +556,8 @@ static int fs_fetch_key_text(const char *search, * @publickey: A pointer to a structure to return the key in. * @intrans: If we're already in a transaction. */ -static int fs_fetch_key_skshash(const struct skshash *hash, +static int fs_fetch_key_skshash(struct onak_dbctx *dbctx, + const struct skshash *hash, struct openpgp_publickey **publickey) { static char buffer[PATH_MAX]; @@ -610,7 +588,8 @@ static int fs_fetch_key_skshash(const struct skshash *hash, * * Returns the number of keys we iterated over. */ -static int fs_iterate_keys(void (*iterfunc)(void *ctx, +static int fs_iterate_keys(struct onak_dbctx *dbctx, + void (*iterfunc)(void *ctx, struct openpgp_publickey *key), void *ctx) { return 0; @@ -622,22 +601,89 @@ static int fs_iterate_keys(void (*iterfunc)(void *ctx, #define NEED_KEYID2UID 1 #define NEED_GETKEYSIGS 1 #define NEED_UPDATEKEYS 1 +#define NEED_GET_FP 1 #include "keydb.c" -struct dbfuncs keydb_fs_funcs = { - .initdb = fs_initdb, - .cleanupdb = fs_cleanupdb, - .starttrans = fs_starttrans, - .endtrans = fs_endtrans, - .fetch_key = fs_fetch_key, - .fetch_key_text = fs_fetch_key_text, - .fetch_key_skshash = fs_fetch_key_skshash, - .store_key = fs_store_key, - .update_keys = generic_update_keys, - .delete_key = fs_delete_key, - .getkeysigs = generic_getkeysigs, - .cached_getkeysigs = generic_cached_getkeysigs, - .keyid2uid = generic_keyid2uid, - .getfullkeyid = fs_getfullkeyid, - .iterate_keys = fs_iterate_keys, -}; +/** + * cleanupdb - De-initialize the key database. + */ +static void fs_cleanupdb(struct onak_dbctx *dbctx) +{ + struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv; + + /* Mmmm nothing to do here? */ + close(privctx->lockfile_fd); +} + +/** + * initdb - Initialize the key database. + */ +struct onak_dbctx *keydb_fs_init(bool readonly) +{ + char buffer[PATH_MAX]; + struct onak_dbctx *dbctx; + struct onak_fs_dbctx *privctx; + + dbctx = malloc(sizeof(struct onak_dbctx)); + if (dbctx == NULL) { + return NULL; + } + dbctx->priv = privctx = malloc(sizeof(*privctx)); + if (privctx == NULL) { + free(dbctx); + return NULL; + } + + privctx->lockfile_readonly = readonly; + + snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir); + + if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) { + if (errno != ENOENT) { + logthing(LOGTHING_CRITICAL, + "Unable to access keydb_fs root of '%s'. (%s)", + config.db_dir, strerror(errno)); + exit(1); /* Lacking rwx on the key dir */ + } + mkdir(config.db_dir, 0777); + privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600); + } + 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) { + logthing(LOGTHING_CRITICAL, + "Unable to open lockfile '%s'. (%s)", + buffer, strerror(errno)); + exit(1); /* Lacking rwx on the key dir */ + } + + dbctx->cleanupdb = fs_cleanupdb; + dbctx->starttrans = fs_starttrans; + dbctx->endtrans = fs_endtrans; + dbctx->fetch_key_id = fs_fetch_key_id; + dbctx->fetch_key_fp = generic_fetch_key_fp; + dbctx->fetch_key_text = fs_fetch_key_text; + dbctx->fetch_key_skshash = fs_fetch_key_skshash; + dbctx->store_key = fs_store_key; + dbctx->update_keys = generic_update_keys; + dbctx->delete_key = fs_delete_key; + dbctx->getkeysigs = generic_getkeysigs; + dbctx->cached_getkeysigs = generic_cached_getkeysigs; + dbctx->keyid2uid = generic_keyid2uid; + dbctx->getfullkeyid = fs_getfullkeyid; + dbctx->iterate_keys = fs_iterate_keys; + + return dbctx; +}