| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
if setgid and alt_gid is not None: |
| 34 |
os.chown(todir, -1, alt_gid) |
| 35 |
|
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 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 |
| 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) |