]> git.ipfire.org Git - pakfire.git/commitdiff
repos: Read key immediately and write it to the config
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Oct 2022 09:25:39 +0000 (09:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Oct 2022 09:25:39 +0000 (09:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/repo.c
src/libpakfire/build.c
src/libpakfire/include/pakfire/repo.h
src/libpakfire/libpakfire.sym
src/libpakfire/repo.c

index 26c09f36b6d6a7f061222036925cf7786a4a9ce6..d3480bdcd94e5ba6e0c3ab6f28cf717c228ba594 100644 (file)
@@ -25,6 +25,7 @@
 #include <pakfire/util.h>
 
 #include "archive.h"
+#include "key.h"
 #include "package.h"
 #include "repo.h"
 
@@ -183,22 +184,17 @@ static int Repo_set_baseurl(RepoObject* self, PyObject* value) {
        return pakfire_repo_set_baseurl(self->repo, baseurl);
 }
 
-static PyObject* Repo_get_keyfile(RepoObject* self) {
-       const char* keyfile = pakfire_repo_get_keyfile(self->repo);
+static PyObject* Repo_get_key(RepoObject* self) {
+       struct pakfire_key* key = pakfire_repo_get_key(self->repo);
 
-       if (keyfile)
-               return PyUnicode_FromString(keyfile);
+       if (key) {
+               PyObject* ret = new_key(&KeyType, key);
+               pakfire_key_unref(key);
 
-       Py_RETURN_NONE;
-}
-
-static int Repo_set_keyfile(RepoObject* self, PyObject* value) {
-       const char* keyfile = NULL;
-
-       if (value != Py_None)
-               keyfile = PyUnicode_AsUTF8(value);
+               return ret;
+       }
 
-       return pakfire_repo_set_keyfile(self->repo, keyfile);
+       Py_RETURN_NONE;
 }
 
 static PyObject* Repo_get_mirrorlist(RepoObject* self) {
@@ -364,9 +360,9 @@ static struct PyGetSetDef Repo_getsetters[] = {
                NULL
        },
        {
-               "keyfile",
-               (getter)Repo_get_keyfile,
-               (setter)Repo_set_keyfile,
+               "key",
+               (getter)Repo_get_key,
+               NULL,
                NULL,
                NULL
        },
index 09f1d9a79f3e88868aa7badebadfa1bb3cef8e07..98712f5def3e41536d4cd10995e8d00d98725e1a 100644 (file)
@@ -100,16 +100,10 @@ static int pakfire_build_has_flag(struct pakfire_build* build, int flag) {
        a pakfire instance running inside the chroot.
 */
 static int pakfire_build_enable_repos(struct pakfire_build* build) {
+       FILE* f = NULL;
        char repopath[PATH_MAX];
        int r = 1;
 
-       // Fetch repository configuration
-       char* config = pakfire_repo_get_config(build->repo);
-       if (!config) {
-               ERROR(build->pakfire, "Could not generate repository configuration: %m\n");
-               goto ERROR;
-       }
-
        const char* path = pakfire_repo_get_path(build->repo);
 
        // Make sure the source directory exists
@@ -132,16 +126,23 @@ static int pakfire_build_enable_repos(struct pakfire_build* build) {
        if (r)
                goto ERROR;
 
+       // Open the repository configuration
+       f = fopen(repopath, "w");
+       if (!f) {
+               ERROR(build->pakfire, "Could not open %s: %m\n", repopath);
+               goto ERROR;
+       }
+
        // Write repository configuration
-       r = pakfire_file_write(build->pakfire, repopath, 0, 0, 0644, "%s", config);
+       r = pakfire_repo_write_config(build->repo, f);
        if (r) {
                ERROR(build->pakfire, "Could not write repository configuration: %m\n");
                goto ERROR;
        }
 
 ERROR:
-       if (config)
-               free(config);
+       if (f)
+               fclose(f);
 
        return r;
 }
index 22acae2bd6bf9972257783613ec43d486d648c18..e1a0ffd44d60a0b03b9fa5a6ea91280532ab2705 100644 (file)
@@ -27,6 +27,7 @@
 
 struct pakfire_repo;
 
+#include <pakfire/key.h>
 #include <pakfire/pakfire.h>
 
 int pakfire_repo_create(struct pakfire_repo** repo, struct pakfire* pakfire, const char* name);
@@ -55,15 +56,14 @@ void pakfire_repo_set_priority(struct pakfire_repo* repo, int priority);
 const char* pakfire_repo_get_baseurl(struct pakfire_repo* repo);
 int pakfire_repo_set_baseurl(struct pakfire_repo* repo, const char* baseurl);
 
-const char* pakfire_repo_get_keyfile(struct pakfire_repo* repo);
-int pakfire_repo_set_keyfile(struct pakfire_repo* repo, const char* keyfile);
+struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo);
 
 const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo* repo);
 int pakfire_repo_set_mirrorlist_url(struct pakfire_repo* repo, const char* url);
 
 struct pakfire_mirrorlist* pakfire_repo_get_mirrors(struct pakfire_repo* repo);
 
-char* pakfire_repo_get_config(struct pakfire_repo* repo);
+int pakfire_repo_write_config(struct pakfire_repo* repo, FILE* f);
 
 int pakfire_repo_is_installed_repo(struct pakfire_repo* repo);
 
index 8245d909efc2711a68db44a5c3490544d120619b..4d472743fe317b2a53a07890cd5678bd7a52a0d6 100644 (file)
@@ -278,11 +278,10 @@ global:
        pakfire_repo_clean;
        pakfire_repo_create;
        pakfire_repo_get_baseurl;
-       pakfire_repo_get_config;
        pakfire_repo_get_description;
        pakfire_repo_get_name;
        pakfire_repo_get_enabled;
-       pakfire_repo_get_keyfile;
+       pakfire_repo_get_key;
        pakfire_repo_get_mirrorlist_url;
        pakfire_repo_get_pakfire;
        pakfire_repo_get_pool;
@@ -296,9 +295,9 @@ global:
        pakfire_repo_set_baseurl;
        pakfire_repo_set_description;
        pakfire_repo_set_enabled;
-       pakfire_repo_set_keyfile;
        pakfire_repo_set_mirrorlist_url;
        pakfire_repo_set_priority;
+       pakfire_repo_write_config;
        pakfire_repo_write_solv;
        pakfire_repo_unref;
 
index 68acb8eddb57f2cd3b424b701e8524cb8712a5e4..9f4f9221108dafd25fc32c5d1b6f6faaf6a4ec24 100644 (file)
@@ -59,10 +59,12 @@ struct pakfire_repo_appdata {
 
        char* description;
        char* baseurl;
-       char* keyfile;
 
        char metadata[PATH_MAX];
 
+       // Key fingerprint
+       char* key;
+
        // Mirrorlist
        char* mirrorlist_url;
        char mirrorlist[PATH_MAX];
@@ -125,37 +127,35 @@ static int pakfire_repo_retrieve(
        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_TMP_DIR "/pakfire-key.XXXXXX";
+static int pakfire_repo_import_key(struct pakfire_repo* repo, FILE* f) {
        struct pakfire_key** keys = NULL;
+       size_t counter = 0;
+       int r;
 
-       // 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,
-               0, NULL, 0, PAKFIRE_TRANSFER_NOTEMP|PAKFIRE_TRANSFER_NOPROGRESS);
-       if (r)
-               goto ERROR;
-
-       // Try importing the key
+       // Import any keys from the file descriptor
        r = pakfire_key_import(repo->pakfire, f, &keys);
        if (r)
+               return r;
+
+       // Count how many keys were imported
+       for (struct pakfire_key** key = keys; *key; key++)
+               counter++;
+
+       // We should only import one key
+       if (counter != 1) {
+               ERROR(repo->pakfire, "Imported %zu key(s) but expected one\n", counter);
+               r = 1;
                goto ERROR;
+       }
 
-       r = 0;
+       for (struct pakfire_key** key = keys; *key; key++) {
+               char* dump = pakfire_key_dump(*key);
 
-ERROR:
-       if (f)
-               fclose(f);
-       unlink(path);
+               if (dump)
+                       DEBUG(repo->pakfire, "Imported key:\n%s\n", dump);
+       }
 
-       // Free keys
+ERROR:
        if (keys) {
                for (struct pakfire_key** key = keys; *key; key++)
                        pakfire_key_unref(*key);
@@ -219,7 +219,7 @@ int pakfire_repo_import(struct pakfire* pakfire, struct pakfire_config* config)
                        pakfire_repo_set_mirrorlist_url(repo, mirrors);
 
                // Key
-               const char* key = pakfire_config_get(config, *section, "key", NULL);
+               FILE* key = pakfire_config_get_fopen(config, *section, "key");
                if (key) {
                        r = pakfire_repo_import_key(repo, key);
                        if (r) {
@@ -437,9 +437,6 @@ static void free_repo_appdata(struct pakfire_repo_appdata* appdata) {
        if (appdata->description)
                free(appdata->description);
 
-       if (appdata->keyfile)
-               free(appdata->keyfile);
-
        if (appdata->mirrorlist_url)
                free(appdata->mirrorlist_url);
 
@@ -767,20 +764,8 @@ const char* pakfire_repo_get_path(struct pakfire_repo* repo) {
        return repo->appdata->baseurl + strlen("file://");
 }
 
-PAKFIRE_EXPORT const char* pakfire_repo_get_keyfile(struct pakfire_repo* repo) {
-       return repo->appdata->keyfile;
-}
-
-PAKFIRE_EXPORT int pakfire_repo_set_keyfile(struct pakfire_repo* repo, const char* keyfile) {
-       if (repo->appdata->keyfile)
-               free(repo->appdata->keyfile);
-
-       if (keyfile)
-               repo->appdata->keyfile = strdup(keyfile);
-       else
-               repo->appdata->keyfile = NULL;
-
-       return 0;
+PAKFIRE_EXPORT struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo) {
+       return pakfire_key_get(repo->pakfire, repo->appdata->key);
 }
 
 PAKFIRE_EXPORT const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo* repo) {
@@ -799,42 +784,109 @@ PAKFIRE_EXPORT int pakfire_repo_set_mirrorlist_url(struct pakfire_repo* repo, co
        return 0;
 }
 
-PAKFIRE_EXPORT char* pakfire_repo_get_config(struct pakfire_repo* repo) {
+PAKFIRE_EXPORT int pakfire_repo_write_config(struct pakfire_repo* repo, FILE* f) {
+       int r;
+
+       // Do nothing for the installed repository
        if (pakfire_repo_is_installed_repo(repo) == 0)
-               return NULL;
+               return 0;
 
-       char buffer[1024 * 4];
-       char* p = buffer;
+       struct pakfire_config* config = NULL;
+       char* section = NULL;
 
-       // Headline
-       p += sprintf(p, "[repo:%s]\n", pakfire_repo_get_name(repo));
+       struct pakfire_key* key = NULL;
+       char* buffer = NULL;
+       size_t length = 0;
+
+       // Create section name
+       r = asprintf(&section, "[repo:%s]", pakfire_repo_get_name(repo));
+       if (r < 0)
+               goto ERROR;
+
+       // Create a new configuration
+       r = pakfire_config_create(&config);
+       if (r)
+               goto ERROR;
 
+       // Description
        const char* description = pakfire_repo_get_description(repo);
-       if (description)
-               p += sprintf(p, "description = %s\n", description);
+       if (description) {
+               r = pakfire_config_set(config, section, "description", description);
+               if (r) {
+                       ERROR(repo->pakfire, "Could not set description: %m\n");
+                       goto ERROR;
+               }
+       }
 
-       // Enabled
-       p += sprintf(p, "enabled = %d\n", pakfire_repo_get_enabled(repo));
+       // Disabled?
+       if (!pakfire_repo_get_enabled(repo)) {
+               r = pakfire_config_set(config, section, "enabled", "0");
+               if (r) {
+                       ERROR(repo->pakfire, "Could not set enabled: %m\n");
+                       goto ERROR;
+               }
+       }
 
        // Base URL
        const char* baseurl = pakfire_repo_get_baseurl(repo);
-       if (baseurl)
-               p += sprintf(p, "baseurl = %s\n", baseurl);
+       if (baseurl) {
+               r = pakfire_config_set(config, section, "baseurl", baseurl);
+               if (r) {
+                       ERROR(repo->pakfire, "Could not set base URL: %m\n");
+                       goto ERROR;
+               }
+       }
 
        // Mirror List
        const char* mirrorlist = pakfire_repo_get_mirrorlist_url(repo);
-       if (mirrorlist)
-               p += sprintf(p, "mirrors = %s\n", mirrorlist);
+       if (mirrorlist) {
+               r = pakfire_config_set(config, section, "mirrors", mirrorlist);
+               if (r) {
+                       ERROR(repo->pakfire, "Could not set mirrors: %m\n");
+                       goto ERROR;
+               }
+       }
+
+       // Key
+       key = pakfire_repo_get_key(repo);
+       if (key) {
+               r = pakfire_key_get_public_key(key, &buffer, &length);
+               if (r) {
+                       ERROR(repo->pakfire, "Could not fetch public key: %m\n");
+                       goto ERROR;
+               }
 
-       // Key File
-       const char* keyfile = pakfire_repo_get_keyfile(repo);
-       if (keyfile)
-               p += sprintf(p, "keyfile = %s\n", keyfile);
+               r = pakfire_config_set(config, section, "key", buffer);
+               if (r) {
+                       ERROR(repo->pakfire, "Could not set key: %m\n");
+                       goto ERROR;
+               }
+       }
 
        // Priority
-       p += sprintf(p, "priority = %d\n", pakfire_repo_get_priority(repo));
+       unsigned int priority = pakfire_repo_get_priority(repo);
+       if (priority) {
+               r = pakfire_config_set_format(config, section, "priority", "%d", priority);
+               if (r) {
+                       ERROR(repo->pakfire, "Could not set priority: %m\n");
+                       goto ERROR;
+               }
+       }
+
+       // Write the configuration
+       r = pakfire_config_dump(config, f);
 
-       return strdup(buffer);
+ERROR:
+       if (key)
+               pakfire_key_unref(key);
+       if (config)
+               pakfire_config_unref(config);
+       if (section)
+               free(section);
+       if (buffer)
+               free(buffer);
+
+       return r;
 }
 
 PAKFIRE_EXPORT int pakfire_repo_is_installed_repo(struct pakfire_repo* repo) {