From edef6cf07ce8af06e56b42cccd9330a81767d92f Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 3 Nov 2026 05:39:57 +0100 Subject: [PATCH] fix(scheduled-jobs): write a linked crontab through its link, with the target's mode stat without -L gave the link's own mode, 777, and install replaced the link with a plain file of that mode: cron ignores a file anyone may write, so every job in it stopped (/etc/cron.d/vzdump on older PVE links to /etc/pve/vzdump.cron). The file the link names is written instead. Refs #86 --- napalm_device_types/scheduled_jobs_posix.py | 7 +++++-- tests/test_scheduled_jobs_posix.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/napalm_device_types/scheduled_jobs_posix.py b/napalm_device_types/scheduled_jobs_posix.py index 441a198..8fee6c9 100644 --- a/napalm_device_types/scheduled_jobs_posix.py +++ b/napalm_device_types/scheduled_jobs_posix.py @@ -244,8 +244,11 @@ class PosixScheduledJobsMixin(ScheduledJobsMixin): if user is not None: self._sj_write_crontab(user, text) return - mode, owner, group = self._sj_checked([*self.STAT_ARGV, path]).stdout.split()[:3] - self._sj_install(text, path, mode, owner, group) + # Through a symbolic link to the file it names, with that file's mode: the + # link's own mode is 777, and cron ignores a file anyone may write. + real = self._sj_checked(["readlink", "-f", "--", path]).stdout.strip() or path + mode, owner, group = self._sj_checked([*self.STAT_ARGV, real]).stdout.split()[:3] + self._sj_install(text, real, mode, owner, group) def _sj_line(self, job: dict[str, object], text: str) -> CronLine: """The job's line in *text* as it is now, read again for the write.""" diff --git a/tests/test_scheduled_jobs_posix.py b/tests/test_scheduled_jobs_posix.py index 57d515e..4b746e7 100644 --- a/tests/test_scheduled_jobs_posix.py +++ b/tests/test_scheduled_jobs_posix.py @@ -335,6 +335,19 @@ class TestSwitching: assert f"install -D -m {mode & 0o777:o}" in host.log() assert os.stat(path).st_mode == mode + def test_a_linked_file_is_written_through_its_link(self, root: Path): + real = root / "etc/vzdump.cron" + real.write_text("0 1 * * * root vzdump --all\n") + real.chmod(0o640) + link = root / "etc/cron.d/vzdump" + link.symlink_to(real) + host = Host(root) + job = self._cron(host, str(link)) + host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + assert link.is_symlink() + assert real.read_text() == "#napalm-disabled: 0 1 * * * root vzdump --all\n" + assert real.stat().st_mode & 0o777 == 0o640 + def test_a_users_line_goes_through_crontab(self, root: Path): host = Host(root) job = self._cron(host, str(root / "spool/www-data"))