diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e7823d..ab86d70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Scheduled jobs on FreeBSD and OpenBSD (NetOrk/netork#1933, #27), through + napalm-device-types' `PosixScheduledJobsMixin`: `get_scheduled_jobs()` reads + `/etc/crontab` and the user crontabs in `/var/cron/tabs`, on FreeBSD also + `/etc/cron.d`, `/usr/local/etc/cron.d` and every `periodic` script in + `/etc/periodic` and `/usr/local/etc/periodic` (`kind="periodic"`, enabled by its + `periodic.conf` knob), in one round trip as root; `manage_scheduled_job()` + enables, disables or runs one now (a periodic script by + `sysrc -f /etc/periodic.conf`); `put_scheduled_job()` and `delete_scheduled_job()` + write and remove an application's own job: a file in `/etc/cron.d` on FreeBSD, + marked lines in the user's crontab on OpenBSD. Another user's job runs through + `su -m` with its script on `/bin/sh`'s stdin, as neither BSD's `su` takes a shell. - netOrk's own account with a fixed uid on FreeBSD (NetOrk/netork#1817): `_account_create_with_uid` makes it with `pw useradd -u -m -w no`. - Personal accounts on FreeBSD (NetOrk/netork#1793), through napalm-device-types' @@ -20,7 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `LocalAccountsMixin`. ### Changed -- The floor on napalm-device-types is 4.23.0, the first release with what this driver uses. +- The floor on napalm-device-types is 4.26.0, the first release with what this driver + uses (`PosixScheduledJobsMixin`). - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). - The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python @@ -44,6 +56,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `expires in N seconds`, OpenBSD's `Expire` column), which is no age: an entry refreshed a second ago looked 20 minutes old (NetOrk/netork#1327). +### Removed +- `get_cron_jobs()`: it read the system crontab and the login user's own only, without + root, and missed `/etc/cron.d`, every other user and FreeBSD's periodic scripts. + `get_scheduled_jobs()` replaces it. + ## [0.5.0] - 2026-10-23 The first release on PyPI. diff --git a/README.md b/README.md index c78bd28..099bd30 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,10 @@ provisions and manages BSD VMs (NetOrk/netork#792). | `get_route_to` | ✓ | ✓ | `netstat -rnW` / `netstat -rn` | | `get_arp_table` | ✓ | ✓ | `arp -an` | | `get_lldp_neighbors` | `{}` | `{}` | no LLDP daemon in either base system | -| `get_os_users`, `get_processes`, `get_cron_jobs` | ✓ | ✓ | `/etc/passwd`+`/etc/group`, `ps … lstart`, system crontab + `crontab -l` | +| `get_os_users`, `get_processes` | ✓ | ✓ | `/etc/passwd`+`/etc/group`, `ps … lstart` | +| `get_scheduled_jobs` | ✓ | ✓ | `/etc/crontab`, `/var/cron/tabs/*`; FreeBSD also `/etc/cron.d`, `/usr/local/etc/cron.d` and every `periodic` script with its `periodic.conf` knob (napalm-device-types' `PosixScheduledJobsMixin`) | +| `manage_scheduled_job(key, action, digest=)` | ✓ | ✓ | enable, disable, run now; a `periodic` script by `sysrc -f /etc/periodic.conf` | +| `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | ✓ | ✓ | an application's own job: a file in `/etc/cron.d` (FreeBSD), marked lines in the user's crontab (OpenBSD) | | `get_listening_sockets` | ✓ | ✓ | `sockstat -46lq` / `fstat -n` as root, `netstat -an` without | | `get_packages`, `install_package`, `uninstall_package` | ✓ | ✓ | `pkg query` / `pkg install`, `pkg delete`; `pkg_info` / `pkg_add -I`, `pkg_delete` | | `get_available_updates` | ✓ | ✓ | `pkg upgrade -n` + `pkg audit` (VuXML); `pkg_add -u -n -v` + `syspatch -c` | @@ -74,6 +77,20 @@ starts sshd only after that and restarts right away. `/etc/rc` removes `/firstbo just before the restart, so `first_boot_pending()` is true until then; netOrk waits for it before it sets up a new VM. OpenBSD has no such marker to ask yet. +**Scheduled jobs** are read in one round trip as root. A job the application did +not write may be enabled, disabled and run, never edited or deleted. What the base +system ships is `platform`: FreeBSD's `/etc/crontab` and `/etc/periodic/*`, and the +`newsyslog`, `/etc/daily`, `weekly` and `monthly` lines of OpenBSD's root crontab +(which run the `.local` files themselves, so those are not jobs of their own). On +FreeBSD a `periodic` script is a job of its own, switched by its +`__enable` knob (`security_status__enable` for the security +scripts, or the one knob the script names); a script whose knob is not known only +runs, and one whose knob is off does nothing when run, so it does not offer `run`. +A job of another user runs through `su -m ` with its script on `/bin/sh`'s +stdin: neither BSD's `su` takes a shell, and `-m` starts root's own (csh on many +FreeBSD hosts). OpenBSD cron's `-n`, `-q` and `-s` flags are left off when a line +runs now. `timeout(1)` bounds a run on both (OpenBSD 7.0 and later). + **Services** are the enabled ones. FreeBSD reads their status as root, as root-only pidfiles hide a daemon from anyone else, and without root when sudo refuses; OpenBSD's `rcctl check` needs no root. Actions run as root. diff --git a/napalm_bsd/base.py b/napalm_bsd/base.py index 769cc2d..ecc98b7 100644 --- a/napalm_bsd/base.py +++ b/napalm_bsd/base.py @@ -28,6 +28,7 @@ from napalm_device_types.models import FixStepOutcomeDict, SnmpAgentSettingsDict from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch from napalm_bsd import parse +from napalm_bsd.scheduled_jobs import BsdScheduledJobsMixin logger = logging.getLogger(__name__) @@ -87,7 +88,9 @@ def render_snmpd_conf(settings: SnmpAgentSettingsDict) -> str: return "\n".join(lines) + "\n" -class BsdDriver(SnmpAgentMixin, EnsurePackageMixin, HostStatusMixin, OSDriver): +class BsdDriver( + BsdScheduledJobsMixin, SnmpAgentMixin, EnsurePackageMixin, HostStatusMixin, OSDriver +): """Base for the BSD drivers; a concrete one names the commands that differ.""" #: One role for FreeBSD and OpenBSD. It replaces OSDriver's "linux", which @@ -540,20 +543,10 @@ class BsdDriver(SnmpAgentMixin, EnsurePackageMixin, HostStatusMixin, OSDriver): port = int(found[1]) if found else port return {"running": True, "community": community, "port": port, "version": "2c"} - # -- accounts, processes, cron ----------------------------------------------- + # -- accounts and processes -------------------------------------------------- def get_os_users(self) -> list[dict[str, Any]]: return parse.users(self._out("cat /etc/passwd"), self._out("cat /etc/group")) def get_processes(self) -> list[dict[str, Any]]: return parse.processes(self._out(self.PS_COMMAND)) - - def get_cron_jobs(self) -> list[dict[str, str]]: - """The system crontab and the login user's own. - - Other users' crontabs (``/var/cron/tabs``) are root's to read. - """ - return [ - *parse.crontab(self._out("cat /etc/crontab"), system=True), - *parse.crontab(self._out("crontab -l"), user=self.username), - ] diff --git a/napalm_bsd/freebsd.py b/napalm_bsd/freebsd.py index edad020..e60b1c2 100644 --- a/napalm_bsd/freebsd.py +++ b/napalm_bsd/freebsd.py @@ -7,9 +7,10 @@ from napalm_device_types import FingerprintRule from napalm_bsd import parse from napalm_bsd.accounts import FreeBSDLocalAccountsMixin from napalm_bsd.base import BASE_SYSTEM, BsdDriver +from napalm_bsd.periodic import PeriodicJobsMixin -class FreeBSDDriver(FreeBSDLocalAccountsMixin, BsdDriver): +class FreeBSDDriver(FreeBSDLocalAccountsMixin, PeriodicJobsMixin, BsdDriver): """NAPALM driver for FreeBSD hosts, over SSH.""" TYPE_LABEL = "FreeBSD" @@ -35,6 +36,20 @@ class FreeBSDDriver(FreeBSDLocalAccountsMixin, BsdDriver): # /etc/rc removes it at the end of the first boot. FIRST_BOOT_MARKER = "/firstboot" + # Scheduled jobs: cron reads both cron.d; the base one is always there. + CRON_D_DIRS = ("/etc/cron.d", "/usr/local/etc/cron.d") + OWN_CRON_D_DIR = "/etc/cron.d" + # What the base system ships is the platform's, packages (pkgbase) or not. + PLATFORM_SOURCES = ("/etc/crontab", "/etc/periodic/*") + # The bootstrap stub would ask to install pkg; "pkg -N" tells it apart. + OWNERS_SH = ( + "if pkg -N >/dev/null 2>&1; then while read -r p; do " + 'o=$(pkg which -q -- "$p" /dev/null) && [ -n "$o" ] && ' + 'printf \'%s: %s\\n\' "$o" "$p"; done; else cat >/dev/null; fi' + ) + # tzsetup names the zone in /var/db/zoneinfo; /etc/localtime may be a copy. + TIME_ZONE_SH = "cat /var/db/zoneinfo 2>/dev/null || readlink /etc/localtime 2>/dev/null" + def _parse_listening(self, output: str, *, attributed: bool) -> list[dict]: return parse.sockstat(output) diff --git a/napalm_bsd/openbsd.py b/napalm_bsd/openbsd.py index e9351a2..e51cba9 100644 --- a/napalm_bsd/openbsd.py +++ b/napalm_bsd/openbsd.py @@ -2,13 +2,19 @@ from __future__ import annotations +import re +from dataclasses import replace from typing import ClassVar from napalm_device_types import FingerprintRule, SnmpHealthProfile +from napalm_device_types._crontab import CronLine from napalm_bsd import parse from napalm_bsd.base import BASE_SYSTEM, BsdDriver +#: OpenBSD cron's flags before a command: -n (no mail), -q (no log), -s (single instance). +_CRON_FLAGS = re.compile(r"^(?:-[nqs]+\s+)+") + class OpenBSDDriver(BsdDriver): """NAPALM driver for OpenBSD hosts, over SSH.""" @@ -35,6 +41,20 @@ class OpenBSDDriver(BsdDriver): LISTENING_COMMAND = "fstat -n" LISTENING_FALLBACK_COMMAND = "netstat -an -f inet; netstat -an -f inet6" + # Scheduled jobs: no cron.d; root's crontab runs /etc/daily, weekly and monthly + # (and their .local), so they are jobs once, as its lines. + PLATFORM_COMMANDS = ( + "/usr/bin/newsyslog", + "/bin/sh /etc/daily", + "/bin/sh /etc/weekly", + "/bin/sh /etc/monthly", + ) + + def _sj_line(self, job: dict[str, object], text: str) -> CronLine: + """The line to switch or run: run without cron's own flags.""" + line = super()._sj_line(job, text) + return replace(line, command=_CRON_FLAGS.sub("", line.command)) + def _parse_listening(self, output: str, *, attributed: bool) -> list[dict]: return parse.fstat_sockets(output) if attributed else parse.netstat_listening(output) diff --git a/napalm_bsd/parse.py b/napalm_bsd/parse.py index 7e7550e..3d8c66c 100644 --- a/napalm_bsd/parse.py +++ b/napalm_bsd/parse.py @@ -224,24 +224,6 @@ def processes(text: str) -> list[dict[str, Any]]: return result -def crontab(text: str, *, system: bool = False, user: str = "") -> list[dict[str, str]]: - """A crontab -> its jobs. A system crontab names the user in the sixth field.""" - jobs: list[dict[str, str]] = [] - for line in text.splitlines(): - line = line.strip() - if not line or line.startswith("#") or re.match(r"^[A-Za-z_][A-Za-z0-9_]*\s*=", line): - continue - fields = 1 if line.startswith("@") else 5 - parts = line.split(None, fields + (1 if system else 0)) - if len(parts) < fields + (2 if system else 1): - continue - schedule = " ".join(parts[:fields]) - owner = parts[fields] if system else user - command = parts[-1] - jobs.append({"user": owner, "schedule": schedule, "command": command}) - return jobs - - def boottime(text: str) -> int | None: """``sysctl -n kern.boottime``: FreeBSD ``{ sec = N, … }``, OpenBSD ``N``.""" match = re.search(r"sec = (\d+)", text) or re.fullmatch(r"\s*(\d+)\s*", text) diff --git a/napalm_bsd/periodic.py b/napalm_bsd/periodic.py new file mode 100644 index 0000000..d15d35c --- /dev/null +++ b/napalm_bsd/periodic.py @@ -0,0 +1,282 @@ +"""FreeBSD's ``periodic``: every script it runs is a scheduled job (#27). + +``periodic daily`` (and weekly, monthly; ``security`` from the daily run) runs +each executable file in ``/etc/periodic/`` and +``/usr/local/etc/periodic/``. A script does its work only when its knob +in ``periodic.conf`` says ``YES``: defaults in ``/etc/defaults/periodic.conf``, +overrides in ``/etc/periodic.conf`` and the files after it. So a script is a job +of ``kind="periodic"``, enabled by its knob; enabling and disabling it writes +``/etc/periodic.conf`` with ``sysrc``; running it runs the script, as periodic does. + +**The knob** is ``__enable`` (``security_status__enable`` for +the security scripts), the name without its number and with ``_`` for ``-``. A +script that names none of these but exactly one ``_…_enable`` has that one +(``480.leapfile-ntpd``: ``daily_ntpd_leapfile_enable``). A script whose knob is not +known this way is only run. A script whose knob is off does nothing when it runs, +so it does not offer ``run`` until it is enabled. + +The scan runs after the cron report in the same round trip, as root; the knobs +are read the way periodic reads them, by sourcing its configuration in a subshell. +""" + +from __future__ import annotations + +import re +from dataclasses import dataclass, field +from shlex import quote +from typing import ClassVar + +from napalm_device_types.channel import CommandResult +from napalm_device_types.scheduled_jobs import ( + ACTIONS, + RUN_TIMEOUT, + ScheduledJobRefused, + ScheduledJobResultDict, + job_digest, +) +from napalm_device_types.terminal import strip_terminal_codes + +from napalm_bsd.scheduled_jobs import BsdScheduledJobsMixin + +PERIODS = ("daily", "weekly", "monthly", "security") +PERIODIC_DIRS = tuple((p, f"/etc/periodic/{p}") for p in PERIODS) + tuple( + (p, f"/usr/local/etc/periodic/{p}") for p in PERIODS +) +PERIODIC_DEFAULTS = "/etc/defaults/periodic.conf" +PERIODIC_CONF = "/etc/periodic.conf" + +_BEGIN = "PERIODIC_BEGIN" +_END = "PERIODIC_END" +#: A knob as a script names it, with the default it gives it (``${x:-YES}``). +_REF = "(daily|weekly|monthly|security)_[A-Za-z0-9_]+_enable(:?-[A-Za-z]+)?" +_REF_PARTS = re.compile(r"^([A-Za-z0-9_]+_enable)(?::?-([A-Za-z]+))?$") +_KNOB = re.compile(r"(daily|weekly|monthly|security)_[A-Za-z0-9_]+_enable") +_SECTION = re.compile(r"^\[(script|conf|read|owners)(?: (.*))?\]$") +_SWITCH = ["enable", "disable", "run"] + + +def _scan(period: str, directory: str) -> str: + return ( + f'for f in {quote(directory)}/*; do [ -f "$f" ] && [ -x "$f" ] || continue; ' + 'c=$(cksum < "$f" | cut -d" " -f1); ' + f'printf \'[script %s %s %s]\\n\' {period} "$c" "$f"; ' + f"k=$(grep -oE '{_REF}' \"$f\" | sort -u); " + "for r in $k; do printf '|%s\\n' \"$r\"; done; " + 'K="$K $k"; Q="$Q\n$f"; done; ' + ) + + +def _source(defaults: str) -> str: + """``sh`` that reads periodic's configuration as periodic does; prints nothing.""" + d = quote(defaults) + return f"if [ -r {d} ]; then . {d}; source_periodic_confs; fi >/dev/null 2>&1 str: + """``sh`` that prints every periodic script, the knobs it names and their values.""" + parts = ["printf '%s%s\\n' PERIODIC_ BEGIN; K=''; Q=''; "] + parts += [_scan(period, directory) for period, directory in dirs] + parts.append( + f"echo '[conf]'; ( {_source(defaults)}; " + 'for r in $K; do k=${r%%[:-]*}; eval "s=\\${$k+x} v=\\${$k-}"; ' + '[ -n "$s" ] && printf \'|%s=%s\\n\' "$k" "$v"; done; echo \'[read]\' ); ' + f"echo '[owners]'; printf '%s\\n' \"$Q\" | sed '/^$/d' | {{ {owners_sh}; }}; " + "printf '%s%s\\n' PERIODIC_ END" + ) + return "".join(parts) + + +def knob_of(period: str, path: str, refs: list[str]) -> tuple[str | None, bool]: + """The knob that switches the script *path*, and whether it is on when unset.""" + defaults: dict[str, bool] = {} + for ref in refs: + parts = _REF_PARTS.match(ref) + if parts: + on = (parts[2] or "").lower() == "yes" + defaults[parts[1]] = defaults.get(parts[1], False) or on + name = re.sub(r"^\d+\.", "", path.rsplit("/", 1)[-1]).replace("-", "_") + for knob in (f"{period}_{name}_enable", f"{period}_status_{name}_enable"): + if knob in defaults: + return knob, defaults[knob] + own = [k for k in defaults if k.startswith(f"{period}_")] + if len(own) == 1: + return own[0], defaults[own[0]] + return None, False + + +@dataclass +class _Raw: + period: str + checksum: str + path: str + refs: list[str] = field(default_factory=list) + + +@dataclass(frozen=True) +class PeriodicScript: + """A periodic script as a job, and what switching and running it need.""" + + job: dict[str, object] + knob: str | None + period: str + + +@dataclass +class _Report: + scripts: list[_Raw] = field(default_factory=list) + conf: dict[str, str] = field(default_factory=dict) + owned: set[str] = field(default_factory=set) + read: bool = False + + +def _take(report: _Report, section: str, value: str) -> None: + if section == "script" and value: + report.scripts[-1].refs.append(value) + elif section == "conf": + knob, sep, setting = value.partition("=") + if sep: + report.conf[knob] = setting + elif section == "owners": + _package, sep, path = value.partition(": ") + if sep and path.startswith("/"): + report.owned.add(path.strip()) + + +def _open(report: _Report, header: re.Match[str]) -> str: + """The section *header* begins; a script's header names the script.""" + if header[1] == "script": + period, checksum, path = (header[2] or "").split(" ", 2) + report.scripts.append(_Raw(period, checksum, path)) + report.read = report.read or header[1] == "read" + return header[1] + + +def _report(output: str) -> _Report: + text = strip_terminal_codes(output) + begin, end = text.rfind(_BEGIN), text.rfind(_END) + if begin < 0 or end < begin: + raise ValueError("the periodic report is cut short (no end marker)") + report, section = _Report(), "" + for line in text[begin + len(_BEGIN) : end].splitlines(): + header = _SECTION.match(line) + if header is not None: + section = _open(report, header) + elif section: + _take(report, section, line[1:] if line.startswith("|") else line) + if not report.read: + raise ValueError("periodic.conf could not be read") + return report + + +def _job(raw: _Raw, report: _Report) -> PeriodicScript: + knob, default_on = knob_of(raw.period, raw.path, raw.refs) + enabled, actions = True, ["run"] + if knob is not None: + value = report.conf.get(knob, "") + enabled = value.lower() == "yes" if value else default_on + actions = _SWITCH if enabled else ["enable", "disable"] + job: dict[str, object] = { + "key": f"periodic:{raw.path}", + "kind": "periodic", + "source": raw.path, + "run_as": "root", + "schedule": raw.period, + "schedule_kind": "period", + "command": raw.path, + "enabled": enabled, + "managed_by": "package" if raw.path in report.owned else "admin", + "actions": actions, + } + job["digest"] = job_digest(job, content=raw.checksum) + return PeriodicScript(job, knob, raw.period) + + +def parse_periodic(output: str) -> list[PeriodicScript]: + """The periodic scripts of a report from :func:`reading_command`. + + :raises ValueError: when the report is cut short or periodic.conf could not be read. + """ + report = _report(output) + return [_job(raw, report) for raw in report.scripts] + + +def _said(result: CommandResult) -> str: + return "\n".join(p for p in (result.stdout.strip(), result.stderr.strip()) if p) + + +class PeriodicJobsMixin(BsdScheduledJobsMixin): + """Cron and FreeBSD's periodic scripts; see the module's docstring.""" + + PERIODIC_DIRS: ClassVar[tuple[tuple[str, str], ...]] = PERIODIC_DIRS + PERIODIC_DEFAULTS: ClassVar[str] = PERIODIC_DEFAULTS + #: Where enabling and disabling a script writes its knob. + PERIODIC_CONF: ClassVar[str] = PERIODIC_CONF + + def _sj_extra_command(self) -> str: + return reading_command(self.PERIODIC_DIRS, self.PERIODIC_DEFAULTS, self.OWNERS_SH) + + def _sj_extra_jobs(self, output: str) -> list[dict[str, object]]: + return [script.job for script in parse_periodic(output)] + + def manage_scheduled_job(self, key: str, action: str, *, digest: str) -> ScheduledJobResultDict: + """Enable, disable or run *key* now: a periodic script here, anything else as cron.""" + if not key.startswith("periodic:"): + return super().manage_scheduled_job(key, action, digest=digest) + if action not in ACTIONS: + raise ScheduledJobRefused(f"unknown action {action!r}; allowed: {list(ACTIONS)}") + reading, output = self._sj_report() + job = self._sj_job(reading, key, digest) + if action not in job["actions"]: # type: ignore[operator] + raise ScheduledJobRefused(f"{key} cannot be told to {action} on this host") + script = next(s for s in parse_periodic(output) if s.job["key"] == key) + if action == "run": + return self._periodic_run(script) + return self._periodic_switch(script, action == "enable") + + def _periodic_run(self, script: PeriodicScript) -> ScheduledJobResultDict: + # periodic exports what it runs for; ``security`` is spawned by the daily run. + runs_for = "security daily" if script.period == "security" else script.period + argv = [ + "timeout", + str(RUN_TIMEOUT), + "env", + f"PERIODIC={runs_for}", + str(script.job["source"]), + ] + result = self._sj_root(argv, timeout=RUN_TIMEOUT + 30) + said = _said(result) + if result.exit_code != 0: + return {"success": False, "output": said or f"exit {result.exit_code}"} + return {"success": True, "output": said or "ran"} + + def _periodic_switch(self, script: PeriodicScript, enabled: bool) -> ScheduledJobResultDict: + knob = script.knob or "" + if not _KNOB.fullmatch(knob): # pragma: no cover - parse_periodic only takes these + raise ScheduledJobRefused(f"not a periodic knob: {knob!r}") + value = "YES" if enabled else "NO" + command = ( + f"sysrc -f {quote(self.PERIODIC_CONF)} {knob}={value} || exit $?; " + f"( {_source(self.PERIODIC_DEFAULTS)}; printf 'now=%s\\n' \"${{{knob}-}}\" )" + ) + return _switched( + self.run_command(command, privileged=True, timeout=60), + knob, + enabled, + self.PERIODIC_CONF, + ) + + +def _switched(result: CommandResult, knob: str, enabled: bool, conf: str) -> ScheduledJobResultDict: + """What writing *knob* did: sysrc's word, and whether periodic now reads it so.""" + lines = result.stdout.strip().splitlines() + if result.exit_code != 0 or not lines or not lines[-1].startswith("now="): + return {"success": False, "output": _said(result) or f"exit {result.exit_code}"} + said = "\n".join(lines[:-1]).strip() + now = lines[-1].removeprefix("now=") + if (now.lower() == "yes") != enabled: + return { + "success": False, + "output": f"{said}\n{knob} is still {now or 'unset'}: " + f"a periodic.conf read after {conf} sets it".strip(), + } + return {"success": True, "output": said or f"{knob}={'YES' if enabled else 'NO'}"} diff --git a/napalm_bsd/scheduled_jobs.py b/napalm_bsd/scheduled_jobs.py new file mode 100644 index 0000000..c93129a --- /dev/null +++ b/napalm_bsd/scheduled_jobs.py @@ -0,0 +1,104 @@ +"""Scheduled jobs on FreeBSD and OpenBSD: cron with the BSD paths and tools (#27). + +napalm-device-types' ``PosixScheduledJobsMixin`` reads, switches, runs and +writes; this names where the BSDs keep their jobs and what their tools take. +Both read ``/etc/crontab`` and the user crontabs in ``/var/cron/tabs``; neither +has run-parts, anacron or systemd. BSD ``install`` makes no directories (its +``-D`` is a destdir), and BSD ``stat`` takes ``-f``. + +**su has no shell option** on FreeBSD (``-s`` is a MAC label there), and ``-m`` +starts root's own login shell -- csh on many FreeBSD hosts. So the job's script +goes to ``/bin/sh`` on stdin, and the one word ``su`` hands that shell is +``/bin/sh``, which every shell runs alike. ``HOME``, ``USER`` and ``LOGNAME`` are +the user's, as cron sets them. + +A driver may add to the report (:meth:`_sj_extra_command`, FreeBSD's +``periodic``): it runs in the same round trip as root, after the cron report. +""" + +from __future__ import annotations + +import fnmatch +from dataclasses import replace +from typing import ClassVar + +from napalm_device_types import PosixScheduledJobsMixin +from napalm_device_types._scheduled_reading import ( + Paths, + Reading, + parse_reading, + reading_command, +) + +_READ_TIMEOUT = 120 + +#: Runs ``$2`` with ``/bin/sh`` as the user ``$1``, from root, the way cron would. +_SU_SH = ( + "h=$(awk -F: -v u=\"$1\" '$1 == u { print $6; exit }' /etc/passwd); " + "{ echo 'SHELL=/bin/sh; export SHELL'; printf '%s\\n' \"$2\"; } | " + 'HOME="${h:-/}" USER="$1" LOGNAME="$1" su -m "$1" -c /bin/sh' +) + + +class BsdScheduledJobsMixin(PosixScheduledJobsMixin): + """Cron on a BSD host; the concrete driver names its cron.d and its tools.""" + + CRON_SYSTEM_FILES: ClassVar[tuple[str, ...]] = ("/etc/crontab",) + CRON_D_DIRS: ClassVar[tuple[str, ...]] = () + CRON_SPOOL_DIRS: ClassVar[tuple[str, ...]] = ("/var/cron/tabs",) + RUN_PARTS_DIRS: ClassVar[tuple[tuple[str, str], ...]] = () + ANACRONTAB: ClassVar[str] = "" + OWN_CRON_D_DIR: ClassVar[str | None] = None + ROOT_GROUP: ClassVar[str] = "wheel" + STAT_ARGV: ClassVar[tuple[str, ...]] = ("stat", "-f", "%Lp %Su %Sg", "--") + INSTALL: ClassVar[tuple[str, ...]] = ("install",) + OWNERS_SH: ClassVar[str] = "cat >/dev/null" + TIME_ZONE_SH: ClassVar[str] = "readlink /etc/localtime 2>/dev/null" + #: Commands the base system puts in a crontab: those lines are ``platform``. + PLATFORM_COMMANDS: ClassVar[tuple[str, ...]] = () + + def _sj_paths(self) -> Paths: + # No BSD runs systemd: never look for its timers, never write one. + return replace(super()._sj_paths(), systemd_marker="") + + def _sj_extra_command(self) -> str: + """``sh`` run after the cron report, in the same round trip; "" for none.""" + return "" + + def _sj_extra_jobs(self, output: str) -> list[dict[str, object]]: + """The jobs :meth:`_sj_extra_command` printed. + + :raises ValueError: when its part of the report is cut short. + """ + return [] + + def _sj_platform(self, job: dict[str, object]) -> bool: + if job["managed_by"] == "application": + return False + source = str(job["source"]) + return any(fnmatch.fnmatchcase(source, p) for p in self.PLATFORM_SOURCES) or ( + job["kind"] == "cron" and job["command"] in self.PLATFORM_COMMANDS + ) + + def _sj_report(self) -> tuple[Reading, str]: + """The reading and the report it was parsed from, in one round trip as root.""" + command = reading_command(self._sj_paths()) + extra = self._sj_extra_command() + if extra: + command = f"{command}; {extra}" + output = self.run_command(command, privileged=True, timeout=_READ_TIMEOUT).stdout + reading = parse_reading(output) + reading.jobs += self._sj_extra_jobs(output) + for job in reading.jobs: + if self._sj_platform(job): + job["managed_by"] = "platform" + return reading, output + + def _sj_read(self) -> Reading: + return self._sj_report()[0] + + def _sj_as(self, user: str, script: str) -> list[str]: + """*script* run by ``sh`` as *user*, from root (see the module's docstring).""" + if user == "root": + return ["sh", "-c", script] + return ["sh", "-c", _SU_SH, "sh", user, script] diff --git a/pyproject.toml b/pyproject.toml index f19ef8b..9f8d256 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ classifiers = [ ] dependencies = [ "napalm>=5.1", # older releases import pkg_resources, which a 3.12 venv lacks - "napalm-device-types>=4.23.0", + "napalm-device-types>=4.27.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_drivers.py b/tests/test_drivers.py index 1b2fd88..df90d30 100644 --- a/tests/test_drivers.py +++ b/tests/test_drivers.py @@ -50,8 +50,6 @@ def _freebsd_host(): "cat /etc/passwd": _read(s, "cat_etc_passwd.txt"), "cat /etc/group": _read(s, "cat_etc_group.txt"), BsdDriver.PS_COMMAND: _read(s, PS), - "cat /etc/crontab": _read(s, "cat_etc_crontab.txt"), - "crontab -l": "0 4 * * * /home/netork/backup.sh\n", }, ) @@ -98,7 +96,6 @@ class TestIdentity: "get_lldp_neighbors", "get_os_users", "get_processes", - "get_cron_jobs", ], ) def test_implements_what_the_poll_reads(self, cls, getter): @@ -198,15 +195,6 @@ class TestAccountsAndProcesses: init = next(p for p in _openbsd_vm().get_processes() if p["pid"] == 1) assert init["command"] == "/sbin/init" - def test_cron_jobs_from_the_system_and_the_login_users_crontab(self): - jobs = _freebsd_host().get_cron_jobs() - assert {"user": "root", "schedule": "0 * * * *", "command": "newsyslog"} in jobs - assert { - "user": "netork", - "schedule": "0 4 * * *", - "command": "/home/netork/backup.sh", - } in jobs - class TestListeningSockets: def test_freebsd_as_root_is_attributed(self): diff --git a/tests/test_parse.py b/tests/test_parse.py index f3d9063..346c64f 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -179,29 +179,6 @@ class TestProcesses: } -class TestCrontab: - def test_system_crontab_names_the_user(self): - jobs = parse.crontab(fixture("freebsd-host", "cat_etc_crontab.txt"), system=True) - assert {"user": "root", "schedule": "0 * * * *", "command": "newsyslog"} in jobs - assert { - "user": "operator", - "schedule": "*/11 * * * *", - "command": "/usr/libexec/save-entropy", - } in jobs - - def test_user_crontab(self): - jobs = parse.crontab(fixture("openbsd-vm", "crontab_l.txt"), user="root") - assert jobs - assert all(j["user"] == "root" for j in jobs) - assert all(len(j["schedule"].split()) == 5 for j in jobs) - - def test_special_schedules_and_environment_lines(self): - text = "SHELL=/bin/sh\n@reboot root /usr/local/bin/start\n# comment\n" - assert parse.crontab(text, system=True) == [ - {"user": "root", "schedule": "@reboot", "command": "/usr/local/bin/start"} - ] - - class TestBoottime: def test_freebsd(self): assert parse.boottime(fixture("freebsd-host", "sysctl_n_kern.boottime.txt")) == 1791405216 diff --git a/tests/test_scheduled_jobs.py b/tests/test_scheduled_jobs.py new file mode 100644 index 0000000..0026aa6 --- /dev/null +++ b/tests/test_scheduled_jobs.py @@ -0,0 +1,594 @@ +"""Scheduled jobs on FreeBSD and OpenBSD: cron with the BSD paths, FreeBSD's periodic (#27). + +The report is napalm-device-types' ``PosixScheduledJobsMixin``'s, with the +periodic scan behind it in the same round trip. Both are run by a real ``sh`` +over a directory tree that stands in for the host, with ``crontab``, ``install``, +``stat``, ``su``, ``pkg`` and ``sysrc`` replaced by scripts that keep a log -- so +the shell itself is what is tested, not a hand-written copy of its output. +""" + +from __future__ import annotations + +import os +import subprocess +from pathlib import Path + +import pytest +from napalm_device_types import CommandResult, PosixScheduledJobsMixin +from napalm_device_types.scheduled_jobs import ScheduledJobChanged, ScheduledJobRefused + +from napalm_bsd import FreeBSDDriver, OpenBSDDriver, parse +from napalm_bsd import periodic as periodic_module +from napalm_bsd.base import BsdDriver +from napalm_bsd.periodic import PERIODIC_DIRS + +CRONTAB = """#!/bin/sh +echo "crontab $*" >> "$LOG" +[ "$1" = -u ] || exit 2 +if [ "$3" = -l ]; then + [ -f "$SPOOL/$2" ] || { echo "crontab: no crontab for $2" >&2; exit 1; } + cat "$SPOOL/$2" +else + cp "$3" "$SPOOL/$2" +fi +""" + +# BSD install has no -D here; owner and group are root's to set. +INSTALL = """#!/bin/sh +echo "install $*" >> "$LOG" +args="" +while [ $# -gt 0 ]; do case "$1" in -o|-g) shift 2 ;; -D) exit 64 ;; *) args="$args '$1'"; shift ;; esac; done +eval "/usr/bin/install $args" +""" + +# BSD stat: -f ; GNU stat answers for it. +STAT = """#!/bin/sh +echo "stat $*" >> "$LOG" +[ "$1" = -f ] && [ "$2" = "%Lp %Su %Sg" ] && [ "$3" = -- ] || exit 64 +exec /usr/bin/stat -c '%a %U %G' -- "$4" +""" + +# BSD su: -m keeps the environment and starts the caller's shell with what follows the login. +SU = """#!/bin/sh +echo "su $* HOME=$HOME USER=$USER LOGNAME=$LOGNAME" >> "$LOG" +[ "$1" = -m ] && [ "$3" = -c ] || exit 64 +exec $4 +""" + +PKG = """#!/bin/sh +case "$1" in + -N) exit 0 ;; + which) [ "$2" = -q ] && [ "$3" = -- ] || exit 64 + case " $OWNED " in *" $4 "*) echo "pkg-2.1.2" ;; *) exit 1 ;; esac ;; +esac +""" + +# sysrc -f =: replace the knob's line, or add one. +SYSRC = """#!/bin/sh +echo "sysrc $*" >> "$LOG" +[ "$1" = -f ] || exit 64 +f=$2; k=${3%%=*}; v=${3#*=} +touch "$f" +old=$(sed -n "s/^$k=\\"*\\([^\\"]*\\)\\"*/\\1/p" "$f") +grep -v "^$k=" "$f" > "$f.new"; echo "$k=\\"$v\\"" >> "$f.new"; mv "$f.new" "$f" +echo "$k: ${old:-} -> $v" +""" + +DEFAULTS = """\ +periodic_conf_files="ROOT/etc/periodic.conf ROOT/etc/periodic.conf.local" +source_periodic_confs() { + for i in $periodic_conf_files; do [ -r "$i" ] && . "$i"; done +} +daily_clean_disks_enable="NO" +daily_status_disks_enable="YES" +security_status_chksetuid_enable="YES" +""" + +SCRIPT = """#!/bin/sh +if [ -r ROOT/etc/defaults/periodic.conf ]; then + . ROOT/etc/defaults/periodic.conf + source_periodic_confs +fi +case "$KNOB" in + [Yy][Ee][Ss]) echo "WHAT ran (PERIODIC=$PERIODIC)" ;; +esac +""" + + +def _recorded(system: str, name: str) -> str: + return (Path(__file__).parent / "fixtures" / system / name).read_text() + + +def _tool(root: Path, name: str, body: str) -> None: + path = root / "bin" / name + path.write_text(body) + path.chmod(0o755) + + +def _script(root: Path, name: str, knob: str, *, what: str = "", mode: int = 0o755) -> None: + path = root / name + body = SCRIPT.replace("ROOT", str(root)).replace("KNOB", knob) + path.write_text(body.replace("WHAT", what or path.name)) + path.chmod(mode) + + +class _Host: + """What both test hosts share: every command run by sh, in a directory.""" + + root: Path + commands: list[str] + + def _setup(self, root: Path) -> None: + self.root = root + self.commands = [] + group = subprocess.run(["id", "-gn"], capture_output=True, text=True).stdout.strip() + self.ROOT_GROUP = group # type: ignore[misc] + self.CRON_SYSTEM_FILES = (str(root / "etc/crontab"),) # type: ignore[misc] + self.CRON_SPOOL_DIRS = (str(root / "var/cron/tabs"),) # type: ignore[misc] + + def run_command(self, command, *, privileged=False, timeout=60, stdin=None): # type: ignore[no-untyped-def] + self.commands.append(command) + env = { + **os.environ, + "PATH": f"{self.root / 'bin'}:{os.environ['PATH']}", + "LOG": str(self.root / "log"), + "SPOOL": str(self.root / "var/cron/tabs"), + "OWNED": str(self.root / "usr/local/etc/periodic/daily/411.pkg-backup"), + "HOME": "/root", + } + done = subprocess.run( + ["sh", "-c", command], input=stdin, capture_output=True, env=env, timeout=30 + ) + return CommandResult(done.stdout.decode(), done.stderr.decode(), done.returncode) + + def log(self) -> str: + log = self.root / "log" + return log.read_text() if log.exists() else "" + + +class FreeBSDHost(_Host, FreeBSDDriver): + def __init__(self, root: Path) -> None: + FreeBSDDriver.__init__(self, "host", "admin", "") + self._setup(root) + etc = root / "etc" + self.CRON_D_DIRS = (str(etc / "cron.d"), str(root / "usr/local/etc/cron.d")) # type: ignore[misc] + self.OWN_CRON_D_DIR = str(etc / "cron.d") # type: ignore[misc] + self.PERIODIC_DIRS = tuple( # type: ignore[misc] + (period, f"{root}{directory}") for period, directory in PERIODIC_DIRS + ) + self.PERIODIC_DEFAULTS = str(etc / "defaults/periodic.conf") # type: ignore[misc] + self.PERIODIC_CONF = str(etc / "periodic.conf") # type: ignore[misc] + self.PLATFORM_SOURCES = (str(etc / "crontab"), f"{etc}/periodic/*") # type: ignore[misc] + + +class OpenBSDHost(_Host, OpenBSDDriver): + def __init__(self, root: Path) -> None: + OpenBSDDriver.__init__(self, "host", "admin", "") + self._setup(root) + + +@pytest.fixture +def root(tmp_path: Path) -> Path: + for d in ( + "etc/cron.d", + "etc/defaults", + "usr/local/etc/cron.d", + "var/cron/tabs", + "bin", + *(f"etc/periodic/{p}" for p in ("daily", "weekly", "monthly", "security")), + "usr/local/etc/periodic/daily", + ): + (tmp_path / d).mkdir(parents=True) + (tmp_path / "etc/crontab").write_text(_recorded("freebsd-host", "cat_etc_crontab.txt")) + (tmp_path / "usr/local/etc/cron.d/certbot").write_text("0 */12 * * * root certbot -q renew\n") + (tmp_path / "var/cron/tabs/www").write_text( + f'*/5 * * * * echo "$HOME $USER" > {tmp_path}/ran\n' + ) + (tmp_path / "etc/defaults/periodic.conf").write_text(DEFAULTS.replace("ROOT", str(tmp_path))) + (tmp_path / "etc/periodic.conf").write_text('daily_clean_disks_enable="YES"\n') + for name, knob in ( + ("daily/100.clean-disks", "daily_clean_disks_enable"), + ("daily/400.status-disks", "daily_status_disks_enable"), + ("daily/480.leapfile-ntpd", "daily_ntpd_leapfile_enable"), + ("daily/999.local", "daily_local"), + ("security/100.chksetuid", "security_status_chksetuid_enable"), + ): + _script(tmp_path, f"etc/periodic/{name}", knob) + _script(tmp_path, "etc/periodic/security/security.functions", "nothing", mode=0o444) + _script( + tmp_path, + "usr/local/etc/periodic/daily/411.pkg-backup", + "{daily_backup_pkgdb_enable:-YES}", + ) + for name, body in ( + ("crontab", CRONTAB), + ("install", INSTALL), + ("stat", STAT), + ("su", SU), + ("pkg", PKG), + ("sysrc", SYSRC), + ): + _tool(tmp_path, name, body) + return tmp_path + + +def _jobs(host: PosixScheduledJobsMixin) -> dict[str, dict]: + return {j["key"]: j for j in host.get_scheduled_jobs()["jobs"]} + + +def _periodic(root: Path, path: str) -> str: + return f"periodic:{root}{path}" + + +class TestTheBsdPaths: + @pytest.mark.parametrize("cls", [FreeBSDDriver, OpenBSDDriver]) + def test_cron_and_nothing_of_linux(self, cls): + assert issubclass(cls, PosixScheduledJobsMixin) + assert cls.CRON_SYSTEM_FILES == ("/etc/crontab",) + assert cls.CRON_SPOOL_DIRS == ("/var/cron/tabs",) + assert cls.RUN_PARTS_DIRS == () + assert cls.ANACRONTAB == "" + assert cls.ROOT_GROUP == "wheel" + assert cls.STAT_ARGV == ("stat", "-f", "%Lp %Su %Sg", "--") + assert cls.INSTALL == ("install",) + # No BSD runs systemd, even where a directory of that name turns up. + assert cls("h", "u", "")._sj_paths().systemd_marker == "" + + def test_freebsd_reads_both_cron_d_and_writes_to_the_base_one(self): + assert FreeBSDDriver.CRON_D_DIRS == ("/etc/cron.d", "/usr/local/etc/cron.d") + assert FreeBSDDriver.OWN_CRON_D_DIR == "/etc/cron.d" + assert FreeBSDDriver.PLATFORM_SOURCES == ("/etc/crontab", "/etc/periodic/*") + + def test_openbsd_has_no_cron_d(self): + assert OpenBSDDriver.CRON_D_DIRS == () + assert OpenBSDDriver.OWN_CRON_D_DIR is None + + def test_the_old_getter_is_gone(self): + assert "get_cron_jobs" not in vars(BsdDriver) + assert not hasattr(parse, "crontab") + + +class TestFreeBSDReading: + def test_one_round_trip_as_root(self, root: Path): + host = FreeBSDHost(root) + host.get_scheduled_jobs() + assert len(host.commands) == 1 + + def test_cron_from_every_bsd_place(self, root: Path): + jobs = _jobs(FreeBSDHost(root)) + cron = {(j["source"], j["run_as"]) for j in jobs.values() if j["kind"] == "cron"} + assert cron == { + (str(root / "etc/crontab"), "operator"), + (str(root / "etc/crontab"), "root"), + (str(root / "usr/local/etc/cron.d/certbot"), "root"), + (str(root / "var/cron/tabs/www"), "www"), + } + + def test_the_base_crontab_is_the_platforms(self, root: Path): + jobs = _jobs(FreeBSDHost(root)).values() + by_source = {j["source"]: j["managed_by"] for j in jobs if j["kind"] == "cron"} + assert by_source[str(root / "etc/crontab")] == "platform" + assert by_source[str(root / "usr/local/etc/cron.d/certbot")] == "admin" + + def test_every_executable_periodic_script_is_a_job(self, root: Path): + jobs = _jobs(FreeBSDHost(root)) + periodic = sorted(k for k, j in jobs.items() if j["kind"] == "periodic") + assert periodic == sorted( + _periodic(root, p) + for p in ( + "/etc/periodic/daily/100.clean-disks", + "/etc/periodic/daily/400.status-disks", + "/etc/periodic/daily/480.leapfile-ntpd", + "/etc/periodic/daily/999.local", + "/etc/periodic/security/100.chksetuid", + "/usr/local/etc/periodic/daily/411.pkg-backup", + ) + ) + + def test_a_periodic_job(self, root: Path): + job = _jobs(FreeBSDHost(root))[_periodic(root, "/etc/periodic/daily/100.clean-disks")] + path = f"{root}/etc/periodic/daily/100.clean-disks" + assert { + k: job[k] for k in ("source", "run_as", "schedule", "schedule_kind", "command") + } == { + "source": path, + "run_as": "root", + "schedule": "daily", + "schedule_kind": "period", + "command": path, + } + assert job["managed_by"] == "platform" + assert job["actions"] == ["enable", "disable", "run"] + + def test_enabled_is_its_knob_after_the_overrides(self, root: Path): + jobs = _jobs(FreeBSDHost(root)) + # NO in the defaults, YES in /etc/periodic.conf. + assert jobs[_periodic(root, "/etc/periodic/daily/100.clean-disks")]["enabled"] + assert jobs[_periodic(root, "/etc/periodic/daily/400.status-disks")]["enabled"] + # security_status__enable, as the security scripts name it. + assert ( + jobs[_periodic(root, "/etc/periodic/security/100.chksetuid")]["schedule"] == "security" + ) + assert jobs[_periodic(root, "/etc/periodic/security/100.chksetuid")]["enabled"] + + def test_a_knob_that_is_off_cannot_run(self, root: Path): + (root / "etc/periodic.conf").write_text("daily_status_disks_enable=NO\n") + job = _jobs(FreeBSDHost(root))[_periodic(root, "/etc/periodic/daily/400.status-disks")] + assert not job["enabled"] + # The script itself would do nothing. + assert job["actions"] == ["enable", "disable"] + + def test_the_knob_the_script_names_when_it_is_not_its_name(self, root: Path): + job = _jobs(FreeBSDHost(root))[_periodic(root, "/etc/periodic/daily/480.leapfile-ntpd")] + assert job["actions"] == ["enable", "disable"] # daily_ntpd_leapfile_enable is unset + + def test_a_script_without_a_knob_only_runs(self, root: Path): + job = _jobs(FreeBSDHost(root))[_periodic(root, "/etc/periodic/daily/999.local")] + assert job["actions"] == ["run"] + assert job["enabled"] + + def test_a_scripts_own_default(self, root: Path): + job = _jobs(FreeBSDHost(root))[ + _periodic(root, "/usr/local/etc/periodic/daily/411.pkg-backup") + ] + assert job["enabled"] # ${daily_backup_pkgdb_enable:-YES}, set nowhere + assert job["managed_by"] == "package" + + def test_a_changed_script_or_knob_changes_the_digest(self, root: Path): + host = FreeBSDHost(root) + key = _periodic(root, "/etc/periodic/daily/100.clean-disks") + before = _jobs(host)[key]["digest"] + (root / "etc/periodic.conf").write_text("daily_clean_disks_enable=NO\n") + off = _jobs(host)[key]["digest"] + assert off != before + _script(root, "etc/periodic/daily/100.clean-disks", "daily_clean_disks_enable", what="x") + assert _jobs(host)[key]["digest"] != off + + def test_the_time_zone(self, root: Path): + (root / "zoneinfo").write_text("Europe/Berlin\n") + host = FreeBSDHost(root) + host.TIME_ZONE_SH = FreeBSDDriver.TIME_ZONE_SH.replace( # type: ignore[misc] + "/var/db/zoneinfo", str(root / "zoneinfo") + ) + assert host.get_scheduled_jobs()["time_zone"] == "Europe/Berlin" + + def test_without_pkg_nothing_is_a_packages(self, root: Path): + _tool(root, "pkg", "#!/bin/sh\necho 'pkg is not installed' >&2\nexit 1\n") + jobs = _jobs(FreeBSDHost(root)) + assert "package" not in {j["managed_by"] for j in jobs.values()} + + def test_a_periodic_conf_that_stops_the_shell_raises(self, root: Path): + (root / "etc/periodic.conf").write_text("exit 1\n") + with pytest.raises(ValueError, match="periodic.conf could not be read"): + FreeBSDHost(root).get_scheduled_jobs() + + def test_a_periodic_report_cut_short_raises(self, root: Path): + host = FreeBSDHost(root) + full = host.run_command + + def cut(command, **kw): # type: ignore[no-untyped-def] + done = full(command, **kw) + return done._replace(stdout=done.stdout.split("PERIODIC_END")[0]) + + host.run_command = cut # type: ignore[method-assign] + with pytest.raises(ValueError, match="cut short"): + host.get_scheduled_jobs() + + +class TestFreeBSDPeriodicActions: + def _job(self, host: FreeBSDHost, path: str) -> dict: + return _jobs(host)[_periodic(host.root, path)] + + def test_disable_and_enable_write_periodic_conf(self, root: Path): + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/100.clean-disks") + result = host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + assert result["success"], result + assert f"sysrc -f {root}/etc/periodic.conf daily_clean_disks_enable=NO" in host.log() + off = self._job(host, "/etc/periodic/daily/100.clean-disks") + assert not off["enabled"] + host.manage_scheduled_job(job["key"], "enable", digest=off["digest"]) + assert self._job(host, "/etc/periodic/daily/100.clean-disks")["enabled"] + + def test_a_later_periodic_conf_that_wins_is_said(self, root: Path): + (root / "etc/periodic.conf.local").write_text("daily_clean_disks_enable=YES\n") + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/100.clean-disks") + result = host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + assert not result["success"] + assert "daily_clean_disks_enable" in result["output"] and "YES" in result["output"] + + def test_a_failed_sysrc_says_so(self, root: Path): + _tool(root, "sysrc", "#!/bin/sh\necho 'sysrc: read-only file system' >&2\nexit 1\n") + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/100.clean-disks") + result = host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + assert result == {"success": False, "output": "sysrc: read-only file system"} + + def test_a_script_that_fails_says_so(self, root: Path): + (root / "etc/periodic/daily/999.local").write_text("#!/bin/sh\nexit 3\n") + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/999.local") + result = host.manage_scheduled_job(job["key"], "run", digest=job["digest"]) + assert result == {"success": False, "output": "exit 3"} + + def test_a_cron_job_on_freebsd_is_still_crons(self, root: Path): + host = FreeBSDHost(root) + job = next(j for j in _jobs(host).values() if j["command"] == "newsyslog") + result = host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + assert result["success"] + assert ( + "#napalm-disabled: 0\t*\t*\t*\t*\troot\tnewsyslog" in (root / "etc/crontab").read_text() + ) + + def test_run_runs_the_script_as_periodic_does(self, root: Path): + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/100.clean-disks") + result = host.manage_scheduled_job(job["key"], "run", digest=job["digest"]) + assert result == {"success": True, "output": "100.clean-disks ran (PERIODIC=daily)"} + assert host.commands[-1].startswith("timeout 300 env PERIODIC=daily ") + + def test_security_runs_as_the_daily_run_spawns_it(self, root: Path): + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/security/100.chksetuid") + result = host.manage_scheduled_job(job["key"], "run", digest=job["digest"]) + assert result["output"] == "100.chksetuid ran (PERIODIC=security daily)" + + def test_a_script_without_a_knob_is_not_switched(self, root: Path): + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/999.local") + with pytest.raises(ScheduledJobRefused): + host.manage_scheduled_job(job["key"], "disable", digest=job["digest"]) + + def test_a_change_it_did_not_see_and_an_unknown_action(self, root: Path): + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/100.clean-disks") + with pytest.raises(ScheduledJobChanged): + host.manage_scheduled_job(job["key"], "disable", digest="0" * 16) + with pytest.raises(ScheduledJobRefused): + host.manage_scheduled_job(job["key"], "explode", digest=job["digest"]) + with pytest.raises(LookupError): + host.manage_scheduled_job("periodic:/nowhere", "run", digest="x") + + def test_a_periodic_script_is_never_deleted(self, root: Path): + host = FreeBSDHost(root) + job = self._job(host, "/etc/periodic/daily/100.clean-disks") + with pytest.raises(ScheduledJobRefused): + host.delete_scheduled_job(job["key"], digest=job["digest"]) + + +class TestFreeBSDCron: + def _cron(self, host: _Host, path: str) -> dict: + return next( + j + for j in _jobs(host).values() + if j["source"] == path and j["kind"] == "cron" # type: ignore[arg-type] + ) + + def test_a_cron_d_line_switched_through_bsd_stat_and_install(self, root: Path): + host = FreeBSDHost(root) + path = root / "usr/local/etc/cron.d/certbot" + job = self._cron(host, str(path)) + mode = os.stat(path).st_mode & 0o777 + assert host.manage_scheduled_job(job["key"], "disable", digest=job["digest"])["success"] + assert path.read_text() == "#napalm-disabled: 0 */12 * * * root certbot -q renew\n" + assert f"stat -f %Lp %Su %Sg -- {path}" in host.log() + assert f"install -m {mode:o} -o " in host.log() + assert os.stat(path).st_mode & 0o777 == mode + + def test_another_users_line_runs_through_su_as_cron_would(self, root: Path): + host = FreeBSDHost(root) + job = self._cron(host, str(root / "var/cron/tabs/www")) + result = host.manage_scheduled_job(job["key"], "run", digest=job["digest"]) + assert result["success"], result + # HOME is the user's, from the password file; USER and LOGNAME are the user. + home = subprocess.run( + ["awk", "-F:", '$1 == "www" { print $6 }', "/etc/passwd"], + capture_output=True, + text=True, + ).stdout.strip() + assert (root / "ran").read_text() == f"{home or '/'} www\n" + assert "su -m www -c /bin/sh" in host.log() + assert host.commands[-1].startswith("timeout 300 sh -c ") + + def test_an_applications_job_is_a_file_in_etc_cron_d(self, root: Path): + host = FreeBSDHost(root) + spec = { + "name": "tidy", + "schedule": "*/30 1-2 * * *", + "run_as": "root", + "command": "find /tmp -mtime +7 -delete", + "enabled": True, + "description": "tidy tmp", + } + host.put_scheduled_job(spec) # type: ignore[arg-type] + assert (root / "etc/cron.d/tidy").read_text() == ( + "# napalm-scheduled-job: tidy tidy tmp\n*/30 1-2 * * * root find /tmp -mtime +7 -delete\n" + ) + job = _jobs(host)["job:tidy"] + assert job["managed_by"] == "application" + host.delete_scheduled_job("job:tidy", digest=job["digest"]) + assert not (root / "etc/cron.d/tidy").exists() + + +class TestOpenBSD: + @pytest.fixture + def host(self, root: Path) -> OpenBSDHost: + # root's crontab as OpenBSD 7.9 ships it, and a line with cron's flags. + tab = _recorded("openbsd-vm", "crontab_l.txt") + flagged = f"~\t*\t*\t*\t*\t-ns echo flagged > {root / 'ran'}\n" + (root / "var/cron/tabs/root").write_text(tab + flagged) + return OpenBSDHost(root) + + def test_the_maintenance_scripts_are_roots_crontab_lines_once(self, host: OpenBSDHost): + jobs = list(_jobs(host).values()) + assert {j["kind"] for j in jobs} == {"cron"} + daily = [j for j in jobs if "/etc/daily" in j["command"]] + assert len(daily) == 1 + assert (daily[0]["schedule"], daily[0]["run_as"]) == ("30 1 * * *", "root") + + def test_the_base_systems_lines_are_the_platforms(self, host: OpenBSDHost): + managed = {j["command"]: j["managed_by"] for j in _jobs(host).values()} + for command in ("/usr/bin/newsyslog", "/bin/sh /etc/daily", "/bin/sh /etc/monthly"): + assert managed[command] == "platform" + assert managed[f"-ns echo flagged > {host.root}/ran"] == "admin" + assert managed[f'echo "$HOME $USER" > {host.root}/ran'] == "admin" + + def test_a_line_with_cron_flags_runs_without_them(self, host: OpenBSDHost): + job = next(j for j in _jobs(host).values() if "flagged" in j["command"]) + assert job["schedule"] == "~ * * * *" + result = host.manage_scheduled_job(job["key"], "run", digest=job["digest"]) + assert result["success"], result + assert (host.root / "ran").read_text() == "flagged\n" + + def test_no_periodic_and_the_time_zone_from_localtime(self, host: OpenBSDHost): + assert OpenBSDDriver.TIME_ZONE_SH == "readlink /etc/localtime 2>/dev/null" + host.get_scheduled_jobs() + assert "PERIODIC_" not in host.commands[0] + + def test_an_applications_job_is_marked_lines_in_the_users_crontab(self, host: OpenBSDHost): + spec = { + "name": "tidy", + "schedule": "0 4 * * *", + "run_as": "nobody", + "command": "true", + "enabled": False, + } + host.put_scheduled_job(spec) # type: ignore[arg-type] + assert (host.root / "var/cron/tabs/nobody").read_text() == ( + "# napalm-scheduled-job: tidy\n#napalm-disabled: 0 4 * * * true\n" + ) + assert "crontab -u nobody /" in host.log() + + +class TestSu: + def test_root_runs_its_script_directly(self): + assert FreeBSDDriver("h", "u", "")._sj_as("root", "true") == ["sh", "-c", "true"] + + def test_su_has_no_shell_option_so_the_script_goes_to_sh_on_stdin(self): + argv = FreeBSDDriver("h", "u", "")._sj_as("www", "echo hi") + assert argv[:2] == ["sh", "-c"] + assert argv[3:] == ["sh", "www", "echo hi"] + assert "su -m " in argv[2] and "-s" not in argv[2].split() + + +class TestPeriodicParse: + def test_the_knob_of_a_script(self): + knob = periodic_module.knob_of + assert knob( + "daily", "/etc/periodic/daily/100.clean-disks", ["daily_clean_disks_enable"] + ) == ( + "daily_clean_disks_enable", + False, + ) + assert knob("security", "/x/100.chksetuid", ["security_status_chksetuid_enable"]) == ( + "security_status_chksetuid_enable", + False, + ) + assert knob("daily", "/x/411.pkg-backup", ["daily_backup_pkgdb_enable:-YES"]) == ( + "daily_backup_pkgdb_enable", + True, + ) + # Two knobs, neither the script's name: which one switches it is not known. + assert knob("daily", "/x/500.y", ["daily_a_enable", "daily_b_enable"]) == (None, False) + assert knob("daily", "/x/999.local", []) == (None, False)