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.

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