Debian Codegraph @

testsuite/daemon-access-ip_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: hosts allow / hosts deny IP and CIDR matching (access.c).
3
4 access.c's make_mask / match_address / match_binary only run for a real TCP
5 peer matched against a numeric hosts allow/deny pattern -- so this needs
6 --use-tcp (the loopback peer is 127.0.0.1). Verifies that exact-IP and CIDR
7 allow patterns permit the connection while a CIDR deny / a non-matching allow
8 refuse it.
9
10 The config sets NO global hosts allow: an inherited global allow-list would
11 match (e.g. via "localhost") and short-circuit before a module's deny is
12 consulted, so the per-module patterns must be the sole decider here.
13 """
14
15 import subprocess
16
17 from rsyncfns import (
18 FROMDIR, SCRATCHDIR,
19 make_tree, require_tcp, rmtree, rsync_argv, start_test_daemon, test_fail,
20 )
21
22 DAEMON_PORT = 12892
23 require_tcp("hosts allow/deny address matching needs a real TCP peer")
24
25 src = FROMDIR
26 rmtree(src)
27 make_tree(src, depth=2)
28
29 conf = SCRATCHDIR / 'access-ip.conf'
30 conf.write_text(
31 f"pid file = {SCRATCHDIR}/rsyncd.pid\n"
32 "use chroot = no\n"
33 f"log file = {SCRATCHDIR}/rsyncd.log\n"
34 f"\n[allow-exact]\n\tpath = {src}\n\tread only = yes\n\thosts allow = 127.0.0.1\n"
35 f"\n[allow-cidr]\n\tpath = {src}\n\tread only = yes\n\thosts allow = 127.0.0.0/8\n"
36 f"\n[deny-cidr]\n\tpath = {src}\n\tread only = yes\n\thosts deny = 127.0.0.0/8\n"
37 f"\n[allow-other]\n\tpath = {src}\n\tread only = yes\n\thosts allow = 10.0.0.0/8\n"
38 )
39 url = start_test_daemon(conf, DAEMON_PORT)
40
41
42 def connect(mod):
43 """Return rsync's exit code for listing the module over the daemon."""
44 return subprocess.run(rsync_argv('-r', f'{url}{mod}/'),
45 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
46 text=True).returncode
47
48
49 for mod in ('allow-exact', 'allow-cidr'):
50 if connect(mod) != 0:
51 test_fail(f"connection to {mod} should be ALLOWED but was refused")
52 for mod in ('deny-cidr', 'allow-other'):
53 if connect(mod) == 0:
54 test_fail(f"connection to {mod} should be DENIED but succeeded")
55
56 # Client --address binds the outgoing socket to a local address (socket.c
57 # try_bind_local) before connecting to the daemon.
58 proc = subprocess.run(
59 rsync_argv('-r', '--address=127.0.0.1', f'{url}allow-cidr/'),
60 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
61 if proc.returncode != 0:
62 test_fail(f"--address=127.0.0.1 client connection failed: {proc.stderr}")
63
64 print("daemon-access-ip: hosts allow/deny matching + client --address verified")