Debian Codegraph @

testsuite/daemon-auth_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
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 # When a daemon module needs auth and no password is available, rsync falls back
22 # to an interactive getpass() prompt that reads /dev/tty directly -- which the
23 # test harness cannot redirect, so it would hang `make coverage` (or any run
24 # with a controlling terminal). Give every client a fallback password via the
25 # environment so it never prompts: the --password-file cases below override it,
26 # and the invalid-credentials case uses it and is correctly rejected.
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 # --- correct password succeeds ----------------------------------------------
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 # --- wrong password is rejected ---------------------------------------------
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 # --- a request with invalid credentials is rejected ------------------------
75 # Local user (not an auth user) with the wrong env-supplied password; rejected
76 # without ever prompting on the tty.
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 # --- strict modes rejects a world-readable secrets file ---------------------
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")