diff --git a/NEWS.md b/NEWS.md index d01174fd1..fb9ea37d3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ of `--update`. - Allow daemon modules to apply PAM account policy after rsync authentication. Both build-time support and the module setting are disabled by default. +- Accept `unix:` in module host rules for named Unix domain sockets. + Proxy protocol trust and module authentication are unchanged. - Let rrsync administrators require a server-side transfer log outside the restricted directory with `-log FILE`. Refuse transfers when the log cannot be opened and report later log write failures. diff --git a/access.c b/access.c index 08d603945..d278321d5 100644 --- a/access.c +++ b/access.c @@ -324,7 +324,8 @@ static int match_address(const char *addr, char *tok) return ret; } -static int access_match(const char *list, const char *addr, const char **host_ptr, int deny) +static int access_match(const char *list, const char *addr, const char **host_ptr, + int deny, BOOL named_unix) { char *tok; char *list2 = strdup(list); @@ -332,7 +333,9 @@ static int access_match(const char *list, const char *addr, const char **host_pt strlower(list2); for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) { - if (match_hostname(host_ptr, addr, tok, deny) || match_address(addr, tok)) { + int match = strcmp(tok, "unix:") == 0 ? named_unix + : match_hostname(host_ptr, addr, tok, deny) || match_address(addr, tok); + if (match) { free(list2); return 1; } @@ -342,7 +345,7 @@ static int access_match(const char *list, const char *addr, const char **host_pt return 0; } -int allow_access(const char *addr, const char **host_ptr, int i) +int allow_access(const char *addr, const char **host_ptr, int i, BOOL named_unix) { const char *allow_list = lp_hosts_allow(i); const char *deny_list = lp_hosts_deny(i); @@ -356,7 +359,7 @@ int allow_access(const char *addr, const char **host_ptr, int i) /* If we match an allow-list item, we always allow access. */ if (allow_list) { - if (access_match(allow_list, addr, host_ptr, 0)) + if (access_match(allow_list, addr, host_ptr, 0, named_unix)) return 1; /* For an allow-list w/o a deny-list, disallow non-matches. */ if (!deny_list) @@ -365,7 +368,7 @@ int allow_access(const char *addr, const char **host_ptr, int i) /* If we match a deny-list item (and got past any allow-list * items), we always disallow access. */ - if (deny_list && access_match(deny_list, addr, host_ptr, 1)) + if (deny_list && access_match(deny_list, addr, host_ptr, 1, named_unix)) return 0; /* Allow all other access. */ @@ -377,5 +380,5 @@ int allow_proxy_protocol_peer(const char *list, const char *addr, const char **h if (!list || !*list) return 0; allow_forward_dns = 0; - return access_match(list, addr, host_ptr, 0); + return access_match(list, addr, host_ptr, 0, False); } diff --git a/clientserver.c b/clientserver.c index ceb45d4c1..e60aef254 100644 --- a/clientserver.c +++ b/clientserver.c @@ -774,7 +774,8 @@ static struct passwd *want_all_groups(int f_out, uid_t uid) } #endif -static int rsync_module(int f_in, int f_out, int i, const char *addr, const char *host) +static int rsync_module(int f_in, int f_out, int i, const char *addr, const char *host, + BOOL named_unix) { int argc; char **argv, **orig_argv, **orig_early_argv, *module_chdir; @@ -808,7 +809,7 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char set_env_str("RSYNC_HOST_NAME", host); set_env_str("RSYNC_HOST_ADDR", addr); - if (!allow_access(addr, &host, i)) { + if (!allow_access(addr, &host, i, named_unix)) { rprintf(FLOG, "rsync denied on module %s from %s (%s)\n", name, host, addr); if (!lp_list(i)) @@ -1465,7 +1466,7 @@ static int load_config(int globals_only) /* this is called when a connection is established to a client and we want to start talking. The setup of the system is done from here */ -int start_daemon(int f_in, int f_out) +static int start_daemon_session(int f_in, int f_out, BOOL named_unix) { char line[1024]; const char *addr, *host; @@ -1498,6 +1499,8 @@ int start_daemon(int f_in, int f_out) if (lp_proxy_protocol()) { if (!proxy_peer_allowed(f_in) || !read_proxy_protocol_header(f_in)) return -1; + /* Module rules apply to the forwarded client, not the proxy transport. */ + named_unix = False; } /* Do reverse DNS lookup before chroot/setuid. The result is cached, @@ -1629,7 +1632,12 @@ int start_daemon(int f_in, int f_out) install_child_exit_handler(); - return rsync_module(f_in, f_out, i, addr, host); + return rsync_module(f_in, f_out, i, addr, host, named_unix); +} + +int start_daemon(int f_in, int f_out) +{ + return start_daemon_session(f_in, f_out, False); } static void create_pid_file(void) @@ -1804,13 +1812,14 @@ static void become_daemon(void) * supply a connected Unix stream whose local endpoint has a name. Other * launchers may use an unnamed local socket for process I/O, which must not * select inetd mode. */ -static int is_inetd_socket(int fd) +static int is_inetd_socket(int fd, BOOL *named_unix) { int type; struct sockaddr_storage peer = {0}; socklen_t type_len = sizeof type; socklen_t peer_len = sizeof peer; + *named_unix = False; if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&type, &type_len) != 0 || type != SOCK_STREAM || getpeername(fd, (struct sockaddr *)&peer, &peer_len) != 0) @@ -1840,8 +1849,10 @@ static int is_inetd_socket(int fd) if (path_len > sizeof local_un->sun_path) path_len = sizeof local_un->sun_path; for (i = 0; i < path_len; i++) { - if (local_un->sun_path[i] != '\0') + if (local_un->sun_path[i] != '\0') { + *named_unix = True; return 1; + } } return 0; } @@ -1852,7 +1863,9 @@ static int is_inetd_socket(int fd) int daemon_main(void) { - if (is_inetd_socket(STDIN_FILENO)) { + BOOL named_unix; + + if (is_inetd_socket(STDIN_FILENO, &named_unix)) { int i; /* we are running via inetd - close off stdout and @@ -1863,7 +1876,7 @@ int daemon_main(void) open("/dev/null", O_RDWR); } - return start_daemon(STDIN_FILENO, STDIN_FILENO); + return start_daemon_session(STDIN_FILENO, STDIN_FILENO, named_unix); } if (!load_config(1)) { diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md index 0701ae809..fc3729e22 100644 --- a/rsyncd.conf.5.md +++ b/rsyncd.conf.5.md @@ -323,6 +323,7 @@ in the values of parameters. See that section for details. to supply a `proxy protocol` header. The syntax is the same token format used by `hosts allow`. When `proxy protocol = true`, this list must match the direct peer before rsync trusts the forwarded client address. + The `unix:` module access rule is not available for proxy trust. 0. `name converter` @@ -874,7 +875,7 @@ in the values of parameters. See that section for details. client's hostname and IP address. If none of the patterns match, then the connection is rejected. - Each pattern can be in one of six forms: + Each pattern can be in one of these forms: - a dotted decimal IPv4 address of the form a.b.c.d, or an IPv6 address of the form a:b:c::d:e:f. In this case the incoming machine's IP address @@ -896,6 +897,11 @@ in the values of parameters. See that section for details. is enabled, as it is by default). Any match will be allowed in. - an '@' followed by a netgroup name, which will match if the reverse DNS of the connecting IP is in the specified netgroup. + - `unix:`, which matches a direct connection accepted on a named Unix + domain socket passed to the daemon on standard input. This rule + applies to the transport, not the client's user or group. It does not + match a client forwarded through the PROXY protocol or replace the + module's authentication checks. If rsync was built with IDN support (look for "IDN" in `rsync --version`), a hostname pattern may contain non-ASCII characters: this file is read as diff --git a/testsuite/tests/daemon/daemon-unix-access.py b/testsuite/tests/daemon/daemon-unix-access.py new file mode 100644 index 000000000..ae0dcd002 --- /dev/null +++ b/testsuite/tests/daemon/daemon-unix-access.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 + +import os +import socket +import subprocess +import sys +import tempfile + +from harness import TestContext, requires, run +from harness.daemon_config import write_daemon_conf +from harness.results import test_fail, test_skipped +from harness.rsync import rsync_argv, under_valgrind + + +def module_reply(listener, config, module, forwarded=False): + timeout = 15 if under_valgrind() else 5 + with socket.socket(listener.family, socket.SOCK_STREAM) as client: + client.settimeout(timeout) + client.connect(listener.getsockname()) + conn, _ = listener.accept() + with conn: + proc = subprocess.Popen( + rsync_argv('--daemon', '--no-detach', f'--config={config}'), + stdin=conn, stdout=conn, stderr=subprocess.DEVNULL) + try: + with client.makefile('rwb', buffering=0) as stream: + if forwarded: + stream.write(b'PROXY TCP4 127.0.0.1 127.0.0.1 43210 873\r\n') + greeting = stream.readline() + if module is None: + return greeting + if not greeting.startswith(b'@RSYNCD: '): + test_fail(f'{module}: unexpected daemon greeting {greeting!r}') + stream.write(b'@RSYNCD: 31.0\n' + module.encode('ascii') + b'\n') + return stream.readline() + finally: + client.close() + try: + proc.wait(timeout=timeout) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait(timeout=timeout) + test_fail('daemon did not exit after the client disconnected') + + +@requires(features={'unix_listener'}, transports={'pipe'}, min_peer='4.0.0', + mutates={'filesystem', 'process', 'socket'}, tags={'daemon', 'regression'}) +def test(context: TestContext): + if not hasattr(socket, 'AF_UNIX') or sys.platform == 'cygwin': + test_skipped('requires a named Unix socket on daemon stdin', capability='unix_listener') + + context.source.mkdir() + modules = [ + ('allow-unix', {'hosts allow': 'UNIX:'}), + ('deny-unix', {'hosts deny': 'unix:'}), + ('allow-ip', {'hosts allow': '127.0.0.1'}), + ('allow-wins', {'hosts allow': 'unix:', 'hosts deny': '*'}), + ('require-auth', {'hosts allow': 'unix:', 'auth users': 'fixture'}), + ] + modules = [(name, {'path': str(context.source), **options}) for name, options in modules] + common = {'hosts allow': '', 'reverse lookup': 'no'} + direct = write_daemon_conf(modules, common) + proxy = write_daemon_conf(modules, {**common, 'proxy protocol': 'yes', + 'proxy protocol hosts': '*'}, name='proxy.conf') + untrusted = write_daemon_conf(modules, {**common, 'proxy protocol': 'yes', + 'proxy protocol hosts': 'unix:'}, name='untrusted.conf') + + def check(listener, config, label, named_unix, forwarded=False): + allowed = b'@RSYNCD: OK' + denied = b'@ERROR: access denied' + expected = { + 'allow-unix': allowed if named_unix else denied, + 'deny-unix': denied if named_unix else allowed, + 'allow-ip': denied if named_unix else allowed, + 'allow-wins': allowed if named_unix else denied, + 'require-auth': b'@RSYNCD: AUTHREQD ' if named_unix else denied, + } + for module, prefix in expected.items(): + reply = module_reply(listener, config, module, forwarded) + if not reply.startswith(prefix): + test_fail(f'{label}/{module}: expected {prefix!r}, got {reply!r}') + + with tempfile.TemporaryDirectory(prefix='rsync-unix-access-') as tmpdir: + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listener: + listener.bind(f'{tmpdir}/peer.sock') + listener.listen(1) + check(listener, direct, 'filesystem Unix', named_unix=True) + check(listener, proxy, 'forwarded IP', named_unix=False, forwarded=True) + if module_reply(listener, untrusted, None): + test_fail('unix: granted trust to a proxy peer') + + if sys.platform.startswith('linux'): + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listener: + listener.bind(f'\0rsync-access-{os.getpid()}') + listener.listen(1) + check(listener, direct, 'abstract Unix', named_unix=True) + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener: + listener.bind(('127.0.0.1', 0)) + listener.listen(1) + check(listener, direct, 'TCP', named_unix=False) + + +if __name__ == '__main__': + run(test)