diff --git a/.github/scripts/changes.py b/.github/scripts/changes.py index 77883cd64..84cb68565 100644 --- a/.github/scripts/changes.py +++ b/.github/scripts/changes.py @@ -146,6 +146,8 @@ def select(paths): enable(selected, *OUTPUTS) elif path in ARCHIVE_DOCUMENTS: enable(selected, 'archive', 'diagnostics') + elif path == 'rsync-web/backup.txt': + enable(selected, 'diagnostics') elif no_tests(path): continue elif path.startswith('testsuite/'): diff --git a/.github/scripts/diagnostics.py b/.github/scripts/diagnostics.py index dc44428f3..b78aa4c3f 100644 --- a/.github/scripts/diagnostics.py +++ b/.github/scripts/diagnostics.py @@ -17,6 +17,13 @@ from harness.check_recipe import check_command +SHELL_NAMES = r'(?:ash|bash|dash|ksh|mksh|sh|zsh)' +SHELL_FILENAME = re.compile(rf'\.{SHELL_NAMES}(?:\.in)?\Z') +SHELL_SHEBANG = re.compile( + rf'^#![ \t]*(?:[^ \t\r\n]*/)?(?:env[ \t]+(?:-S[ \t]+)?)?{SHELL_NAMES}(?:[ \t\r\n]|$)') +SHEBANG_BYTES = 256 + + def check_helpers(directory): suites = sorted(directory.glob('test-*.py')) if not suites: @@ -29,6 +36,26 @@ def check_helpers(directory): return 0 +def check_shell_scripts(directory): + paths = subprocess.check_output(['git', 'ls-files', '-z'], cwd=directory).split(b'\0') + scripts = [] + for name in filter(None, paths): + relative = os.fsdecode(name) + path = directory / relative + if path.is_symlink() or not path.is_file(): + continue + if not SHELL_FILENAME.search(path.name): + with path.open('rb') as stream: + shebang = stream.readline(SHEBANG_BYTES).decode('ascii', errors='replace') + if not SHELL_SHEBANG.match(shebang): + continue + scripts.append(relative) + if not scripts: + raise ValueError('No tracked shell scripts found') + print(f'Checking {len(scripts)} tracked shell scripts', flush=True) + return subprocess.call(['shellcheck', '--', *scripts], cwd=directory) + + def logged(command, path): with path.open('w') as log, subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, errors='replace') as proc: @@ -96,6 +123,7 @@ def main(): sub = parser.add_subparsers(dest='task', required=True) sub.add_parser('install-actionlint') sub.add_parser('check-helpers') + sub.add_parser('check-shell-scripts') coverage = sub.add_parser('coverage') coverage.add_argument('transport', choices=['pipe', 'tcp']) sub.add_parser('coverage-summary') @@ -113,6 +141,8 @@ def main(): return 0 if args.task == 'check-helpers': return check_helpers(Path(__file__).resolve().parent) + if args.task == 'check-shell-scripts': + return check_shell_scripts(Path(__file__).resolve().parents[2]) if args.task == 'coverage': target = 'coverage' if args.transport == 'pipe' else 'coverage-tcp' return logged(['make', target], Path(f'cov-{args.transport}.log')) diff --git a/.github/scripts/test-changes.py b/.github/scripts/test-changes.py index 8a5cb1a60..38dd44687 100644 --- a/.github/scripts/test-changes.py +++ b/.github/scripts/test-changes.py @@ -37,6 +37,9 @@ def test_generated_document_inputs_select_build_qualification(self): def test_editorconfig_selects_no_test_family(self): self.assertEqual(self.enabled(['.editorconfig']), set()) + def test_website_backup_script_selects_diagnostics(self): + self.assertEqual(self.enabled(['rsync-web/backup.txt']), {'diagnostics'}) + def test_production_paths_select_the_full_matrix(self): for path in ('receiver.c', 'Makefile.in', 'packaging/release.py', 'README.c', 'support/README.md', 'lib/new.c', 'wildtest.txt', diff --git a/.github/scripts/test-diagnostics.py b/.github/scripts/test-diagnostics.py index 6953bac20..961c82215 100644 --- a/.github/scripts/test-diagnostics.py +++ b/.github/scripts/test-diagnostics.py @@ -28,6 +28,26 @@ def test_helper_runner_rejects_an_empty_roster(self): with self.assertRaisesRegex(ValueError, 'No CI helper tests'): diagnostics['check_helpers'](Path(directory)) + def test_shellcheck_discovers_tracked_scripts_and_propagates_failure(self): + with tempfile.TemporaryDirectory(prefix='rsync-diagnostics-') as directory: + root = Path(directory) + files = {'--shell helper': '#! /bin/sh -e\n', + 'backup.txt': '#!/usr/bin/env -S bash -e\n', + 'template.sh.in': '# shellcheck shell=sh\n', + 'example.md': 'Example:\n#!/bin/sh\n', + 'other': '#!/usr/bin/env python3\n', + 'untracked.sh': '#!/bin/sh\n'} + for name, contents in files.items(): + (root / name).write_text(contents) + tracked = b'--shell helper\0backup.txt\0template.sh.in\0example.md\0other\0' + process = diagnostics['subprocess'] + with patch.object(process, 'check_output', return_value=tracked) as list_files, \ + patch.object(process, 'call', return_value=17) as lint, redirect_stdout(StringIO()): + self.assertEqual(diagnostics['check_shell_scripts'](root), 17) + list_files.assert_called_once_with(['git', 'ls-files', '-z'], cwd=root) + lint.assert_called_once_with(['shellcheck', '--', '--shell helper', 'backup.txt', + 'template.sh.in'], cwd=root) + def test_logged_failure(self): with tempfile.TemporaryDirectory(prefix='rsync-diagnostics-') as directory: log = Path(directory) / 'run.log' diff --git a/.github/workflows/diagnostics.yml b/.github/workflows/diagnostics.yml index e658d6722..388879720 100644 --- a/.github/workflows/diagnostics.yml +++ b/.github/workflows/diagnostics.yml @@ -85,11 +85,15 @@ jobs: if: ${{ inputs.lint }} runs-on: ubuntu-24.04 timeout-minutes: 15 - name: Workflow lint + name: Lint steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false + - name: Check shell scripts + run: | + shellcheck --version + python3 -B -S .github/scripts/diagnostics.py check-shell-scripts - name: Install actionlint run: python3 -S .github/scripts/diagnostics.py install-actionlint - name: Version diff --git a/config.sub b/config.sub index de4259e40..0d773c425 100755 --- a/config.sub +++ b/config.sub @@ -120,8 +120,8 @@ case $# in esac # Split fields of configuration type -# shellcheck disable=SC2162 saved_IFS=$IFS +# shellcheck disable=SC2162 IFS="-" read field1 field2 field3 field4 < $FAKE_ROOT/pkginfo << EOF_INFO +cat > "$FAKE_ROOT/pkginfo" << EOF_INFO || exit 1 PKG=$PKGNAME NAME=$NAME DESC="Program for efficient remote updates of files." @@ -61,7 +57,7 @@ CLASSES=none EOF_INFO ## Build prototype file -cat > $FAKE_ROOT/prototype << EOFPROTO +cat > "$FAKE_ROOT/prototype" << EOFPROTO || exit 1 i copyright=COPYING i pkginfo=pkginfo d none bin 0755 bin bin @@ -81,14 +77,14 @@ EOFPROTO ## And now build the package. OUTPUTFILE=$PKGNAME-$VERSION-sol8-$ARCH-local.pkg echo "Building package.." -echo FAKE_ROOT = $FAKE_ROOT -cd $FAKE_ROOT -pkgmk -d . -r . -f ./prototype -o -pkgtrans -os . $OUTPUTFILE $PKGNAME +( + cd "$FAKE_ROOT" || exit 1 + pkgmk -d . -r . -f ./prototype -o && + pkgtrans -os . "$OUTPUTFILE" "$PKGNAME" +) || exit 1 -mv $OUTPUTFILE .. -cd .. +mv "$FAKE_ROOT/$OUTPUTFILE" . || exit 1 # Comment this out if you want to see, which file structure has been created -rm -rf $FAKE_ROOT +rm -rf "$FAKE_ROOT" diff --git a/rsync-ssl b/rsync-ssl index 822d9ff84..5d38ce3f8 100755 --- a/rsync-ssl +++ b/rsync-ssl @@ -78,21 +78,20 @@ function rsync_ssl_helper { ;; esac + local -a gnutls_opts=(--logfile=/dev/null) if [[ -z "$RSYNC_SSL_CERT" ]]; then certopt="" - gnutls_cert_opt="" socat_cert_opt="" else certopt="-cert$optsep$RSYNC_SSL_CERT" - gnutls_cert_opt="--x509certfile=$RSYNC_SSL_CERT" + gnutls_opts+=("--x509certfile=$RSYNC_SSL_CERT") socat_cert_opt=",cert=$RSYNC_SSL_CERT" fi if [[ -z "$RSYNC_SSL_KEY" ]]; then - gnutls_key_opt="" socat_key_opt="" else - gnutls_key_opt="--x509keyfile=$RSYNC_SSL_KEY" + gnutls_opts+=("--x509keyfile=$RSYNC_SSL_KEY") socat_key_opt=",key=$RSYNC_SSL_KEY" fi @@ -105,7 +104,6 @@ function rsync_ssl_helper { # gnutls-cli has no reliable "use the system trust store AND make # verification fatal" switch across versions, so the gnutls path # refuses below unless RSYNC_SSL_ALLOW_INSECURE_GNUTLS is set. - gnutls_opts="" # socat: socat_opts="verify=1" # stunnel: @@ -119,7 +117,7 @@ function rsync_ssl_helper { # openssl: openssl_ca_opts=(-verify 1) # gnutls: - gnutls_opts="--insecure" + gnutls_opts+=(--insecure) # socat: socat_opts="verify=0" # stunnel: @@ -130,7 +128,7 @@ function rsync_ssl_helper { # openssl: openssl_ca_opts=(-CAfile "$RSYNC_SSL_CA_CERT" -verify_return_error -verify 4) # gnutls: - gnutls_opts="--x509cafile=$RSYNC_SSL_CA_CERT" + gnutls_opts+=("--x509cafile=$RSYNC_SSL_CA_CERT") # socat: socat_opts="cafile=$RSYNC_SSL_CA_CERT,verify=1" # stunnel: @@ -182,7 +180,7 @@ function rsync_ssl_helper { if [[ $RSYNC_SSL_TYPE == openssl ]]; then run_openssl "$hostname" "$port" "${openssl_ca_opts[@]}" elif [[ $RSYNC_SSL_TYPE == gnutls ]]; then - exec "$RSYNC_SSL_GNUTLS" --logfile=/dev/null $gnutls_cert_opt $gnutls_key_opt $gnutls_opts $hostname:$port + exec "$RSYNC_SSL_GNUTLS" "${gnutls_opts[@]}" "--port=$port" "$hostname" elif [[ $RSYNC_SSL_TYPE == socat ]]; then exec "$RSYNC_SSL_SOCAT" - "OPENSSL:$hostname:$port,commonname=$hostname$socat_sni_opt,$socat_opts$socat_cert_opt$socat_key_opt" else diff --git a/rsync-web/backup.txt b/rsync-web/backup.txt index c09fb57d1..7b7aa66b9 100644 --- a/rsync-web/backup.txt +++ b/rsync-web/backup.txt @@ -21,16 +21,19 @@ export RSYNC_PASSWORD=XXXXXX ######################################################################## -BACKUPDIR=`date +%A` -OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES - --delete --backup --backup-dir=/$BACKUPDIR -a" - -export PATH=$PATH:/bin:/usr/bin:/usr/local/bin - -# the following line clears the last weeks incremental directory -[ -d $HOME/emptydir ] || mkdir $HOME/emptydir -rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/ -rmdir $HOME/emptydir - -# now the actual transfer -rsync $OPTS $BDIR $BSERVER::$USER/current +BACKUPDIR=$(date +%A) || exit 1 +DESTINATION=$BSERVER::$USER +EMPTYDIR=$HOME/emptydir + +PATH="$PATH:/bin:/usr/bin:/usr/local/bin" +export PATH + +# Clear the previous backup for this weekday using a newly created empty directory. +mkdir "$EMPTYDIR" || exit 1 +rsync --delete -a "$EMPTYDIR/" "$DESTINATION/$BACKUPDIR/" +status=$? +rmdir "$EMPTYDIR" || exit 1 +[ "$status" -eq 0 ] || exit "$status" + +rsync --force --ignore-errors --delete-excluded --exclude-from="$EXCLUDES" \ + --delete --backup --backup-dir="/$BACKUPDIR" -a "$BDIR" "$DESTINATION/current" diff --git a/testsuite/README.md b/testsuite/README.md index 1067d4f08..f2cd2a616 100644 --- a/testsuite/README.md +++ b/testsuite/README.md @@ -58,7 +58,7 @@ With profiles active, an unsupported result is accepted only when the test decla [ci.yml](../.github/workflows/ci.yml) defines the platform groups and required checks. Native jobs exercise root and non-root transfers where available. Historical peers run applicable tests in both executable roles over pipe and TCP. Diagnostics cover the minimum Python version, static analysis, sanitisers, coverage and Valgrind. Coverage reports omit daemon work after chroot because its counter files are outside the chroot. -The minimum Python and workflow lint jobs use the same helper test roster. Run those checks from the repository root with: +The minimum Python and lint jobs use the same helper test roster. Run those checks from the repository root with: ```sh python3 -B -S .github/scripts/diagnostics.py check-helpers ``` diff --git a/testsuite/tests/build/solaris-package.py b/testsuite/tests/build/solaris-package.py new file mode 100644 index 000000000..dcddb4314 --- /dev/null +++ b/testsuite/tests/build/solaris-package.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import os +import subprocess + +from harness import TestContext, requires, run +from harness.results import test_fail + + +def make_tool(directory, name, body): + path = directory / name + path.write_text('#!/bin/sh\n' + body) + path.chmod(0o755) + + +def package(context, tools, name, *, fail_build=False, existing_stage=False): + source = context.scratch / name + work = source / 'packaging/solaris/5.8' + work.mkdir(parents=True) + for artifact in ('rsync', 'rsync.1', 'rsyncd.conf.5', 'README.md', 'COPYING', 'tech_report.pdf'): + (source / artifact).write_text(f'fixture {artifact}\n') + stage = work / 'SMBrsync' + if existing_stage: + stage.mkdir() + (stage / 'existing').write_text('preserve this file\n') + + trace = source / 'package.trace' + env = {**os.environ, 'PATH': str(tools) + os.pathsep + os.environ.get('PATH', ''), + 'PACKAGE_TRACE': str(trace), 'PACKAGE_STAGE': str(stage.resolve()), + 'PACKAGE_FAIL_BUILD': 'yes' if fail_build else 'no'} + script = context.repository / 'packaging/solaris/build_pkg.sh' + result = subprocess.run(['sh', str(script)], cwd=work, env=env, + capture_output=True, text=True, timeout=20) + events = trace.read_text().splitlines() if trace.exists() else [] + return result, work, stage, events + + +@requires(transports={'pipe'}, mutates={'filesystem', 'process'}, tags={'build'}) +def test(context: TestContext): + tools = context.scratch / 'bin' + tools.mkdir() + make_tool(tools, 'pkgmk', 'printf "%s\\n" pkgmk >> "$PACKAGE_TRACE"\n' + '[ "$(pwd -P)" = "$PACKAGE_STAGE" ] || exit 7\n' + '[ -f pkginfo ] && [ -f prototype ] || exit 8\n' + '[ "$PACKAGE_FAIL_BUILD" = no ]\n') + make_tool(tools, 'pkgtrans', 'printf "%s\\n" pkgtrans >> "$PACKAGE_TRACE"\n' + '[ "$(pwd -P)" = "$PACKAGE_STAGE" ] || exit 7\n' + 'printf "fixture package\\n" > "$3"\n') + # Record the cleanup target without removing any files on either revision. + make_tool(tools, 'rm', 'printf "%s\\n" cleanup >> "$PACKAGE_TRACE"\n' + '[ "$#" -eq 2 ] && [ "$1" = -rf ] && [ "$(cd "$2" && pwd -P)" = "$PACKAGE_STAGE" ]\n') + + result, work, _, events = package(context, tools, 'checkout with spaces') + packages = list(work.glob('*.pkg')) + if (result.returncode or events != ['pkgmk', 'pkgtrans', 'cleanup'] or len(packages) != 1 + or packages[0].read_text() != 'fixture package\n'): + test_fail(f'package build in a path with spaces failed: {events!r}\n{result.stderr}') + + result, work, stage, events = package(context, tools, 'failed', fail_build=True) + if not result.returncode or events != ['pkgmk'] or list(work.glob('*.pkg')) or not stage.is_dir(): + test_fail(f'package build continued after pkgmk failed: {events!r}\n{result.stderr}') + + result, _, stage, events = package(context, tools, 'existing', existing_stage=True) + if not result.returncode or events or (stage / 'existing').read_text() != 'preserve this file\n': + test_fail(f'package build reused an existing stage: {events!r}\n{result.stderr}') + + +if __name__ == '__main__': + run(test) diff --git a/testsuite/tests/transfer/rsync-ssl.py b/testsuite/tests/transfer/rsync-ssl.py index 5b1ed82e4..3767802da 100644 --- a/testsuite/tests/transfer/rsync-ssl.py +++ b/testsuite/tests/transfer/rsync-ssl.py @@ -67,6 +67,25 @@ def openssl_run(env): if not {'-servername', 'localhost', '-CAfile'} <= set(captured): test_fail(f'hostname opt-out removed SNI or chain verification: {captured}') +fake_gnutls = executable(base / 'gnutls-cli', '#!/bin/sh\nprintf "%s\\n" "$@"\n') +gnutls_env = {name: value for name, value in os.environ.items() + if not name.startswith('RSYNC_SSL_')} +gnutls_env.update(RSYNC_SSL_TYPE='gnutls', RSYNC_SSL_GNUTLS=str(fake_gnutls), + RSYNC_SSL_CA_CERT=str(base / 'ca cert.pem'), + RSYNC_SSL_CERT=str(base / 'client cert.pem'), + RSYNC_SSL_KEY=str(base / 'client key.pem'), RSYNC_PORT='8874') +result = invoke(SRCDIR / 'rsync-ssl', helper, gnutls_env) +expected = ['--logfile=/dev/null', f'--x509certfile={gnutls_env["RSYNC_SSL_CERT"]}', + f'--x509keyfile={gnutls_env["RSYNC_SSL_KEY"]}', + f'--x509cafile={gnutls_env["RSYNC_SSL_CA_CERT"]}', '--port=8874', 'localhost'] +if result.returncode or result.stdout.splitlines() != expected: + test_fail(f'GnuTLS did not receive the certificate paths and endpoint as separate arguments:\n{result.stdout}') + +gnutls_env.pop('RSYNC_SSL_CA_CERT') +result = invoke(SRCDIR / 'rsync-ssl', helper, gnutls_env) +if result.returncode != 1 or 'gnutls-cli requires RSYNC_SSL_CA_CERT' not in result.stdout: + test_fail(f'GnuTLS without a CA was not refused:\n{result.stdout}') + stunnel_dir = base / 'stunnel' stunnel_dir.mkdir() stunnel_config = stunnel_dir / 'config'