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.

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