ACC SHELL

Path : /etc/rc.d/
File Upload :
Current File : //etc/rc.d/named

#! /bin/sh
# Copyright (c) 1995-2004 SUSE Linux AG, Nuernberg, Germany
# All rights reserved.
#
# Author: Lars Mueller <lmuelle@suse.de>
#
# /etc/init.d/named
#   and its symbolic link
# /usr/sbin/rcnamed
#
#    This program is free software; you can redistribute it and/or modify 
#    it under the terms of the GNU General Public License as published by 
#    the Free Software Foundation; either version 2 of the License, or 
#    (at your option) any later version. 
# 
#    This program is distributed in the hope that it will be useful, 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
#    GNU General Public License for more details. 
# 
#    You should have received a copy of the GNU General Public License 
#    along with this program; if not, write to the Free Software 
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
### BEGIN INIT INFO
# Provides:          named
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      ldap
# Should-Stop:       ldap
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Domain Name System (DNS) server, named
# Description:       Berkeley Internet Name Domain (BIND) implementation of the
#	Domain Name System (DNS) server named.
### END INIT INFO

. /etc/rc.status
rc_reset

PATH="/sbin:/usr/sbin:/bin:/usr/bin"
SYSCONFIG_FILE="/etc/sysconfig/named"
NAMED_CONF="/etc/named.conf"

if [ "$1" != "stop" ]; then
	for configfile in ${SYSCONFIG_FILE} ${NAMED_CONF}; do
		if [ ! -f ${configfile} ]; then
			echo -n "Name server configuration file ${configfile} does not exist. "
			# Tell the user this has skipped
			rc_status -s
			exit 6
		fi
	done
	. /etc/sysconfig/named
	# Ensure to work always with absolut filenames.
	temp_file_list=""
	for configfile in ${NAMED_CONF_INCLUDE_FILES}; do
		# prepend the default include directory if the filename is
		# relative
		test "${configfile:0:1}" = "/" || configfile="/etc/named.d/${configfile}"
		temp_file_list="${temp_file_list} ${configfile}"
	done
	NAMED_CONF_INCLUDE_FILES=${temp_file_list}
	# Add include files from named.conf.
	NAMED_CONF_INCLUDE_LINES=$( grep -e '^[[:space:]]*include' "${NAMED_CONF}" | cut -f 2 -d '"')
	for file in ${NAMED_CONF_INCLUDE_LINES}; do
		test "${file}" = "${NAMED_CONF_META_INCLUDE_FILE}" && continue
		case "${NAMED_CONF_INCLUDE_FILES}" in
			*${file}*) ;;
			*) NAMED_CONF_INCLUDE_FILES="${NAMED_CONF_INCLUDE_FILES} ${file}" ;;
		esac
	done
fi

if [ "${NAMED_RUN_CHROOTED}" = "yes" ]; then
	CHROOT_PREFIX="/var/lib/named"
	NAMED_ARGS="${NAMED_ARGS} -t ${CHROOT_PREFIX}"
	NAMED_CHECKCONF_ARGS="-t ${CHROOT_PREFIX}"
else
	CHROOT_PREFIX=""
fi

NAMED_BIN="/usr/sbin/named"
NAMED_CHECKCONF_BIN="/usr/sbin/named-checkconf"
NAMED_CONF_META_INCLUDE_FILE_SCRIPT="/usr/share/bind/createNamedConfInclude"
NAMED_PID="${CHROOT_PREFIX}/var/run/named/named.pid"
RNDC_BIN="/usr/sbin/rndc"

if [ ! -x ${NAMED_BIN} -a "$1" != "stop" ] ; then
	echo -n "Name server, ${NAMED_BIN} not installed! "
	# Tell the user this has skipped
	rc_status -s
	exit 5
fi

# Check for NAMED_CONF_META_INCLUDE_FILE or set it to our default if we use
# the NAMED_CONF_META_INCLUDE_FILE_SCRIPT script.
if [ -z "${NAMED_CONF_META_INCLUDE_FILE}" ]; then
	BASENAME_NAMED_CONF_META_INCLUDE_FILE_SCRIPT=$( basename ${NAMED_CONF_META_INCLUDE_FILE_SCRIPT})
	for script in ${NAMED_INITIALIZE_SCRIPTS}; do
		if [ "${script}" = "${BASENAME_NAMED_CONF_META_INCLUDE_FILE_SCRIPT}" -o \
		"${script}" = "${NAMED_CONF_META_INCLUDE_FILE_SCRIPT}" ]; then
			NAMED_CONF_META_INCLUDE_FILE="/etc/named.conf.include"
			break
		fi
	done
fi

function warnMessage()
{
	tput bold
	echo -en "\nWarning: "
	tput sgr0
	echo -e "$1 "
}

function initializeNamed
{
	rc=0
	test "${initializeNamedCalled}" = "yes" && return
	for script in ${NAMED_INITIALIZE_SCRIPTS}; do
		test "${script:0:1}" = "/" || script="/usr/share/bind/${script}"
		${script}
		test $? -ne 0 && rc=$?
	done
	# We want to start each script one time only
	export initializeNamedCalled="yes"
	return ${rc}
}

# Create destination directory in the chroot.
function makeDestDir
{
	if [ ! -d "${CHROOT_PREFIX}/${configfile%/*}" ]; then
		umask 0022
		mkdir -p "${CHROOT_PREFIX}/${configfile%/*}"
	fi
}

# Check if all needed configuration files exist and copy these files relativly
# to the chroot directory if 'named' runs chrooted.
function checkAndCopyConfigFiles
{
	test "${checkAndCopyConfigFilesCalled}" = "yes" && return
	# check for /etc/rndc.key
	if [ ! -f /etc/rndc.key ]; then
		warnMessage "File /etc/rndc.key not found. Creating it."
		/usr/sbin/rndc-confgen -a -b 512 -r /dev/urandom
		chmod 0640 /etc/rndc.key
		chown root:named /etc/rndc.key
	fi
	# Handle all include files.
	#####################################################################
	#  OLD ---  Copies only include files mentioned in NAMED_CONF_INCLUDE
	#####################################################################
#	for configfile in "${NAMED_CONF_META_INCLUDE_FILE}" ${NAMED_CONF_INCLUDE_FILES}; do
#		if [ ! -f "${configfile}" ]; then
#			case "${configfile}" in
#				"${NAMED_CONF_META_INCLUDE_FILE}")
#					warnMessage "File, ${configfile} not found.  Creating it."
#					initializeNamed
#					;;
#				*)
#					test -z "${NAMED_CONF_META_INCLUDE_FILE}" && continue
#					grep -qe "^[[:space:]]*include[[:space:]]*\"${configfile}\"" "${NAMED_CONF_META_INCLUDE_FILE}" && \
#						initializeNamed || \
#						warnMessage "File, ${configfile} not found.  Skipping.\nPlease check the setting of NAMED_CONF_INCLUDE_FILES in /etc/sysconfig/named."
#					continue
#					;;
#			esac
#		fi
#		if [ "${NAMED_RUN_CHROOTED}" = "yes" ]; then
#			makeDestDir
#            rm -f ${CHROOT_PREFIX}/${configfile}
#			cp -a -L ${configfile} ${CHROOT_PREFIX}/${configfile%/*}
#		fi
#	done


	# Handle known configuration files.
	if [ "${NAMED_RUN_CHROOTED}" = "yes" ]; then

	# mount /proc for multicore CPUs (bnc#470828)
        if [ ! -e "${CHROOT_PREFIX}/proc/meminfo" ]; then
            mkdir -p "${CHROOT_PREFIX}/proc"
            mount --bind /proc "${CHROOT_PREFIX}/proc" 2>/dev/null
            mount -o remount,ro,nosuid,nodev ${CHROOT_PREFIX}/proc 2>/dev/null
        fi;

        ###########################
        # NEW --- Copy complete /etc/named.d dir. Bug: bnc#408145
        ###########################
        NAMED_D="/etc/named.d"
        # delete old named.d
        test -z "${CHROOT_PREFIX}${NAMED_D}" || rm -rf ${CHROOT_PREFIX}${NAMED_D}
        # copy new
        cp -a -L  ${NAMED_D} ${CHROOT_PREFIX}${NAMED_D%/*}
		for configfile in ${NAMED_CONF_INCLUDE_FILES} "${NAMED_CONF}" "${NAMED_CONF_META_INCLUDE_FILE}" /etc/{localtime,rndc.key}; do
			if [ ! -e ${configfile} ]; then
				warnMessage "File ${configfile} not found. Skipping."
				continue
			fi
			makeDestDir
            rm -f ${CHROOT_PREFIX}/${configfile}
			cp -a -L ${configfile} ${CHROOT_PREFIX}/${configfile%/*}
		done
	fi
	export checkAndCopyConfigFilesCalled="yes"
}

# Check the syntax of our 'named' configuration.
function namedCheckConf
{
	test "${namedConfChecked}" = "yes" && return
	if ! ${NAMED_CHECKCONF_BIN} ${NAMED_CHECKCONF_ARGS} >/dev/null; then
		checkAndCopyConfigFiles
		if ! ${NAMED_CHECKCONF_BIN} ${NAMED_CHECKCONF_ARGS}; then
			rc_status -s
			rc_failed 6
			rc_exit
		fi
	fi
	export namedConfChecked="yes"
}

case "$1" in
    start)
	echo -n "Starting name server BIND "
	if [ ! -e /etc/named.conf.include ]; then
	    touch /etc/named.conf.include
	fi
	if [ ! -e /etc/named.d/forwarders.conf ]; then
		touch /etc/named.d/forwarders.conf
	fi
        # Create link if needed, /var/run might be on tmpfs
        if [ ! -L /var/run/named ] ; then
          ln -s ${CHROOT_PREFIX}/var/run/named /var/run/named
        fi

	checkproc -p ${NAMED_PID} ${NAMED_BIN}
	case $? in
		0) echo -n "- Warning: named already running! " ;;
		1) echo -n "- Warning: ${NAMED_PID} exists! " ;;
	esac
	initializeNamed
	checkAndCopyConfigFiles
	namedCheckConf
	start_daemon -p ${NAMED_PID} ${NAMED_BIN} ${NAMED_ARGS} -u named
	rc_status -v
	;;
    stop)
	echo -n "Shutting down name server BIND "
	checkproc -p ${NAMED_PID} ${NAMED_BIN} || echo -n "- Warning: named not running! "
	if ${RNDC_BIN} status &>/dev/null; then
		${RNDC_BIN} stop
	else
		killproc -p ${NAMED_PID} -TERM ${NAMED_BIN}
	fi
	rc=$?

	# let's wait, because
	# 1) trying to start named before it has terminated can leave
	#    us without a running named...
	# 2) making sure that pending updates are written to zone files is good
	#    before trying to modify them externally
	info="no"; timeout=30; rc=0; startDate=$( date +%s)
	while [ ${rc} -eq 0 ]; do
		checkproc -p ${NAMED_PID} ${NAMED_BIN}
		rc=$?
		if [ ${rc} -ne 0 ]; then
			test "${info}" = "yes" && rc_timer_off
			break
		elif [ ${info} = "no" ]; then
			echo -n >&2 " waiting for named to shut down "
			rc_timer_on ${timeout} 63
			info="yes"
		fi
		if [ $(( $( date +%s) - ${startDate} )) -gt $timeout ]; then
			echo -en >&2 "\nNamed still appears to be running after $timeout seconds, sending SIGTERM"
			rc=1
			killproc -p ${NAMED_PID} -TERM ${NAMED_BIN}
		else
			sleep 2
		fi
	done
        if [ -e "/var/lib/named/proc/meminfo" ] ; then
	    umount "/var/lib/named/proc"
	fi;
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	if [ ! -e /etc/named.conf.include ]; then
	    touch /etc/named.conf.include
	fi
	if [ ! -e /etc/named.d/forwarders.conf ]; then
		touch /etc/named.d/forwarders.conf
	fi
	namedCheckConf
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset
	fi
	rc_status
	;;
    restart)
	if [ ! -e /etc/named.conf.include ]; then
	    touch /etc/named.conf.include
	fi
	if [ ! -e /etc/named.d/forwarders.conf ]; then
		touch /etc/named.d/forwarders.conf
	fi
	namedCheckConf
	# Some of the scripts need a running named.
	$0 status >/dev/null && initializeNamed
	$0 stop
	$0 start
	rc_status
	;;
    force-reload|reload)
	echo -n "Reloading name server BIND "
	checkproc -p ${NAMED_PID} ${NAMED_BIN}
	rc=$?
	if [ ${rc} -ne 0 ]; then
		echo "- Warning: named not running! "
	else
		checkAndCopyConfigFiles
		namedCheckConf
		initializeNamed
		${RNDC_BIN} status &>/dev/null
		if [ $? -ne 0 ]; then
			killproc -p ${NAMED_PID} -HUP ${NAMED_BIN}
		else
			${RNDC_BIN} reload
		fi
		rc=$?
	fi
	( exit ${rc} )
	rc_status -v
	;;
    status)
	echo -n "Checking for nameserver BIND "
	checkproc -p ${NAMED_PID} ${NAMED_BIN}
	v=$?
	if [ $v -ne 0 ]; then
	    rc_failed $v	
	else
		test "${RNDC_BIN}" && echo && ${RNDC_BIN} status && echo -en "${esc}[1A"
        true
	fi
	rc_status -v
	;;
    probe)
	rc=0
	for configfile in ${NAMED_CONF} /etc/{localtime,rndc.key} "${NAMED_CONF_META_INCLUDE_FILE}" ${NAMED_CONF_INCLUDE_FILES}; do
		if [ "${configfile}" -nt ${NAMED_PID} ]; then
			rc=1
			break
		fi
	done
	test ${rc} -ne 0 && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit


ACC SHELL 2018