2 # function library used by adblock-update.sh
3 # written by Dirk Brenken (openwrt@brenken.org)
5 #####################################
6 # f_envload: load adblock environment
12 # get version string from default adblock configuration file
14 cfg_version
="$(/sbin/uci -q get adblock.global.adb_cfgver 2>/dev/null)"
15 cfg_enabled
="$(/sbin/uci -q get adblock.global.adb_enabled 2>/dev/null)"
17 if [ $
((rc
)) -ne 0 ] ||
[ "${cfg_version}" != "${adb_scriptver%.*}" ]
19 cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock" >/dev
/null
2>&1
23 f_log
"new default adblock configuration applied, please check your settings in '/etc/config/adblock'"
25 f_log
"original adblock configuration not found, please (re-)install the adblock package via 'opkg install adblock --force-maintainer'" "${rc}"
28 elif [ $
((rc
)) -eq 0 ] && [ $
((cfg_enabled
)) -ne 1 ]
31 f_log
"adblock is currently disabled, please run 'uci set adblock.global.adb_enabled=1' and 'uci commit adblock' to enable this service"
35 # source in openwrt function library
37 if [ -r "/lib/functions.sh" ]
39 .
"/lib/functions.sh" 2>/dev
/null
42 f_log
"openwrt function library not found" "${rc}"
46 # source in openwrt network library
48 if [ -r "/lib/functions/network.sh" ]
50 .
"/lib/functions/network.sh" 2>/dev
/null
53 f_log
"openwrt network library not found" "${rc}"
57 # check opkg availability and get list with all installed openwrt packages
59 if [ -r "/var/lock/opkg.lock" ]
62 f_log
"adblock installation finished, 'opkg' currently locked by package installer"
65 pkg_list
="$(opkg list-installed 2>/dev/null)"
66 if [ -z "${pkg_list}" ]
69 f_log
"empty openwrt package list" "${rc}"
74 ######################################################
75 # f_envparse: parse adblock config and set environment
79 # set initial defaults,
80 # may be overwritten by setting appropriate adblock config options in global section of /etc/config/adblock
85 adb_nullipv4
="192.0.2.1"
86 adb_nullipv6
="::ffff:c000:0201"
87 adb_blacklist
="/etc/adblock/adblock.blacklist"
88 adb_whitelist
="/etc/adblock/adblock.whitelist"
91 # function to read global options by callback
97 if [ "${type}" = "adblock" ]
103 eval "${option}=\"${value}\""
110 # function to iterate through config list, read only options in "enabled" sections
112 adb_cfglist
="adb_backupdir adb_src"
117 config_get switch
"${config}" "enabled"
118 if [ "${switch}" = "1" ]
120 for option
in ${adb_cfglist}
122 config_get value
"${config}" "${option}"
125 if [ "${option}" = "adb_src" ]
127 if [ "${config}" = "shalla" ]
132 adb_cat_shalla
="${adb_cat_shalla} ${cat}"
134 eval "adb_arc_shalla=\"${value}\""
135 config_list_foreach
"shalla" "adb_catlist" "categories"
137 adb_sources
="${adb_sources} ${value}"
140 eval "${option}=\"${value}\""
147 # load adblock config and start parsing functions
150 config_foreach parse_config service
151 config_foreach parse_config
source
153 # set more script defaults (can't be overwritten by adblock config options)
157 adb_tmpfile
="$(mktemp -tu 2>/dev/null)"
158 adb_tmpdir
="$(mktemp -p /tmp -d 2>/dev/null)"
159 adb_dnsdir
="/tmp/dnsmasq.d"
160 adb_dnsprefix
="adb_list"
161 adb_prechain_ipv4
="prerouting_rule"
162 adb_fwdchain_ipv4
="forwarding_rule"
163 adb_outchain_ipv4
="output_rule"
164 adb_prechain_ipv6
="PREROUTING"
165 adb_fwdchain_ipv6
="forwarding_rule"
166 adb_outchain_ipv6
="output_rule"
167 adb_fetch
="/usr/bin/wget"
168 unset adb_srclist adb_revsrclist adb_errsrclist
170 # set adblock source ruleset definitions
172 rset_core
="([A-Za-z0-9_-]+\.){1,}[A-Za-z]+"
173 rset_adaway
="awk '\$0 ~/^127\.0\.0\.1[ ]+${rset_core}/{print tolower(\$2)}'"
174 rset_blacklist
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
175 rset_disconnect
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
176 rset_dshield
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
177 rset_feodo
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
178 rset_malware
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
179 rset_malwarelist
="awk '\$0 ~/^127\.0\.0\.1[ ]+${rset_core}/{print tolower(\$2)}'"
180 rset_openphish
="awk -F '/' '\$3 ~/^${rset_core}/{print tolower(\$3)}'"
181 rset_palevo
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
182 rset_ruadlist
="awk -F '[|^]' '\$0 ~/^\|\|${rset_core}\^$/{print tolower(\$3)}'"
183 rset_shalla
="awk -F '/' '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
184 rset_spam404
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
185 rset_whocares
="awk '\$0 ~/^127\.0\.0\.1[ ]+${rset_core}/{print tolower(\$2)}'"
186 rset_winhelp
="awk '\$0 ~/^0\.0\.0\.0[ ]+${rset_core}/{print tolower(\$2)}'"
187 rset_yoyo
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
188 rset_zeus
="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
190 # get logical wan update interfaces (with default route) and their device names
192 network_find_wan adb_wanif4
2>/dev
/null
193 network_find_wan6 adb_wanif6
2>/dev
/null
194 if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
197 f_log
"no valid IPv4/IPv6 wan update interface found" "${rc}"
199 elif [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
202 f_log
"LAN only (${adb_lanif}) network, no valid IPv4/IPv6 wan update interface found" "${rc}"
205 network_get_device adb_wandev4
"${adb_wanif4}" 2>/dev
/null
206 network_get_device adb_wandev6
"${adb_wanif6}" 2>/dev
/null
210 # get lan ip addresses
212 network_get_ipaddr adb_ipv4
"${adb_lanif}" 2>/dev
/null
213 network_get_ipaddr6 adb_ipv6
"${adb_lanif}" 2>/dev
/null
214 if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
217 f_log
"no valid IPv4/IPv6 configuration for given logical LAN interface found (${adb_lanif}), please set 'adb_lanif' manually" "${rc}"
222 #################################################
223 # f_envcheck: check/set environment prerequisites
229 # check general package dependencies
234 f_depend
"kmod-ipt-nat"
236 # check ipv6 related package dependencies
238 if [ -n "${adb_wanif6}" ]
240 check
="$(printf "${pkg_list}" | grep "^ip6tables
-" 2>/dev/null)"
243 f_log
"package 'ip6tables' not found, IPv6 support will be disabled"
246 check
="$(printf "${pkg_list}" | grep "^kmod-ipt-nat6
-" 2>/dev/null)"
249 f_log
"package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
255 # check ca-certificates package and set wget parms accordingly
257 wget_parm
="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=5 --connect-timeout=5 --read-timeout=5"
258 check
="$(printf "${pkg_list}" | grep "^ca-certificates
-" 2>/dev/null)"
261 wget_parm
="${wget_parm} --no-check-certificate"
264 # check adblock blacklist/whitelist configuration
266 if [ ! -r "${adb_blacklist}" ]
268 f_log
"adblock blacklist not found, source will be disabled"
270 if [ ! -r "${adb_whitelist}" ]
272 f_log
"adblock whitelist not found, source will be disabled"
275 # check adblock temp directory
277 if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
279 f_space
"${adb_tmpdir}"
280 if [ "${space_ok}" = "false" ]
282 if [ $
((av_space
)) -le 2000 ]
285 f_log
"not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)" "${rc}"
288 f_log
"not enough free space to handle all adblock list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
293 f_log
"temp directory not found" "${rc}"
299 mem_total
="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc
/meminfo
" 2>/dev/null)"
300 mem_free
="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc
/meminfo
" 2>/dev/null)"
301 mem_swap
="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc
/meminfo
" 2>/dev/null)"
302 if [ $
((mem_total
)) -le 64000 ] && [ $
((mem_swap
)) -eq 0 ]
305 f_log
"not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
308 # check backup configuration
310 if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
312 f_space
"${adb_backupdir}"
313 if [ "${space_ok}" = "false" ]
315 f_log
"not enough free space in '${adb_backupdir}'(avail. ${av_space} kb), backup/restore will be disabled"
318 f_log
"backup/restore will be enabled"
323 f_log
"backup/restore will be disabled"
326 # check ipv4/iptables configuration
328 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wandev4}" ]
330 f_firewall
"IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-prerouting" "! -i ${adb_wandev4} -p tcp -d ${adb_nullipv4} -m multiport --dports 80,443 -j REDIRECT --to-ports ${adb_port}"
331 f_firewall
"IPv4" "filter" "A" "${adb_fwdchain_ipv4}" "adb-forward" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
332 f_firewall
"IPv4" "filter" "A" "${adb_outchain_ipv4}" "adb-output" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
333 if [ $
((adb_forcedns
)) -eq 1 ]
335 f_firewall
"IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p udp --dport 53 -j REDIRECT"
336 f_firewall
"IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p tcp --dport 53 -j REDIRECT"
338 if [ "${fw_done}" = "true" ]
340 f_log
"created volatile IPv4 firewall ruleset"
345 # check ipv6/ip6tables configuration
347 if [ -n "${adb_wanif6}" ] && [ -n "${adb_wandev6}" ]
349 f_firewall
"IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-prerouting" "! -i ${adb_wandev6} -p tcp -d ${adb_nullipv6} -m multiport --dports 80,443 -j REDIRECT --to-ports ${adb_port}"
350 f_firewall
"IPv6" "filter" "A" "${adb_fwdchain_ipv6}" "adb-forward" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
351 f_firewall
"IPv6" "filter" "A" "${adb_outchain_ipv6}" "adb-output" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
352 if [ $
((adb_forcedns
)) -eq 1 ]
354 f_firewall
"IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p udp --dport 53 -j REDIRECT"
355 f_firewall
"IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p tcp --dport 53 -j REDIRECT"
357 if [ "${fw_done}" = "true" ]
359 f_log
"created volatile IPv6 firewall ruleset"
364 # check volatile adblock uhttpd instance configuration
366 rc
="$(ps | grep "[u
]httpd.
*\
-h /www
/adblock
" >/dev/null 2>&1; printf ${?})"
369 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
371 uhttpd
-h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}" -p "[${adb_ipv6}]:${adb_port}">/dev
/null
2>&1
373 elif [ -n "${adb_wanif4}" ]
375 uhttpd
-h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}" >/dev
/null
2>&1
377 elif [ -n "${adb_wanif6}" ]
379 uhttpd
-h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_port}" >/dev
/null
2>&1
384 f_log
"created volatile uhttpd instance"
386 f_log
"failed to initialize volatile uhttpd instance" "${rc}"
391 # set dnsmasq defaults
393 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
395 adb_dnsformat
="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
396 elif [ -n "${adb_wanif4}" ]
398 adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print
\"address
=/\"\
$0\"/\"ipv4
}'"
399 elif [ -n "${adb_wanif6}" ]
401 adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print
\"address
=/\"\
$0\"/\"ipv6
}'"
404 # remove no longer used opkg package list
409 ######################################
410 # f_depend: check package dependencies
417 check="$(printf "${pkg_list}" | grep "^${package} -" 2>/dev/null)"
421 f_log "package '${package}' not found" "${rc}"
426 ##############################################
427 # f_firewall: set iptables rules for ipv4/ipv6
432 local iptv4="/usr/sbin/iptables"
433 local iptv6="/usr/sbin/ip6tables"
441 # select appropriate iptables executable
443 if [ "${proto}" = "IPv4" ]
450 # check whether iptables rule already applied and proceed accordingly
452 rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
455 "${ipt}" -w -t "${table}" -"${ctype}" "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1
461 f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}"
467 ##########################################
468 # f_log: log messages to stdout and syslog
477 # check for terminal session
484 # log to different output devices and set log class accordingly
486 if [ -n "${log_msg}" ]
488 if [ $((log_rc)) -gt 0 ]
491 log_rc=", rc: ${log_rc}"
492 log_msg="${log_msg}${log_rc}"
494 /usr/bin/logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}"
498 ################################################
499 # f_space: check mount points/space requirements
505 # check relevant mount points in a subshell
509 av_space="$(df "${mp}" 2>/dev/null | tail -n1 2>/dev/null | awk '{printf $4}')"
510 if [ $((av_space)) -lt $((adb_minspace)) ]
517 ##################################################################
518 # f_restore: restore last adblock list backups and restart dnsmasq
525 # remove bogus adblock lists
527 if [ -n "${adb_revsrclist}" ]
529 rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \; 2>/dev/null)"
531 if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
533 f_log "all bogus adblock lists removed"
534 elif [ $((rc)) -ne 0 ]
536 f_log "error during removal of bogus adblock lists" "${rc}"
543 if [ "${backup_ok}" = "true" ] && [ "$(printf "${adb_backupdir}/${adb_dnsprefix}."*)" != "${adb_backupdir}/${adb_dnsprefix}.*" ]
545 restore_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec cp -pf "{}" "${adb_dnsdir}" \; 2>/dev/null)"
547 if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
549 f_log "all available backups restored"
550 elif [ $((rc)) -ne 0 ]
552 f_log "error during restore of adblock lists" "${rc}"
556 f_log "no backups found, nothing to restore"
559 # (re-)try dnsmasq restart without bogus adblock lists / with backups
561 if [ -n "${restore_done}" ] || [ -n "${rm_done}" ]
563 /etc/init.d/dnsmasq restart >/dev/null 2>&1
565 dns_status="$(ps 2>/dev/null | grep "[d]nsmasq" 2>/dev/null)"
566 if [ -n "${dns_status}" ]
569 adb_count="$(head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null | wc -l)"
570 if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
572 adb_count="$((adb_count / 2))"
574 f_log "adblock lists with overall ${adb_count} domains loaded"
577 f_log "dnsmasq restart failed, please check 'logread
' output" "${rc}"
583 ###################################
584 # f_exit: delete (temporary) files,
585 # generate statistics and exit
589 local ipv4_prerouting=0
592 local ipv6_prerouting=0
595 local iptv4="/usr/sbin/iptables"
596 local iptv6="/usr/sbin/ip6tables"
598 # delete temporary files & directories
600 if [ -f "${adb_tmpfile}" ]
602 rm -f "${adb_tmpfile}" >/dev/null 2>&1
604 if [ -d "${adb_tmpdir}" ]
606 rm -rf "${adb_tmpdir}" >/dev/null 2>&1
609 # final log message and iptables statistics
613 if [ -n "${adb_wanif4}" ]
615 ipv4_prerouting="$(${iptv4} -t nat -vnL | awk '$11 ~
/^adb-prerouting$
/ {sum += $1} END
{printf sum}')"
616 ipv4_forward="$(${iptv4} -vnL | awk '$11 ~
/^adb-forward$
/ {sum += $1} END
{printf sum}')"
617 ipv4_output="$(${iptv4} -vnL | awk '$11 ~
/^adb-output$
/ {sum += $1} END
{printf sum}')"
619 if [ -n "${adb_wanif6}" ]
621 ipv6_prerouting="$(${iptv6} -t nat -vnL | awk '$10 ~
/^adb-prerouting$
/ {sum += $1} END
{printf sum}')"
622 ipv6_forward="$(${iptv6} -vnL | awk '$10 ~
/^adb-forward$
/ {sum += $1} END
{printf sum}')"
623 ipv6_output="$(${iptv6} -vnL | awk '$10 ~
/^adb-output$
/ {sum += $1} END
{printf sum}')"
625 f_log "adblock firewall statistics (IPv4/IPv6):"
626 f_log "${ipv4_prerouting}/${ipv6_prerouting} packets redirected in PREROUTING chain"
627 f_log "${ipv4_forward}/${ipv6_forward} packets rejected in FORWARD chain"
628 f_log "${ipv4_output}/${ipv6_output} packets rejected in OUTPUT chain"
629 f_log "domain adblock processing finished successfully (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
630 elif [ $((rc)) -gt 0 ]
632 f_log "domain adblock processing failed (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
636 rm -f "${adb_pidfile}" >/dev/null 2>&1