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.

122 lines
3.7 KiB

  1. #!/bin/sh
  2. # helper script to resolve domains for adding to banIP-related IPSets
  3. # written by Dirk Brenken (dev@brenken.org)
  4. #
  5. # This is free software, licensed under the GNU General Public License v3.
  6. #
  7. # (s)hellcheck exceptions
  8. # shellcheck disable=1091,2030,2031,2034,2039,2086,2129,2140,2143,2154,2181,2183,2188
  9. export LC_ALL=C
  10. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. set -o pipefail
  12. if [ -r "/lib/functions.sh" ]
  13. then
  14. . "/lib/functions.sh"
  15. ban_debug="$(uci_get banip global ban_debug "0")"
  16. ban_tmpbase="$(uci_get banip global ban_tmpbase "/tmp")"
  17. ban_backupdir="$(uci_get banip global ban_backupdir "${ban_tmpbase}/banIP-Backup")"
  18. ban_proto4_enabled="$(uci_get banip global ban_proto4_enabled "0")"
  19. ban_proto6_enabled="$(uci_get banip global ban_proto6_enabled "0")"
  20. else
  21. exit 1
  22. fi
  23. ban_ver="${1}"
  24. ban_action="${2}"
  25. ban_src_name="${3}"
  26. ban_src_file="${4}"
  27. ban_ipset_cmd="$(command -v ipset)"
  28. ban_lookup_cmd="$(command -v nslookup)"
  29. ban_logger_cmd="$(command -v logger)"
  30. ban_rc=1
  31. f_log()
  32. {
  33. local class="${1}" log_msg="${2}"
  34. if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${ban_debug}" = "1" ]; }
  35. then
  36. if [ -x "${ban_logger_cmd}" ]
  37. then
  38. "${ban_logger_cmd}" -p "${class}" -t "banIP-${ban_ver%-*}[${$}]" "${log_msg}"
  39. else
  40. printf "%s %s %s\n" "${class}" "banIP-${ban_ver%-*}[${$}]" "${log_msg}"
  41. fi
  42. fi
  43. }
  44. if [ "${ban_action}" = "start" ] || [ "${ban_action}" = "refresh" ]
  45. then
  46. for proto in "4" "6"
  47. do
  48. if [ -s "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}.gz" ]
  49. then
  50. gzip -df "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}.gz"
  51. if [ "${?}" = "0" ]
  52. then
  53. ban_rc=0
  54. else
  55. ban_rc=1
  56. break
  57. fi
  58. fi
  59. done
  60. fi
  61. if [ "${ban_rc}" = "1" ]
  62. then
  63. > "${ban_backupdir}/banIP.${ban_src_name}_addon_4"
  64. > "${ban_backupdir}/banIP.${ban_src_name}_addon_6"
  65. while read -r domain
  66. do
  67. update_ips=""
  68. result="$("${ban_lookup_cmd}" "${domain}" 2>/dev/null; printf "%s" "${?}")"
  69. if [ "$(printf "%s" "${result}" | tail -1)" = "0" ]
  70. then
  71. ips="$(printf "%s" "${result}" | awk '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
  72. for ip in ${ips}
  73. do
  74. for proto in "4" "6"
  75. do
  76. if { [ "${proto}" = "4" ] && [ "${ban_proto4_enabled}" = "1" ] && \
  77. [ -n "$("${ban_ipset_cmd}" -q -n list "${ban_src_name}_${proto}")" ] && \
  78. [ -n "$(printf "%s" "${ip}" | awk '/^(([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?)([[:space:]]|$)/{print $1}')" ]; } || \
  79. { [ "${proto}" = "6" ] && [ "${ban_proto6_enabled}" = "1" ] && \
  80. [ -n "$("${ban_ipset_cmd}" -q -n list "${ban_src_name}_${proto}")" ] && \
  81. [ -z "$(printf "%s" "${ip}" | awk '/^(([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?)([[:space:]]|$)/{print $1}')" ]; }
  82. then
  83. printf "%s\n" "add ${ban_src_name}_${proto} ${ip}" >> "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  84. if [ -z "${update_ips}" ]
  85. then
  86. update_ips="${ip}"
  87. else
  88. update_ips="${update_ips}, ${ip}"
  89. fi
  90. fi
  91. done
  92. done
  93. if [ -n "${update_ips}" ]
  94. then
  95. ban_rc=0
  96. f_log "debug" "dns_imp ::: source '${ban_src_name}' supplemented by '${domain}' (${update_ips})"
  97. fi
  98. fi
  99. done < "${ban_src_file}"
  100. fi
  101. if [ "${ban_rc}" = "0" ]
  102. then
  103. for proto in "4" "6"
  104. do
  105. if [ -n "$("${ban_ipset_cmd}" -q -n list "${ban_src_name}_${proto}")" ] && [ -s "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}" ]
  106. then
  107. "${ban_ipset_cmd}" -q -! restore < "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  108. gzip -f "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  109. fi
  110. rm -f "${ban_backupdir}/banIP.${ban_src_name}_addon_${proto}"
  111. done
  112. fi
  113. f_log "info" "banIP domain import for source '${ban_src_name}' has been finished with rc '${ban_rc}'"
  114. rm -f "${ban_src_file}"
  115. exit "${ban_rc}"