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