Debian Codegraph @

testsuite/daemon-chroot-munge-default_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Pin WHEN the daemon applies default symlink munging, by config regime.
3
4 The rsyncd.conf(5) `use chroot` entry says:
5
6 "If the daemon is serving the / dir (either directly or due to being chrooted
7 to the module's path), rsync does not do any path sanitizing or (default)
8 munging. When it has to limit access to a particular subdir (either due to
9 chroot being disabled or having an inside-chroot path set), rsync will munge
10 symlinks (by default) and sanitize paths."
11
12 This was flagged as a suspected code mismatch during the doc audit. It is NOT a
13 mismatch: clientserver.c derives `module_dirlen` (0 when the inside-chroot path
14 is "/", >0 for a real subdir) and sets
15 munge_symlinks default = !use_chroot || module_dirlen
16 so the doc's regime split is exactly right. This test confirms it by observing
17 the default (no explicit `munge symlinks`) across three regimes:
18
19 A. use chroot = no -> module_dirlen>0 -> munge ON
20 B. use chroot = yes, path=DIR -> chrooted to DIR, inside path "/", len 0 -> munge OFF
21 C. use chroot = yes, path=BASE/./SUB -> inside path "SUB", len>0 -> munge ON
22
23 Regimes B and C need root (chroot). A runs anywhere. The observable is whether
24 an uploaded symlink is stored verbatim or with the /rsyncd-munged/ prefix.
25 (Path sanitizing -- the other half of the same sentence -- is exercised by the
26 operator-path-* and daemon-scan-dir-escape tests.)
27 """
28
29 import os
30 import subprocess
31
32 from rsyncfns import (
33 SCRATCHDIR,
34 rmtree, rsync_argv, start_test_daemon, test_fail, test_skipped,
35 write_daemon_conf,
36 )
37
38 DAEMON_PORT = 12911
39 LINKVAL = 'realfile' # a plain rel-within target
40
41 base = SCRATCHDIR / 'chroot-munge'
42 rmtree(base)
43 base.mkdir(parents=True)
44
45 # Upload source: one symlink we can inspect after it lands.
46 srcup = base / 'srcup'
47 srcup.mkdir()
48 os.symlink(LINKVAL, srcup / 'sl')
49
50 is_root = os.geteuid() == 0
51
52 # Module A: no chroot. Modules B/C: chroot (root only).
53 mod_a_dir = base / 'a_root'
54 mod_b_dir = base / 'b_root'
55 mod_c_base = base / 'c_base'
56 mod_c_sub = mod_c_base / 'sub'
57 for d in (mod_a_dir, mod_b_dir, mod_c_base, mod_c_sub):
58 d.mkdir(parents=True)
59
60 modules = [('a_nochroot', {'path': str(mod_a_dir), 'use chroot': 'no',
61 'read only': 'no'})]
62 if is_root:
63 modules += [
64 ('b_chroot_root', {'path': str(mod_b_dir), 'use chroot': 'yes',
65 'read only': 'no'}),
66 ('c_chroot_sub', {'path': f'{mod_c_base}/./sub', 'use chroot': 'yes',
67 'read only': 'no'}),
68 ]
69
70 conf = write_daemon_conf(modules, name='chroot-munge.conf')
71 url = start_test_daemon(conf, DAEMON_PORT)
72
73
74 def stored_link(module, landing_dir):
75 """Upload the symlink into `module`; return the value as stored on disk."""
76 subprocess.run(rsync_argv('-al', f'{srcup}/', f'{url}{module}/'),
77 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
78 sl = landing_dir / 'sl'
79 if not sl.is_symlink():
80 test_fail(f"{module}: uploaded symlink did not land at {sl}")
81 return os.readlink(sl)
82
83
84 MUNGED = '/rsyncd-munged/' + LINKVAL
85
86 # A: no chroot -> default munge ON.
87 got = stored_link('a_nochroot', mod_a_dir)
88 if got != MUNGED:
89 test_fail(f"use chroot=no: expected default munge ({MUNGED!r}), stored {got!r}")
90
91 if is_root:
92 # B: chrooted to the module path, inside path "/" -> default munge OFF.
93 got = stored_link('b_chroot_root', mod_b_dir)
94 if got != LINKVAL:
95 test_fail(f"use chroot=yes serving '/': expected NO munge ({LINKVAL!r}), "
96 f"stored {got!r}")
97 # C: chroot with a /./ inner subdir -> default munge ON.
98 got = stored_link('c_chroot_sub', mod_c_sub)
99 if got != MUNGED:
100 test_fail(f"use chroot=yes with inside-path 'sub': expected default munge "
101 f"({MUNGED!r}), stored {got!r}")
102 print("daemon-chroot-munge-default: default munge OFF when serving '/', ON "
103 "for a non-chroot module and for a chroot module with an inside subdir "
104 "-- the use-chroot doc regime split is accurate")
105 else:
106 test_skipped("chroot regimes (B/C) need root; verified the no-chroot regime "
107 "munges by default")