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