# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The radio and system parts of an AP configuration, as UCI commands.""" from __future__ import annotations from collections.abc import Mapping from napalm_device_types import ApConfigDict from napalm_openwrt.ap_config._common import Plan, Radios, sq _DEFAULT_SYSLOG_PORT = 514 def _country(config: ApConfigDict, radios: Radios, plan: Plan) -> None: if not config.get("country_code") or not radios.all: return for radio in (radios.r5g, radios.r2g): if radio: plan.commands.append(f"uci set wireless.{radio}.country={sq(config['country_code'])}") plan.commits.add("wireless") def _htmode(field: str, radio: str | None, config: ApConfigDict, plan: Plan) -> None: values: Mapping[str, object] = config if values.get(field) and radio: plan.commands.append(f"uci set wireless.{radio}.htmode={sq(values[field])}") plan.commits.add("wireless") def radio_part(part: str, config: ApConfigDict, radios: Radios, plan: Plan) -> None: """``radio/country`` on every radio, ``radio/5g_htmode`` and ``radio/2g_htmode`` on theirs.""" if part == "radio/country": _country(config, radios, plan) elif part == "radio/5g_htmode": _htmode("htmode_5g", radios.r5g, config, plan) elif part == "radio/2g_htmode": _htmode("htmode_2g", radios.r2g, config, plan) def _syslog(config: ApConfigDict, plan: Plan) -> None: host = config.get("syslog_host") if not host: return port = config.get("syslog_port") or _DEFAULT_SYSLOG_PORT plan.commands += [ f"uci set system.@system[0].log_ip={sq(host)}", f"uci set system.@system[0].log_port={sq(port)}", "uci set system.@system[0].log_proto='udp'", "uci set system.@system[0].log_remote='1'", ] plan.commits.add("system") def _web_ui(config: ApConfigDict, plan: Plan) -> None: """LuCI is served by uhttpd, so the policy is the service state; the listener config stays as it is, and enabling it again restores exactly what was there.""" if "web_ui" not in config: return if config["web_ui"]: plan.commands.append("/etc/init.d/uhttpd enable && /etc/init.d/uhttpd start") else: plan.commands.append("/etc/init.d/uhttpd stop; /etc/init.d/uhttpd disable") def _ssh_port(config: ApConfigDict, plan: Plan) -> None: if not config.get("ssh_port"): return plan.commands.append(f"uci set dropbear.@dropbear[0].Port={sq(config['ssh_port'])}") plan.commits.add("dropbear") def _password_auth(config: ApConfigDict, plan: Plan) -> None: if "ssh_password_auth" not in config: return value = "on" if config["ssh_password_auth"] else "off" plan.commands.append(f"uci set dropbear.@dropbear[0].PasswordAuth={sq(value)}") plan.commands.append(f"uci set dropbear.@dropbear[0].RootPasswordAuth={sq(value)}") plan.commits.add("dropbear") _SYSTEM_PARTS = { "system/syslog": _syslog, "system/web_ui": _web_ui, "system/ssh_port": _ssh_port, "system/password_auth": _password_auth, } def system_part(part: str, config: ApConfigDict, plan: Plan) -> None: """Remote syslog, the web interface, the SSH port and SSH password logins.""" build = _SYSTEM_PARTS.get(part) if build is not None: build(config, plan)