| 1 |
|
| 2 |
"""Daemon coverage: `auth users = @group` matching and `gid = *`. |
| 3 |
|
| 4 |
daemon-auth_test.py covers plain username auth; this exercises the |
| 5 |
`@group` branch in authenticate.c auth_server() (lines ~362-395), which |
| 6 |
resolves the connecting username to a uid (user_to_uid), enumerates that |
| 7 |
uid's groups via uidlist.c getallgroups() -> getgrouplist(), and |
| 8 |
wildmatches each group name against the @pattern. Also covers |
| 9 |
clientserver.c want_all_groups() via a `gid = *` module, and |
| 10 |
uidlist.c is_in_group() via a non-root receiver child. |
| 11 |
|
| 12 |
Needs real passwd/group entries -- skips if `daemon` (or any non-root |
| 13 |
user with a same-named primary group) isn't present. |
| 14 |
""" |
| 15 |
|
| 16 |
import grp |
| 17 |
import os |
| 18 |
import platform |
| 19 |
import pwd |
| 20 |
import subprocess |
| 21 |
|
| 22 |
from rsyncfns import ( |
| 23 |
FROMDIR, SCRATCHDIR, |
| 24 |
make_tree, makepath, owners_supported, rmtree, rsync_argv, |
| 25 |
start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
if platform.system() != 'Linux': |
| 33 |
test_skipped("@group daemon-auth coverage is Linux-specific") |
| 34 |
|
| 35 |
DAEMON_PORT = 12892 |
| 36 |
os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong' |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
def pick_user(): |
| 43 |
for name in ('daemon', 'bin', 'nobody'): |
| 44 |
try: |
| 45 |
p = pwd.getpwnam(name) |
| 46 |
g = grp.getgrgid(p.pw_gid) |
| 47 |
if p.pw_uid != 0: |
| 48 |
return p, g |
| 49 |
except KeyError: |
| 50 |
continue |
| 51 |
return None, None |
| 52 |
|
| 53 |
U, G = pick_user() |
| 54 |
if U is None: |
| 55 |
test_skipped("no suitable non-root passwd entry for @group auth") |
| 56 |
|
| 57 |
|
| 58 |
def pick_outsider(): |
| 59 |
for p in pwd.getpwall(): |
| 60 |
if p.pw_uid in (0, U.pw_uid): |
| 61 |
continue |
| 62 |
try: |
| 63 |
if G.gr_gid != p.pw_gid and U.pw_name not in grp.getgrgid(p.pw_gid).gr_mem: |
| 64 |
return p |
| 65 |
except KeyError: |
| 66 |
continue |
| 67 |
return None |
| 68 |
|
| 69 |
OUT = pick_outsider() |
| 70 |
|
| 71 |
src = FROMDIR |
| 72 |
rmtree(src) |
| 73 |
make_tree(src, depth=2) |
| 74 |
|
| 75 |
secrets = SCRATCHDIR / 'group.secrets' |
| 76 |
lines = [f'{U.pw_name}:upass', 'root:rpass'] |
| 77 |
if OUT: |
| 78 |
lines.append(f'{OUT.pw_name}:opass') |
| 79 |
secrets.write_text('\n'.join(lines) + '\n') |
| 80 |
secrets.chmod(0o600) |
| 81 |
|
| 82 |
dest_g = SCRATCHDIR / 'dest-gauth' |
| 83 |
dest_star = SCRATCHDIR / 'dest-gidstar' |
| 84 |
dest_nonroot = SCRATCHDIR / 'dest-nonroot' |
| 85 |
makepath(dest_g, dest_star, dest_nonroot) |
| 86 |
|
| 87 |
|
| 88 |
if not owners_supported(): |
| 89 |
test_skipped("needs chown to set up the non-root-owned dest tree") |
| 90 |
os.chown(dest_nonroot, U.pw_uid, U.pw_gid) |
| 91 |
|
| 92 |
mods = [ |
| 93 |
|
| 94 |
|
| 95 |
('gauth', { |
| 96 |
'path': str(dest_g), 'read only': 'no', 'use chroot': 'no', |
| 97 |
'auth users': f'@{G.gr_name}, @ro*:ro', |
| 98 |
'secrets file': str(secrets), 'strict modes': 'no', |
| 99 |
}), |
| 100 |
|
| 101 |
('gidstar', { |
| 102 |
'path': str(dest_star), 'read only': 'no', 'use chroot': 'no', |
| 103 |
'uid': '0', 'gid': '*', |
| 104 |
}), |
| 105 |
] |
| 106 |
|
| 107 |
|
| 108 |
if owners_supported(): |
| 109 |
mods.append(('nonroot', { |
| 110 |
'path': str(dest_nonroot), 'read only': 'no', 'use chroot': 'no', |
| 111 |
'uid': str(U.pw_uid), 'gid': str(U.pw_gid), |
| 112 |
})) |
| 113 |
|
| 114 |
conf = write_daemon_conf(mods, name='auth-group.conf') |
| 115 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 116 |
|
| 117 |
|
| 118 |
def pwfile(name, text): |
| 119 |
p = SCRATCHDIR / name |
| 120 |
p.write_text(text) |
| 121 |
p.chmod(0o600) |
| 122 |
return p |
| 123 |
|
| 124 |
|
| 125 |
def push(user, pw, module, *extra): |
| 126 |
return subprocess.run( |
| 127 |
rsync_argv('-r', f'--password-file={pw}', *extra, |
| 128 |
f'{src}/', url.replace('rsync://', f'rsync://{user}@', 1) + f'{module}/'), |
| 129 |
capture_output=True, text=True, |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
r = push(U.pw_name, pwfile('pw-u', 'upass'), 'gauth') |
| 135 |
if r.returncode != 0: |
| 136 |
test_fail(f"@{G.gr_name} should allow {U.pw_name!r} (rc={r.returncode}):\n{r.stderr}") |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
r = push('root', pwfile('pw-r', 'rpass'), 'gauth') |
| 142 |
if r.returncode == 0: |
| 143 |
test_fail(f"@ro*:ro should make [gauth] read-only for root, but push succeeded") |
| 144 |
if 'read only' not in r.stderr: |
| 145 |
test_fail(f"expected 'read only' for root@gauth (got rc={r.returncode}):\n{r.stderr}") |
| 146 |
|
| 147 |
|
| 148 |
if OUT: |
| 149 |
r = push(OUT.pw_name, pwfile('pw-o', 'opass'), 'gauth') |
| 150 |
if r.returncode == 0: |
| 151 |
test_fail(f"{OUT.pw_name!r} (group {grp.getgrgid(OUT.pw_gid).gr_name!r}) " |
| 152 |
f"should NOT match @{G.gr_name}/@ro*, but push succeeded") |
| 153 |
if 'auth failed' not in r.stderr: |
| 154 |
test_fail(f"expected 'auth failed' for {OUT.pw_name!r}:\n{r.stderr}") |
| 155 |
|
| 156 |
|
| 157 |
r = subprocess.run(rsync_argv('-r', f'{src}/', f'{url}gidstar/'), |
| 158 |
capture_output=True, text=True) |
| 159 |
if r.returncode != 0: |
| 160 |
test_fail(f"push to [gidstar] (gid=*) failed (rc={r.returncode}):\n{r.stderr}") |
| 161 |
|
| 162 |
|
| 163 |
if owners_supported(): |
| 164 |
r = subprocess.run(rsync_argv('-rg', f'{src}/', f'{url}nonroot/'), |
| 165 |
capture_output=True, text=True) |
| 166 |
if r.returncode != 0: |
| 167 |
test_fail(f"push to [nonroot] (uid={U.pw_uid}) failed (rc={r.returncode}):\n{r.stderr}") |
| 168 |
|
| 169 |
print(f"daemon-auth-group: @{G.gr_name} allow, @ro*:ro, " |
| 170 |
f"{'@-deny, ' if OUT else ''}gid=*, " |
| 171 |
f"{'non-root is_in_group' if owners_supported() else 'is_in_group SKIPPED (non-root)'}") |