diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f0502..0d3ae89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.27.0] - 2026-12-03 + +### Added +- `get_shares()` (napalm-device-types' `PosixSharesMixin`, 4.58.0): the NFS exports and + Samba shares the host serves, from the kernel's export table and `testparm -s`, each + naming the service that serves it (NetOrk/netork#3104). Read on an exec channel as the + login user: both are readable to everyone. A NAS driver built on `LinuxDriver` that reads + its own configuration (OpenMediaVault) keeps its own `get_shares`. +- `get_network_mounts()` (napalm-device-types' `NetworkMountsMixin`, 4.58.0): the NFS + exports and SMB shares the host has mounted, from `/proc/self/mounts`, on an exec channel + as the login user (NetOrk/netork#3104). + +### Changed +- Through napalm-device-types 4.58.0, `get_services` reports `nfs-server` as running while + its unit is active, and `get_listening_sockets` names it as the unit of the kernel's NFS + sockets. + ## [0.26.0] - 2026-12-01 ### Added diff --git a/napalm_linux/_readings.py b/napalm_linux/_readings.py index 9f6d1ff..36164cb 100644 --- a/napalm_linux/_readings.py +++ b/napalm_linux/_readings.py @@ -1,5 +1,6 @@ """Readings of the host's processes: listening sockets, log sources, running backups, -and the services that still run replaced code. +and the services that still run replaced code; and the file shares the host serves +and has mounted (``_shares``, NetOrk/netork#3104). The first two commands are napalm-device-types' and read-only. Root only widens what they see -- ``ss`` names every process behind a socket, ``/proc//fd`` shows every @@ -26,6 +27,7 @@ from napalm_device_types import ListeningSocketsMixin, LogSourcesMixin, Services from napalm_linux._hardening import LinuxHardeningMixin from napalm_linux._logins import LinuxLoginAccountsMixin from napalm_linux._running_backups import LinuxRunningBackupsMixin +from napalm_linux._shares import LinuxSharesMixin #: Seconds either reading may take; a busy host lists thousands of descriptors. _READ_TIMEOUT = 60 @@ -38,6 +40,7 @@ class LinuxProcessReadingsMixin( LinuxRunningBackupsMixin, LinuxHardeningMixin, LinuxLoginAccountsMixin, + LinuxSharesMixin, ): """The transports for ``get_listening_sockets``, ``get_log_sources`` and ``get_services_need_restart``, and ``get_running_backups``, ``get_hardening`` diff --git a/napalm_linux/_shares.py b/napalm_linux/_shares.py new file mode 100644 index 0000000..dd4d79c --- /dev/null +++ b/napalm_linux/_shares.py @@ -0,0 +1,38 @@ +"""The file shares the host serves and the ones it has mounted (NetOrk/netork#3104). + +Both readings are napalm-device-types': the NFS exports and Samba shares the host +serves (``PosixSharesMixin``) and the NFS exports and SMB shares it has mounted +(``NetworkMountsMixin``). Both commands are read-only and read files every user may +read, so they run on an exec channel under ``sh`` as the login user, never through sudo. +""" + +from __future__ import annotations + +from shlex import quote +from typing import TYPE_CHECKING + +from napalm_device_types import NetworkMountsMixin, PosixSharesMixin + +if TYPE_CHECKING: # pragma: no cover - for type checkers only + from napalm_device_types import CommandResult + + +class LinuxSharesMixin(PosixSharesMixin, NetworkMountsMixin): + """The transports for ``get_shares`` and ``get_network_mounts``.""" + + if TYPE_CHECKING: # pragma: no cover - declared for type checkers only + + def run_command( + self, + command: str, + *, + privileged: bool = False, + timeout: float = 60, + stdin: bytes | None = None, + ) -> CommandResult: ... + + def _run_shares_command(self, command: str) -> str: + return self.run_command(f"sh -c {quote(command)}", timeout=60).stdout + + def _run_network_mounts_command(self, command: str) -> str: + return self.run_command(f"sh -c {quote(command)}", timeout=30).stdout diff --git a/pyproject.toml b/pyproject.toml index 708b956..3f76ea7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-linux" -version = "0.26.0" +version = "0.27.0" description = "NAPALM driver for generic Linux systems via SSH" readme = "README.md" requires-python = ">=3.12" @@ -34,7 +34,7 @@ classifiers = [ ] dependencies = [ "napalm>=4.0", - "napalm-device-types>=4.55.0", # LoginSessionsMixin + "napalm-device-types>=4.58.0", # PosixSharesMixin, nfs-server running "netmiko>=4.0.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_linux.py b/tests/test_linux.py index 6e3fd03..23d220e 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -1523,6 +1523,61 @@ def test_get_service_packages_runs_the_shared_command_under_sh_on_an_exec_channe driver._device.send_command.assert_not_called() +_SHARES = ( + "SHARES_BEGIN\n[units]\nId=nfs-server.service\nLoadState=loaded\n\n" + "Id=smbd.service\nLoadState=not-found\n\nId=smb.service\nLoadState=not-found\n" + "[etab]\n/srv/backups\t10.0.0.0/24(rw,sync,no_subtree_check)\n[samba]\nSHARES_END\n" +) + + +def test_get_shares_runs_the_shared_command_under_sh_on_an_exec_channel(driver): + """The NFS exports and Samba shares an ordinary server serves (NetOrk/netork#3104). + Everything it reads is readable to everyone, so never through sudo.""" + from shlex import quote + + from napalm_device_types import CommandResult, PosixSharesMixin + from napalm_device_types.posix_shares import POSIX_SHARES_COMMAND + + assert isinstance(driver, PosixSharesMixin) + calls = [] + + def run_command(command, *, privileged=False, timeout=60, stdin=None): + calls.append((command, privileged)) + return CommandResult(_SHARES, "", 0) + + driver.run_command = run_command + + shares = driver.get_shares() + + assert list(shares) == ["nfs:/srv/backups"] + assert shares["nfs:/srv/backups"]["service"] == "nfs-server" + assert calls == [(f"sh -c {quote(POSIX_SHARES_COMMAND)}", False)] + driver._device.send_command.assert_not_called() + + +def test_get_network_mounts_reads_the_hosts_mounts_without_sudo(driver): + """Which NFS exports and SMB shares the host uses (NetOrk/netork#3104).""" + from shlex import quote + + from napalm_device_types import CommandResult, NetworkMountsMixin + from napalm_device_types.network_mounts import NETWORK_MOUNTS_COMMAND + + assert isinstance(driver, NetworkMountsMixin) + calls = [] + + def run_command(command, *, privileged=False, timeout=60, stdin=None): + calls.append((command, privileged)) + out = "MOUNTS_BEGIN\nnas:/export/scan /mnt/scan nfs4 rw,addr=192.0.2.8 0 0\nMOUNTS_END\n" + return CommandResult(out, "", 0) + + driver.run_command = run_command + + [mount] = driver.get_network_mounts() + + assert (mount["server"], mount["export"]) == ("nas", "/export/scan") + assert calls == [(f"sh -c {quote(NETWORK_MOUNTS_COMMAND)}", False)] + + # --------------------------------------------------------------------------- # Services: listed in one round trip, controlled through systemctl (#7) # ---------------------------------------------------------------------------