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
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:
1 parent
120297ab12
commit
faa5f24182
5 files changed
+171
-1
No files matched your search
@@ -0,0 +1,58 @@
|
||||
"""Personal accounts on a FreeBSD host, with pw(8) and chpass(1) (NetOrk/netork#1793).
|
||||
|
||||
napalm-device-types' ``LocalAccountsMixin`` runs the steps; this names the
|
||||
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.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shlex
|
||||
|
||||
from napalm_device_types import LocalAccountsMixin
|
||||
|
||||
#: What ``pw lock`` puts in front of the password.
|
||||
_LOCKED_PREFIX = "*LOCKED*"
|
||||
|
||||
|
||||
class FreeBSDLocalAccountsMixin(LocalAccountsMixin):
|
||||
ACCOUNT_SHELL = "/bin/sh"
|
||||
ACCOUNT_ADMIN_GROUPS = ("wheel",)
|
||||
#: The sudo package's, which its default sudoers includes.
|
||||
SUDOERS_DIR = "/usr/local/etc/sudoers.d"
|
||||
ROOT_GROUP = "wheel"
|
||||
|
||||
def _account_create(self, name: str, password_hash: str | None) -> list[list[str]]:
|
||||
# -w no: the password field is "*", no password login until one is set.
|
||||
create = ["pw", "useradd", "-n", name, "-m", "-s", self.ACCOUNT_SHELL, "-w", "no"]
|
||||
return [create, *self._set_password(name, password_hash)]
|
||||
|
||||
def _account_open(
|
||||
self, name: str, password_hash: str | None, *, locked: bool
|
||||
) -> list[list[str]]:
|
||||
unlock = [["pw", "unlock", name]] if locked else []
|
||||
return [*unlock, *self._set_password(name, password_hash)]
|
||||
|
||||
def _account_lock(self, name: str, *, locked: bool) -> list[list[str]]:
|
||||
# pw refuses to lock a locked account again.
|
||||
return [] if locked else [["pw", "lock", name]]
|
||||
|
||||
def _account_locked(self, name: str, passwd: list[str]) -> bool:
|
||||
result = self.run_command(
|
||||
shlex.join(["getent", "passwd", name]), privileged=True, timeout=30
|
||||
)
|
||||
fields = result.stdout.strip().split(":")
|
||||
if result.exit_code != 0 or len(fields) < 7 or fields[0] != name:
|
||||
reason = (result.stderr or result.stdout).strip() or f"exit {result.exit_code}"
|
||||
raise RuntimeError(f"cannot read whether {name} is locked: {reason}")
|
||||
return fields[1].startswith(_LOCKED_PREFIX)
|
||||
|
||||
def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]:
|
||||
return ["pw", "groupmod", group, "-d", name]
|
||||
|
||||
@staticmethod
|
||||
def _set_password(name: str, password_hash: str | None) -> list[list[str]]:
|
||||
"""chpass takes the crypt(3) string itself from root."""
|
||||
return [["chpass", "-p", password_hash, name]] if password_hash else []
|
||||
@@ -5,10 +5,11 @@ from __future__ import annotations
|
||||
from napalm_device_types import FingerprintRule
|
||||
|
||||
from napalm_bsd import parse
|
||||
from napalm_bsd.accounts import FreeBSDLocalAccountsMixin
|
||||
from napalm_bsd.base import BASE_SYSTEM, BsdDriver
|
||||
|
||||
|
||||
class FreeBSDDriver(BsdDriver):
|
||||
class FreeBSDDriver(FreeBSDLocalAccountsMixin, BsdDriver):
|
||||
"""NAPALM driver for FreeBSD hosts, over SSH."""
|
||||
|
||||
TYPE_LABEL = "FreeBSD"
|
||||
|
||||
Reference in new issue
Block a user