Debian Codegraph @

testsuite/daemon-auth-digest-floor_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon 'auth digest' floor: refuse an auth digest weaker than a configured minimum.
3
4 The daemon picks the auth challenge-response digest from the client's
5 negotiation, or -- for a peer that sends no digest list (any rsync older than
6 3.2.0, e.g. the macOS openrsync) -- falls back to md5 (proto >= 30) or md4. So
7 an on-path downgrade, or simply an old client, can pin the auth exchange to
8 md5/md4, making a captured response far cheaper to brute-force offline.
9 'auth digest = NAME' lets the operator require at least NAME (strongest-first
10 order: sha512 sha256 sha1 md5 md4); a connection whose negotiated digest is
11 weaker -- or a misconfigured floor naming an unknown digest -- is refused before
12 the challenge. Default off (no floor).
13
14 Coverage:
15 * a default (modern) client negotiates sha512 and clears an 'auth digest =
16 sha256' floor;
17 * a floor naming a digest this build doesn't have is fail-closed (rejects even
18 a strong client) -- portable, proves the floor actually enforces;
19 * a genuine pre-3.2.0 client (no digest list -> md5 fallback) is accepted by a
20 no-floor module but refused by the sha256 floor (the real downgrade case) --
21 gated on a bundled old static rsync, which is export-ignored from the fleet
22 git-archive, so it's only exercised where that binary is present.
23 """
24
25 import os
26 import shlex
27 import subprocess
28
29 from rsyncfns import (
30 FROMDIR, RSYNC, SCRATCHDIR, SRCDIR,
31 make_tree, makepath, rmtree, run_rsync, rsync_argv, start_test_daemon,
32 test_fail, test_skipped, write_daemon_conf, split_rsync_cmd,
33 )
34
35 DAEMON_PORT = 12953
36
37 # Requiring a strong auth digest needs a build that has one.
38 vv = run_rsync('-VV', check=True, capture_output=True).stdout
39 if '"sha256"' not in vv or '"sha512"' not in vv:
40 test_skipped("rsync built without sha256/sha512 daemon-auth digests "
41 "(no openssl); cannot require a strong auth digest")
42
43 # Never fall back to an interactive getpass() on a missing password (it reads
44 # /dev/tty and would hang the harness) -- see daemon-auth_test.py.
45 os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong'
46
47 src = FROMDIR
48 rmtree(src)
49 make_tree(src, depth=3)
50
51 floordir = SCRATCHDIR / 'floordest'
52 nofloordir = SCRATCHDIR / 'nofloordest'
53 badfloordir = SCRATCHDIR / 'badfloordest'
54 secrets = SCRATCHDIR / 'rsyncd.secrets'
55 secrets.write_text('tuser:secretpass\n')
56 secrets.chmod(0o600)
57
58 pw = SCRATCHDIR / 'pw.ok'
59 pw.write_text('secretpass\n')
60 pw.chmod(0o600)
61
62 conf = write_daemon_conf([
63 ('floor', {'path': floordir, 'read only': 'no',
64 'auth users': 'tuser', 'secrets file': secrets,
65 'auth digest': 'sha256'}),
66 ('nofloor', {'path': nofloordir, 'read only': 'no',
67 'auth users': 'tuser', 'secrets file': secrets}),
68 ('badfloor', {'path': badfloordir, 'read only': 'no',
69 'auth users': 'tuser', 'secrets file': secrets,
70 'auth digest': 'no-such-digest'}),
71 ])
72 url = start_test_daemon(conf, DAEMON_PORT)
73 userurl = url.replace('rsync://', 'rsync://tuser@', 1)
74
75
76 def push(module, dest, rsync_cmd=None):
77 rmtree(dest)
78 makepath(dest)
79 argv = rsync_argv('-a', f'--password-file={pw}', f'{src}/', f'{userurl}{module}/')
80 if rsync_cmd:
81 # Replace the whole RSYNC wrapper (which under --valgrind is
82 # 'valgrind <opts> .../rsync', not a bare binary) with the old static
83 # rsync; keep only the transfer args that rsync_argv appended. The old
84 # fixture binary is run directly, not under valgrind.
85 argv = [rsync_cmd] + list(argv[len(split_rsync_cmd(RSYNC)):])
86 return subprocess.run(argv, stdout=subprocess.DEVNULL,
87 stderr=subprocess.PIPE, text=True)
88
89
90 # --- a strong (default) client clears the floor -----------------------------
91 proc = push('floor', floordir)
92 if proc.returncode not in (0, 23):
93 test_fail("a default client (negotiates sha512) was refused by the "
94 f"'auth digest = sha256' floor: {proc.stderr}")
95
96 # --- a floor naming an unknown digest is fail-closed (portable reject) -------
97 proc = push('badfloor', badfloordir)
98 if proc.returncode == 0 or 'auth failed' not in proc.stderr:
99 test_fail("a module whose 'auth digest' names an unsupported digest must "
100 "refuse auth (fail-closed) with an auth-failure error, not just a "
101 f"transfer failure: rc={proc.returncode} stderr={proc.stderr!r}")
102
103 # --- real downgrade: a pre-3.2.0 client (md5 fallback) vs the floor ----------
104 old_rsync = SRCDIR / 'old_versions' / 'rsync_3.1.3'
105 if not old_rsync.is_file() or not os.access(old_rsync, os.X_OK):
106 print("daemon auth-digest floor: strong client accepted, bad-floor refused; "
107 "skipped the md5-downgrade case (no old_versions/rsync_3.1.3 here)")
108 raise SystemExit(0)
109
110 # The static rsync_3.1.3 is a Linux binary; confirm it actually runs on this
111 # OS/arch before depending on it (a full checkout ships it even on BSD/macOS,
112 # where exec raises OSError or it won't print the rsync banner) -- skip the
113 # md5-downgrade case there rather than failing the whole test.
114 try:
115 probe = subprocess.run([str(old_rsync), '--version'],
116 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
117 runs = probe.returncode == 0 and 'version 3.1.3' in probe.stdout
118 except OSError:
119 runs = False
120 if not runs:
121 print("daemon auth-digest floor: strong client accepted, bad-floor refused; "
122 "skipped the md5-downgrade case (rsync_3.1.3 does not run on this OS/arch)")
123 raise SystemExit(0)
124
125 # Control: the old (md5-auth) client authenticates fine against a no-floor module.
126 proc = push('nofloor', nofloordir, rsync_cmd=str(old_rsync))
127 if proc.returncode not in (0, 23):
128 test_fail("control failed: a pre-3.2.0 (md5-auth) client could not "
129 f"authenticate even without an auth-digest floor: {proc.stderr}")
130
131 # The floor refuses that same md5 client.
132 proc = push('floor', floordir, rsync_cmd=str(old_rsync))
133 if proc.returncode == 0 or 'auth failed' not in proc.stderr:
134 test_fail("the 'auth digest = sha256' floor did NOT refuse (with an "
135 "auth-failure error) a pre-3.2.0 client whose auth digest fell "
136 f"back to md5: rc={proc.returncode} stderr={proc.stderr!r}")
137
138 print("daemon auth-digest floor: strong client accepted, bad-floor refused, "
139 "md5-downgraded old client refused")