"""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", ] def test_netorks_own_account_is_made_with_its_uid() -> None: """NetOrk/netork#1817: a fixed uid on every host, no password.""" assert _Driver()._account_create_with_uid("netork", 900) == [ ["pw", "useradd", "-n", "netork", "-u", "900", "-m", "-s", "/bin/sh", "-w", "no"] ]