From af94d5de33e653cc685c14f6cae845e3d04b9ed9 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 2 Aug 2017 03:37:04 +0200 Subject: [PATCH] - Some minor fixes on script 'bind_change_ip.sh'. - Redesign script 'bind_change_ttl.sh'. - Redesign script 'bind_set_ttl_to_default.sh'. --- bind_change_ip.sh | 9 +- bind_change_ttl.sh | 434 +++++++++++++++++++++++++++++-------- bind_set_ttl_to_default.sh | 351 +++++++++++++++++++++++------- 3 files changed, 620 insertions(+), 174 deletions(-) diff --git a/bind_change_ip.sh b/bind_change_ip.sh index 317f815..1ab11d5 100755 --- a/bind_change_ip.sh +++ b/bind_change_ip.sh @@ -687,8 +687,6 @@ while IFS='' read -r _line || [[ -n $_line ]] ; do fi fi - - if $_set_ipv6 ; then if $(grep -q -e "$IPv6_ADDRESS_OLD" "$zone_file") ; then if [ ${#zonefiles_arr[@]} -eq 0 ] ; then @@ -713,12 +711,9 @@ echo "" - - - for zone_file in ${zonefiles_arr[@]} ; do - echo -e "\tconverting $zone_file.." + echo -e "\tEditing \033[1m$zone_file\033[m .." ## - calculate new serial ## - @@ -777,7 +772,7 @@ for zone_file in ${zonefiles_arr[@]} ; do if $_replaced_ipv6 && $IGNORE_ALT_HOSTNAMES ; then echononl "\t Setting back hostnames containing \"-alt\".." - perl -i -n -p -e "s#^(.+(-alt).*)\s+IN\s+A\s+$IPv6_ADDRESS_NEW#\1 IN A $IPv6_ADDRESS_OLD#" $zone_file > /dev/null 2>&1 + perl -i -n -p -e "s#^(.+(-alt).*)\s+IN\s+AAAA\s+$IPv6_ADDRESS_NEW#\1 IN AAAA $IPv6_ADDRESS_OLD#" $zone_file > /dev/null 2>&1 if [ "$?" = "0" ]; then echo_ok else diff --git a/bind_change_ttl.sh b/bind_change_ttl.sh index 4f2abfa..c2dce83 100755 --- a/bind_change_ttl.sh +++ b/bind_change_ttl.sh @@ -1,20 +1,29 @@ #!/usr/bin/env bash -## --- Variables (default Values) -## --- -_zone_file_dir=/etc/bind/master -#_zone_file_dir=/root/tmp/master + +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/bind.conf" + +log_file="$(mktemp)" + +backup_date="$(date +%Y-%m-%d-%H%M)" _serial_new=`date +%Y%m%d01` -_zone_file_suffix=zone -## --- -## --- End: Variables (default Values) +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- +clean_up() { + + # Perform program exit housekeeping + rm $log_file + exit $1 +} -## --- some functions -## --- echononl(){ echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then @@ -31,7 +40,7 @@ fatal(){ echo "" echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" echo "" - exit 1 + clean_up 1 } warn (){ @@ -61,6 +70,9 @@ error(){ 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 ]" } @@ -110,8 +122,71 @@ is_valid_ipv6() { fi } -## --- -## --- 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 "$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 "$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 + DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" +fi + echo "" echo -e "\033[32m--\033[m" @@ -182,9 +257,15 @@ while [ "X$IPv6_ADDRESS" = "X" ]; do done done + +if ! $_set_ipv6 && ! $_set_ipv4 ; then + fatal "No IP-Adresses given to change TTL for.." +fi + echo "" echo -e "\033[32m--\033[m" echo "" + echo "Insert New TTL for the given IP-Address(es)" echo "" echo -e "[ \033[33mTIP\033[m ]: Choose a extraordinary Number (like 363 or 181), so you can" @@ -212,44 +293,6 @@ while [ "X$TTL" = "X" ]; do done -echo "" -echo -e "\033[32m--\033[m" -echo "" -echo "Insert the directory, where your zone-files resides." -echo "" -echo "" -ZONE_FILE_DIR= -while [ "X$ZONE_FILE_DIR" = "X" ]; do - echononl "Zone File Directory [$_zone_file_dir]: " - read ZONE_FILE_DIR - if [ "X$ZONE_FILE_DIR" = "X" ]; then - ZONE_FILE_DIR=$_zone_file_dir - fi - if [ ! -d $ZONE_FILE_DIR ]; then - echo -e "\n\tDirectory \033[33m\033[1m$ZONE_FILE_DIR\033[m does NOT exist!\n" - ZONE_FILE_DIR= - fi -done - -echo "" -echo -e "\033[32m--\033[m" -echo "" -echo "Insert the file-suffix of thr zone-files" -echo "" -echo "" -ZONE_FILE_SUFFIX= -echononl "Suffix of Zone Files [$_zone_file_suffix]: " -read ZONE_FILE_SUFFIX -if [ "X$ZONE_FILE_SUFFIX" = "X" ]; then - ZONE_FILE_SUFFIX=$_zone_file_suffix -fi - - -if ! $_set_ipv6 && ! $_set_ipv4 ; then - fatal "No IP-Adresses given to change TTL for.." -fi - - echo "" echo -e "\033[32m--\033[m" echo "" @@ -277,6 +320,100 @@ while [ "$OK" != "yes" -o "$OK" != "no" ] ; do done [[ $OK = "yes" ]] && IGNORE_ALT_HOSTNAMES=true + +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 "$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 -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 thr zone-files" +#echo "" +#echo "" +#ZONE_FILE_SUFFIX= +#echononl "Suffix of Zone Files [$_zone_file_suffix]: " +#read ZONE_FILE_SUFFIX +#if [ "X$ZONE_FILE_SUFFIX" = "X" ]; then +# ZONE_FILE_SUFFIX=$_zone_file_suffix +#fi + + clear echo "" echo "" @@ -299,8 +436,10 @@ echo "New TTL...........................: $TTL" echo "" echo "Ignore hostnames containing \"-alt\": $IGNORE_ALT_HOSTNAMES" echo "" -echo "Zone File Directory...............: $ZONE_FILE_DIR" -echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX" +echo "Bind Configuration Directory......: $CONF_FILE_DIR" +echo "Zones Declaration File............: $ZONES_DECLARATION_FILE" +echo "Zone File Directory...............: $ZONE_FILE_MASTER_DIR" +#echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX" echo "" OK= @@ -324,64 +463,166 @@ done [[ $OK = "yes" ]] || fatal Repeat execution with different parameters -cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M` +echo "" +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 +echo "" +echononl "\tCreate array of given IPv4 addresses" ipv4_addresses_arr=() for _ipv4_address in $IPv4_ADDRESS ; do containsElement "$_ipv4_address" "${ipv4_addresses_arr[@]}" && continue ipv4_addresses_arr+=("$_ipv4_address") done +echo_done +echononl "\tCreate array of given IPv6 addresses" ipv6_addresses_arr=() for _ipv6_address in $IPv6_ADDRESS ; do containsElement "$_ipv6_address" "${ipv6_addresses_arr[@]}" && continue ipv6_addresses_arr+=("$_ipv6_address") done +echo_done +#zonefiles_arr=() +#if $_set_ipv4 ; then +# for _ipv4_address in ${ipv4_addresses_arr[@]} ; do +# _zone_files_ipv4=`grep -l -e "$_ipv4_address" ${ZONE_FILE_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 +# done +#fi +# +#if $_set_ipv6 ; then +# for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do +# _zone_files_ipv6=`grep -l -e "$_ipv6_address" ${ZONE_FILE_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 +# done +#fi + + +echo "" +echo "" +echo -e "\033[37m\033[1mDetermine zone files conatining ip-adressse for which TTL time is requested to change..\033[m" + +_found=false +_is_master=false zonefiles_arr=() -if $_set_ipv4 ; then - for _ipv4_address in ${ipv4_addresses_arr[@]} ; do - _zone_files_ipv4=`grep -l -e "$_ipv4_address" ${ZONE_FILE_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 - done -fi +zone_file="" +regex_master="type[[:space:]]+master" +regex_file="^[[:space:]]*file" +while IFS='' read -r _line || [[ -n $_line ]] ; do -if $_set_ipv6 ; then - for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do - _zone_files_ipv6=`grep -l -e "$_ipv6_address" ${ZONE_FILE_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 + 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 - done -fi + if [[ $_line =~ $regex_master ]]; then + _is_master=true + fi + if [[ "$_line" =~ ^[[:space:]]*\}[[:space:]]*\; ]]; then + if $_is_master && [[ -n "$zone_file" ]]; then + + + if $_set_ipv4 ; then + + for _ipv4_address in "${ipv4_addresses_arr[@]}" ; do + if $(grep -q -E "IN\s+A\s+$_ipv4_address" "$zone_file") ; then + if [ ${#zonefiles_arr[@]} -eq 0 ] ; then + zonefiles_arr+=("$zone_file") + else + if ! containsElement "$zone_file" "${zonefiles_arr[@]}" ; then + zonefiles_arr+=("$zone_file") + fi + fi + fi + done + + fi + + if $_set_ipv6 ; then + + for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do + if $(grep -q -E "IN\s+AAAA\s+$_ipv6_address" "$zone_file") > /dev/null 2>&1 ; then + if [ ${#zonefiles_arr[@]} -eq 0 ] ; then + zonefiles_arr+=("$zone_file") + else + if ! containsElement "$zone_file" "${zonefiles_arr[@]}" ; then + zonefiles_arr+=("$zone_file") + fi + fi + fi + done + fi + + fi + _is_master=false + _found=false + zone_file="" + fi + fi +done < "$ZONES_DECLARATION_FILE" + +echo "" + #for _val in "${zonefiles_arr[@]}" ; do # echo # echo -e "$_val" #done # -#exit +#clean_up 0 for zone_file in ${zonefiles_arr[@]} ; do - echo -e "\n\tconverting $zone_file .." + echo -e "\n\tEditing \033[37m\033[1m$zone_file\033[m .." _replaced=false @@ -395,7 +636,7 @@ for zone_file in ${zonefiles_arr[@]} ; do if $_set_ipv4 ; then for _ipv4_address in "${ipv4_addresses_arr[@]}" ; do - if grep -e "IN\s*A\s*$_ipv4_address" $zone_file > /dev/null 2>&1 ; then + if $(grep -q -E "IN\s+A\s+$_ipv4_address" "$zone_file") ; then ## - setze neue ttl für ipv4 address ## - @@ -434,7 +675,7 @@ for zone_file in ${zonefiles_arr[@]} ; do if $_set_ipv6 ; then for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do - if grep -e "IN\s*AAAA\s*$_ipv6_address" $zone_file > /dev/null 2>&1 ; then + if $(grep -q -E "IN\s+AAAA\s+$_ipv6_address" "$zone_file") > /dev/null 2>&1 ; then ## - setze neue ttl für ipv6 address ## - @@ -457,7 +698,7 @@ for zone_file in ${zonefiles_arr[@]} ; do fi if $_replaced && $IGNORE_ALT_HOSTNAMES ; then echononl "\t Setting back hostnames containing \"-alt\".." - perl -i -n -p -e "s#^(.+(-alt).*)\s+$TTL\s+IN\s+AAAA\s+$_ipv6_address#\1 IN A $_ipv6_address#" $zone_file > /dev/null 2>&1 + perl -i -n -p -e "s#^(.+(-alt).*)\s+$TTL\s+IN\s+AAAA\s+$_ipv6_address#\1 IN AAAA $_ipv6_address#" $zone_file > /dev/null 2>&1 if [ "$?" = "0" ]; then echo_ok else @@ -488,5 +729,20 @@ for zone_file in ${zonefiles_arr[@]} ; do done -echo -exit + +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_set_ttl_to_default.sh b/bind_set_ttl_to_default.sh index f9605c4..52d772f 100755 --- a/bind_set_ttl_to_default.sh +++ b/bind_set_ttl_to_default.sh @@ -1,20 +1,29 @@ #!/usr/bin/env bash -## --- Variables (default Values) -## --- -#_zone_file_dir=/etc/bind/master -_zone_file_dir=/root/tmp/master +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/bind.conf" + +log_file="$(mktemp)" + +backup_date="$(date +%Y-%m-%d-%H%M)" _serial_new=`date +%Y%m%d01` -_zone_file_suffix=zone -## --- -## --- End: Variables (default Values) +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +clean_up() { + + # Perform program exit housekeeping + rm $log_file + exit $1 +} -## --- some functions -## --- echononl(){ echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then @@ -31,7 +40,7 @@ fatal(){ echo "" echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" echo "" - exit 1 + clean_up 1 } warn (){ @@ -61,6 +70,9 @@ error(){ 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 ]" } @@ -110,8 +122,71 @@ is_valid_ipv6() { fi } -## --- -## --- 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 "$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 "$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 + DEFAULT_ZONES_DECLARATION_FILE="${CONF_FILE_DIR}/named.conf.local" +fi + echo "" echo -e "\033[32m--\033[m" @@ -178,44 +253,90 @@ while [ "X$IPv6_ADDRESS" = "X" ]; do fi done +if ! $_set_ipv6 && ! $_set_ipv4 ; then + fatal "No IP-Adresses given for changing their TTL to the zone-file default.." +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 "$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 -e "\033[32m--\033[m" + echo "" echo "Insert the directory, where your zone-files resides." echo "" echo "" -ZONE_FILE_DIR= -while [ "X$ZONE_FILE_DIR" = "X" ]; do - echononl "Zone File Directory [$_zone_file_dir]: " - read ZONE_FILE_DIR - if [ "X$ZONE_FILE_DIR" = "X" ]; then - ZONE_FILE_DIR=$_zone_file_dir +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 - if [ ! -d $ZONE_FILE_DIR ]; then - echo -e "\n\tDirectory \033[33m\033[1m$ZONE_FILE_DIR\033[m does NOT exist!\n" - ZONE_FILE_DIR= - fi -done - -echo "" -echo -e "\033[32m--\033[m" -echo "" -echo "Insert the file-suffix of thr zone-files" -echo "" -echo "" -ZONE_FILE_SUFFIX= -echononl "Suffix of Zone Files [$_zone_file_suffix]: " -read ZONE_FILE_SUFFIX -if [ "X$ZONE_FILE_SUFFIX" = "X" ]; then - ZONE_FILE_SUFFIX=$_zone_file_suffix +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 -if ! $_set_ipv6 && ! $_set_ipv4 ; then - fatal "No IP-Adresses given for changing their TTL to the zone-file default.." -fi - clear echo "" echo "" @@ -236,8 +357,10 @@ fi echo "" echo "New TTL...........................: Zonefile default" echo "" -echo "Zone File Directory...............: $ZONE_FILE_DIR" -echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX" +echo "Bind Configuration Directory......: $CONF_FILE_DIR" +echo "Zones Declaration File............: $ZONES_DECLARATION_FILE" +echo "Zone File Directory...............: $ZONE_FILE_MASTER_DIR" +#echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX" echo "" OK= @@ -262,57 +385,114 @@ done [[ $OK = "yes" ]] || fatal Repeat execution with different parameters -cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M` +echo "" +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 +echo "" +echononl "\tCreate array of given IPv4 addresses" ipv4_addresses_arr=() for _ipv4_address in $IPv4_ADDRESS ; do containsElement "$_ipv4_address" "${ipv4_addresses_arr[@]}" && continue ipv4_addresses_arr+=("$_ipv4_address") done +echo_done +echononl "\tCreate array of given IPv6 addresses" ipv6_addresses_arr=() for _ipv6_address in $IPv6_ADDRESS ; do containsElement "$_ipv6_address" "${ipv6_addresses_arr[@]}" && continue ipv6_addresses_arr+=("$_ipv6_address") done +echo_done + +echo "" +echo "" +echo -e "\033[37m\033[1mDetermine zone files conatining ip-adressse for which TTL time is requested to change..\033[m" + +_found=false +_is_master=false zonefiles_arr=() -if $_set_ipv4 ; then - for _ipv4_address in "${ipv4_addresses_arr[@]}" ; do - _zone_files_ipv4=`grep -l -e "$_ipv4_address" ${ZONE_FILE_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 - done -fi +zone_file="" +regex_master="type[[:space:]]+master" +regex_file="^[[:space:]]*file" +while IFS='' read -r _line || [[ -n $_line ]] ; do -if $_set_ipv6 ; then - for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do - _zone_files_ipv6=`grep -l -e "$_ipv6_address" ${ZONE_FILE_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 + if [[ $_line =~ ^[[:space:]]*zone[[:space:]]+ ]]; then + _found=true + 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 - done -fi + if [[ $_line =~ $regex_master ]]; then + _is_master=true + fi + if [[ "$_line" =~ ^[[:space:]]*\}[[:space:]]*\; ]]; then + if $_is_master && [[ -n "$zone_file" ]]; then + if $_set_ipv4 ; then + + for _ipv4_address in "${ipv4_addresses_arr[@]}" ; do + if $(grep -q -E "IN\s+A\s+$_ipv4_address" "$zone_file") ; then + if [ ${#zonefiles_arr[@]} -eq 0 ] ; then + zonefiles_arr+=("$zone_file") + else + if ! containsElement "$zone_file" "${zonefiles_arr[@]}" ; then + zonefiles_arr+=("$zone_file") + fi + fi + fi + done + + fi + + if $_set_ipv6 ; then + + for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do + if $(grep -q -E "IN\s+AAAA\s+$_ipv6_address" "$zone_file") > /dev/null 2>&1 ; then + if [ ${#zonefiles_arr[@]} -eq 0 ] ; then + zonefiles_arr+=("$zone_file") + else + if ! containsElement "$zone_file" "${zonefiles_arr[@]}" ; then + zonefiles_arr+=("$zone_file") + fi + fi + fi + done + fi + + fi + _is_master=false + _found=false + zone_file="" + fi + fi +done < "$ZONES_DECLARATION_FILE" + +echo "" + for zone_file in ${zonefiles_arr[@]} ; do - echo -e "\n\tconverting $zone_file .." + echo -e "\n\tEditing \033[37m\033[1m$zone_file\033[m .." _replaced=false @@ -326,14 +506,14 @@ for zone_file in ${zonefiles_arr[@]} ; do if $_set_ipv4 ; then for _ipv4_address in "${ipv4_addresses_arr[@]}" ; do - if grep -e "IN\s*A\s*$_ipv4_address" $zone_file > /dev/null 2>&1 ; then + if $(grep -q -E "IN\s+A\s+$_ipv4_address" "$zone_file") ; then ## - setze neue ttl für ipv4 address ## - echononl "\t Set TTL to the zonfile default for IPv4 address $_ipv4_address .." if grep -e "\s*[0-9][0-9]\s*IN\s*A\s*$_ipv4_address" $zone_file > /dev/null 2>&1 ; then - perl -i -n -p -e "s/^(.+\s+)[0-9]{2,}(\s+IN\s+A\s+$_ipv4_address)/\1\2/" $zone_file + perl -i -n -p -e "s/^(.+\s+)[0-9]{2,}\s+(IN\s+A\s+$_ipv4_address)/\1\2/" $zone_file if [ "$?" = "0" ]; then echo_ok _replaced=true @@ -352,14 +532,14 @@ for zone_file in ${zonefiles_arr[@]} ; do if $_set_ipv6 ; then for _ipv6_address in "${ipv6_addresses_arr[@]}" ; do - if grep -e "IN\s*AAAA\s*$_ipv6_address" $zone_file > /dev/null 2>&1 ; then + if $(grep -q -E "IN\s+AAAA\s+$_ipv6_address" "$zone_file") > /dev/null 2>&1 ; then ## - setze neue ttl für ipv6 address ## - echononl "\t Set TTL to the zonfile default for IPv6 address $_ipv6_address .." if grep -e "\s*[0-9][0-9]\s*IN\s*AAAA\s*$_ipv6_address" $zone_file > /dev/null 2>&1 ; then - perl -i -n -p -e "s/^(.+\s+)[0-9]{2,}(\s+IN\s+AAAA\s+$_ipv6_address)/\1\2/" $zone_file + perl -i -n -p -e "s/^(.+\s+)[0-9]{2,}\s+(IN\s+AAAA\s+$_ipv6_address)/\1\2/" $zone_file if [ "$?" = "0" ]; then echo_ok _replaced=true @@ -393,5 +573,20 @@ for zone_file in ${zonefiles_arr[@]} ; do done -echo -exit + +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