trezor-agent/trezor_agent/__main__.py

139 lines
4.9 KiB
Python
Raw Normal View History

2016-02-19 09:35:16 +00:00
"""SSH-agent implementation using hardware authentication devices."""
import argparse
import functools
2016-01-04 17:17:08 +00:00
import logging
import re
2016-01-09 14:06:47 +00:00
import os
import subprocess
2016-01-09 14:06:47 +00:00
import sys
import time
from . import client, formats, protocol, server
2015-06-16 07:20:11 +00:00
log = logging.getLogger(__name__)
2015-06-16 07:03:48 +00:00
2015-11-27 07:59:06 +00:00
def ssh_args(label):
2016-02-19 09:35:16 +00:00
"""Create SSH command for connecting specified server."""
2016-02-15 15:22:01 +00:00
identity = client.string_to_identity(label, identity_type=dict)
2015-11-27 07:59:06 +00:00
args = []
if 'port' in identity:
args += ['-p', identity['port']]
if 'user' in identity:
args += ['-l', identity['user']]
return ['ssh'] + args + [identity['host']]
2016-03-05 08:46:36 +00:00
def create_parser():
2016-02-19 09:35:16 +00:00
"""Create argparse.ArgumentParser for this tool."""
p = argparse.ArgumentParser()
p.add_argument('-v', '--verbose', default=0, action='count')
2015-07-04 07:47:32 +00:00
2016-01-04 17:17:08 +00:00
curve_names = [name.decode('ascii') for name in formats.SUPPORTED_CURVES]
curve_names = ', '.join(sorted(curve_names))
p.add_argument('-e', '--ecdsa-curve-name', metavar='CURVE',
default=formats.CURVE_NIST256,
2016-01-04 17:17:08 +00:00
help='specify ECDSA curve name: ' + curve_names)
p.add_argument('--timeout',
default=server.UNIX_SOCKET_TIMEOUT, type=float,
help='Timeout for accepting SSH client connections')
p.add_argument('--debug', default=False, action='store_true',
help='Log SSH protocol messages for debugging.')
2015-07-04 09:29:49 +00:00
p.add_argument('command', type=str, nargs='*', metavar='ARGUMENT',
help='command to run under the SSH agent')
return p
2015-07-21 11:38:26 +00:00
2016-03-05 08:46:36 +00:00
def create_agent_parser():
p = create_parser()
p.add_argument('identity', type=str, default=None,
help='proto://[user@]host[:port][/path]')
g = p.add_mutually_exclusive_group()
g.add_argument('-s', '--shell', default=False, action='store_true',
help='run ${SHELL} as subprocess under SSH agent')
g.add_argument('-c', '--connect', default=False, action='store_true',
help='connect to specified host via SSH')
g.add_argument('-g', '--git', default=False, action='store_true',
help='run git using specified identity as remote name')
return p
def setup_logging(verbosity):
2016-02-19 09:35:16 +00:00
"""Configure logging for this tool."""
2015-07-21 11:38:26 +00:00
fmt = ('%(asctime)s %(levelname)-12s %(message)-100s '
'[%(filename)s:%(lineno)d]')
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
level = levels[min(verbosity, len(levels) - 1)]
2015-07-21 11:38:26 +00:00
logging.basicConfig(format=fmt, level=level)
def git_host(remote_name):
2016-02-19 18:51:19 +00:00
"""Extract git SSH host for specified remote name."""
output = subprocess.check_output('git config --local --list'.split())
pattern = r'remote\.{}\.url=(.*)'.format(remote_name)
matches = re.findall(pattern, output)
log.debug('git remote "%r": %r', remote_name, matches)
if len(matches) != 1:
raise ValueError('{:d} git remotes found: %s', matches)
url = matches[0].strip()
user, url = url.split('@', 1)
host, path = url.split(':', 1)
return 'ssh://{}@{}/{}'.format(user, host, path)
2016-02-19 18:51:19 +00:00
2016-02-15 15:22:01 +00:00
def ssh_sign(conn, label, blob):
2016-02-19 09:35:16 +00:00
"""Perform SSH signature using given hardware device connection."""
now = time.strftime('%Y-%m-%d %H:%M:%S')
return conn.sign_ssh_challenge(label=label, blob=blob, visual=now)
2015-08-17 15:04:44 +00:00
def run_agent(client_factory):
2016-02-19 09:35:16 +00:00
"""Run ssh-agent using given hardware client factory."""
2015-08-17 15:09:45 +00:00
args = create_agent_parser().parse_args()
setup_logging(verbosity=args.verbose)
2016-02-15 15:22:01 +00:00
with client_factory(curve=args.ecdsa_curve_name) as conn:
label = args.identity
command = args.command
if args.git:
label = git_host(remote_name=label)
log.debug('Git identity: %r', label)
if command:
command = ['git'] + command
log.debug('Git command: %r', command)
2016-02-15 15:22:01 +00:00
public_key = conn.get_public_key(label=label)
if args.connect:
2015-11-27 07:59:06 +00:00
command = ssh_args(label) + args.command
log.debug('SSH connect: %r', command)
2016-02-15 18:38:34 +00:00
use_shell = bool(args.shell)
if use_shell:
command = os.environ['SHELL']
log.debug('using shell: %r', command)
if not command:
sys.stdout.write(public_key)
return
try:
2016-02-15 15:22:01 +00:00
signer = functools.partial(ssh_sign, conn=conn)
public_keys = [formats.import_public_key(public_key)]
handler = protocol.Handler(keys=public_keys, signer=signer,
debug=args.debug)
with server.serve(handler=handler, timeout=args.timeout) as env:
return server.run_process(command=command,
environ=env,
use_shell=use_shell)
except KeyboardInterrupt:
log.info('server stopped')
2016-02-15 15:22:01 +00:00
def main():
2016-02-19 09:35:16 +00:00
"""Main entry point (see setup.py)."""
2016-02-15 15:22:01 +00:00
run_agent(client.Client)