]> git.ipfire.org Git - pakfire.git/commitdiff
key: Write keys to keystore after import
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 7 Aug 2021 19:39:24 +0000 (19:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 7 Aug 2021 19:39:24 +0000 (19:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/key.c

index 46dcdd5b46f662182894640dfccc848989fadb40..3bbc106839933ec03ce6879878d5aa3408efb24e 100644 (file)
@@ -409,6 +409,8 @@ PAKFIRE_EXPORT int pakfire_key_export(struct pakfire_key* key, FILE* f,
 
        const char* fingerprint = pakfire_key_get_fingerprint(key);
 
+       DEBUG(key->pakfire, "Exporting key %s\n", fingerprint);
+
        gpgme_data_t data = NULL;
        char* buffer = NULL;
        int r = 1;
@@ -458,6 +460,49 @@ 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;
@@ -526,6 +571,11 @@ PAKFIRE_EXPORT int pakfire_key_import(struct pakfire* pakfire, FILE* f,
 
                                        // Append to array
                                        (*keys)[i] = key;
+
+                                       // Write key to keystore
+                                       r = pakfire_key_write_to_keystore(key);
+                                       if (r)
+                                               goto ERROR;
                                }
                        }
                        break;