]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Automatically import repository keys
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 7 Aug 2021 19:42:10 +0000 (19:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 7 Aug 2021 19:42:10 +0000 (19:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/repo.c

index c14c4a676fdae259edf2b2028a86d454e3c891aa..44e0cef7a22cc2fc3703163d4edd039c6884fd5e 100644 (file)
@@ -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];