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.

753 lines
22 KiB

  1. #!/bin/sh
  2. # function library used by adblock-update.sh
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # set initial defaults
  5. #
  6. LC_ALL=C
  7. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  8. adb_scriptver="1.5.4"
  9. adb_mincfgver="2.5"
  10. adb_hotplugif=""
  11. adb_lanif="lan"
  12. adb_nullport="65534"
  13. adb_nullportssl="65535"
  14. adb_nullipv4="198.18.0.1"
  15. adb_nullipv6="::ffff:c612:0001"
  16. adb_whitelist="/etc/adblock/adblock.whitelist"
  17. adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
  18. adb_dnsdir="/tmp/dnsmasq.d"
  19. adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
  20. adb_dnsprefix="adb_list"
  21. adb_count=0
  22. adb_minspace=12000
  23. adb_forcedns=1
  24. adb_fetchttl=5
  25. adb_restricted=0
  26. adb_loglevel=1
  27. adb_uci="$(which uci)"
  28. # f_envload: load adblock environment
  29. #
  30. f_envload()
  31. {
  32. # source in system function library
  33. #
  34. if [ -r "/lib/functions.sh" ]
  35. then
  36. . "/lib/functions.sh"
  37. else
  38. rc=-10
  39. f_log "system function library not found, please check your installation"
  40. f_exit
  41. fi
  42. # source in system network library
  43. #
  44. if [ -r "/lib/functions/network.sh" ]
  45. then
  46. . "/lib/functions/network.sh"
  47. else
  48. rc=-10
  49. f_log "system network library not found, please check your installation"
  50. f_exit
  51. fi
  52. # uci function to parse global section by callback
  53. #
  54. config_cb()
  55. {
  56. local type="${1}"
  57. if [ "${type}" = "adblock" ]
  58. then
  59. option_cb()
  60. {
  61. local option="${1}"
  62. local value="${2}"
  63. eval "${option}=\"${value}\""
  64. }
  65. else
  66. reset_cb
  67. fi
  68. }
  69. # uci function to parse 'service' and 'source' sections
  70. #
  71. parse_config()
  72. {
  73. local value opt section="${1}" options="enabled adb_dir adb_src adb_src_rset adb_src_cat"
  74. if [ "${section}" != "backup" ]
  75. then
  76. eval "adb_sources=\"${adb_sources} ${section}\""
  77. fi
  78. for opt in ${options}
  79. do
  80. config_get value "${section}" "${opt}"
  81. if [ -n "${value}" ]
  82. then
  83. eval "${opt}_${section}=\"${value}\""
  84. fi
  85. done
  86. }
  87. # load adblock config and start parsing functions
  88. #
  89. config_load adblock
  90. config_foreach parse_config service
  91. config_foreach parse_config source
  92. # get network basics
  93. #
  94. network_get_ipaddr adb_ipv4 "${adb_lanif}"
  95. network_get_ipaddr6 adb_ipv6 "${adb_lanif}"
  96. network_get_device adb_landev "${adb_lanif}"
  97. network_find_wan adb_wanif4
  98. network_find_wan6 adb_wanif6
  99. }
  100. # f_envcheck: check/set environment prerequisites
  101. #
  102. f_envcheck()
  103. {
  104. local check
  105. # check 'enabled' & 'version' config options
  106. #
  107. if [ -z "${adb_enabled}" ] || [ -z "${adb_cfgver}" ] || [ "${adb_cfgver%%.*}" != "${adb_mincfgver%%.*}" ]
  108. then
  109. rc=-1
  110. f_log "outdated adblock config (${adb_cfgver} vs. ${adb_mincfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
  111. f_exit
  112. elif [ "${adb_cfgver#*.}" != "${adb_mincfgver#*.}" ]
  113. then
  114. outdated_ok="true"
  115. fi
  116. if [ "${adb_enabled}" != "1" ]
  117. then
  118. rc=-10
  119. f_log "adblock is currently disabled, please set adb_enabled to '1' to use this service"
  120. f_exit
  121. fi
  122. # check opkg availability
  123. #
  124. adb_pkglist="$(opkg list-installed)"
  125. if [ $(($?)) -eq 255 ]
  126. then
  127. rc=-10
  128. f_log "adblock installation finished successfully, 'opkg' currently locked by package installer"
  129. f_exit
  130. elif [ -z "${adb_pkglist}" ]
  131. then
  132. rc=-1
  133. f_log "empty 'opkg' package list, please check your installation"
  134. f_exit
  135. fi
  136. adb_sysver="$(printf "${adb_pkglist}" | grep "^base-files -")"
  137. adb_sysver="${adb_sysver##*-}"
  138. # get lan ip addresses
  139. #
  140. if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
  141. then
  142. rc=-1
  143. f_log "no valid IPv4/IPv6 configuration found (${adb_lanif}), please set 'adb_lanif' manually"
  144. f_exit
  145. fi
  146. # check logical update interfaces (with default route)
  147. #
  148. if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
  149. then
  150. adb_wanif4="${adb_lanif}"
  151. fi
  152. # check AP mode
  153. #
  154. if [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
  155. then
  156. adb_nullipv4="${adb_ipv4}"
  157. adb_nullipv6="${adb_ipv6}"
  158. if [ -n "$(${adb_uci} -q get uhttpd.main.listen_http | grep -o ":80$")" ] ||
  159. [ -n "$(${adb_uci} -q get uhttpd.main.listen_https | grep -o ":443$")" ]
  160. then
  161. rc=-1
  162. f_log "AP mode detected, please set local LuCI instance to ports <> 80/443"
  163. f_exit
  164. else
  165. apmode_ok="true"
  166. fi
  167. else
  168. apmode_ok="false"
  169. check="$(${adb_uci} -q get bcp38.@bcp38[0].enabled)"
  170. if [ "${check}" = "1" ]
  171. then
  172. if [ -n "$(${adb_uci} -q get bcp38.@bcp38[0].match | grep -Fo "${adb_nullipv4%.*}")" ]
  173. then
  174. rc=-1
  175. f_log "please whitelist '${adb_nullipv4}' in your bcp38 configuration to use adblock"
  176. f_exit
  177. fi
  178. fi
  179. fi
  180. # check general package dependencies
  181. #
  182. f_depend "busybox -"
  183. f_depend "uci -"
  184. f_depend "uhttpd -"
  185. f_depend "iptables -"
  186. f_depend "kmod-ipt-nat -"
  187. f_depend "firewall -"
  188. f_depend "dnsmasq*"
  189. # check ipv6 related package dependencies
  190. #
  191. if [ -n "${adb_wanif6}" ]
  192. then
  193. f_depend "ip6tables -" "true"
  194. if [ "${package_ok}" = "false" ]
  195. then
  196. f_log "package 'ip6tables' not found, IPv6 support will be disabled"
  197. unset adb_wanif6
  198. else
  199. f_depend "kmod-ipt-nat6 -" "true"
  200. if [ "${package_ok}" = "false" ]
  201. then
  202. f_log "package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
  203. unset adb_wanif6
  204. fi
  205. fi
  206. fi
  207. # check uclient-fetch/wget dependencies
  208. #
  209. f_depend "uclient-fetch -" "true"
  210. if [ "${package_ok}" = "true" ]
  211. then
  212. f_depend "libustream-polarssl -" "true"
  213. if [ "${package_ok}" = "false" ]
  214. then
  215. f_depend "libustream-\(mbedtls\|openssl\|cyassl\) -" "true"
  216. if [ "${package_ok}" = "true" ]
  217. then
  218. adb_fetch="$(which uclient-fetch)"
  219. fetch_parm="-q"
  220. response_parm=
  221. fi
  222. fi
  223. fi
  224. if [ -z "${adb_fetch}" ]
  225. then
  226. f_depend "wget -" "true"
  227. if [ "${package_ok}" = "true" ]
  228. then
  229. adb_fetch="$(which /usr/bin/wget* | head -1)"
  230. fetch_parm="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=${adb_fetchttl} --connect-timeout=${adb_fetchttl} --read-timeout=${adb_fetchttl}"
  231. response_parm="--spider --server-response"
  232. fi
  233. if [ -z "${adb_fetch}" ]
  234. then
  235. rc=-1
  236. f_log "please install 'uclient-fetch' or 'wget' with ssl support to use adblock"
  237. f_exit
  238. fi
  239. fi
  240. # check ca-certificate package and set fetch parm accordingly
  241. #
  242. f_depend "ca-certificates -" "true"
  243. if [ "${package_ok}" = "false" ]
  244. then
  245. fetch_parm="${fetch_parm} --no-check-certificate"
  246. fi
  247. # start normal processing/logging
  248. #
  249. f_log "domain adblock processing started (${adb_scriptver}, ${adb_sysver}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  250. # log partially outdated config
  251. #
  252. if [ "${outdated_ok}" = "true" ]
  253. then
  254. f_log "partially outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
  255. fi
  256. # log ap mode
  257. #
  258. if [ "${apmode_ok}" = "true" ]
  259. then
  260. f_log "AP mode enabled"
  261. fi
  262. # set/log restricted mode
  263. #
  264. if [ "${adb_restricted}" = "1" ]
  265. then
  266. adb_uci="$(which true)"
  267. f_log "Restricted mode enabled"
  268. fi
  269. # check dns hideout directory
  270. #
  271. if [ -d "${adb_dnshidedir}" ]
  272. then
  273. mv_done="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec mv -f "{}" "${adb_dnsdir}" \;)"
  274. else
  275. mkdir -p -m 660 "${adb_dnshidedir}"
  276. fi
  277. # check adblock temp directory
  278. #
  279. adb_tmpfile="$(mktemp -tu)"
  280. adb_tmpdir="$(mktemp -p /tmp -d)"
  281. if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
  282. then
  283. f_space "${adb_tmpdir}"
  284. if [ "${space_ok}" = "false" ]
  285. then
  286. if [ $((av_space)) -le 2000 ]
  287. then
  288. rc=105
  289. f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)"
  290. f_exit
  291. else
  292. f_log "not enough free space to handle all block list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
  293. fi
  294. fi
  295. else
  296. rc=110
  297. f_log "temp directory not found"
  298. f_exit
  299. fi
  300. # check memory
  301. #
  302. mem_total="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc/meminfo")"
  303. mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo")"
  304. mem_swap="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc/meminfo")"
  305. if [ $((mem_total)) -le 64000 ] && [ $((mem_swap)) -eq 0 ]
  306. then
  307. mem_ok="false"
  308. f_log "not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
  309. else
  310. mem_ok="true"
  311. fi
  312. # check backup configuration
  313. #
  314. if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
  315. then
  316. f_space "${adb_dir_backup}"
  317. if [ "${space_ok}" = "false" ]
  318. then
  319. f_log "not enough free space in '${adb_dir_backup}'(avail. ${av_space} kb), backup/restore will be disabled"
  320. backup_ok="false"
  321. else
  322. f_log "backup/restore will be enabled"
  323. backup_ok="true"
  324. fi
  325. else
  326. backup_ok="false"
  327. f_log "backup/restore will be disabled"
  328. fi
  329. # set dnsmasq defaults
  330. #
  331. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  332. then
  333. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
  334. elif [ -n "${adb_wanif4}" ]
  335. then
  336. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
  337. else
  338. adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
  339. fi
  340. # check volatile iptables configuration
  341. #
  342. if [ -n "${adb_wanif4}" ]
  343. then
  344. if [ "${apmode_ok}" = "false" ]
  345. then
  346. if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
  347. then
  348. f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
  349. f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
  350. fi
  351. f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
  352. f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp-host-unreachable"
  353. f_firewall "IPv4" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
  354. f_firewall "IPv4" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp-host-unreachable"
  355. fi
  356. f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
  357. f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullportssl}"
  358. fi
  359. if [ -n "${adb_wanif6}" ]
  360. then
  361. if [ "${apmode_ok}" = "false" ]
  362. then
  363. if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
  364. then
  365. f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
  366. f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
  367. fi
  368. f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
  369. f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp6-addr-unreachable"
  370. f_firewall "IPv6" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
  371. f_firewall "IPv6" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp6-addr-unreachable"
  372. fi
  373. f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
  374. f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullportssl}"
  375. fi
  376. if [ "${firewall_ok}" = "true" ]
  377. then
  378. f_log "created volatile firewall rulesets"
  379. fi
  380. # check volatile uhttpd instance configuration
  381. #
  382. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  383. then
  384. f_uhttpd "adbIPv46_80" "1" "-p ${adb_ipv4}:${adb_nullport} -p [${adb_ipv6}]:${adb_nullport}"
  385. f_uhttpd "adbIPv46_443" "0" "-p ${adb_ipv4}:${adb_nullportssl} -p [${adb_ipv6}]:${adb_nullportssl}"
  386. elif [ -n "${adb_wanif4}" ]
  387. then
  388. f_uhttpd "adbIPv4_80" "1" "-p ${adb_ipv4}:${adb_nullport}"
  389. f_uhttpd "adbIPv4_443" "0" "-p ${adb_ipv4}:${adb_nullportssl}"
  390. else
  391. f_uhttpd "adbIPv6_80" "1" "-p [${adb_ipv6}]:${adb_nullport}"
  392. f_uhttpd "adbIPv6_443" "0" "-p [${adb_ipv6}]:${adb_nullportssl}"
  393. fi
  394. if [ "${uhttpd_ok}" = "true" ]
  395. then
  396. f_log "created volatile uhttpd instances"
  397. fi
  398. # check whitelist entries
  399. #
  400. if [ -s "${adb_whitelist}" ]
  401. then
  402. awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
  403. fi
  404. # remove temporary package list
  405. #
  406. unset adb_pkglist
  407. }
  408. # f_depend: check package dependencies
  409. #
  410. f_depend()
  411. {
  412. local check
  413. local package="${1}"
  414. local check_only="${2}"
  415. package_ok="true"
  416. check="$(printf "${adb_pkglist}" | grep "^${package}")"
  417. if [ "${check_only}" = "true" ] && [ -z "${check}" ]
  418. then
  419. package_ok="false"
  420. elif [ -z "${check}" ]
  421. then
  422. rc=-1
  423. f_log "package '${package}' not found"
  424. f_exit
  425. fi
  426. }
  427. # f_firewall: set iptables rules for ipv4/ipv6
  428. #
  429. f_firewall()
  430. {
  431. local ipt="iptables"
  432. local nullip="${adb_nullipv4}"
  433. local proto="${1}"
  434. local table="${2}"
  435. local chsrc="${3}"
  436. local chain="${4}"
  437. local chpos="${5}"
  438. local notes="adb-${6}"
  439. local rules="${7}"
  440. # select appropriate iptables executable for IPv6
  441. #
  442. if [ "${proto}" = "IPv6" ]
  443. then
  444. ipt="ip6tables"
  445. nullip="${adb_nullipv6}"
  446. fi
  447. # check whether iptables chain already exist
  448. #
  449. rc="$("${ipt}" -w -t "${table}" -nL "${chain}" >/dev/null 2>&1; printf ${?})"
  450. if [ $((rc)) -ne 0 ]
  451. then
  452. "${ipt}" -w -t "${table}" -N "${chain}"
  453. "${ipt}" -w -t "${table}" -A "${chain}" -m comment --comment "${notes}" -j RETURN
  454. if [ "${chain}" = "adb-dns" ]
  455. then
  456. "${ipt}" -w -t "${table}" -A "${chsrc}" -i "${adb_landev}+" -m comment --comment "${notes}" -j "${chain}"
  457. else
  458. "${ipt}" -w -t "${table}" -A "${chsrc}" -d "${nullip}" -m comment --comment "${notes}" -j "${chain}"
  459. fi
  460. rc=${?}
  461. if [ $((rc)) -ne 0 ]
  462. then
  463. f_log "failed to initialize volatile ${proto} firewall chain '${chain}'"
  464. f_exit
  465. fi
  466. fi
  467. # check whether iptables rule already exist
  468. #
  469. rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
  470. if [ $((rc)) -ne 0 ]
  471. then
  472. "${ipt}" -w -t "${table}" -I "${chain}" "${chpos}" -m comment --comment "${notes}" ${rules}
  473. rc=${?}
  474. if [ $((rc)) -eq 0 ]
  475. then
  476. firewall_ok="true"
  477. else
  478. f_log "failed to initialize volatile ${proto} firewall rule '${notes}'"
  479. f_exit
  480. fi
  481. fi
  482. }
  483. # f_uhttpd: start uhttpd instances
  484. #
  485. f_uhttpd()
  486. {
  487. local check
  488. local realm="${1}"
  489. local timeout="${2}"
  490. local ports="${3}"
  491. check="$(pgrep -f "uhttpd -h /www/adblock -N 25 -T ${timeout} -r ${realm}")"
  492. if [ -z "${check}" ]
  493. then
  494. uhttpd -h "/www/adblock" -N 25 -T "${timeout}" -r "${realm}" -k 0 -t 0 -R -D -S -E "/index.html" ${ports}
  495. rc=${?}
  496. if [ $((rc)) -eq 0 ]
  497. then
  498. uhttpd_ok="true"
  499. else
  500. f_log "failed to initialize volatile uhttpd instance (${realm})"
  501. f_exit
  502. fi
  503. fi
  504. }
  505. # f_space: check mount points/space requirements
  506. #
  507. f_space()
  508. {
  509. local mp="${1}"
  510. space_ok="true"
  511. if [ -d "${mp}" ]
  512. then
  513. av_space="$(df "${mp}" | tail -n1 | awk '{printf $4}')"
  514. if [ $((av_space)) -lt $((adb_minspace)) ]
  515. then
  516. space_ok="false"
  517. fi
  518. fi
  519. }
  520. # f_cntconfig: calculate counters in config
  521. #
  522. f_cntconfig()
  523. {
  524. local src_name
  525. local count=0
  526. for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
  527. do
  528. count="$(wc -l < "${src_name}")"
  529. src_name="${src_name##*.}"
  530. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  531. then
  532. count=$((count / 2))
  533. fi
  534. "${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}"
  535. adb_count=$((adb_count + count))
  536. done
  537. "${adb_uci}" -q set "adblock.global.adb_overall_count=${adb_count}"
  538. }
  539. # f_rmconfig: remove volatile config entries
  540. #
  541. f_rmconfig()
  542. {
  543. local opt
  544. local options="adb_src_timestamp adb_src_count"
  545. local section="${1}"
  546. "${adb_uci}" -q delete "adblock.global.adb_overall_count"
  547. "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
  548. "${adb_uci}" -q delete "adblock.global.adb_percentage"
  549. "${adb_uci}" -q delete "adblock.global.adb_lastrun"
  550. for opt in ${options}
  551. do
  552. "${adb_uci}" -q delete "adblock.${section}.${opt}"
  553. done
  554. }
  555. # f_rmdns: remove dns block lists and backups
  556. #
  557. f_rmdns()
  558. {
  559. rm_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
  560. if [ -n "${rm_dns}" ]
  561. then
  562. rm -rf "${adb_dnshidedir}"
  563. if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
  564. then
  565. rm -f "${adb_dir_backup}/${adb_dnsprefix}"*.gz
  566. fi
  567. /etc/init.d/dnsmasq restart
  568. fi
  569. }
  570. # f_rmuhttpd: remove uhttpd instances
  571. #
  572. f_rmuhttpd()
  573. {
  574. rm_uhttpd="$(pgrep -f "uhttpd -h /www/adblock")"
  575. if [ -n "${rm_uhttpd}" ]
  576. then
  577. for pid in ${rm_uhttpd}
  578. do
  579. kill -9 "${pid}"
  580. done
  581. fi
  582. }
  583. # f_rmfirewall: remove firewall rulsets
  584. #
  585. f_rmfirewall()
  586. {
  587. rm_fw="$(iptables -w -t nat -vnL | grep -Fo "adb-")"
  588. if [ -n "${rm_fw}" ]
  589. then
  590. iptables-save | grep -Fv -- "adb-" | iptables-restore
  591. if [ -n "$(lsmod | grep -Fo "ip6table_nat")" ]
  592. then
  593. ip6tables-save | grep -Fv -- "adb-" | ip6tables-restore
  594. fi
  595. fi
  596. }
  597. # f_log: log messages to stdout and syslog
  598. #
  599. f_log()
  600. {
  601. local log_parm
  602. local log_msg="${1}"
  603. local class="info "
  604. if [ $((rc)) -gt 0 ]
  605. then
  606. class="error"
  607. elif [ $((rc)) -lt 0 ]
  608. then
  609. class="warn "
  610. fi
  611. if [ -t 1 ]
  612. then
  613. log_parm="-s"
  614. fi
  615. if [ -n "${log_msg}" ] && ([ $((adb_loglevel)) -gt 0 ] || [ "${class}" != "info " ])
  616. then
  617. logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
  618. fi
  619. }
  620. # f_statistics: adblock runtime statistics
  621. f_statistics()
  622. {
  623. local ipv4_blk=0 ipv4_all=0 ipv4_pct=0
  624. local ipv6_blk=0 ipv6_all=0 ipv6_pct=0
  625. if [ -n "${adb_wanif4}" ]
  626. then
  627. ipv4_blk="$(iptables -t nat -vxnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
  628. ipv4_all="$(iptables -t nat -vxnL PREROUTING | awk '$3 ~ /^(delegate_prerouting|prerouting_rule)$/ {sum += $1} END {printf sum}')"
  629. if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ]
  630. then
  631. ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')"
  632. elif [ $((ipv4_all)) -lt $((ipv4_blk)) ]
  633. then
  634. iptables -t nat -Z adb-nat
  635. fi
  636. fi
  637. if [ -n "${adb_wanif6}" ]
  638. then
  639. ipv6_blk="$(ip6tables -t nat -vxnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
  640. ipv6_all="$(ip6tables -t nat -vxnL PREROUTING | awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')"
  641. if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ]
  642. then
  643. ipv6_pct="$(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')"
  644. elif [ $((ipv6_all)) -lt $((ipv6_blk)) ]
  645. then
  646. ip6tables -t nat -Z adb-nat
  647. fi
  648. fi
  649. "${adb_uci}" -q set "adblock.global.adb_percentage=${ipv4_pct}%/${ipv6_pct}%"
  650. f_log "firewall statistics (IPv4/IPv6): ${ipv4_pct}%/${ipv6_pct}% of all packets in prerouting chain are ad related & blocked"
  651. }
  652. # f_exit: delete temporary files, generate statistics and exit
  653. #
  654. f_exit()
  655. {
  656. local lastrun="$(date "+%d.%m.%Y %H:%M:%S")"
  657. if [ "${adb_restricted}" = "1" ]
  658. then
  659. adb_uci="$(which true)"
  660. fi
  661. # delete temp files & directories
  662. #
  663. rm -f "${adb_tmpfile}"
  664. rm -rf "${adb_tmpdir}"
  665. # tidy up on error
  666. #
  667. if [ $((rc)) -lt 0 ] || [ $((rc)) -gt 0 ]
  668. then
  669. f_rmdns
  670. f_rmuhttpd
  671. f_rmfirewall
  672. config_foreach f_rmconfig source
  673. if [ $((rc)) -eq -1 ]
  674. then
  675. "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun} => runtime error, please check the log!"
  676. fi
  677. fi
  678. # final log message and iptables statistics
  679. #
  680. if [ $((rc)) -eq 0 ]
  681. then
  682. f_statistics
  683. "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun}"
  684. f_log "domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
  685. elif [ $((rc)) -gt 0 ]
  686. then
  687. f_log "domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
  688. else
  689. rc=0
  690. fi
  691. if [ -n "$("${adb_uci}" -q changes adblock)" ]
  692. then
  693. "${adb_uci}" -q commit "adblock"
  694. fi
  695. rm -f "${adb_pidfile}"
  696. exit ${rc}
  697. }