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