feat(accounts): clear a password, replace and remove keys
driver / lint (pull_request) Successful in 17s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m27s
driver / publish (pull_request) Skipped

napalm-device-types 4.28.0 lets add_local_account remove the old keys
(replaces) and the password (clear_password), and adds
remove_local_account_keys. The key changes run through the mixin's own
key files as the person; the password goes with usermod -p '*', which
leaves the expiry (the lock) alone and keeps the key login working with
and without PAM, where an empty password or a '!' would not.

See NetOrk/netork#2013
This commit is contained in:
Christian Manivong committed 2026-11-04 11:46:13 +01:00
1 parent 650379680f
commit d03fe2a2ba
4 files changed
+131 -1

No files matched your search

+14
View File
@@ -5,6 +5,13 @@ commands. A locked account is an expired one (``usermod -L -e 1``): PAM's
account check refuses it every login, a key's too, where a ``!`` in front of
the password -- ``usermod -L`` alone -- stops only the password. Opening it
again lifts the expiry and gives the old password back.
An account whose password is removed (NetOrk/netork#2013) gets ``*`` as its
password field, which no hash matches: it logs in with a key only. Not an
empty field (``passwd -d``), which pam_unix's ``nullok`` -- on by default on
Debian and Ubuntu -- takes as "no password needed" for ``su`` and the console.
Not ``!``, which sshd takes for a locked account and refuses a key too where
PAM is off (``UsePAM no``). And not the expiry, which is the lock.
"""
from __future__ import annotations
@@ -16,6 +23,8 @@ from napalm_device_types import LocalAccountsMixin
#: What the password field holds when the account never had a password.
_NO_PASSWORD = frozenset({"", "!", "!!", "!*", "*"})
#: The password field of an account that logs in with a key only: no hash matches it.
_KEY_ONLY = "*"
class LinuxLocalAccountsMixin(LocalAccountsMixin):
@@ -53,6 +62,11 @@ class LinuxLocalAccountsMixin(LocalAccountsMixin):
def _account_leave_group(self, name: str, group: str, groups: list[str]) -> list[str]:
return ["gpasswd", "-d", name, group]
def _account_clear_password(self, name: str) -> list[list[str]]:
# usermod -p leaves the expiry -- the lock -- alone, and dates the change today,
# so pam_unix asks for no new password at the next key login.
return [["usermod", "-p", _KEY_ONLY, name]]
def _shadow(self, name: str) -> list[str]:
"""*name*'s shadow entry, split; only the lock and the expiry are looked at.