# 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. """Time sync on OpenWrt: UCI's ``system`` config (napalm-device-types ``TimeSyncMixin``). OpenWrt keeps both values in ``/etc/config/system`` (NetOrk/netork#2364): * **The servers** are the ``system.ntp.server`` list, which ``/etc/init.d/sysntpd`` hands to BusyBox ntpd while ``system.ntp.enabled`` is on. A disabled client has no servers: the list names nothing the clock takes time from. * **What DHCP brings** (option 42) counts too (NetOrk/netork#2380): while ``use_dhcp`` is on (the default) sysntpd adds the NTP servers of every interface's lease -- or of the ``dhcp_interface`` list's only -- that netifd keeps as ``data.ntpserver`` in ``ubus call network.interface dump`` (``/lib/netifd/dhcp.script`` stores udhcpc's ``ntpsrv`` there). They are read from the same place, in the same order, as ``dhcp_ntp_servers``; a write leaves ``use_dhcp`` as the router has it. * **The zone** is kept twice: ``zonename`` (the IANA name, as LuCI writes it -- with spaces where the IANA name has underscores) and ``timezone``, the POSIX rule ``/etc/init.d/system`` writes to ``/tmp/TZ`` for libc. The rule is what the clock runs on: an IANA name written there left an AP on UTC (NetOrk/netork#1849). So the name is read as the zone only where the rule in force is that zone's; a rule without a matching name is read as the one zone that has it, or as no zone where several do (``CET-1CEST,...`` is Berlin, Paris, Madrid, ...). Reading is one round trip as the login user: ``uci -q show system``, then netifd's dump (:data:`READ_WITH_DHCP`). A write reads ``uci`` alone. Every write compares first and sends nothing when the router already has the values -- an application aligns on every poll -- else sets the options, commits ``system`` and applies them: ``/etc/init.d/sysntpd restart`` for the servers, ``/etc/init.d/system reload`` and a restart of logd, which reads the zone when it starts, for the zone. Writes run as root (``run_command(privileged=True)``); OpenWrt has no sudo. ``synchronized`` is None: BusyBox ntpd keeps no state another process can read, and ``ubus call system info`` says nothing about the clock's source. """ from __future__ import annotations import json import re import shlex from collections import defaultdict from dataclasses import dataclass, field from functools import lru_cache from importlib.resources import files from typing import TYPE_CHECKING, Any, ClassVar from napalm_device_types import FixStepOutcomeDict from napalm_device_types.posix_tz import is_iana_zone, parse_posix_tz, posix_tz from napalm_device_types.time_sync import TIME_SYNC_VALUES, TimeSyncDict, TimeSyncMixin if TYPE_CHECKING: from napalm_device_types import CommandResult READ_COMMAND = "uci -q show system" #: The line between uci's answer and netifd's in :data:`READ_WITH_DHCP`. DHCP_MARK = "@@napalm-openwrt: network.interface dump" #: ``get_time_sync``'s reading: uci's ``system`` config, then netifd's interfaces, where #: a lease keeps its NTP servers. Its exit code is uci's: ubus may refuse, uci may not. READ_WITH_DHCP = ( f"{READ_COMMAND} && {{ echo {shlex.quote(DHCP_MARK)}; " "ubus call network.interface dump 2>/dev/null || true; }" ) #: The rule ``/etc/init.d/system`` uses where the section names none. DEFAULT_RULE = "UTC" #: A zone of the database that stands for "not known", not for a place. _NO_PLACE = frozenset({"Factory"}) #: ``system.
.