]> git.ipfire.org Git - pakfire.git/commitdiff
keystore: Automatically import all keys in /etc/pakfire/trusted.keys.d
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 30 Jul 2021 15:40:40 +0000 (15:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 30 Jul 2021 15:40:40 +0000 (15:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/keystore.c

index e49e55bd0c466d1b59bb4ac61074712547583238..4d80ab64183091f1c16f160f2f316ed7beebb7c3 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <fts.h>
 
 #include <gpgme.h>
 
@@ -51,6 +52,54 @@ static int pakfire_init_gpgme(struct pakfire* pakfire) {
        return 0;
 }
 
+static int pakfire_keystore_import(struct pakfire* pakfire, gpgme_ctx_t ctx) {
+       char path[PATH_MAX];
+
+       // Make path
+       int r = pakfire_make_path(pakfire, path, "/etc/pakfire/trusted.keys.d");
+       if (r < 0)
+               return r;
+
+       DEBUG(pakfire, "Loading keys from %s\n", path);
+
+       char* paths[] = {
+               path, NULL,
+       };
+
+       FTS* fts = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL);
+       if (!fts)
+               goto ERROR;
+
+       for (;;) {
+               FTSENT* fent = fts_read(fts);
+               if (!fent)
+                       break;
+
+               // Only handle files
+               if (fent->fts_info == FTS_F) {
+                       FILE* f = fopen(fent->fts_path, "r");
+                       if (!f) {
+                               ERROR(pakfire, "Could not open %s: %m\n", fent->fts_path);
+                               continue;
+                       }
+
+                       // Import keys from file
+                       r = pakfire_key_import(pakfire, f, NULL);
+                       fclose(f);
+
+                       // End if key import was unsuccessful
+                       if (r)
+                               break;
+               }
+       }
+
+ERROR:
+       if (fts)
+               fts_close(fts);
+
+       return r;
+}
+
 int pakfire_keystore_init(struct pakfire* pakfire, gpgme_ctx_t* ctx) {
        char path[PATH_MAX] = PAKFIRE_CACHE_PATH "/tmp/pakfire-keystore.XXXXXX";
        char* tmp = NULL;
@@ -88,6 +137,13 @@ int pakfire_keystore_init(struct pakfire* pakfire, gpgme_ctx_t* ctx) {
        DEBUG(pakfire, "GPGME engine info: %s, path = %s\n",
                engine_info->file_name, engine_info->home_dir);
 
+       // Import keys
+       r = pakfire_keystore_import(pakfire, *ctx);
+       if (r) {
+               ERROR(pakfire, "Could not import keys: %m\n");
+               goto ERROR;
+       }
+
        // Success
        r = 0;