b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | # Copyright (C) 2006-2015 OpenWrt.org |
| 3 | |
| 4 | START=15 |
| 5 | USE_PROCD=1 |
| 6 | PROG=/usr/sbin/chronyd |
| 7 | CONFIGFILE=/etc/chrony/chrony.conf |
| 8 | INCLUDEFILE=/var/etc/chrony.d/10-uci.conf |
| 9 | RTCDEVICE=/dev/rtc0 |
| 10 | |
| 11 | handle_source() { |
| 12 | local cfg=$1 sourcetype=$2 disabled hostname minpoll maxpoll iburst nts |
| 13 | |
| 14 | config_get_bool disabled "$cfg" disabled 0 |
| 15 | [ "$disabled" = "1" ] && return |
| 16 | hostname=$NTP_SOURCE_HOSTNAME |
| 17 | [ -z "$hostname" ] && config_get hostname "$cfg" hostname |
| 18 | [ -z "$hostname" ] && return |
| 19 | config_get minpoll "$cfg" minpoll |
| 20 | config_get maxpoll "$cfg" maxpoll |
| 21 | config_get_bool iburst "$cfg" iburst 0 |
| 22 | config_get_bool nts "$cfg" nts 0 |
| 23 | echo $( |
| 24 | echo $sourcetype $hostname |
| 25 | [ -n "$minpoll" ] && echo minpoll $minpoll |
| 26 | [ -n "$maxpoll" ] && echo maxpoll $maxpoll |
| 27 | [ "$iburst" = "1" ] && echo iburst |
| 28 | [ "$nts" = "1" ] && echo nts |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | handle_allow() { |
| 33 | local cfg=$1 iface wan_iface wan6_iface subnet subnets subnets6 |
| 34 | |
| 35 | network_find_wan wan_iface true |
| 36 | network_find_wan6 wan6_iface true |
| 37 | config_get iface "$cfg" interface |
| 38 | |
| 39 | if [ "$wan_iface" = "$iface" ]; then |
| 40 | echo allow 0/0 |
| 41 | elif [ "$wan6_iface" = "$iface" ]; then |
| 42 | echo allow ::/0 |
| 43 | else |
| 44 | network_get_subnets subnets $iface |
| 45 | network_get_subnets6 subnets6 $iface |
| 46 | for subnet in $subnets $subnets6; do |
| 47 | echo allow $subnet |
| 48 | done |
| 49 | fi |
| 50 | } |
| 51 | |
| 52 | handle_makestep() { |
| 53 | local cfg=$1 threshold limit |
| 54 | |
| 55 | config_get threshold "$cfg" threshold |
| 56 | config_get limit "$cfg" limit |
| 57 | [ -z "$threshold" -o -z "$limit" ] && return |
| 58 | echo makestep $threshold $limit |
| 59 | } |
| 60 | |
| 61 | handle_nts() { |
| 62 | local cfg=$1 threshold limit |
| 63 | |
| 64 | config_get_bool rtccheck "$cfg" rtccheck 0 |
| 65 | config_get_bool systemcerts "$cfg" systemcerts 1 |
| 66 | config_get trustedcerts "$cfg" trustedcerts |
| 67 | # Disable certificate time checks if no RTC is present |
| 68 | [ "$rtccheck" = "1" ] && ! [ -c $RTCDEVICE ] && echo nocerttimecheck 1 |
| 69 | [ "$systemcerts" = "0" ] && echo nosystemcert |
| 70 | [ -n "$trustedcerts" ] && echo ntstrustedcerts "$trustedcerts" |
| 71 | } |
| 72 | |
| 73 | start_service() { |
| 74 | . /lib/functions/network.sh |
| 75 | |
| 76 | procd_open_instance |
| 77 | procd_set_param command $PROG -n |
| 78 | procd_set_param file $CONFIGFILE |
| 79 | procd_set_param file $INCLUDEFILE |
| 80 | procd_close_instance |
| 81 | |
| 82 | config_load chrony |
| 83 | mkdir -p $(dirname $INCLUDEFILE) |
| 84 | |
| 85 | ( |
| 86 | config_foreach handle_source server server |
| 87 | config_foreach handle_source pool pool |
| 88 | config_foreach handle_source peer peer |
| 89 | config_foreach handle_allow allow |
| 90 | config_foreach handle_makestep makestep |
| 91 | config_foreach handle_nts nts |
| 92 | ) > $INCLUDEFILE |
| 93 | } |