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.

639 lines
21 KiB

  1. #!/bin/sh
  2. # function library used by adblock-update.sh
  3. # written by Dirk Brenken (openwrt@brenken.org)
  4. #####################################
  5. # f_envload: load adblock environment
  6. #
  7. f_envload()
  8. {
  9. local cfg_version
  10. # get version string from default adblock configuration file
  11. #
  12. cfg_version="$(/sbin/uci -q get adblock.global.adb_cfgver 2>/dev/null)"
  13. cfg_enabled="$(/sbin/uci -q get adblock.global.adb_enabled 2>/dev/null)"
  14. rc=$?
  15. if [ $((rc)) -ne 0 ] || [ "${cfg_version}" != "${adb_scriptver%.*}" ]
  16. then
  17. cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock" >/dev/null 2>&1
  18. rc=$?
  19. if [ $((rc)) -eq 0 ]
  20. then
  21. f_log "new default adblock configuration applied, please check your settings in '/etc/config/adblock'"
  22. else
  23. f_log "original adblock configuration not found, please (re-)install the adblock package via 'opkg install adblock --force-maintainer'" "${rc}"
  24. f_exit
  25. fi
  26. elif [ $((rc)) -eq 0 ] && [ $((cfg_enabled)) -ne 1 ]
  27. then
  28. rc=-1
  29. f_log "adblock is currently disabled, please run 'uci set adblock.global.adb_enabled=1' and 'uci commit adblock' to enable this service"
  30. f_exit
  31. fi
  32. # source in openwrt function library
  33. #
  34. if [ -r "/lib/functions.sh" ]
  35. then
  36. . "/lib/functions.sh" 2>/dev/null
  37. else
  38. rc=110
  39. f_log "openwrt function library not found" "${rc}"
  40. f_exit
  41. fi
  42. # source in openwrt network library
  43. #
  44. if [ -r "/lib/functions/network.sh" ]
  45. then
  46. . "/lib/functions/network.sh" 2>/dev/null
  47. else
  48. rc=115
  49. f_log "openwrt network library not found" "${rc}"
  50. f_exit
  51. fi
  52. # check opkg availability and get list with all installed openwrt packages
  53. #
  54. if [ -r "/var/lock/opkg.lock" ]
  55. then
  56. rc=-1
  57. f_log "adblock installation finished, 'opkg' currently locked by package installer"
  58. f_exit
  59. fi
  60. pkg_list="$(opkg list-installed 2>/dev/null)"
  61. if [ -z "${pkg_list}" ]
  62. then
  63. rc=120
  64. f_log "empty openwrt package list" "${rc}"
  65. f_exit
  66. fi
  67. }
  68. ######################################################
  69. # f_envparse: parse adblock config and set environment
  70. #
  71. f_envparse()
  72. {
  73. # set initial defaults,
  74. # may be overwritten by setting appropriate adblock config options in global section of /etc/config/adblock
  75. #
  76. adb_wanif="wan"
  77. adb_lanif="lan"
  78. adb_port="65535"
  79. adb_nullipv4="192.0.2.1"
  80. adb_nullipv6="::ffff:c000:0201"
  81. adb_blacklist="/etc/adblock/adblock.blacklist"
  82. adb_whitelist="/etc/adblock/adblock.whitelist"
  83. adb_forcedns=1
  84. # function to read global options by callback
  85. #
  86. config_cb()
  87. {
  88. local type="${1}"
  89. local name="${2}"
  90. if [ "${type}" = "adblock" ]
  91. then
  92. option_cb()
  93. {
  94. local option="${1}"
  95. local value="${2}"
  96. eval "${option}=\"${value}\""
  97. }
  98. else
  99. reset_cb
  100. fi
  101. }
  102. # function to iterate through config list, read only options in "enabled" sections
  103. #
  104. adb_cfglist="adb_backupdir adb_src"
  105. unset adb_sources
  106. parse_config()
  107. {
  108. local config="${1}"
  109. config_get switch "${config}" "enabled"
  110. if [ "${switch}" = "1" ]
  111. then
  112. for option in ${adb_cfglist}
  113. do
  114. config_get value "${config}" "${option}"
  115. if [ -n "${value}" ]
  116. then
  117. if [ "${option}" = "adb_src" ]
  118. then
  119. if [ "${config}" = "shalla" ]
  120. then
  121. categories()
  122. {
  123. local cat="${1}"
  124. adb_cat_shalla="${adb_cat_shalla} ${cat}"
  125. }
  126. eval "adb_arc_shalla=\"${value}\""
  127. config_list_foreach "shalla" "adb_catlist" "categories"
  128. else
  129. adb_sources="${adb_sources} ${value}"
  130. fi
  131. else
  132. eval "${option}=\"${value}\""
  133. fi
  134. fi
  135. done
  136. fi
  137. }
  138. # load adblock config and start parsing functions
  139. #
  140. config_load adblock
  141. config_foreach parse_config service
  142. config_foreach parse_config source
  143. # set more script defaults (can't be overwritten by adblock config options)
  144. #
  145. adb_count=0
  146. adb_minspace=12000
  147. adb_tmpfile="$(mktemp -tu 2>/dev/null)"
  148. adb_tmpdir="$(mktemp -p /tmp -d 2>/dev/null)"
  149. adb_dnsdir="/tmp/dnsmasq.d"
  150. adb_dnsprefix="adb_list"
  151. adb_prechain_ipv4="prerouting_rule"
  152. adb_fwdchain_ipv4="forwarding_rule"
  153. adb_outchain_ipv4="output_rule"
  154. adb_prechain_ipv6="PREROUTING"
  155. adb_fwdchain_ipv6="forwarding_rule"
  156. adb_outchain_ipv6="output_rule"
  157. adb_fetch="/usr/bin/wget"
  158. unset adb_srclist adb_revsrclist adb_errsrclist
  159. # set adblock source ruleset definitions
  160. #
  161. rset_start="sed -r 's/[[:space:]]|[\[!#/:;_].*|[0-9\.]*localhost.*//g; s/[\^#/:;_\.\t ]*$//g'"
  162. rset_end="tr -cd '[0-9a-z\.\-]\n' | sed -r 's/^[ \.\-].*$|^[a-z0-9]*[ \.\-]*$//g; /^[#/:;_\s]*$/d'"
  163. rset_adaway="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  164. rset_blacklist="${rset_start} | ${rset_end}"
  165. rset_disconnect="${rset_start} | ${rset_end}"
  166. rset_dshield="${rset_start} | ${rset_end}"
  167. rset_feodo="${rset_start} | ${rset_end}"
  168. rset_malware="${rset_start} | ${rset_end}"
  169. rset_malwarelist="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  170. rset_openphish="sed -e 's|^[^/]*//||' -e 's|/.*$||'"
  171. rset_palevo="${rset_start} | ${rset_end}"
  172. rset_ruadlist="sed -e '/^\|\|/! s/.*//; /\^$/! s/.*//; s/\^$//g; /[\.]/! s/.*//; s/^[\|]\{1,2\}//g' | ${rset_end}"
  173. rset_shalla="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}$//g' | ${rset_end}"
  174. rset_spam404="${rset_start} | sed 's/^\|\|//g' | ${rset_end}"
  175. rset_whocares="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  176. rset_winhelp="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  177. rset_yoyo="${rset_start} | sed 's/,/\n/g' | ${rset_end}"
  178. rset_zeus="${rset_start} | ${rset_end}"
  179. # get logical wan update interfaces (with default route) and their device names
  180. #
  181. network_find_wan adb_wanif4 2>/dev/null
  182. network_find_wan6 adb_wanif6 2>/dev/null
  183. if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
  184. then
  185. rc=125
  186. f_log "no valid IPv4/IPv6 wan update interface found" "${rc}"
  187. f_exit
  188. elif [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
  189. then
  190. rc=125
  191. f_log "LAN only (${adb_lanif}) network, no valid IPv4/IPv6 wan update interface found" "${rc}"
  192. f_exit
  193. else
  194. network_get_device adb_wandev4 "${adb_wanif4}" 2>/dev/null
  195. network_get_device adb_wandev6 "${adb_wanif6}" 2>/dev/null
  196. break
  197. fi
  198. # get lan ip addresses
  199. #
  200. network_get_ipaddr adb_ipv4 "${adb_lanif}" 2>/dev/null
  201. network_get_ipaddr6 adb_ipv6 "${adb_lanif}" 2>/dev/null
  202. if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
  203. then
  204. rc=130
  205. f_log "no valid IPv4/IPv6 configuration for given logical LAN interface found (${adb_lanif}), please set 'adb_lanif' manually" "${rc}"
  206. f_exit
  207. fi
  208. }
  209. #################################################
  210. # f_envcheck: check/set environment prerequisites
  211. #
  212. f_envcheck()
  213. {
  214. local check
  215. # check general package dependencies
  216. #
  217. f_depend "uhttpd"
  218. f_depend "wget"
  219. f_depend "iptables"
  220. f_depend "kmod-ipt-nat"
  221. # check ipv6 related package dependencies
  222. #
  223. if [ -n "${adb_wanif6}" ]
  224. then
  225. check="$(printf "${pkg_list}" | grep "^ip6tables -" 2>/dev/null)"
  226. if [ -z "${check}" ]
  227. then
  228. f_log "package 'ip6tables' not found, IPv6 support will be disabled"
  229. unset adb_wanif6
  230. else
  231. check="$(printf "${pkg_list}" | grep "^kmod-ipt-nat6 -" 2>/dev/null)"
  232. if [ -z "${check}" ]
  233. then
  234. f_log "package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
  235. unset adb_wanif6
  236. fi
  237. fi
  238. fi
  239. # check ca-certificates package and set wget parms accordingly
  240. #
  241. wget_parm="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=5 --connect-timeout=5 --read-timeout=5"
  242. check="$(printf "${pkg_list}" | grep "^ca-certificates -" 2>/dev/null)"
  243. if [ -z "${check}" ]
  244. then
  245. wget_parm="${wget_parm} --no-check-certificate"
  246. fi
  247. # check adblock blacklist/whitelist configuration
  248. #
  249. if [ ! -r "${adb_blacklist}" ]
  250. then
  251. f_log "adblock blacklist not found, source will be disabled"
  252. fi
  253. if [ ! -r "${adb_whitelist}" ]
  254. then
  255. f_log "adblock whitelist not found, source will be disabled"
  256. fi
  257. # check adblock temp directory
  258. #
  259. if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
  260. then
  261. f_space "${adb_tmpdir}"
  262. if [ "${space_ok}" = "false" ]
  263. then
  264. if [ $((av_space)) -le 2000 ]
  265. then
  266. rc=135
  267. f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)" "${rc}"
  268. f_exit
  269. else
  270. f_log "not enough free space to handle all adblock list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
  271. fi
  272. fi
  273. else
  274. rc=135
  275. f_log "temp directory not found" "${rc}"
  276. f_exit
  277. fi
  278. # memory check
  279. #
  280. mem_total="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
  281. mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
  282. mem_swap="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
  283. if [ $((mem_total)) -le 64000 ] && [ $((mem_swap)) -eq 0 ]
  284. then
  285. mem_ok="false"
  286. f_log "not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
  287. fi
  288. # check backup configuration
  289. #
  290. if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
  291. then
  292. f_space "${adb_backupdir}"
  293. if [ "${space_ok}" = "false" ]
  294. then
  295. f_log "not enough free space in '${adb_backupdir}'(avail. ${av_space} kb), backup/restore will be disabled"
  296. backup_ok="false"
  297. else
  298. f_log "backup/restore will be enabled"
  299. backup_ok="true"
  300. fi
  301. else
  302. backup_ok="false"
  303. f_log "backup/restore will be disabled"
  304. fi
  305. # check ipv4/iptables configuration
  306. #
  307. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wandev4}" ]
  308. then
  309. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-prerouting" "! -i ${adb_wandev4} -p tcp -d ${adb_nullipv4} -m multiport --dports 80,443 -j REDIRECT --to-ports ${adb_port}"
  310. f_firewall "IPv4" "filter" "A" "${adb_fwdchain_ipv4}" "adb-forward" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  311. f_firewall "IPv4" "filter" "A" "${adb_outchain_ipv4}" "adb-output" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  312. if [ $((adb_forcedns)) -eq 1 ]
  313. then
  314. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p udp --dport 53 -j REDIRECT"
  315. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p tcp --dport 53 -j REDIRECT"
  316. fi
  317. if [ "${fw_done}" = "true" ]
  318. then
  319. f_log "created volatile IPv4 firewall ruleset"
  320. fw_done="false"
  321. fi
  322. fi
  323. # check ipv6/ip6tables configuration
  324. #
  325. if [ -n "${adb_wanif6}" ] && [ -n "${adb_wandev6}" ]
  326. then
  327. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-prerouting" "! -i ${adb_wandev6} -p tcp -d ${adb_nullipv6} -m multiport --dports 80,443 -j REDIRECT --to-ports ${adb_port}"
  328. f_firewall "IPv6" "filter" "A" "${adb_fwdchain_ipv6}" "adb-forward" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  329. f_firewall "IPv6" "filter" "A" "${adb_outchain_ipv6}" "adb-output" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  330. if [ $((adb_forcedns)) -eq 1 ]
  331. then
  332. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p udp --dport 53 -j REDIRECT"
  333. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p tcp --dport 53 -j REDIRECT"
  334. fi
  335. if [ "${fw_done}" = "true" ]
  336. then
  337. f_log "created volatile IPv6 firewall ruleset"
  338. fw_done="false"
  339. fi
  340. fi
  341. # check volatile adblock uhttpd instance configuration
  342. #
  343. rc="$(ps | grep "[u]httpd.*\-h /www/adblock" >/dev/null 2>&1; printf ${?})"
  344. if [ $((rc)) -ne 0 ]
  345. then
  346. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  347. then
  348. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}" -p "[${adb_ipv6}]:${adb_port}">/dev/null 2>&1
  349. rc=${?}
  350. elif [ -n "${adb_wanif4}" ]
  351. then
  352. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}" >/dev/null 2>&1
  353. rc=${?}
  354. elif [ -n "${adb_wanif6}" ]
  355. then
  356. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_port}" >/dev/null 2>&1
  357. rc=${?}
  358. fi
  359. if [ $((rc)) -eq 0 ]
  360. then
  361. f_log "created volatile uhttpd instance"
  362. else
  363. f_log "failed to initialize volatile uhttpd instance" "${rc}"
  364. f_restore
  365. fi
  366. fi
  367. # set dnsmasq defaults
  368. #
  369. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  370. then
  371. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
  372. elif [ -n "${adb_wanif4}" ]
  373. then
  374. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
  375. elif [ -n "${adb_wanif6}" ]
  376. then
  377. adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
  378. fi
  379. # remove no longer used opkg package list
  380. #
  381. unset pkg_list
  382. }
  383. ######################################
  384. # f_depend: check package dependencies
  385. #
  386. f_depend()
  387. {
  388. local check
  389. local package="${1}"
  390. check="$(printf "${pkg_list}" | grep "^${package} -" 2>/dev/null)"
  391. if [ -z "${check}" ]
  392. then
  393. rc=140
  394. f_log "package '${package}' not found" "${rc}"
  395. f_exit
  396. fi
  397. }
  398. ##############################################
  399. # f_firewall: set iptables rules for ipv4/ipv6
  400. #
  401. f_firewall()
  402. {
  403. local ipt
  404. local iptv4="/usr/sbin/iptables"
  405. local iptv6="/usr/sbin/ip6tables"
  406. local proto="${1}"
  407. local table="${2}"
  408. local ctype="${3}"
  409. local chain="${4}"
  410. local notes="${5}"
  411. local rules="${6}"
  412. # select appropriate iptables executable
  413. #
  414. if [ "${proto}" = "IPv4" ]
  415. then
  416. ipt="${iptv4}"
  417. else
  418. ipt="${iptv6}"
  419. fi
  420. # check whether iptables rule already applied and proceed accordingly
  421. #
  422. rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
  423. if [ $((rc)) -ne 0 ]
  424. then
  425. "${ipt}" -w -t "${table}" -"${ctype}" "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1
  426. rc=${?}
  427. if [ $((rc)) -eq 0 ]
  428. then
  429. fw_done="true"
  430. else
  431. f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}"
  432. f_restore
  433. fi
  434. fi
  435. }
  436. ##########################################
  437. # f_log: log messages to stdout and syslog
  438. #
  439. f_log()
  440. {
  441. local log_parm
  442. local log_msg="${1}"
  443. local log_rc="${2}"
  444. local class="info "
  445. # check for terminal session
  446. #
  447. if [ -t 1 ]
  448. then
  449. log_parm="-s"
  450. fi
  451. # log to different output devices and set log class accordingly
  452. #
  453. if [ -n "${log_msg}" ]
  454. then
  455. if [ $((log_rc)) -gt 0 ]
  456. then
  457. class="error"
  458. log_rc=", rc: ${log_rc}"
  459. log_msg="${log_msg}${log_rc}"
  460. fi
  461. /usr/bin/logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}"
  462. fi
  463. }
  464. ################################################
  465. # f_space: check mount points/space requirements
  466. #
  467. f_space()
  468. {
  469. local mp="${1}"
  470. # check relevant mount points in a subshell
  471. #
  472. if [ -d "${mp}" ]
  473. then
  474. av_space="$(df "${mp}" 2>/dev/null | tail -n1 2>/dev/null | awk '{printf $4}')"
  475. if [ $((av_space)) -lt $((adb_minspace)) ]
  476. then
  477. space_ok="false"
  478. fi
  479. fi
  480. }
  481. ##################################################################
  482. # f_restore: restore last adblock list backups and restart dnsmasq
  483. #
  484. f_restore()
  485. {
  486. local rm_done
  487. local restore_done
  488. # remove bogus adblock lists
  489. #
  490. if [ -n "${adb_revsrclist}" ]
  491. then
  492. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \; 2>/dev/null)"
  493. rc=${?}
  494. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  495. then
  496. f_log "all bogus adblock lists removed"
  497. elif [ $((rc)) -ne 0 ]
  498. then
  499. f_log "error during removal of bogus adblock lists" "${rc}"
  500. f_exit
  501. fi
  502. fi
  503. # restore backups
  504. #
  505. if [ "${backup_ok}" = "true" ] && [ "$(printf "${adb_backupdir}/${adb_dnsprefix}."*)" != "${adb_backupdir}/${adb_dnsprefix}.*" ]
  506. then
  507. restore_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec cp -pf "{}" "${adb_dnsdir}" \; 2>/dev/null)"
  508. rc=${?}
  509. if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
  510. then
  511. f_log "all available backups restored"
  512. elif [ $((rc)) -ne 0 ]
  513. then
  514. f_log "error during restore of adblock lists" "${rc}"
  515. f_exit
  516. fi
  517. else
  518. f_log "no backups found, nothing to restore"
  519. fi
  520. # (re-)try dnsmasq restart without bogus adblock lists / with backups
  521. #
  522. if [ -n "${restore_done}" ] || [ -n "${rm_done}" ]
  523. then
  524. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  525. sleep 1
  526. dns_status="$(ps 2>/dev/null | grep "[d]nsmasq" 2>/dev/null)"
  527. if [ -n "${dns_status}" ]
  528. then
  529. rc=0
  530. adb_count="$(head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null | wc -l)"
  531. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  532. then
  533. adb_count="$((adb_count / 2))"
  534. fi
  535. f_log "adblock lists with overall ${adb_count} domains loaded"
  536. else
  537. rc=145
  538. f_log "dnsmasq restart failed, please check 'logread' output" "${rc}"
  539. fi
  540. fi
  541. f_exit
  542. }
  543. ###################################
  544. # f_exit: delete (temporary) files,
  545. # generate statistics and exit
  546. #
  547. f_exit()
  548. {
  549. local ipv4_prerouting=0
  550. local ipv4_forward=0
  551. local ipv4_output=0
  552. local ipv6_prerouting=0
  553. local ipv6_forward=0
  554. local ipv6_output=0
  555. local iptv4="/usr/sbin/iptables"
  556. local iptv6="/usr/sbin/ip6tables"
  557. # delete temporary files & directories
  558. #
  559. if [ -f "${adb_tmpfile}" ]
  560. then
  561. rm -f "${adb_tmpfile}" >/dev/null 2>&1
  562. fi
  563. if [ -d "${adb_tmpdir}" ]
  564. then
  565. rm -rf "${adb_tmpdir}" >/dev/null 2>&1
  566. fi
  567. # final log message and iptables statistics
  568. #
  569. if [ $((rc)) -eq 0 ]
  570. then
  571. if [ -n "${adb_wanif4}" ]
  572. then
  573. ipv4_prerouting="$(${iptv4} -t nat -vnL | awk '$11 ~ /^adb-prerouting$/ {sum += $1} END {printf sum}')"
  574. ipv4_forward="$(${iptv4} -vnL | awk '$11 ~ /^adb-forward$/ {sum += $1} END {printf sum}')"
  575. ipv4_output="$(${iptv4} -vnL | awk '$11 ~ /^adb-output$/ {sum += $1} END {printf sum}')"
  576. fi
  577. if [ -n "${adb_wanif6}" ]
  578. then
  579. ipv6_prerouting="$(${iptv6} -t nat -vnL | awk '$10 ~ /^adb-prerouting$/ {sum += $1} END {printf sum}')"
  580. ipv6_forward="$(${iptv6} -vnL | awk '$10 ~ /^adb-forward$/ {sum += $1} END {printf sum}')"
  581. ipv6_output="$(${iptv6} -vnL | awk '$10 ~ /^adb-output$/ {sum += $1} END {printf sum}')"
  582. fi
  583. f_log "adblock firewall statistics (IPv4/IPv6):"
  584. f_log "${ipv4_prerouting}/${ipv6_prerouting} packets redirected in PREROUTING chain"
  585. f_log "${ipv4_forward}/${ipv6_forward} packets rejected in FORWARD chain"
  586. f_log "${ipv4_output}/${ipv6_output} packets rejected in OUTPUT chain"
  587. f_log "domain adblock processing finished successfully (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  588. elif [ $((rc)) -gt 0 ]
  589. then
  590. f_log "domain adblock processing failed (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  591. else
  592. rc=0
  593. fi
  594. rm -f "${adb_pidfile}" >/dev/null 2>&1
  595. exit ${rc}
  596. }