From: Michael Tremer Date: Sat, 7 Aug 2021 19:42:10 +0000 (+0000) Subject: repo: Automatically import repository keys X-Git-Tag: 0.9.28~998 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e447bf4de2f6f9c63773fb875a6b85237cad23e;p=pakfire.git repo: Automatically import repository keys Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index c14c4a676..44e0cef7a 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -85,6 +85,67 @@ static int pakfire_repo_is_internal(struct pakfire_repo* repo) { return (*name == '@'); } +static int pakfire_repo_retrieve(struct pakfire_repo* repo, const char* title, + const char* url, const char* path, enum pakfire_transfer_flags flags) { + struct pakfire_downloader* downloader; + int r = pakfire_downloader_create(&downloader, repo->pakfire); + if (r) + return r; + + // Fetch mirrorlist + struct pakfire_mirrorlist* mirrorlist = pakfire_repo_get_mirrorlist(repo); + + // Retrieve the database file + r = pakfire_downloader_retrieve(downloader, repo->appdata->baseurl, mirrorlist, + title, url, path, flags); + + if (mirrorlist) + pakfire_mirrorlist_unref(mirrorlist); + pakfire_downloader_unref(downloader); + + return r; +} + +static int pakfire_repo_import_key(struct pakfire_repo* repo, const char* url) { + const char* name = pakfire_repo_get_name(repo); + DEBUG(repo->pakfire, "Importing key for repository %s from %s...\n", name, url); + + char path[PATH_MAX] = PAKFIRE_CACHE_PATH "/tmp/XXXXXX"; + struct pakfire_key** keys = NULL; + + // Allocate a temporary file name + FILE* f = pakfire_mktemp(path); + if (!f) + return 1; + + // Try to download the key + int r = pakfire_repo_retrieve(repo, NULL, url, path, + PAKFIRE_TRANSFER_NOTEMP|PAKFIRE_TRANSFER_NOPROGRESS); + if (r) + goto ERROR; + + // Try importing the key + r = pakfire_key_import(repo->pakfire, f, &keys); + if (r) + goto ERROR; + + r = 0; + +ERROR: + if (f) + fclose(f); + unlink(path); + + // Free keys + if (keys) { + for (struct pakfire_key** key = keys; *key; key++) + pakfire_key_unref(*key); + free(keys); + } + + return r; +} + int pakfire_repo_import(struct pakfire* pakfire, struct pakfire_config* config) { char** sections = pakfire_config_sections(config); @@ -138,6 +199,16 @@ int pakfire_repo_import(struct pakfire* pakfire, struct pakfire_config* config) if (mirrors) pakfire_repo_set_mirrorlist_url(repo, mirrors); + // Key + const char* key = pakfire_config_get(config, *section, "key", NULL); + if (key) { + r = pakfire_repo_import_key(repo, key); + if (r) { + pakfire_repo_unref(repo); + goto ERROR; + } + } + pakfire_repo_unref(repo); } @@ -182,27 +253,6 @@ struct pakfire_mirrorlist* pakfire_repo_get_mirrorlist(struct pakfire_repo* repo return pakfire_mirrorlist_ref(repo->mirrorlist); } -static int pakfire_repo_retrieve(struct pakfire_repo* repo, const char* title, - const char* url, const char* path, enum pakfire_transfer_flags flags) { - struct pakfire_downloader* downloader; - int r = pakfire_downloader_create(&downloader, repo->pakfire); - if (r) - return r; - - // Fetch mirrorlist - struct pakfire_mirrorlist* mirrorlist = pakfire_repo_get_mirrorlist(repo); - - // Retrieve the database file - r = pakfire_downloader_retrieve(downloader, repo->appdata->baseurl, mirrorlist, - title, url, path, flags); - - if (mirrorlist) - pakfire_mirrorlist_unref(mirrorlist); - pakfire_downloader_unref(downloader); - - return r; -} - static int pakfire_repo_download_database(struct pakfire_repo* repo, const char* database, const char* cache_path) { char title[NAME_MAX];