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.

345 lines
9.8 KiB

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