#!/usr/bin/env python3

import os
import subprocess
from pathlib import Path

from harness.daemon_config import write_daemon_conf
from harness.daemon import start_test_daemon
from harness.environment import FROMDIR, SCRATCHDIR
from harness.filesystem import make_tree, makepath, rmtree
from harness.results import test_fail
from harness.rsync import rsync_argv
from harness import metadata

metadata(transports={'pipe', 'tcp'})

PORT = 12943

src = FROMDIR
rmtree(src)
make_tree(src, depth=1)

TEMPLATES = {
    'unq': 'printf %s {v} >{out}',
    'sq': "printf %s '{v}' >{out}",
    'dq': 'printf %s "{v}" >{out}',
    'dqapos': 'printf %s "it\'s {v}" >{out}',
    'nest': "sh -c 'printf %s {v} >{out}'",
}

MARKER = 'metachar-pwned'
HOSTILE = f'$(touch${{IFS}}{MARKER})x'
BENIGN = 'alice.b-1_x'

sentinel = Path.cwd() / MARKER
if sentinel.exists():
    sentinel.unlink()

modules = []
cases = []
for label, template in TEMPLATES.items():
    for kind, user in (('bad', HOSTILE), ('ok', BENIGN)):
        mod = f'{label}_{kind}'
        dest = SCRATCHDIR / f'metachar-dest-{mod}'
        rmtree(dest)
        makepath(dest)
        out = SCRATCHDIR / f'metachar-out-{mod}'
        if out.exists():
            out.unlink()
        secrets = SCRATCHDIR / f'metachar-{mod}.secrets'
        secrets.write_text(f'{user}:pw\n')
        secrets.chmod(0o600)
        modules.append((mod, {
            'path': str(dest),
            'read only': 'no',
            'use chroot': 'no',
            'auth users': '*',
            'secrets file': str(secrets),
            'pre-xfer exec': template.format(v='%RSYNC_USER_NAME%', out=out),
        }))
        cases.append((mod, kind, user, out))

pwfile = SCRATCHDIR / 'metachar.password'
pwfile.write_text('pw\n')
pwfile.chmod(0o600)

conf = write_daemon_conf(modules, name='metachar.conf')
url = start_test_daemon(conf, PORT)
dlog = SCRATCHDIR / 'rsyncd.log'

os.environ['RSYNC_PASSWORD'] = 'wrong-environment-fallback'
for mod, kind, user, out in cases:
    before = len(dlog.read_text(errors='replace')) if dlog.exists() else 0
    user_url = url.replace('rsync://', f'rsync://{user}@', 1) + mod + '/'
    proc = subprocess.run(
        rsync_argv('-r', f'--password-file={pwfile}', f'{src}/', user_url),
        stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    log = (dlog.read_text(errors='replace')[before:]) if dlog.exists() else ''
    ctx = (f'[{mod}] rc={proc.returncode} stderr={proc.stderr.strip()[:200]!r} '
           f'log={log[-300:]!r}')

    if kind == 'bad':
        if sentinel.exists():
            sentinel.unlink()
            test_fail(f'peer value executed as shell syntax {ctx}')
        if proc.returncode == 0:
            test_fail(f'hostile value did not fail the transfer {ctx}')
        if 'shell metacharacter' not in log:
            test_fail(f'transfer failed but not via the metacharacter refusal {ctx}')
    else:
        if proc.returncode != 0:
            test_fail(f'refusal rule rejected an ordinary value {ctx}')
        if not out.exists():
            test_fail(f'positive control failed: hook did not run {ctx}')
        got = out.read_text()
        if BENIGN not in got:
            test_fail(f'hook did not receive the value intact: {got!r} {ctx}')

print('shell metacharacters in peer values are refused; ordinary values still run')
