Files
napalm-bsd/napalm_bsd/openbsd.py
T
Christian Manivong 991e07043f
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 13s
driver / test (3.12) (pull_request) Failing after 15s
driver / test (3.13) (pull_request) Failing after 13s
driver / types (pull_request) Failing after 25s
driver / lowest (pull_request) Failing after 19s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Failing after 36s
CI / driver (pull_request) Failing after 1m10s
driver / publish (pull_request) Skipped
feat(scheduled-jobs): FreeBSD and OpenBSD through PosixScheduledJobsMixin (#27)
get_scheduled_jobs reads /etc/crontab and the user crontabs in
/var/cron/tabs, on FreeBSD also both cron.d directories and every
periodic script, in one round trip as root; manage_scheduled_job
enables, disables or runs one now; put_ and delete_scheduled_job write
and remove an application's own job (/etc/cron.d on FreeBSD, marked
lines in the user's crontab on OpenBSD).

A periodic script is what FreeBSD runs daily, weekly, monthly and for
the security run; it does its work only when its periodic.conf knob
says YES, so that knob is its enabled state, written with sysrc to
/etc/periodic.conf. A later periodic.conf that still overrides it is
reported instead of claimed as done. A script whose knob is not known
only runs; one whose knob is off does nothing when run and offers no run.

BSD su takes no shell (FreeBSD's -s is a MAC label) and -m starts
root's own shell, csh on many FreeBSD hosts, so another user's job goes
to /bin/sh on stdin. OpenBSD cron's -n/-q/-s flags are left off when a
line runs now. What the base system ships is platform: FreeBSD's
/etc/crontab and /etc/periodic, OpenBSD's newsyslog and
/etc/daily|weekly|monthly lines, which stay one job each.

get_cron_jobs goes: it read without root and missed cron.d, every other
user and periodic.

Requires napalm-device-types 4.26.0.

Closes #27
2026-11-03 05:53:01 +01:00

117 lines
4.7 KiB
Python

"""OpenBSD: hw.* sysctls for the hardware, uname for release and kernel."""
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."""
TYPE_LABEL = "OpenBSD"
VENDOR = "OpenBSD"
DRIVER_NAME = "openbsd"
# OpenBSD's SSH banner names no OS; its sysDescr does ("OpenBSD host 7.9 GENERIC.MP#449").
SNMP_FINGERPRINT = [FingerprintRule("openbsd", weight=5.0)]
# net-snmp maps OpenBSD's CPU states wrongly: ssCpuIdle is 0 on an idle host, and
# hrProcessorLoad 100. No figure beats one that says the host is always busy (#7).
SNMP_HEALTH_PROFILE: ClassVar[SnmpHealthProfile] = {"cpu": {"unknown": True}}
OS_VERSION_COMMAND = "uname -sr"
# The kernel's build, e.g. GENERIC.MP#449; the release is in uname -r.
KERNEL_COMMAND = "uname -v"
# A sysctl node the machine lacks fails on its own and prints nothing.
PLATFORM_COMMAND = (
"for k in vendor product serialno; do "
'printf "%s=%s\\n" "$k" "$(sysctl -n hw.$k 2>/dev/null)"; done'
)
# fstat shows a user only their own processes; netstat shows every socket, unnamed.
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)
INSTALL_COMMAND = "pkg_add -I {name}"
UNINSTALL_COMMAND = "pkg_delete {name}"
# rcctl check needs no root; "rcctl ls started" does.
SERVICE_STATUS_COMMAND = (
"for s in $(rcctl ls on); do "
"if rcctl check $s >/dev/null 2>&1; then r=1; else r=0; fi; "
'printf "%s\\t%s\\n" "$s" "$r"; done'
)
SERVICE_ACTION_COMMAND = "rcctl {action} {name}"
SNMPD_CONF = "/etc/snmp/snmpd.conf"
# net-snmp's rc script; "snmpd" is OpenBSD's own daemon.
SNMPD_START = "rcctl enable netsnmpd && rcctl restart netsnmpd"
def _parse_services(self, output: str) -> list[dict]:
return parse.rcctl_check(output)
def get_services(self) -> list[dict]:
return self._parse_services(self._out(self.SERVICE_STATUS_COMMAND, timeout=120))
def get_packages(self) -> list[dict]:
return parse.pkg_info(self._out("pkg_info"))
def get_available_updates(self) -> list[dict]:
"""Package updates from ``pkg_add -u -n -v``, and the base system's syspatches.
syspatch applies its patches together and in order, so they are one
entry (#799). Neither tool says which update is a security fix.
"""
updates: list[dict] = [
{**candidate, "origin": None, "security": None}
for candidate in parse.pkg_add_candidates(
self._read("pkg_add -u -n -v", privileged=True, timeout=300).stdout
)
]
patches = parse.syspatch(self._read("syspatch -c", privileged=True, timeout=120).stdout)
if patches:
installed = parse.syspatch(self._out("syspatch -l", privileged=True))
summary = (
", ".join(patches)
if len(patches) <= 3
else f"{len(patches)} patches: {patches[0]} … {patches[-1]}"
)
updates.append(
{
"name": BASE_SYSTEM,
"current_version": installed[-1] if installed else self._out("uname -r"),
"new_version": summary,
"origin": "syspatch",
"security": None,
}
)
return updates
def _hardware(self) -> tuple[str, str, str]:
hw = self._platform()
return hw.get("vendor", ""), hw.get("product", ""), hw.get("serialno", "")