X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;f=keydb%2Fkeydb_keyd.c;h=05b14872aa16c665f3bf0e3fac006f428e3f3d35;hb=f67f09be8979050e73855b6551da83ca48dde950;hp=e8f9961b79a6d2f9364ec53e6dd42abe70953ec5;hpb=51c1a7dd950efef6a4d00df1878341777f8064ff;p=onak.git diff --git a/keydb/keydb_keyd.c b/keydb/keydb_keyd.c index e8f9961..05b1487 100644 --- a/keydb/keydb_keyd.c +++ b/keydb/keydb_keyd.c @@ -28,6 +28,7 @@ #include #include +#include "build-config.h" #include "charfuncs.h" #include "keyd.h" #include "keydb.h" @@ -46,7 +47,7 @@ * operations on the database to help speed it all up, or if we want * something to only succeed if all relevant operations are successful. */ -static bool keyd_starttrans(struct onak_dbctx *dbctx) +static bool keyd_starttrans(__unused struct onak_dbctx *dbctx) { return true; } @@ -56,7 +57,7 @@ static bool keyd_starttrans(struct onak_dbctx *dbctx) * * Ends a transaction. */ -static void keyd_endtrans(struct onak_dbctx *dbctx) +static void keyd_endtrans(__unused struct onak_dbctx *dbctx) { return; } @@ -91,22 +92,27 @@ static bool keyd_send_cmd(int fd, enum keyd_ops _cmd) * * This function returns a public key from whatever storage mechanism we * are using. - * - * TODO: What about keyid collisions? Should we use fingerprint instead? */ -static int keyd_fetch_key_id(struct onak_dbctx *dbctx, - uint64_t keyid, +static int keyd_fetch_key(struct onak_dbctx *dbctx, + struct openpgp_fingerprint *fingerprint, struct openpgp_publickey **publickey, - bool intrans) + __unused bool intrans) { int keyd_fd = (intptr_t) dbctx->priv; struct buffer_ctx keybuf; struct openpgp_packet_list *packets = NULL; ssize_t bytes = 0; ssize_t count = 0; + uint8_t size; - if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_ID)) { - write(keyd_fd, &keyid, sizeof(keyid)); + if (fingerprint->length > MAX_FINGERPRINT_LEN) { + return 0; + } + + if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET)) { + size = fingerprint->length; + write(keyd_fd, &size, sizeof(size)); + write(keyd_fd, fingerprint->fp, size); keybuf.offset = 0; read(keyd_fd, &keybuf.size, sizeof(keybuf.size)); if (keybuf.size > 0) { @@ -139,7 +145,7 @@ static int keyd_fetch_key_id(struct onak_dbctx *dbctx, static int keyd_fetch_key_fp(struct onak_dbctx *dbctx, struct openpgp_fingerprint *fingerprint, struct openpgp_publickey **publickey, - bool intrans) + __unused bool intrans) { int keyd_fd = (intptr_t) dbctx->priv; struct buffer_ctx keybuf; @@ -185,6 +191,48 @@ static int keyd_fetch_key_fp(struct onak_dbctx *dbctx, return (count > 0) ? 1 : 0; } +static int keyd_fetch_key_id(struct onak_dbctx *dbctx, + uint64_t keyid, + struct openpgp_publickey **publickey, + __unused bool intrans) +{ + int keyd_fd = (intptr_t) dbctx->priv; + struct buffer_ctx keybuf; + struct openpgp_packet_list *packets = NULL; + ssize_t bytes = 0; + ssize_t count = 0; + + if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_ID)) { + write(keyd_fd, &keyid, sizeof(keyid)); + keybuf.offset = 0; + read(keyd_fd, &keybuf.size, sizeof(keybuf.size)); + if (keybuf.size > 0) { + keybuf.buffer = malloc(keybuf.size); + bytes = count = 0; + logthing(LOGTHING_TRACE, + "Getting %d bytes of key data.", + keybuf.size); + while (bytes >= 0 && count < keybuf.size) { + bytes = read(keyd_fd, &keybuf.buffer[count], + keybuf.size - count); + logthing(LOGTHING_TRACE, + "Read %d bytes.", bytes); + count += bytes; + } + read_openpgp_stream(buffer_fetchchar, &keybuf, + &packets, 0); + parse_keys(packets, publickey); + free_packet_list(packets); + packets = NULL; + free(keybuf.buffer); + keybuf.buffer = NULL; + keybuf.size = 0; + } + } + + return (count > 0) ? 1 : 0; +} + /** * delete_key - Given a keyid delete the key from storage. * @fp: The fingerprint of the key to delete. @@ -194,7 +242,8 @@ static int keyd_fetch_key_fp(struct onak_dbctx *dbctx, * are using. Returns 0 if the key existed. */ static int keyd_delete_key(struct onak_dbctx *dbctx, - struct openpgp_fingerprint *fp, bool intrans) + struct openpgp_fingerprint *fp, + __unused bool intrans) { int keyd_fd = (intptr_t) dbctx->priv; @@ -220,7 +269,8 @@ static int keyd_delete_key(struct onak_dbctx *dbctx, * it? */ static int keyd_store_key(struct onak_dbctx *dbctx, - struct openpgp_publickey *publickey, bool intrans, + struct openpgp_publickey *publickey, + __unused bool intrans, bool update) { int keyd_fd = (intptr_t) dbctx->priv; @@ -480,7 +530,8 @@ static void keyd_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_keyd_init(struct onak_db_config *dbcfg, bool readonly) +struct onak_dbctx *keydb_keyd_init(struct onak_db_config *dbcfg, + __unused bool readonly) { struct sockaddr_un sock; uint32_t cmd = KEYD_CMD_UNKNOWN; @@ -559,8 +610,9 @@ struct onak_dbctx *keydb_keyd_init(struct onak_db_config *dbcfg, bool readonly) dbctx->cleanupdb = keyd_cleanupdb; dbctx->starttrans = keyd_starttrans; dbctx->endtrans = keyd_endtrans; - dbctx->fetch_key_id = keyd_fetch_key_id; + dbctx->fetch_key = keyd_fetch_key; dbctx->fetch_key_fp = keyd_fetch_key_fp; + dbctx->fetch_key_id = keyd_fetch_key_id; dbctx->fetch_key_text = keyd_fetch_key_text; dbctx->fetch_key_skshash = keyd_fetch_key_skshash; dbctx->store_key = keyd_store_key;