From bbb8c5a02305351682e4312d3f237872fa268e4c Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 1 Aug 2017 12:13:46 +0200 Subject: [PATCH] - Integrate configuration file to script bind_change_ip.sh. - Read Zonefiles from bind configuration instead from given zone file directory. --- bind_change_ip.sh | 449 +++++++++++++++++++++++++++++++++++------- conf/bind.conf.sample | 7 + 2 files changed, 381 insertions(+), 75 deletions(-) diff --git a/bind_change_ip.sh b/bind_change_ip.sh index 382e2d9..317f815 100755 --- a/bind_change_ip.sh +++ b/bind_change_ip.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 (){ @@ -110,8 +119,73 @@ 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 "" echo -e "\033[32m--\033[m" @@ -132,7 +206,7 @@ while [ "X$IPv4_ADDRESS_OLD" = "X" ]; do IPv4_ADDRESS_OLD="" continue fi - if [ "$IPv4_ADDRESS_OLD" = "none" ];then + if [ "${IPv4_ADDRESS_OLD,,}" = "none" ];then _set_ipv4=false break fi @@ -203,7 +277,7 @@ echo -e "\033[32m--\033[m" echo "" echo "Insert IPv6-Address which do you want to change (old address)." echo "" -echo -e "Type \"\033[33mNone\033[m\" if no IPv6 address should be changed." +echo -e "Type \"\033[33mnone\033[m\" if no IPv6 address should be changed." echo "" IPv6_ADDRESS_OLD= _set_ipv6=true @@ -217,14 +291,13 @@ while [ "X$IPv6_ADDRESS_OLD" = "X" ]; do IPv6_ADDRESS_OLD="" continue fi - if [ "$IPv6_ADDRESS_OLD" = "none" ];then + if [ "${IPv6_ADDRESS_OLD,,}" = "none" ];then _set_ipv6=false break fi ipv6_addresses_old_arr=() for _ipv6_address in $IPv6_ADDRESS_OLD ; do - echo "$_ipv6_address" containsElement "$_ipv6_address" "${ipv6_addresses_old_arr[@]}" && continue ipv6_addresses_old_arr+=("$_ipv6_address") done @@ -289,37 +362,51 @@ 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_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 +#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 "" @@ -350,6 +437,109 @@ 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 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 "" echo "" @@ -373,8 +563,10 @@ fi 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 (master)......: $ZONE_FILE_MASTER_DIR" +#echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX" echo "" OK= @@ -398,38 +590,130 @@ done echo "" -cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M` +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 +#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" + +_found=false +_is_master=false zonefiles_arr=() -if $_set_ipv4 ; then - _zone_files_ipv4=`grep -l -e "$IPv4_ADDRESS_OLD" ${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 +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 -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 $_set_ipv6 ; then - _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 $_set_ipv4 ; then + + if $(grep -q -e "$IPv4_ADDRESS_OLD" "$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 + fi + + + + if $_set_ipv6 ; then + if $(grep -q -e "$IPv6_ADDRESS_OLD" "$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 + fi + + fi + _is_master=false + _found=false + zone_file="" + fi fi -fi +done < "$ZONES_DECLARATION_FILE" + +echo "" + + + + for zone_file in ${zonefiles_arr[@]} ; do @@ -523,5 +807,20 @@ for zone_file in ${zonefiles_arr[@]} ; do done +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 "" -exit +clean_up 0 diff --git a/conf/bind.conf.sample b/conf/bind.conf.sample index 141d2df..3386ce9 100644 --- a/conf/bind.conf.sample +++ b/conf/bind.conf.sample @@ -31,6 +31,13 @@ #ZONE_FILE_SLAVE_DIR="${CONF_FILE_DIR}/slave" +# - ZONE_FILE_SUFFIX +# - +# - Defaults to 'zone' +# - +#ZONE_FILE_SUFFIX="zone" + + # - ZONES_DECLARATION_FILE # - # - File containing zone declarations