xref: /dragonfly/contrib/dhcpcd/hooks/50-ntp.conf (revision 80aa9461)
17827cba2SAaron LI# Sample dhcpcd hook script for NTP
27827cba2SAaron LI# It will configure either one of NTP, OpenNTP or Chrony (in that order)
37827cba2SAaron LI# and will default to NTP if no default config is found.
47827cba2SAaron LI
57827cba2SAaron LI# Like our resolv.conf hook script, we store a database of ntp.conf files
67827cba2SAaron LI# and merge into /etc/ntp.conf
77827cba2SAaron LI
87827cba2SAaron LI# You can set the env var NTP_CONF to override the derived default on
97827cba2SAaron LI# systems with >1 NTP client installed.
107827cba2SAaron LI# Here is an example for OpenNTP
117827cba2SAaron LI#   dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
127827cba2SAaron LI# or by adding this to /etc/dhcpcd.conf
137827cba2SAaron LI#   env NTP_CONF=/usr/pkg/etc/ntpd.conf
147827cba2SAaron LI# or by adding this to /etc/dhcpcd.enter-hook
157827cba2SAaron LI#   NTP_CONF=/usr/pkg/etc/ntpd.conf
167827cba2SAaron LI# To use Chrony instead, simply change ntpd.conf to chrony.conf in the
177827cba2SAaron LI# above examples.
187827cba2SAaron LI
197827cba2SAaron LI: ${ntp_confs:=ntp.conf ntpd.conf chrony.conf}
207827cba2SAaron LI: ${ntp_conf_dirs=/etc /usr/pkg/etc /usr/local/etc}
217827cba2SAaron LIntp_conf_dir="$state_dir/ntp.conf"
227827cba2SAaron LI
237827cba2SAaron LI# If NTP_CONF is not set, work out a good default
247827cba2SAaron LIif [ -z "$NTP_CONF" ]; then
257827cba2SAaron LI	for d in ${ntp_conf_dirs}; do
267827cba2SAaron LI		for f in ${ntp_confs}; do
277827cba2SAaron LI			if [ -e "$d/$f" ]; then
287827cba2SAaron LI				NTP_CONF="$d/$f"
297827cba2SAaron LI				break 2
307827cba2SAaron LI			fi
317827cba2SAaron LI		done
327827cba2SAaron LI	done
337827cba2SAaron LI	[ -e "$NTP_CONF" ] || NTP_CONF=/etc/ntp.conf
347827cba2SAaron LIfi
357827cba2SAaron LI
367827cba2SAaron LI# Derive service name from configuration
377827cba2SAaron LIif [ -z "$ntp_service" ]; then
387827cba2SAaron LI	case "$NTP_CONF" in
397827cba2SAaron LI	*chrony.conf)		ntp_service=chronyd;;
407827cba2SAaron LI	*)			ntp_service=ntpd;;
417827cba2SAaron LI	esac
427827cba2SAaron LIfi
437827cba2SAaron LI
446e63cc1fSRoy Marples# Debian has a separate file for DHCP config to avoid stamping on
457827cba2SAaron LI# the master.
46*80aa9461SRoy Marplesif [ "$ntp_service" = ntpd ] && command -v invoke-rc.d >/dev/null 2>&1; then
477827cba2SAaron LI	[ -e /var/lib/ntp ] || mkdir /var/lib/ntp
487827cba2SAaron LI	: ${ntp_service:=ntp}
497827cba2SAaron LI	: ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
507827cba2SAaron LIfi
517827cba2SAaron LI
527827cba2SAaron LI: ${ntp_restart_cmd:=service_condcommand $ntp_service restart}
537827cba2SAaron LI
547827cba2SAaron LIntp_conf=${NTP_CONF}
557827cba2SAaron LINL="
567827cba2SAaron LI"
577827cba2SAaron LI
587827cba2SAaron LIbuild_ntp_conf()
597827cba2SAaron LI{
607827cba2SAaron LI	cf="$state_dir/ntp.conf.$ifname"
617827cba2SAaron LI
627827cba2SAaron LI	# Build a list of interfaces
637827cba2SAaron LI	interfaces=$(list_interfaces "$ntp_conf_dir")
647827cba2SAaron LI
658d36e1dfSRoy Marples	header=
667827cba2SAaron LI	servers=
677827cba2SAaron LI	if [ -n "$interfaces" ]; then
687827cba2SAaron LI		# Build the header
697827cba2SAaron LI		for x in ${interfaces}; do
707827cba2SAaron LI			header="$header${header:+, }$x"
717827cba2SAaron LI		done
727827cba2SAaron LI
737827cba2SAaron LI		# Build a server list
747827cba2SAaron LI		srvs=$(cd "$ntp_conf_dir";
757827cba2SAaron LI			key_get_value "server " $interfaces)
767827cba2SAaron LI		if [ -n "$srvs" ]; then
777827cba2SAaron LI			for x in $(uniqify $srvs); do
787827cba2SAaron LI				servers="${servers}server $x$NL"
797827cba2SAaron LI			done
807827cba2SAaron LI		fi
817827cba2SAaron LI	fi
827827cba2SAaron LI
837827cba2SAaron LI	# Merge our config into ntp.conf
847827cba2SAaron LI	[ -e "$cf" ] && rm -f "$cf"
857827cba2SAaron LI	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
867827cba2SAaron LI
877827cba2SAaron LI	if [ -n "$NTP_DHCP_CONF" ]; then
887827cba2SAaron LI		[ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
897827cba2SAaron LI		ntp_conf="$NTP_DHCP_CONF"
907827cba2SAaron LI	elif [ -e "$ntp_conf" ]; then
917827cba2SAaron LI		remove_markers "$signature_base" "$signature_base_end" \
927827cba2SAaron LI			"$ntp_conf" > "$cf"
937827cba2SAaron LI	fi
947827cba2SAaron LI
957827cba2SAaron LI	if [ -n "$servers" ]; then
967827cba2SAaron LI		echo "$signature_base${header:+ $from }$header" >> "$cf"
977827cba2SAaron LI		printf %s "$servers" >> "$cf"
987827cba2SAaron LI		echo "$signature_base_end${header:+ $from }$header" >> "$cf"
997827cba2SAaron LI	else
1008d36e1dfSRoy Marples		[ -e "$ntp_conf" ] && [ -e "$cf" ] || return
1017827cba2SAaron LI	fi
1027827cba2SAaron LI
1037827cba2SAaron LI	# If we changed anything, restart ntpd
1047827cba2SAaron LI	if change_file "$ntp_conf" "$cf"; then
1057827cba2SAaron LI		[ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
1067827cba2SAaron LI	fi
1077827cba2SAaron LI}
1087827cba2SAaron LI
1097827cba2SAaron LIadd_ntp_conf()
1107827cba2SAaron LI{
1117827cba2SAaron LI	cf="$ntp_conf_dir/$ifname"
1127827cba2SAaron LI
1137827cba2SAaron LI	[ -e "$cf" ] && rm "$cf"
1147827cba2SAaron LI	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
1157827cba2SAaron LI	if [ -n "$new_ntp_servers" ]; then
116*80aa9461SRoy Marples		for x in $(uniqify $new_ntp_servers); do
1177827cba2SAaron LI			echo "server $x" >> "$cf"
1187827cba2SAaron LI		done
1197827cba2SAaron LI	fi
1207827cba2SAaron LI	build_ntp_conf
1217827cba2SAaron LI}
1227827cba2SAaron LI
1237827cba2SAaron LIremove_ntp_conf()
1247827cba2SAaron LI{
1257827cba2SAaron LI	if [ -e "$ntp_conf_dir/$ifname" ]; then
1267827cba2SAaron LI		rm "$ntp_conf_dir/$ifname"
1277827cba2SAaron LI	fi
1287827cba2SAaron LI	build_ntp_conf
1297827cba2SAaron LI}
1307827cba2SAaron LI
1317827cba2SAaron LI# For ease of use, map DHCP6 names onto our DHCP4 names
1327827cba2SAaron LIcase "$reason" in
1337827cba2SAaron LIBOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
134*80aa9461SRoy Marples	new_ntp_servers="$new_dhcp6_sntp_servers $new_dhcp6_ntp_server_addr $new_dhcp6_ntp_server_fqdn"
1357827cba2SAaron LI;;
1367827cba2SAaron LIesac
1377827cba2SAaron LI
138b2927f2bSRoy Marplesif $if_configured; then
1397827cba2SAaron LI	if $if_up; then
1407827cba2SAaron LI		add_ntp_conf
1417827cba2SAaron LI	elif $if_down; then
1427827cba2SAaron LI		remove_ntp_conf
1437827cba2SAaron LI	fi
144b2927f2bSRoy Marplesfi
145