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.

751 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.2"
  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 --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 "adbIPv46_80" "1" "-p ${adb_ipv4}:${adb_nullport} -p [${adb_ipv6}]:${adb_nullport}"
  383. f_uhttpd "adbIPv46_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 adb_pkglist
  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 "${adb_pkglist}" | 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 | grep -Fv -- "adb-" | iptables-restore
  589. if [ -n "$(lsmod | grep -Fo "ip6table_nat")" ]
  590. then
  591. ip6tables-save | grep -Fv -- "adb-" | ip6tables-restore
  592. fi
  593. fi
  594. }
  595. # f_log: log messages to stdout and syslog
  596. #
  597. f_log()
  598. {
  599. local log_parm
  600. local log_msg="${1}"
  601. local class="info "
  602. if [ $((rc)) -gt 0 ]
  603. then
  604. class="error"
  605. elif [ $((rc)) -lt 0 ]
  606. then
  607. class="warn "
  608. fi
  609. if [ -t 1 ]
  610. then
  611. log_parm="-s"
  612. fi
  613. if [ -n "${log_msg}" ] && ([ $((adb_loglevel)) -gt 0 ] || [ "${class}" != "info " ])
  614. then
  615. logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
  616. fi
  617. }
  618. # f_statistics: adblock runtime statistics
  619. f_statistics()
  620. {
  621. local ipv4_blk=0 ipv4_all=0 ipv4_pct=0
  622. local ipv6_blk=0 ipv6_all=0 ipv6_pct=0
  623. if [ -n "${adb_wanif4}" ]
  624. then
  625. ipv4_blk="$(iptables -t nat -vxnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
  626. ipv4_all="$(iptables -t nat -vxnL PREROUTING | awk '$3 ~ /^(delegate_prerouting|prerouting_rule)$/ {sum += $1} END {printf sum}')"
  627. if [ $((ipv4_all)) -gt 0 ] && [ $((ipv4_blk)) -gt 0 ] && [ $((ipv4_all)) -gt $((ipv4_blk)) ]
  628. then
  629. ipv4_pct="$(printf "${ipv4_blk}" | awk -v all="${ipv4_all}" '{printf( "%5.2f\n",$1/all*100)}')"
  630. elif [ $((ipv4_all)) -lt $((ipv4_blk)) ]
  631. then
  632. iptables -t nat -Z adb-nat
  633. fi
  634. fi
  635. if [ -n "${adb_wanif6}" ]
  636. then
  637. ipv6_blk="$(ip6tables -t nat -vxnL adb-nat | awk '$3 ~ /^DNAT$/ {sum += $1} END {printf sum}')"
  638. ipv6_all="$(ip6tables -t nat -vxnL PREROUTING | awk '$3 ~ /^(adb-nat|DNAT)$/ {sum += $1} END {printf sum}')"
  639. if [ $((ipv6_all)) -gt 0 ] && [ $((ipv6_blk)) -gt 0 ] && [ $((ipv6_all)) -gt $((ipv6_blk)) ]
  640. then
  641. ipv6_pct="$(printf "${ipv6_blk}" | awk -v all="${ipv6_all}" '{printf( "%5.2f\n",$1/all*100)}')"
  642. elif [ $((ipv6_all)) -lt $((ipv6_blk)) ]
  643. then
  644. ip6tables -t nat -Z adb-nat
  645. fi
  646. fi
  647. "${adb_uci}" -q set "adblock.global.adb_percentage=${ipv4_pct}%/${ipv6_pct}%"
  648. f_log "firewall statistics (IPv4/IPv6): ${ipv4_pct}%/${ipv6_pct}% of all packets in prerouting chain are ad related & blocked"
  649. }
  650. # f_exit: delete temporary files, generate statistics and exit
  651. #
  652. f_exit()
  653. {
  654. local lastrun="$(date "+%d.%m.%Y %H:%M:%S")"
  655. if [ "${adb_restricted}" = "1" ]
  656. then
  657. adb_uci="$(which true)"
  658. fi
  659. # delete temp files & directories
  660. #
  661. rm -f "${adb_tmpfile}"
  662. rm -rf "${adb_tmpdir}"
  663. # tidy up on error
  664. #
  665. if [ $((rc)) -lt 0 ] || [ $((rc)) -gt 0 ]
  666. then
  667. f_rmdns
  668. f_rmuhttpd
  669. f_rmfirewall
  670. config_foreach f_rmconfig source
  671. if [ $((rc)) -eq -1 ]
  672. then
  673. "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun} => runtime error, please check the log!"
  674. fi
  675. fi
  676. # final log message and iptables statistics
  677. #
  678. if [ $((rc)) -eq 0 ]
  679. then
  680. f_statistics
  681. "${adb_uci}" -q set "adblock.global.adb_lastrun=${lastrun}"
  682. f_log "domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
  683. elif [ $((rc)) -gt 0 ]
  684. then
  685. f_log "domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
  686. else
  687. rc=0
  688. fi
  689. if [ -n "$("${adb_uci}" -q changes adblock)" ]
  690. then
  691. "${adb_uci}" -q commit "adblock"
  692. fi
  693. rm -f "${adb_pidfile}"
  694. exit ${rc}
  695. }