| 1 |
|
| 2 |
"""Daemon coverage: auth users, secrets file, strict modes. |
| 3 |
|
| 4 |
A module with auth users + a secrets file must accept the right password, |
| 5 |
reject a wrong one, and (with the default strict modes) refuse a |
| 6 |
world-readable secrets file. Authentication happens in the daemon protocol, so |
| 7 |
it works over the default secure stdio-pipe transport. |
| 8 |
""" |
| 9 |
|
| 10 |
import os |
| 11 |
import subprocess |
| 12 |
|
| 13 |
from rsyncfns import ( |
| 14 |
FROMDIR, SCRATCHDIR, |
| 15 |
make_tree, makepath, rmtree, rsync_argv, start_test_daemon, test_fail, |
| 16 |
verify_dirs, write_daemon_conf, |
| 17 |
) |
| 18 |
|
| 19 |
DAEMON_PORT = 12888 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong' |
| 28 |
|
| 29 |
src = FROMDIR |
| 30 |
rmtree(src) |
| 31 |
make_tree(src, depth=3) |
| 32 |
|
| 33 |
authdir = SCRATCHDIR / 'authdest' |
| 34 |
secrets = SCRATCHDIR / 'rsyncd.secrets' |
| 35 |
secrets.write_text('tuser:secretpass\n') |
| 36 |
secrets.chmod(0o600) |
| 37 |
|
| 38 |
conf = write_daemon_conf([ |
| 39 |
('auth', {'path': authdir, 'read only': 'no', |
| 40 |
'auth users': 'tuser', 'secrets file': secrets}), |
| 41 |
]) |
| 42 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 43 |
userurl = url.replace('rsync://', 'rsync://tuser@', 1) |
| 44 |
|
| 45 |
|
| 46 |
def pwfile(name, text): |
| 47 |
p = SCRATCHDIR / name |
| 48 |
p.write_text(text) |
| 49 |
p.chmod(0o600) |
| 50 |
return p |
| 51 |
|
| 52 |
|
| 53 |
def push(pw, **kw): |
| 54 |
rmtree(authdir) |
| 55 |
makepath(authdir) |
| 56 |
return subprocess.run( |
| 57 |
rsync_argv('-a', f'--password-file={pw}', f'{src}/', f'{userurl}auth/'), |
| 58 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, **kw) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
ok = pwfile('pw.ok', 'secretpass\n') |
| 63 |
proc = push(ok) |
| 64 |
if proc.returncode not in (0, 23): |
| 65 |
test_fail(f"auth with the correct password failed: {proc.stderr}") |
| 66 |
verify_dirs(src, authdir, label="auth success") |
| 67 |
|
| 68 |
|
| 69 |
bad = pwfile('pw.bad', 'wrongpass\n') |
| 70 |
proc = push(bad) |
| 71 |
if proc.returncode == 0: |
| 72 |
test_fail("auth with the wrong password unexpectedly succeeded") |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
proc = subprocess.run( |
| 78 |
rsync_argv('-a', f'{src}/', f'{url}auth/'), |
| 79 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, |
| 80 |
stdin=subprocess.DEVNULL) |
| 81 |
if proc.returncode == 0: |
| 82 |
test_fail("a request with invalid credentials succeeded against an " |
| 83 |
"auth-users module") |
| 84 |
|
| 85 |
|
| 86 |
secrets.chmod(0o644) |
| 87 |
proc = push(ok) |
| 88 |
if proc.returncode == 0: |
| 89 |
test_fail("strict modes did not reject a world-readable secrets file") |
| 90 |
secrets.chmod(0o600) |
| 91 |
|
| 92 |
print("daemon-auth: auth users / secrets file / strict modes verified") |