Hi,

the previous patch has a really silly error, sorry for that. The
fork() in the atexit() handler is missing :(

Attached a updated version. I will try to write a very basic test ASAP
to avoid this kind of mistake.

Sorry,
 Michael
>From 9f327ecc7abae8234029a57bc9bfc28c7aee962f Mon Sep 17 00:00:00 2001
From: Michael Vogt <m...@ubuntu.com>
Date: Fri, 22 Aug 2014 18:08:47 +0200
Subject: [PATCH] run gpg with its own temporary GNUPGHOME directory

---
 gpg-parse.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)

diff --git a/gpg-parse.c b/gpg-parse.c
index bae2181..6d5d66e 100644
--- a/gpg-parse.c
+++ b/gpg-parse.c
@@ -21,6 +21,9 @@
  * routines to parse gpg output
  */
 
+#include <dpkg/path.h>
+
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -32,16 +35,40 @@
 #include "debsig.h"
 
 static int gpg_inited = 0;
+static char *gpg_tmpdir;
+
+static void
+cleanup_gpg_tmpdir(void)
+{
+   pid_t pid = fork();
+   if (pid < 0)
+      ds_fail_printf(DS_FAIL_INTERNAL, "Cleanup failed to fork() (%s)",
+                     strerror(errno));
+   if (pid == 0)
+      execlp("rm", "rm", "-rf", gpg_tmpdir, NULL);
+   waitpid(pid, NULL, 0);
+
+   free(gpg_tmpdir);
+   gpg_tmpdir = NULL;
+}
 
-/* Crazy damn hack to make sure gpg has created ~/.gnupg, else it will
- * fail first time called */
-static void gpg_init(void) {
+/* Ensure that gpg has a writable HOME to put its keyrings */
+static void
+gpg_init(void)
+{
     int rc;
 
     if (gpg_inited) return;
-    rc = system(GPG_PROG" --options /dev/null < /dev/null > /dev/null 2>&1");
-    if (rc < 0)
-        ds_fail_printf(DS_FAIL_INTERNAL, "error writing initializing gpg");
+
+    gpg_tmpdir = mkdtemp(path_make_temp_template("debsig-verify"));
+    if(!gpg_tmpdir)
+       ds_fail_printf(DS_FAIL_INTERNAL, "Cannot create temporary directory '%s'", gpg_tmpdir);
+    rc = setenv("GNUPGHOME", gpg_tmpdir, 1);
+    if(rc < 0)
+       ds_fail_printf(DS_FAIL_INTERNAL, "Can not set environment GNUPGHOME to '%s' (%s)", gpg_tmpdir, strerror(errno));
+    rc = atexit(cleanup_gpg_tmpdir);
+    if(rc != 0)
+       ds_fail_printf(DS_FAIL_INTERNAL, "Can not set atexit cleanup handler");
     gpg_inited = 1;
 }
 
-- 
2.0.0.rc0

Reply via email to