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_hotplugif=""
  9. adb_lanif="lan"
  10. adb_nullport="65534"
  11. adb_nullportssl="65535"
  12. adb_nullipv4="198.18.0.1"
  13. adb_nullipv6="::ffff:c612:0001"
  14. adb_whitelist="/etc/adblock/adblock.whitelist"
  15. adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\"^\"\$1\"\\\|[.]\"\$1)}"
  16. adb_dnsdir="/tmp/dnsmasq.d"
  17. adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
  18. adb_dnsprefix="adb_list"
  19. adb_count=0
  20. adb_minspace=12000
  21. adb_forcedns=1
  22. adb_fetchttl=5
  23. adb_restricted=0
  24. adb_uci="$(which uci)"
  25. # f_envload: load adblock environment
  26. #
  27. f_envload()
  28. {
  29. # source in system function library
  30. #
  31. if [ -r "/lib/functions.sh" ]
  32. then
  33. . "/lib/functions.sh"
  34. else
  35. rc=-10
  36. f_log "system function library not found, please check your installation"
  37. f_exit
  38. fi
  39. # source in system network library
  40. #
  41. if [ -r "/lib/functions/network.sh" ]
  42. then
  43. . "/lib/functions/network.sh"
  44. else
  45. rc=-10
  46. f_log "system network library not found, please check your installation"
  47. f_exit
  48. fi
  49. # check opkg availability
  50. #
  51. if [ -f "/var/lock/opkg.lock" ]
  52. then
  53. rc=-10
  54. f_log "adblock installation finished successfully, 'opkg' currently locked by package installer"
  55. f_exit
  56. fi
  57. # uci function to parse global section by callback
  58. #
  59. config_cb()
  60. {
  61. local type="${1}"
  62. if [ "${type}" = "adblock" ]
  63. then
  64. option_cb()
  65. {
  66. local option="${1}"
  67. local value="${2}"
  68. eval "${option}=\"${value}\""
  69. }
  70. else
  71. reset_cb
  72. fi
  73. }
  74. # uci function to parse 'service' and 'source' sections
  75. #
  76. parse_config()
  77. {
  78. local value opt section="${1}" options="enabled adb_dir adb_src adb_src_rset adb_src_cat"
  79. if [ "${section}" != "backup" ]
  80. then
  81. eval "adb_sources=\"${adb_sources} ${section}\""
  82. fi
  83. for opt in ${options}
  84. do
  85. config_get value "${section}" "${opt}"
  86. if [ -n "${value}" ]
  87. then
  88. eval "${opt}_${section}=\"${value}\""
  89. fi
  90. done
  91. }
  92. # load adblock config and start parsing functions
  93. #
  94. config_load adblock
  95. config_foreach parse_config service
  96. config_foreach parse_config source
  97. # get network basics
  98. #
  99. network_get_ipaddr adb_ipv4 "${adb_lanif}"
  100. network_get_ipaddr6 adb_ipv6 "${adb_lanif}"
  101. network_get_device adb_landev "${adb_lanif}"
  102. network_find_wan adb_wanif4
  103. network_find_wan6 adb_wanif6
  104. }
  105. # f_envcheck: check/set environment prerequisites
  106. #
  107. f_envcheck()
  108. {
  109. local check
  110. # check 'enabled' & 'version' config options
  111. #
  112. if [ -z "${adb_enabled}" ] || [ -z "${adb_cfgver}" ] || [ "${adb_cfgver%%.*}" != "${adb_mincfgver%%.*}" ]
  113. then
  114. rc=-1
  115. f_log "outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
  116. f_exit
  117. elif [ "${adb_cfgver#*.}" != "${adb_mincfgver#*.}" ]
  118. then
  119. outdated_ok="true"
  120. fi
  121. if [ "${adb_enabled}" != "1" ]
  122. then
  123. rc=-10
  124. f_log "adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service"
  125. f_exit
  126. fi
  127. # get list with all installed packages
  128. #
  129. pkg_list="$(opkg list-installed)"
  130. if [ -z "${pkg_list}" ]
  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 "${pkg_list}" | 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 --timeout=${adb_fetchttl}"
  220. response_parm="--spider"
  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 wget)"
  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. else
  233. rc=-1
  234. f_log "please install 'uclient-fetch' or 'wget' with ssl support to use adblock"
  235. f_exit
  236. fi
  237. fi
  238. # check ca-certificate package and set fetch parm accordingly
  239. #
  240. f_depend "ca-certificates -" "true"
  241. if [ "${package_ok}" = "false" ]
  242. then
  243. fetch_parm="${fetch_parm} --no-check-certificate"
  244. fi
  245. # start normal processing/logging
  246. #
  247. f_log "domain adblock processing started (${adb_scriptver}, ${adb_sysver}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  248. # log partially outdated config
  249. #
  250. if [ "${outdated_ok}" = "true" ]
  251. then
  252. f_log "partially outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
  253. fi
  254. # log ap mode
  255. #
  256. if [ "${apmode_ok}" = "true" ]
  257. then
  258. f_log "AP mode enabled"
  259. fi
  260. # set/log restricted mode
  261. #
  262. if [ "${adb_restricted}" = "1" ]
  263. then
  264. adb_uci="$(which true)"
  265. f_log "Restricted mode enabled"
  266. fi
  267. # check dns hideout directory
  268. #
  269. if [ -d "${adb_dnshidedir}" ]
  270. then
  271. mv_done="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec mv -f "{}" "${adb_dnsdir}" \;)"
  272. else
  273. mkdir -p -m 660 "${adb_dnshidedir}"
  274. fi
  275. # check adblock temp directory
  276. #
  277. adb_tmpfile="$(mktemp -tu)"
  278. adb_tmpdir="$(mktemp -p /tmp -d)"
  279. if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
  280. then
  281. f_space "${adb_tmpdir}"
  282. if [ "${space_ok}" = "false" ]
  283. then
  284. if [ $((av_space)) -le 2000 ]
  285. then
  286. rc=105
  287. f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)"
  288. f_exit
  289. else
  290. f_log "not enough free space to handle all block list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
  291. fi
  292. fi
  293. else
  294. rc=110
  295. f_log "temp directory not found"
  296. f_exit
  297. fi
  298. # check memory
  299. #
  300. mem_total="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc/meminfo")"
  301. mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo")"
  302. mem_swap="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc/meminfo")"
  303. if [ $((mem_total)) -le 64000 ] && [ $((mem_swap)) -eq 0 ]
  304. then
  305. mem_ok="false"
  306. f_log "not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
  307. else
  308. mem_ok="true"
  309. fi
  310. # check backup configuration
  311. #
  312. if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
  313. then
  314. f_space "${adb_dir_backup}"
  315. if [ "${space_ok}" = "false" ]
  316. then
  317. f_log "not enough free space in '${adb_dir_backup}'(avail. ${av_space} kb), backup/restore will be disabled"
  318. backup_ok="false"
  319. else
  320. f_log "backup/restore will be enabled"
  321. backup_ok="true"
  322. fi
  323. else
  324. backup_ok="false"
  325. f_log "backup/restore will be disabled"
  326. fi
  327. # set dnsmasq defaults
  328. #
  329. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  330. then
  331. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
  332. elif [ -n "${adb_wanif4}" ]
  333. then
  334. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
  335. else
  336. adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
  337. fi
  338. # check volatile iptables configuration
  339. #
  340. if [ -n "${adb_wanif4}" ]
  341. then
  342. if [ "${apmode_ok}" = "false" ]
  343. then
  344. if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
  345. then
  346. f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
  347. f_firewall "IPv4" "nat" "prerouting_rule" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
  348. fi
  349. f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
  350. f_firewall "IPv4" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp-host-unreachable"
  351. f_firewall "IPv4" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
  352. f_firewall "IPv4" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp-host-unreachable"
  353. fi
  354. f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
  355. f_firewall "IPv4" "nat" "prerouting_rule" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullportssl}"
  356. fi
  357. if [ -n "${adb_wanif6}" ]
  358. then
  359. if [ "${apmode_ok}" = "false" ]
  360. then
  361. if [ "${adb_forcedns}" = "1" ] && [ -n "${adb_landev}" ]
  362. then
  363. f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "1" "dns" "-p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
  364. f_firewall "IPv6" "nat" "PREROUTING" "adb-dns" "2" "dns" "-p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
  365. fi
  366. f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "1" "fwd" "-p tcp -j REJECT --reject-with tcp-reset"
  367. f_firewall "IPv6" "filter" "forwarding_rule" "adb-fwd" "2" "fwd" "-j REJECT --reject-with icmp6-addr-unreachable"
  368. f_firewall "IPv6" "filter" "output_rule" "adb-out" "1" "out" "-p tcp -j REJECT --reject-with tcp-reset"
  369. f_firewall "IPv6" "filter" "output_rule" "adb-out" "2" "out" "-j REJECT --reject-with icmp6-addr-unreachable"
  370. fi
  371. f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "1" "nat" "-p tcp --dport 80 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
  372. f_firewall "IPv6" "nat" "PREROUTING" "adb-nat" "2" "nat" "-p tcp --dport 443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullportssl}"
  373. fi
  374. if [ "${firewall_ok}" = "true" ]
  375. then
  376. f_log "created volatile firewall rulesets"
  377. fi
  378. # check volatile uhttpd instance configuration
  379. #
  380. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  381. then
  382. f_uhttpd "adbIPv4+6_80" "1" "-p ${adb_ipv4}:${adb_nullport} -p [${adb_ipv6}]:${adb_nullport}"
  383. f_uhttpd "adbIPv4+6_443" "0" "-p ${adb_ipv4}:${adb_nullportssl} -p [${adb_ipv6}]:${adb_nullportssl}"
  384. elif [ -n "${adb_wanif4}" ]
  385. then
  386. f_uhttpd "adbIPv4_80" "1" "-p ${adb_ipv4}:${adb_nullport}"
  387. f_uhttpd "adbIPv4_443" "0" "-p ${adb_ipv4}:${adb_nullportssl}"
  388. else
  389. f_uhttpd "adbIPv6_80" "1" "-p [${adb_ipv6}]:${adb_nullport}"
  390. f_uhttpd "adbIPv6_443" "0" "-p [${adb_ipv6}]:${adb_nullportssl}"
  391. fi
  392. if [ "${uhttpd_ok}" = "true" ]
  393. then
  394. f_log "created volatile uhttpd instances"
  395. fi
  396. # check whitelist entries
  397. #
  398. if [ -s "${adb_whitelist}" ]
  399. then
  400. awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
  401. fi
  402. # remove temporary package list
  403. #
  404. unset pkg_list
  405. }
  406. # f_depend: check package dependencies
  407. #
  408. f_depend()
  409. {
  410. local check
  411. local package="${1}"
  412. local check_only="${2}"
  413. package_ok="true"
  414. check="$(printf "${pkg_list}" | grep "^${package}")"
  415. if [ "${check_only}" = "true" ] && [ -z "${check}" ]
  416. then
  417. package_ok="false"
  418. elif [ -z "${check}" ]
  419. then
  420. rc=-1
  421. f_log "package '${package}' not found"
  422. f_exit
  423. fi
  424. }
  425. # f_firewall: set iptables rules for ipv4/ipv6
  426. #
  427. f_firewall()
  428. {
  429. local ipt="iptables"
  430. local nullip="${adb_nullipv4}"
  431. local proto="${1}"
  432. local table="${2}"
  433. local chsrc="${3}"
  434. local chain="${4}"
  435. local chpos="${5}"
  436. local notes="adb-${6}"
  437. local rules="${7}"
  438. # select appropriate iptables executable for IPv6
  439. #
  440. if [ "${proto}" = "IPv6" ]
  441. then
  442. ipt="ip6tables"
  443. nullip="${adb_nullipv6}"
  444. fi
  445. # check whether iptables chain already exist
  446. #
  447. rc="$("${ipt}" -w -t "${table}" -nL "${chain}" >/dev/null 2>&1; printf ${?})"
  448. if [ $((rc)) -ne 0 ]
  449. then
  450. "${ipt}" -w -t "${table}" -N "${chain}"
  451. "${ipt}" -w -t "${table}" -A "${chain}" -m comment --comment "${notes}" -j RETURN
  452. if [ "${chain}" = "adb-dns" ]
  453. then
  454. "${ipt}" -w -t "${table}" -A "${chsrc}" -i "${adb_landev}+" -m comment --comment "${notes}" -j "${chain}"
  455. else
  456. "${ipt}" -w -t "${table}" -A "${chsrc}" -d "${nullip}" -m comment --comment "${notes}" -j "${chain}"
  457. fi
  458. rc=${?}
  459. if [ $((rc)) -ne 0 ]
  460. then
  461. f_log "failed to initialize volatile ${proto} firewall chain '${chain}'"
  462. f_exit
  463. fi
  464. fi
  465. # check whether iptables rule already exist
  466. #
  467. rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
  468. if [ $((rc)) -ne 0 ]
  469. then
  470. "${ipt}" -w -t "${table}" -I "${chain}" "${chpos}" -m comment --comment "${notes}" ${rules}
  471. rc=${?}
  472. if [ $((rc)) -eq 0 ]
  473. then
  474. firewall_ok="true"
  475. else
  476. f_log "failed to initialize volatile ${proto} firewall rule '${notes}'"
  477. f_exit
  478. fi
  479. fi
  480. }
  481. # f_uhttpd: start uhttpd instances
  482. #
  483. f_uhttpd()
  484. {
  485. local check
  486. local realm="${1}"
  487. local timeout="${2}"
  488. local ports="${3}"
  489. check="$(pgrep -f "uhttpd -h /www/adblock -N 25 -T ${timeout} -r ${realm}")"
  490. if [ -z "${check}" ]
  491. then
  492. uhttpd -h "/www/adblock" -N 25 -T "${timeout}" -r "${realm}" -k 0 -t 0 -R -D -S -E "/index.html" ${ports}
  493. rc=${?}
  494. if [ $((rc)) -eq 0 ]
  495. then
  496. uhttpd_ok="true"
  497. else
  498. f_log "failed to initialize volatile uhttpd instance (${realm})"
  499. f_exit
  500. fi
  501. fi
  502. }
  503. # f_space: check mount points/space requirements
  504. #
  505. f_space()
  506. {
  507. local mp="${1}"
  508. space_ok="true"
  509. if [ -d "${mp}" ]
  510. then
  511. av_space="$(df "${mp}" | tail -n1 | awk '{printf $4}')"
  512. if [ $((av_space)) -lt $((adb_minspace)) ]
  513. then
  514. space_ok="false"
  515. fi
  516. fi
  517. }
  518. # f_cntconfig: calculate counters in config
  519. #
  520. f_cntconfig()
  521. {
  522. local src_name
  523. local count=0
  524. for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
  525. do
  526. count="$(wc -l < "${src_name}")"
  527. src_name="${src_name##*.}"
  528. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  529. then
  530. count=$((count / 2))
  531. fi
  532. "${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}"
  533. adb_count=$((adb_count + count))
  534. done
  535. "${adb_uci}" -q set "adblock.global.adb_overall_count=${adb_count}"
  536. }
  537. # f_rmconfig: remove volatile config entries
  538. #
  539. f_rmconfig()
  540. {
  541. local opt
  542. local options="adb_src_timestamp adb_src_count"
  543. local section="${1}"
  544. "${adb_uci}" -q delete "adblock.global.adb_overall_count"
  545. "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
  546. "${adb_uci}" -q delete "adblock.global.adb_percentage"
  547. "${adb_uci}" -q delete "adblock.global.adb_lastrun"
  548. for opt in ${options}
  549. do
  550. "${adb_uci}" -q delete "adblock.${section}.${opt}"
  551. done
  552. }
  553. # f_rmdns: remove dns block lists and backups
  554. #
  555. f_rmdns()
  556. {
  557. rm_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec rm -f "{}" \;)"
  558. if [ -n "${rm_dns}" ]
  559. then
  560. rm -rf "${adb_dnshidedir}"
  561. if [ "${enabled_backup}" = "1" ] && [ -d "${adb_dir_backup}" ]
  562. then
  563. rm -f "${adb_dir_backup}/${adb_dnsprefix}"*.gz
  564. fi
  565. /etc/init.d/dnsmasq restart
  566. fi
  567. }
  568. # f_rmuhttpd: remove uhttpd instances
  569. #
  570. f_rmuhttpd()
  571. {
  572. rm_uhttpd="$(pgrep -f "uhttpd -h /www/adblock")"
  573. if [ -n "${rm_uhttpd}" ]
  574. then
  575. for pid in ${rm_uhttpd}
  576. do
  577. kill -9 "${pid}"
  578. done
  579. fi
  580. }
  581. # f_rmfirewall: remove firewall rulsets
  582. #
  583. f_rmfirewall()
  584. {
  585. rm_fw="$(iptables -w -t nat -vnL | grep -Fo "adb-")"
  586. if [ -n "${rm_fw}" ]
  587. then
  588. iptables-save -t nat | grep -Fv -- "adb-" | iptables-restore
  589. iptables-save -t filter | grep -Fv -- "adb-" | iptables-restore
  590. if [ -n "$(lsmod | grep -Fo "ip6table_nat")" ]
  591. then
  592. ip6tables-save -t nat | grep -Fv -- "adb-" | ip6tables-restore
  593. ip6tables-save -t filter | 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. # check for terminal session
  605. #
  606. if [ -t 1 ]
  607. then
  608. log_parm="-s"
  609. fi
  610. # log to different output devices and set log class accordingly
  611. #
  612. if [ -n "${log_msg}" ]
  613. then
  614. if [ $((rc)) -gt 0 ]
  615. then
  616. class="error"
  617. fi
  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. "${adb_uci}" -q commit "adblock"
  693. rm -f "${adb_pidfile}"
  694. exit ${rc}
  695. }