Debian Codegraph @

testsuite/dir-sgid_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/dir-sgid.test.
3 #
4 # Check that rsync obeys the setgid bit on the destination's parent
5 # directory when creating a new directory to hold the transferred files,
6 # even though that parent directory is outside the transfer itself.
7
8 import os
9 import shutil
10 import subprocess
11
12 from rsyncfns import (
13 SCRATCHDIR, check_perms, run_rsync, test_fail, test_skipped,
14 )
15
16
17 old_umask = os.umask(0o077)
18
19 # A secondary group (if the user has one) lets the gid checks below be
20 # discriminating: a setgid parent makes new dirs inherit the PARENT's group,
21 # whereas without setgid they get the process's own group.
22 prim = os.getgid()
23 alt_gid = next((g for g in os.getgroups() if g != prim), None)
24
25
26 def testit(dirname, dirperms, file_expected, prog_expected, dir_expected, setgid):
27 """Mirror shell `testit dirname dirperms file_expected prog_expected dir_expected`."""
28 todir = SCRATCHDIR / dirname
29 todir.mkdir()
30 # For the setgid case, give the parent a distinct group (when available) so
31 # the inherited gid differs from the process's; chmod is applied after the
32 # chown so the setgid bit survives.
33 if setgid and alt_gid is not None:
34 os.chown(todir, -1, alt_gid)
35 # dirperms is either an octal int or the symbolic shell form we translate.
36 if isinstance(dirperms, int):
37 os.chmod(todir, dirperms)
38 else:
39 subprocess.run(['chmod', dirperms, str(todir)], check=True)
40
41 run_rsync('-rvv', str(SCRATCHDIR / 'dir'),
42 str(SCRATCHDIR / 'file'),
43 str(SCRATCHDIR / 'program'),
44 f'{todir}/to/')
45
46 check_perms(todir / 'to', dir_expected)
47 check_perms(todir / 'to' / 'dir', dir_expected)
48 check_perms(todir / 'to' / 'file', file_expected)
49 check_perms(todir / 'to' / 'program', prog_expected)
50
51 # With setgid set, new dirs must inherit the PARENT's group. We only assert
52 # the setgid case: it holds on both SysV/Linux and BSD, and (because the
53 # parent is given a secondary group above) still proves setgid took effect
54 # on Linux. The no-setgid case is deliberately not checked -- the inherited
55 # gid is OS-defined there (the process's group on Linux, but always the
56 # parent's on BSD), so it's not a portable assertion.
57 if setgid:
58 expect_gid = os.stat(todir).st_gid
59 for sub in ('to', 'to/dir'):
60 g = os.stat(todir / sub).st_gid
61 if g != expect_gid:
62 test_fail(f"{dirname}: {sub} gid is {g}, expected {expect_gid} "
63 "(setgid inheritance)")
64
65
66 # Cygwin's default dir ACL ruins this test; mimic the shell's getfacl skip.
67 src_dir = SCRATCHDIR / 'dir'
68 src_dir.mkdir()
69 try:
70 out = subprocess.run(['getfacl', str(src_dir)],
71 capture_output=True, text=True)
72 if 'default:user::' in out.stdout:
73 test_skipped("The default ACL mode interferes with this test")
74 except FileNotFoundError:
75 pass # No getfacl -- proceed.
76
77 (SCRATCHDIR / 'file').write_text("File!\n")
78 (SCRATCHDIR / 'program').write_text("#!/bin/sh\n")
79
80 try:
81 subprocess.run(['chmod', 'u=rwx,g=rw,g+s,o=r', str(src_dir)], check=True)
82 except subprocess.CalledProcessError:
83 test_skipped("Can't chmod")
84 os.chmod(SCRATCHDIR / 'file', 0o664)
85 os.chmod(SCRATCHDIR / 'program', 0o775)
86
87 if not (os.stat(src_dir).st_mode & 0o2000):
88 test_skipped("The directory setgid bit vanished!")
89
90 (src_dir / 'blah').mkdir()
91 if not (os.stat(src_dir / 'blah').st_mode & 0o2000):
92 test_skipped("Your filesystem doesn't use directory setgid; maybe it's BSD.")
93
94 testit('setgid-off', 0o700, 'rw-------', 'rwx------', 'rwx------', setgid=False)
95 testit('setgid-on', 'u=rwx,g=rw,g+s,o-rwx', 'rw-------', 'rwx------', 'rwx--S---',
96 setgid=True)
97
98 os.umask(old_umask)