bind/bind_remove_domain.sh
root 2124a5bbff - Add configuration directory for scripts.
- Add sample configuratin file 'bind.conf.sample'.
- Redisign script 'bind_change_SOA.sh'.
- Add script 'bind_remove_domain.sh'.
2017-08-01 01:29:48 +02:00

624 lines
15 KiB
Bash
Executable File

#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/bind.conf"
log_file="$(mktemp)"
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: $(basename $0) [Options]
Script removes a domain from bind nameservice.
If running in a terminal and NOT '-q' is used, script acts in
interactive mode. Script uses defaults either reading from file
${conf_file}, or if not present, bei hardcoded default settings.
You will be asked to confirm or override the default setting.
If not running in a terminal (i.e. used as cronjob) or flag '-q' (and
within '-d') is set, all configurations are read from file '${conf_file}'.
Options:
-d <domain>
The name of the domain, which is requested for deletion.
-h
Prints this help.
-q
Be quiet. If '-q' is used, also a domain must be given using
parameter '-d'.
EOF
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm $log_file
exit $1
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
echononl(){
if $terminal && $LOGGING ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mScript was interupted\033[m!"
else
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated...."
fi
echo ""
clean_up 1
}
info (){
echo ""
if $terminal && $LOGGING ; then
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
else
if $LOGGING ; then
echo "Info: $*"
fi
fi
echo ""
}
warn (){
echo ""
if $terminal ; then
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
else
echo "Warning: $*"
fi
echo ""
}
error (){
echo ""
if $terminal ; then
echo -e "\t[ \033[31m\033[1mError\033[m ]: $*"
else
echo "Error: $*"
fi
echo ""
}
echo_done() {
if $terminal && $LOGGING ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
else
if $LOGGING ; then
echo " [ done ]"
fi
fi
}
echo_ok() {
if $terminal && $LOGGING ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
else
if $LOGGING ; then
echo " [ ok ]"
fi
fi
}
echo_failed(){
if $terminal && $LOGGING ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
else
if $LOGGING ; then
echo " [ failed ]"
fi
fi
}
echo_skipped() {
if $terminal && $LOGGING ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
else
if $LOGGING ; then
echo " [ skipped ]"
fi
fi
}
trap clean_up SIGHUP SIGINT SIGTERM
#---------------------------------------
#-----------------------------
# Check some prerequisites
#-----------------------------
#---------------------------------------
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
while getopts d:hq opt ; do
case $opt in
d) DOMAIN_REQUESTED_TO_REMOVE="$OPTARG"
;;
q) LOGGING=false
;;
h) usage
;;
*) usage
esac
done
if ! $LOGGING && [[ -z "$DOMAIN_REQUESTED_TO_REMOVE" ]] ; then
usage "Giving Domain for deletion (option '-d') is required if using option '-q'."
fi
#---------------------------------------
#-----------------------------
# Setting Defaults
#-----------------------------
#---------------------------------------
DEFAULT_CONF_FILE_DIR="/etc/bind"
#---------------------------------------
#-----------------------------
# Load default values from bind.conf
#
# Overwrites the settings above
#
#-----------------------------
#---------------------------------------
if $LOGGING ; then
clear
echo ""
echo ""
echononl " Loading default Configuration values from $(basename ${conf_file}).."
if [[ ! -f "$conf_file" ]]; then
echo_skipped
else
source "${conf_file}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
[[ -n "$CONF_FILE_DIR" ]] && DEFAULT_CONF_FILE_DIR="$CONF_FILE_DIR"
if [[ -n "$ZONE_FILE_MASTER_DIR" ]] ; then
DEFAULT_ZONE_FILE_MASTER_DIR="$ZONE_FILE_MASTER_DIR"
else
DEFAULT_ZONE_FILE_MASTER_DIR="${DEFAULT_CONF_FILE_DIR}/master"
fi
if [[ -n "$ZONE_FILE_SLAVE_DIR" ]] ; then
DEFAULT_ZONE_FILE_SLAVE_DIR="$ZONE_FILE_SLAVE_DIR"
else
DEFAULT_ZONE_FILE_SLAVE_DIR="${DEFAULT_CONF_FILE_DIR}/slave"
fi
[[ -n "$ZONES_DECLARATION_FILE" ]] && DEFAULT_ZONES_DECLARATION_FILE="$ZONES_DECLARATION_FILE"
echo ""
if [[ -z "$DOMAIN_REQUESTED_TO_REMOVE" ]] ;then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert domain which you want to be removed from bind nameservice."
echo ""
DOMAIN_REQUESTED_TO_REMOVE=""
echononl "Remove Domain: "
read DOMAIN_REQUESTED_TO_REMOVE
while [ "X$DOMAIN_REQUESTED_TO_REMOVE" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mSetting 'Remove Domain' is required!\033[m\n"
echononl "Remove Domain: "
read DOMAIN_REQUESTED_TO_REMOVE
done
fi
echo ""
echo ""
echo -e "\033[32m--\033[m"
echo "Common parameters"
echo -e "\033[32m--\033[m"
echo ""
echo "Insert directory containing the bind configuration files."
echo ""
CONF_FILE_DIR=""
if [[ -n "$DEFAULT_CONF_FILE_DIR" ]] ; then
echononl "Bind Configuration Directory [${DEFAULT_CONF_FILE_DIR}]: "
read CONF_FILE_DIR
if [[ "X$CONF_FILE_DIR" = "X" ]]; then
CONF_FILE_DIR="$DEFAULT_CONF_FILE_DIR"
fi
else
echononl "Bind Configuration Directory: "
read CONF_FILE_DIR
while [ "X$CONF_FILE_DIR" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mSetting 'Bind Configuration Directory' is required!\033[m\n"
echononl "Bind Configuration Directory: "
read CONF_FILE_DIR
done
fi
[[ -n "$DEFAULT_ZONES_DECLARATION_FILE" ]] || DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local"
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert zones declaration file."
echo ""
ZONES_DECLARATION_FILE=""
if [[ -n "$DEFAULT_ZONES_DECLARATION_FILE" ]] ; then
echononl "Zones Declaration File [${DEFAULT_ZONES_DECLARATION_FILE}]: "
read ZONES_DECLARATION_FILE
if [[ "X$ZONES_DECLARATION_FILE" = "X" ]]; then
ZONES_DECLARATION_FILE="$DEFAULT_ZONES_DECLARATION_FILE"
fi
else
echononl "Zones Declaration File: "
read ZONES_DECLARATION_FILE
while [ "X$ZONES_DECLARATION_FILE" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mSetting 'Zones Declaration File' is required!\033[m\n"
echononl "Zones Declaration File: "
read ZONES_DECLARATION_FILE
done
fi
echo ""
echo ""
echo -e "\033[1;32mSettings for \033[1;37mRemove Domain\033[m Script"
echo ""
echo -e "\tDomain requested to remove..........: $DOMAIN_REQUESTED_TO_REMOVE"
echo ""
echo -e "\tBind Configuration Directory........: $CONF_FILE_DIR"
echo -e "\tZones Declaration File..............: $ZONES_DECLARATION_FILE"
echo ""
info "Remove Domain \033[37m\033[1m${DOMAIN_REQUESTED_TO_REMOVE}\033[m.."
echo -n "To continue type uppercase 'YES': "
read OK
echo ""
if [[ "$OK" != "YES" ]] ; then
fatal "Abort by user request - Answer as not 'YES'"
fi
else # if $LOGGING
if [[ ! -f "$conf_file" ]]; then
fatal "Configuration file '$conf_file' not found!"
else
echononl " Loading default Configuration values from $(basename ${conf_file}).."
source "${conf_file}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
if [[ -z "$CONF_FILE_DIR" ]]; then
fatal "Directory contaning bind configurations not set (see CONF_FILE_DIR)!"
fi
[[ -n "$ZONES_DECLARATION_FILE" ]] || ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local"
fi
if [[ ! -f "$ZONES_DECLARATION_FILE" ]]; then
fatal "Zone declaration file '${ZONES_DECLARATION_FILE}' not found!"
fi
if ! grep -q -E "^\s*zone\s+\"?${DOMAIN_REQUESTED_TO_REMOVE}\"?\s+\{" $ZONES_DECLARATION_FILE 2> /dev/null ; then
fatal "No decleration for domain '${DOMAIN_REQUESTED_TO_REMOVE}' found!"
fi
backup_date=$(date +%Y-%m-%d-%H%M)
echononl " Backup '$ZONES_DECLARATION_FILE'.."
cp -a "$ZONES_DECLARATION_FILE" "${ZONES_DECLARATION_FILE}.$backup_date" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
declare -i line_number=0
_found=false
declare -i _first_line=0
declare -i _last_line=0
while IFS='' read -r _line || [[ -n $_line ]] ; do
let line_number++
if [[ "$_line" =~ ^[[:space:]]*zone[[:space:]]+\"?${DOMAIN_REQUESTED_TO_REMOVE}\"?[[:space:]]+\{ ]]; then
_found=true
_first_line=$line_number
fi
if $_found ; then
if [[ "$_line" =~ ^[[:space:]]*file[[:space:]]+\"? ]]; then
zone_file="$(echo $_line | awk '{print$2}')"
shopt -s extglob
if [[ "$zone_file" =~ \;$ ]]; then
zone_file=${zone_file%%*(;)}
fi
if [[ "$zone_file" =~ ^\" ]]; then
zone_file=${zone_file##*(\")}
zone_file=${zone_file%%*(\")}
fi
shopt -u extglob
fi
if [[ "$_line" =~ ^[[:space:]]*key-directory[[:space:]]+\"? ]]; then
key_directory="$(echo $_line | awk '{print$2}')"
shopt -s extglob
if [[ "$key_directory" =~ \;$ ]]; then
key_directory=${key_directory%%*(;)}
fi
if [[ "$key_directory" =~ ^\" ]]; then
key_directory=${key_directory##*(\")}
key_directory=${key_directory%%*(\")}
fi
shopt -u extglob
fi
if [[ "$_line" =~ ^[[:space:]]*\}[[:space:]]*\; ]]; then
_found=false
_last_line=$line_number
fi
elif [[ $_last_line -gt 0 ]] ; then
if [[ "$_line" =~ ^[[:space:]]*$ ]] ; then
_last_line=$line_number
fi
break;
fi
done < "$ZONES_DECLARATION_FILE"
$LOGGING && echo ""
echononl " Delete zone declaration for domain '${DOMAIN_REQUESTED_TO_REMOVE}'"
sed -i "${_first_line},${_last_line}d" "$ZONES_DECLARATION_FILE" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
$LOGGING && echo ""
echononl " Create directory '$(dirname ${zone_file})/DELETED'.."
if [[ ! -d "$(dirname ${zone_file})/DELETED" ]] ; then
mkdir "$(dirname ${zone_file})/DELETED" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
else
echo_skipped
fi
echononl " Move zone file '$(basename $zone_file)' into directory 'DELETED'"
if [[ ! -f "$zone_file" ]] ; then
echo_failed
error "Concerning zone file '$zone_file' not found!"
else
mv "${zone_file}" "$(dirname ${zone_file})/DELETED" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
fi
while IFS='' read -r -d '' _file ; do
echononl " Move file '$(basename $_file)' into directory 'DELETED'"
mv "$_file" "$(dirname ${zone_file})/DELETED" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
#error "$(cat $log_file)"
else
echo_ok
fi
done < <(find $(dirname ${zone_file}) -mindepth 1 -maxdepth 1 -type f -name "${DOMAIN_REQUESTED_TO_REMOVE}.*" -print0)
if [[ -n "$key_directory" ]]; then
$LOGGING && echo ""
_dir="$(dirname ${key_directory})/DELETED"
echononl " Create directory '${_dir}'.."
if [[ ! -d "${_dir}" ]] ; then
mkdir "${_dir}" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
else
echo_skipped
fi
_dir="$(dirname ${key_directory})/DELETED/$(basename ${key_directory})"
echononl " Create directory '${_dir}'.."
if [[ ! -d "${_dir}" ]] ; then
mkdir "${_dir}" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
else
echo_skipped
fi
while IFS='' read -r -d '' _file ; do
echononl " Move file '$(basename $_file)' into directory 'DELETED'"
mv "$_file" "$(dirname ${key_directory})/DELETED/$(basename ${key_directory})" > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
done < <(find $key_directory -mindepth 1 -maxdepth 1 -type f -name "K${DOMAIN_REQUESTED_TO_REMOVE}.*" -print0)
if [[ ! "$(ls -A $key_directory)" ]] ; then
echononl " Remove empty key directory '$(basename $key_directory)'.."
rmdir $key_directory > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_ok
fi
fi
fi
$LOGGING && echo ""
echononl " Reeload bind configuration"
rndc reconfig > $log_file 2>&1
if [[ $? -gt 0 ]]; then
echo_failed
error "$(cat $log_file)"
else
echo_done
fi
#echo "zone file: $zone_file"
#echo "$line_number - $_first_line , $_last_line"
$LOGGING && echo ""
clean_up 0
echo ""
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo ""
## - Read list of zonefiles
## -
while IFS='' read -r -d '' zone_file ; do
declare -i _serial_new=`date +%Y%m%d01`
filename=$(basename "$zone_file")
zone="${filename%.*}"
echo ""
echo -e "\tlooking at $zone_file.."
## - calculate new serial
## -
declare -i __serial=`grep -e "[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'`
while [ ! $_serial_new -gt $__serial ]; do
let _serial_new++
done
echononl "\t Changing SOA Record.."
if ! grep -E "^.*IN\s+SOA\s+${_soa_nameserver}\.\s+${_soa_admin}\.\s+\(" > /dev/null 2>&1 $zone_file ; then
perl -i -n -p -e "s/^(.*IN\s+SOA).*$/\1 ${_soa_nameserver}. ${_soa_admin}. \(/" $zone_file
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
else
echo_skipped
continue
fi
## - Set new serial
## -
echononl "\t Renew serial.."
perl -i -n -p -e "s#^(\s*) $__serial(.*)#\1 $_serial_new\2#" $zone_file
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
## - Reload Zone
## -
echononl "\t Reload Zone $zone.."
/usr/sbin/rndc reload $zone > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
sleep 1
done < <(find $_zone_files_dir -mindepth 1 -maxdepth 1 -type f -print0)
echo ""
clean_up 0