From efeacd0aa5792b10ff2ce4414f52d5e48177cd68 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 4 Aug 2017 00:15:29 +0200 Subject: [PATCH] - Add/Archive old versions of script. - Add some Readme's. --- OLD_Versions/bind_change_SOA.sh | 117 ++++ OLD_Versions/bind_change_ip.sh | 527 ++++++++++++++++++ OLD_Versions/bind_change_ttl.sh | 492 ++++++++++++++++ OLD_Versions/bind_disable_ipv6.sh | 40 ++ OLD_Versions/bind_enable_ipv6.sh | 40 ++ OLD_Versions/bind_get_domain_by_hostname.sh | 113 ++++ OLD_Versions/bind_get_zonefile_by_hostname.sh | 120 ++++ OLD_Versions/bind_reload_all_zones.sh | 155 ++++++ OLD_Versions/bind_set_new_serial.sh | 281 ++++++++++ OLD_Versions/bind_set_renew_tlsa.sh | 351 ++++++++++++ OLD_Versions/bind_set_ttl_to_default.sh | 397 +++++++++++++ README.fix_journal_out_of_sync | 32 ++ README.manual_update_dynamic_zone | 14 + 13 files changed, 2679 insertions(+) create mode 100755 OLD_Versions/bind_change_SOA.sh create mode 100755 OLD_Versions/bind_change_ip.sh create mode 100755 OLD_Versions/bind_change_ttl.sh create mode 100755 OLD_Versions/bind_disable_ipv6.sh create mode 100755 OLD_Versions/bind_enable_ipv6.sh create mode 100755 OLD_Versions/bind_get_domain_by_hostname.sh create mode 100755 OLD_Versions/bind_get_zonefile_by_hostname.sh create mode 100755 OLD_Versions/bind_reload_all_zones.sh create mode 100755 OLD_Versions/bind_set_new_serial.sh create mode 100755 OLD_Versions/bind_set_renew_tlsa.sh create mode 100755 OLD_Versions/bind_set_ttl_to_default.sh create mode 100644 README.fix_journal_out_of_sync create mode 100644 README.manual_update_dynamic_zone diff --git a/OLD_Versions/bind_change_SOA.sh b/OLD_Versions/bind_change_SOA.sh new file mode 100755 index 0000000..e3f6edf --- /dev/null +++ b/OLD_Versions/bind_change_SOA.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash + +#_zone_files_dir="/etc/bind/master" +_zone_files_dir=/root/tmp/master + +## - Settings SOA Record +## - +_soa_nameserver="a\.ns\.oopen\.de" +_soa_admin="domreg\.oopen\.de" + +cp -a $_zone_files_dir ${_zone_files_dir}.BAK-`date +%Y-%m-%d-%H%M` + +## --- some functions +## --- +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[1mInstalllation wird abgebrochen\033[m\033[m" + echo "" + exit 1 +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +echo_ok() { + echo -e "\033[75G[ \033[32mok\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 ]" +} +## --- +## --- END: functions + + +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 "" +exit diff --git a/OLD_Versions/bind_change_ip.sh b/OLD_Versions/bind_change_ip.sh new file mode 100755 index 0000000..382e2d9 --- /dev/null +++ b/OLD_Versions/bind_change_ip.sh @@ -0,0 +1,527 @@ +#!/usr/bin/env bash + + +## --- Variables (default Values) +## --- +_zone_file_dir=/etc/bind/master +#_zone_file_dir=/root/tmp/master + +_serial_new=`date +%Y%m%d01` + +_zone_file_suffix=zone +## --- +## --- End: Variables (default Values) + + +## --- some functions +## --- +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 "" + exit 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_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 +} + + +## - Test of valid IPv4 Address +## - +## - Returns 0 if valid, > 0 otherwise +## - +is_valid_ipv4() { + local -a octets=( ${1//\./ } ) + local RETURNVALUE=0 + + # return an error if the IP doesn't have exactly 4 octets + [[ ${#octets[@]} -ne 4 ]] && return 1 + + for octet in ${octets[@]} + do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] + then # shift number by 8 bits, anything larger than 255 will be > 0 + ((RETURNVALUE += octet>>8 )) + else # octet wasn't numeric, return error + return 1 + fi + done + return ${RETURNVALUE} +} + +is_valid_ipv6() { + local _ipv6=$1 + + if [ "$1" != "${1#[0-9a-f]*:}" ] \ + && [ "$1" = "${1#*[^0-9a-f:]}" ] \ + && [ "${1#*[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]}" = "${1#*:*:*:*:*:*:*:*:*:}" ]; then + return 0 + else + return 1 + fi +} + +## --- +## --- END: functions + +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert IPv4-Address which do you want to change (old address)." +echo "" +echo -e "Type \"\033[33mNone\033[m\" if no IPv4 address should be changed." +echo "" +IPv4_ADDRESS_OLD= +_set_ipv4=true +while [ "X$IPv4_ADDRESS_OLD" = "X" ]; do + echononl "Old IPv4-Address: " + read IPv4_ADDRESS_OLD + ## - To lower case + IPv4_ADDRESS_OLD=${IPv4_ADDRESS_OLD,,} + if [ "X$IPv4_ADDRESS_OLD" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv4_ADDRESS_OLD="" + continue + fi + if [ "$IPv4_ADDRESS_OLD" = "none" ];then + _set_ipv4=false + break + fi + + ipv4_addresses_old_arr=() + for _ipv4_address in $IPv4_ADDRESS_OLD ; do + containsElement "$_ipv4_address" "${ipv4_addresses_old_arr[@]}" && continue + ipv4_addresses_old_arr+=("$_ipv4_address") + done + if [ ${#ipv4_addresses_old_arr[@]} -gt 1 ] ; then + echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n" + IPv4_ADDRESS_OLD="" + continue + fi + unset ipv4_addresses_old_arr + + if ! is_valid_ipv4 $IPv4_ADDRESS_OLD ; then + echo -e "\n\t\033[33m\033[1m$IPv4_ADDRESS_OLD\033[m is NOT a valid IPv4 Address\n" + IPv4_ADDRESS_OLD="" + continue + fi +done + +if $_set_ipv4 ; then + + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo "Insert IPv4-Address which do you want to replace (new address)." + echo "" + + IPv4_ADDRESS_NEW= + _set_ipv4=true + while [ "X$IPv4_ADDRESS_NEW" = "X" ]; do + echononl "New IPv4-Address: " + read IPv4_ADDRESS_NEW + ## - To lower case + IPv4_ADDRESS_NEW=${IPv4_ADDRESS_NEW,,} + if [ "X$IPv4_ADDRESS_NEW" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv4_ADDRESS_NEW="" + continue + fi + + ipv4_addresses_new_arr=() + for _ipv4_address in $IPv4_ADDRESS_NEW ; do + containsElement "$_ipv4_address" "${ipv4_addresses_new_arr[@]}" && continue + ipv4_addresses_new_arr+=("$_ipv4_address") + done + if [ ${#ipv4_addresses_new_arr[@]} -gt 1 ] ; then + echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n" + IPv4_ADDRESS_NEW="" + continue + fi + unset ipv4_addresses_new_arr + + if ! is_valid_ipv4 $IPv4_ADDRESS_NEW ; then + echo -e "\n\t\033[33m\033[1m$IPv4_ADDRESS_NEW\033[m is NOT a valid IPv4 Address\n" + IPv4_ADDRESS_NEW="" + continue + fi + done +fi + + +echo "" +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 "" +IPv6_ADDRESS_OLD= +_set_ipv6=true +while [ "X$IPv6_ADDRESS_OLD" = "X" ]; do + echononl "Old IPv6-Address: " + read IPv6_ADDRESS_OLD + ## - To lower case + IPv6_ADDRESS_OLD=${IPv6_ADDRESS_OLD,,} + if [ "X$IPv6_ADDRESS_OLD" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv6_ADDRESS_OLD="" + continue + fi + 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 + if [ ${#ipv6_addresses_old_arr[@]} -gt 1 ] ; then + echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n" + IPv6_ADDRESS_OLD="" + continue + fi + unset ipv6_addresses_old_arr + + if ! is_valid_ipv6 $IPv6_ADDRESS_OLD ; then + echo -e "\n\t\033[33m\033[1m$IPv6_ADDRESS_OLD\033[m is NOT a valid IPv6 Address\n" + IPv6_ADDRESS_OLD="" + continue + fi +done + +if $_set_ipv6 ; then + + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo "Insert IPv6-Address which do you want to replace (new address)." + echo "" + + IPv6_ADDRESS_NEW= + _set_ipv6=true + while [ "X$IPv6_ADDRESS_NEW" = "X" ]; do + echononl "New IPv6-Address: " + read IPv6_ADDRESS_NEW + ## - To lower case + IPv6_ADDRESS_NEW=${IPv6_ADDRESS_NEW,,} + if [ "X$IPv6_ADDRESS_NEW" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv6_ADDRESS_NEW="" + continue + fi + + ipv6_addresses_new_arr=() + for _ipv6_address in $IPv6_ADDRESS_NEW ; do + containsElement "$_ipv6_address" "${ipv6_addresses_new_arr[@]}" && continue + ipv6_addresses_new_arr+=("$_ipv6_address") + done + if [ ${#ipv6_addresses_new_arr[@]} -gt 1 ] ; then + echo -e "\n\t\033[33m\033[1mOnly one address is possible!\033[m\n" + IPv6_ADDRESS_NEW="" + continue + fi + unset ipv6_addresses_new_arr + + if ! is_valid_ipv6 $IPv6_ADDRESS_NEW ; then + echo -e "\n\t\033[33m\033[1m$IPv6_ADDRESS_NEW\033[m is NOT a valid IPv6 Address\n" + IPv6_ADDRESS_NEW="" + continue + fi + done +fi + + +if ! $_set_ipv6 && ! $_set_ipv4 ; then + fatal "Neither IPv4-Adress nor IPv6-Adress given .." +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 "Ignore Hostnames containing \"-alt\" (as ww-alt.oopen.de or d-alt.mx.oopen.de)" +echo "" +echo "" +OK= +IGNORE_ALT_HOSTNAMES=false +while [ "$OK" != "yes" -o "$OK" != "no" ] ; do + echononl "Ignore Hostnames containing \"-alt\"? [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="" + fi + echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" +done +[[ $OK = "yes" ]] && IGNORE_ALT_HOSTNAMES=true + + +clear +echo "" +echo "" +echo -e "\033[21G\033[32mReplace IP-Address(es)\033[m" +echo "" + +echo "" +if $_set_ipv4 ; then + echo "Old IPv4 Address..................: $IPv4_ADDRESS_OLD" + echo "New IPv4 Address..................: $IPv4_ADDRESS_NEW" +else + echo -e "IPv4 Address(es)..................: \033[33mNone\033[m" +fi +echo "" +if $_set_ipv6 ; then + echo "Old IPv6 Address..................: $IPv6_ADDRESS_OLD" + echo "New IPv6 Address..................: $IPv6_ADDRESS_NEW" +else + echo -e "IPv6 Address(es)..................: \033[33mNone\033[m" +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 "" +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 + fi + echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" +done +[[ $OK = "yes" ]] || fatal Repeat execution with different parameters + +echo "" + + +cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M` + + +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 + fi +fi + + +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 + fi +fi + + +for zone_file in ${zonefiles_arr[@]} ; do + + echo -e "\tconverting $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 + + ## - setze neue ipv4 adresse + ## - + echononl "\t Set new IPv4 address ($IPv4_ADDRESS_NEW).." + + _replaced=false + if grep -e "IN\s*A\s*$IPv4_ADDRESS_OLD" $zone_file > /dev/null 2>&1 ; then + perl -i -n -p -e "s#IN\s+A\s+$IPv4_ADDRESS_OLD#IN A $IPv4_ADDRESS_NEW#" $zone_file + if [ "$?" = "0" ]; then + echo_ok + _replaced=true + else + echo_failed + error "Replacing $IPv4_ADDRESS_OLD with $IPv4_ADDRESS_NEW in zone file \"$zone_file\" failed!" + fi + else + echo_skipped + fi + + if $_replaced && $IGNORE_ALT_HOSTNAMES ; then + echononl "\t Setting back hostnames containing \"-alt\".." + perl -i -n -p -e "s#^(.+(-alt).*)\s+IN\s+A\s+$IPv4_ADDRESS_NEW#\1 IN A $IPv4_ADDRESS_OLD#" $zone_file > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + else + echo_failed + error "Setting back hostnames containing \"-alt\" to $IPv4_ADDRESS_OLD in zone file \"$zone_file\" failed!" + fi + fi + + ## - setze neue ipv6 adresse + ## - + echononl "\t Set new IPv6 address ($IPv6_ADDRESS_NEW).." + + _replaced_ipv6=false + if grep -e "IN\s*AAAA\s*$IPv6_ADDRESS_OLD" $zone_file > /dev/null 2>&1 ; then + perl -i -n -p -e "s#IN\s+AAAA\s+$IPv6_ADDRESS_OLD#IN AAAA $IPv6_ADDRESS_NEW#" $zone_file + if [ "$?" = "0" ]; then + echo_ok + _replaced=true + _replaced_ipv6=true + else + echo_failed + error "Replacing $IPv6_ADDRESS_OLD with $IPv6_ADDRESS_NEW in zone file \"$zone_file\" failed!" + fi + else + echo_skipped + fi + + 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 + if [ "$?" = "0" ]; then + echo_ok + else + echo_failed + error "Setting back hostnames containing \"-alt\" to $IPv6_ADDRESS_OLD in zone file \"$zone_file\" failed!" + fi + fi + + ## - setze neue serial + ## - + echo "" + echononl "\t Increase Serial for zone file \"`basename $zone_file`\"" + 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 + _replaced=true + else + echo_failed + error "Increasing Serial for zone file \"`basename $zone_file`\" failed!" + fi + else + echo_skipped + fi + + echo + +done + +echo "" +exit diff --git a/OLD_Versions/bind_change_ttl.sh b/OLD_Versions/bind_change_ttl.sh new file mode 100755 index 0000000..4f2abfa --- /dev/null +++ b/OLD_Versions/bind_change_ttl.sh @@ -0,0 +1,492 @@ +#!/usr/bin/env bash + +## --- Variables (default Values) +## --- +_zone_file_dir=/etc/bind/master +#_zone_file_dir=/root/tmp/master + +_serial_new=`date +%Y%m%d01` + +_zone_file_suffix=zone + +## --- +## --- End: Variables (default Values) + + +## --- some functions +## --- +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 "" + exit 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_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 +} + + +## - Test of valid IPv4 Address +## - +## - Returns 0 if valid, > 0 otherwise +## - +is_valid_ipv4() { + local -a octets=( ${1//\./ } ) + local RETURNVALUE=0 + + # return an error if the IP doesn't have exactly 4 octets + [[ ${#octets[@]} -ne 4 ]] && return 1 + + for octet in ${octets[@]} + do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] + then # shift number by 8 bits, anything larger than 255 will be > 0 + ((RETURNVALUE += octet>>8 )) + else # octet wasn't numeric, return error + return 1 + fi + done + return ${RETURNVALUE} +} + +is_valid_ipv6() { + local _ipv6=$1 + + if [ "$1" != "${1#[0-9a-f]*:}" ] \ + && [ "$1" = "${1#*[^0-9a-f:]}" ] \ + && [ "${1#*[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]}" = "${1#*:*:*:*:*:*:*:*:*:}" ]; then + return 0 + else + return 1 + fi +} + +## --- +## --- END: functions + +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert IPv4-Address(es) to change TTL for." +echo "" +echo "For multiple IPv4 Addresses, insert a blank separated list" +echo "" +echo -e "Type \"\033[33mNone\033[m\" if no IPv4 address should be changed." +echo "" +IPv4_ADDRESS= +_set_ipv4=true +while [ "X$IPv4_ADDRESS" = "X" ]; do + echononl "IPv4-Address: " + read IPv4_ADDRESS + ## - To lower case + IPv4_ADDRESS=${IPv4_ADDRESS,,} + if [ "X$IPv4_ADDRESS" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv4_ADDRESS="" + continue + fi + if [ "$IPv4_ADDRESS" = "none" ];then + _set_ipv4=false + break + fi + for _addr in $IPv4_ADDRESS ; do + if ! is_valid_ipv4 $_addr ; then + echo -e "\n\t\033[33m\033[1m$_addr\033[m is NOT a valid IPv4 Address\n" + IPv4_ADDRESS="" + break + fi + done +done + + +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert IPv6-Address to change TTL for." +echo "" +echo "For multiple IPv6 Addresses, insert a blank separated list" +echo "" +echo -e "Type \"\033[33mNone\033[m\" if no IPv6 address should be changed." +echo "" +IPv6_ADDRESS= +_set_ipv6=true +while [ "X$IPv6_ADDRESS" = "X" ]; do + echononl "IPv6-Address: " + read IPv6_ADDRESS + ## - To lower case + IPv6_ADDRESS=${IPv6_ADDRESS,,} + if [ "X$IPv6_ADDRESS" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv6_ADDRESS="" + continue + fi + if [ "$IPv6_ADDRESS" = "none" ];then + _set_ipv6=false + break + fi + for _addr in $IPv6_ADDRESS ; do + if ! is_valid_ipv6 $_addr ; then + echo -e "\n\t\033[33m\033[1m$_addr\033[m is NOT a valid IPv6 Address\n" + IPv6_ADDRESS="" + break + fi + done +done + +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" +echo " identify it later very simple for setting back." +echo "" +TTL= +regular_expression_number='^[0-9]+$' +while [ "X$TTL" = "X" ]; do + echononl "TTL: " + read TTL + if [ "X$TTL" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + TTL="" + continue + fi + if ! [[ $TTL =~ $regular_expression_number ]] ; then + echo -e "\n\t\033[33m\033[1m$TTL\033[m is NOT a number\n" + TTL="" + continue + elif [[ $TTL -lt "61" ]]; then + echo -e "\n\t\033[33m\033[1m$TTL\033[m Choose a number greater than "60"\n" + TTL="" + continue + fi +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 "" +echo "Ignore Hostnames containing \"-alt\" (as ww-alt.oopen.de or d-alt.mx.oopen.de)" +echo "" +echo "" +OK= +IGNORE_ALT_HOSTNAMES=false +while [ "$OK" != "yes" -o "$OK" != "no" ] ; do + echononl "Ignore Hostnames containing \"-alt\"? [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="" + fi + echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" +done +[[ $OK = "yes" ]] && IGNORE_ALT_HOSTNAMES=true + +clear +echo "" +echo "" +echo -e "\033[21G\033[32mChange TTL Settings\033[m" +echo "" + +echo "" +if $_set_ipv4 ; then + echo "IPv4 Address(es)..................: $IPv4_ADDRESS" +else + echo -e "IPv4 Address(es)..................: \033[33mNone\033[m" +fi +if $_set_ipv6 ; then + echo "IPv6 Address(es)..................: $IPv6_ADDRESS" +else + echo -e "IPv6 Address(es)..................: \033[33mNone\033[m" +fi +echo "" +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 "" +OK= +while [ "$OK" != "yes" -a "$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" -a "$OK" != "no" ] ; then + OK="" + else + break + fi + echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" +done +[[ $OK = "yes" ]] || fatal Repeat execution with different parameters + + +cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M` + +ipv4_addresses_arr=() +for _ipv4_address in $IPv4_ADDRESS ; do + containsElement "$_ipv4_address" "${ipv4_addresses_arr[@]}" && continue + ipv4_addresses_arr+=("$_ipv4_address") +done + +ipv6_addresses_arr=() +for _ipv6_address in $IPv6_ADDRESS ; do + containsElement "$_ipv6_address" "${ipv6_addresses_arr[@]}" && continue + ipv6_addresses_arr+=("$_ipv6_address") +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 + +#for _val in "${zonefiles_arr[@]}" ; do +# echo +# echo -e "$_val" +#done +# +#exit + + +for zone_file in ${zonefiles_arr[@]} ; do + + echo -e "\n\tconverting $zone_file .." + + _replaced=false + + ## - 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 + + 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 + + ## - setze neue ttl für ipv4 address + ## - + echononl "\t Set new TTL ($TTL) for IPv4 address $_ipv4_address .." + + if grep -e "$TTL\s*IN\s*A\s*$_ipv4_address" $zone_file > /dev/null 2>&1 ; then + echo_skipped + elif grep -e "\s\{1,\}[0-9]\{2,5\}\s\{1,\}IN\s\{1,\}A\s\{1,\}$_ipv4_address" $zone_file > /dev/null 2>&1 ; then + echo_skipped + warn "Another TTL is already set for that IP-Address in file \"`basename $zone_file`\"" + else + perl -i -n -p -e "s#IN\s+A\s+$_ipv4_address#$TTL IN A $_ipv4_address#" $zone_file > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + _replaced=true + else + echo_failed + error "Setting new TTL for $_ipv4_address in zone file \"$zone_file\" failed!" + fi + 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+A\s+$_ipv4_address#\1 IN A $_ipv4_address#" $zone_file > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + else + echo_failed + error "Setting back hostnames containing \"-alt\" for $_ipv4_address in zone file \"$zone_file\" failed!" + fi + fi + fi + done + fi + + 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 + + ## - setze neue ttl für ipv6 address + ## - + echononl "\t Set new TTL ($TTL) for IPv6 address $_ipv6_address .." + + if grep -e "$TTL\s*IN\s*AAAA\s*$_ipv6_address" $zone_file > /dev/null 2>&1 ; then + echo_skipped + elif grep -e "\s\{1,\}[0-9]\{2,5\}\s\{1,\}IN\s\{1,\}AAAA\s\{1,\}$_ipv6_address" $zone_file > /dev/null 2>&1 ; then + echo_skipped + warn "Another TTL is already set for that IP-Address in file \"`basename $zone_file`\"" + else + perl -i -n -p -e "s#IN\s+AAAA\s+$_ipv6_address#$TTL IN AAAA $_ipv6_address#" $zone_file > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + _replaced=true + else + echo_failed + error "Setting new TTL for $_ipv6_address in zone file \"$zone_file\" failed!" + fi + 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 + if [ "$?" = "0" ]; then + echo_ok + else + echo_failed + error "Setting back hostnames containing \"-alt\" for $_ipv6_address in zone file \"$zone_file\" failed!" + fi + fi + fi + done + fi + + ## - setze neue serial + ## - + echo "" + echononl "\t Increase Serial for zone file \"`basename $zone_file`\"" + 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 + _replaced=false + else + echo_failed + error "Increasing Serial for zone file \"`basename $zone_file`\" failed!" + fi + else + echo_skipped + fi + +done + +echo +exit diff --git a/OLD_Versions/bind_disable_ipv6.sh b/OLD_Versions/bind_disable_ipv6.sh new file mode 100755 index 0000000..0549b2d --- /dev/null +++ b/OLD_Versions/bind_disable_ipv6.sh @@ -0,0 +1,40 @@ +#!/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` + +__zonefiles=`grep -l -E "^[^;].*\s*AAAA" ${_zone_files_dir}/*.zone` + +for zone_file in $__zonefiles ; do + + _serial_new=`date +%Y%m%d01` + + echo -e "\tconverting $zone_file .." + + _replaced=false + + ## - 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 + + perl -i -n -p -e "s/^([^;]+.*\s+AAAA\s+.*)$/${_comment}\1/" $zone_file + if [ $? -eq 0 ]; then + _replaced=true + fi + + + ## - setze neue serial + ## - + if $_replaced ; then + perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file + fi + +done + +exit diff --git a/OLD_Versions/bind_enable_ipv6.sh b/OLD_Versions/bind_enable_ipv6.sh new file mode 100755 index 0000000..3c362f0 --- /dev/null +++ b/OLD_Versions/bind_enable_ipv6.sh @@ -0,0 +1,40 @@ +#!/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` + +__zonefiles=`grep -l -E "^${_comment}.*\s*AAAA" ${_zone_files_dir}/*.zone` + +for zone_file in $__zonefiles ; do + + _serial_new=`date +%Y%m%d01` + + echo -e "\tconverting $zone_file .." + + _replaced=false + + ## - 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 + + perl -i -n -p -e "s/^${_comment}(.*\s+AAAA\s+.*)$/\1/" $zone_file + if [ $? -eq 0 ]; then + _replaced=true + fi + + + ## - setze neue serial + ## - + if $_replaced ; then + perl -i -n -p -e "s#^(\s*)\s$__serial(.*)#\1 $_serial_new\2#" $zone_file + fi + +done + +exit diff --git a/OLD_Versions/bind_get_domain_by_hostname.sh b/OLD_Versions/bind_get_domain_by_hostname.sh new file mode 100755 index 0000000..cb5ef14 --- /dev/null +++ b/OLD_Versions/bind_get_domain_by_hostname.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + +# - Bind configuration file containing zone definitions +# - +ZONE_CONF_FILE=/etc/bind/named.conf.local + +## -- +## -- End: Variable definitions + +## *** +## *** Don't make changes after this line *** +## *** + +## --- some functions +## --- +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$$ +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[32m\033[1mInfo\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_failed(){ + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" +} + +## --- +## --- END: functions + +hostname=$1 + +# - Test whether stdout (file descriptor 1) is a terminal or not (e.g. cron +# - or if you pipe the output to some other program) +# +if [[ -t 1 ]] ; then + verbose=true +else + verbose=false +fi + +$verbose && echo "" + + +# - Validate Syntax of given domain +# - +valid_domain_regex="^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$" +$verbose && echononl "\tValidate syntax of given hostname/domain.." +if [[ $hostname =~ $valid_domain_regex ]]; then + if [[ ! $hostname =~ \. ]]; then + $verbose && echo_failed + $verbose && error "Invalid hostname/domain given!" + exit 10 + else + $verbose && echo_ok + fi +else + $verbose && echo_failed + $verbose && error "Invalid hostname/domain given!" + exit 10 +fi + +_failed=false +_hostname=$(echo ${hostname//\./\\.}) +while ! grep -e "$_hostname" $ZONE_CONF_FILE > /dev/null 2>&1 ; do + hostname=${hostname#*.} + _hostname=$(echo ${hostname//\./\\.}) + if [[ ! $_hostname =~ \. ]]; then + _failed=true + break + fi +done + +if $_failed ; then + $verbose && error "hostname \"$1\" not supported by this nameserver!" +else + domain=$hostname + if $verbose ; then + info "Domain: $domain" + else + echo "$domain" + fi +fi + +$verbose && echo +exit 0 + diff --git a/OLD_Versions/bind_get_zonefile_by_hostname.sh b/OLD_Versions/bind_get_zonefile_by_hostname.sh new file mode 100755 index 0000000..e246471 --- /dev/null +++ b/OLD_Versions/bind_get_zonefile_by_hostname.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash + +# - Bind configuration file containing zone definitions +# - +ZONE_CONF_FILE=/etc/bind/named.conf.local + +## -- +## -- End: Variable definitions + +## *** +## *** Don't make changes after this line *** +## *** + +## --- some functions +## --- +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 "" + exit 1 +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[32m\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_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 +} + +## --- +## --- END: functions + +hostname=$1 + + +echo "" + + +# - Validate Syntax of given domain +# - +valid_domain_regex="^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$" +echononl "\tValidate syntax of given domain.." +if [[ $hostname =~ $valid_domain_regex ]]; then + if [[ ! $hostname =~ \. ]]; then + echo_failed + error "Invalid hostname/domain given!" + exit 10 + else + echo_ok + fi +else + echo_failed + error "Invalid hostname/domain given!" + exit 10 +fi + +_failed=false +while ! grep $hostname $ZONE_CONF_FILE > /dev/null 2>&1 ; do + hostname=${hostname#*.} + if [[ ! $hostname =~ \. ]]; then + _failed=true + break + fi +done + +if $_failed ; then + echo "hostname \"$1\" not supported by this nameserver!" +else + domain=$hostname + echo "domain: $domain" +fi + +echo +exit 0 + diff --git a/OLD_Versions/bind_reload_all_zones.sh b/OLD_Versions/bind_reload_all_zones.sh new file mode 100755 index 0000000..c8217f9 --- /dev/null +++ b/OLD_Versions/bind_reload_all_zones.sh @@ -0,0 +1,155 @@ +#!/usr/bin/env bash + +## --- Variables (default Values) +## --- + +## - file, where zone definitions live.. +## - +_CONF_FILE=/etc/bind/named.conf.local + +## --- +## --- End: Variables (default Values) + + +## --- some functions +## --- +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 "" + exit 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_failed(){ + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" +} + +## --- +## --- END: functions + +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo " Enter filename containing the zone definitions" +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 +fi + +if [ ! -f "$CONF_FILE" ]; then + fatal File not found: $CONF_FILE +fi + +clear +echo "" +echo "" +echo -e "\033[21G\033[32mReload all (master) Zones\033[m" +echo "" + +echo "" +echo "File containing zone definitions..: $CONF_FILE" + +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 +echo "" + +## - Read Zonefile line by line +## - +## - || [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n +## - +declare -i number=0 +regex_master="type[[:space:]]+master" +while IFS='' read -r line || [[ -n "$line" ]] ; do + if [[ $line =~ ^\s*zone ]]; then + zone=`echo $line | awk '{print$2}'` + shopt -s extglob + if [[ $zone =~ ^\" ]]; then + zone=${zone##*(\")} + zone=${zone%%*(\")} + fi + shopt -u extglob + fi + if [[ $line =~ $regex_master ]]; then + echononl "\tReload Zone \"$zone\"" + /usr/sbin/rndc reload $zone > /dev/null 2>&1 + if [ "$?" = "0" ]; then + echo_ok + let number="$number+1" + sleep 1 + else + echo_failed + fi + fi +done < $CONF_FILE + +[[ $number -eq 0 ]] && info "No Zonedefinition (master) found in file $CONF_FILE ." + +info $number Zones reloaded. + +echo +exit diff --git a/OLD_Versions/bind_set_new_serial.sh b/OLD_Versions/bind_set_new_serial.sh new file mode 100755 index 0000000..ffe9838 --- /dev/null +++ b/OLD_Versions/bind_set_new_serial.sh @@ -0,0 +1,281 @@ +#!/usr/bin/env bash + +# - Sets new serial and reloads zone +# - +# - Return (Exit) Codes: +# - success: +# - 0: Serial is replaced and Zone is reloaded +# - error: +# - 10: Invalid Hostname/Domain given +# - 15: Hostname/Domain not supported +# - 11: No zonefile found +# - 12: Determin new Serial failed +# - 13: Increasing Serial failed +# - 14: Reloading Zone failed +# - 99: Fatal error +# - +# - usage: ./nd_set_new_serial.sh +# - +# - example: ./nd_set_new_serial.sh a.mx.open.de + +# - + +## -- Variable definitions +## -- + +# - Bind configuration file containing zone definitions +# - +ZONE_CONF_FILE=/etc/bind/named.conf.local + +BIND_USER=bind +BIND_GROUP=bind + +## -- +## -- End: Variable definitions + +## *** +## *** Don't make changes after this line *** +## *** + +## --- some functions +## --- +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 "" + exit 1 +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[32m\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_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 +} + +## --- +## --- END: functions + +# - Parameter "check" can be used, to test whether this script +# - is accessable (e.g. from a script on a remote host) +# - +if [[ "$1" = "check" ]]; then + echo "\$1: $1" + exit 0 +fi + +host_name=$1 + + +echo "" + + +# - Validate Syntax of given domain +# - +valid_domain_regex="^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$" +echononl "\tValidate syntax of given domain.." +if [[ $host_name =~ $valid_domain_regex ]]; then + if [[ ! $host_name =~ \. ]]; then + echo_failed + error "Invalid hostname/domain \"$1\" given!" + exit 10 + else + echo_ok + fi +else + echo_failed + error "Invalid hostname/domain ($1) given!" + exit 10 +fi + + +# - Determin zone (domain) +# - +_failed=false +_host_name=$host_name +_tmp_host_name=$(echo ${_host_name//\./\\.}) +while ! grep -e "$_tmp_host_name" $ZONE_CONF_FILE > /dev/null 2>&1 ; do + _host_name=${_host_name#*.} + _tmp_host_name=$(echo ${_host_name//\./\\.}) + if [[ ! $_tmp_host_name =~ \. ]]; then + _failed=true + break + fi +done + +if $_failed ; then + error "Given hostname/domain \"$1\" not supported by this nameserver!" +else + domain=$_host_name +fi + + +# - Determine zonefile (by reading bind configuration) +# - +_found=false +declare -i _number=0 +regex_zone="^[[:space:]]*zone[[:space:]]+\"$_tmp_host_name\"" +regex_file="^[[:space:]]*file" +while IFS='' read -r line || [[ -n "$line" ]] ; do + if [[ $line =~ $regex_zone ]]; 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 + let number++ + break + fi + fi +done < $ZONE_CONF_FILE + +zone_file_dir=`dirname $zone_file` + +if [[ $number -eq 0 ]] ; then + error "No Zonefile (master) found for domain \"$domain\" ." + exit 11 +fi + + +echononl "\tBackup existing directory containg zonefiles.." +if [[ -d "$zone_file_dir" ]] ; then + cp -a $zone_file_dir ${zone_file_dir}.BAK.`date +%Y-%m-%d-%H%M` + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + echo "" + exit 99 + fi +else + echo_failed + error "Zonefile directory not found for domain \"$domain\" ." + exit 99 +fi + + +# - Determin new serial +# - +echononl "\tDetermin new serial.." +_failed=false +declare -i serial_new=`date +%Y%m%d01` +serial_cur=`grep -e "^\s*[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'` +if [[ $? -gt 0 ]] ; then + _failed=true +fi +while [ ! $serial_new -gt $serial_cur ]; do + let serial_new++ +done +if [[ $? -gt 0 ]] ; then + _failed=true +fi +if $_failed ; then + echo_failed + error "Determin Serial failed!" + exit 12 +else + echo_ok +fi + + +# - Replace serial with the new one +# - +echononl "\tIncrease serial for zone file \"`basename $zone_file`\".." +perl -i -n -p -e "s#^(\s*)\s$serial_cur(.*)#\1 $serial_new\2#" $zone_file > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "Increasing Serial failed!" + exit 13 +fi + +echo "" +echononl "\tCorrect Owner for $zone_file .." +chown $BIND_USER:$BIND_GROUP $zone_file +if [[ $? -eq 0 ]] ; then + echo_ok +else + echo_failed + exit 99 +fi +echononl "\tCorrect permissions on $zone_file .." +chmod 644 $zone_file +if [[ $? -eq 0 ]] ; then + echo_ok +else + echo_failed + exit 99 +fi + + +# - Reload Zone +# - +echononl "\tReloading zone \"$domain\".." +rndc reload $domain > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok + info "Serial increased and zone reloaded ($domain)" + exit 0 +else + echo_failed + error "Increasing Serial failed!" + exit 13 +fi + +echo +exit 99 diff --git a/OLD_Versions/bind_set_renew_tlsa.sh b/OLD_Versions/bind_set_renew_tlsa.sh new file mode 100755 index 0000000..ad22371 --- /dev/null +++ b/OLD_Versions/bind_set_renew_tlsa.sh @@ -0,0 +1,351 @@ +#!/usr/bin/env bash + +# - Replaces a TLSA Record +# - +# - Return (Exit) Codes: +# - success: +# - 0: TLSA record is up to date +# - 1: TLSA record replaced +# - 2: New TLSA record written +# - error: +# - 10: Invalid TLSA record given +# - 11: No zonefile for TLSA record found +# - 15: Hostname/Domain not supported +# - 20: Replacing record failed +# 21: Adding Record failed +# - 99: Fatal error +# - +# - example: +# - ./replace_dns_tlsa.sh _25._tcp.mail.initiativenserver.de. IN TLSA 3 1 1 aab3a46b387dd543ed8d... + + +## -- Variable definitions +## -- + +# - Bind configuration file containing zone definitions +# - +ZONE_CONF_FILE=/etc/bind/named.conf.local + +BIND_USER=bind +BIND_GROUP=bind + +## -- +## -- End: Variable definitions + + +## *** +## *** Don't make changes after this line *** +## *** + + +## --- some functions +## --- +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$$ +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \033[33m\033[1mInfo\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_failed(){ + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" +} +echo_skipped() { + echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" +} + +backup_dir () { + dir_to_backup=$1 + echononl "\tBackup existing directory \"$dir_to_backup\" .." + if [[ -d "$dir_to_backup" ]] ; then + cp -a $dir_to_backup ${dir_to_backup}.BAK.`date +%Y-%m-%d-%H%M` + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + echo "" + exit 99 + fi + else + echo_failed + error "Directory \"$dir_to_backup\" not found. No Backup written!" + exit 99 + fi + +} + +## --- +## --- END: functions + +# - Parameter "check" can be used, to test whether this script +# - is accessable (e.g. from a script on a remote host) +# - +if [[ "$1" = "check" ]]; then + echo "\$1: $1" + exit 0 +fi + + +# - Split given Record into an array +# - +declare -a record_arr=($@); +if [[ ${#record_arr[@]} -eq 7 ]]; then + + record_name=${record_arr[0]} + record_ttl="" + record_type="${record_arr[1]} ${record_arr[2]} ${record_arr[3]} ${record_arr[4]} ${record_arr[5]}" + record_hash=${record_arr[6]} + +elif [[ ${#record_arr[@]} -eq 8 ]]; then + + record_name=${record_arr[0]} + record_ttl=${record_arr[1]} + record_type="${record_arr[2]} ${record_arr[3]} ${record_arr[4]} ${record_arr[5]} ${record_arr[6]}" + record_hash=${record_arr[7]} + +else + + error "Invalid TLSA record given!" + exit 10 + +fi + + +# - Split record_name, to get port,protocol,hostnaem,domain +# - +CUR_IFS=$IFS +IFS='\.' +declare -a split_record_name_arr=($record_name) +IFS=$CUR_IFS + +_port=${split_record_name_arr[0]} +port=${_port##*_} +_protocol=${split_record_name_arr[1]} +protocol=${_protocol##*_} + +hostname="${split_record_name_arr[2]}" +declare -i _index=3 +while [[ $_index -lt ${#split_record_name_arr[@]} ]] ; do + hostname="${hostname}.${split_record_name_arr[$_index]}" + let _index++ +done + + +# - Determin zone (domain) +# - +_failed=false +_hostname=$hostname +_tmp_hostname=$(echo ${_hostname//\./\\.}) +while ! grep -e "$_tmp_hostname" $ZONE_CONF_FILE > /dev/null 2>&1 ; do + _hostname=${_hostname#*.} + _tmp_hostname=$(echo ${_hostname//\./\\.}) + if [[ ! $_tmp_hostname =~ \. ]]; then + _failed=true + break + fi +done + +if $_failed ; then + error "Given hostname/domain \"$hostname\" not supported by this nameserver!" + exit 15 +else + domain=$_hostname +fi + + +# - Determine zonefile (by reading bind configuration) +# - +_found=false +declare -i _number=0 +regex_zone="^[[:space:]]*zone[[:space:]]+\"$_tmp_hostname\"" +regex_file="^[[:space:]]*file" +while IFS='' read -r line || [[ -n "$line" ]] ; do + if [[ $line =~ $regex_zone ]]; 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 + let number++ + break + fi + fi +done < $ZONE_CONF_FILE + +if [[ $number -eq 0 ]] ; then + error "No Zonefile (master) found for domain \"$domain\" ." + exit 11 +fi + +zone_file_dir=`dirname $zone_file` + +# - Backup existing zone file directory +# - + + +# - Update/Add TLSA recotd if needed +# - +if grep -E "^$record_name.+$record_type" $zone_file > /dev/null 2>&1 ; then + if [[ -n "$record_ttl" ]]; then + search_string="^$record_name\\s+$record_ttl\\s+$record_type" + else + search_string="^$record_name\\s+$record_type" + fi + if grep -E "$search_string" $zone_file | grep $record_hash > /dev/null 2>&1 ; then + info "TLSA record is already up to date.." + echo "" + exit 0 + else + _replac_string=${record_arr[@]} + # - Backup Zone directory + backup_dir $zone_file_dir + + # - Replace TLSA Record + echononl "\tGoing to replace TLSA Record.." + perl -i -n -p -e "s#^${record_name}.+${record_type}.*#$_replac_string#" $zone_file + if [[ $? -eq 0 ]] ; then + echo_ok + echo "" + exit 1 + else + echo_failed + echo "" + exit 20 + fi + fi +else + warn "No Record for replacing fount in zonefile \"`basename $zone_file`\"!" + + declare -i _count + + search_string="^_${port}\._(tcp|udp)\.$hostname" + _count=`grep -Eo "$search_string" $zone_file | wc -l` + + _tlsa_record_found=true + if [[ $_count -eq 0 ]]; then + + search_string="^_[0-9]{1,4}\._(tcp|udp)\.$hostname" + _count=`grep -Eo "$search_string" $zone_file | wc -l` + + if [[ $_count -eq 0 ]]; then + + search_string="^_[0-9]{1,4}\._(tcp|udp).*TLSA" + _count=`grep -Eo "$search_string" $zone_file | wc -l` + + if [[ $_count -eq 0 ]]; then + + _tlsa_record_found=false + + search_string="^[^;].+\s+IN\s+MX" + _count=`grep -Eo "$search_string" $zone_file | wc -l` + + if [[ $_count -eq 0 ]]; then + search_string="^[^;].+\s+IN\s+NS" + _count=`grep -Eo "$search_string" $zone_file | wc -l` + if [[ $_count -eq 0 ]]; then + error "No place for adding a new TLSA record found. Check manually!" + exit 99 + fi + fi + fi + fi + fi + + CUR_IFS=$IFS + IFS='' + _tmpfile=`mktemp` + > $_tmpfile + + # - backup zone directory + backup_dir $zone_file_dir + + # - Add new TLSA record + echononl "\tAdd new TLSA record to zonefile \"\".." + while read -r line || [[ -n "$line" ]]; do + + echo $line >> $_tmpfile + + if echo "$line" | grep -E "$search_string" > /dev/null 2>&1 ; then + let _count-- + fi + if [[ $_count -eq 0 ]]; then + echo "" >> $_tmpfile + if ! $_tlsa_record_found ; then + echo ";" >> $_tmpfile + echo "; DANE" >> $_tmpfile + echo ";" >> $_tmpfile + fi + echo "${record_arr[@]}" >> $_tmpfile + echo "" >> $_tmpfile + _count=-1 + fi + done < "$zone_file" + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + rm $_tmpfile + exit 21 + fi + + IFS=$CUR_IFS + mv $_tmpfile $zone_file + + # - Set Coorect Owner/Permission + echo "" + echononl "\tCorrect Owner for $zone_file .." + chown $BIND_USER:$BIND_GROUP $zone_file + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + exit 99 + fi + echononl "\tCorrect permissions on $zone_file .." + chmod 644 $zone_file + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + exit 99 + fi + + echo "" + exit 2 +fi + +echo +exit 99 diff --git a/OLD_Versions/bind_set_ttl_to_default.sh b/OLD_Versions/bind_set_ttl_to_default.sh new file mode 100755 index 0000000..f9605c4 --- /dev/null +++ b/OLD_Versions/bind_set_ttl_to_default.sh @@ -0,0 +1,397 @@ +#!/usr/bin/env bash + + +## --- Variables (default Values) +## --- +#_zone_file_dir=/etc/bind/master +_zone_file_dir=/root/tmp/master + +_serial_new=`date +%Y%m%d01` + +_zone_file_suffix=zone +## --- +## --- End: Variables (default Values) + + +## --- some functions +## --- +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 "" + exit 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_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 +} + + +## - Test of valid IPv4 Address +## - +## - Returns 0 if valid, > 0 otherwise +## - +is_valid_ipv4() { + local -a octets=( ${1//\./ } ) + local RETURNVALUE=0 + + # return an error if the IP doesn't have exactly 4 octets + [[ ${#octets[@]} -ne 4 ]] && return 1 + + for octet in ${octets[@]} + do + if [[ ${octet} =~ ^[0-9]{1,3}$ ]] + then # shift number by 8 bits, anything larger than 255 will be > 0 + ((RETURNVALUE += octet>>8 )) + else # octet wasn't numeric, return error + return 1 + fi + done + return ${RETURNVALUE} +} + +is_valid_ipv6() { + local _ipv6=$1 + + if [ "$1" != "${1#[0-9a-f]*:}" ] \ + && [ "$1" = "${1#*[^0-9a-f:]}" ] \ + && [ "${1#*[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]}" = "${1#*:*:*:*:*:*:*:*:*:}" ]; then + return 0 + else + return 1 + fi +} + +## --- +## --- END: functions + +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert IPv4-Address(es) for which TTL should be set to zone-file default." +echo "" +echo "For multiple IPv4 Addresses, insert a blank separated list" +echo "" +echo -e "Type \"\033[33mNone\033[m\" if no IPv4 address should be changed." +echo "" +IPv4_ADDRESS= +_set_ipv4=true +while [ "X$IPv4_ADDRESS" = "X" ]; do + echononl "IPv4-Address: " + read IPv4_ADDRESS + ## - To lower case + IPv4_ADDRESS=${IPv4_ADDRESS,,} + if [ "X$IPv4_ADDRESS" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv4_ADDRESS="" + continue + fi + if [ "$IPv4_ADDRESS" = "none" ];then + _set_ipv4=false + break + fi + if ! is_valid_ipv4 $IPv4_ADDRESS ; then + echo -e "\n\t\033[33m\033[1m$IPv4_ADDRESS\033[m is NOT a valid IPv4 Address\n" + IPv4_ADDRESS="" + continue + fi +done + + +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Insert IPv6-Address(es) for which TTL should be set to zone-file default." +echo "" +echo "For multiple IPv6 Addresses, insert a blank separated list" +echo "" +echo -e "Type \"\033[33mNone\033[m\" if no IPv6 address should be changed." +echo "" +IPv6_ADDRESS= +_set_ipv6=true +while [ "X$IPv6_ADDRESS" = "X" ]; do + echononl "IPv6-Address: " + read IPv6_ADDRESS + ## - To lower case + IPv6_ADDRESS=${IPv6_ADDRESS,,} + if [ "X$IPv6_ADDRESS" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" + IPv6_ADDRESS="" + continue + fi + if [ "$IPv6_ADDRESS" = "none" ];then + _set_ipv6=false + break + fi + if ! is_valid_ipv6 $IPv6_ADDRESS ; then + echo -e "\n\t\033[33m\033[1m$IPv6_ADDRESS\033[m is NOT a valid IPv6 Address\n" + IPv6_ADDRESS="" + continue + fi +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 for changing their TTL to the zone-file default.." +fi + +clear +echo "" +echo "" +echo -e "\033[21G\033[32mChange TTL settings to the zonefile default value\033[m" +echo "" + +echo "" +if $_set_ipv4 ; then + echo "IPv4 Address(es)..................: $IPv4_ADDRESS" +else + echo -e "IPv4 Address(es)..................: \033[33mNone\033[m" +fi +if $_set_ipv6 ; then + echo "IPv6 Address(es)..................: $IPv6_ADDRESS" +else + echo -e "IPv6 Address(es)..................: \033[33mNone\033[m" +fi +echo "" +echo "New TTL...........................: Zonefile default" +echo "" +echo "Zone File Directory...............: $ZONE_FILE_DIR" +echo "Zone File Suffix..................: $ZONE_FILE_SUFFIX" + +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 + + +cp -a $ZONE_FILE_DIR ${ZONE_FILE_DIR}.BAK-`date +%Y-%m-%d-%H%M` + +ipv4_addresses_arr=() +for _ipv4_address in $IPv4_ADDRESS ; do + containsElement "$_ipv4_address" "${ipv4_addresses_arr[@]}" && continue + ipv4_addresses_arr+=("$_ipv4_address") +done + +ipv6_addresses_arr=() +for _ipv6_address in $IPv6_ADDRESS ; do + containsElement "$_ipv6_address" "${ipv6_addresses_arr[@]}" && continue + ipv6_addresses_arr+=("$_ipv6_address") +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 + + +for zone_file in ${zonefiles_arr[@]} ; do + + echo -e "\n\tconverting $zone_file .." + + _replaced=false + + ## - 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 + + 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 + + ## - 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 + if [ "$?" = "0" ]; then + echo_ok + _replaced=true + else + echo_failed + error "Setting TTL to the zonfile default for $_ipv4_address in zone file \"$zone_file\" failed!" + fi + else + echo_skipped + fi + + fi + done + fi + + 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 + + ## - 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 + if [ "$?" = "0" ]; then + echo_ok + _replaced=true + else + echo_failed + error "Setting TTL to the zonfile default for $_ipv6_address in zone file \"$zone_file\" failed!" + fi + else + echo_skipped + fi + fi + done + fi + + ## - setze neue serial + ## - + echo "" + echononl "\t Increase Serial for zone file \"`basename $zone_file`\"" + 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 + _replaced=true + else + echo_failed + error "Increasing Serial for zone file \"`basename $zone_file`\" failed!" + fi + else + echo_skipped + fi + +done + +echo +exit diff --git a/README.fix_journal_out_of_sync b/README.fix_journal_out_of_sync new file mode 100644 index 0000000..bc8c13b --- /dev/null +++ b/README.fix_journal_out_of_sync @@ -0,0 +1,32 @@ +# ----- +# - Fixing BIND's journal out of sync with zone error. +# ----- + +# - If a zone (or zones) in your BIND setup gets out of sync (almost undoubtedly caused +# - by manual editing a zone): +# - +# - zone utternerd.org/IN: journal rollforward failed: journal out of sync with zone +# - zone utternerd.org/IN: not loaded due to errors. +# - +# - +# - Fixing it is rather simple, simply delete the offending zone journal which is easily +# - identified by the "zonename.jnl": + + # rm /var/cache/bind/zones/utternerd.org.jnl + + +# - Now simply restart BIND as you usually would (YMMV): + + # service bind9 restart + + +# - In the future if you need to update a dynamic zone, freeze, modify, +# - and thaw them manually: +# - +# - Notice: Thawing the zone will force BIND to reload it. + + # rndc freeze utternerd.org + + # vi /var/cache/bind/zones/utternerd.org (make manual changes needed) + # rndc thaw utternerd.org + diff --git a/README.manual_update_dynamic_zone b/README.manual_update_dynamic_zone new file mode 100644 index 0000000..db8684b --- /dev/null +++ b/README.manual_update_dynamic_zone @@ -0,0 +1,14 @@ +# ----- +# - Manually update a dynamic zone +# ----- + +# - If you need to update a dynamic zone, freeze, modify, +# - and thaw them manually: +# - +# - Notice: Thawing the zone will force BIND to reload it. + + # rndc freeze utternerd.org + + # vi /var/cache/bind/zones/utternerd.org (make manual changes needed) + # rndc thaw utternerd.org +