From: Michael Tremer Date: Fri, 30 Jul 2021 15:40:40 +0000 (+0000) Subject: keystore: Automatically import all keys in /etc/pakfire/trusted.keys.d X-Git-Tag: 0.9.28~1009 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b0eed58c99604a5f232cca9af2819634e9d75c08;p=pakfire.git keystore: Automatically import all keys in /etc/pakfire/trusted.keys.d Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/keystore.c b/src/libpakfire/keystore.c index e49e55bd0..4d80ab641 100644 --- a/src/libpakfire/keystore.c +++ b/src/libpakfire/keystore.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include @@ -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;