feat: personal accounts on FreeBSD with pw and chpass (NetOrk/netork#1793)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Failing after 1m5s
driver / test (3.13) (pull_request) Failing after 55s
driver / test (3.12) (pull_request) Failing after 1m1s
driver / lowest (pull_request) Failing after 30s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m30s
CI / driver (pull_request) Failing after 3m1s
driver / publish (pull_request) Skipped

FreeBSDDriver mixes in napalm-device-types' LocalAccountsMixin: pw useradd
-m -w no, chpass -p for the password hash, pw lock/unlock and pw groupmod
-d. A locked account carries *LOCKED*, which sshd refuses a key login too
(nuageinit sets UsePAM no, so nothing else would). getpwnam(3) shows the
password field to root only, so the lock is read with getent as root and
only its prefix is looked at. The sudo rule goes to the sudo package's
/usr/local/etc/sudoers.d.

Checked on a FreeBSD 15.1 cloud image with the real driver over SSH and
sudo -S: key login works after adding and after reopening and is refused
after locking, sudo takes the person's password, wheel is left when sudo
is not wanted. OpenBSD is left out for now.

CI pre-installs napalm-device-types from main until the release with
LocalAccountsMixin is on PyPI.
This commit is contained in:
Christian Manivong committed 2026-10-31 23:32:01 +01:00
1 parent 120297ab12
commit faa5f24182
5 files changed
+171 -1

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
"""Personal accounts on a FreeBSD host (NetOrk/netork#1793).
napalm-device-types' ``LocalAccountsMixin`` runs the steps; the driver names
FreeBSD's commands. A locked account is ``pw lock``'s ``*LOCKED*``, which sshd
refuses a key login too. OpenBSD offers none of it yet.
"""
from __future__ import annotations
import pytest
from napalm_device_types import CommandResult, LocalAccountsMixin
from napalm_bsd import FreeBSDDriver, OpenBSDDriver
HASH = "$6$salt$" + "x" * 86
class _Driver(FreeBSDDriver):
"""A FreeBSD driver whose host answers ``getent passwd alice`` with *entry*."""
def __init__(self, entry: str = "alice:$6$old:1001:1001::0:0:Alice:/home/alice:/bin/sh"):
super().__init__("host", "netadmin", "pw")
self.entry = entry
self.ran: list[tuple[str, bool]] = []
def run_command(self, command, *, privileged=False, timeout=60, stdin=None):
self.ran.append((command, privileged))
if command == "getent passwd alice":
return CommandResult(self.entry + "\n", "", 0 if self.entry else 2)
return CommandResult("", "", 0)
def _passwd() -> list[str]:
return "alice:*:1001:1001:Alice:/home/alice:/bin/sh".split(":")
def test_freebsd_can_add_and_lock_personal_accounts() -> None:
assert issubclass(FreeBSDDriver, LocalAccountsMixin)
assert FreeBSDDriver.ACCOUNT_SHELL == "/bin/sh"
assert FreeBSDDriver.ACCOUNT_PASSWORD_SCHEME == "sha512"
assert FreeBSDDriver.ACCOUNT_ADMIN_GROUPS == ("wheel",)
# The sudo package lives under /usr/local and includes its sudoers.d.
assert FreeBSDDriver.SUDOERS_DIR == "/usr/local/etc/sudoers.d"
assert FreeBSDDriver.ROOT_GROUP == "wheel"
def test_openbsd_offers_none_of_it_yet() -> None:
assert not issubclass(OpenBSDDriver, LocalAccountsMixin)
assert not hasattr(OpenBSDDriver, "add_local_account")
def test_a_new_account_gets_a_home_and_its_password_through_chpass() -> None:
d = _Driver()
create = ["pw", "useradd", "-n", "alice", "-m", "-s", "/bin/sh", "-w", "no"]
assert d._account_create("alice", None) == [create]
assert d._account_create("alice", HASH) == [create, ["chpass", "-p", HASH, "alice"]]
def test_opening_unlocks_only_a_locked_account() -> None:
d = _Driver()
assert d._account_open("alice", None, locked=True) == [["pw", "unlock", "alice"]]
assert d._account_open("alice", None, locked=False) == []
assert d._account_open("alice", HASH, locked=True) == [
["pw", "unlock", "alice"],
["chpass", "-p", HASH, "alice"],
]
def test_locking_is_pw_lock_once() -> None:
d = _Driver()
# pw refuses to lock a locked account again.
assert d._account_lock("alice", locked=False) == [["pw", "lock", "alice"]]
assert d._account_lock("alice", locked=True) == []
@pytest.mark.parametrize(
("password", "locked"),
[("$6$old", False), ("*", False), ("*LOCKED*$6$old", True), ("*LOCKED**", True)],
)
def test_the_lock_is_read_from_the_password_field_as_root(password: str, locked: bool) -> None:
# getpwnam(3) returns the encrypted password only to root, "*" to anyone else.
d = _Driver(entry=f"alice:{password}:1001:1001:Alice:/home/alice:/bin/sh")
assert d._account_locked("alice", _passwd()) is locked
assert d.ran == [("getent passwd alice", True)]
def test_an_entry_that_cannot_be_read_raises() -> None:
with pytest.raises(RuntimeError, match="alice"):
_Driver(entry="")._account_locked("alice", _passwd())
def test_leaving_a_group_is_pw_groupmod() -> None:
assert _Driver()._account_leave_group("alice", "wheel", ["alice", "wheel"]) == [
"pw",
"groupmod",
"wheel",
"-d",
"alice",
]