aboutsummaryrefslogtreecommitdiffstats
path: root/dhcpcd-hooks/50-ntp.conf
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpcd-hooks/50-ntp.conf')
-rw-r--r--dhcpcd-hooks/50-ntp.conf87
1 files changed, 59 insertions, 28 deletions
diff --git a/dhcpcd-hooks/50-ntp.conf b/dhcpcd-hooks/50-ntp.conf
index 3772215..536f14e 100644
--- a/dhcpcd-hooks/50-ntp.conf
+++ b/dhcpcd-hooks/50-ntp.conf
@@ -1,4 +1,6 @@
# Sample dhcpcd hook script for ntp
+# Like our resolv.conf hook script, we store a database of ntp.conf files
+# and merge into /etc/ntp.conf
# Detect OpenRC or BSD rc
# Distributions may want to just have their command here instead of this
@@ -10,42 +12,71 @@ elif [ -x /usr/local/etc/rc.d/ntpd ]; then
ntpd_restart_cmd="/usr/local/etc/rc.d/ntpd restart"
fi
-make_ntp_conf()
+ntp_conf_dir="${state_dir}/ntp.conf"
+
+build_ntp_conf()
{
- [ -z "${new_ntp_servers}" ] && return 0
- local cf=/etc/ntp.conf."${interface}" x=
- echo "${signature}" > "${cf}"
- echo "restrict default noquery notrust nomodify" >> "${cf}"
- echo "restrict 127.0.0.1" >> "${cf}"
- for x in ${new_ntp_servers}; do
- echo "restrict ${x} nomodify notrap noquery" >> "${cf}"
- echo "server ${x}" >> "${cf}"
- done
- if [ ! -e /etc/ntp.conf ]; then
- false
- elif type cmp >/dev/null 2>&1; then
- cmp -s /etc/ntp.conf "${cf}"
- elif type diff >/dev/null 2>&1; then
- diff -q /etc/ntp.conf "${cf}" >/dev/null
- else
- false
+ local cf="/etc/ntp.conf.${interface}"
+ local interfaces= header= srvs= servers= x=
+
+ # Build a list of interfaces
+ interfaces=$(list_interfaces "${ntp_conf_dir}")
+
+ if [ -n "${interfaces}" ]; then
+ # Build the header
+ for x in ${interfaces}; do
+ header="${header}${header:+, }${x}"
+ done
+
+ # Build a server list
+ srvs=$(cd "${ntp_conf_dir}";
+ key_get_value "server " ${interfaces})
+ if [ -n "${srvs}" ]; then
+ for x in $(uniqify ${srvs}); do
+ servers="${servers}server ${x}\n"
+ done
+ fi
fi
- if [ $? = 0 ]; then
- rm -f "${cf}"
- else
- save_conf /etc/ntp.conf
- mv -f "${cf}" /etc/ntp.conf
+
+ # Merge our config into ntp.conf
+ [ -e "${cf}" ] && rm -f "${cf}"
+ remove_markers "${signature_base}" "${signature_base_end}" \
+ /etc/ntp.conf > "${cf}"
+ if [ -n "${servers}" ]; then
+ echo "${signature_base}${header:+ ${from} }${header}" >> "${cf}"
+ printf "${search}${servers}" >> "${cf}"
+ echo "${signature_base_end}${header:+ ${from} }${header}" >> "${cf}"
+ fi
+
+ # If we changed anything, restart ntpd
+ if change_file /etc/ntp.conf "${cf}"; then
[ -n "${ntpd_restart_cmd}" ] && ${ntpd_restart_cmd}
fi
}
-restore_ntp_conf()
+add_ntp_conf()
{
- restore_conf /etc/ntp.conf || return 0
- [ -n "${ntpd_restart_cmd}" ] && ${ntpd_restart_cmd}
+ local cf="${ntp_conf_dir}/${interface}" x=
+
+ [ -e "${cf}" ] && rm "${cf}"
+ [ -d "${ntp_conf_dir}" ] || mkdir -p "${ntp_conf_dir}"
+ if [ -n "${new_ntp_servers}" ]; then
+ for x in ${new_ntp_servers}; do
+ echo "server ${x}" >> "${cf}"
+ done
+ fi
+ build_ntp_conf
+}
+
+remove_ntp_conf()
+{
+ if [ -e "${ntp_conf_dir}/${interface}" ]; then
+ rm "${ntp_conf_dir}/${interface}"
+ fi
+ build_ntp_conf
}
case "${reason}" in
-BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) make_ntp_conf;;
-EXPIRE|FAIL|IPV4LL|RELEASE|STOP) restore_ntp_conf;;
+BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) add_ntp_conf add;;
+PREINIT|EXPIRE|FAIL|IPV4LL|RELEASE|STOP) remove_ntp_conf del;;
esac