204 lines
5.2 KiB
Bash
Executable File
204 lines
5.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
SCRIPT_PATH=$(realpath -s "$0")
|
|
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
|
SCRIPT=$(basename "$SCRIPT_PATH")
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: $SCRIPT [OPTIONS]... ZONE[@VIEW] NAME TYPE [VALUE]
|
|
|
|
Delete DNS records.
|
|
|
|
Options:
|
|
-c, --config path to config file
|
|
-h, --help print this help message
|
|
-f, --force delete records without confirmation prompt
|
|
-i, --interactive interactively ask for missing arguments
|
|
|
|
EOF
|
|
exit
|
|
}
|
|
|
|
config_file="/etc/dns-manager/config.sh"
|
|
force=false
|
|
interactive=false
|
|
|
|
declare -a args=()
|
|
while [ -n "$1" ]; do
|
|
opt=$1
|
|
shift
|
|
case "$opt" in
|
|
-c|--config)
|
|
if [ -z "$1" ]; then
|
|
echo "$SCRIPT: missing argument to option -- '$opt'" >&2
|
|
exit 1
|
|
fi
|
|
config_file=$1
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
;;
|
|
-i|--interactive)
|
|
interactive=true
|
|
;;
|
|
-f|--force)
|
|
force=true
|
|
;;
|
|
-*)
|
|
echo "$SCRIPT: invalid option -- '$opt'" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
args+=("$opt")
|
|
if (( ${#args[@]} > 5 )); 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
|
|
|
|
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 zone_data
|
|
|
|
if [ "${view}" == "*" ]; then
|
|
json_array_to_bash views < <(dns_zone_views "$zone")
|
|
else
|
|
views=("$view")
|
|
fi
|
|
|
|
records=$(
|
|
for view in "${views[@]}"; do
|
|
dns_zone "$zone" "$view" || exit 22
|
|
done | "$JQ" --compact-output --slurp 'add'
|
|
)
|
|
|
|
name=$1
|
|
if shift; then
|
|
dns_check_record_name "$name" name || exit 21
|
|
elif $interactive; then
|
|
if (( ${#zone_data[@]} > 1 )); then
|
|
dns_read_record_name name || exit 10
|
|
else
|
|
json_array_to_bash names < <("$JQ" --compact-output '[ .[] | .name ] | sort | unique' <<<"$records")
|
|
COLUMNS=30
|
|
echo -e "Select record name:\n"
|
|
select name in "${names[@]}"; do
|
|
[ -n "$name" ] && break
|
|
done
|
|
[ -z "$name" ] && echo "ERROR: record name selection failed" >&2 && exit 11
|
|
echo
|
|
fi
|
|
else
|
|
echo "$SCRIPT: missing argument -- NAME" >&2
|
|
exit 1
|
|
fi
|
|
|
|
records=$("$JQ" --compact-output --arg name "$name" '[ .[] | select(.name == $name) ]' <<<"$records")
|
|
[ "$records" == "[]" ] && echo "ERROR: no such record -- '$name'" >&2 && exit 5
|
|
|
|
json_array_to_bash rtypes < <("$JQ" --compact-output '[ .[] | .type ] | sort | unique' <<<"$records")
|
|
|
|
rtype=${1^^}
|
|
if shift; then
|
|
dns_check_record_type "$rtype" || exit 23
|
|
elif $interactive; then
|
|
if [ "$view" == "*" ]; then
|
|
dns_select_record_type rtype || exit 10
|
|
else
|
|
echo -e "Select record type:\n"
|
|
select rtype in "${rtypes[@]}"; do
|
|
[ -n "$rtype" ] && break
|
|
done
|
|
[ -z "$name" ] && echo "ERROR: record type selection failed" >&2 && exit 11
|
|
echo
|
|
fi
|
|
else
|
|
echo "$SCRIPT: missing argument -- TYPE" >&2
|
|
exit 1
|
|
fi
|
|
|
|
records=$("$JQ" --compact-output --arg rtype "$rtype" '[ .[] | select(.type == $rtype) ]' <<<"$records")
|
|
[ "$records" == "[]" ] && echo "ERROR: no ${rtype} record found" >&2 && exit 5
|
|
|
|
json_array_to_bash values < <("$JQ" --compact-output '[ .[] | .value ] | sort | unique' <<<"$records")
|
|
|
|
value=$1
|
|
if shift; then
|
|
dns_check_record_value "$rtype" "$value" value || exit 24
|
|
elif $interactive; then
|
|
if [ "$view" == "*" ]; then
|
|
dns_read_record_value "$rtype" value || exit 10
|
|
else
|
|
if ! yes_no "Delete all ${rtype} records?"; then
|
|
echo -e "\nSelect value:\n"
|
|
select value in "${values[@]}"; do
|
|
[ -n "$value" ] && break
|
|
done
|
|
fi
|
|
[ -z "$name" ] && echo "ERROR: invalid answer" >&2 && exit 11
|
|
echo
|
|
fi
|
|
fi
|
|
|
|
if [ -n "$value" ]; then
|
|
decoded_value=$(dns_decode_txt_value "$value")
|
|
records=$("$JQ" --compact-output --arg value "$decoded_value" '[ .[] | select(.decoded_value == $value) ]' <<<"$records")
|
|
[ "$records" == "[]" ] && echo "ERROR: no $rtype record matches value" >&2 && exit 6
|
|
fi
|
|
|
|
json_array_to_bash views < <("$JQ" --compact-output '[ .[] | .view ] | sort | unique' <<<"$records")
|
|
|
|
if ! $force; then
|
|
for view in "${views[@]}"; do
|
|
echo "View: $view"
|
|
json_array_to_bash values < <("$JQ" --compact-output '[ .[] | .value ] | sort | unique' <<<"$records")
|
|
for value in "${values[@]}"; do
|
|
output=$(dns_record_delete "true" "$zone" "$view" "$name" "$rtype" "$value" 2>&1)
|
|
if (( $? == 0 )); then
|
|
echo -n -e "\e[31m- $TAB"
|
|
echo -n -e "$output\e[0m" | grep --color=never -v -E '^(Outgoing update query:|;.*)?$'
|
|
else
|
|
echo -e "\e[31mERROR:\n" >&2
|
|
echo -e "$output\e[0m" >&2
|
|
exit 30
|
|
fi
|
|
done
|
|
done
|
|
echo
|
|
! yes_no "Proceed?" && echo -e "Aborted" && exit
|
|
echo
|
|
fi
|
|
|
|
echo -n "Sending DDNS update(s)... "
|
|
for view in "${views[@]}"; do
|
|
json_array_to_bash values < <("$JQ" --compact-output '[ .[] | .value ] | sort | unique' <<<"$records")
|
|
for value in "${values[@]}"; do
|
|
output=$(dns_record_delete "false" "$zone" "$view" "$name" "$rtype" "$value" 2>&1)
|
|
if (( $? != 0 )); then
|
|
echo -e "ERROR updating view -- '$view'\n" >&2
|
|
echo "$output" >&2
|
|
exit 31
|
|
fi
|
|
done
|
|
done
|
|
echo "OK"
|