chantra / rpms / rpm

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