summaryrefslogtreecommitdiffstats
path: root/net/chrony/files/chronyd.init
blob: 85de475dffe0e3a93a214f73bd51e615db43e599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2015 OpenWrt.org

START=15
USE_PROCD=1
PROG=/usr/sbin/chronyd
HOTPLUG=/usr/sbin/chrony-hotplug
CONFIGFILE=/etc/chrony/chrony.conf
INCLUDEFILE=/var/etc/chrony.d/10-uci.conf
RTCDEVICE=/dev/rtc0

handle_source() {
	local cfg=$1 sourcetype=$2 disabled hostname minpoll maxpoll iburst nts
	local prefer xleave maxdelay mindelay maxsamples minsamples port ntsport maxsources

	config_get_bool disabled "$cfg" disabled 0
	[ "$disabled" = "1" ] && return
	hostname=$NTP_SOURCE_HOSTNAME
	[ -z "$hostname" ] && config_get hostname "$cfg" hostname
	[ -z "$hostname" ] && return
	config_get minpoll "$cfg" minpoll
	config_get maxpoll "$cfg" maxpoll
	config_get_bool iburst "$cfg" iburst 0
	config_get_bool nts "$cfg" nts 0
	config_get_bool prefer "$cfg" prefer 0
	config_get_bool xleave "$cfg" xleave 0
	config_get maxdelay "$cfg" maxdelay
	config_get mindelay "$cfg" mindelay
	config_get maxsamples "$cfg" maxsamples
	config_get minsamples "$cfg" minsamples
	config_get port "$cfg" port
	config_get ntsport "$cfg" ntsport
	config_get maxsources "$cfg" maxsources
	echo $(
		echo $sourcetype $hostname
		[ -n "$minpoll" ] && echo minpoll $minpoll
		[ -n "$maxpoll" ] && echo maxpoll $maxpoll
		[ "$iburst" = "1" ] && echo iburst
		[ "$nts" = "1" ] && echo nts
		[ "$prefer" = "1" ] && echo prefer
		[ "$xleave" = "1" ] && echo xleave
		[ -n "$maxdelay" ] && echo maxdelay $maxdelay
		[ -n "$mindelay" ] && echo mindelay $mindelay
		[ -n "$maxsamples" ] && echo maxsamples "$maxsamples"
		[ -n "$minsamples" ] && echo minsamples "$minsamples"
		[ -n "$port" ] && [ "$nts" = "0" ] && echo port $port
		[ -n "$ntsport" ] && [ "$nts" = "1" ] && echo ntsport $ntsport
		[ -n "$maxsources" ] && [ "$cfg" = "pool" ] && echo maxsources $maxsources
	)
}

handle_allow() {
	local cfg=$1 iface wan_iface wan6_iface subnet subnets subnets6

	network_find_wan wan_iface true
	network_find_wan6 wan6_iface true
	config_get ifaces "$cfg" interface

	for iface in $ifaces; do
		if [ "$wan_iface" = "$iface" ]; then
			echo allow 0/0
		elif [ "$wan6_iface" = "$iface" ]; then
			echo allow ::/0
		else
			network_get_subnets subnets $iface
			network_get_subnets6 subnets6 $iface
			for subnet in $subnets $subnets6; do
				echo allow $subnet
			done
		fi
	done
}

handle_makestep() {
	local cfg=$1 threshold limit

	config_get threshold "$cfg" threshold
	config_get limit "$cfg" limit
	[ -z "$threshold" -o -z "$limit" ] && return
	echo makestep $threshold $limit
}

handle_nts() {
	local cfg=$1 rtccheck systemcerts trustedcerts

	config_get_bool rtccheck "$cfg" rtccheck 0
	config_get_bool systemcerts "$cfg" systemcerts 1
	config_get trustedcerts "$cfg" trustedcerts
	# Disable certificate time checks if no RTC is present
	[ "$rtccheck" = "1" ] && ! [ -c $RTCDEVICE ] && echo nocerttimecheck 1
	[ "$systemcerts" = "0" ] && echo nosystemcert
	[ -n "$trustedcerts" ] && echo ntstrustedcerts "$trustedcerts"
}

handle_smoothtime() {
	local cfg=$1 maxppm maxwander leaponly suffix
	config_get maxppm "$cfg" maxppm
	config_get maxwander "$cfg" maxwander
	config_get_bool leaponly "$cfg" leaponly 0
	[ "$leaponly" = "1" ] && suffix=leaponly
	[ -n "$maxppm" ] && [ -n "$maxwander" ] && echo smoothtime "$maxppm" "$maxwander" "$suffix"
}

handle_systemclock() {
	# system clock specific settings
	local cfg=$1 precision leapsecmode
	config_get precision "$cfg" precision
	config_get leapsecmode "$cfg" leapsecmode
	[ -n "$precision" ] && echo clockprecision "$precision"
	[ -n "$leapsecmode" ] && echo clockleapsecmode "$leapsecmode"
}

handle_logging() {
	local cfg=$1 logchange

	config_get logchange "$cfg" logchange
	[ -n "$logchange" ] && echo logchange "$logchange"
}

start_service() {
	. /lib/functions/network.sh

	procd_open_instance
	procd_set_param command $PROG -n
	procd_set_param file $CONFIGFILE
	procd_set_param file $INCLUDEFILE
	procd_close_instance

	procd_open_instance
	procd_set_param command $HOTPLUG
	procd_close_instance

	config_load chrony
	mkdir -p $(dirname $INCLUDEFILE)

	(
		config_foreach handle_source server server
		config_foreach handle_source pool pool
		config_foreach handle_source peer peer
		config_foreach handle_allow allow
		config_foreach handle_makestep makestep
		config_foreach handle_smoothtime smoothtime
		config_foreach handle_systemclock systemclock
		config_foreach handle_nts nts
		config_foreach handle_logging logging
	) > $INCLUDEFILE
}