| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import os |
| 15 |
import subprocess |
| 16 |
|
| 17 |
from rsyncfns import SCRATCHDIR, SRCDIR, rmtree, test_fail |
| 18 |
|
| 19 |
base = SCRATCHDIR / 'rsync-ssl-stunnel-host' |
| 20 |
rmtree(base) |
| 21 |
base.mkdir(parents=True) |
| 22 |
|
| 23 |
config_capture = base / 'stunnel_config' |
| 24 |
fake_stunnel = base / 'fake_stunnel' |
| 25 |
|
| 26 |
fake_stunnel.write_text(f"#!/usr/bin/env bash\ncat <&10 > {config_capture} 2>/dev/null\nexit 0\n") |
| 27 |
fake_stunnel.chmod(0o755) |
| 28 |
ca = base / 'ca.pem' |
| 29 |
ca.write_text("dummy-ca\n") |
| 30 |
|
| 31 |
helper = ['--HELPER', 'localhost', 'rsync', '--server', '--daemon', '.'] |
| 32 |
base_env = {**os.environ, 'RSYNC_SSL_TYPE': 'stunnel', |
| 33 |
'RSYNC_SSL_STUNNEL': str(fake_stunnel)} |
| 34 |
for v in ('RSYNC_SSL_CA_CERT', 'RSYNC_SSL_ALLOW_INSECURE_STUNNEL', |
| 35 |
'RSYNC_SSL_SKIP_HOSTNAME_CHECK'): |
| 36 |
base_env.pop(v, None) |
| 37 |
|
| 38 |
|
| 39 |
def run(env): |
| 40 |
if config_capture.exists(): |
| 41 |
config_capture.unlink() |
| 42 |
subprocess.run(['bash', str(SRCDIR / 'rsync-ssl')] + helper, env=env, |
| 43 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 44 |
return config_capture.read_text() if config_capture.exists() else '' |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
cfg = run({**base_env, 'RSYNC_SSL_CA_CERT': str(ca)}) |
| 49 |
if 'connect = localhost' not in cfg: |
| 50 |
test_fail(f"premise: fake stunnel did not capture a config:\n{cfg!r}") |
| 51 |
if 'verifyChain = yes' not in cfg: |
| 52 |
test_fail(f"a CA-configured stunnel config must verify the chain:\n{cfg}") |
| 53 |
if 'checkHost = localhost' not in cfg: |
| 54 |
test_fail("CA-configured stunnel config is missing the hostname binding " |
| 55 |
f"(checkHost = localhost):\n{cfg}") |
| 56 |
|
| 57 |
|
| 58 |
cfg = run({**base_env, 'RSYNC_SSL_CA_CERT': '', |
| 59 |
'RSYNC_SSL_ALLOW_INSECURE_STUNNEL': '1'}) |
| 60 |
if 'checkHost' in cfg: |
| 61 |
test_fail(f"insecure (no-CA) stunnel config must not emit checkHost:\n{cfg}") |
| 62 |
|
| 63 |
|
| 64 |
cfg = run({**base_env, 'RSYNC_SSL_CA_CERT': str(ca), |
| 65 |
'RSYNC_SSL_SKIP_HOSTNAME_CHECK': '1'}) |
| 66 |
if 'verifyChain = yes' not in cfg: |
| 67 |
test_fail(f"SKIP_HOSTNAME_CHECK must keep chain verification:\n{cfg}") |
| 68 |
if 'checkHost' in cfg: |
| 69 |
test_fail("RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 must drop the hostname binding " |
| 70 |
f"(no checkHost):\n{cfg}") |
| 71 |
|
| 72 |
print("rsync-ssl-stunnel-hostname-check: stunnel binds the cert to the host when " |
| 73 |
"verifying a chain, and SKIP_HOSTNAME_CHECK opts back to chain-only") |