michal-grzedzicki / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone

Blame SOURCES/rpm-4.14.3-GPG-Switch-back-to-pipe-7-for-signing.patch

b7b8f7
diff -up rpm-4.14.3/sign/rpmgensig.c.orig rpm-4.14.3/sign/rpmgensig.c
b7b8f7
--- rpm-4.14.3/sign/rpmgensig.c.orig	2020-06-26 15:57:43.781333983 +0200
b7b8f7
+++ rpm-4.14.3/sign/rpmgensig.c	2020-06-26 15:58:29.819229616 +0200
b7b8f7
@@ -8,7 +8,6 @@
b7b8f7
 #include <errno.h>
b7b8f7
 #include <sys/wait.h>
b7b8f7
 #include <popt.h>
b7b8f7
-#include <libgen.h>
b7b8f7
 
b7b8f7
 #include <rpm/rpmlib.h>			/* RPMSIGTAG & related */
b7b8f7
 #include <rpm/rpmmacro.h>
b7b8f7
@@ -33,68 +32,6 @@ typedef struct sigTarget_s {
b7b8f7
     rpm_loff_t size;
b7b8f7
 } *sigTarget;
b7b8f7
 
b7b8f7
-/*
b7b8f7
- * There is no function for creating unique temporary fifos so create
b7b8f7
- * unique temporary directory and then create fifo in it.
b7b8f7
- */
b7b8f7
-static char *mkTempFifo(void)
b7b8f7
-{
b7b8f7
-    char *tmppath = NULL, *tmpdir = NULL, *fifofn = NULL;
b7b8f7
-    mode_t mode;
b7b8f7
-
b7b8f7
-    tmppath = rpmExpand("%{_tmppath}", NULL);
b7b8f7
-    if (rpmioMkpath(tmppath, 0755, (uid_t) -1, (gid_t) -1))
b7b8f7
-	goto exit;
b7b8f7
-
b7b8f7
-
b7b8f7
-    tmpdir = rpmGetPath(tmppath, "/rpm-tmp.XXXXXX", NULL);
b7b8f7
-    mode = umask(0077);
b7b8f7
-    tmpdir = mkdtemp(tmpdir);
b7b8f7
-    umask(mode);
b7b8f7
-    if (tmpdir == NULL) {
b7b8f7
-	rpmlog(RPMLOG_ERR, _("error creating temp directory %s: %m\n"),
b7b8f7
-	    tmpdir);
b7b8f7
-	tmpdir = _free(tmpdir);
b7b8f7
-	goto exit;
b7b8f7
-    }
b7b8f7
-
b7b8f7
-    fifofn = rpmGetPath(tmpdir, "/fifo", NULL);
b7b8f7
-    if (mkfifo(fifofn, 0600) == -1) {
b7b8f7
-	rpmlog(RPMLOG_ERR, _("error creating fifo %s: %m\n"), fifofn);
b7b8f7
-	fifofn = _free(fifofn);
b7b8f7
-    }
b7b8f7
-
b7b8f7
-exit:
b7b8f7
-    if (fifofn == NULL && tmpdir != NULL)
b7b8f7
-	unlink(tmpdir);
b7b8f7
-
b7b8f7
-    free(tmppath);
b7b8f7
-    free(tmpdir);
b7b8f7
-
b7b8f7
-    return fifofn;
b7b8f7
-}
b7b8f7
-
b7b8f7
-/* Delete fifo and then temporary directory in which it was located */
b7b8f7
-static int rpmRmTempFifo(const char *fn)
b7b8f7
-{
b7b8f7
-    int rc = 0;
b7b8f7
-    char *dfn = NULL, *dir = NULL;
b7b8f7
-
b7b8f7
-    if ((rc = unlink(fn)) != 0) {
b7b8f7
-	rpmlog(RPMLOG_ERR, _("error delete fifo %s: %m\n"), fn);
b7b8f7
-	return rc;
b7b8f7
-    }
b7b8f7
-
b7b8f7
-    dfn = xstrdup(fn);
b7b8f7
-    dir = dirname(dfn);
b7b8f7
-
b7b8f7
-    if ((rc = rmdir(dir)) != 0)
b7b8f7
-	rpmlog(RPMLOG_ERR, _("error delete directory %s: %m\n"), dir);
b7b8f7
-    free(dfn);
b7b8f7
-
b7b8f7
-    return rc;
b7b8f7
-}
b7b8f7
-
b7b8f7
 static int closeFile(FD_t *fdp)
b7b8f7
 {
b7b8f7
     if (fdp == NULL || *fdp == NULL)
b7b8f7
@@ -241,27 +178,38 @@ exit:
b7b8f7
 static int runGPG(sigTarget sigt, const char *sigfile)
b7b8f7
 {
b7b8f7
     int pid = 0, status;
b7b8f7
-    FD_t fnamedPipe = NULL;
b7b8f7
-    char *namedPipeName = NULL;
b7b8f7
+    int pipefd[2];
b7b8f7
+    FILE *fpipe = NULL;
b7b8f7
     unsigned char buf[BUFSIZ];
b7b8f7
     ssize_t count;
b7b8f7
     ssize_t wantCount;
b7b8f7
     rpm_loff_t size;
b7b8f7
     int rc = 1; /* assume failure */
b7b8f7
 
b7b8f7
-    namedPipeName = mkTempFifo();
b7b8f7
+    if (pipe(pipefd) < 0) {
b7b8f7
+        rpmlog(RPMLOG_ERR, _("Could not create pipe for signing: %m\n"));
b7b8f7
+        goto exit;
b7b8f7
+    }
b7b8f7
 
b7b8f7
-    rpmPushMacro(NULL, "__plaintext_filename", NULL, namedPipeName, -1);
b7b8f7
+    rpmPushMacro(NULL, "__plaintext_filename", NULL, "-", -1);
b7b8f7
     rpmPushMacro(NULL, "__signature_filename", NULL, sigfile, -1);
b7b8f7
 
b7b8f7
     if (!(pid = fork())) {
b7b8f7
 	char *const *av;
b7b8f7
 	char *cmd = NULL;
b7b8f7
-	const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
b7b8f7
+	const char *tty = ttyname(STDIN_FILENO);
b7b8f7
+	const char *gpg_path = NULL;
b7b8f7
+
b7b8f7
+	if (!getenv("GPG_TTY") && (!tty || setenv("GPG_TTY", tty, 0)))
b7b8f7
+	    rpmlog(RPMLOG_WARNING, _("Could not set GPG_TTY to stdin: %m\n"));
b7b8f7
 
b7b8f7
+	gpg_path = rpmExpand("%{?_gpg_path}", NULL);
b7b8f7
 	if (gpg_path && *gpg_path != '\0')
b7b8f7
 	    (void) setenv("GNUPGHOME", gpg_path, 1);
b7b8f7
 
b7b8f7
+	dup2(pipefd[0], STDIN_FILENO);
b7b8f7
+	close(pipefd[1]);
b7b8f7
+
b7b8f7
 	unsetenv("MALLOC_CHECK_");
b7b8f7
 	cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
b7b8f7
 	rc = poptParseArgvString(cmd, NULL, (const char ***)&av;;
b7b8f7
@@ -276,9 +224,10 @@ static int runGPG(sigTarget sigt, const
b7b8f7
     rpmPopMacro(NULL, "__plaintext_filename");
b7b8f7
     rpmPopMacro(NULL, "__signature_filename");
b7b8f7
 
b7b8f7
-    fnamedPipe = Fopen(namedPipeName, "w");
b7b8f7
-    if (!fnamedPipe) {
b7b8f7
-	rpmlog(RPMLOG_ERR, _("Fopen failed\n"));
b7b8f7
+    close(pipefd[0]);
b7b8f7
+    fpipe = fdopen(pipefd[1], "w");
b7b8f7
+    if (!fpipe) {
b7b8f7
+	rpmlog(RPMLOG_ERR, _("Could not open pipe for writing: %m\n"));
b7b8f7
 	goto exit;
b7b8f7
     }
b7b8f7
 
b7b8f7
@@ -291,8 +240,8 @@ static int runGPG(sigTarget sigt, const
b7b8f7
     size = sigt->size;
b7b8f7
     wantCount = size < sizeof(buf) ? size : sizeof(buf);
b7b8f7
     while ((count = Fread(buf, sizeof(buf[0]), wantCount, sigt->fd)) > 0) {
b7b8f7
-	Fwrite(buf, sizeof(buf[0]), count, fnamedPipe);
b7b8f7
-	if (Ferror(fnamedPipe)) {
b7b8f7
+	fwrite(buf, sizeof(buf[0]), count, fpipe);
b7b8f7
+	if (ferror(fpipe)) {
b7b8f7
 	    rpmlog(RPMLOG_ERR, _("Could not write to pipe\n"));
b7b8f7
 	    goto exit;
b7b8f7
 	}
b7b8f7
@@ -304,8 +253,13 @@ static int runGPG(sigTarget sigt, const
b7b8f7
 		sigt->fileName, Fstrerror(sigt->fd));
b7b8f7
 	goto exit;
b7b8f7
     }
b7b8f7
-    Fclose(fnamedPipe);
b7b8f7
-    fnamedPipe = NULL;
b7b8f7
+
b7b8f7
+exit:
b7b8f7
+
b7b8f7
+    if (fpipe)
b7b8f7
+	fclose(fpipe);
b7b8f7
+    if (pipefd[1])
b7b8f7
+	close(pipefd[1]);
b7b8f7
 
b7b8f7
     (void) waitpid(pid, &status, 0);
b7b8f7
     pid = 0;
b7b8f7
@@ -314,20 +268,6 @@ static int runGPG(sigTarget sigt, const
b7b8f7
     } else {
b7b8f7
 	rc = 0;
b7b8f7
     }
b7b8f7
-
b7b8f7
-exit:
b7b8f7
-
b7b8f7
-    if (fnamedPipe)
b7b8f7
-	Fclose(fnamedPipe);
b7b8f7
-
b7b8f7
-    if (pid)
b7b8f7
-	waitpid(pid, &status, 0);
b7b8f7
-
b7b8f7
-    if (namedPipeName) {
b7b8f7
-	rpmRmTempFifo(namedPipeName);
b7b8f7
-	free(namedPipeName);
b7b8f7
-    }
b7b8f7
-
b7b8f7
     return rc;
b7b8f7
 }
b7b8f7