• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Sample dhcpcd hook script for ntp
2# Like our resolv.conf hook script, we store a database of ntp.conf files
3# and merge into /etc/ntp.conf
4
5# You can set the env var NTP_CONF to another file like this
6#   dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
7# or by adding this to /etc/dhcpcd.enter-hook
8#   NTP_CONF=/usr/pkg/etc/ntpd.conf
9# to use OpenNTPD instead of the default NTP.
10
11if type invoke-rc.d >/dev/null 2>&1; then
12	# Debian has a seperate file for DHCP config to avoid stamping on
13	# the master.
14	[ -e /var/lib/ntp ] || mkdir /var/lib/ntp
15	: ${ntp_service:=ntp}
16	: ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
17fi
18
19: ${ntp_service:=ntpd}
20: ${ntp_restart_cmd:=service_condcommand $ntp_service restart}
21ntp_conf_dir="$state_dir/ntp.conf"
22
23# If we have installed OpenNTPD but not NTP then prefer it
24# XXX If both exist then update both?
25if [ -z "$NTP_CONF" -a -e /etc/ntpd.conf -a ! -e /etc/ntp.conf ]; then
26	: ${NTP_CONF:=/etc/ntpd.conf}
27else
28	: ${NTP_CONF:=/etc/ntp.conf}
29fi
30
31ntp_conf=${NTP_CONF}
32NL="
33"
34
35build_ntp_conf()
36{
37	local cf="$state_dir/ntp.conf.$ifname"
38	local interfaces= header= srvs= servers= x=
39
40	# Build a list of interfaces
41	interfaces=$(list_interfaces "$ntp_conf_dir")
42
43	if [ -n "$interfaces" ]; then
44		# Build the header
45		for x in ${interfaces}; do
46			header="$header${header:+, }$x"
47		done
48
49		# Build a server list
50		srvs=$(cd "$ntp_conf_dir";
51			key_get_value "server " $interfaces)
52		if [ -n "$srvs" ]; then
53			for x in $(uniqify $srvs); do
54				servers="${servers}server $x$NL"
55			done
56		fi
57	fi
58
59	# Merge our config into ntp.conf
60	[ -e "$cf" ] && rm -f "$cf"
61	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
62
63	if [ -n "$NTP_DHCP_CONF" ]; then
64		[ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
65		ntp_conf="$NTP_DHCP_CONF"
66	elif [ -e "$ntp_conf" ]; then
67		remove_markers "$signature_base" "$signature_base_end" \
68			"$ntp_conf" > "$cf"
69	fi
70
71	if [ -n "$servers" ]; then
72		echo "$signature_base${header:+ $from }$header" >> "$cf"
73		printf %s "$servers" >> "$cf"
74		echo "$signature_base_end${header:+ $from }$header" >> "$cf"
75	else
76		[ -e "$ntp_conf" -a -e "$cf" ] || return
77	fi
78
79	# If we changed anything, restart ntpd
80	if change_file "$ntp_conf" "$cf"; then
81		[ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
82	fi
83}
84
85add_ntp_conf()
86{
87	local cf="$ntp_conf_dir/$ifname" x=
88
89	[ -e "$cf" ] && rm "$cf"
90	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
91	if [ -n "$new_ntp_servers" ]; then
92		for x in $new_ntp_servers; do
93			echo "server $x" >> "$cf"
94		done
95	fi
96	build_ntp_conf
97}
98
99remove_ntp_conf()
100{
101	if [ -e "$ntp_conf_dir/$ifname" ]; then
102		rm "$ntp_conf_dir/$ifname"
103	fi
104	build_ntp_conf
105}
106
107# For ease of use, map DHCP6 names onto our DHCP4 names
108case "$reason" in
109BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
110	new_ntp_servers="$new_dhcp6_sntp_servers"
111;;
112esac
113
114if $if_up; then
115	add_ntp_conf
116elif $if_down; then
117	remove_ntp_conf
118fi
119