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 from pkgsrc instead of the system provided ntp. |
---|
10 | |
---|
11 | if 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} |
---|
17 | fi |
---|
18 | |
---|
19 | : ${ntp_service:=ntpd} |
---|
20 | : ${ntp_restart_cmd:=service_condcommand $ntp_service restart} |
---|
21 | ntp_conf_dir="$state_dir/ntp.conf" |
---|
22 | ntp_conf=${NTP_CONF:-/etc/ntp.conf} |
---|
23 | NL=" |
---|
24 | " |
---|
25 | |
---|
26 | build_ntp_conf() |
---|
27 | { |
---|
28 | local cf="$state_dir/ntp.conf.$ifname" |
---|
29 | local interfaces= header= srvs= servers= x= |
---|
30 | |
---|
31 | # Build a list of interfaces |
---|
32 | interfaces=$(list_interfaces "$ntp_conf_dir") |
---|
33 | |
---|
34 | if [ -n "$interfaces" ]; then |
---|
35 | # Build the header |
---|
36 | for x in ${interfaces}; do |
---|
37 | header="$header${header:+, }$x" |
---|
38 | done |
---|
39 | |
---|
40 | # Build a server list |
---|
41 | srvs=$(cd "$ntp_conf_dir"; |
---|
42 | key_get_value "server " $interfaces) |
---|
43 | if [ -n "$srvs" ]; then |
---|
44 | for x in $(uniqify $srvs); do |
---|
45 | servers="${servers}server $x$NL" |
---|
46 | done |
---|
47 | fi |
---|
48 | fi |
---|
49 | |
---|
50 | # Merge our config into ntp.conf |
---|
51 | [ -e "$cf" ] && rm -f "$cf" |
---|
52 | [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir" |
---|
53 | |
---|
54 | if [ -n "$NTP_DHCP_CONF" ]; then |
---|
55 | [ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf" |
---|
56 | ntp_conf="$NTP_DHCP_CONF" |
---|
57 | elif [ -e "$ntp_conf" ]; then |
---|
58 | remove_markers "$signature_base" "$signature_base_end" \ |
---|
59 | "$ntp_conf" > "$cf" |
---|
60 | fi |
---|
61 | |
---|
62 | if [ -n "$servers" ]; then |
---|
63 | echo "$signature_base${header:+ $from }$header" >> "$cf" |
---|
64 | printf %s "$servers" >> "$cf" |
---|
65 | echo "$signature_base_end${header:+ $from }$header" >> "$cf" |
---|
66 | else |
---|
67 | [ -e "$ntp_conf" -a -e "$cf" ] || return |
---|
68 | fi |
---|
69 | |
---|
70 | # If we changed anything, restart ntpd |
---|
71 | if change_file "$ntp_conf" "$cf"; then |
---|
72 | [ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd |
---|
73 | fi |
---|
74 | } |
---|
75 | |
---|
76 | add_ntp_conf() |
---|
77 | { |
---|
78 | local cf="$ntp_conf_dir/$ifname" x= |
---|
79 | |
---|
80 | [ -e "$cf" ] && rm "$cf" |
---|
81 | [ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir" |
---|
82 | if [ -n "$new_ntp_servers" ]; then |
---|
83 | for x in $new_ntp_servers; do |
---|
84 | echo "server $x" >> "$cf" |
---|
85 | done |
---|
86 | fi |
---|
87 | build_ntp_conf |
---|
88 | } |
---|
89 | |
---|
90 | remove_ntp_conf() |
---|
91 | { |
---|
92 | if [ -e "$ntp_conf_dir/$ifname" ]; then |
---|
93 | rm "$ntp_conf_dir/$ifname" |
---|
94 | fi |
---|
95 | build_ntp_conf |
---|
96 | } |
---|
97 | |
---|
98 | # For ease of use, map DHCP6 names onto our DHCP4 names |
---|
99 | case "$reason" in |
---|
100 | BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6) |
---|
101 | new_ntp_servers="$new_dhcp6_sntp_servers" |
---|
102 | ;; |
---|
103 | esac |
---|
104 | |
---|
105 | if $if_up; then |
---|
106 | add_ntp_conf add |
---|
107 | elif $if_down; then |
---|
108 | remove_ntp_conf del |
---|
109 | fi |
---|