3 |
- |
1 |
#!/bin/bash
|
|
|
2 |
#
|
|
|
3 |
# ntp.sh: dhclient-script plugin for NTP settings,
|
|
|
4 |
# place in /etc/dhcp/dhclient.d and 'chmod +x ntp.sh' to enable
|
|
|
5 |
#
|
|
|
6 |
# Copyright (C) 2008 Red Hat, Inc.
|
|
|
7 |
#
|
|
|
8 |
# This program is free software; you can redistribute it and/or modify
|
|
|
9 |
# it under the terms of the GNU General Public License as published by
|
|
|
10 |
# the Free Software Foundation; either version 2 of the License, or
|
|
|
11 |
# (at your option) any later version.
|
|
|
12 |
#
|
|
|
13 |
# This program is distributed in the hope that it will be useful,
|
|
|
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
16 |
# GNU General Public License for more details.
|
|
|
17 |
#
|
|
|
18 |
# You should have received a copy of the GNU General Public License
|
|
|
19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20 |
#
|
|
|
21 |
# Author(s): David Cantrell <dcantrell@redhat.com>
|
|
|
22 |
# Miroslav Lichvar <mlichvar@redhat.com>
|
|
|
23 |
#
|
|
|
24 |
|
|
|
25 |
CONF=/etc/ntp.conf
|
|
|
26 |
SAVECONF=${SAVEDIR}/${CONF##*/}.predhclient.${interface}
|
|
|
27 |
|
|
|
28 |
ntp_replace_conf() {
|
|
|
29 |
echo "$1" | diff -q ${CONF} - > /dev/null 2>&1
|
|
|
30 |
if [ $? -eq 1 ]; then
|
|
|
31 |
echo "$1" > ${CONF}
|
|
|
32 |
restorecon ${CONF} >/dev/null 2>&1
|
|
|
33 |
service ntpd condrestart >/dev/null 2>&1
|
|
|
34 |
fi
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
ntp_config() {
|
|
|
38 |
if [ ! "${PEERNTP}" = "no" ] && [ -n "${new_ntp_servers}" ] &&
|
|
|
39 |
[ -e ${CONF} ] && [ -d ${SAVEDIR} ]; then
|
|
|
40 |
local conf=$(egrep -v '^server .* # added by /sbin/dhclient-script$' < ${CONF})
|
|
|
41 |
local unique_servers=$(comm -23 \
|
|
|
42 |
<(for s in ${new_ntp_servers}; do echo $s; done | sort -u) \
|
|
|
43 |
<(echo "$conf" | awk '$1=="peer"||$1=="server"{print $2}' | sort -u))
|
|
|
44 |
|
|
|
45 |
conf=$(echo "$conf"
|
|
|
46 |
for s in ${unique_servers}; do
|
|
|
47 |
echo "server ${s} # added by /sbin/dhclient-script"
|
|
|
48 |
done)
|
|
|
49 |
|
|
|
50 |
[ -f ${SAVECONF} ] || touch ${SAVECONF}
|
|
|
51 |
ntp_replace_conf "$conf"
|
|
|
52 |
fi
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
ntp_restore() {
|
|
|
56 |
if [ -e ${CONF} ] && [ -f ${SAVECONF} ]; then
|
|
|
57 |
local conf=$(egrep -v '^server .* # added by /sbin/dhclient-script$' < ${CONF})
|
|
|
58 |
|
|
|
59 |
ntp_replace_conf "$conf"
|
|
|
60 |
rm -f ${SAVECONF}
|
|
|
61 |
fi
|
|
|
62 |
}
|