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.

293 lines
9.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (c) 2018-2021 Dirk Brenken (dev@brenken.org)
  3. # This is free software, licensed under the GNU General Public License v3.
  4. # (s)hellcheck exceptions
  5. # shellcheck disable=1091,2034,3043,3057,3060
  6. START=30
  7. USE_PROCD=1
  8. if type extra_command >/dev/null 2>&1; then
  9. extra_command "refresh" "Refresh ipsets without new list downloads"
  10. extra_command "suspend" "Suspend banIP processing"
  11. extra_command "resume" "Resume banIP processing"
  12. extra_command "query" "<IP> Query active banIP IPSets for a specific IP address"
  13. extra_command "report" "[<cli>|<mail>|<gen>|<json>] Print banIP related IPset statistics"
  14. extra_command "list" "[<add>|<add_asn>|<add_country>|<remove>|<remove_asn>|<remove_country>] <source(s)> List/Edit available sources"
  15. extra_command "timer" "[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
  16. else
  17. EXTRA_COMMANDS="status refresh suspend resume query report list timer version"
  18. EXTRA_HELP=" status Service status
  19. refresh Refresh ipsets without new list downloads
  20. suspend Suspend banIP processing
  21. resume Resume banIP processing
  22. query <IP> Query active banIP IPSets for a specific IP address
  23. report [<cli>|<mail>|<gen>|<json>] Print banIP related IPset statistics
  24. list [<add>|<add_asn>|<add_country>|<remove>|<remove_asn>|<remove_country>] <source(s)> List/Edit available sources
  25. timer [<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
  26. fi
  27. ban_init="/etc/init.d/banip"
  28. ban_script="/usr/bin/banip.sh"
  29. ban_pidfile="/var/run/banip.pid"
  30. if [ -s "${ban_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] ||
  31. [ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "refresh" ] ||
  32. [ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] ||
  33. { [ "${action}" = "list" ] && [ -n "${1}" ]; } || { [ "${action}" = "report" ] && [ "${1}" != "json" ]; }; }; then
  34. exit 0
  35. fi
  36. boot() {
  37. : >"${ban_pidfile}"
  38. rc_procd start_service
  39. }
  40. start_service() {
  41. if "${ban_init}" enabled; then
  42. if [ "${action}" = "boot" ]; then
  43. return 0
  44. fi
  45. procd_open_instance "banip"
  46. procd_set_param command "${ban_script}" "${@}"
  47. procd_set_param pidfile "${ban_pidfile}"
  48. procd_set_param nice "$(uci_get banip global ban_nice "0")"
  49. procd_set_param stdout 1
  50. procd_set_param stderr 1
  51. procd_close_instance
  52. fi
  53. }
  54. version() {
  55. rc_procd "${ban_script}" version
  56. }
  57. refresh() {
  58. rc_procd start_service refresh
  59. }
  60. reload_service() {
  61. rc_procd start_service reload
  62. }
  63. stop_service() {
  64. rc_procd "${ban_script}" stop
  65. }
  66. restart() {
  67. rc_procd start_service restart
  68. }
  69. suspend() {
  70. rc_procd start_service suspend
  71. }
  72. resume() {
  73. rc_procd start_service resume
  74. }
  75. query() {
  76. rc_procd "${ban_script}" query "${1}"
  77. }
  78. list() {
  79. local src_archive src_file src_enabled key name enabled focus descurl url_4 rule_4 url_6 rule_6 action="${1}"
  80. if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]; then
  81. shift
  82. for name in "${@}"; do
  83. case "${action}" in
  84. "add")
  85. if ! uci_get banip global ban_sources | grep -q "${name}"; then
  86. uci_add_list banip global ban_sources "${name}"
  87. printf "%s\n" "::: banIP source '${name}' added to config"
  88. fi
  89. ;;
  90. "remove")
  91. if uci_get banip global ban_sources | grep -q "${name}"; then
  92. uci_remove_list banip global ban_sources "${name}"
  93. printf "%s\n" "::: banIP source '${name}' removed from config"
  94. fi
  95. ;;
  96. "add_asn")
  97. if ! uci_get banip global ban_asns | grep -q "${name}"; then
  98. uci_add_list banip global ban_asns "${name}"
  99. printf "%s\n" "::: banIP asn '${name}' added to config"
  100. fi
  101. ;;
  102. "remove_asn")
  103. if uci_get banip global ban_asns | grep -q "${name}"; then
  104. uci_remove_list banip global ban_asns "${name}"
  105. printf "%s\n" "::: banIP asn '${name}' removed from config"
  106. fi
  107. ;;
  108. "add_country")
  109. if ! uci_get banip global ban_countries | grep -q "${name}"; then
  110. uci_add_list banip global ban_countries "${name}"
  111. printf "%s\n" "::: banIP country '${name}' added to config"
  112. fi
  113. ;;
  114. "remove_country")
  115. if uci_get banip global ban_countries | grep -q "${name}"; then
  116. uci_remove_list banip global ban_countries "${name}"
  117. printf "%s\n" "::: banIP country '${name}' removed from config"
  118. fi
  119. ;;
  120. esac
  121. done
  122. if [ -n "$(uci -q changes banip)" ]; then
  123. uci_commit banip
  124. "${ban_init}" start
  125. fi
  126. else
  127. src_archive="$(uci_get banip global ban_srcarc "/etc/banip/banip.sources.gz")"
  128. src_file="$(uci_get banip global ban_srcfile "/tmp/ban_sources.json")"
  129. src_enabled="$(uci -q show banip.global.ban_sources)"
  130. if [ -r "${src_archive}" ]; then
  131. zcat "${src_archive}" >"${src_file}"
  132. else
  133. printf "%s\n" "::: banIP source archive '${src_archive}' not found"
  134. fi
  135. if [ -r "${src_file}" ]; then
  136. src_enabled="${src_enabled#*=}"
  137. src_enabled="${src_enabled//\'/}"
  138. printf "%s\n" "::: Available banIP sources"
  139. printf "%s\n" ":::"
  140. printf "%-25s%-10s%-36s%s\n" " Name" "Enabled" "Focus" "Info URL"
  141. printf "%s\n" " ---------------------------------------------------------------------------"
  142. json_load_file "${src_file}"
  143. json_get_keys keylist
  144. for key in ${keylist}; do
  145. json_select "${key}"
  146. json_get_var focus "focus"
  147. json_get_var descurl "descurl"
  148. json_get_var url_4 "url_4"
  149. json_get_var rule_4 "rule_4"
  150. json_get_var url_6 "url_6"
  151. json_get_var rule_6 "rule_6"
  152. if { [ -n "${url_4}" ] && [ -n "${rule_4}" ]; } || { [ -n "${url_6}" ] && [ -n "${rule_6}" ]; }; then
  153. if printf "%s" "${src_enabled}" | grep -q "${key}"; then
  154. enabled="x"
  155. else
  156. enabled=" "
  157. fi
  158. src_enabled="${src_enabled/${key}/}"
  159. printf " + %-21s%-10s%-36s%s\n" "${key:0:20}" "${enabled}" "${focus:0:35}" "${descurl:0:50}"
  160. else
  161. src_enabled="${src_enabled} ${key}"
  162. fi
  163. json_select ..
  164. done
  165. asn_list="$(uci_get banip global ban_asns "-")"
  166. country_list="$(uci_get banip global ban_countries "-")"
  167. printf "%s\n" " ---------------------------------------------------------------------------"
  168. printf " * %s\n" "Configured ASNs: ${asn_list// /, }"
  169. printf " * %s\n" "Configured Countries: ${country_list// /, }"
  170. if [ -n "${src_enabled// /}" ]; then
  171. printf "%s\n" " ---------------------------------------------------------------------------"
  172. printf "%s\n" " Sources without valid configuration"
  173. printf "%s\n" " ---------------------------------------------------------------------------"
  174. for key in ${src_enabled}; do
  175. printf " - %s\n" "${key:0:20}"
  176. done
  177. fi
  178. else
  179. printf "%s\n" "::: banIP source file '${src_file}' not found"
  180. fi
  181. fi
  182. }
  183. status() {
  184. status_service
  185. }
  186. status_service() {
  187. local key keylist type value index_value values rtfile
  188. rtfile="$(uci_get banip global ban_rtfile "/tmp/ban_runtime.json")"
  189. json_load_file "${rtfile}" >/dev/null 2>&1
  190. json_get_keys keylist
  191. if [ -n "${keylist}" ]; then
  192. printf "%s\n" "::: banIP runtime information"
  193. for key in ${keylist}; do
  194. json_get_var value "${key}" >/dev/null 2>&1
  195. if [ "${key%_*}" = "active" ]; then
  196. printf " + %-15s : " "${key}"
  197. json_select "${key}" >/dev/null 2>&1
  198. values=""
  199. index=1
  200. while json_get_type type "${index}" && [ "${type}" = "object" ]; do
  201. json_get_values index_value "${index}" >/dev/null 2>&1
  202. if [ "${index}" = "1" ]; then
  203. values="${index_value}"
  204. else
  205. values="${values}, ${index_value}"
  206. fi
  207. index=$((index + 1))
  208. done
  209. values="$(printf "%s" "${values}" | awk '{NR=1;max=98;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max)}else{printf"%-22s%s\n","",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
  210. printf "%s\n" "${values:-"-"}"
  211. json_select ".."
  212. else
  213. printf " + %-15s : %s\n" "${key}" "${value:-"-"}"
  214. fi
  215. done
  216. else
  217. printf "%s\n" "::: no banIP runtime information available"
  218. fi
  219. }
  220. report() {
  221. rc_procd "${ban_script}" report "${1:-"cli"}"
  222. }
  223. timer() {
  224. local cron_file cron_content cron_lineno action="${1:-"list"}" cron_tasks="${2}" hour="${3}" minute="${4:-0}" weekday="${5:-"*"}"
  225. cron_file="/etc/crontabs/root"
  226. if [ -s "${cron_file}" ] && [ "${action}" = "list" ]; then
  227. awk '{print NR "> " $0}' "${cron_file}"
  228. elif [ "${action}" = "add" ]; then
  229. hour="${hour//[[:alpha:]]/}"
  230. minute="${minute//[[:alpha:]]/}"
  231. if [ -n "${cron_tasks}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] &&
  232. [ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] &&
  233. [ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]; then
  234. printf "%02d %02d %s\n" "${minute}" "${hour}" "* * ${weekday} ${ban_init} ${cron_tasks}" >>"${cron_file}"
  235. /etc/init.d/cron restart
  236. fi
  237. elif [ -s "${cron_file}" ] && [ "${action}" = "remove" ]; then
  238. cron_tasks="${cron_tasks//[[:alpha:]]/}"
  239. cron_lineno="$(awk 'END{print NR}' "${cron_file}")"
  240. cron_content="$(awk '{print $0}' "${cron_file}")"
  241. if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]; then
  242. printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" >"${cron_file}"
  243. /etc/init.d/cron restart
  244. fi
  245. fi
  246. }
  247. service_triggers() {
  248. local iface delay
  249. iface="$(uci_get banip global ban_trigger)"
  250. delay="$(uci_get banip global ban_triggerdelay "5")"
  251. PROCD_RELOAD_DELAY=$((delay * 1000))
  252. if [ -z "${iface}" ]; then
  253. . "/lib/functions/network.sh"
  254. network_find_wan iface
  255. if [ -n "${iface}" ]; then
  256. uci_set banip global ban_trigger "${iface}"
  257. uci_commit "banip"
  258. fi
  259. fi
  260. if [ -n "${iface}" ]; then
  261. procd_add_interface_trigger "interface.*.up" "${iface}" "${ban_init}" "start"
  262. fi
  263. procd_add_reload_trigger "banip"
  264. }