Blob Blame History Raw
#!/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

### BEGIN INIT INFO
# Provides: $named
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start|stop|status|restart|try-restart|reload|force-reload DNS server
# Description: control ISC BIND implementation of DNS server
### END INIT INFO

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

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

RETVAL=0
export KRB5_KTNAME=${KEYTAB_FILE:-/etc/named.keytab}

# Don't kill named during clean-up
NAMED_SHUTDOWN_TIMEOUT=${NAMED_SHUTDOWN_TIMEOUT:-100}

if [ -n "$ROOTDIR" ]; then
   ROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`;
   rdl=`/usr/bin/readlink $ROOTDIR`;
   if [ -n "$rdl" ]; then
      ROOTDIR="$rdl";
   fi;
fi

# Manage forwarders through D-BUS?
for a in $OPTIONS; do
  if [ $a  = "-D" ]; then
    dbusEnabled=1;
  fi;
done

if [ -z $dbusEnabled ] && [ -z "$DISABLE_NAMED_DBUS" ] ; then
  for l in 0 1 2 3 4 5 6; do
    if /sbin/chkconfig --level=$l NetworkManager >/dev/null 2>&1; then
      dbusEnabled=1;
    fi;
  done
  [ -z $dbusEnabled ] || OPTIONS="$OPTIONS -D";
fi;

# Check if all what named needs running
start()
{
  # Source networking configuration.
  [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

  # Check that networking is up
  [ "${NETWORKING}" = "no" ] && exit 1


  [ -x /usr/sbin/named ] || exit 5

  # Handle -c option
  previous_option='unspecified';
  for a in $OPTIONS; do
    if [ $previous_option = '-c' ]; then
      named_conf=$a;
    fi;
    previous_option=$a;
  done;

  named_conf=${named_conf:-/etc/named.conf};

  if [ ! -r $named_conf ]; then
    echo 'Cannot find configuration file. You could create it by system-config-bind'
    exit 6;
  fi;

  # all pre-start is done, lets start named
  echo -n $"Starting named: "
  if [ -n "`/sbin/pidof -o %PPID named`" ]; then
    echo -n $"named: already running"
    failure
    echo
    exit 1;
  fi;

  ckcf_options='-z'; # enable named-checkzone for each zone (9.3.1+) !
  if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
    OPTIONS="${OPTIONS} -t ${ROOTDIR}"
    ckcf_options="$ckcf_options -t ${ROOTDIR}";
    [ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;
    [ -d ${ROOTDIR}/proc ] || mkdir -p ${ROOTDIR}/proc;
    if ! egrep -q '^/proc[[:space:]]+'${ROOTDIR}'/proc' /proc/mounts; then
      mount --bind -n /proc ${ROOTDIR}/proc >/dev/null 2>&1;
    fi
    if [ ! -z $dbusEnabled ]; then
      if ! egrep -q '^/[^[:space:]]+[[:space:]]+'${ROOTDIR}'/var/run/dbus' /proc/mounts; then
        mkdir -p ${ROOTDIR}/var/run/dbus
        [ -d /var/run/dbus ] || mkdir -p /var/run/dbus;
	mount --bind -n /var/run/dbus ${ROOTDIR}/var/run/dbus > /dev/null 2>&1;
      fi;
    fi;
  fi

  RETVAL=0
  # check if configuration is correct
  if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; then

    daemon /usr/sbin/named -u named ${OPTIONS};
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      rm -f /var/run/named.pid;
      rm -f /var/run/named_sdb.pid;
      ln -s $ROOTDIR/var/run/named/named.pid /var/run/named.pid;
    fi;

    if [ -n "`/sbin/pidof -o %PPID named`" ]; then
      # Verify that named actually started (JM 2006-10-04)
      if [ ! -e $ROOTDIR/var/run/named/named.pid ]; then
      # If there is not a file containing the PID of the now running named daemon then create it (JM 2006-10-04)
        echo `/sbin/pidof -o %PPID named` > $ROOTDIR/var/run/named/named.pid;
      fi;
    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
    [ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;
    exit 2;
  fi;
  echo
  if [ $RETVAL -eq 0 ]; then
    touch /var/lock/subsys/named;
  else
    exit 7;
  fi
  return 0;
}

stop() {
  # Stop daemons.
  echo -n $"Stopping named: "
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc stop >/dev/null 2>&1;
  RETVAL=$?
  # is it still here?
  if /sbin/pidof -o %PPID named >/dev/null; then
    timeout=0
    while : ; do
      if /sbin/pidof -o %PPID named >/dev/null; then
        if [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; then
          killproc named -TERM >/dev/null 2>&1
          RETVAL=$?
          echo $"no response, killing with -TERM "
          break
        else
          sleep 2 && echo -n "."
          timeout=$((timeout+2))
        fi;
      else
        break
      fi;
    done
  fi;

  # remove pid files
  if [ $RETVAL -eq 0 ]; then
    rm -f /var/lock/subsys/named
    rm -f /var/run/named.pid
  fi;

  # unmount mounted filesystems/directories
  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
    if [ ! -z $dbusEnabled ]; then
      if egrep -q '^/[^[:space:]]+[[:space:]]+'${ROOTDIR}'/var/run/dbus' /proc/mounts; then
        umount ${ROOTDIR}/var/run/dbus > /dev/null 2>&1
      fi;
    fi;
  fi;
  if [ $RETVAL -eq 0 ]; then
    success
  else
    failure
    RETVAL=1
  fi;
  echo
  return $RETVAL
}


rhstatus() {
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc status;
  status /usr/bin/named;
  return $?
}	
restart() {
	stop
	start
}	
reload() {
        echo -n $"Reloading named: "
	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 $"named reload" || failure $"named reload"
        echo
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		rhstatus;
		RETVAL=$?
		;;
	restart)
		restart
		;;
	condrestart)
		echo "condrestart option is obsolete. Use try-restart instead"
		if [ -e /var/lock/subsys/named ]; then restart; fi
		;;
	try-restart)
		if [ -e /var/lock/subsys/named ]; then restart; fi
                ;;
	reload)
		reload
		;;
	force-reload)
		if ! reload; then restart; fi
		;;
	*)
        	echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
		exit 3
esac

exit $RETVAL