]> git.ipfire.org Git - pakfire.git/commitdiff
keystore: Create a temporary GnuPG database
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 30 Jul 2021 15:07:59 +0000 (15:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 30 Jul 2021 15:07:59 +0000 (15:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/keystore.c

index 3667d4385b0ff2e0dc1a720b5e8d077620edc3e7..e49e55bd0c466d1b59bb4ac61074712547583238 100644 (file)
@@ -52,7 +52,8 @@ static int pakfire_init_gpgme(struct pakfire* pakfire) {
 }
 
 int pakfire_keystore_init(struct pakfire* pakfire, gpgme_ctx_t* ctx) {
-       char path[PATH_MAX];
+       char path[PATH_MAX] = PAKFIRE_CACHE_PATH "/tmp/pakfire-keystore.XXXXXX";
+       char* tmp = NULL;
 
        // Initialise GPGME
        int r = pakfire_init_gpgme(pakfire);
@@ -70,20 +71,13 @@ int pakfire_keystore_init(struct pakfire* pakfire, gpgme_ctx_t* ctx) {
        // Set output to be ASCII armoured
        gpgme_set_armor(*ctx, 1);
 
-       // Set home
-       r = pakfire_make_path(pakfire, path, "/etc/pakfire/gnupg");
-       if (r < 0)
+       // Create a temporary directory
+       tmp = pakfire_mkdtemp(path);
+       if (!tmp)
                goto ERROR;
 
        DEBUG(pakfire, "Using PGP database at %s\n", path);
 
-       // Create home
-       r = pakfire_mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR);
-       if (r && errno != EEXIST) {
-               ERROR(pakfire, "Could not initialize the PGP database at %s: %m\n", path);
-               goto ERROR;
-       }
-
        // Setup engine
        error = gpgme_ctx_set_engine_info(*ctx, GPGME_PROTOCOL_OpenPGP, NULL, path);
        if (gpg_err_code(error) != GPG_ERR_NO_ERROR)
@@ -101,12 +95,25 @@ ERROR:
        gpgme_release(*ctx);
        *ctx = NULL;
 
+       // Cleanup temporary files
+       if (tmp)
+               pakfire_rmtree(tmp, 0);
+
        return r;
 }
 
 int pakfire_keystore_destroy(struct pakfire* pakfire, gpgme_ctx_t* ctx) {
+       char path[PATH_MAX];
+
+       // Retrieve engine info
+       gpgme_engine_info_t engine_info = gpgme_ctx_get_engine_info(*ctx);
+
+       // Store a copy of the home directory
+       pakfire_string_set(path, engine_info->home_dir);
+
        // Free GPGME context
        gpgme_release(*ctx);
 
-       return 0;
+       // Remove home directory
+       return pakfire_rmtree(path, 0);
 }