| 1 |
|
| 2 |
"""rrsync -no-overwrite must protect existing partial-dir files too.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import shlex |
| 6 |
import signal |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
|
| 10 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 11 |
signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 12 |
if '--shell' in sys.argv: |
| 13 |
i = sys.argv.index('--shell') + 2 |
| 14 |
env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])} |
| 15 |
signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
| 16 |
signal.signal(signal.SIGUSR2, signal.SIG_DFL) |
| 17 |
wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT'] |
| 18 |
flags = env.get('RRSYNC_FLAGS', '-wo -no-overwrite -no-lock').split() |
| 19 |
os.execve(wrapper, [wrapper, *flags, root], env) |
| 20 |
|
| 21 |
from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg |
| 22 |
|
| 23 |
base = SCRATCHDIR / 'rrsync-no-overwrite-partial-dir' |
| 24 |
rmtree(base) |
| 25 |
src = base / 'src' |
| 26 |
root = base / 'root' |
| 27 |
makepath(src, root / 'protected') |
| 28 |
(src / 'victim').write_bytes(b'NEW') |
| 29 |
protected = root / 'protected' / 'victim' |
| 30 |
protected.write_bytes(b'POLICY') |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
shim = base / 'rsync-shim' |
| 36 |
shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n') |
| 37 |
shim.chmod(0o755) |
| 38 |
wrapper = patched_rrsync(base, rsync_path=str(shim)) |
| 39 |
rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell' |
| 40 |
env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)} |
| 41 |
got = subprocess.run( |
| 42 |
rsync_argv('-rI', '--partial', '--partial-dir=protected', |
| 43 |
'-e', rsh, f'{src}/victim', 'ignored:'), |
| 44 |
env=env, capture_output=True, text=True, timeout=20) |
| 45 |
|
| 46 |
state = protected.read_bytes() if protected.exists() else None |
| 47 |
assert got.returncode != 0 and 'option --partial-dir has been disabled' in got.stderr, ( |
| 48 |
f'rrsync -no-overwrite accepted --partial-dir: rc={got.returncode}, ' |
| 49 |
f'stdout={got.stdout!r}, stderr={got.stderr!r}') |
| 50 |
assert state == b'POLICY', ( |
| 51 |
f'rrsync consumed an existing partial file: rc={got.returncode}, ' |
| 52 |
f'state={state!r}, stderr={got.stderr!r}') |
| 53 |
|
| 54 |
safe = subprocess.run( |
| 55 |
rsync_argv('-rI', '-e', rsh, f'{src}/victim', 'ignored:'), |
| 56 |
env=env, capture_output=True, text=True, timeout=20) |
| 57 |
assert safe.returncode == 0 and (root / 'victim').read_bytes() == b'NEW', ( |
| 58 |
f'ordinary upload failed: rc={safe.returncode}, stderr={safe.stderr!r}') |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
plain_root = base / 'root-plain' |
| 65 |
rmtree(plain_root) |
| 66 |
makepath(plain_root / 'protected') |
| 67 |
(plain_root / 'protected' / 'victim').write_bytes(b'POLICY') |
| 68 |
plain_env = {**env, 'RRSYNC_ROOT': str(plain_root), 'RRSYNC_FLAGS': '-wo -no-lock'} |
| 69 |
plain = subprocess.run( |
| 70 |
rsync_argv('-rI', '--partial', '--partial-dir=protected', '-e', rsh, f'{src}/victim', 'ignored:'), |
| 71 |
env=plain_env, capture_output=True, text=True, timeout=20) |
| 72 |
assert plain.returncode == 0, ( |
| 73 |
'without -no-overwrite the same option must be accepted, or the refusal ' |
| 74 |
'above is unconditional and says nothing about the policy: ' |
| 75 |
f'rc={plain.returncode}, stderr={plain.stderr!r}') |