xref: /dragonfly/contrib/dhcpcd/hooks/50-ntp.conf (revision 7b1120e5)
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 seperate file for DHCP config to avoid stamping on
45# the master.
46if [ "$ntp_service" = ntpd ] && type 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	servers=
66	if [ -n "$interfaces" ]; then
67		# Build the header
68		for x in ${interfaces}; do
69			header="$header${header:+, }$x"
70		done
71
72		# Build a server list
73		srvs=$(cd "$ntp_conf_dir";
74			key_get_value "server " $interfaces)
75		if [ -n "$srvs" ]; then
76			for x in $(uniqify $srvs); do
77				servers="${servers}server $x$NL"
78			done
79		fi
80	fi
81
82	# Merge our config into ntp.conf
83	[ -e "$cf" ] && rm -f "$cf"
84	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
85
86	if [ -n "$NTP_DHCP_CONF" ]; then
87		[ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
88		ntp_conf="$NTP_DHCP_CONF"
89	elif [ -e "$ntp_conf" ]; then
90		remove_markers "$signature_base" "$signature_base_end" \
91			"$ntp_conf" > "$cf"
92	fi
93
94	if [ -n "$servers" ]; then
95		echo "$signature_base${header:+ $from }$header" >> "$cf"
96		printf %s "$servers" >> "$cf"
97		echo "$signature_base_end${header:+ $from }$header" >> "$cf"
98	else
99		[ -e "$ntp_conf" -a -e "$cf" ] || return
100	fi
101
102	# If we changed anything, restart ntpd
103	if change_file "$ntp_conf" "$cf"; then
104		[ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
105	fi
106}
107
108add_ntp_conf()
109{
110	cf="$ntp_conf_dir/$ifname"
111
112	[ -e "$cf" ] && rm "$cf"
113	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
114	if [ -n "$new_ntp_servers" ]; then
115		for x in $new_ntp_servers; do
116			echo "server $x" >> "$cf"
117		done
118	fi
119	build_ntp_conf
120}
121
122remove_ntp_conf()
123{
124	if [ -e "$ntp_conf_dir/$ifname" ]; then
125		rm "$ntp_conf_dir/$ifname"
126	fi
127	build_ntp_conf
128}
129
130# For ease of use, map DHCP6 names onto our DHCP4 names
131case "$reason" in
132BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
133	new_ntp_servers="$new_dhcp6_sntp_servers"
134;;
135esac
136
137if $if_up; then
138	add_ntp_conf
139elif $if_down; then
140	remove_ntp_conf
141fi
142