]> arthur.barton.de Git - bup.git/blob - lib/bup/ssh.py
Merge branch 'bl/dumbserver' into next
[bup.git] / lib / bup / ssh.py
1 """SSH connection.
2 Connect to a remote host via SSH and execute a command on the host.
3 """
4 import os
5 import sys
6 import re
7 import subprocess
8
9 from bup import helpers
10
11
12 def connect(rhost, port, subcmd):
13     """Connect to 'rhost' and execute the bup subcommand 'subcmd' on it."""
14     assert(not re.search(r'[^\w-]', subcmd))
15     main_exe = os.environ.get('BUP_MAIN_EXE') or sys.argv[0]
16     nicedir = os.path.split(os.path.abspath(main_exe))[0]
17     nicedir = re.sub(r':', "_", nicedir)
18     if rhost == '-':
19         rhost = None
20     if not rhost:
21         argv = ['bup', subcmd]
22     else:
23         # WARNING: shell quoting security holes are possible here, so we
24         # have to be super careful.  We have to use 'sh -c' because
25         # csh-derived shells can't handle PATH= notation.  We can't
26         # set PATH in advance, because ssh probably replaces it.  We
27         # can't exec *safely* using argv, because *both* ssh and 'sh -c'
28         # allow shellquoting.  So we end up having to double-shellquote
29         # stuff here.
30         escapedir = re.sub(r'([^\w/])', r'\\\\\\\1', nicedir)
31         buglvl = helpers.atoi(os.environ.get('BUP_DEBUG'))
32         force_tty = helpers.atoi(os.environ.get('BUP_FORCE_TTY'))
33         cmd = r"""
34                    sh -c PATH=%s:'$PATH BUP_DEBUG=%s BUP_FORCE_TTY=%s bup %s'
35                """ % (escapedir, buglvl, force_tty, subcmd)
36         argv = ['ssh']
37         if port:
38             argv.extend(('-p', port))
39         argv.extend((rhost, '--', cmd.strip()))
40         #helpers.log('argv is: %r\n' % argv)
41     def setup():
42         # runs in the child process
43         if not rhost:
44             os.environ['PATH'] = ':'.join([nicedir,
45                                            os.environ.get('PATH', '')])
46         os.setsid()
47     return subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
48                             preexec_fn=setup)