Debian Codegraph @

testsuite/daemon-early-exec-nameconv_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: `early exec` and `name converter` module hooks.
3
4 daemon-exec_test.py covers pre-xfer/post-xfer exec; this fills in the other
5 two start_pre_exec() callers in clientserver.c:
6
7 early exec -- runs before the @RSYNCD: OK, before pre-xfer exec, with
8 request="(NONE)" and the client's --early-input bytes on
9 its stdin (write_pre_exec_args type 1).
10 name converter -- a long-running script that the daemon queries via
11 namecvt_call() (uidlist.c) for every uid/gid <-> name
12 mapping, used when chroot would hide /etc/passwd.
13
14 Both go through the start_pre_exec() forked-child path that 845fd1103113
15 made gcov-visible.
16 """
17
18 import os
19 import pwd
20 import subprocess
21 import time
22
23 from rsyncfns import (
24 FROMDIR, SCRATCHDIR, SRCDIR,
25 make_tree, makepath, owners_supported, rmtree, rsync_argv,
26 start_test_daemon, test_fail, test_skipped, write_daemon_conf,
27 )
28
29 DAEMON_PORT = 12891
30
31 src = FROMDIR
32 rmtree(src)
33 make_tree(src, depth=2)
34
35 markers = SCRATCHDIR / 'markers'
36 rmtree(markers)
37 makepath(markers)
38 dest_early = SCRATCHDIR / 'dest-early'
39 dest_conv = SCRATCHDIR / 'dest-conv'
40 makepath(dest_early, dest_conv)
41
42
43 def script(name, body):
44 p = SCRATCHDIR / name
45 p.write_text('#!/bin/sh\n' + body)
46 p.chmod(0o755)
47 return p
48
49
50 # early-exec: capture RSYNC_MODULE_NAME (set before start_pre_exec) and the
51 # --early-input bytes (piped to the child's stdin after the request/argv).
52 early = script(
53 'early.sh',
54 f'printf "%s|" "$RSYNC_MODULE_NAME" > {markers}/early\n'
55 f'cat >> {markers}/early\n',
56 )
57
58 # name converter: log every request to a marker so the test can verify
59 # namecvt_call() actually drove it, then defer to the shipped support script
60 # for the answer.
61 nameconv = script(
62 'nameconv.sh',
63 f'tee -a {markers}/nameconv | python3 {SRCDIR}/support/nameconvert\n',
64 )
65
66 early_input = SCRATCHDIR / 'early-input.bin'
67 early_input.write_bytes(b'EARLY-PAYLOAD')
68
69 conf = write_daemon_conf([
70 ('early', {
71 'path': str(dest_early), 'read only': 'no', 'use chroot': 'no',
72 'early exec': str(early),
73 }),
74 ('conv', {
75 'path': str(dest_conv), 'read only': 'no', 'use chroot': 'no',
76 'name converter': str(nameconv),
77 # Force name<->id mapping so uidlist.c hits namecvt_call().
78 'numeric ids': 'no',
79 }),
80 ], name='early-nameconv.conf')
81 url = start_test_daemon(conf, DAEMON_PORT)
82
83
84 # --- early exec -------------------------------------------------------------
85
86 r = subprocess.run(
87 rsync_argv('-r', f'--early-input={early_input}', f'{src}/', f'{url}early/'),
88 capture_output=True, text=True,
89 )
90 if r.returncode != 0:
91 test_fail(f"push to [early] failed (rc={r.returncode}):\n{r.stderr}")
92 em = (markers / 'early')
93 if not em.is_file():
94 test_fail("early-exec script never ran (no marker file)")
95 got = em.read_text()
96 if got != 'early|EARLY-PAYLOAD':
97 test_fail(f"early-exec env/--early-input wrong: {got!r}")
98
99
100 # --- name converter ---------------------------------------------------------
101
102 # recv_add_id() only calls user_to_uid()/group_to_gid() (-> namecvt_call) when
103 # the incoming id is NON-zero, so the source tree needs a non-root owner.
104 # Pick any non-root passwd entry that exists in this environment; skip the
105 # name-converter half if there isn't one (or we can't chown).
106 nonroot = next((p for p in pwd.getpwall() if p.pw_uid != 0 and p.pw_gid != 0), None)
107 if not owners_supported() or nonroot is None:
108 print("daemon-early-exec-nameconv: early-exec ok; "
109 "name-converter SKIPPED (no non-root passwd entry / cannot chown)")
110 raise SystemExit(0)
111 for p in src.rglob('*'):
112 os.chown(p, nonroot.pw_uid, nonroot.pw_gid)
113
114 # -og (preserve owner+group) without --numeric-ids: the daemon receiver maps
115 # the sender's uid/gid names through namecvt_call() -> our script.
116 r = subprocess.run(
117 rsync_argv('-rog', f'{src}/', f'{url}conv/'),
118 capture_output=True, text=True,
119 )
120 if r.returncode != 0:
121 test_fail(f"push to [conv] failed (rc={r.returncode}):\n{r.stderr}")
122
123 # The converter is a co-process; give it a moment to flush the tee.
124 nm = markers / 'nameconv'
125 deadline = time.monotonic() + 5
126 while time.monotonic() < deadline and not (nm.is_file() and nm.stat().st_size):
127 time.sleep(0.05)
128 if not nm.is_file() or not nm.stat().st_size:
129 test_fail("name-converter script never received any namecvt_call() requests")
130
131 reqs = nm.read_text().splitlines()
132 # At minimum: a 'usr <name>' and a 'grp <name>' for the sending user's uid/gid.
133 if not any(l.startswith('usr ') for l in reqs):
134 test_fail(f"name-converter saw no 'usr' request: {reqs!r}")
135 if not any(l.startswith('grp ') for l in reqs):
136 test_fail(f"name-converter saw no 'grp' request: {reqs!r}")
137
138 # And the dest tree's owner should have been mapped through to the same uid.
139 some_dest = next(p for p in dest_conv.rglob('*') if p.is_file())
140 st = os.stat(some_dest)
141 if st.st_uid != nonroot.pw_uid:
142 test_fail(f"name-converter mapping did not apply: dest uid {st.st_uid} "
143 f"!= {nonroot.pw_uid} ({nonroot.pw_name})")
144
145 print(f"daemon-early-exec-nameconv: early-exec env+stdin ok; "
146 f"name-converter handled {len(reqs)} request(s)")