From 23dc36f0d587495f2d29ebefd9e46437069b5a2d Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Mon, 29 May 2017 16:11:55 +0300
Subject: [PATCH] Pass sign arguments to signature deletion too

Refactor rpmsign and python bindings to be more similar on both
addsign/delsign operations, and always pass the signing arguments
along. Deletion doesn't actually (yet) use the arguments for anything
but makes things more symmetric (I remember having doubts about
this when adding - reminder to self: if in doubt, add more arguments ;)

Yet another API break, but what the hey... Other than that, behavior is
not supposed to change here.
---
 python/rpmsmodule.c | 28 ++++++++++++++++------------
 rpmsign.c           | 13 +++++++------
 sign/rpmgensig.c    |  2 +-
 sign/rpmsign.h      |  3 ++-
 4 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/python/rpmsmodule.c b/python/rpmsmodule.c
index 0601353b9..72465221d 100644
--- a/python/rpmsmodule.c
+++ b/python/rpmsmodule.c
@@ -5,32 +5,36 @@
 static char rpms__doc__[] =
 "";
 
+static int parseSignArgs(PyObject * args, PyObject *kwds,
+			const char **path, struct rpmSignArgs *sargs)
+{
+    char * kwlist[] = { "path", "keyid", "hashalgo", NULL };
+
+    memset(sargs, 0, sizeof(*sargs));
+    return PyArg_ParseTupleAndKeywords(args, kwds, "s|si", kwlist,
+				    path, &sargs->keyid, &sargs->hashalgo);
+}
+
 static PyObject * addSign(PyObject * self, PyObject * args, PyObject *kwds)
 {
     const char *path = NULL;
-    char * kwlist[] = { "path", "keyid", "hashalgo", NULL };
-    struct rpmSignArgs sig, *sigp = NULL;
+    struct rpmSignArgs sargs;
 
-    memset(&sig, 0, sizeof(sig));
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|si", kwlist,
-				&path, &sig.keyid, &sig.hashalgo))
+    if (parseSignArgs(args, kwds, &path, &sargs))
 	return NULL;
 
-    if (sig.keyid || sig.hashalgo)
-	sigp = &sig;
-
-    return PyBool_FromLong(rpmPkgSign(path, sigp) == 0);
+    return PyBool_FromLong(rpmPkgSign(path, &sargs) == 0);
 }
 
 static PyObject * delSign(PyObject * self, PyObject * args, PyObject *kwds)
 {
     const char *path = NULL;
-    char * kwlist[] = { "path", NULL };
+    struct rpmSignArgs sargs;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &path))
+    if (parseSignArgs(args, kwds, &path, &sargs))
 	return NULL;
 
-    return PyBool_FromLong(rpmPkgDelSign(path) == 0);
+    return PyBool_FromLong(rpmPkgDelSign(path, &sargs) == 0);
 }
 
 /*
diff --git a/rpmsign.c b/rpmsign.c
index 3834b505e..0402af556 100644
--- a/rpmsign.c
+++ b/rpmsign.c
@@ -25,6 +25,8 @@ static int signfiles = 0, fskpass = 0;
 static char * fileSigningKey = NULL;
 static char * fileSigningKeyPassword = NULL;
 
+static struct rpmSignArgs sargs = {NULL, 0, 0};
+
 static struct poptOption signOptsTable[] = {
     { "addsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_ADDSIGN,
 	N_("sign package(s)"), NULL },
@@ -54,11 +56,10 @@ static struct poptOption optionsTable[] = {
 };
 
 /* TODO: permit overriding macro setup on the command line */
-static int doSign(poptContext optCon)
+static int doSign(poptContext optCon, struct rpmSignArgs *sargs)
 {
     int rc = EXIT_FAILURE;
     char * name = rpmExpand("%{?_gpg_name}", NULL);
-    struct rpmSignArgs sig = {NULL, 0, 0};
     char *key = NULL;
 
     if (rstreq(name, "")) {
@@ -92,13 +93,13 @@ static int doSign(poptContext optCon)
 	    free(fileSigningKeyPassword);
 	}
 
-	sig.signfiles = 1;
+	sargs->signfiles = 1;
     }
 
     const char *arg;
     rc = 0;
     while ((arg = poptGetArg(optCon)) != NULL) {
-	rc += rpmPkgSign(arg, &sig);
+	rc += rpmPkgSign(arg, sargs);
     }
 
 exit:
@@ -133,12 +134,12 @@ int main(int argc, char *argv[])
     switch (mode) {
     case MODE_ADDSIGN:
     case MODE_RESIGN:
-	ec = doSign(optCon);
+	ec = doSign(optCon, &sargs);
 	break;
     case MODE_DELSIGN:
 	ec = 0;
 	while ((arg = poptGetArg(optCon)) != NULL) {
-	    ec += rpmPkgDelSign(arg);
+	    ec += rpmPkgDelSign(arg, &sargs);
 	}
 	break;
     default:
diff --git a/sign/rpmgensig.c b/sign/rpmgensig.c
index 4f5ff7b59..32bcfb3fb 100644
--- a/sign/rpmgensig.c
+++ b/sign/rpmgensig.c
@@ -863,7 +863,7 @@ int rpmPkgSign(const char *path, const struct rpmSignArgs * args)
     return rc;
 }
 
-int rpmPkgDelSign(const char *path)
+int rpmPkgDelSign(const char *path, const struct rpmSignArgs * args)
 {
     return rpmSign(path, 1, 0);
 }
diff --git a/sign/rpmsign.h b/sign/rpmsign.h
index b41e3caab..bed8d6245 100644
--- a/sign/rpmsign.h
+++ b/sign/rpmsign.h
@@ -31,9 +31,10 @@ int rpmPkgSign(const char *path, const struct rpmSignArgs * args);
 /** \ingroup rpmsign
  * Delete signature(s) from a package
  * @param path		path to package
+ * @param args		signing parameters (or NULL for defaults)
  * @return		0 on success
  */
-int rpmPkgDelSign(const char *path);
+int rpmPkgDelSign(const char *path, const struct rpmSignArgs * args);
 
 #ifdef __cplusplus
 }
-- 
2.11.0

