diff --git a/CHANGELOG.md b/CHANGELOG.md index c9e4e53..8bd2538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- `get_ssh_server_auth()` on FreeBSD and OpenBSD, through napalm-device-types' + `SshServerAuthMixin` (4.32.0): whether sshd takes passwords, keyboard-interactive or keys, + its `AuthenticationMethods`, `PermitRootLogin` and, on FreeBSD, the modules of sshd's PAM + `auth` stack. Needs root for `sshd -T` (NetOrk/netork#662). - Personal accounts on FreeBSD follow what the person changes (NetOrk/netork#2013): `add_local_account(..., replaces=[...])` takes the old keys out of the account's key files as the new one goes in, `remove_local_account_keys` takes deleted keys out and diff --git a/README.md b/README.md index 099bd30..f069252 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ provisions and manages BSD VMs (NetOrk/netork#792). | `get_services`, `manage_service` | ✓ | ✓ | `service -e` + `service … status` / `service … `; `rcctl ls on` + `rcctl check` / `rcctl ` | | `run_device_action("fix_snmp")`, `get_snmp_config` | ✓ | ✓ | net-snmp from packages: `/usr/local/etc/snmp/snmpd.conf` + `service snmpd`; `/etc/snmp/snmpd.conf` + `rcctl … netsnmpd` | | `get_host_status` | ✓ | unknown | napalm-device-types' host status: on FreeBSD `freebsd-version -k` vs `-r` (4.1+) | +| `get_ssh_server_auth` | ✓ | ✓ | napalm-device-types' `SshServerAuthMixin`: `sshd -T` as root, sshd's PAM `auth` stack from `/etc/pam.d` (FreeBSD; OpenBSD's sshd has no PAM) | | `first_boot_pending` | ✓ | always `False` | whether `/firstboot` still exists | `get_listening_sockets` has the shape of napalm-device-types' diff --git a/napalm_bsd/base.py b/napalm_bsd/base.py index ecc98b7..40aafd8 100644 --- a/napalm_bsd/base.py +++ b/napalm_bsd/base.py @@ -26,6 +26,7 @@ from napalm_device_types.channel import ( from napalm_device_types.host_status import HostStatusMixin from napalm_device_types.models import FixStepOutcomeDict, SnmpAgentSettingsDict from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch +from napalm_device_types.ssh_server_auth import SshServerAuthMixin from napalm_bsd import parse from napalm_bsd.scheduled_jobs import BsdScheduledJobsMixin @@ -89,9 +90,18 @@ def render_snmpd_conf(settings: SnmpAgentSettingsDict) -> str: class BsdDriver( - BsdScheduledJobsMixin, SnmpAgentMixin, EnsurePackageMixin, HostStatusMixin, OSDriver + BsdScheduledJobsMixin, + SnmpAgentMixin, + EnsurePackageMixin, + HostStatusMixin, + SshServerAuthMixin, + OSDriver, ): - """Base for the BSD drivers; a concrete one names the commands that differ.""" + """Base for the BSD drivers; a concrete one names the commands that differ. + + ``SshServerAuthMixin`` reads how sshd lets people log in (``sshd -T`` as root, + and sshd's PAM stack on FreeBSD); OpenBSD's sshd has no PAM. + """ #: One role for FreeBSD and OpenBSD. It replaces OSDriver's "linux", which #: would have the application treat a BSD host as a Linux one (#10). diff --git a/pyproject.toml b/pyproject.toml index b3a6740..0cc0f88 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.29.0", + "napalm-device-types>=4.32.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_drivers.py b/tests/test_drivers.py index df90d30..8235398 100644 --- a/tests/test_drivers.py +++ b/tests/test_drivers.py @@ -7,6 +7,7 @@ from pathlib import Path import pytest from napalm.base import NetworkDriver from napalm_device_types import OSDriver, primary_role_of, role_keys_of +from napalm_device_types.ssh_server_auth import PAM_FILES_COMMAND, SSHD_EFFECTIVE_COMMAND from napalm_bsd import FreeBSDDriver, OpenBSDDriver, base from napalm_bsd.base import BsdDriver @@ -737,3 +738,56 @@ class TestFirstBoot: driver = _channel(OpenBSDDriver, {}) assert driver.first_boot_pending() is False assert driver.calls == [] + + +#: The authentication lines of ``sshd -T`` on FreeBSD 15 as installed: PAM on, +#: keyboard-interactive on (PAM's password prompt), password authentication off. +_SSHD_T_FREEBSD = """\ +port 22 +usepam yes +permitrootlogin no +hostbasedauthentication no +pubkeyauthentication yes +passwordauthentication no +kbdinteractiveauthentication yes +authenticationmethods any +""" +#: FreeBSD's /etc/pam.d/sshd, abridged. +_PAM_SSHD_FREEBSD = """\ +# auth +auth\t\trequired\tpam_unix.so\t\tno_warn try_first_pass +# account +account\t\trequired\tpam_nologin.so +account\t\trequired\tpam_unix.so +""" + + +class TestSshServerAuth: + """How sshd lets people log in, through napalm-device-types' SshServerAuthMixin (NetOrk/netork#662).""" + + def test_freebsd_reads_sshd_t_as_root_and_its_pam_stack(self): + driver = _channel( + FreeBSDDriver, + { + (SSHD_EFFECTIVE_COMMAND, True): _SSHD_T_FREEBSD, + PAM_FILES_COMMAND: f"==> /etc/pam.d/sshd <==\n{_PAM_SSHD_FREEBSD}", + }, + ) + auth = driver.get_ssh_server_auth() + assert auth["kbd_interactive_authentication"] is True + assert auth["password_authentication"] is False + assert [e["module"] for e in auth["pam_auth_stack"]] == ["pam_unix.so"] + assert driver.calls == [(SSHD_EFFECTIVE_COMMAND, True), (PAM_FILES_COMMAND, False)] + + def test_openbsd_has_no_pam_to_read(self): + text = _SSHD_T_FREEBSD.replace("usepam yes\n", "") + driver = _channel(OpenBSDDriver, {(SSHD_EFFECTIVE_COMMAND, True): text}) + auth = driver.get_ssh_server_auth() + assert auth["use_pam"] is None + assert auth["pam_auth_stack"] is None + assert driver.calls == [(SSHD_EFFECTIVE_COMMAND, True)] + + def test_without_root_the_read_raises(self): + driver = _channel(FreeBSDDriver, {(SSHD_EFFECTIVE_COMMAND, True): ("", 1)}) + with pytest.raises(RuntimeError, match="sshd -T"): + driver.get_ssh_server_auth()