diff --git a/CHANGELOG.md b/CHANGELOG.md index d6a8ea8..58e1ba6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,9 @@ recorded here; their history is the git log. account that is there is opened again and keeps its other keys; root is refused (NetOrk/netork#1817). With `uid=`, a new account gets that uid once no other account has it; one that is there keeps its own. A driver supplies - `_account_create_with_uid`. + `_account_create_with_uid`. An account that is there and cannot log in (`nologin`, + `false`) is another service's, such as a netOrk satellite's `netork`, and is refused + before anything changes. - `LocalAccountsMixin` (`napalm_device_types.local_accounts`): personal login accounts on a host. `get_local_accounts(names)` reads them back (groups, lock, sudo, key files), `add_local_account(name, public_key, password_hash=, admin=)` creates or diff --git a/napalm_device_types/local_accounts.py b/napalm_device_types/local_accounts.py index fae3d6e..d7f7b1e 100644 --- a/napalm_device_types/local_accounts.py +++ b/napalm_device_types/local_accounts.py @@ -85,6 +85,9 @@ _RUN_ALL_NOPASSWD_RE = re.compile( re.MULTILINE, ) +#: Login shells that refuse every login: an account with one belongs to a service. +_NO_LOGIN_SHELLS = frozenset({"nologin", "false"}) + #: Stages stdin as a file anyone may read -- the person reads the key file #: from it -- and prints its path. The content is a public key or a sudo rule. _STAGE = 't=$(mktemp) && chmod 644 "$t" && cat > "$t" && printf %s "$t"' @@ -409,17 +412,15 @@ class LocalAccountsMixin: Steps as in :meth:`add_local_account`: ``account``, then ``sudo`` and ``keys``. The new login itself is for the caller to try. - :raises ValueError: for a name or key no host takes, root, or a uid of 0 or less. + An account that is there and cannot log in (``nologin``, ``false``) is + another service's -- a satellite's ``netork``, say -- and is left alone. + + :raises ValueError: for a name or key no host takes, root, a uid of 0 or + less, or another service's account. :raises RuntimeError: when the login user cannot be told. """ - check_account_name(name) check_public_key(public_key) - if uid is not None and uid <= 0: - raise ValueError(f"not a uid for netOrk's own account: {uid}") - login = self._accounts_login() - existing = self._accounts_passwd(name) - if existing is not None and int(existing[2]) == 0: - raise ValueError(f"{name} is root") + login, existing = self._accounts_service_target(name, uid) if name == login: step: FixStepDict = { "step": "account", @@ -439,6 +440,24 @@ class LocalAccountsMixin: ] return self._accounts_result(name, parts) + def _accounts_service_target(self, name: str, uid: int | None) -> tuple[str, list[str] | None]: + """The login user and *name*'s passwd line, once *name* may become netOrk's. + + :raises ValueError: as :meth:`add_service_account` says. + """ + check_account_name(name) + if uid is not None and uid <= 0: + raise ValueError(f"not a uid for netOrk's own account: {uid}") + login = self._accounts_login() + existing = self._accounts_passwd(name) + if existing is not None and int(existing[2]) == 0: + raise ValueError(f"{name} is root") + if existing is not None and existing[6].rsplit("/", 1)[-1] in _NO_LOGIN_SHELLS: + raise ValueError( + f"{name} is there and cannot log in ({existing[6]}): another service's account" + ) + return login, existing + def _accounts_service_account( self, name: str, passwd: list[str] | None, uid: int | None ) -> FixStepOutcomeDict: diff --git a/tests/test_service_account.py b/tests/test_service_account.py index d9d4169..9bc270a 100644 --- a/tests/test_service_account.py +++ b/tests/test_service_account.py @@ -280,3 +280,22 @@ def test_a_driver_that_cannot_set_a_uid_says_so(host: _ServiceHost, driver: _Dri assert not result["success"] assert "uid" in result["output"] + + +# -- another service's account of the same name (bw-01: the satellite's) ------------------ + + +@pytest.mark.parametrize( + "shell", ["/usr/sbin/nologin", "/sbin/nologin", "/bin/false", "/usr/bin/false"] +) +def test_an_account_that_cannot_log_in_is_another_services_and_left_alone( + host: _ServiceHost, driver: _Driver, shell: str +) -> None: + host.accounts["netork"] = _Account( + 999, "/home/netork", shell=shell, groups=["netork", "docker"] + ) + + with pytest.raises(ValueError, match="cannot log in"): + driver.add_service_account("netork", NETORK_KEY) + assert not _root_commands(host) + assert service_sudoers_rule_path(_Driver.SUDOERS_DIR, "netork") not in host.files