From a32bfc749bc7dec10ed87076503f20f5c1da5226 Mon Sep 17 00:00:00 2001 From: Serge Pokhodyaev Date: Sun, 18 Feb 2018 20:24:22 +0300 Subject: [PATCH 1/4] don't overwrite homedir --- libagent/gpg/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libagent/gpg/__init__.py b/libagent/gpg/__init__.py index 351b7da..7513f18 100644 --- a/libagent/gpg/__init__.py +++ b/libagent/gpg/__init__.py @@ -126,7 +126,10 @@ def run_init(device_type, args): homedir = os.path.expanduser('~/.gnupg/{}'.format(device_name)) log.info('GPG home directory: %s', homedir) - check_call(['rm', '-rf', homedir]) + if os.path.exists(homedir): + log.error('GPG home directory exists, remove it manually if required') + exit(1) + check_call(['mkdir', '-p', homedir]) check_call(['chmod', '700', homedir]) From 6363eb0d4a8faeb85aa32ec0a770a65c6ce2f8d2 Mon Sep 17 00:00:00 2001 From: Serge Pokhodyaev Date: Sun, 18 Feb 2018 21:08:36 +0300 Subject: [PATCH 2/4] add -f/--foreground option to run as systemd service --- libagent/ssh/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libagent/ssh/__init__.py b/libagent/ssh/__init__.py index ac54940..674d22a 100644 --- a/libagent/ssh/__init__.py +++ b/libagent/ssh/__init__.py @@ -88,6 +88,8 @@ def create_agent_parser(device_type): g = p.add_mutually_exclusive_group() g.add_argument('-d', '--daemonize', default=False, action='store_true', help='Daemonize the agent and print its UNIX socket path') + g.add_argument('-f', '--foreground', default=False, action='store_true', + help='Run agent in foreground with specified UNIX socket path') g.add_argument('-s', '--shell', default=False, action='store_true', help=('run ${SHELL} as subprocess under SSH agent, allowing ' 'regular SSH-based tools to be used in the shell')) @@ -234,7 +236,11 @@ def main(device_type): sock_path = args.sock_path if not sock_path: - sock_path = tempfile.mktemp(prefix='trezor-ssh-agent-') + if args.foreground: + log.error("if running in foreground you must specify the socket path") + return 1 + else: + sock_path = tempfile.mktemp(prefix='trezor-ssh-agent-') command = args.command context = _dummy_context() @@ -248,6 +254,8 @@ def main(device_type): sys.stdout.flush() context = daemon.DaemonContext() log.info('running the agent as a daemon on %s', sock_path) + elif args.foreground: + log.info('running the agent on %s', sock_path) use_shell = bool(args.shell) if use_shell: @@ -258,7 +266,7 @@ def main(device_type): conn_factory=lambda: client.Client(device_type()), identities=identities, public_keys=public_keys) - if command or args.daemonize: + if command or args.daemonize or args.foreground: with context: return run_server(conn=conn, command=command, sock_path=sock_path, debug=args.debug, timeout=args.timeout) From 199fb299c3013fc37d550bbf6c05273b60ba96fa Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 24 Feb 2018 20:46:04 +0200 Subject: [PATCH 3/4] gpg: use 'sys.exit' and log homedir --- libagent/gpg/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libagent/gpg/__init__.py b/libagent/gpg/__init__.py index 7513f18..d4ca9ad 100644 --- a/libagent/gpg/__init__.py +++ b/libagent/gpg/__init__.py @@ -127,8 +127,9 @@ def run_init(device_type, args): log.info('GPG home directory: %s', homedir) if os.path.exists(homedir): - log.error('GPG home directory exists, remove it manually if required') - exit(1) + log.error('GPG home directory %s exists, ' + 'remove it manually if required', homedir) + sys.exit(1) check_call(['mkdir', '-p', homedir]) check_call(['chmod', '700', homedir]) From b20d98bf5749be23338dae0489b328cb3a44250f Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 24 Feb 2018 21:03:30 +0200 Subject: [PATCH 4/4] gpg: move socket path generation into a helper function --- libagent/ssh/__init__.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libagent/ssh/__init__.py b/libagent/ssh/__init__.py index 674d22a..2bce4ed 100644 --- a/libagent/ssh/__init__.py +++ b/libagent/ssh/__init__.py @@ -213,6 +213,17 @@ def _dummy_context(): yield +def _get_sock_path(args): + sock_path = args.sock_path + if not sock_path: + if args.foreground: + log.error('running in foreground mode requires UNIX socket path') + return 1 + else: + sock_path = tempfile.mktemp(prefix='trezor-ssh-agent-') + return sock_path + + @handle_connection_error def main(device_type): """Run ssh-agent using given hardware client factory.""" @@ -234,13 +245,7 @@ def main(device_type): identity.identity_dict['proto'] = u'ssh' log.info('identity #%d: %s', index, identity.to_string()) - sock_path = args.sock_path - if not sock_path: - if args.foreground: - log.error("if running in foreground you must specify the socket path") - return 1 - else: - sock_path = tempfile.mktemp(prefix='trezor-ssh-agent-') + sock_path = _get_sock_path(args) command = args.command context = _dummy_context()