Blame SOURCES/ntp.dhclient

2b78f7
#!/bin/bash
2b78f7
#
2b78f7
# ntp.sh: dhclient-script plugin for NTP settings,
2b78f7
#         place in /etc/dhcp/dhclient.d and 'chmod +x ntp.sh' to enable
2b78f7
#
2b78f7
# Copyright (C) 2008 Red Hat, Inc.
2b78f7
#
2b78f7
# This program is free software; you can redistribute it and/or modify
2b78f7
# it under the terms of the GNU General Public License as published by
2b78f7
# the Free Software Foundation; either version 2 of the License, or
2b78f7
# (at your option) any later version.
2b78f7
#
2b78f7
# This program is distributed in the hope that it will be useful,
2b78f7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2b78f7
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2b78f7
# GNU General Public License for more details.
2b78f7
#
2b78f7
# You should have received a copy of the GNU General Public License
2b78f7
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2b78f7
#
2b78f7
# Author(s): David Cantrell <dcantrell@redhat.com>
2b78f7
#            Miroslav Lichvar <mlichvar@redhat.com>
2b78f7
#
2b78f7
2b78f7
CONF=/etc/ntp.conf
2b78f7
SAVECONF=${SAVEDIR}/${CONF##*/}.predhclient.${interface}
2b78f7
2b78f7
ntp_replace_conf() {
2b78f7
        echo "$1" | diff -q ${CONF} - > /dev/null 2>&1
2b78f7
        if [ $? -eq 1 ]; then
2b78f7
            echo "$1" > ${CONF}
2b78f7
            restorecon ${CONF} >/dev/null 2>&1
2b78f7
            systemctl try-restart ntpd.service > /dev/null 2>&1 ||
2b78f7
                service ntpd condrestart > /dev/null 2>&1
2b78f7
        fi
2b78f7
}
2b78f7
2b78f7
ntp_config() {
2b78f7
    if [ ! "${PEERNTP}" = "no" ] && [ -n "${new_ntp_servers}" ] &&
2b78f7
        [ -e ${CONF} ] && [ -d ${SAVEDIR} ]; then
2b78f7
        local conf=$(grep -v '^server .*  # added by /sbin/dhclient-script$' < ${CONF})
2b78f7
        local unique_servers=$(comm -23 \
2b78f7
            <(for s in ${new_ntp_servers}; do echo $s; done | sort -u) \
2b78f7
            <(echo "$conf" | awk '$1=="peer"||$1=="server"{print $2}' | sort -u))
2b78f7
2b78f7
        conf=$(echo "$conf"
2b78f7
            for s in ${unique_servers}; do
2b78f7
                echo "server ${s} ${NTPSERVERARGS}  # added by /sbin/dhclient-script"
2b78f7
            done)
2b78f7
2b78f7
        [ -f ${SAVECONF} ] || touch ${SAVECONF}
2b78f7
        ntp_replace_conf "$conf"
2b78f7
    fi
2b78f7
}
2b78f7
2b78f7
ntp_restore() {
2b78f7
    if [ -e ${CONF} ] && [ -f ${SAVECONF} ]; then
2b78f7
        local conf=$(grep -v '^server .*  # added by /sbin/dhclient-script$' < ${CONF})
2b78f7
2b78f7
        ntp_replace_conf "$conf"
2b78f7
        rm -f ${SAVECONF}
2b78f7
    fi
2b78f7
}