@ -15,9 +15,9 @@ f_envload()
then
then
. /lib/functions.sh
. /lib/functions.sh
else
else
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: openwrt function library not found"
rc = 510
f_log "openwrt function library not found" " ${ rc } "
f_deltemp
f_deltemp
exit 10
fi
fi
# source in openwrt json helpers library
# source in openwrt json helpers library
@ -26,9 +26,9 @@ f_envload()
then
then
. "/usr/share/libubox/jshn.sh"
. "/usr/share/libubox/jshn.sh"
else
else
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: openwrt json helpers library not found"
rc = 515
f_log "openwrt json helpers library not found" " ${ rc } "
f_deltemp
f_deltemp
exit 15
fi
fi
# get list with all installed openwrt packages
# get list with all installed openwrt packages
@ -36,9 +36,9 @@ f_envload()
pkg_list = " $( opkg list-installed 2>/dev/null) "
pkg_list = " $( opkg list-installed 2>/dev/null) "
if [ -z " ${ pkg_list } " ]
if [ -z " ${ pkg_list } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: empty openwrt package list"
rc = 520
f_log "empty openwrt package list" " ${ rc } "
f_deltemp
f_deltemp
exit 20
fi
fi
}
}
@ -133,11 +133,8 @@ f_envparse()
# set temp variables and counter
# set temp variables and counter
#
#
adb_tmpfile = " $( mktemp -tu) "
adb_tmpdir = " $( mktemp -d) "
cnt = 0
max_cnt = 30
max_time = 60
adb_tmpfile = " $( mktemp -tu 2>/dev/null) "
adb_tmpdir = " $( mktemp -d 2>/dev/null) "
# set adblock source ruleset definitions
# set adblock source ruleset definitions
#
#
@ -160,13 +157,26 @@ f_envparse()
#
#
f_envcheck( )
f_envcheck( )
{
{
# check required config variables
#
adb_varlist = "adb_ip adb_dev adb_if adb_domain adb_minspace adb_maxloop adb_maxtime adb_blacklist adb_whitelist"
for var in ${ adb_varlist }
do
if [ -z " $( eval printf \" \$ " ${ var } " \" ) " ]
then
rc = 525
f_log " missing adblock config option ( ${ var } ) " " ${ rc } "
f_deltemp
fi
done
# check adblock network device configuration
# check adblock network device configuration
#
#
if [ ! -d " /sys/class/net/ ${ adb_dev } " ]
if [ ! -d " /sys/class/net/ ${ adb_dev } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: invalid adblock network device input ( ${ adb_dev } ) "
rc = 530
f_log " invalid adblock network device input ( ${ adb_dev } ) " " ${ rc } "
f_deltemp
f_deltemp
exit 25
fi
fi
# check adblock network interface configuration
# check adblock network interface configuration
@ -175,9 +185,9 @@ f_envcheck()
banned_if = " $( printf " ${ adb_if } " | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p' ) "
banned_if = " $( printf " ${ adb_if } " | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p' ) "
if [ -n " ${ check_if } " ] || [ -n " ${ banned_if } " ]
if [ -n " ${ check_if } " ] || [ -n " ${ banned_if } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: invalid adblock network interface input ( ${ adb_if } ) "
rc = 535
f_log " invalid adblock network interface input ( ${ adb_if } ) " " ${ rc } "
f_deltemp
f_deltemp
exit 30
fi
fi
# check adblock ip address configuration
# check adblock ip address configuration
@ -185,43 +195,56 @@ f_envcheck()
check_ip = " $( printf " ${ adb_ip } " | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p' ) "
check_ip = " $( printf " ${ adb_ip } " | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p' ) "
if [ -z " ${ check_ip } " ]
if [ -z " ${ check_ip } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: invalid adblock ip address input ( ${ adb_ip } ) "
rc = 540
f_log " invalid adblock ip address input ( ${ adb_ip } ) " " ${ rc } "
f_deltemp
f_deltemp
exit 35
fi
fi
# check adblock blacklist/whitelist configuration
# check adblock blacklist/whitelist configuration
#
#
if [ ! -r " ${ adb_blacklist } " ]
if [ ! -r " ${ adb_blacklist } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: adblock blacklist not found"
rc = 545
f_log "adblock blacklist not found" " ${ rc } "
f_deltemp
f_deltemp
exit 40
elif [ ! -r " ${ adb_whitelist } " ]
elif [ ! -r " ${ adb_whitelist } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: adblock whitelist not found"
rc = 550
f_log "adblock whitelist not found" " ${ rc } "
f_deltemp
f_deltemp
exit 45
fi
fi
# check wan update configuration
# check adblock temp directory
#
#
if [ -n " ${ adb_wandev } " ]
if [ -n " ${ adb_tmpdir } " ] && [ -d " ${ adb_tmpdir } " ]
then
then
wan_ok = "true"
f_space " ${ adb_tmpdir } "
tmp_ok = "true"
else
else
wan_ok = "false"
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: wan update check will be disabled"
rc = 555
tmp_ok = "false"
f_log "temp directory not found" " ${ rc } "
f_deltemp
fi
fi
# check ntp sync configuration
# check curl package dependency
#
#
if [ -n " ${ adb_ntpsrv } " ]
check = " $( printf " ${ pkg_list } " | grep "^curl" ) "
if [ -z " ${ check } " ]
then
then
ntp_ok = "true"
else
ntp_ok = "false"
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: ntp time sync will be disabled"
rc = 560
f_log "curl package not found" " ${ rc } "
f_deltemp
fi
# check wget package dependency
#
check = " $( printf " ${ pkg_list } " | grep "^wget" ) "
if [ -z " ${ check } " ]
then
rc = 565
f_log "wget package not found" " ${ rc } "
f_deltemp
fi
fi
# check backup configuration
# check backup configuration
@ -229,34 +252,17 @@ f_envcheck()
adb_backupdir = " ${ adb_backupfile %/* } "
adb_backupdir = " ${ adb_backupfile %/* } "
if [ -n " ${ adb_backupdir } " ] && [ -d " ${ adb_backupdir } " ]
if [ -n " ${ adb_backupdir } " ] && [ -d " ${ adb_backupdir } " ]
then
then
f_space " ${ adb_backupdir } "
backup_ok = "true"
backup_ok = "true"
adb_mounts = " ${ adb_backupdir } ${ adb_tmpdir } "
else
else
backup_ok = "false"
backup_ok = "false"
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: backup/restore will be disabled"
fi
# check error log configuration
#
adb_logdir = " ${ adb_logfile %/* } "
if [ -n " ${ adb_logfile } " ] && [ " ${ adb_logfile } " = "/dev/stdout" ]
then
log_ok = "true"
adb_logfile = "/proc/self/fd/1"
elif [ -n " ${ adb_logdir } " ] && [ -d " ${ adb_logdir } " ] && [ " ${ ntp_ok } " = "true" ]
then
log_ok = "true"
adb_mounts = " ${ adb_mounts } ${ adb_logdir } "
else
log_ok = "false"
adb_logfile = "/dev/null"
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: error logging will be disabled"
f_log "backup/restore will be disabled"
fi
fi
# check dns query log configuration
# check dns query log configuration
#
#
adb_querydir = " ${ adb_queryfile %/* } "
adb_querydir = " ${ adb_queryfile %/* } "
query_ pid = "/var/run/adb_query.pid"
adb_querypid = "/var/run/adb_query.pid"
if [ -n " ${ adb_querydir } " ] && [ -d " ${ adb_querydir } " ]
if [ -n " ${ adb_querydir } " ] && [ -d " ${ adb_querydir } " ]
then
then
# check find capabilities
# check find capabilities
@ -265,77 +271,59 @@ f_envcheck()
if [ -z " ${ check } " ]
if [ -z " ${ check } " ]
then
then
query_ok = "false"
query_ok = "false"
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: busybox without 'find/mtime' support (min. r47362), dns query logging will be disabled"
f_log " busybox without 'find/mtime' support (min. r47362), dns query logging will be disabled"
else
else
f_space " ${ adb_querydir } "
query_ok = "true"
query_ok = "true"
query_name = " ${ adb_queryfile ##*/ } "
query_name = " ${ adb_queryfile ##*/ } "
query_ip = " ${ adb_ip //./ \\ . } "
query_ip = " ${ adb_ip //./ \\ . } "
adb_mounts = " ${ adb_mounts } ${ adb_querydir } "
fi
fi
else
else
query_ok = "false"
query_ok = "false"
if [ -s " ${ query_pid } " ]
f_log "dns query logging will be disabled"
if [ -s " ${ adb_querypid } " ]
then
then
kill -9 $( cat " ${ query_ pid } " ) 2 >/dev/null
> " ${ query_ pid } "
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: remove old dns query log background process "
kill -9 " $( cat " ${ adb_ querypid} " ) " >/dev/null 2>& 1
f_log " remove old dns query log background process (pid: $( cat " ${ adb_ querypid} " ) ) "
> " ${ adb_query pid } "
fi
fi
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: dns query logging will be disabled"
fi
fi
# check mount points & space requirements
# check debug log configuration
#
#
adb_mounts = " ${ adb_mounts } ${ adb_tmpdir } "
for mp in ${ adb_mounts }
do
df " ${ mp } " 2>/dev/null | \
tail -n1 | \
while read filesystem overall used available scrap
do
av_space = " ${ available } "
if [ $(( av_space)) -eq 0 ]
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: no space left on device/not mounted ( ${ mp } ) "
exit 50
elif [ $(( av_space)) -lt $(( adb_minspace)) ]
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: not enough space left on device ( ${ mp } ) "
exit 55
fi
done
# subshell return code handling
#
rc = $?
if [ $(( rc)) -ne 0 ]
then
f_deltemp
exit ${ rc }
fi
done
adb_logdir = " ${ adb_logfile %/* } "
if [ -n " ${ adb_logdir } " ] && [ -d " ${ adb_logdir } " ]
then
f_space " ${ adb_logdir } "
log_ok = "true"
else
log_ok = "false"
f_log "debug logging will be disabled"
fi
# check curl package dependency
# check wan update configuration
#
#
check = " $( printf " ${ pkg_list } " | grep "^curl" ) "
if [ -z " ${ check } " ]
if [ -n " ${ adb_wandev } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: curl package not found"
f_deltemp
exit 60
f_wancheck " ${ adb_maxloop } "
else
wan_ok = "false"
f_log "wan update check will be disabled"
fi
fi
# check wget package dependency
# check ntp sync configuration
#
#
check = " $( printf " ${ pkg_list } " | grep "^wget" ) "
if [ -z " ${ check } " ]
if [ -n " ${ adb_ntpsrv } " ]
then
then
/usr/bin/logger -t " adblock[ ${ pid } ] " "error: wget package not found"
f_deltemp
exit 65
f_ntpcheck " ${ adb_maxloop } "
else
ntp_ok = "false"
f_log "ntp time sync will be disabled"
fi
fi
# check dynamic/volatile adblock network interface configuration
# check dynamic/volatile adblock network interface configuration
#
#
rc = " $( ifstatus " ${ adb_if } " >/dev/null 2>& 1; printf $? ) "
rc = " $( ifstatus " ${ adb_if } " >/dev/null 2>& 1; printf ${ ? } ) "
if [ $(( rc)) -ne 0 ]
if [ $(( rc)) -ne 0 ]
then
then
json_init
json_init
@ -347,97 +335,148 @@ f_envcheck()
json_close_array
json_close_array
json_close_object
json_close_object
ubus call network add_dynamic " $( json_dump) "
ubus call network add_dynamic " $( json_dump) "
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: created new dynamic/volatile network interface ( ${ adb_if } , ${ adb_ip } ) "
rc = ${ ? }
if [ $(( rc)) -eq 0 ]
then
f_log " created new dynamic/volatile network interface ( ${ adb_if } , ${ adb_ip } ) "
else
f_log " failed to initialize new dynamic/volatile network interface ( ${ adb_if } , ${ adb_ip } ) " " ${ rc } "
f_deltemp
fi
fi
fi
# check dynamic/volatile adblock uhttpd instance configuration
# check dynamic/volatile adblock uhttpd instance configuration
#
#
rc = " $( ps | grep " [u]httpd.*\-r ${ adb_if } " >/dev/null 2>& 1; printf $? ) "
rc = " $( ps | grep " [u]httpd.*\-r ${ adb_if } " >/dev/null 2>& 1; printf ${ ? } ) "
if [ $(( rc)) -ne 0 ]
if [ $(( rc)) -ne 0 ]
then
then
uhttpd -h "/www/adblock" -r " ${ adb_if } " -E "/adblock.html" -p " ${ adb_ip } :80 "
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: created new dynamic/volatile uhttpd instance ( ${ adb_if } , ${ adb_ip } ) "
uhttpd -h "/www/adblock" -r " ${ adb_if } " -E "/adblock.html" -p " ${ adb_ip } :80 " >/dev/null 2>& 1
rc = ${ ? }
if [ $(( rc)) -eq 0 ]
then
f_log " created new dynamic/volatile uhttpd instance ( ${ adb_if } , ${ adb_ip } ) "
else
f_log " failed to initialize new dynamic/volatile uhttpd instance ( ${ adb_if } , ${ adb_ip } ) " " ${ rc } "
f_deltemp
fi
fi
}
################################################
# f_log: log messages to stdout, syslog, logfile
#
f_log( )
{
local log_msg = " ${ 1 } "
local log_rc = " ${ 2 } "
local class = "info "
if [ -n " ${ log_msg } " ]
then
if [ $(( log_rc)) -ne 0 ]
then
class = "error"
log_rc = " , rc: ${ log_rc } "
fi
/usr/bin/logger -s -t " adblock[ ${ pid } ] ${ class } " " ${ log_msg } ${ log_rc } "
if [ " ${ log_ok } " = "true" ] && [ " ${ ntp_ok } " = "true" ]
then
printf "%s\n" " $( /bin/date "+%d.%m.%Y %H:%M:%S" ) adblock[ ${ pid } ] ${ class } : ${ log_msg } ${ log_rc } " >> " ${ adb_logfile } "
fi
fi
}
################################################
# f_space: check mount points/space requirements
#
f_space( )
{
local mp = " ${ 1 } "
if [ -d " ${ mp } " ]
then
df " ${ mp } " 2>/dev/null | \
tail -n1 | \
while read filesystem overall used available scrap
do
av_space = " ${ available } "
if [ $(( av_space)) -eq 0 ]
then
rc = 570
f_log " no space left on device/not mounted ( ${ mp } ) " " ${ rc } "
exit ${ rc }
elif [ $(( av_space)) -lt $(( adb_minspace)) ]
then
rc = 575
f_log " not enough space left on device ( ${ mp } ) " " ${ rc } "
exit ${ rc }
fi
done
rc = ${ ? }
if [ $(( rc)) -eq 0 ]
then
space_ok = "true"
else
space_ok = "false"
f_deltemp
fi
fi
fi
}
}
###################################################
# f_deltemp: delete temporary files and directories
####################################################
# f_deltemp: delete temp files, directories and exit
#
#
f_deltemp( )
f_deltemp( )
{
{
if [ -f " ${ adb_tmpfile } " ]
if [ -f " ${ adb_tmpfile } " ]
then
then
rm -f " ${ adb_tmpfile } " 2>/dev/null
rm -f " ${ adb_tmpfile } " >/dev/null 2>& 1
fi
fi
if [ -d " ${ adb_tmpdir } " ]
if [ -d " ${ adb_tmpdir } " ]
then
then
rm -rf " ${ adb_tmpdir } " 2>/dev/null
rm -rf " ${ adb_tmpdir } " >/dev/null 2>& 1
fi
fi
f_log " domain adblock processing finished ( ${ adb_version } ) "
exit ${ rc }
}
}
################################################################
# f_remove: remove temporary files, start and maintain query log
####################################################
# f_remove: maintain and (re-)start dom ain query log
#
#
f_remove( )
f_remove( )
{
{
# delete temporary files and directories
#
f_deltemp
# remove existing domain query log background process,
# do housekeeping and start a new process on daily basis
#
if [ " ${ query_ok } " = "true" ] && [ " ${ ntp_ok } " = "true" ]
if [ " ${ query_ok } " = "true" ] && [ " ${ ntp_ok } " = "true" ]
then
then
query_date = " $( date "+%Y%m%d" ) "
query_date = " $( date "+%Y%m%d" ) "
if [ -s " ${ query_pid } " ] && [ ! -f " ${ adb_queryfile } . ${ query_date } " ]
if [ -s " ${ adb_querypid } " ] && [ ! -f " ${ adb_queryfile } . ${ query_date } " ]
then
then
kill -9 $( cat " ${ query_pid } " ) 2>/dev/null
> " ${ query_pid } "
find " ${ adb_backupdir } " -maxdepth 1 -type f -mtime +${ adb_queryhistory } -name " ${ query_name } .* " -exec rm -f { } \; 2>/dev/null
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: remove old dns query log background process and do logfile housekeeping"
kill -9 " $( cat " ${ adb_querypid } " ) " >/dev/null 2>& 1
find " ${ adb_backupdir } " -maxdepth 1 -type f -mtime +" ${ adb_queryhistory } " -name " ${ query_name } .* " -exec rm -f { } \; 2>/dev/null
f_log " remove old dns query log background process (pid: $( cat " ${ adb_querypid } " ) ) and do logfile housekeeping "
> " ${ adb_query pid } "
fi
fi
if [ ! -s " ${ query_pid } " ]
if [ ! -s " ${ adb_ querypid} " ]
then
then
( logread -f 2>/dev/null & printf " $! " > " ${ query_pid } " ) | egrep -o " (query\[A\].*)|([a-z0-9\.\-]* is ${ query_ip } $) " >> " ${ adb_queryfile } . ${ query_date } " &
/usr/bin/logger -t " adblock[ ${ pid } ] " "info: start new domain query log background process"
( logread -f 2>/dev/null & printf ${ ! } > " ${ adb_ querypid} " ) | egrep -o " (query\[A\].*)|([a-z0-9\.\-]* is ${ query_ip } $) " >> " ${ adb_queryfile } . ${ query_date } " &
f_log " new domain query log background process started (pid: $( cat " ${ adb_query pid} " ) ) "
fi
fi
fi
fi
# final log entry
#
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: domain adblock processing finished ( ${ adb_version } ) "
f_deltemp
}
}
#####################################################
# f_restore: if available, restore last adlist backup
################################################################
# f_restore: restore last adblock list backup and restart dnsmasq
#
#
f_restore( )
f_restore( )
{
{
if [ -z " ${ restore_msg } " ]
then
restore_msg = "unknown"
fi
if [ " ${ backup_ok } " = "true" ] && [ -f " ${ adb_backupfile } " ]
if [ " ${ backup_ok } " = "true" ] && [ -f " ${ adb_backupfile } " ]
then
then
cp -f " ${ adb_backupfile } " " ${ adb_dnsfile } " 2>/dev/null
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: ${ restore_msg } , adlist backup restored "
printf "%s\n" " $( /bin/date "+%d.%m.%Y %H:%M:%S" ) - error: ${ restore_msg } , adlist backup restored " >> " ${ adb_logfile } "
cp -f " ${ adb_backupfile } " " ${ adb_dnsfile } " >/dev/null 2>& 1
f_log "adblocklist backup restored"
else
else
> " ${ adb_dnsfile } "
> " ${ adb_dnsfile } "
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: ${ restore_msg } , empty adlist generated "
printf "%s\n" " $( /bin/date "+%d.%m.%Y %H:%M:%S" ) - error: ${ restore_msg } , empty adlist generated " >> " ${ adb_logfile } "
f_log = "empty adblocklist generated"
fi
fi
# restart dnsmasq
#
/etc/init.d/dnsmasq restart >/dev/null 2>& 1
/etc/init.d/dnsmasq restart >/dev/null 2>& 1
# remove files and exit
#
f_remove
f_remove
exit 100
}
}
#######################################################
#######################################################
@ -445,35 +484,35 @@ f_restore()
#
#
f_wancheck( )
f_wancheck( )
{
{
if [ " ${ wan_ok } " = "true" ]
then
# wait for wan update interface(s)
#
while [ $(( cnt)) -le $(( max_cnt)) ]
local cnt = 0
local cnt_max = " ${ 1 } "
local dev
local dev_out
while [ $(( cnt)) -le $(( cnt_max)) ]
do
for dev in ${ adb_wandev }
do
do
for dev in ${ adb_wandev }
do
if [ -d " /sys/class/net/ ${ dev } " ]
then
dev_out = " $( cat /sys/class/net/${ dev } /operstate 2>/dev/null) "
if [ " ${ dev_out } " = "up" ]
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: get wan/update interface: ${ dev } , after ${ cnt } loops "
break 2
fi
fi
if [ $(( cnt)) -eq $(( max_cnt)) ]
if [ -d " /sys/class/net/ ${ dev } " ]
then
dev_out = " $( cat /sys/class/net/${ dev } /operstate 2>/dev/null) "
rc = ${ ? }
if [ " ${ dev_out } " = "up" ]
then
then
wan_ok = "false"
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: no wan/update interface(s) found ( ${ adb_wandev } ) "
printf "%s\n" " $( /bin/date "+%d.%m.%Y %H:%M:%S" ) - error: no wan/update interface(s) found ( ${ adb_wandev } ) " >> " ${ adb_logfile } "
restore_msg = "no wan/update interface(s)"
f_restore
wan_ok = "true"
f_log " get wan/update interface ( ${ dev } ), after ${ cnt } loops "
break 2
fi
fi
done
sleep 1
cnt = $(( cnt + 1 ))
fi
done
done
sleep 1
cnt = $(( cnt + 1 ))
done
if [ -z " ${ wan_ok } " ]
then
rc = 580
wan_ok = "false"
f_log " no wan/update interface(s) found ( ${ adb_wandev # } ) " " ${ rc } "
f_restore
fi
fi
}
}
@ -482,72 +521,63 @@ f_wancheck()
#
#
f_ntpcheck( )
f_ntpcheck( )
{
{
if [ " ${ ntp_ok } " = "true" ]
local cnt = 0
local cnt_max = " ${ 1 } "
local ntp_pool
for srv in ${ adb_ntpsrv }
do
ntp_pool = " ${ ntp_pool } -p ${ srv } "
done
while [ $(( cnt)) -le $(( cnt_max)) ]
do
/usr/sbin/ntpd -nq ${ ntp_pool } >/dev/null 2>& 1
rc = ${ ? }
if [ $(( rc)) -eq 0 ]
then
ntp_ok = "true"
f_log " get ntp time sync ( ${ adb_ntpsrv # } ), after ${ cnt } loops "
break
fi
sleep 1
cnt = $(( cnt + 1 ))
done
if [ -z " ${ ntp_ok } " ]
then
then
# prepare ntp server pool
#
unset ntp_pool
for srv in ${ adb_ntpsrv }
do
ntp_pool = " ${ ntp_pool } -p ${ srv } "
done
# wait for ntp time sync
#
while [ $(( cnt)) -le $(( max_cnt)) ]
do
/usr/sbin/ntpd -nq ${ ntp_pool } >/dev/null 2>& 1
rc = $?
if [ $(( rc)) -eq 0 ]
then
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: get ntp time sync ( ${ adb_ntpsrv } ), after ${ cnt } loops "
break
fi
if [ $(( cnt)) -eq $(( max_cnt)) ]
then
ntp_ok = "false"
/usr/bin/logger -t " adblock[ ${ pid } ] " " error: ntp time sync failed ( ${ adb_ntpsrv } ) "
printf "%s\n" " $( /bin/date "+%d.%m.%Y %H:%M:%S" ) - error: ntp time sync failed ( ${ adb_ntpsrv } ) " >> " ${ adb_logfile } "
restore_msg = "time sync failed"
f_restore
fi
sleep 1
cnt = $(( cnt + 1 ))
done
rc = 585
ntp_ok = "false"
f_log " ntp time sync failed ( ${ adb_ntpsrv # } ) " " ${ rc } "
f_restore
fi
fi
}
}
#################################################################
# f_dnscheck: dnsmasq health check with newly generated blocklist
####################################################################
# f_dnscheck: dnsmasq health check with newly generated adblock list
#
#
f_dnscheck( )
f_dnscheck( )
{
{
# check 1: dnsmasq startup
#
local dns_status
dns_status = " $( logread -l 20 -e "dnsmasq" -e "FAILED to start up" ) "
dns_status = " $( logread -l 20 -e "dnsmasq" -e "FAILED to start up" ) "
rc = ${ ? }
if [ -z " ${ dns_status } " ]
if [ -z " ${ dns_status } " ]
then
then
# check 2: nslookup probe
#
dns_status = " $( nslookup " ${ adb_domain } " 2>/dev/null | grep " ${ adb_ip } " ) "
dns_status = " $( nslookup " ${ adb_domain } " 2>/dev/null | grep " ${ adb_ip } " ) "
rc = ${ ? }
if [ -z " ${ dns_status } " ]
if [ -z " ${ dns_status } " ]
then
then
# create backup of new block list only, if both checks are OK and backup enabled
#
if [ " ${ backup_ok } " = "true" ]
if [ " ${ backup_ok } " = "true" ]
then
then
cp -f " ${ adb_dnsfile } " " ${ adb_backupfile } " 2 >/dev/null
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: new block list with ${ adb_count } domains loaded, backup generated "
cp -f " ${ adb_dnsfile } " " ${ adb_backupfile } " >/dev/null 2>& 1
f_log " new adblock list with ${ adb_count } domains loaded, backup generated "
else
else
/usr/bin/logger -t " adblock[ ${ pid } ] " " info: new block list with ${ adb_count } domains loaded, no backup "
f_log " new adblock list with ${ adb_count } domains loaded, no backup "
fi
fi
else
else
restore_msg = "nslookup probe failed "
f_log "nslookup probe failed" " ${ rc } "
f_restore
f_restore
fi
fi
else
else
restore_msg = "dnsmasq probe failed "
f_restore
f_log "dnsmasq probe failed" " ${ rc } "
f_restore
fi
fi
}
}
@ -556,9 +586,10 @@ f_dnscheck()
#
#
f_footer( )
f_footer( )
{
{
local url
adb_count = " $( wc -l < " ${ adb_dnsfile } " ) "
adb_count = " $( wc -l < " ${ adb_dnsfile } " ) "
printf "%s\n" "###################################################" >> " ${ adb_dnsfile } "
printf "%s\n" " # last adblock file update: $( date +"%d.%m.%Y - %T" ) " >> " ${ adb_dnsfile } "
printf "%s\n" "#################################################### " >> " ${ adb_dnsfile } "
printf "%s\n" " # last adblock list update: $( date +"%d.%m.%Y - %T" ) " >> " ${ adb_dnsfile } "
printf "%s\n" " # ${ 0 ##*/ } ( ${ adb_version } ) - ${ adb_count } ad/abuse domains blocked " >> " ${ adb_dnsfile } "
printf "%s\n" " # ${ 0 ##*/ } ( ${ adb_version } ) - ${ adb_count } ad/abuse domains blocked " >> " ${ adb_dnsfile } "
printf "%s\n" "# domain blacklist sources:" >> " ${ adb_dnsfile } "
printf "%s\n" "# domain blacklist sources:" >> " ${ adb_dnsfile } "
for src in ${ adb_sources }
for src in ${ adb_sources }
@ -566,8 +597,7 @@ f_footer()
url = " ${ src // \& ruleset =*/ } "
url = " ${ src // \& ruleset =*/ } "
printf "%s\n" " # ${ url } " >> " ${ adb_dnsfile } "
printf "%s\n" " # ${ url } " >> " ${ adb_dnsfile } "
done
done
printf "%s\n" "###################################################" >> " ${ adb_dnsfile } "
printf "%s\n" "# domain whitelist source:" >> " ${ adb_dnsfile } "
printf "%s\n" "#####" >> " ${ adb_dnsfile } "
printf "%s\n" " # ${ adb_whitelist } " >> " ${ adb_dnsfile } "
printf "%s\n" " # ${ adb_whitelist } " >> " ${ adb_dnsfile } "
printf "%s\n" "###################################################" >> " ${ adb_dnsfile } "
printf "%s\n" "#################################################### " >> " ${ adb_dnsfile } "
}
}