[projects/pld-ftp-admin] pfa-signpkg: ask for the passphrase through gpg, before the check

arekm arekm at pld-linux.org
Thu Oct 29 09:18:06 CET 2026


commit 923efd8ccb98040cfe6f25742d4717cbd3d12dcf
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Wed Oct 28 22:45:33 2026 +0100

    pfa-signpkg: ask for the passphrase through gpg, before the check
    
    pfa-signpkg read the passphrase itself only after checking the
    signatures, so it asked after a long wait, and handed it to rpm with
    pexpect by waiting for gpg's "Enter passphrase:". A wrong passphrase
    made gpg ask again, and pfa-signpkg waited for ever.
    
    It now first signs nothing with rpm's own signing command, as rpm 4.20
    and later build it. gpg asks on the terminal, through pinentry or in
    loopback mode, whichever it is set up for, and its agent keeps the
    passphrase for rpm. A wrong passphrase stops pfa-signpkg before anything
    is read or signed. pexpect is no longer needed.
    
    Like gpg, pfa-signpkg now needs a terminal: a passphrase piped to it is
    no longer read.

 bin/pfa-signpkg | 11 +++++++----
 modules/sign.py | 46 ++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 28 deletions(-)
---
diff --git a/bin/pfa-signpkg b/bin/pfa-signpkg
index cc40fbe..1488e82 100755
--- a/bin/pfa-signpkg
+++ b/bin/pfa-signpkg
@@ -2,7 +2,6 @@
 # vi: ts=8 sts=4 sw=4 et
 
 import argparse
-import getpass
 import itertools
 import os
 import sys
@@ -21,7 +20,7 @@ import ftpio
 import ftptree
 from common import checkdir
 from config import sign_key
-from sign import signpkgs
+from sign import signpkgs, unlock_key
 
 os.umask(0o022)
 
@@ -35,6 +34,11 @@ packages = args.packages
 
 checkdir(treename)
 
+# before the long check of the signatures, so that no one has to wait for the question
+if not unlock_key():
+    print("ERR: cannot unlock the signing key", file=sys.stderr)
+    sys.exit(1)
+
 ftpio.connect('sign')
 
 if not ftpio.lock(treename, True):
@@ -99,11 +103,10 @@ def chunk(seq, size, pad=None):
          yield seq[-mod:]
 
 print(f"Total {len(sign)} files to sign")
-password = getpass.getpass("Enter signing password: ")
 try:
     for x in chunk(sign, 512):
         print(f"Signing {len(x)} files")
-        signpkgs(x, password, force_resign)
+        signpkgs(x, force_resign)
 except OSError as e:
     print(f"ERR: {e}", file=sys.stderr)
     sys.exit(1)
diff --git a/modules/sign.py b/modules/sign.py
index 59f21ac..b93c8b3 100644
--- a/modules/sign.py
+++ b/modules/sign.py
@@ -1,9 +1,10 @@
 # vi: ts=8 sts=4 sw=4 et
 
 import os
+import shlex
+import subprocess
 import sys
 
-import pexpect
 import rpm
 from config import sign_key
 
@@ -60,7 +61,23 @@ def signed_by_key(hdr):
     # rpm prints key ids in lowercase
     return sigid.lower().endswith(sign_key.lower())
 
-def signpkgs(files, password, force=False):
+def unlock_key():
+    """Signs nothing as rpm signs with sign_key, so that gpg asks for the passphrase now and its agent keeps it"""
+    # rpm's own signing command (rpm >= 4.20), with the key signpkgs() gives it: the data on stdin, the signature to /dev/null
+    name = rpm.expandMacro('__%{_openpgp_sign}_sign_cmd')
+    rpm.addMacro('_gpg_name', sign_key)
+    try:
+        cmd = rpm.expandMacro('%{' + name + ' - /dev/null}')
+    finally:
+        rpm.delMacro('_gpg_name')
+    env = dict(os.environ)
+    # as rpm does: with the data on stdin, gpg has to be told where the terminal is
+    if 'GPG_TTY' not in env and sys.stdin.isatty():
+        env['GPG_TTY'] = os.ttyname(sys.stdin.fileno())
+    return subprocess.run(shlex.split(cmd), stdin=subprocess.DEVNULL, env=env, check=False).returncode == 0
+
+def signpkgs(files, force=False):
+    """Signs files with sign_key; gpg takes the passphrase from its agent, see unlock_key()"""
     if not os.path.isfile('/usr/bin/gpg'):
         raise OSError('Missing gnupg binary')
     if not os.path.isfile('/bin/rpm'):
@@ -75,29 +92,10 @@ def signpkgs(files, password, force=False):
         else:
             print(f'WARN: removed {rpmfile}.sig, left by an interrupted signing', file=sys.stderr)
 
-    os.putenv('LC_ALL', 'C')
     args = ['--verbose', '--define', '_signature gpg', '--define', '_gpg_name ' + sign_key] + files
-    if force:
-        child = pexpect.spawn('/bin/rpm', ['--delsign'] + args, encoding='utf-8')
-        child.logfile_read = sys.stderr
-        child.expect(pexpect.EOF, timeout=None)
-        child.close()
-        rc = child.exitstatus
-        if rc != 0:
-            raise OSError('package re-signing failed')
-
-    child = pexpect.spawn('/bin/rpm', ['--resign'] + args, encoding='utf-8')
-    child.logfile_read = sys.stderr
-    # TODO: we need a smarter way to figuring out if rpm already stored password in gpg-agent
-    try:
-        child.expect('Enter passphrase:', timeout=30)
-        child.sendline(password)
-    except (pexpect.exceptions.TIMEOUT, pexpect.exceptions.EOF):
-        print('WARN: rpm did not ask for password', file=sys.stderr)
-    child.expect(pexpect.EOF, timeout=None)
-    child.close()
-    rc = child.exitstatus
-    if rc != 0:
+    if force and subprocess.run(['/bin/rpm', '--delsign'] + args, check=False, stdout=sys.stderr).returncode != 0:
+        raise OSError('package re-signing failed')
+    if subprocess.run(['/bin/rpm', '--resign'] + args, check=False, stdout=sys.stderr).returncode != 0:
         raise OSError('package signing failed')
     for rpmfile in files:
         os.chmod(rpmfile, 0o644)
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-ftp-admin.git/commitdiff/16c6c5f2d4269aef64813afc632c36b5ce242afa



More information about the pld-cvs-commit mailing list