"""How people log in to the firewall: the web interface's auth servers, and SSH (NetOrk/netork#662). **One list for the web interface, the console and SSH passwords.** ```` names auth servers, comma-separated, by their ````. Without it, only the built-in "Local Database" is used. OPNsense tries a login against each named server in turn, and the first that accepts it lets the person in (``AuthenticationFactory::authenticate``). A name no server has is skipped without a word, and so is the API's own "Local API". Names are not trimmed. The ``System`` service (``Auth/Services/System.php``) runs the same list for ``sshd``, ``login``, ``su`` and ``sudo``, so an SSH password or challenge-response login goes through it too. An SSH key does not: sshd checks it itself. **What each kind of server asks for** (``Auth/*.php``, ``getType()``): - ``local`` and ``ldap`` ask for a password, and a ``voucher`` for its code alone. - ``totp`` and ``ldap-totp`` ask for the password and a code in one field. A user without an ``otp_seed`` is refused, and no option lets them through. - ``radius`` hands the login to another system, which may ask for anything. - Any other type is one this driver does not know. **sshd** is written from ```` (``openssh_configure_do`` in ``plugins.inc.d/openssh.inc``): - It runs only with ``enabled`` set. The installer's live system is not read. - ``PasswordAuthentication`` and ``ChallengeResponseAuthentication`` follow ``passwordauth``, ``PermitRootLogin`` follows ``permitrootlogin``. Each switch is tested with ``isset``, so an empty element counts as set. - ``PubkeyAuthentication`` is always on. The rest is FreeBSD's defaults: PAM on, host-based and GSSAPI off, no ``AuthenticationMethods``. All of it is read from config.xml, the download ``get_config`` uses: no API controller reads these settings, up to 26.7 and on ``master``. """ from __future__ import annotations # config.xml as the firewall's own API hands it over; ElementTree resolves no external # entities, and expat limits entity expansion since 2.4.1. import xml.etree.ElementTree as ET # nosec B405 from typing import TYPE_CHECKING from napalm_device_types import LoginFactors, LoginSourceDict, SshServerAuthDict, WebLoginAuthDict #: config.xml itself (the newest entry of the configuration history). CONFIG_PATH = "/api/core/backup/download/this" #: The built-in server's name, and the one used when ``authmode`` names none. LOCAL_DATABASE = "Local Database" #: What each kind of auth server asks for; any other kind is ``unknown``. FACTORS_BY_KIND: dict[str, LoginFactors] = { "local": "password", "ldap": "password", "voucher": "password", "totp": "password_otp", "ldap-totp": "password_otp", "radius": "external", } def _system(config_xml: str) -> ET.Element: # The firewall's own config.xml: see the import. system = ET.fromstring(config_xml).find("system") # nosec B314 return system if system is not None else ET.Element("system") def _servers(system: ET.Element) -> dict[str, str]: """Each auth server's type by its name, the built-in one first (``listServers``). A configured server needs both; one named "Local Database" replaces the built-in. """ servers = {LOCAL_DATABASE: "local"} for server in system.findall("authserver"): name = server.findtext("name") or "" kind = server.findtext("type") or "" if name and kind: servers[name] = kind return servers def _login_sources(system: ET.Element) -> list[LoginSourceDict]: authmode = system.findtext("webgui/authmode") or "" names = authmode.split(",") if authmode else [LOCAL_DATABASE] servers = _servers(system) return [ { "name": name, "kind": servers[name], "factors": FACTORS_BY_KIND.get(servers[name], "unknown"), } for name in names if name in servers ] def login_sources(config_xml: str) -> list[LoginSourceDict]: """The servers a login is tried against, in their order. :raises xml.etree.ElementTree.ParseError: for a text that is no XML. """ return _login_sources(_system(config_xml)) def ssh_server_auth(config_xml: str) -> SshServerAuthDict: """sshd's login settings as config.xml makes them, and what checks a password. :raises xml.etree.ElementTree.ParseError: for a text that is no XML. """ system = _system(config_xml) ssh = system.find("ssh") def switched(key: str) -> bool: return ssh is not None and ssh.find(key) is not None password = switched("passwordauth") return { "password_authentication": password, "kbd_interactive_authentication": password, "pubkey_authentication": True, "hostbased_authentication": False, "gssapi_authentication": False, "authentication_methods": [], "permit_root_login": "yes" if switched("permitrootlogin") else "no", "use_pam": True, "pam_auth_stack": None, "password_sources": _login_sources(system), "server_enabled": switched("enabled"), } class OPNsenseLoginAuthMixin: """Read how the web interface and SSH let people log in; change nothing.""" if TYPE_CHECKING: # pragma: no cover - the driver's REST helper def _get_text(self, path: str) -> str: ... def get_web_login_auth(self) -> WebLoginAuthDict: """The auth servers the web interface tries, in their order. Example:: {"sources": [{"name": "TOTP", "kind": "totp", "factors": "password_otp"}]} :raises Exception: when config.xml cannot be read, e.g. by a key without the backup privilege. """ return {"sources": login_sources(self._get_text(CONFIG_PATH))} def get_ssh_server_auth(self) -> SshServerAuthDict: """sshd's login settings from config.xml, with ``password_sources`` and ``server_enabled`` (napalm-device-types' ``SshServerAuthDict``). :raises Exception: when config.xml cannot be read. """ return ssh_server_auth(self._get_text(CONFIG_PATH))