xref: /dragonfly/contrib/dhcpcd/hooks/50-ntp.conf (revision f984587a)
1# Sample dhcpcd hook script for NTP
2# It will configure either one of NTP, OpenNTP or Chrony (in that order)
3# and will default to NTP if no default config is found.
4
5# Like our resolv.conf hook script, we store a database of ntp.conf files
6# and merge into /etc/ntp.conf
7
8# You can set the env var NTP_CONF to override the derived default on
9# systems with >1 NTP client installed.
10# Here is an example for OpenNTP
11#   dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
12# or by adding this to /etc/dhcpcd.conf
13#   env NTP_CONF=/usr/pkg/etc/ntpd.conf
14# or by adding this to /etc/dhcpcd.enter-hook
15#   NTP_CONF=/usr/pkg/etc/ntpd.conf
16# To use Chrony instead, simply change ntpd.conf to chrony.conf in the
17# above examples.
18
19: ${ntp_confs:=ntp.conf ntpd.conf chrony.conf}
20: ${ntp_conf_dirs=/etc /usr/pkg/etc /usr/local/etc}
21ntp_conf_dir="$state_dir/ntp.conf"
22
23# If NTP_CONF is not set, work out a good default
24if [ -z "$NTP_CONF" ]; then
25	for d in ${ntp_conf_dirs}; do
26		for f in ${ntp_confs}; do
27			if [ -e "$d/$f" ]; then
28				NTP_CONF="$d/$f"
29				break 2
30			fi
31		done
32	done
33	[ -e "$NTP_CONF" ] || NTP_CONF=/etc/ntp.conf
34fi
35
36# Derive service name from configuration
37if [ -z "$ntp_service" ]; then
38	case "$NTP_CONF" in
39	*chrony.conf)		ntp_service=chronyd;;
40	*)			ntp_service=ntpd;;
41	esac
42fi
43
44# Debian has a separate file for DHCP config to avoid stamping on
45# the master.
46if [ "$ntp_service" = ntpd ] && command -v invoke-rc.d >/dev/null 2>&1; then
47	[ -e /var/lib/ntp ] || mkdir /var/lib/ntp
48	: ${ntp_service:=ntp}
49	: ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
50fi
51
52: ${ntp_restart_cmd:=service_condcommand $ntp_service restart}
53
54ntp_conf=${NTP_CONF}
55NL="
56"
57
58build_ntp_conf()
59{
60	cf="$state_dir/ntp.conf.$ifname"
61
62	# Build a list of interfaces
63	interfaces=$(list_interfaces "$ntp_conf_dir")
64
65	header=
66	servers=
67	if [ -n "$interfaces" ]; then
68		# Build the header
69		for x in ${interfaces}; do
70			header="$header${header:+, }$x"
71		done
72
73		# Build a server list
74		srvs=$(cd "$ntp_conf_dir";
75			key_get_value "server " $interfaces)
76		if [ -n "$srvs" ]; then
77			for x in $(uniqify $srvs); do
78				servers="${servers}server $x$NL"
79			done
80		fi
81	fi
82
83	# Merge our config into ntp.conf
84	[ -e "$cf" ] && rm -f "$cf"
85	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
86
87	if [ -n "$NTP_DHCP_CONF" ]; then
88		[ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
89		ntp_conf="$NTP_DHCP_CONF"
90	elif [ -e "$ntp_conf" ]; then
91		remove_markers "$signature_base" "$signature_base_end" \
92			"$ntp_conf" > "$cf"
93	fi
94
95	if [ -n "$servers" ]; then
96		echo "$signature_base${header:+ $from }$header" >> "$cf"
97		printf %s "$servers" >> "$cf"
98		echo "$signature_base_end${header:+ $from }$header" >> "$cf"
99	else
100		[ -e "$ntp_conf" ] && [ -e "$cf" ] || return
101	fi
102
103	# If we changed anything, restart ntpd
104	if change_file "$ntp_conf" "$cf"; then
105		[ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
106	fi
107}
108
109add_ntp_conf()
110{
111	cf="$ntp_conf_dir/$ifname"
112
113	[ -e "$cf" ] && rm "$cf"
114	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
115	if [ -n "$new_ntp_servers" ]; then
116		for x in $(uniqify $new_ntp_servers); do
117			echo "server $x" >> "$cf"
118		done
119	fi
120	build_ntp_conf
121}
122
123remove_ntp_conf()
124{
125	if [ -e "$ntp_conf_dir/$ifname" ]; then
126		rm "$ntp_conf_dir/$ifname"
127	fi
128	build_ntp_conf
129}
130
131# For ease of use, map DHCP6 names onto our DHCP4 names
132case "$reason" in
133BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
134	new_ntp_servers="$new_dhcp6_sntp_servers $new_dhcp6_ntp_server_addr $new_dhcp6_ntp_server_fqdn"
135;;
136esac
137
138if $if_configured; then
139	if $if_up; then
140		add_ntp_conf
141	elif $if_down; then
142		remove_ntp_conf
143	fi
144fi
145