#!/bin/bash
#
# named           This shell script takes care of starting and stopping
#                 named (BIND DNS server).
#
# chkconfig: - 13 87
# description: named (BIND) is a Domain Name Server (DNS) \
# that is used to resolve host names to IP addresses.
# probe: true

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

[ -n "$ROOTDIR" ] && ROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`

RETVAL=0
prog="named"

[ -r /etc/sysconfig/named ] && . /etc/sysconfig/named

nmdcOption()
{ let i=0; 
  for a in $*; 
  do ((++i)); 
     if [ $a = -c ]; then 
	 ((++i)); 
	 eval 'echo $'$i;
     elif [[ $a = -c* ]]; then 
	 echo ${a#-c}; 
     fi; 
  done; 
}

named_conf=`nmdcOption $OPTIONS`;

start() {
	[ -x /usr/sbin/named ] || exit 5

	[ -r ${ROOTDIR}/${named_conf:-etc/named.conf} ] || exit 6

        # Start daemons.
        echo -n $"Starting $prog: "
	if [ -n "`/sbin/pidof named`" ]; then
		echo -n $"$prog: already running"
		failure
		echo
		return 1
	fi
	ckcf_options='';
	if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
		OPTIONS="${OPTIONS} -t ${ROOTDIR}"
		ckcf_options="-t ${ROOTDIR}";
		if [ -s /etc/localtime ]; then
		    cp -fp /etc/localtime ${ROOTDIR}/etc/localtime
		fi;
		if [ ! -d ${ROOTDIR}/proc ]; then
		    mkdir -p ${ROOTDIR}/proc
		fi
		if ! egrep -q '^/proc[[:space:]]+'${ROOTDIR}'/proc' /proc/mounts; then
		    mount --bind -n /proc ${ROOTDIR}/proc >/dev/null 2>&1 
		fi
	fi
	no_write_master_zones=0
	if [ -e /etc/selinux/config ]; then
           .  /etc/selinux/config
           if [ "$SELINUX" != 'disabled' ] && [ "$SELINUXTYPE" != "" ] && [ -d /etc/selinux/${SELINUXTYPE} ] && [ -e /etc/selinux/${SELINUXTYPE}/booleans  ]; then
	       . /etc/selinux/${SELINUXTYPE}/booleans 
	       if echo "$named_write_master_zones" | /bin/egrep -q '^[0-9]+$'; then
		   if [ "$named_write_master_zones" -eq 1 ] ; then
		       /bin/chown -f --from=root:named named:named $ROOTDIR/var/named
		   elif [ "$named_write_master_zones" -eq 0 ] ; then
		       /bin/chown -f --from=named:named root:named $ROOTDIR/var/named
		   fi;
               fi;
	   else
	       no_write_master_zones=1
	   fi;
	else
	    no_write_master_zones=1
        fi;
	if [ "$no_write_master_zones" -eq 1 ]; then
	    if [[ "$ENABLE_ZONE_WRITE" =  [yY1]* ]]; then
		/bin/chown -f --from=root:named named:named $ROOTDIR/var/named
	    elif [[ "$ENABLE_ZONE_WRITE" =  [nN0]* ]]; then	        
		/bin/chown -f --from=named:named root:named $ROOTDIR/var/named
	    fi;
	fi
        conf_ok=0;
	if [ -x /usr/sbin/named-checkconf ] && /usr/sbin/named-checkconf $ckcf_options $named_conf >/dev/null 2>&1; then
           conf_ok=1;
        else
	   RETVAL=$?;
	fi
	if [ $conf_ok -eq 1 ]; then	   
	   daemon /usr/sbin/named -u named ${OPTIONS};
	   RETVAL=$?;
	   if [ $RETVAL -eq 0 ]; then
	       rm -f /var/run/named.pid
	       ln -s $ROOTDIR/var/run/named/named.pid /var/run/named.pid;
	   fi;
	else
	   named_err="`/usr/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";	   
	   echo
	   echo $"Error in named configuration"':';
	   echo "$named_err";
	   failure
	   echo
	   if [ -x /usr/bin/logger ]; then
	       echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed 
           fi;
           return 6;
        fi;
 	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/named 
        echo
	[ $RETVAL -eq 0 ] || RETVAL=1
	return $RETVAL
}
stop() {
        # Stop daemons.
        echo -n $"Stopping $prog: "
	/usr/sbin/rndc stop >/dev/null 2>&1 
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    rm -f /var/lock/subsys/named
	    rm -f /var/run/named.pid	    
	elif pidof named >/dev/null; then
	    killproc named -TERM >/dev/null 2>&1
	    RETVAL=$?
	    if [ $RETVAL -eq 0 ]; then
		rm -f /var/lock/subsys/named
		rm -f /var/run/named.pid
	    fi;
	fi;
        if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
                if egrep -q '^/proc[[:space:]]+'${ROOTDIR}'/proc' /proc/mounts; then
                    umount ${ROOTDIR}/proc >/dev/null 2>&1
                fi;
        fi;
	if [ $RETVAL -eq 0 ]; then
	    success
	else
	    failure
        fi;
	echo
	[ $RETVAL -eq 0 ] || RETVAL=1
	return $RETVAL
}
rhstatus() {
	/usr/sbin/rndc status
	status /usr/sbin/$prog
	return $?
}	
restart() {
	stop
# wait a couple of seconds for the named to finish closing down
	sleep 2
	start
}	
reload() {
        echo -n $"Reloading $prog: "
	p=`/sbin/pidof -o %PPID named`	
	RETVAL=$?
	if [ "$RETVAL" -eq 0 ]; then 
	    /usr/sbin/rndc reload >/dev/null 2>&1 || /bin/kill -HUP $p;
	    RETVAL=$?
        fi
	[ "$RETVAL" -eq 0 ] && success $"$prog reload" || failure $"$prog reload"
        echo
	return $?
}
probe() {
	# named knows how to reload intelligently; we don't want linuxconf
	# to offer to restart every time
	/usr/sbin/rndc reload >/dev/null 2>&1 || echo start
	return $?
}
checkconfig() {
        ckcf_options=''
	if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
		OPTIONS="${OPTIONS} -t ${ROOTDIR}"
		ckcf_options="$ckcf_options -t ${ROOTDIR}";  
	fi;
	if [ -x /usr/sbin/named-checkconf ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf}; then
	    return 0;
	else
	    return 6;
	fi
}
# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		rhstatus
		;;
	restart)
		restart
		;;
	condrestart)
		if [ -e /var/lock/subsys/named ]; then restart; fi
		;;
	reload)
		reload
		;;
	probe)
		probe
		;;
        checkconfig|configtest|check|test)
	        checkconfig
		;;
	*)
        	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|probe|configtest}"
		exit 3
esac

exit $?

