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