2015-07-03 13:09:49 +00:00
|
|
|
import os
|
2015-07-04 18:57:57 +00:00
|
|
|
import re
|
2015-06-15 13:37:16 +00:00
|
|
|
import sys
|
|
|
|
import argparse
|
2015-07-04 18:57:57 +00:00
|
|
|
import subprocess
|
2015-06-15 13:37:16 +00:00
|
|
|
|
2015-06-15 15:13:10 +00:00
|
|
|
from . import trezor
|
|
|
|
from . import server
|
2015-06-15 13:37:16 +00:00
|
|
|
|
2015-06-16 07:20:11 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-06-16 07:03:48 +00:00
|
|
|
|
2015-07-04 18:57:57 +00:00
|
|
|
def identity_from_gitconfig():
|
|
|
|
out = subprocess.check_output(args='git config --list --local'.split())
|
2015-07-21 11:38:26 +00:00
|
|
|
config = [line.split('=', 1) for line in out.strip().split('\n')]
|
2015-07-20 13:04:43 +00:00
|
|
|
config_dict = dict(item for item in config if len(item) == 2)
|
2015-07-04 18:57:57 +00:00
|
|
|
|
|
|
|
name_regex = re.compile(r'^remote\..*\.trezor$')
|
2015-07-06 06:36:56 +00:00
|
|
|
names = [item[0] for item in config if name_regex.match(item[0])]
|
|
|
|
if len(names) != 1:
|
2015-07-21 11:38:26 +00:00
|
|
|
log.error('please add "trezor" key '
|
|
|
|
'to a single remote section at .git/config')
|
2015-07-06 06:36:56 +00:00
|
|
|
sys.exit(1)
|
2015-07-20 13:04:43 +00:00
|
|
|
key_name = names[0]
|
|
|
|
identity_label = config_dict.get(key_name)
|
|
|
|
if identity_label:
|
|
|
|
return identity_label
|
|
|
|
|
2015-07-21 11:38:26 +00:00
|
|
|
# extract remote name marked as TREZOR's
|
|
|
|
section_name, _ = key_name.rsplit('.', 1)
|
2015-07-06 06:36:56 +00:00
|
|
|
|
|
|
|
key_name = section_name + '.url'
|
|
|
|
url = config_dict[key_name]
|
2015-07-20 13:04:43 +00:00
|
|
|
log.debug('using "%s=%s" from git-config', key_name, url)
|
2015-07-05 12:10:24 +00:00
|
|
|
|
|
|
|
user, url = url.split('@', 1)
|
|
|
|
host, path = url.split(':', 1)
|
|
|
|
return 'ssh://{0}@{1}/{2}'.format(user, host, path)
|
2015-07-04 18:57:57 +00:00
|
|
|
|
|
|
|
|
2015-07-22 10:48:15 +00:00
|
|
|
def create_parser():
|
2015-06-15 13:37:16 +00:00
|
|
|
p = argparse.ArgumentParser()
|
2015-07-04 08:22:44 +00:00
|
|
|
p.add_argument('-v', '--verbose', default=0, action='count')
|
2015-07-04 07:47:32 +00:00
|
|
|
|
2015-07-04 18:57:57 +00:00
|
|
|
p.add_argument('identity', type=str, default=None,
|
2015-07-03 13:09:49 +00:00
|
|
|
help='proto://[user@]host[:port][/path]')
|
2015-07-04 08:22:44 +00:00
|
|
|
|
|
|
|
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')
|
2015-07-04 09:29:49 +00:00
|
|
|
p.add_argument('command', type=str, nargs='*', metavar='ARGUMENT',
|
2015-07-04 06:23:36 +00:00
|
|
|
help='command to run under the SSH agent')
|
2015-07-22 10:48:15 +00:00
|
|
|
return p
|
2015-07-21 11:38:26 +00:00
|
|
|
|
2015-06-15 13:37:16 +00:00
|
|
|
|
2015-07-22 10:48:15 +00:00
|
|
|
def setup_logging(verbosity):
|
2015-07-21 11:38:26 +00:00
|
|
|
fmt = ('%(asctime)s %(levelname)-12s %(message)-100s '
|
|
|
|
'[%(filename)s:%(lineno)d]')
|
2015-07-04 08:22:44 +00:00
|
|
|
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
|
2015-07-22 10:48:15 +00:00
|
|
|
level = levels[min(verbosity, len(levels) - 1)]
|
2015-07-21 11:38:26 +00:00
|
|
|
logging.basicConfig(format=fmt, level=level)
|
2015-06-15 13:37:16 +00:00
|
|
|
|
2015-07-22 10:48:15 +00:00
|
|
|
|
|
|
|
def trezor_agent():
|
|
|
|
args = create_parser().parse_args()
|
|
|
|
setup_logging(verbosity=args.verbose)
|
|
|
|
|
2015-06-17 13:52:11 +00:00
|
|
|
with trezor.Client(factory=trezor.TrezorLibrary) as client:
|
2015-07-04 18:57:57 +00:00
|
|
|
|
|
|
|
label = args.identity
|
|
|
|
command = args.command
|
|
|
|
|
|
|
|
if label == 'git':
|
|
|
|
label = identity_from_gitconfig()
|
2015-07-20 13:04:43 +00:00
|
|
|
log.info('using identity %r for git command %r', label, command)
|
2015-07-04 18:57:57 +00:00
|
|
|
if command:
|
|
|
|
command = ['git'] + command
|
|
|
|
|
|
|
|
identity = client.get_identity(label=label)
|
2015-07-04 07:47:32 +00:00
|
|
|
public_key = client.get_public_key(identity=identity)
|
2015-06-17 13:52:11 +00:00
|
|
|
|
2015-07-04 18:57:57 +00:00
|
|
|
use_shell = False
|
2015-07-04 08:22:44 +00:00
|
|
|
if args.connect:
|
2015-07-21 11:38:26 +00:00
|
|
|
def to_ascii(s):
|
|
|
|
return s.encode('ascii')
|
|
|
|
|
2015-07-04 08:22:44 +00:00
|
|
|
command = ['ssh', to_ascii(identity.host)]
|
|
|
|
if identity.user:
|
|
|
|
command += ['-l', to_ascii(identity.user)]
|
|
|
|
if identity.port:
|
|
|
|
command += ['-p', to_ascii(identity.port)]
|
|
|
|
log.debug('SSH connect: %r', command)
|
2015-07-20 15:05:59 +00:00
|
|
|
command = command + args.command
|
2015-07-04 08:22:44 +00:00
|
|
|
|
|
|
|
if args.shell:
|
2015-07-04 07:47:32 +00:00
|
|
|
command, use_shell = os.environ['SHELL'], True
|
2015-07-04 08:22:44 +00:00
|
|
|
log.debug('using shell: %r', command)
|
|
|
|
|
|
|
|
if not command:
|
|
|
|
sys.stdout.write(public_key)
|
|
|
|
return
|
2015-06-17 13:52:11 +00:00
|
|
|
|
2015-07-04 18:15:09 +00:00
|
|
|
def signer(label, blob):
|
|
|
|
identity = client.get_identity(label=label)
|
|
|
|
return client.sign_ssh_challenge(identity=identity, blob=blob)
|
2015-06-17 13:52:11 +00:00
|
|
|
|
|
|
|
try:
|
2015-07-04 07:47:32 +00:00
|
|
|
with server.serve(public_keys=[public_key], signer=signer) as env:
|
2015-07-04 08:22:44 +00:00
|
|
|
return server.run_process(command=command, environ=env,
|
|
|
|
use_shell=use_shell)
|
2015-06-17 13:52:11 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
log.info('server stopped')
|
2015-07-22 10:48:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def trezor_verify():
|
|
|
|
|
|
|
|
p = argparse.ArgumentParser()
|
|
|
|
p.add_argument('-v', '--verbose', default=0, action='count')
|
|
|
|
p.add_argument('-a', '--address', default=None)
|
|
|
|
p.add_argument('identity')
|
|
|
|
args = p.parse_args()
|
|
|
|
|
|
|
|
setup_logging(verbosity=args.verbose)
|
|
|
|
with trezor.Client(factory=trezor.TrezorLibrary) as client:
|
|
|
|
client.sign_identity(identity=args.identity,
|
|
|
|
expected_address=args.address)
|