From: Michael Tremer Date: Wed, 7 Jul 2021 20:24:49 +0000 (+0000) Subject: key: Refactor finding keys in local keystore X-Git-Tag: 0.9.28~1084 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6309d5a4f937a11c168419aeeacde99ec8ec2d0;p=pakfire.git key: Refactor finding keys in local keystore Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index 5659efaa7..fc5e4c0ae 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -83,35 +83,46 @@ PAKFIRE_EXPORT void pakfire_key_unref(struct pakfire_key* key) { pakfire_key_free(key); } -static struct pakfire_key* __pakfire_get_key(Pakfire pakfire, gpgme_ctx_t gpgctx, const char* fingerprint) { +static int pakfire_find_key(struct pakfire_key** key, Pakfire pakfire, const char* fingerprint) { + // Reset key + *key = NULL; + + // Fetch GPGME context + gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); + if (!gpgctx) + return 1; + DEBUG(pakfire, "Seaching for key with fingerprint %s\n", fingerprint); - struct pakfire_key* key = NULL; gpgme_key_t gpgkey = NULL; + int r; gpgme_error_t error = gpgme_get_key(gpgctx, fingerprint, &gpgkey, 0); switch (gpg_error(error)) { + // Create a key object if we found something case GPG_ERR_NO_ERROR: - pakfire_key_create(&key, pakfire, gpgkey); + r = pakfire_key_create(key, pakfire, gpgkey); gpgme_key_unref(gpgkey); + if (r) + return r; break; + // Nothing found case GPG_ERR_EOF: - DEBUG(pakfire, "Nothing found\n"); - break; - - default: - DEBUG(pakfire, "Could not find key: %s\n", gpgme_strerror(error)); break; } - return key; + return 0; } PAKFIRE_EXPORT struct pakfire_key* pakfire_key_get(Pakfire pakfire, const char* fingerprint) { - gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); + struct pakfire_key* key = NULL; + + int r = pakfire_find_key(&key, pakfire, fingerprint); + if (r) + return NULL; - return __pakfire_get_key(pakfire, gpgctx, fingerprint); + return key; } PAKFIRE_EXPORT int pakfire_key_delete(struct pakfire_key* key) { @@ -312,15 +323,17 @@ PAKFIRE_EXPORT struct pakfire_key** pakfire_key_import(Pakfire pakfire, const ch struct pakfire_key** list = head; // Retrieve all imported keys + struct pakfire_key* key; while (status) { - struct pakfire_key* key = __pakfire_get_key(pakfire, gpgctx, status->fpr); - if (key) { - const char* fingerprint = pakfire_key_get_fingerprint(key); - INFO(pakfire, "Imported key %s\n", fingerprint); - - // Append key to list - *list++ = key; - } + int r = pakfire_find_key(&key, pakfire, status->fpr); + if (r) + goto FAIL; + + const char* fingerprint = pakfire_key_get_fingerprint(key); + INFO(pakfire, "Imported key %s\n", fingerprint); + + // Append key to list + *list++ = key; status = status->next; }