Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/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/namedRETVAL=0export KRB5_KTNAME=${KEYTAB_FILE:-/etc/named.keytab}named='named'if [ -x /usr/sbin/named-sdb ]; thennamed='named-sdb'fi# Don't kill named during clean-upNAMED_SHUTDOWN_TIMEOUT=${NAMED_SHUTDOWN_TIMEOUT:-25}if [ -n "$ROOTDIR" ]; thenROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`;rdl=`/usr/bin/readlink $ROOTDIR`;if [ -n "$rdl" ]; thenROOTDIR="$rdl";fi;fiPIDFILE="/var/run/named/named.pid"ROOTDIR_MOUNT='/etc/named /etc/pki/dnssec-keys /var/named /etc/named.conf/etc/named.dnssec.keys /etc/named.rfc1912.zones /etc/rndc.conf /etc/rndc.key/usr/lib64/bind /usr/lib/bind /etc/named.iscdlv.key /etc/named.root.key'mount_chroot_conf(){if [ -n "$ROOTDIR" ]; thenfor all in $ROOTDIR_MOUNT; do# Skip nonexistant files[ -e "$all" ] || continue# If mount source is a fileif ! [ -d "$all" ]; then# mount it only if it is not present in chroot or it is emptyif ! [ -e "$ROOTDIR$all" ] || [ `stat -c'%s' "$ROOTDIR$all"` -eq 0 ]; thentouch "$ROOTDIR$all"mount --bind "$all" "$ROOTDIR$all"fielse# Mount source is a directory. Mount it only if directory in chroot is# empty.if [ -e "$all" ] && [ `ls -1A $ROOTDIR$all | wc -l` -eq 0 ]; thenmount --bind "$all" "$ROOTDIR$all"fifidonefi}umount_chroot_conf(){if [ -n "$ROOTDIR" ]; thenfor all in $ROOTDIR_MOUNT; do# Check if file is mount target. Do not use /proc/mounts because detecting# of modified mounted files can fail.if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; thenumount "$ROOTDIR$all"# Remove temporary created files[ -f "$all" ] && rm -f "$ROOTDIR$all"fidonefi}check_pidfile() {PID="`pidofproc -p "$ROOTDIR$PIDFILE" "$named"`"if [ -n "$PID" ] && [ "`ps -p "$PID" --no-headers -o comm`" != "$named" ]; thenrm -f $ROOTDIR$PIDFILE &> /dev/nullfi}pidofnamed() {pidofproc -p "$ROOTDIR$PIDFILE" "$named";}# Check if all what named needs runningstart(){[ "$EUID" != "0" ] && exit 4# Source networking configuration.[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network# Check that networking is up[ "${NETWORKING}" = "no" ] && exit 1[ -x /usr/sbin/"$named" ] || exit 5if [ ! -s /etc/rndc.key -a ! -s /etc/rndc.conf ]; then# Generate rndc.key if doesn't exist AND there is no rndc.confecho -n $"Generating /etc/rndc.key:"if /usr/sbin/rndc-confgen -a -r /dev/urandom > /dev/null 2>&1; thenchmod 640 /etc/rndc.keychown root.named /etc/rndc.key[ -x /sbin/restorecon ] && /sbin/restorecon /etc/rndc.keysuccess $"/etc/rndc.key generation"echoelsefailure $"/etc/rndc.key generation"echofifi# Handle -c optionprevious_option='unspecified';for a in $OPTIONS; doif [ $previous_option = '-c' ]; thennamed_conf=$a;fi;previous_option=$a;done;named_conf=${named_conf:-/etc/named.conf};# check if named is running before mounting files/dirsecho -n $"Starting named: "check_pidfileif [ -n "`pidofnamed`" ]; thenecho -n $"named: already running"successechoexit 0;fi;mount_chroot_confif [ ! -r $ROOTDIR$named_conf ]; thenecho 'Cannot find configuration file. You could create it by system-config-bind'exit 6;fi;[ -x /sbin/portrelease ] && /sbin/portrelease named &>/dev/null || :if ! [ "$DISABLE_ZONE_CHECKING" = yes ]; thenckcf_options='-z'; # enable named-checkzone for each zone (9.3.1+) !fi;if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; thenOPTIONS="${OPTIONS} -t ${ROOTDIR}"ckcf_options="$ckcf_options -t ${ROOTDIR}";[ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;fiRETVAL=0# check if configuration is correctif [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; thendaemon --pidfile "$ROOTDIR$PIDFILE" /usr/sbin/"$named" -u named ${OPTIONS};RETVAL=$?if [ $RETVAL -eq 0 ]; thenrm -f /var/run/{named,named-sdb}.pid;ln -s "$ROOTDIR$PIDFILE" /var/run/"$named".pid;fi;elsenamed_err="`/usr/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";echoecho "Error in named configuration:";echo "$named_err";failureecho[ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;umount_chroot_confexit 2;fi;echoif [ $RETVAL -eq 0 ]; thentouch /var/lock/subsys/named;elseumount_chroot_confexit 7;fireturn 0;}stop() {[ "$EUID" != "0" ] && exit 4# Stop daemons.echo -n $"Stopping named: "check_pidfile[ -x /usr/sbin/rndc ] && /usr/sbin/rndc stop >/dev/null 2>&1;RETVAL=$?# was rndc successful?[ "$RETVAL" -eq 0 ] || \killproc -p "$ROOTDIR$PIDFILE" "$named" -TERM >/dev/null 2>&1timeout=0RETVAL=0while pidofnamed &>/dev/null; doif [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; thenRETVAL=1breakelsesleep 2 && echo -n "."timeout=$((timeout+2))fi;doneumount_chroot_conf# remove pid filesif [ $RETVAL -eq 0 ]; thenrm -f /var/lock/subsys/namedrm -f /var/run/{named,named-sdb}.pidfi;if [ $RETVAL -eq 0 ]; thensuccesselsefailureRETVAL=1fi;echoreturn $RETVAL}rhstatus() {[ -x /usr/sbin/rndc ] && /usr/sbin/rndc status;check_pidfilestatus -p "$ROOTDIR$PIDFILE" -l named /usr/sbin/"$named";return $?}restart() {stopstart}reload() {[ "$EUID" != "0" ] && exitecho -n $"Reloading "$named": "check_pidfilep=`pidofnamed`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"echoreturn $RETVAL}checkconfig() {ckcf_options='-z';if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; thenckcf_options="$ckcf_options -t ${ROOTDIR}";if ! [ -e "$ROOTDIR/$PIDFILE" ]; thenmount_chroot_conffifi;if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} ; thenRETVAL=0elseRETVAL=1fiif ! [ -e "$ROOTDIR/$PIDFILE" ]; thenumount_chroot_conffireturn $RETVAL}# See how we were called.case "$1" instart)start;;stop)stop;;status)rhstatus;RETVAL=$?;;restart)restart;;condrestart|try-restart)if [ -e /var/lock/subsys/named ]; then restart; fi;;reload)reload;;force-reload)if ! reload; then restart; fi;;checkconfig|configtest|check|test)checkconfigRETVAL=$?;;*)echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"[ "x$1" = "x" ] && exit 0exit 2esacexit $RETVAL