Bump version to v1.5 and start work on adding 4.19 kernel suppot
[librecmc/librecmc.git] / package / network / config / adblock / files / adblock.sh
1 #!/bin/sh
2 # dns based ad/abuse domain blocking
3 # written by Dirk Brenken (dev@brenken.org)
4
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8
9 # set initial defaults
10 #
11 LC_ALL=C
12 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
13 adb_ver="3.1.1"
14 adb_sysver="unknown"
15 adb_enabled=0
16 adb_debug=0
17 adb_backup_mode=0
18 adb_whitelist_mode=0
19 adb_forcesrt=0
20 adb_forcedns=0
21 adb_triggerdelay=0
22 adb_backup=0
23 adb_backupdir="/mnt"
24 adb_fetch="/usr/bin/wget"
25 adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
26 adb_dns="dnsmasq"
27 adb_dnsprefix="adb_list"
28 adb_dnsfile="${adb_dnsprefix}.overall"
29 adb_whitelist="/etc/adblock/adblock.whitelist"
30 adb_rtfile="/tmp/adb_runtime.json"
31 adb_hashsum="$(command -v sha256sum)"
32 adb_action="${1:-"start"}"
33 adb_cnt=0
34 adb_rc=0
35
36 # f_envload: load adblock environment
37 #
38 f_envload()
39 {
40     local dns_up sys_call sys_desc sys_model sys_ver cnt=0
41
42     # get system information
43     #
44     sys_call="$(ubus -S call system board 2>/dev/null)"
45     if [ -n "${sys_call}" ]
46     then
47         sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
48         sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
49         sys_ver="$(cat /etc/turris-version 2>/dev/null)"
50         if [ -n "${sys_ver}" ]
51         then
52             sys_desc="${sys_desc}/${sys_ver}"
53         fi
54         adb_sysver="${sys_model}, ${sys_desc}"
55     fi
56
57     # source in system libraries
58     #
59     if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
60     then
61         . "/lib/functions.sh"
62         . "/usr/share/libubox/jshn.sh"
63     else
64         f_log "error" "system libraries not found"
65     fi
66
67     # parse 'global' and 'extra' section by callback
68     #
69     config_cb()
70     {
71         local type="${1}"
72         if [ "${type}" = "adblock" ]
73         then
74             option_cb()
75             {
76                 local option="${1}"
77                 local value="${2}"
78                 eval "${option}=\"${value}\""
79             }
80         else
81             reset_cb
82         fi
83     }
84
85     # parse 'source' typed sections
86     #
87     parse_config()
88     {
89         local value opt section="${1}" options="enabled adb_src adb_src_rset adb_src_cat"
90         eval "adb_sources=\"${adb_sources} ${section}\""
91         for opt in ${options}
92         do
93             config_get value "${section}" "${opt}"
94             if [ -n "${value}" ]
95             then
96                 eval "${opt}_${section}=\"${value}\""
97             fi
98         done
99     }
100
101     # load adblock config
102     #
103     config_load adblock
104     config_foreach parse_config source
105
106     # set/check dns backend environment
107     #
108     case "${adb_dns}" in
109         dnsmasq)
110             adb_dnsuser="${adb_dnsuser:-"dnsmasq"}"
111             adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
112             adb_dnsformat="awk '{print \"local=/\"\$0\"/\"}'"
113             if [ ${adb_whitelist_mode} -eq 1 ]
114             then
115                 adb_dnsformat="awk '{print \"local=/\"\$0\"/#\"}'"
116                 adb_dnsblock="local=/#/"
117             fi
118             ;;
119         unbound)
120             adb_dnsuser="${adb_dnsuser:-"unbound"}"
121             adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}"
122             adb_dnsformat="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
123             if [ ${adb_whitelist_mode} -eq 1 ]
124             then
125                 adb_dnsformat="awk '{print \"local-zone: \042\"\$0\"\042 transparent\"}'"
126                 adb_dnsblock="local-zone: \".\" static"
127             fi
128             ;;
129         named)
130             adb_dnsuser="${adb_dnsuser:-"bind"}"
131             adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}"
132             adb_dnsheader="\$TTL 2h"$'\n'"@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)"$'\n'"  IN NS localhost."
133             adb_dnsformat="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
134             if [ ${adb_whitelist_mode} -eq 1 ]
135             then
136                 adb_dnsformat="awk '{print \"\"\$0\" CNAME rpz-passthru.\n*.\"\$0\" CNAME rpz-passthru.\"}'"
137                 adb_dnsblock="* CNAME ."
138             fi
139             ;;
140         kresd)
141             adb_dnsuser="${adb_dnsuser:-"root"}"
142             adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}"
143             adb_dnsheader="\$TTL 2h"$'\n'"@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)"$'\n'"  IN NS  localhost."
144             adb_dnsformat="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
145             if [ ${adb_whitelist_mode} -eq 1 ]
146             then
147                 adb_dnsformat="awk '{print \"\"\$0\" CNAME rpz-passthru.\n*.\"\$0\" CNAME rpz-passthru.\"}'"
148                 adb_dnsblock="* CNAME ."
149             fi
150             ;;
151         dnscrypt-proxy)
152             adb_dnsuser="${adb_dnsuser:-"nobody"}"
153             adb_dnsdir="${adb_dnsdir:-"/tmp"}"
154             adb_dnsformat="awk '{print \$0}'"
155             ;;
156     esac
157
158     # check adblock status
159     #
160     if [ ${adb_enabled} -eq 0 ]
161     then
162         if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
163         then
164             f_rmdns
165             f_dnsrestart
166         fi
167         f_extconf
168         f_jsnupdate
169         f_log "info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
170         exit 0
171     fi
172
173     if [ -d "${adb_dnsdir}" ] && [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ]
174     then
175         > "${adb_dnsdir}/${adb_dnsfile}"
176     fi
177
178     case "${adb_action}" in
179         start|restart|reload)
180             > "${adb_rtfile}"
181             if [ "${adb_action}" = "start" ] && [ "${adb_trigger}" = "timed" ]
182             then
183                 sleep ${adb_triggerdelay}
184             fi
185         ;;
186     esac
187
188     while [ ${cnt} -le 30 ]
189     do
190         dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running" 2>/dev/null)"
191         if [ "${dns_up}" = "true" ]
192         then
193             break
194         fi
195         sleep 1
196         cnt=$((cnt+1))
197     done
198
199     if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
200     then
201         f_log "error" "'${adb_dns}' not running, DNS backend not found"
202     fi
203 }
204
205 # f_envcheck: check/set environment prerequisites
206 #
207 f_envcheck()
208 {
209     local ssl_lib
210
211     # check external uci config files
212     #
213     f_extconf
214
215     # check fetch utility
216     #
217     ssl_lib="-"
218     if [ -x "${adb_fetch}" ]
219     then
220         if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
221         then
222             adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
223         elif [ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ] ||
224             ([ "$(readlink -fn "/bin/wget")" = "/bin/busybox" ] && [ "$(readlink -fn "${adb_fetch}")" != "/usr/bin/wget" ])
225         then
226             adb_fetch="/bin/busybox"
227             adb_fetchparm="-q -O"
228         else
229             ssl_lib="built-in"
230         fi
231     fi
232     if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin/wget")" = "/bin/uclient-fetch" ]
233     then
234         adb_fetch="/bin/uclient-fetch"
235         if [ -f "/lib/libustream-ssl.so" ]
236         then
237             adb_fetchparm="-q --timeout=10 --no-check-certificate -O"
238             ssl_lib="libustream-ssl"
239         else
240             adb_fetchparm="-q --timeout=10 -O"
241         fi
242     fi
243     if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
244     then
245         f_log "error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
246     fi
247     adb_fetchinfo="${adb_fetch##*/} (${ssl_lib})"
248
249     # check hashsum utility
250     #
251     if [ ! -x "${adb_hashsum}" ]
252     then
253         adb_hashsum="$(command -v md5sum)"
254     fi
255
256     # initialize temp files and directories
257     #
258     adb_tmpload="$(mktemp -tu)"
259     adb_tmpfile="$(mktemp -tu)"
260     adb_tmpdir="$(mktemp -p /tmp -d)"
261     > "${adb_tmpdir}/tmp.whitelist"
262 }
263
264 # f_extconf: set external config options
265 #
266 f_extconf()
267 {
268     # kresd related options
269     #
270     if [ "${adb_dns}" = "kresd" ]
271     then
272         if [ ${adb_enabled} -eq 1 ] && [ -z "$(uci -q get resolver.kresd.rpz_file | grep -Fo "${adb_dnsdir}/${adb_dnsfile}")" ]
273         then
274             uci -q add_list resolver.kresd.rpz_file="${adb_dnsdir}/${adb_dnsfile}"
275         elif [ ${adb_enabled} -eq 0 ] && [ -n "$(uci -q get resolver.kresd.rpz_file | grep -Fo "${adb_dnsdir}/${adb_dnsfile}")" ]
276         then
277             uci -q del_list resolver.kresd.rpz_file="${adb_dnsdir}/${adb_dnsfile}"
278         fi
279         if [ -n "$(uci -q changes resolver)" ]
280         then
281             uci -q commit resolver
282         fi
283     fi
284
285     # firewall related options
286     #
287     if [ ${adb_enabled} -eq 1 ] && [ ${adb_forcedns} -eq 1 ] && [ -z "$(uci -q get firewall.adblock_dns)" ]
288     then
289         uci -q set firewall.adblock_dns="redirect"
290         uci -q set firewall.adblock_dns.name="Adblock DNS"
291         uci -q set firewall.adblock_dns.src="lan"
292         uci -q set firewall.adblock_dns.proto="tcp udp"
293         uci -q set firewall.adblock_dns.src_dport="53"
294         uci -q set firewall.adblock_dns.dest_port="53"
295         uci -q set firewall.adblock_dns.target="DNAT"
296     elif [ -n "$(uci -q get firewall.adblock_dns)" ] && ([ ${adb_enabled} -eq 0 ] || [ ${adb_forcedns} -eq 0 ])
297     then
298         uci -q delete firewall.adblock_dns
299     fi
300     if [ -n "$(uci -q changes firewall)" ]
301     then
302         uci -q commit firewall
303         if [ $(/etc/init.d/firewall enabled; printf "%u" ${?}) -eq 0 ]
304         then
305             /etc/init.d/firewall reload >/dev/null 2>&1
306         fi
307     fi
308 }
309
310 # f_rmtemp: remove temporary files & directories
311 #
312 f_rmtemp()
313 {
314     if [ -d "${adb_tmpdir}" ]
315     then
316         rm -f "${adb_tmpload}"
317         rm -f "${adb_tmpfile}"
318         rm -rf "${adb_tmpdir}"
319     fi
320 }
321
322 # f_rmdns: remove dns related files & directories
323 #
324 f_rmdns()
325 {
326     if [ -n "${adb_dns}" ]
327     then
328         > "${adb_dnsdir}/${adb_dnsfile}"
329         > "${adb_rtfile}"
330         rm -f "${adb_dnsdir}/.${adb_dnsfile}"
331         rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
332     fi
333 }
334
335 # f_dnsrestart: restart the dns backend
336 #
337 f_dnsrestart()
338 {
339     local dns_up cnt=0
340
341     "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
342     while [ ${cnt} -le 10 ]
343     do
344         dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running")"
345         if [ "${dns_up}" = "true" ]
346         then
347             return 0
348         fi
349         cnt=$((cnt+1))
350         sleep 1
351     done
352     return 1
353 }
354
355 # f_list: backup/restore/remove blocklists
356 #
357 f_list()
358 {
359     local mode="${1}" in_rc="${adb_rc}" cnt=0
360
361     case "${mode}" in
362         backup)
363             cnt="$(wc -l < "${adb_tmpfile}")"
364             if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
365             then
366                 gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
367                 adb_rc=${?}
368             fi
369             ;;
370         restore)
371             if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ] &&
372                 [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
373             then
374                 gunzip -cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
375                 adb_rc=${?}
376             fi
377             ;;
378         remove)
379             if [ -d "${adb_backupdir}" ]
380             then
381                 rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
382             fi
383             adb_rc=${?}
384             ;;
385         merge)
386             if [ -s "${adb_tmpfile}" ]
387             then
388                 cat "${adb_tmpfile}" >> "${adb_tmpdir}/${adb_dnsfile}"
389                 adb_rc=${?}
390             fi
391             ;;
392         format)
393             if [ -s "${adb_tmpdir}/tmp.whitelist" ]
394             then
395                 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpdir}/${adb_dnsfile}" | eval "${adb_dnsformat}" > "${adb_dnsdir}/${adb_dnsfile}"
396             else
397                 eval "${adb_dnsformat}" "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
398             fi
399             if [ -n "${adb_dnsheader}" ]
400             then
401                 printf '%s\n' "${adb_dnsheader}" | cat - "${adb_dnsdir}/${adb_dnsfile}" > "${adb_tmpdir}/${adb_dnsfile}"
402                 cat "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
403             fi
404             adb_rc=${?}
405             ;;
406     esac
407     f_log "debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
408 }
409
410 # f_tldcompression: top level domain compression
411 #
412 f_tldcompression()
413 {
414     local source="${1}" temp="${adb_tmpload}"
415
416     awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev/null | sort -u > "${temp}"
417     awk '{if(NR==1){tld=$NF};while(getline){if($NF !~ tld"\\."){print tld;tld=$NF}}print tld}' "${temp}" 2>/dev/null > "${source}"
418     awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev/null > "${temp}"
419     sort -u "${temp}" > "${source}"
420 }
421
422 # f_switch: suspend/resume adblock processing
423 #
424 f_switch()
425 {
426     local source target status mode="${1}"
427
428     if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && [ "${mode}" = "suspend" ]
429     then
430         source="${adb_dnsdir}/${adb_dnsfile}"
431         target="${adb_dnsdir}/.${adb_dnsfile}"
432         status="suspended"
433     elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ] && [ "${mode}" = "resume" ]
434     then
435         source="${adb_dnsdir}/.${adb_dnsfile}"
436         target="${adb_dnsdir}/${adb_dnsfile}"
437         status="resumed"
438     fi
439     if [ -n "${status}" ]
440     then
441         cat "${source}" > "${target}"
442         > "${source}"
443         chown "${adb_dnsuser}" "${target}" 2>/dev/null
444         f_dnsrestart
445         f_jsnupdate
446         f_log "info " "adblock processing ${status}"
447     fi
448 }
449
450 # f_query: query blocklist for certain (sub-)domains
451 #
452 f_query()
453 {
454     local search result cnt
455     local domain="${1}"
456     local tld="${domain#*.}"
457
458     if [ ! -s "${adb_dnsdir}/${adb_dnsfile}" ]
459     then
460          printf "%s\n" "::: no active blocklist found, please start / resume adblock first"
461     elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
462     then
463         printf "%s\n" "::: invalid domain input, please submit a single domain, e.g. 'doubleclick.net'"
464     else
465         cd "${adb_dnsdir}"
466         while [ "${domain}" != "${tld}" ]
467         do
468             search="${domain//./\.}"
469             if [ "${adb_dns}" = "dnsmasq" ] || [ "${adb_dns}" = "unbound" ]
470             then
471                 result="$(awk -F '/|\"' "/[\/\"\.]${search}/{i++;{printf(\"  + %s\n\",\$2)};if(i>9){exit}}" "${adb_dnsfile}")"
472             else
473                 result="$(awk "/(^[^\*][[:alpha:]]*[\.]+${search}|^${search})/{i++;{printf(\"  + %s\n\",\$1)};if(i>9){exit}}" "${adb_dnsfile}")"
474             fi
475             printf "%s\n" "::: max. ten results for domain '${domain}'"
476             printf "%s\n" "${result:-"  - no match"}"
477             domain="${tld}"
478             tld="${domain#*.}"
479         done
480     fi
481 }
482
483 # f_jsnupdate: update runtime information
484 #
485 f_jsnupdate()
486 {
487     local status rundate="$(/bin/date "+%d.%m.%Y %H:%M:%S")"
488
489     if [ ${adb_rc} -gt 0 ]
490     then
491         status="error"
492     elif [ ${adb_enabled} -eq 0 ]
493     then
494         status="disabled"
495     elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ]
496     then
497         status="paused"
498     else
499         status="enabled"
500         if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
501         then
502             if [ "${adb_dns}" = "named" ] || [ "${adb_dns}" = "kresd" ]
503             then
504                 adb_cnt="$(( ( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ) / 2 ))"
505             else
506                 adb_cnt="$(wc -l < "${adb_dnsdir}/${adb_dnsfile}")"
507             fi
508         fi
509     fi
510
511     if [ -z "${adb_fetchinfo}" ] && [ -s "${adb_rtfile}" ]
512     then
513         json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
514         json_select data
515         json_get_var adb_fetchinfo "fetch_utility"
516     fi
517
518     json_init
519     json_add_object "data"
520     json_add_string "adblock_status" "${status}"
521     json_add_string "adblock_version" "${adb_ver}"
522     json_add_string "overall_domains" "${adb_cnt}"
523     json_add_string "fetch_utility" "${adb_fetchinfo}"
524     json_add_string "dns_backend" "${adb_dns} (${adb_dnsdir})"
525     json_add_string "last_rundate" "${rundate}"
526     json_add_string "system_release" "${adb_sysver}"
527     json_close_object
528     json_dump > "${adb_rtfile}"
529 }
530
531 # f_status: output runtime information
532 #
533 f_status()
534 {
535     local key keylist value
536
537     if [ -s "${adb_rtfile}" ]
538     then
539         printf "%s\n" "::: adblock runtime information"
540         json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
541         json_select data
542         json_get_keys keylist
543         for key in ${keylist}
544         do
545             json_get_var value "${key}"
546             printf "  + %-15s : %s\n" "${key}" "${value}"
547         done
548     fi
549 }
550
551 # f_log: write to syslog, exit on error
552 #
553 f_log()
554 {
555     local class="${1}" log_msg="${2}"
556
557     if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
558     then
559         logger -t "adblock-[${adb_ver}] ${class}" "${log_msg}"
560         if [ "${class}" = "error" ]
561         then
562             logger -t "adblock-[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
563             f_rmtemp
564             if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
565             then
566                 f_rmdns
567                 f_dnsrestart
568             fi
569             adb_rc=1
570             f_jsnupdate
571             exit 1
572         fi
573     fi
574 }
575
576 # main function for blocklist processing
577 #
578 f_main()
579 {
580     local src_name src_rset shalla_archive enabled url hash_old hash_new
581     local mem_total="$(awk '/^MemTotal/ {print int($2/1000)}' "/proc/meminfo")"
582
583     f_log "info " "start adblock processing ..."
584     f_log "debug" "action: ${adb_action}, dns: ${adb_dns}, fetch: ${adb_fetchinfo}, hashsum: ${adb_hashsum}, backup: ${adb_backup}, backup_mode: ${adb_backup_mode}, whitelist_mode: ${adb_whitelist_mode}, force_srt/_dns: ${adb_forcesrt}/${adb_forcedns}, mem_total: ${mem_total}"
585     > "${adb_rtfile}"
586     > "${adb_dnsdir}/.${adb_dnsfile}"
587
588     # prepare whitelist entries
589     #
590     if [ -s "${adb_whitelist}" ]
591     then
592         if [ ${adb_whitelist_mode} -eq 1 ] && [ "${adb_dns}" != "dnscrypt-proxy" ]
593         then
594             adb_whitelist_rset="\$0~/^([[:alnum:]_-]+\.){1,}[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}"
595         else
596             adb_whitelist_rset="\$0~/^([[:alnum:]_-]+\.){1,}[[:alpha:]]+([[:space:]]|$)/{gsub(\"\\\.\",\"\\\.\",\$1);print tolower(\"^\"\$1\"\\\|\\\.\"\$1)}"
597         fi
598         awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
599     fi
600
601     # whitelist mode
602     #
603     if [ ${adb_whitelist_mode} -eq 1 ] && [ "${adb_dns}" != "dnscrypt-proxy" ]
604     then
605         f_tldcompression "${adb_tmpdir}/tmp.whitelist"
606         eval "${adb_dnsformat}" "${adb_tmpdir}/tmp.whitelist" > "${adb_dnsdir}/${adb_dnsfile}"
607         printf '%s\n' "${adb_dnsblock}" >> "${adb_dnsdir}/${adb_dnsfile}"
608         if [ -n "${adb_dnsheader}" ]
609         then
610             printf '%s\n' "${adb_dnsheader}" | cat - "${adb_dnsdir}/${adb_dnsfile}" > "${adb_tmpdir}/${adb_dnsfile}"
611             cat "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
612         fi
613         f_dnsrestart
614         if [ ${?} -eq 0 ]
615         then
616             f_jsnupdate "${adb_cnt}"
617             f_log "info " "whitelist with overall ${adb_cnt} domains loaded successfully (${adb_sysver})"
618         else
619             f_log "error" "dns backend restart with active whitelist failed"
620         fi
621         return
622     fi
623
624     # normal & backup mode
625     #
626     for src_name in ${adb_sources}
627     do
628         eval "enabled=\"\${enabled_${src_name}}\""
629         eval "url=\"\${adb_src_${src_name}}\""
630         eval "src_rset=\"\${adb_src_rset_${src_name}}\""
631         > "${adb_tmpload}"
632         > "${adb_tmpfile}"
633         adb_rc=4
634
635         # basic pre-checks
636         #
637         f_log "debug" "name: ${src_name}, enabled: ${enabled}, url: ${url}, rset: ${src_rset}"
638         if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
639         then
640             f_list remove
641             continue
642         fi
643
644         # backup mode
645         #
646         if [ ${adb_backup_mode} -eq 1 ] && [ "${adb_action}" = "start" ] && [ "${src_name}" != "blacklist" ]
647         then
648             f_list restore
649             if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
650             then
651                 f_list merge
652                 continue
653             fi
654         fi
655
656         # download blocklist
657         #
658         if [ "${src_name}" = "blacklist" ] && [ -s "${url}" ]
659         then
660             cat "${url}" > "${adb_tmpload}"
661             adb_rc=${?}
662         elif [ "${src_name}" = "shalla" ]
663         then
664             shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
665             "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}" 2>/dev/null
666             adb_rc=${?}
667             if [ ${adb_rc} -eq 0 ]
668             then
669                 for category in ${adb_src_cat_shalla}
670                 do
671                     tar -xOzf "${shalla_archive}" "BL/${category}/domains" >> "${adb_tmpload}"
672                     adb_rc=${?}
673                     if [ ${adb_rc} -ne 0 ]
674                     then
675                         break
676                     fi
677                 done
678             fi
679             rm -f "${shalla_archive}"
680             rm -rf "${adb_tmpdir}/BL"
681         else
682             "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}" 2>/dev/null
683             adb_rc=${?}
684         fi
685
686         # check download result and prepare list output (incl. tld compression, list backup & restore)
687         #
688         if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
689         then
690             awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
691             if [ -s "${adb_tmpfile}" ]
692             then
693                 f_tldcompression "${adb_tmpfile}"
694                 if [ "${src_name}" != "blacklist" ]
695                 then
696                     f_list backup
697                 fi
698             else
699                 f_list restore
700             fi
701         else
702             f_list restore
703         fi
704
705         # list merge
706         #
707         if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
708         then
709             f_list merge
710             if [ ${adb_rc} -ne 0 ]
711             then
712                 f_list remove
713             fi
714         else
715             f_list remove
716         fi
717     done
718
719     # hash preparation, whitelist removal and overall sort
720     #
721     if [ -x "${adb_hashsum}" ] && [ -f "${adb_dnsdir}/${adb_dnsfile}" ]
722     then
723         hash_old="$(${adb_hashsum} "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
724     fi
725     if [ -s "${adb_tmpdir}/${adb_dnsfile}" ]
726     then
727         if [ ${mem_total} -ge 64 ] || [ ${adb_forcesrt} -eq 1 ]
728         then
729             f_tldcompression "${adb_tmpdir}/${adb_dnsfile}"
730         fi
731         f_list format
732     else
733         > "${adb_dnsdir}/${adb_dnsfile}"
734     fi
735     chown "${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
736     f_rmtemp
737
738     # conditional restart of the dns backend and runtime information export
739     #
740     if [ -x "${adb_hashsum}" ] && [ -f "${adb_dnsdir}/${adb_dnsfile}" ]
741     then
742         hash_new="$(${adb_hashsum} "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
743     fi
744     if [ -z "${hash_old}" ] || [ -z "${hash_new}" ] || [ "${hash_old}" != "${hash_new}" ]
745     then
746         f_dnsrestart
747     fi
748     if [ ${?} -eq 0 ]
749     then
750         f_jsnupdate "${adb_cnt}"
751         f_log "info " "blocklist with overall ${adb_cnt} domains loaded successfully (${adb_sysver})"
752     else
753         f_log "error" "dns backend restart with active blocklist failed"
754     fi
755 }
756
757 # handle different adblock actions
758 #
759 f_envload
760 case "${adb_action}" in
761     stop)
762         f_rmtemp
763         f_rmdns
764         f_dnsrestart
765         ;;
766     restart)
767         f_rmtemp
768         f_rmdns
769         f_envcheck
770         f_main
771         ;;
772     suspend)
773         f_switch suspend
774         ;;
775     resume)
776         f_switch resume
777         ;;
778     query)
779         f_query "${2}"
780         ;;
781     status)
782         f_status
783         ;;
784     *)
785         f_envcheck
786         f_main
787         ;;
788 esac
789 exit 0