#!/usr/bin/env bash SCRIPT_PATH=$(realpath -s "${0}") SCRIPT_DIR=$(dirname "$SCRIPT_PATH") SCRIPT=$(basename "$SCRIPT_PATH") usage() { cat <&2 exit 1 fi ;; -h|--help) usage ;; -f|--force) force=true ;; -i|--interactive) interactive=true ;; -*) echo "$SCRIPT: invalid option -- '$opt'" >&2 exit 1 ;; *) args+=("$opt") if (( ${#args[@]} > 1 )); then echo "$SCRIPT: invalid argument -- '$opt'" >&2 exit 1 fi ;; esac done source "$config_file" || exit 2 LIB_DIR=${LIB_DIR:-$SCRIPT_DIR/lib} source "$LIB_DIR"/dns.sh || exit 3 source "$LIB_DIR"/output.sh || exit 3 set -- "${args[@]}" zone=$1 if shift; then dns_check_zone_view "$zone" zone view || exit 10 elif $interactive; then dns_select_zone zone view || exit 11 else echo "$SCRIPT: missing argument -- ZONE[@VIEW]" >&2 exit 1 fi declare -A output if [ "${view}" == "*" ]; then json_array_to_bash views < <(dns_zone_views) else views=("$view") fi for view in "${views[@]}"; do dns_get_base_config "$view" zone_dir conf_dir conf_file || exit 10 zone_conf_file="$conf_dir/$zone.conf" ! [ -f "$zone_conf_file" ] && echo "ERROR: zone exists in DNS but no config file found, zone not managed by DNS-Manager -- '$zone_conf_file'" >&2 && exit 14 zone_file="$zone_dir/$zone.zone" ! [ -f "$zone_file" ] && echo "ERROR: zone exists in DNS but no zone file found, zone not managed by DNS-Manager -- '$zone_file'" >&2 && exit 15 done if ! $force; then for view in "${views[@]}"; do echo "View: $view" echo -e "\e[31m- $TAB$zone\e[0m" done echo ! yes_no "Proceed?" && echo -e "Aborted" && exit echo fi echo -n "Deleting zone from config... " for view in "${views[@]}"; do dns_get_base_config "$view" zone_dir conf_dir conf_file || exit 10 zone_conf_file="$conf_dir/$zone.conf" ! rm "$zone_conf_file" && echo "ERROR: unable to delete config file -- '$zone_conf_file'" >&2 && exit 14 tmp=$(mktemp) cat >"$tmp" <>"$tmp"; then echo "ERROR: unable to write to temp file -- '$tmp'" >&2 rm "$tmp" exit 23 fi done < <(find "$conf_dir" -maxdepth 1 -type f -name '*.conf') if ! cat "$tmp" > "$conf_file"; then echo "ERROR: unable to write config file -- '$conf_file'" >&2 rm "$tmp" exit 24 fi rm "$tmp" done echo "Ok" echo -n "Reload Bind config... " rndc_args="" [ -n "$CONTROL_KEY" ] && rndc_args="-k $CONTROL_KEY" ! "$RNDC" $rndc_args reconfig && echo "ERROR: rndc reconfig failed" >&2 && exit 25 echo "Ok" error=false echo -n "Deleting zone files... " for view in "${views[@]}"; do dns_get_base_config "$view" zone_dir || exit 10 zone_file="$zone_dir/$zone.zone" ! rm "$zone_file" && echo "ERROR: unable to delete zone file -- '$zone_file'" >&2 && error=true while IFS=$NEWLINE read -r file; do ! rm "$file" && echo "ERROR: unable to delete zone related file -- '$zone_file'" >&2 && error=true done < <(find "$zone_dir" -maxdepth 1 -type f -name "$zone.zone.*") done ! $error && echo "Ok"