Debian Codegraph @

testsuite/daemon-groupmap-wild_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for issue #829.
3 #
4 # Without --secluded-args the client's safe_arg() backslash-escapes wildcard
5 # chars in option values, so --chown / --groupmap=*:GROUP is sent to a daemon
6 # as --groupmap=\*:GROUP. A daemon has no shell to strip the backslash, and
7 # read_args() used to store option args verbatim, so the receiver saw the
8 # literal "\*", the wildcard never matched, and the map was ignored (the
9 # module's configured gid won instead). The fix un-backslashes daemon option
10 # args.
11 #
12 # We run it both ways:
13 # * default args -- the '*' is safe_arg-escaped and the daemon must
14 # un-backslash it (the path the fix repairs);
15 # * --secluded-args -- the '*' is sent raw over the protected channel and
16 # read with unescape=0, so it must keep working too
17 # (a guard that the fix didn't disturb that path).
18 #
19 # No root needed: a non-root receiver can chgrp(2) to a group the test user
20 # belongs to, so we map every source group to a second such group and check
21 # the wildcard took effect.
22
23 import os
24 import subprocess
25
26 from rsyncfns import (
27 SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon,
28 test_fail, test_skipped, write_daemon_conf,
29 )
30
31 DAEMON_PORT = 12923
32
33 # Two distinct groups to map between. As root (the usual CI case) we can
34 # chgrp(2) to any gid, so take two distinct named groups from the group
35 # database; a non-root user can only chgrp to groups it belongs to, so use those
36 # (skip if it is in fewer than two).
37 if os.geteuid() == 0:
38 import grp
39 usable = []
40 for gr in grp.getgrall():
41 if gr.gr_gid not in usable:
42 usable.append(gr.gr_gid)
43 if len(usable) < 2:
44 test_skipped("need >=2 groups defined on the system")
45 else:
46 usable = []
47 for g in [os.getgid()] + list(os.getgroups()):
48 if g not in usable:
49 usable.append(g)
50 if len(usable) < 2:
51 test_skipped("need >=2 groups the test user belongs to")
52 src_gid, dst_gid = usable[0], usable[1]
53
54 moddir = SCRATCHDIR / 'gmod'
55 srcdir = SCRATCHDIR / 'gsrc'
56 makepath(moddir)
57
58 conf = write_daemon_conf([('gmod', {'path': str(moddir), 'read only': 'no'})])
59 url = start_test_daemon(conf, DAEMON_PORT) + 'gmod/'
60
61
62 def check(label, *extra_opts):
63 rmtree(moddir)
64 rmtree(srcdir)
65 makepath(moddir)
66 makepath(srcdir)
67 f = srcdir / 'f.dat'
68 f.write_text("hi\n")
69 os.chown(f, -1, src_gid) # source group differs from the map target
70
71 # A --chown-style wildcard map sent to a daemon: the '*' must survive as a
72 # wildcard so every source group is remapped to dst_gid.
73 proc = subprocess.run(
74 rsync_argv('-rg', *extra_opts, f'--groupmap=*:{dst_gid}', str(f), url),
75 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
76 if proc.returncode != 0:
77 print(proc.stdout)
78 test_fail(f"[{label}] groupmap upload failed (rc={proc.returncode})")
79
80 got = os.stat(moddir / 'f.dat').st_gid
81 if got != dst_gid:
82 test_fail(f"[{label}] --groupmap='*:{dst_gid}' wildcard ignored over "
83 f"daemon: got gid {got}, expected {dst_gid} (regression of #829)")
84
85
86 check('default-args')
87 check('secluded-args', '--secluded-args')