#!/usr/bin/env bash 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` #--------------------------------------- #----------------------------- # Base Function(s) #----------------------------- #--------------------------------------- clean_up() { # Perform program exit housekeeping rm $log_file blank_line exit $1 } echononl(){ echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "$*\\c" 1>&2 else echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ } fatal(){ echo "" echo -e "[ \033[31m\033[1mError\033[m ]: $*" echo "" echo -e "\t\033[31m\033[1mScript is canceled\033[m\033[m" echo "" clean_up 1 } warn (){ echo "" echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" echo "" } info (){ echo "" echo -e "\t[ \033[33m\033[1mInfo\033[m ]: $*" echo "" } ok (){ echo "" echo -e "\t[ \033[36m\033[1mOk\033[m ]: $*" echo "" } error(){ echo "" echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" echo "" } echo_ok() { echo -e "\033[75G[ \033[32mok\033[m ]" } echo_done() { echo -e "\033[75G[ \033[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 } blank_line() { if $terminal ; then echo "" fi } ## - 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 } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ============= # --- Some checks # ============= # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi #--------------------------------------- #----------------------------- # Setting Defaults #----------------------------- #--------------------------------------- DEFAULT_CONF_FILE_DIR="/etc/bind" DEFAULT_BIND_USER="bind" DEFAULT_BIND_GROUP="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 "$BIND_USER" ]] || BIND_USER="$DEFAULT_BIND_USER" [[ -n "$BIND_GROUP" ]] || BIND_GROUP="$DEFAULT_BIND_GROUP" #[[ -n "$BIND_CACHE_DIR" ]] && DEFAULT_BIND_CACHE_DIR="$BIND_CACHE_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_BIND_CACHE_DIR}" #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" 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" [[ -n "$ZONE_FILE_MASTER_DIR" ]] || DEFAULT_ZONE_FILE_MASTER_DIR="${CONF_FILE_DIR}/master" 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 clear echo "" echo "" echo -e "\033[21G\033[32mAdd SPF recotd if not exists\033[m" echo "" echo "" echo " Bind Configuration Directory...............: $CONF_FILE_DIR" echo " Zones Declaration File.....................: $ZONES_DECLARATION_FILE" echo " Zone File Directory (master)...............: $ZONE_FILE_MASTER_DIR" echo "" echo " Bind user..................................: ${BIND_USER}" echo " Bind group.................................: ${BIND_GROUP}" 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 "" 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 "" echo "" echo -e "\033[37m\033[1mDetermine zone files for adding a DMARC Record ..\033[m" _found=false _is_master=false zonefiles_arr=() zone_file="" regex_master="type[[:space:]]+master" regex_file="^[[:space:]]*file" regex_mx="^[^;].+\s+IN\s+MX" while IFS='' read -r _line || [[ -n $_line ]] ; do if [[ $_line =~ ^[[:space:]]*zone[[:space:]]+ ]]; then _found=true zone="$(echo $_line | awk '{print$2}')" shopt -s extglob if [[ $zone =~ \; ]]; then zone=${zone%%*(\;)} fi if [[ $zone =~ ^\" ]]; then zone=${zone##*(\")} zone=${zone%%*(\")} fi shopt -u extglob fi if $_found ; then if [[ $_line =~ $regex_file ]]; then zone_file=$(echo $_line | awk '{print$2}') shopt -s extglob if [[ $zone_file =~ \; ]]; then zone_file=${zone_file%%*(\;)} fi if [[ $zone_file =~ ^\" ]]; then zone_file=${zone_file##*(\")} zone_file=${zone_file%%*(\")} fi shopt -u extglob fi if [[ $_line =~ $regex_master ]]; then _is_master=true fi if [[ "$_line" =~ ^[[:space:]]*\}[[:space:]]*\; ]]; then if $_is_master && [[ -n "$zone_file" ]]; then if $(grep -q -E "${regex_mx}" "$zone_file" 2> /dev/null) ; then if $(grep -q -E "^\s*_dmarc\s*IN\s+TXT\s+\"v=DMARC1.*" "$zone_file") ; then : else if [ ${#zonefiles_arr[@]} -eq 0 ] ; then zonefiles_arr+=("${zone_file}:$zone") else if ! containsElement "${zone_file}:$zone" "${zonefiles_arr[@]}" ; then zonefiles_arr+=("${zone_file}:$zone") fi fi fi fi fi _is_master=false _found=false zone_file="" fi fi done < "$ZONES_DECLARATION_FILE" echo "" for _val in ${zonefiles_arr[@]} ; do echo "" IFS=':' read -a _val_arr <<< "${_val}" zone_file="${_val_arr[0]}" zone="${_val_arr[1]}" CUR_IFS=$IFS IFS='' #_tmpfile=`mktemp` _tmpfile="/tmp/$(basename "${zone_file}")" > $_tmpfile echo -e "\tEditing \033[1m$zone_file\033[m .." _replaced=false ## - Add SPF record ## - echononl "\t Add (default) DMARC TXT record for zone '${zone}'.." # - We will place the new SPF Record after the last existing MX record. # - # - first we will count the number ox existing MX records # - declare -i _count search_string="^[^;].+\s+IN\s+TXT\s+\"" _count=$(grep -Eo "$search_string" $zone_file | wc -l) if [[ $_count -eq 0 ]]; then echo_skipped warn "No existing MX record found. Check and add subdomain SPF record manually!" continue fi _failed=false 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 cat << EOF >> $_tmpfile ; ; DMARC ; _dmarc IN TXT "v=DMARC1; p=none;" EOF _count=-1 fi [[ $? -ne 0 ]] && _failed=true done < "$zone_file" if $_failed ; then echo_failed rm $_tmpfile else echo_ok _replaced=true fi IFS=$CUR_IFS mv $_tmpfile $zone_file # - Set Correct Owner/Permission # - blank_line echononl "\t Correct Owner for $(basename "$zone_file") .." chown $BIND_USER:$BIND_GROUP $zone_file if [[ $? -eq 0 ]] ; then echo_ok else echo_failed error "Setting ownership for '$zone_file' failed!" fi echononl "\t Correct permissions on $(basename "$zone_file") .." chmod 644 $zone_file if [[ $? -eq 0 ]] ; then echo_ok else echo_failed error "Correct permissions on '$zone_file' failed!" fi # - Calculate new serial # - echo "" echononl "\t Calculate new serial" if $_replaced ; then declare -i __serial=`grep -e "[0-9]\{10\}" $zone_file | grep serial | awk '{print$1}'` while [ ! $_serial_new -gt $__serial ]; do let _serial_new++ done echo_done else echo_skipped fi # - Set new serial # - echononl "\t Increase Serial 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 else echo_failed error "Increasing Serial for zone file \"`basename $zone_file`\" failed!" fi else echo_skipped fi # - Reload Zone # - echononl "\t Reload zone '${zone}'" if $_replaced ; then /usr/sbin/rndc reload $zone > /dev/null 2>&1 if [[ $? -gt 0 ]]; then echo_failed else echo_ok fi else echo_skipped fi echo 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 "" clean_up 0