From: Michael Tremer Date: Sat, 7 Aug 2021 19:55:53 +0000 (+0000) Subject: key: Write generated keys into key store X-Git-Tag: 0.9.28~997 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35737de2033dd384f2929d7785dabc2382edb995;p=pakfire.git key: Write generated keys into key store Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index 3bbc10683..bcdc74f8a 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -352,6 +352,49 @@ PAKFIRE_EXPORT int pakfire_key_is_revoked(struct pakfire_key* key) { return 0; } +static int pakfire_key_write_to_keystore(struct pakfire_key* key) { + // Fetch keystore path + const char* keystore_path = pakfire_get_keystore_path(key->pakfire); + if (!keystore_path) + return 1; + + // Fetch fingerprint + const char* fpr = pakfire_key_get_fingerprint(key); + if (!fpr) + return 1; + + char path[PATH_MAX]; + + // Make path + int r = pakfire_string_format(path, "%s/%s.key", keystore_path, fpr); + if (r < 0) + return r; + + // Create parent directory + r = pakfire_mkparentdir(path, 0); + if (r) + return r; + + // Create file + FILE* f = fopen(path, "w"); + if (!f) { + ERROR(key->pakfire, "Could not open %s for writing: %m\n", path); + return 1; + } + + // Write key to file + r = pakfire_key_export(key, f, 0); + if (r) { + ERROR(key->pakfire, "Could not export key %s: %m\n", fpr); + unlink(path); + } + + // Close file + fclose(f); + + return r; +} + PAKFIRE_EXPORT int pakfire_key_generate(struct pakfire_key** key, struct pakfire* pakfire, const char* algo, const char* userid) { // Reset key @@ -383,7 +426,12 @@ PAKFIRE_EXPORT int pakfire_key_generate(struct pakfire_key** key, struct pakfire gpgme_genkey_result_t result = gpgme_op_genkey_result(gpgctx); // Retrieve the key by its fingerprint - return pakfire_find_key(key, pakfire, result->fpr); + int r = pakfire_find_key(key, pakfire, result->fpr); + if (r) + return r; + + // Store the key in the keystore + return pakfire_key_write_to_keystore(*key); } PAKFIRE_EXPORT int pakfire_key_export(struct pakfire_key* key, FILE* f, @@ -460,49 +508,6 @@ ERROR: return r; } -static int pakfire_key_write_to_keystore(struct pakfire_key* key) { - // Fetch keystore path - const char* keystore_path = pakfire_get_keystore_path(key->pakfire); - if (!keystore_path) - return 1; - - // Fetch fingerprint - const char* fpr = pakfire_key_get_fingerprint(key); - if (!fpr) - return 1; - - char path[PATH_MAX]; - - // Make path - int r = pakfire_string_format(path, "%s/%s.key", keystore_path, fpr); - if (r < 0) - return r; - - // Create parent directory - r = pakfire_mkparentdir(path, 0); - if (r) - return r; - - // Create file - FILE* f = fopen(path, "w"); - if (!f) { - ERROR(key->pakfire, "Could not open %s for writing: %m\n", path); - return 1; - } - - // Write key to file - r = pakfire_key_export(key, f, 0); - if (r) { - ERROR(key->pakfire, "Could not export key %s: %m\n", fpr); - unlink(path); - } - - // Close file - fclose(f); - - return r; -} - PAKFIRE_EXPORT int pakfire_key_import(struct pakfire* pakfire, FILE* f, struct pakfire_key*** keys) { gpgme_data_t data;