From 7ed76fe4726c6474a7a919ed5ab3722c561b31a7 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 21 Oct 2017 18:46:04 +0300 Subject: [PATCH] gpg: use correct GNUPGHOME for gpgconf --- libagent/gpg/__init__.py | 2 +- libagent/gpg/keyring.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/libagent/gpg/__init__.py b/libagent/gpg/__init__.py index 94b0ce5..3901eb8 100644 --- a/libagent/gpg/__init__.py +++ b/libagent/gpg/__init__.py @@ -208,7 +208,7 @@ def run_agent(device_type): log.debug('sys.argv: %s', sys.argv) log.debug('os.environ: %s', os.environ) try: - sock_path = keyring.get_agent_sock_path() + sock_path = keyring.get_agent_sock_path(env={'GNUPGHOME': args.homedir}) handler = agent.Handler(device=device_type()) with server.unix_domain_socket_server(sock_path) as sock: for conn in agent.yield_connections(sock): diff --git a/libagent/gpg/keyring.py b/libagent/gpg/keyring.py index b748f9a..080d398 100644 --- a/libagent/gpg/keyring.py +++ b/libagent/gpg/keyring.py @@ -14,17 +14,18 @@ from .. import util log = logging.getLogger(__name__) -def get_agent_sock_path(sp=subprocess): +def get_agent_sock_path(env=None, sp=subprocess): """Parse gpgconf output to find out GPG agent UNIX socket path.""" - lines = sp.check_output(['gpgconf', '--list-dirs']).strip().split(b'\n') + output = sp.check_output(['gpgconf', '--list-dirs'], env=env) + lines = output.strip().split(b'\n') dirs = dict(line.split(b':', 1) for line in lines) log.debug('gpgconf --list-dirs: %s', dirs) return dirs[b'agent-socket'] -def connect_to_agent(sp=subprocess): +def connect_to_agent(env=None, sp=subprocess): """Connect to GPG agent's UNIX socket.""" - sock_path = get_agent_sock_path(sp=sp) + sock_path = get_agent_sock_path(sp=sp, env=env) sp.check_call(['gpg-connect-agent', '/bye']) # Make sure it's running sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(sock_path) @@ -231,7 +232,7 @@ def export_public_keys(sp=subprocess): def create_agent_signer(user_id): """Sign digest with existing GPG keys using gpg-agent tool.""" - sock = connect_to_agent() + sock = connect_to_agent(env=os.environ) keygrip = get_keygrip(user_id) def sign(digest):