From 55507a8a25dad47e61112fd607a054e39272eb42 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 1 Dec 2026 09:40:21 +0100 Subject: [PATCH] feat(logins): get_login_sessions through LoginSessionsMixin, 0.26.0 (NetOrk/netork#2881) The interactive logins the host recorded, napalm-device-types' command on an exec channel as the login user: wtmp is readable to everyone, and sudo would drop SSH_CONNECTION, which names the driver's own address. --- CHANGELOG.md | 11 +++++++++++ napalm_linux/_logins.py | 19 ++++++++++++------- napalm_linux/_readings.py | 8 +++++--- pyproject.toml | 4 ++-- tests/test_linux.py | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbe6c7e..01f0502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.26.0] - 2026-12-01 + +### Added +- `get_login_sessions()` (napalm-device-types' `LoginSessionsMixin`, 4.55.0): the + interactive logins the host recorded over the last seven days, from `wtmpdb last` and + `last`, each with its account, terminal, address and times, and the address of the + driver's own SSH session, whose sessions are left out (NetOrk/netork#2881). Read on an + exec channel as the login user: the records are readable to everyone, and sudo would + drop `SSH_CONNECTION`. OpenMediaVault and QNAP have it through `LinuxDriver`; a host + whose `last` is BusyBox's raises `LoginSessionsUnavailable`. + ## [0.25.0] - 2026-11-29 ### Added diff --git a/napalm_linux/_logins.py b/napalm_linux/_logins.py index 4477c79..0c6bb8f 100644 --- a/napalm_linux/_logins.py +++ b/napalm_linux/_logins.py @@ -1,27 +1,29 @@ """Who can log in to the host, and how they do: napalm-device-types' ``LoginAccountsMixin`` -(NetOrk/netork#2707). +(NetOrk/netork#2707); and the interactive logins it recorded, ``LoginSessionsMixin`` +(NetOrk/netork#2881). -The command and its parse are napalm-device-types'; this is only how the command -reaches the host. An exec channel, as for the hardening: the report can run to +The commands and their parses are napalm-device-types'; this is only how the commands +reach the host. An exec channel, as for the hardening: the report can run to thousands of lines on a host whose btmp is full, more than netmiko's PTY reads reliably. Read-only either way; root for the lock, the expiry and the failed logins, and the mixin falls back to the login user, for the last logins only, where root is -refused. +refused. The sessions are read as the login user only: the records are readable to +everyone, and sudo would drop ``SSH_CONNECTION``, which names the driver's own address. """ from __future__ import annotations from typing import TYPE_CHECKING -from napalm_device_types import CommandResult, LoginAccountsMixin +from napalm_device_types import CommandResult, LoginAccountsMixin, LoginSessionsMixin #: Seconds the reading may take: ``lastb`` reads all of btmp, which a host on the #: internet fills quickly. _READ_TIMEOUT = 120 -class LinuxLoginAccountsMixin(LoginAccountsMixin): - """The transport for ``get_login_accounts``.""" +class LinuxLoginAccountsMixin(LoginAccountsMixin, LoginSessionsMixin): + """The transports for ``get_login_accounts`` and ``get_login_sessions``.""" if TYPE_CHECKING: # pragma: no cover - the driver's own, declared for type checkers @@ -36,3 +38,6 @@ class LinuxLoginAccountsMixin(LoginAccountsMixin): def _run_login_accounts_command(self, command: str, *, privileged: bool) -> str: return self.run_command(command, privileged=privileged, timeout=_READ_TIMEOUT).stdout + + def _run_login_sessions_command(self, command: str) -> str: + return self.run_command(command, timeout=_READ_TIMEOUT).stdout diff --git a/napalm_linux/_readings.py b/napalm_linux/_readings.py index 0ccb870..9f6d1ff 100644 --- a/napalm_linux/_readings.py +++ b/napalm_linux/_readings.py @@ -11,7 +11,8 @@ would read as clean. It is one fixed command and reads no stdin. The running backups need no root: every process's command line and cgroup are readable to the login user (``_running_backups``). How the host is hardened (``_hardening``) and who can log in to it (``_logins``, NetOrk/netork#2707) are read -as root first, as the login user where root is refused. Canonical Livepatch's status +as root first, as the login user where root is refused; the logins it recorded +(``_logins``, NetOrk/netork#2881) as the login user only. Canonical Livepatch's status (napalm-device-types' kernel facts, NetOrk/netork#2701) is read again as root where it did not read without: one fixed, read-only command, no stdin. """ @@ -40,8 +41,9 @@ class LinuxProcessReadingsMixin( ): """The transports for ``get_listening_sockets``, ``get_log_sources`` and ``get_services_need_restart``, and ``get_running_backups``, ``get_hardening`` - (``_hardening``, NetOrk/netork#2706) and ``get_login_accounts`` (``_logins``, - NetOrk/netork#2707), which ride along here rather than lengthen ``linux.py``.""" + (``_hardening``, NetOrk/netork#2706), ``get_login_accounts`` and ``get_login_sessions`` + (``_logins``, NetOrk/netork#2707, NetOrk/netork#2881), which ride along here rather + than lengthen ``linux.py``.""" if TYPE_CHECKING: # pragma: no cover - the driver's own, declared for type checkers diff --git a/pyproject.toml b/pyproject.toml index 5902f75..708b956 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-linux" -version = "0.25.0" +version = "0.26.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.53.0", # virtualization in the hardening, CpuMicrocodeMixin + "napalm-device-types>=4.55.0", # LoginSessionsMixin "netmiko>=4.0.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_linux.py b/tests/test_linux.py index efa1ee5..6e3fd03 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -2227,6 +2227,38 @@ class TestGetLoginAccounts: assert calls == [True, False] +class TestGetLoginSessions: + """The interactive logins the host recorded (NetOrk/netork#2881): napalm-device-types' + command on an exec channel as the login user -- sudo would drop ``SSH_CONNECTION``, + and the login records are readable to everyone.""" + + _REPORT = ( + "LOGSESSIONS_BEGIN\n[own]\n198.51.100.2\n[now]\n2026-11-30T12:00:00Z\n[wtmp since]\n" + "alice pts/1 192.0.2.10 2026-11-30T10:15:02+01:00 still logged in\n" + "LOGSESSIONS_END\n" + ) + + def test_it_reads_the_shared_command_as_the_login_user_on_an_exec_channel(self, driver): + from napalm_device_types import CommandResult, LoginSessionsMixin + + assert isinstance(driver, LoginSessionsMixin) + calls = [] + + def run_command(command, *, privileged=False, timeout=60, stdin=None): + calls.append((command, privileged, stdin)) + return CommandResult(self._REPORT, "", 0) + + driver.run_command = run_command + + reading = driver.get_login_sessions() + + assert reading["own_address"] == "198.51.100.2" + assert reading["sessions"][0]["address"] == "192.0.2.10" + assert [(privileged, stdin) for _, privileged, stdin in calls] == [(False, None)] + assert calls[0][0].startswith("sh -c ") + driver._device.send_command.assert_not_called() + + # --------------------------------------------------------------------------- # get_host_firewall (napalm-device-types HostFirewallMixin, NetOrk/netork#1344) # ---------------------------------------------------------------------------