From 5a4eab9261e5aa2bcbad56247fefa931501ec4f6 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 2 Aug 2017 14:08:23 +0200 Subject: [PATCH] - bind_change_ip.sh: Remove unused and commented code. - bind_reload_all_zones.sh, bind_disable_ipv6.sh, bind_enable_ipv6.sh: Redesign of the code. Add support for configuration file. - --- bind_change_ip.sh | 107 ----------- bind_disable_ipv6.sh | 405 ++++++++++++++++++++++++++++++++++++--- bind_enable_ipv6.sh | 404 +++++++++++++++++++++++++++++++++++--- bind_reload_all_zones.sh | 134 ++++++++++--- 4 files changed, 871 insertions(+), 179 deletions(-) diff --git a/bind_change_ip.sh b/bind_change_ip.sh index 2802609..b7f99ca 100755 --- a/bind_change_ip.sh +++ b/bind_change_ip.sh @@ -175,12 +175,6 @@ else DEFAULT_ZONE_FILE_SLAVE_DIR="${DEFAULT_CONF_FILE_DIR}/slave" fi -if [[ -n "$ZONE_FILE_SUFFIX" ]] ; then - DEFAULT_ZONE_FILE_SUFFIX="$ZONE_FILE_SUFFIX" -else - DEFAULT_ZONE_FILE_SUFFIX='zone' -fi - if [[ -n "$ZONES_DECLARATION_FILE" ]] ; then DEFAULT_ZONES_DECLARATION_FILE="$ZONES_DECLARATION_FILE" else @@ -365,53 +359,6 @@ if ! $_set_ipv6 && ! $_set_ipv4 ; then fi -#echo "" -#echo -e "\033[32m--\033[m" -#echo "" -#echo "Insert the directory, where your zone-files resides." -#echo "" -#echo "" -#ZONE_FILE_MASTER_DIR="" -#if [[ -n "$DEFAULT_ZONE_FILE_MASTER_DIR" ]] ; then -# echononl "Zone File Directory (master) [${DEFAULT_ZONE_FILE_MASTER_DIR}]: " -# read ZONE_FILE_MASTER_DIR -# if [[ "X$ZONE_FILE_MASTER_DIR" = "X" ]]; then -# ZONE_FILE_MASTER_DIR="$DEFAULT_ZONE_FILE_MASTER_DIR" -# fi -#else -# echononl "Zone File Directory (master): " -# read ZONE_FILE_MASTER_DIR -# while [ "X$ZONE_FILE_MASTER_DIR" = "X" ] ; do -# echo -e "\n\t\033[33m\033[1mSetting 'Zone File Directory (master)' is required!\033[m\n" -# echononl "Zone File Directory (master): " -# read ZONE_FILE_MASTER_DIR -# done -#fi -# -#echo "" -#echo -e "\033[32m--\033[m" -#echo "" -#echo "Insert the file-suffix of the zone-files" -#echo "" -#echo "" -#ZONE_FILE_SUFFIX= -#if [[ -n "$DEFAULT_ZONE_FILE_SUFFIX" ]] ; then -# echononl "Suffix of Zone Files [${DEFAULT_ZONE_FILE_SUFFIX}]: " -# read ZONE_FILE_SUFFIX -# if [[ "X$ZONE_FILE_SUFFIX" = "X" ]]; then -# ZONE_FILE_SUFFIX="$DEFAULT_ZONE_FILE_SUFFIX" -# fi -#else -# echononl "Suffix of Zone Files: " -# read ZONE_FILE_SUFFIX -# while [ "X$ZONE_FILE_SUFFIX" = "X" ] ; do -# echo -e "\n\t\033[33m\033[1mSetting 'Suffix of Zone Files' is required!\033[m\n" -# echononl "Suffix of Zone Files: " -# read ZONE_FILE_SUFFIX -# done -#fi - - echo "" echo -e "\033[32m--\033[m" echo "" @@ -518,30 +465,6 @@ else done fi -#echo "" -#echo -e "\033[32m--\033[m" -# -#echo "" -#echo "Insert the file-suffix of the zone-files" -#echo "" -#echo "" -#ZONE_FILE_SUFFIX= -#if [[ -n "$DEFAULT_ZONE_FILE_SUFFIX" ]] ; then -# echononl "Suffix of Zone Files [${DEFAULT_ZONE_FILE_SUFFIX}]: " -# read ZONE_FILE_SUFFIX -# if [[ "X$ZONE_FILE_SUFFIX" = "X" ]]; then -# ZONE_FILE_SUFFIX="$DEFAULT_ZONE_FILE_SUFFIX" -# fi -#else -# echononl "Suffix of Zone Files: " -# read ZONE_FILE_SUFFIX -# while [ "X$ZONE_FILE_SUFFIX" = "X" ] ; do -# echo -e "\n\t\033[33m\033[1mSetting 'Suffix of Zone Files' is required!\033[m\n" -# echononl "Suffix of Zone Files: " -# read ZONE_FILE_SUFFIX -# done -#fi - clear echo "" @@ -602,36 +525,6 @@ else fatal "$(cat $log_file)" fi - -#if $_set_ipv4 ; then -# _zone_files_ipv4=`grep -l -e "$IPv4_ADDRESS_OLD" ${ZONE_FILE_MASTER_DIR}/*.$ZONE_FILE_SUFFIX` -# if [ ${#zonefiles_arr[@]} -eq 0 ] ; then -# for _zone_file in $_zone_files_ipv4 ; do -# zonefiles_arr+=("$_zone_file") -# done -# else -# for _zone_file in $_zone_files_ipv4 ; do -# containsElement "$_zone_file" "${zonefiles_arr[@]}" && continue -# zonefiles_arr+=("$_zone_file") -# done -# fi -#fi -# -# -#if $_set_ipv6 ; then -# _zone_files_ipv6=`grep -l -e "$_ipv6_address" ${ZONE_FILE_MASTER_DIR}/*.$ZONE_FILE_SUFFIX` -# if [ ${#zonefiles_arr[@]} -eq 0 ] ; then -# for _zone_file in $_zone_files_ipv6 ; do -# zonefiles_arr+=("$_zone_file") -# done -# else -# for _zone_file in $_zone_files_ipv6 ; do -# containsElement "$_zone_file" "${zonefiles_arr[@]}" && continue -# zonefiles_arr+=("$_zone_file") -# done -# fi -#fi - echo "" echo "" echo -e "\033[37m\033[1mDetermine zone files conatining ip-adressse requested for change..\033[m" diff --git a/bind_disable_ipv6.sh b/bind_disable_ipv6.sh index 0549b2d..4f2309d 100755 --- a/bind_disable_ipv6.sh +++ b/bind_disable_ipv6.sh @@ -1,40 +1,399 @@ #!/usr/bin/env bash -#_zone_files_dir=/etc/bind/master -_zone_files_dir=/root/tmp/master -_comment=";; - IPv6 disabled " -cp -a $_zone_files_dir ${_zone_files_dir}.BAK-`date +%Y-%m-%d-%H%M` +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/bind.conf" -__zonefiles=`grep -l -E "^[^;].*\s*AAAA" ${_zone_files_dir}/*.zone` +log_file="$(mktemp)" -for zone_file in $__zonefiles ; do +backup_date="$(date +%Y-%m-%d-%H%M)" - _serial_new=`date +%Y%m%d01` +_serial_new="$(date +%Y%m%d01)" - echo -e "\tconverting $zone_file .." - - _replaced=false +comment=";; - IPv6 disabled " - ## - 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++ + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +clean_up() { + + # Perform program exit housekeeping + rm $log_file + exit $1 +} + +echononl(){ + 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$$ +} + +fatal(){ + echo "" + echo -e "[ \033[31m\033[1mError\033[m ]: $*" + echo "" + echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" + echo "" + clean_up 1 +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[33m\033[1mInfo\033[m ]: $*" + echo "" +} + +ok (){ + echo "" + echo -e "\t[ \033[36m\033[1mOk\033[m ]: $*" + echo "" +} + +error(){ + echo "" + echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + echo "" +} + +echo_ok() { + echo -e "\033[75G[ \033[32mok\033[m ]" +} +echo_done() { + echo -e "\033[75G[ \033[32mdone\033[m ]" +} +echo_failed(){ + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" +} + +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + +trap clean_up SIGHUP SIGINT SIGTERM + + + +#--------------------------------------- +#----------------------------- +# Setting Defaults +#----------------------------- +#--------------------------------------- + +DEFAULT_CONF_FILE_DIR="/etc/bind" + + + +#--------------------------------------- +#----------------------------- +# Load default values from bind.conf +# +# Overwrites the settings above +# +#----------------------------- +#--------------------------------------- + +#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 + +if [[ -n "$ZONES_DECLARATION_FILE" ]] ; then + DEFAULT_ZONES_DECLARATION_FILE="$ZONES_DECLARATION_FILE" +else + DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" +fi + + + +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 - perl -i -n -p -e "s/^([^;]+.*\s+AAAA\s+.*)$/${_comment}\1/" $zone_file - if [ $? -eq 0 ]; then - _replaced=true +[[ -n "$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 - ## - setze neue serial - ## - - if $_replaced ; then - perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file +echo "" +echo -e "\033[32m--\033[m" + +echo "" +echo "Insert the directory, where your zone-files resides." +echo "" +echo "" +ZONE_FILE_MASTER_DIR="" +if [[ -n "$DEFAULT_ZONE_FILE_MASTER_DIR" ]] ; then + echononl "Zone File Directory (master) [${DEFAULT_ZONE_FILE_MASTER_DIR}]: " + read ZONE_FILE_MASTER_DIR + if [[ "X$ZONE_FILE_MASTER_DIR" = "X" ]]; then + ZONE_FILE_MASTER_DIR="$DEFAULT_ZONE_FILE_MASTER_DIR" fi +else + echononl "Zone File Directory (master): " + read ZONE_FILE_MASTER_DIR + while [ "X$ZONE_FILE_MASTER_DIR" = "X" ] ; do + echo -e "\n\t\033[33m\033[1mSetting 'Zone File Directory (master)' is required!\033[m\n" + echononl "Zone File Directory (master): " + read ZONE_FILE_MASTER_DIR + done +fi + + +clear +echo "" +echo "" +echo -e "\033[21G\033[32mDisable IPv6 for all (master) Zones\033[m" +echo "" + +echo "" +echo "Bind Configuration Directory......: $CONF_FILE_DIR" +echo "Zones Declaration File............: $ZONES_DECLARATION_FILE" +echo "Zone File Directory (master)......: $ZONE_FILE_MASTER_DIR" + +echo "" +OK= +while [ "$OK" != "yes" -o "$OK" != "no" ] ; do + echononl "Parameters ok? [yes/no]: " + read OK + ## - To lower case + OK=${OK,,} + if [ "X$OK" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + OK="" + continue + fi + if [ "$OK" = "yes" -o "$OK" = "no" ] ; then + break + else + OK="" + continue + fi + echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" done +[[ $OK = "yes" ]] || fatal Repeat execution with different parameters -exit +echo "" + +echononl "\tBackup directory '${ZONE_FILE_MASTER_DIR}'.." +cp -a "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + fatal "$(cat $log_file)" +fi + + +_found=false +_is_master=false +zonefiles_arr=() +zone_file="" +regex_master="type[[:space:]]+master" +regex_file="^[[:space:]]*file" +while IFS='' read -r _line || [[ -n $_line ]] ; do + + if [[ $_line =~ ^[[:space:]]*zone[[:space:]]+ ]]; then + _found=true + zone="$(echo $_line | awk '{print$2}')" + shopt -s extglob + if [[ $zone =~ \; ]]; then + zone=${zone%%*(\;)} + fi + if [[ $zone =~ ^\" ]]; then + zone=${zone##*(\")} + zone=${zone%%*(\")} + fi + shopt -u extglob + fi + if $_found ; then + if [[ $_line =~ $regex_file ]]; 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 =~ $regex_master ]]; then + _is_master=true + fi + if [[ "$_line" =~ ^[[:space:]]*\}[[:space:]]*\; ]]; then + if $_is_master && [[ -n "$zone_file" ]]; then + if $(grep -q -E "^[^;].*\s*AAAA" "$zone_file" 2> /dev/null) ; then + + + echo -e "\tEditing \033[1m$zone_file\033[m .." + + _replaced=false + + echononl "\t Comment all IPv6 AAAA Records" + perl -i -n -p -e "s/^([^;]+.*\s+AAAA\s+.*)$/${comment}\1/" "$zone_file" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + _replaced=true + else + echo_failed + fi + + # - Calculate new serial + # - + echononl "\t Calculate new serial" + if $_replaced ; then + 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 + echo_done + else + echo_skipped + fi + + + # - Set new serial + # - + echononl "\t Increase serial" + if $_replaced ; then + perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + fi + else + echo_skipped + fi + + + # - Reload Zone + # - + echononl "\t Reload zone '${zone}'" + if $_replaced ; then + /usr/sbin/rndc reload $zone > $log_file 2>&1 + if [[ $? -gt 0 ]]; then + echo_failed + error "$(cat $log_file)" + else + echo_ok + fi + else + echo_skipped + fi + + echo + + fi + fi + _is_master=false + _found=false + zone_file="" + fi + fi +done < "$ZONES_DECLARATION_FILE" + + +if [[ -d "${ZONE_FILE_MASTER_DIR}.${backup_date}" ]] ; then + diff -Nur "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "No zone file has changed.\n\t Removing previously created backup" + echononl "\tDelete '${ZONE_FILE_MASTER_DIR}.${backup_date}'.." + rm -rf "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi + fi +fi + + + +echo "" +clean_up 0 diff --git a/bind_enable_ipv6.sh b/bind_enable_ipv6.sh index 3c362f0..07556cc 100755 --- a/bind_enable_ipv6.sh +++ b/bind_enable_ipv6.sh @@ -1,40 +1,398 @@ #!/usr/bin/env bash -#_zone_files_dir=/etc/bind/master -_zone_files_dir=/root/tmp/master -_comment=";; - IPv6 disabled " -cp -a $_zone_files_dir ${_zone_files_dir}.BAK-`date +%Y-%m-%d-%H%M` +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/bind.conf" -__zonefiles=`grep -l -E "^${_comment}.*\s*AAAA" ${_zone_files_dir}/*.zone` +log_file="$(mktemp)" -for zone_file in $__zonefiles ; do +backup_date="$(date +%Y-%m-%d-%H%M)" - _serial_new=`date +%Y%m%d01` +_serial_new="$(date +%Y%m%d01)" - echo -e "\tconverting $zone_file .." - - _replaced=false +comment=";; - IPv6 disabled " - ## - 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++ + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +clean_up() { + + # Perform program exit housekeeping + rm $log_file + exit $1 +} + +echononl(){ + 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$$ +} + +fatal(){ + echo "" + echo -e "[ \033[31m\033[1mError\033[m ]: $*" + echo "" + echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" + echo "" + clean_up 1 +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[33m\033[1mInfo\033[m ]: $*" + echo "" +} + +ok (){ + echo "" + echo -e "\t[ \033[36m\033[1mOk\033[m ]: $*" + echo "" +} + +error(){ + echo "" + echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + echo "" +} + +echo_ok() { + echo -e "\033[75G[ \033[32mok\033[m ]" +} +echo_done() { + echo -e "\033[75G[ \033[32mdone\033[m ]" +} +echo_failed(){ + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" +} + +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + +trap clean_up SIGHUP SIGINT SIGTERM + + + +#--------------------------------------- +#----------------------------- +# Setting Defaults +#----------------------------- +#--------------------------------------- + +DEFAULT_CONF_FILE_DIR="/etc/bind" + + + +#--------------------------------------- +#----------------------------- +# Load default values from bind.conf +# +# Overwrites the settings above +# +#----------------------------- +#--------------------------------------- + +#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 + +if [[ -n "$ZONES_DECLARATION_FILE" ]] ; then + DEFAULT_ZONES_DECLARATION_FILE="$ZONES_DECLARATION_FILE" +else + DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" +fi + + + +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 - perl -i -n -p -e "s/^${_comment}(.*\s+AAAA\s+.*)$/\1/" $zone_file - if [ $? -eq 0 ]; then - _replaced=true +[[ -n "$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 - ## - setze neue serial - ## - - if $_replaced ; then - perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file +echo "" +echo -e "\033[32m--\033[m" + +echo "" +echo "Insert the directory, where your zone-files resides." +echo "" +echo "" +ZONE_FILE_MASTER_DIR="" +if [[ -n "$DEFAULT_ZONE_FILE_MASTER_DIR" ]] ; then + echononl "Zone File Directory (master) [${DEFAULT_ZONE_FILE_MASTER_DIR}]: " + read ZONE_FILE_MASTER_DIR + if [[ "X$ZONE_FILE_MASTER_DIR" = "X" ]]; then + ZONE_FILE_MASTER_DIR="$DEFAULT_ZONE_FILE_MASTER_DIR" fi +else + echononl "Zone File Directory (master): " + read ZONE_FILE_MASTER_DIR + while [ "X$ZONE_FILE_MASTER_DIR" = "X" ] ; do + echo -e "\n\t\033[33m\033[1mSetting 'Zone File Directory (master)' is required!\033[m\n" + echononl "Zone File Directory (master): " + read ZONE_FILE_MASTER_DIR + done +fi + + +clear +echo "" +echo "" +echo -e "\033[21G\033[32mEnable IPv6 for all previously disabled (master) Zones\033[m" +echo "" + +echo "" +echo "Bind Configuration Directory......: $CONF_FILE_DIR" +echo "Zones Declaration File............: $ZONES_DECLARATION_FILE" +echo "Zone File Directory (master)......: $ZONE_FILE_MASTER_DIR" + +echo "" +OK= +while [ "$OK" != "yes" -o "$OK" != "no" ] ; do + echononl "Parameters ok? [yes/no]: " + read OK + ## - To lower case + OK=${OK,,} + if [ "X$OK" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + OK="" + continue + fi + if [ "$OK" = "yes" -o "$OK" = "no" ] ; then + break + else + OK="" + continue + fi + echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" done +[[ $OK = "yes" ]] || fatal Repeat execution with different parameters -exit +echo "" + +echononl "\tBackup directory '${ZONE_FILE_MASTER_DIR}'.." +cp -a "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + fatal "$(cat $log_file)" +fi + + +_found=false +_is_master=false +zonefiles_arr=() +zone_file="" +regex_master="type[[:space:]]+master" +regex_file="^[[:space:]]*file" +while IFS='' read -r _line || [[ -n $_line ]] ; do + + if [[ $_line =~ ^[[:space:]]*zone[[:space:]]+ ]]; then + _found=true + zone="$(echo $_line | awk '{print$2}')" + shopt -s extglob + if [[ $zone =~ \; ]]; then + zone=${zone%%*(\;)} + fi + if [[ $zone =~ ^\" ]]; then + zone=${zone##*(\")} + zone=${zone%%*(\")} + fi + shopt -u extglob + fi + if $_found ; then + if [[ $_line =~ $regex_file ]]; 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 =~ $regex_master ]]; then + _is_master=true + fi + if [[ "$_line" =~ ^[[:space:]]*\}[[:space:]]*\; ]]; then + if $_is_master && [[ -n "$zone_file" ]]; then + if $(grep -q -E "^${comment}.*\s*AAAA" "$zone_file" 2> /dev/null) ; then + + + echo -e "\tEditing \033[1m$zone_file\033[m .." + + _replaced=false + + echononl "\t Uncomment all previosly commented IPv6 AAAA Records" + perl -i -n -p -e "s/^${comment}(.*\s+AAAA\s+.*)$/\1/" "$zone_file" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + _replaced=true + else + echo_failed + fi + + # - Calculate new serial + # - + echononl "\t Calculate new serial" + if $_replaced ; then + 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 + echo_done + else + echo_skipped + fi + + + # - Set new serial + # - + echononl "\t Increase serial" + if $_replaced ; then + perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + else + echo_failed + error "$(cat $log_file)" + fi + else + echo_skipped + fi + + + # - Reload Zone + # - + echononl "\t Reload zone '${zone}'" + if $_replaced ; then + /usr/sbin/rndc reload $zone > $log_file 2>&1 + if [[ $? -gt 0 ]]; then + echo_failed + error "$(cat $log_file)" + else + echo_ok + fi + else + echo_skipped + fi + + echo + + fi + fi + _is_master=false + _found=false + zone_file="" + fi + fi +done < "$ZONES_DECLARATION_FILE" + + +if [[ -d "${ZONE_FILE_MASTER_DIR}.${backup_date}" ]] ; then + diff -Nur "${ZONE_FILE_MASTER_DIR}" "${ZONE_FILE_MASTER_DIR}.${backup_date}" > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + info "No zone file has changed.\n\t Removing previously created backup" + echononl "\tDelete '${ZONE_FILE_MASTER_DIR}.${backup_date}'.." + rm -rf "${ZONE_FILE_MASTER_DIR}.${backup_date}" > $log_file 2>&1 + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi + fi +fi + + +echo "" +clean_up 0 diff --git a/bind_reload_all_zones.sh b/bind_reload_all_zones.sh index c8217f9..f9f9976 100755 --- a/bind_reload_all_zones.sh +++ b/bind_reload_all_zones.sh @@ -1,18 +1,25 @@ #!/usr/bin/env bash -## --- Variables (default Values) -## --- -## - file, where zone definitions live.. -## - -_CONF_FILE=/etc/bind/named.conf.local +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/bind.conf" -## --- -## --- End: Variables (default Values) +log_file="$(mktemp)" -## --- some functions -## --- +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +clean_up() { + + # Perform program exit housekeeping + rm $log_file + exit $1 +} + echononl(){ echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then @@ -29,7 +36,7 @@ fatal(){ echo "" echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" echo "" - exit 1 + clean_up 1 } warn (){ @@ -66,25 +73,99 @@ echo_skipped() { echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" } -## --- -## --- END: functions +trap clean_up SIGHUP SIGINT SIGTERM + + + +#--------------------------------------- +#----------------------------- +# Setting Defaults +#----------------------------- +#--------------------------------------- + +DEFAULT_CONF_FILE_DIR="/etc/bind" + + + +#--------------------------------------- +#----------------------------- +# Load default values from bind.conf +# +# Overwrites the settings above +# +#----------------------------- +#--------------------------------------- +#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 "$ZONES_DECLARATION_FILE" ]] ; then + DEFAULT_ZONES_DECLARATION_FILE="$ZONES_DECLARATION_FILE" +else + DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" +fi + + +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 "$ZONES_DECLARATION_FILE" ]] || DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" + echo "" echo -e "\033[32m--\033[m" + echo "" -echo " Enter filename containing the zone definitions" +echo "Insert zones declaration file." echo "" -echo "" -CONF_FILE= -echononl "Filename containing the zone definitions [$_CONF_FILE]: " -read CONF_FILE -if [ "X$CONF_FILE" = "X" ]; then - CONF_FILE=$_CONF_FILE +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 -if [ ! -f "$CONF_FILE" ]; then - fatal File not found: $CONF_FILE -fi clear echo "" @@ -93,7 +174,8 @@ echo -e "\033[21G\033[32mReload all (master) Zones\033[m" echo "" echo "" -echo "File containing zone definitions..: $CONF_FILE" +echo "Bind Configuration Directory......: $CONF_FILE_DIR" +echo "Zones Declaration File............: $ZONES_DECLARATION_FILE" echo "" OK= @@ -145,11 +227,11 @@ while IFS='' read -r line || [[ -n "$line" ]] ; do echo_failed fi fi -done < $CONF_FILE +done < $ZONES_DECLARATION_FILE -[[ $number -eq 0 ]] && info "No Zonedefinition (master) found in file $CONF_FILE ." +[[ $number -eq 0 ]] && info "No Zonedefinition (master) found in file $ZONES_DECLARATION_FILE ." info $number Zones reloaded. echo -exit +clean_up 0