You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
3.1 KiB

  1. #!/bin/sh
  2. # helper script to resolve domains for adding to banIP-related IPSets
  3. # Copyright (c) 2020-2021 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # (s)hellcheck exceptions
  6. # shellcheck disable=1091,3040
  7. export LC_ALL=C
  8. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  9. set -o pipefail
  10. . "/lib/functions.sh"
  11. ban_src_name="${1}"
  12. ban_src_file="${2}"
  13. ban_tmpbase="$(uci_get banip global ban_tmpbase "/tmp")"
  14. ban_backupdir="$(uci_get banip global ban_backupdir "${ban_tmpbase}/banIP-Backup")"
  15. ban_proto4_enabled="$(uci_get banip global ban_proto4_enabled "0")"
  16. ban_proto6_enabled="$(uci_get banip global ban_proto6_enabled "0")"
  17. ban_ipset_cmd="$(command -v ipset)"
  18. ban_lookup_cmd="$(command -v nslookup)"
  19. ban_logger_cmd="$(command -v logger)"
  20. ban_cnt_err="0"
  21. ban_message=""
  22. rm -f "${ban_backupdir}/banIP.${ban_src_name}_addon_4" "${ban_backupdir}/banIP.${ban_src_name}_addon_6"
  23. while read -r domain; do
  24. result="$(
  25. "${ban_lookup_cmd}" "${domain}" 2>/dev/null
  26. printf "%s" "${?}"
  27. )"
  28. if [ "$(printf "%s" "${result}" | tail -1)" = "0" ]; then
  29. ips="$(printf "%s" "${result}" | awk '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
  30. for ip in ${ips}; do
  31. for proto in "4" "6"; do
  32. if { [ "${proto}" = "4" ] && [ "${ban_proto4_enabled}" = "1" ] && [ -n "$("${ban_ipset_cmd}" -q -n list "${ban_src_name}_${proto}")" ] &&
  33. [ -n "$(printf "%s" "${ip}" | awk '/^(([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?)([[:space:]]|$)/{print $1}')" ]; } ||
  34. { [ "${proto}" = "6" ] && [ "${ban_proto6_enabled}" = "1" ] && [ -n "$("${ban_ipset_cmd}" -q -n list "${ban_src_name}_${proto}")" ] &&
  35. [ -n "$(printf "%s" "${ip}" | awk '/^(([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?)([[:space:]]|$)/{print $1}')" ]; }; then
  36. printf "%s\n" "add ${ban_src_name}_${proto} ${ip}" >>"${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  37. fi
  38. done
  39. done
  40. [ -n "${ips}" ] && "${ban_logger_cmd}" -p "debug" -t "banIP-resolve [${$}]" "added IPs of '${domain}' to ${ban_src_name} (${ips})" 2>/dev/null
  41. else
  42. ban_cnt_err=$((ban_cnt_err + 1))
  43. fi
  44. done <"${ban_src_file}"
  45. for proto in "4" "6"; do
  46. if { { [ "${proto}" = "4" ] && [ "${ban_proto4_enabled}" = "1" ]; } || { [ "${proto}" = "6" ] && [ "${ban_proto6_enabled}" = "1" ]; }; } &&
  47. [ ! -s "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}" ] && [ -s "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}.gz" ]; then
  48. gzip -df "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}.gz" 2>/dev/null
  49. "${ban_ipset_cmd}" -q -! restore <"${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  50. ban_message="backup used"
  51. elif [ -n "$("${ban_ipset_cmd}" -q -n list "${ban_src_name}_${proto}")" ] && [ -s "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}" ]; then
  52. "${ban_ipset_cmd}" -q -! restore <"${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  53. ban_message="${ban_cnt_err} lookup errors"
  54. fi
  55. gzip -f "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}" 2>/dev/null
  56. done
  57. "${ban_logger_cmd}" -p "info" -t "banIP-resolve [${$}]" "${ban_src_name} domain import has been finished (${ban_message:-"-"})" 2>/dev/null
  58. rm -f "${ban_src_file}"