]> arthur.barton.de Git - bup.git/commitdiff
ssh: simplify the code
authorJohannes Berg <johannes@sipsolutions.net>
Mon, 13 Jan 2020 19:53:26 +0000 (20:53 +0100)
committerRob Browning <rlb@defaultvalue.org>
Sun, 19 Apr 2020 20:52:02 +0000 (15:52 -0500)
There's no point in shipping PATH to the remote server, since
it will be different there. We can also simplify the loopback
check, and we don't really need to munge the PATH there either
if we just use path.exe() in place of a plain 'bup' for it.

While at it, also fix the formatting instruction for the ints
to %d, instead of %s.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/ssh.py

index de0448d003467f248572311b81b861e6d3560e46..ae560908fc7439c8dd11eb16284d5974dd523745 100644 (file)
@@ -11,45 +11,26 @@ from bup.compat import environ
 def connect(rhost, port, subcmd, stderr=None):
     """Connect to 'rhost' and execute the bup subcommand 'subcmd' on it."""
     assert not re.search(br'[^\w-]', subcmd)
-    nicedir = re.sub(b':', b'_', path.exedir())
-    if rhost == b'-':
-        rhost = None
-    if not rhost:
-        argv = [b'bup', subcmd]
+    if rhost is None or rhost == b'-':
+        argv = [path.exe(), subcmd]
     else:
-        # WARNING: shell quoting security holes are possible here, so we
-        # have to be super careful.  We have to use 'sh -c' because
-        # csh-derived shells can't handle PATH= notation.  We can't
-        # set PATH in advance, because ssh probably replaces it.  We
-        # can't exec *safely* using argv, because *both* ssh and 'sh -c'
-        # allow shellquoting.  So we end up having to double-shellquote
-        # stuff here.
-        escapedir = re.sub(br'([^\w/])', br'\\\\\\\1', nicedir)
         buglvl = helpers.atoi(environ.get(b'BUP_DEBUG'))
         force_tty = helpers.atoi(environ.get(b'BUP_FORCE_TTY'))
         cmd = b"""
-                   sh -c PATH=%s:'$PATH BUP_DEBUG=%s BUP_FORCE_TTY=%s bup %s'
-               """ % (escapedir, buglvl, force_tty, subcmd)
+                   sh -c 'BUP_DEBUG=%d BUP_FORCE_TTY=%d bup %s'
+               """ % (buglvl, force_tty, subcmd)
         argv = [b'ssh']
         if port:
             argv.extend((b'-p', port))
         argv.extend((rhost, b'--', cmd.strip()))
         #helpers.log('argv is: %r\n' % argv)
-    if rhost:
-        env = environ
-    else:
-        envpath = environ.get(b'PATH')
-        env = environ.copy()
-        env[b'PATH'] = nicedir if not envpath else nicedir + b':' + envpath
     if sys.version_info[0] < 3:
         return subprocess.Popen(argv,
                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                 stderr=stderr,
-                                env=env,
                                 preexec_fn=lambda: os.setsid())
     else:
         return subprocess.Popen(argv,
                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                 stderr=stderr,
-                                env=env,
                                 start_new_session=True)