diff --git a/CHANGELOG.md b/CHANGELOG.md index ab86d70..75c0a03 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] +### Added +- 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 + leaves the account open, and `add_local_account(..., clear_password=True)` removes the + password (`chpass -p '*'`): the account logs in with its key only and has no sudo. + Keys the person added on the host stay. + +### Changed +- Needs napalm-device-types 4.28.0. + ### Added - Scheduled jobs on FreeBSD and OpenBSD (NetOrk/netork#1933, #27), through napalm-device-types' `PosixScheduledJobsMixin`: `get_scheduled_jobs()` reads diff --git a/napalm_bsd/accounts.py b/napalm_bsd/accounts.py index 213bdea..054d369 100644 --- a/napalm_bsd/accounts.py +++ b/napalm_bsd/accounts.py @@ -5,6 +5,10 @@ commands. A locked account is ``pw lock``'s: ``*LOCKED*`` in front of the password, which sshd refuses a key login too. getpwnam(3) shows the password field to root only, so the lock is read as root; nothing but its prefix is looked at. + +An account whose password is removed (NetOrk/netork#2013) gets ``*`` as its +password field, as ``pw useradd -w no`` gives a new one: no hash matches it, +and sshd takes only ``*LOCKED*`` for a lock, so the key still logs in. """ from __future__ import annotations @@ -15,6 +19,8 @@ from napalm_device_types import LocalAccountsMixin #: What ``pw lock`` puts in front of the password. _LOCKED_PREFIX = "*LOCKED*" +#: The password field of an account that logs in with a key only: no hash matches it. +_KEY_ONLY = "*" class FreeBSDLocalAccountsMixin(LocalAccountsMixin): @@ -69,6 +75,9 @@ class FreeBSDLocalAccountsMixin(LocalAccountsMixin): def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]: return ["pw", "groupmod", group, "-d", name] + def _account_clear_password(self, name: str) -> list[list[str]]: + return [["chpass", "-p", _KEY_ONLY, name]] + @staticmethod def _set_password(name: str, password_hash: str | None) -> list[list[str]]: """chpass takes the crypt(3) string itself from root.""" diff --git a/pyproject.toml b/pyproject.toml index 9f8d256..891d376 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.27.0", + "napalm-device-types>=4.28.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_local_accounts.py b/tests/test_local_accounts.py index 30b7865..1a02d7b 100644 --- a/tests/test_local_accounts.py +++ b/tests/test_local_accounts.py @@ -7,6 +7,8 @@ refuses a key login too. OpenBSD offers none of it yet. from __future__ import annotations +import shlex + import pytest from napalm_device_types import CommandResult, LocalAccountsMixin @@ -104,3 +106,85 @@ def test_netorks_own_account_is_made_with_its_uid() -> None: assert _Driver()._account_create_with_uid("netork", 900) == [ ["pw", "useradd", "-n", "netork", "-u", "900", "-m", "-s", "/bin/sh", "-w", "no"] ] + + +# -- what a person changes follows them (NetOrk/netork#2013) ---------------------------- + +ALICE_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAlice alice@laptop" +OLD_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOld alice@lost-laptop" +OWN_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQOwn own@desktop" +_KEYS = "/home/alice/.ssh/authorized_keys" + + +class _Host(_Driver): + """A FreeBSD host with Alice's account and her key files.""" + + def __init__(self, keys: str) -> None: + # getent passwd: the seven fields, the hash shown to root only. + super().__init__(entry="alice:$6$old:1001:1001:Alice:/home/alice:/bin/sh") + self.files = {_KEYS: keys} + + def run_command(self, command, *, privileged=False, timeout=60, stdin=None): + self.ran.append((command, privileged)) + if stdin is not None: + self.files["/tmp/staged"] = stdin.decode() + return CommandResult("/tmp/staged", "", 0) + argv = shlex.split(command) + answers = { + "id -un": "netadmin\n", + "getent passwd alice": self.entry + "\n", + "id -Gn alice": "alice wheel\n", + "sudo -l -U alice": "User alice is not allowed to run sudo on host.\n", + } + if command in answers: + return CommandResult(answers[command], "", 0) + if argv[:2] == ["test", "-e"]: + return CommandResult("", "", 0 if argv[2] in self.files else 1) + if argv[:4] == ["sudo", "-n", "-u", "alice"] and argv[4] == "cat": + return CommandResult(self.files.get(argv[-1], ""), "", 0) + if argv[:4] == ["sudo", "-n", "-u", "alice"] and argv[4] == "install": + self.files[argv[-1]] = self.files[argv[-2]] + return CommandResult("", "", 0) + + def changed_as_root(self) -> list[list[str]]: + reads = (["getent"], ["id"], ["test"], ["sudo", "-n"], ["sudo", "-l"]) + words = [shlex.split(c) for c, privileged in self.ran if privileged] + return [w for w in words if not any(w[: len(r)] == r for r in reads)] + + +def test_clearing_the_password_leaves_the_star_pw_gives_a_key_only_account() -> None: + # "*" is what pw useradd -w no writes: sshd lets the key in, no password matches. + # Not "*LOCKED*", which is the lock and refuses the key too. + assert _Driver()._account_clear_password("alice") == [["chpass", "-p", "*", "alice"]] + + +def test_removing_the_password_runs_chpass_and_takes_sudo_away() -> None: + host = _Host(keys=ALICE_KEY + "\n") + + result = host.add_local_account("alice", ALICE_KEY, clear_password=True) + + assert result["success"], result["output"] + assert host.changed_as_root() == [ + ["rm", "-f", "--", "/usr/local/etc/sudoers.d/80-account-alice"], + ["pw", "groupmod", "wheel", "-d", "alice"], + ["chpass", "-p", "*", "alice"], + ] + + +def test_a_replaced_key_is_written_out_of_the_key_file_as_the_person() -> None: + host = _Host(keys=f"{OLD_KEY}\n{OWN_KEY}\n") + + result = host.add_local_account("alice", ALICE_KEY, replaces=[OLD_KEY]) + + assert result["success"], result["output"] + assert host.files[_KEYS] == f"{OWN_KEY}\n{ALICE_KEY}\n" + + +def test_a_deleted_key_goes_and_the_account_stays_open() -> None: + host = _Host(keys=f"{ALICE_KEY}\n{OWN_KEY}\n") + + result = host.remove_local_account_keys("alice", [ALICE_KEY]) + + assert result["success"], result["output"] + assert host.files[_KEYS] == f"{OWN_KEY}\n" + assert host.changed_as_root() == []