eae879
#!/bin/bash
eae879
#
eae879
# nis.sh: dhclient-script plugin for NIS settings,
eae879
#         place in /etc/dhcp/dhclient.d and 'chmod +x nis.sh' to enable
eae879
#
eae879
# Copyright (C) 2008 Red Hat, Inc.
eae879
#
eae879
# This program is free software; you can redistribute it and/or modify
eae879
# it under the terms of the GNU General Public License as published by
eae879
# the Free Software Foundation; either version 2 of the License, or
eae879
# (at your option) any later version.
eae879
#
eae879
# This program is distributed in the hope that it will be useful,
eae879
# but WITHOUT ANY WARRANTY; without even the implied warranty of
eae879
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
eae879
# GNU General Public License for more details.
eae879
#
eae879
# You should have received a copy of the GNU General Public License
eae879
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
eae879
#
eae879
# Author(s): David Cantrell <dcantrell@redhat.com>
eae879
eae879
CONF=/etc/yp.conf
eae879
SAVECONF=${SAVEDIR}/${CONF##*/}.predhclient.${interface}
eae879
eae879
fix_context() {
eae879
    if [ -x /sbin/restorecon ]; then
eae879
        /sbin/restorecon ${1} >/dev/null 2>&1
eae879
    fi
eae879
}
eae879
eae879
save_config_file() {
eae879
    if [ ! -d ${SAVEDIR} ]; then
eae879
        mkdir -p ${SAVEDIR}
eae879
    fi
eae879
eae879
    if [ -e ${CONF} ]; then
eae879
        # cp+rm instead of mv: preserve SELinux context
eae879
        # rhbz#509240
eae879
        # Do not rely on restorecon.
eae879
        cp -c ${CONF} ${SAVECONF}
eae879
        rm ${CONF}
eae879
    else
eae879
        echo > ${SAVECONF}
eae879
        # Try restorecon
eae879
        fix_context ${SAVECONF}
eae879
    fi
eae879
}
eae879
eae879
nis_config() {
eae879
    if [ ! "${PEERNIS}" = "no" ]; then
eae879
        if [ -n "${new_nis_domain}" ]; then
eae879
            domainname "${new_nis_domain}"
eae879
            save_config_file
eae879
            echo '# generated by /sbin/dhclient-script' > ${CONF}
eae879
            fix_context ${CONF}
eae879
eae879
            if [ -n "${new_nis_servers}" ]; then
eae879
                for i in ${new_nis_servers} ; do
eae879
                    echo "domain ${new_nis_domain} server ${i}" >> ${CONF}
eae879
                done
eae879
            else
eae879
                echo "domain ${new_nis_domain} broadcast" >> ${CONF}
eae879
            fi
eae879
eae879
        elif [ -n "${new_nis_servers}" ]; then
eae879
            save_config_file
eae879
            echo '# generated by /sbin/dhclient-script' > ${CONF}
eae879
            fix_context ${CONF}
eae879
eae879
            for i in ${new_nis_servers} ; do
eae879
                echo "ypserver ${i}" >> ${CONF}
eae879
            done
eae879
eae879
        fi
eae879
eae879
		# domainname or servers changed, restart ypbind
eae879
		if [ "${old_nis_domain}" != "${new_nis_domain}" ] \
eae879
			|| [ "${old_nis_servers}" != "${new_nis_servers}" ]
eae879
		then
eae879
            service ypbind condrestart >/dev/null 2>&1
eae879
		fi
eae879
    fi
eae879
}
eae879
eae879
nis_restore() {
eae879
    if [ ! "${PEERNIS}" = "no" ]; then
eae879
        if [ -f ${SAVECONF} ]; then
eae879
            rm -f ${CONF}
eae879
            # cp+rm instead of mv: preserve SELinux context
eae879
            # rhbz#509240
eae879
            cp -c ${SAVECONF} ${CONF}
eae879
            rm ${SAVECONF}
eae879
            fix_context ${CONF} # Restorecon again to be sure.
eae879
            service ypbind condrestart >/dev/null 2>&1
eae879
        fi
eae879
    fi
eae879
}
eae879
eae879
# Local Variables:
eae879
# indent-tabs-mode: nil
eae879
# sh-basic-offset: 4
eae879
# show-trailing-whitespace: t
eae879
# End: