chantra / rpms / rpm

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