|
@@ -15,9 +15,9 @@ f_envload()
|
15
|
15
|
then
|
16
|
16
|
. /lib/functions.sh
|
17
|
17
|
else
|
18
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: openwrt function library not found"
|
|
18
|
+ rc=510
|
|
19
|
+ f_log "openwrt function library not found" "${rc}"
|
19
|
20
|
f_deltemp
|
20
|
|
- exit 10
|
21
|
21
|
fi
|
22
|
22
|
|
23
|
23
|
# source in openwrt json helpers library
|
|
@@ -26,9 +26,9 @@ f_envload()
|
26
|
26
|
then
|
27
|
27
|
. "/usr/share/libubox/jshn.sh"
|
28
|
28
|
else
|
29
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: openwrt json helpers library not found"
|
|
29
|
+ rc=515
|
|
30
|
+ f_log "openwrt json helpers library not found" "${rc}"
|
30
|
31
|
f_deltemp
|
31
|
|
- exit 15
|
32
|
32
|
fi
|
33
|
33
|
|
34
|
34
|
# get list with all installed openwrt packages
|
|
@@ -36,9 +36,9 @@ f_envload()
|
36
|
36
|
pkg_list="$(opkg list-installed 2>/dev/null)"
|
37
|
37
|
if [ -z "${pkg_list}" ]
|
38
|
38
|
then
|
39
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: empty openwrt package list"
|
|
39
|
+ rc=520
|
|
40
|
+ f_log "empty openwrt package list" "${rc}"
|
40
|
41
|
f_deltemp
|
41
|
|
- exit 20
|
42
|
42
|
fi
|
43
|
43
|
}
|
44
|
44
|
|
|
@@ -133,11 +133,8 @@ f_envparse()
|
133
|
133
|
|
134
|
134
|
# set temp variables and counter
|
135
|
135
|
#
|
136
|
|
- adb_tmpfile="$(mktemp -tu)"
|
137
|
|
- adb_tmpdir="$(mktemp -d)"
|
138
|
|
- cnt=0
|
139
|
|
- max_cnt=30
|
140
|
|
- max_time=60
|
|
136
|
+ adb_tmpfile="$(mktemp -tu 2>/dev/null)"
|
|
137
|
+ adb_tmpdir="$(mktemp -d 2>/dev/null)"
|
141
|
138
|
|
142
|
139
|
# set adblock source ruleset definitions
|
143
|
140
|
#
|
|
@@ -160,13 +157,26 @@ f_envparse()
|
160
|
157
|
#
|
161
|
158
|
f_envcheck()
|
162
|
159
|
{
|
|
160
|
+ # check required config variables
|
|
161
|
+ #
|
|
162
|
+ adb_varlist="adb_ip adb_dev adb_if adb_domain adb_minspace adb_maxloop adb_maxtime adb_blacklist adb_whitelist"
|
|
163
|
+ for var in ${adb_varlist}
|
|
164
|
+ do
|
|
165
|
+ if [ -z "$(eval printf \"\$"${var}"\")" ]
|
|
166
|
+ then
|
|
167
|
+ rc=525
|
|
168
|
+ f_log "missing adblock config option (${var})" "${rc}"
|
|
169
|
+ f_deltemp
|
|
170
|
+ fi
|
|
171
|
+ done
|
|
172
|
+
|
163
|
173
|
# check adblock network device configuration
|
164
|
174
|
#
|
165
|
175
|
if [ ! -d "/sys/class/net/${adb_dev}" ]
|
166
|
176
|
then
|
167
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: invalid adblock network device input (${adb_dev})"
|
|
177
|
+ rc=530
|
|
178
|
+ f_log "invalid adblock network device input (${adb_dev})" "${rc}"
|
168
|
179
|
f_deltemp
|
169
|
|
- exit 25
|
170
|
180
|
fi
|
171
|
181
|
|
172
|
182
|
# check adblock network interface configuration
|
|
@@ -175,9 +185,9 @@ f_envcheck()
|
175
|
185
|
banned_if="$(printf "${adb_if}" | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p')"
|
176
|
186
|
if [ -n "${check_if}" ] || [ -n "${banned_if}" ]
|
177
|
187
|
then
|
178
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: invalid adblock network interface input (${adb_if})"
|
|
188
|
+ rc=535
|
|
189
|
+ f_log "invalid adblock network interface input (${adb_if})" "${rc}"
|
179
|
190
|
f_deltemp
|
180
|
|
- exit 30
|
181
|
191
|
fi
|
182
|
192
|
|
183
|
193
|
# check adblock ip address configuration
|
|
@@ -185,43 +195,56 @@ f_envcheck()
|
185
|
195
|
check_ip="$(printf "${adb_ip}" | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p')"
|
186
|
196
|
if [ -z "${check_ip}" ]
|
187
|
197
|
then
|
188
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: invalid adblock ip address input (${adb_ip})"
|
|
198
|
+ rc=540
|
|
199
|
+ f_log "invalid adblock ip address input (${adb_ip})" "${rc}"
|
189
|
200
|
f_deltemp
|
190
|
|
- exit 35
|
191
|
201
|
fi
|
192
|
202
|
|
193
|
203
|
# check adblock blacklist/whitelist configuration
|
194
|
204
|
#
|
195
|
205
|
if [ ! -r "${adb_blacklist}" ]
|
196
|
206
|
then
|
197
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: adblock blacklist not found"
|
|
207
|
+ rc=545
|
|
208
|
+ f_log "adblock blacklist not found" "${rc}"
|
198
|
209
|
f_deltemp
|
199
|
|
- exit 40
|
200
|
210
|
elif [ ! -r "${adb_whitelist}" ]
|
201
|
211
|
then
|
202
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: adblock whitelist not found"
|
|
212
|
+ rc=550
|
|
213
|
+ f_log "adblock whitelist not found" "${rc}"
|
203
|
214
|
f_deltemp
|
204
|
|
- exit 45
|
205
|
215
|
fi
|
206
|
216
|
|
207
|
|
- # check wan update configuration
|
|
217
|
+ # check adblock temp directory
|
208
|
218
|
#
|
209
|
|
- if [ -n "${adb_wandev}" ]
|
|
219
|
+ if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
|
210
|
220
|
then
|
211
|
|
- wan_ok="true"
|
|
221
|
+ f_space "${adb_tmpdir}"
|
|
222
|
+ tmp_ok="true"
|
212
|
223
|
else
|
213
|
|
- wan_ok="false"
|
214
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: wan update check will be disabled"
|
|
224
|
+ rc=555
|
|
225
|
+ tmp_ok="false"
|
|
226
|
+ f_log "temp directory not found" "${rc}"
|
|
227
|
+ f_deltemp
|
215
|
228
|
fi
|
216
|
229
|
|
217
|
|
- # check ntp sync configuration
|
|
230
|
+ # check curl package dependency
|
218
|
231
|
#
|
219
|
|
- if [ -n "${adb_ntpsrv}" ]
|
|
232
|
+ check="$(printf "${pkg_list}" | grep "^curl")"
|
|
233
|
+ if [ -z "${check}" ]
|
220
|
234
|
then
|
221
|
|
- ntp_ok="true"
|
222
|
|
- else
|
223
|
|
- ntp_ok="false"
|
224
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: ntp time sync will be disabled"
|
|
235
|
+ rc=560
|
|
236
|
+ f_log "curl package not found" "${rc}"
|
|
237
|
+ f_deltemp
|
|
238
|
+ fi
|
|
239
|
+
|
|
240
|
+ # check wget package dependency
|
|
241
|
+ #
|
|
242
|
+ check="$(printf "${pkg_list}" | grep "^wget")"
|
|
243
|
+ if [ -z "${check}" ]
|
|
244
|
+ then
|
|
245
|
+ rc=565
|
|
246
|
+ f_log "wget package not found" "${rc}"
|
|
247
|
+ f_deltemp
|
225
|
248
|
fi
|
226
|
249
|
|
227
|
250
|
# check backup configuration
|
|
@@ -229,34 +252,17 @@ f_envcheck()
|
229
|
252
|
adb_backupdir="${adb_backupfile%/*}"
|
230
|
253
|
if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
|
231
|
254
|
then
|
|
255
|
+ f_space "${adb_backupdir}"
|
232
|
256
|
backup_ok="true"
|
233
|
|
- adb_mounts="${adb_backupdir} ${adb_tmpdir}"
|
234
|
257
|
else
|
235
|
258
|
backup_ok="false"
|
236
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: backup/restore will be disabled"
|
237
|
|
- fi
|
238
|
|
-
|
239
|
|
- # check error log configuration
|
240
|
|
- #
|
241
|
|
- adb_logdir="${adb_logfile%/*}"
|
242
|
|
- if [ -n "${adb_logfile}" ] && [ "${adb_logfile}" = "/dev/stdout" ]
|
243
|
|
- then
|
244
|
|
- log_ok="true"
|
245
|
|
- adb_logfile="/proc/self/fd/1"
|
246
|
|
- elif [ -n "${adb_logdir}" ] && [ -d "${adb_logdir}" ] && [ "${ntp_ok}" = "true" ]
|
247
|
|
- then
|
248
|
|
- log_ok="true"
|
249
|
|
- adb_mounts="${adb_mounts} ${adb_logdir}"
|
250
|
|
- else
|
251
|
|
- log_ok="false"
|
252
|
|
- adb_logfile="/dev/null"
|
253
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: error logging will be disabled"
|
|
259
|
+ f_log "backup/restore will be disabled"
|
254
|
260
|
fi
|
255
|
261
|
|
256
|
262
|
# check dns query log configuration
|
257
|
263
|
#
|
258
|
264
|
adb_querydir="${adb_queryfile%/*}"
|
259
|
|
- query_pid="/var/run/adb_query.pid"
|
|
265
|
+ adb_querypid="/var/run/adb_query.pid"
|
260
|
266
|
if [ -n "${adb_querydir}" ] && [ -d "${adb_querydir}" ]
|
261
|
267
|
then
|
262
|
268
|
# check find capabilities
|
|
@@ -265,77 +271,59 @@ f_envcheck()
|
265
|
271
|
if [ -z "${check}" ]
|
266
|
272
|
then
|
267
|
273
|
query_ok="false"
|
268
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: busybox without 'find/mtime' support (min. r47362), dns query logging will be disabled"
|
|
274
|
+ f_log "busybox without 'find/mtime' support (min. r47362), dns query logging will be disabled"
|
269
|
275
|
else
|
|
276
|
+ f_space "${adb_querydir}"
|
270
|
277
|
query_ok="true"
|
271
|
278
|
query_name="${adb_queryfile##*/}"
|
272
|
279
|
query_ip="${adb_ip//./\\.}"
|
273
|
|
- adb_mounts="${adb_mounts} ${adb_querydir}"
|
274
|
280
|
fi
|
275
|
281
|
else
|
276
|
282
|
query_ok="false"
|
277
|
|
- if [ -s "${query_pid}" ]
|
|
283
|
+ f_log "dns query logging will be disabled"
|
|
284
|
+ if [ -s "${adb_querypid}" ]
|
278
|
285
|
then
|
279
|
|
- kill -9 $(cat "${query_pid}") 2>/dev/null
|
280
|
|
- > "${query_pid}"
|
281
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: remove old dns query log background process"
|
|
286
|
+ kill -9 "$(cat "${adb_querypid}")" >/dev/null 2>&1
|
|
287
|
+ f_log "remove old dns query log background process (pid: $(cat "${adb_querypid}"))"
|
|
288
|
+ > "${adb_querypid}"
|
282
|
289
|
fi
|
283
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: dns query logging will be disabled"
|
284
|
290
|
fi
|
285
|
291
|
|
286
|
|
- # check mount points & space requirements
|
|
292
|
+ # check debug log configuration
|
287
|
293
|
#
|
288
|
|
- adb_mounts="${adb_mounts} ${adb_tmpdir}"
|
289
|
|
- for mp in ${adb_mounts}
|
290
|
|
- do
|
291
|
|
- df "${mp}" 2>/dev/null |\
|
292
|
|
- tail -n1 |\
|
293
|
|
- while read filesystem overall used available scrap
|
294
|
|
- do
|
295
|
|
- av_space="${available}"
|
296
|
|
- if [ $((av_space)) -eq 0 ]
|
297
|
|
- then
|
298
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: no space left on device/not mounted (${mp})"
|
299
|
|
- exit 50
|
300
|
|
- elif [ $((av_space)) -lt $((adb_minspace)) ]
|
301
|
|
- then
|
302
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: not enough space left on device (${mp})"
|
303
|
|
- exit 55
|
304
|
|
- fi
|
305
|
|
- done
|
306
|
|
- # subshell return code handling
|
307
|
|
- #
|
308
|
|
- rc=$?
|
309
|
|
- if [ $((rc)) -ne 0 ]
|
310
|
|
- then
|
311
|
|
- f_deltemp
|
312
|
|
- exit ${rc}
|
313
|
|
- fi
|
314
|
|
- done
|
|
294
|
+ adb_logdir="${adb_logfile%/*}"
|
|
295
|
+ if [ -n "${adb_logdir}" ] && [ -d "${adb_logdir}" ]
|
|
296
|
+ then
|
|
297
|
+ f_space "${adb_logdir}"
|
|
298
|
+ log_ok="true"
|
|
299
|
+ else
|
|
300
|
+ log_ok="false"
|
|
301
|
+ f_log "debug logging will be disabled"
|
|
302
|
+ fi
|
315
|
303
|
|
316
|
|
- # check curl package dependency
|
|
304
|
+ # check wan update configuration
|
317
|
305
|
#
|
318
|
|
- check="$(printf "${pkg_list}" | grep "^curl")"
|
319
|
|
- if [ -z "${check}" ]
|
|
306
|
+ if [ -n "${adb_wandev}" ]
|
320
|
307
|
then
|
321
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: curl package not found"
|
322
|
|
- f_deltemp
|
323
|
|
- exit 60
|
|
308
|
+ f_wancheck "${adb_maxloop}"
|
|
309
|
+ else
|
|
310
|
+ wan_ok="false"
|
|
311
|
+ f_log "wan update check will be disabled"
|
324
|
312
|
fi
|
325
|
313
|
|
326
|
|
- # check wget package dependency
|
|
314
|
+ # check ntp sync configuration
|
327
|
315
|
#
|
328
|
|
- check="$(printf "${pkg_list}" | grep "^wget")"
|
329
|
|
- if [ -z "${check}" ]
|
|
316
|
+ if [ -n "${adb_ntpsrv}" ]
|
330
|
317
|
then
|
331
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: wget package not found"
|
332
|
|
- f_deltemp
|
333
|
|
- exit 65
|
|
318
|
+ f_ntpcheck "${adb_maxloop}"
|
|
319
|
+ else
|
|
320
|
+ ntp_ok="false"
|
|
321
|
+ f_log "ntp time sync will be disabled"
|
334
|
322
|
fi
|
335
|
323
|
|
336
|
324
|
# check dynamic/volatile adblock network interface configuration
|
337
|
325
|
#
|
338
|
|
- rc="$(ifstatus "${adb_if}" >/dev/null 2>&1; printf $?)"
|
|
326
|
+ rc="$(ifstatus "${adb_if}" >/dev/null 2>&1; printf ${?})"
|
339
|
327
|
if [ $((rc)) -ne 0 ]
|
340
|
328
|
then
|
341
|
329
|
json_init
|
|
@@ -347,97 +335,148 @@ f_envcheck()
|
347
|
335
|
json_close_array
|
348
|
336
|
json_close_object
|
349
|
337
|
ubus call network add_dynamic "$(json_dump)"
|
350
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: created new dynamic/volatile network interface (${adb_if}, ${adb_ip})"
|
|
338
|
+ rc=${?}
|
|
339
|
+ if [ $((rc)) -eq 0 ]
|
|
340
|
+ then
|
|
341
|
+ f_log "created new dynamic/volatile network interface (${adb_if}, ${adb_ip})"
|
|
342
|
+ else
|
|
343
|
+ f_log "failed to initialize new dynamic/volatile network interface (${adb_if}, ${adb_ip})" "${rc}"
|
|
344
|
+ f_deltemp
|
|
345
|
+ fi
|
351
|
346
|
fi
|
352
|
347
|
|
353
|
348
|
# check dynamic/volatile adblock uhttpd instance configuration
|
354
|
349
|
#
|
355
|
|
- rc="$(ps | grep "[u]httpd.*\-r ${adb_if}" >/dev/null 2>&1; printf $?)"
|
|
350
|
+ rc="$(ps | grep "[u]httpd.*\-r ${adb_if}" >/dev/null 2>&1; printf ${?})"
|
356
|
351
|
if [ $((rc)) -ne 0 ]
|
357
|
352
|
then
|
358
|
|
- uhttpd -h "/www/adblock" -r "${adb_if}" -E "/adblock.html" -p "${adb_ip}:80"
|
359
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: created new dynamic/volatile uhttpd instance (${adb_if}, ${adb_ip})"
|
|
353
|
+ uhttpd -h "/www/adblock" -r "${adb_if}" -E "/adblock.html" -p "${adb_ip}:80" >/dev/null 2>&1
|
|
354
|
+ rc=${?}
|
|
355
|
+ if [ $((rc)) -eq 0 ]
|
|
356
|
+ then
|
|
357
|
+ f_log "created new dynamic/volatile uhttpd instance (${adb_if}, ${adb_ip})"
|
|
358
|
+ else
|
|
359
|
+ f_log "failed to initialize new dynamic/volatile uhttpd instance (${adb_if}, ${adb_ip})" "${rc}"
|
|
360
|
+ f_deltemp
|
|
361
|
+ fi
|
|
362
|
+ fi
|
|
363
|
+}
|
|
364
|
+
|
|
365
|
+################################################
|
|
366
|
+# f_log: log messages to stdout, syslog, logfile
|
|
367
|
+#
|
|
368
|
+f_log()
|
|
369
|
+{
|
|
370
|
+ local log_msg="${1}"
|
|
371
|
+ local log_rc="${2}"
|
|
372
|
+ local class="info "
|
|
373
|
+ if [ -n "${log_msg}" ]
|
|
374
|
+ then
|
|
375
|
+ if [ $((log_rc)) -ne 0 ]
|
|
376
|
+ then
|
|
377
|
+ class="error"
|
|
378
|
+ log_rc=", rc: ${log_rc}"
|
|
379
|
+ fi
|
|
380
|
+ /usr/bin/logger -s -t "adblock[${pid}] ${class}" "${log_msg}${log_rc}"
|
|
381
|
+ if [ "${log_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
|
|
382
|
+ then
|
|
383
|
+ printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") adblock[${pid}] ${class}: ${log_msg}${log_rc}" >> "${adb_logfile}"
|
|
384
|
+ fi
|
|
385
|
+ fi
|
|
386
|
+}
|
|
387
|
+
|
|
388
|
+################################################
|
|
389
|
+# f_space: check mount points/space requirements
|
|
390
|
+#
|
|
391
|
+f_space()
|
|
392
|
+{
|
|
393
|
+ local mp="${1}"
|
|
394
|
+ if [ -d "${mp}" ]
|
|
395
|
+ then
|
|
396
|
+ df "${mp}" 2>/dev/null |\
|
|
397
|
+ tail -n1 |\
|
|
398
|
+ while read filesystem overall used available scrap
|
|
399
|
+ do
|
|
400
|
+ av_space="${available}"
|
|
401
|
+ if [ $((av_space)) -eq 0 ]
|
|
402
|
+ then
|
|
403
|
+ rc=570
|
|
404
|
+ f_log "no space left on device/not mounted (${mp})" "${rc}"
|
|
405
|
+ exit ${rc}
|
|
406
|
+ elif [ $((av_space)) -lt $((adb_minspace)) ]
|
|
407
|
+ then
|
|
408
|
+ rc=575
|
|
409
|
+ f_log "not enough space left on device (${mp})" "${rc}"
|
|
410
|
+ exit ${rc}
|
|
411
|
+ fi
|
|
412
|
+ done
|
|
413
|
+ rc=${?}
|
|
414
|
+ if [ $((rc)) -eq 0 ]
|
|
415
|
+ then
|
|
416
|
+ space_ok="true"
|
|
417
|
+ else
|
|
418
|
+ space_ok="false"
|
|
419
|
+ f_deltemp
|
|
420
|
+ fi
|
360
|
421
|
fi
|
361
|
422
|
}
|
362
|
423
|
|
363
|
|
-###################################################
|
364
|
|
-# f_deltemp: delete temporary files and directories
|
|
424
|
+####################################################
|
|
425
|
+# f_deltemp: delete temp files, directories and exit
|
365
|
426
|
#
|
366
|
427
|
f_deltemp()
|
367
|
428
|
{
|
368
|
429
|
if [ -f "${adb_tmpfile}" ]
|
369
|
430
|
then
|
370
|
|
- rm -f "${adb_tmpfile}" 2>/dev/null
|
|
431
|
+ rm -f "${adb_tmpfile}" >/dev/null 2>&1
|
371
|
432
|
fi
|
372
|
433
|
if [ -d "${adb_tmpdir}" ]
|
373
|
434
|
then
|
374
|
|
- rm -rf "${adb_tmpdir}" 2>/dev/null
|
|
435
|
+ rm -rf "${adb_tmpdir}" >/dev/null 2>&1
|
375
|
436
|
fi
|
|
437
|
+ f_log "domain adblock processing finished (${adb_version})"
|
|
438
|
+ exit ${rc}
|
376
|
439
|
}
|
377
|
440
|
|
378
|
|
-################################################################
|
379
|
|
-# f_remove: remove temporary files, start and maintain query log
|
|
441
|
+####################################################
|
|
442
|
+# f_remove: maintain and (re-)start domain query log
|
380
|
443
|
#
|
381
|
444
|
f_remove()
|
382
|
445
|
{
|
383
|
|
- # delete temporary files and directories
|
384
|
|
- #
|
385
|
|
- f_deltemp
|
386
|
|
-
|
387
|
|
- # remove existing domain query log background process,
|
388
|
|
- # do housekeeping and start a new process on daily basis
|
389
|
|
- #
|
390
|
446
|
if [ "${query_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
|
391
|
447
|
then
|
392
|
448
|
query_date="$(date "+%Y%m%d")"
|
393
|
|
- if [ -s "${query_pid}" ] && [ ! -f "${adb_queryfile}.${query_date}" ]
|
|
449
|
+ if [ -s "${adb_querypid}" ] && [ ! -f "${adb_queryfile}.${query_date}" ]
|
394
|
450
|
then
|
395
|
|
- kill -9 $(cat "${query_pid}") 2>/dev/null
|
396
|
|
- > "${query_pid}"
|
397
|
|
- find "${adb_backupdir}" -maxdepth 1 -type f -mtime +${adb_queryhistory} -name "${query_name}.*" -exec rm -f {} \; 2>/dev/null
|
398
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: remove old dns query log background process and do logfile housekeeping"
|
|
451
|
+ kill -9 "$(cat "${adb_querypid}")" >/dev/null 2>&1
|
|
452
|
+ find "${adb_backupdir}" -maxdepth 1 -type f -mtime +"${adb_queryhistory}" -name "${query_name}.*" -exec rm -f {} \; 2>/dev/null
|
|
453
|
+ f_log "remove old dns query log background process (pid: $(cat "${adb_querypid}")) and do logfile housekeeping"
|
|
454
|
+ > "${adb_querypid}"
|
399
|
455
|
fi
|
400
|
|
- if [ ! -s "${query_pid}" ]
|
|
456
|
+ if [ ! -s "${adb_querypid}" ]
|
401
|
457
|
then
|
402
|
|
- ( logread -f 2>/dev/null & printf "$!" > "${query_pid}" ) | egrep -o "(query\[A\].*)|([a-z0-9\.\-]* is ${query_ip}$)" >> "${adb_queryfile}.${query_date}" &
|
403
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: start new domain query log background process"
|
|
458
|
+ ( logread -f 2>/dev/null & printf ${!} > "${adb_querypid}" ) | egrep -o "(query\[A\].*)|([a-z0-9\.\-]* is ${query_ip}$)" >> "${adb_queryfile}.${query_date}" &
|
|
459
|
+ f_log "new domain query log background process started (pid: $(cat "${adb_querypid}"))"
|
404
|
460
|
fi
|
405
|
461
|
fi
|
406
|
|
-
|
407
|
|
- # final log entry
|
408
|
|
- #
|
409
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: domain adblock processing finished (${adb_version})"
|
|
462
|
+ f_deltemp
|
410
|
463
|
}
|
411
|
464
|
|
412
|
|
-#####################################################
|
413
|
|
-# f_restore: if available, restore last adlist backup
|
|
465
|
+################################################################
|
|
466
|
+# f_restore: restore last adblocklist backup and restart dnsmasq
|
414
|
467
|
#
|
415
|
468
|
f_restore()
|
416
|
469
|
{
|
417
|
|
- if [ -z "${restore_msg}" ]
|
418
|
|
- then
|
419
|
|
- restore_msg="unknown"
|
420
|
|
- fi
|
421
|
|
-
|
422
|
470
|
if [ "${backup_ok}" = "true" ] && [ -f "${adb_backupfile}" ]
|
423
|
471
|
then
|
424
|
|
- cp -f "${adb_backupfile}" "${adb_dnsfile}" 2>/dev/null
|
425
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: ${restore_msg}, adlist backup restored"
|
426
|
|
- printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, adlist backup restored" >> "${adb_logfile}"
|
|
472
|
+ cp -f "${adb_backupfile}" "${adb_dnsfile}" >/dev/null 2>&1
|
|
473
|
+ f_log "adblocklist backup restored"
|
427
|
474
|
else
|
428
|
475
|
> "${adb_dnsfile}"
|
429
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: ${restore_msg}, empty adlist generated"
|
430
|
|
- printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, empty adlist generated" >> "${adb_logfile}"
|
|
476
|
+ f_log="empty adblocklist generated"
|
431
|
477
|
fi
|
432
|
|
-
|
433
|
|
- # restart dnsmasq
|
434
|
|
- #
|
435
|
478
|
/etc/init.d/dnsmasq restart >/dev/null 2>&1
|
436
|
|
-
|
437
|
|
- # remove files and exit
|
438
|
|
- #
|
439
|
479
|
f_remove
|
440
|
|
- exit 100
|
441
|
480
|
}
|
442
|
481
|
|
443
|
482
|
#######################################################
|
|
@@ -445,35 +484,35 @@ f_restore()
|
445
|
484
|
#
|
446
|
485
|
f_wancheck()
|
447
|
486
|
{
|
448
|
|
- if [ "${wan_ok}" = "true" ]
|
449
|
|
- then
|
450
|
|
- # wait for wan update interface(s)
|
451
|
|
- #
|
452
|
|
- while [ $((cnt)) -le $((max_cnt)) ]
|
|
487
|
+ local cnt=0
|
|
488
|
+ local cnt_max="${1}"
|
|
489
|
+ local dev
|
|
490
|
+ local dev_out
|
|
491
|
+ while [ $((cnt)) -le $((cnt_max)) ]
|
|
492
|
+ do
|
|
493
|
+ for dev in ${adb_wandev}
|
453
|
494
|
do
|
454
|
|
- for dev in ${adb_wandev}
|
455
|
|
- do
|
456
|
|
- if [ -d "/sys/class/net/${dev}" ]
|
457
|
|
- then
|
458
|
|
- dev_out="$(cat /sys/class/net/${dev}/operstate 2>/dev/null)"
|
459
|
|
- if [ "${dev_out}" = "up" ]
|
460
|
|
- then
|
461
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: get wan/update interface: ${dev}, after ${cnt} loops"
|
462
|
|
- break 2
|
463
|
|
- fi
|
464
|
|
- fi
|
465
|
|
- if [ $((cnt)) -eq $((max_cnt)) ]
|
|
495
|
+ if [ -d "/sys/class/net/${dev}" ]
|
|
496
|
+ then
|
|
497
|
+ dev_out="$(cat /sys/class/net/${dev}/operstate 2>/dev/null)"
|
|
498
|
+ rc=${?}
|
|
499
|
+ if [ "${dev_out}" = "up" ]
|
466
|
500
|
then
|
467
|
|
- wan_ok="false"
|
468
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: no wan/update interface(s) found (${adb_wandev})"
|
469
|
|
- printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: no wan/update interface(s) found (${adb_wandev})" >> "${adb_logfile}"
|
470
|
|
- restore_msg="no wan/update interface(s)"
|
471
|
|
- f_restore
|
|
501
|
+ wan_ok="true"
|
|
502
|
+ f_log "get wan/update interface (${dev}), after ${cnt} loops"
|
|
503
|
+ break 2
|
472
|
504
|
fi
|
473
|
|
- done
|
474
|
|
- sleep 1
|
475
|
|
- cnt=$((cnt + 1))
|
|
505
|
+ fi
|
476
|
506
|
done
|
|
507
|
+ sleep 1
|
|
508
|
+ cnt=$((cnt + 1))
|
|
509
|
+ done
|
|
510
|
+ if [ -z "${wan_ok}" ]
|
|
511
|
+ then
|
|
512
|
+ rc=580
|
|
513
|
+ wan_ok="false"
|
|
514
|
+ f_log "no wan/update interface(s) found (${adb_wandev# })" "${rc}"
|
|
515
|
+ f_restore
|
477
|
516
|
fi
|
478
|
517
|
}
|
479
|
518
|
|
|
@@ -482,72 +521,63 @@ f_wancheck()
|
482
|
521
|
#
|
483
|
522
|
f_ntpcheck()
|
484
|
523
|
{
|
485
|
|
- if [ "${ntp_ok}" = "true" ]
|
|
524
|
+ local cnt=0
|
|
525
|
+ local cnt_max="${1}"
|
|
526
|
+ local ntp_pool
|
|
527
|
+ for srv in ${adb_ntpsrv}
|
|
528
|
+ do
|
|
529
|
+ ntp_pool="${ntp_pool} -p ${srv}"
|
|
530
|
+ done
|
|
531
|
+ while [ $((cnt)) -le $((cnt_max)) ]
|
|
532
|
+ do
|
|
533
|
+ /usr/sbin/ntpd -nq ${ntp_pool} >/dev/null 2>&1
|
|
534
|
+ rc=${?}
|
|
535
|
+ if [ $((rc)) -eq 0 ]
|
|
536
|
+ then
|
|
537
|
+ ntp_ok="true"
|
|
538
|
+ f_log "get ntp time sync (${adb_ntpsrv# }), after ${cnt} loops"
|
|
539
|
+ break
|
|
540
|
+ fi
|
|
541
|
+ sleep 1
|
|
542
|
+ cnt=$((cnt + 1))
|
|
543
|
+ done
|
|
544
|
+ if [ -z "${ntp_ok}" ]
|
486
|
545
|
then
|
487
|
|
- # prepare ntp server pool
|
488
|
|
- #
|
489
|
|
- unset ntp_pool
|
490
|
|
- for srv in ${adb_ntpsrv}
|
491
|
|
- do
|
492
|
|
- ntp_pool="${ntp_pool} -p ${srv}"
|
493
|
|
- done
|
494
|
|
-
|
495
|
|
- # wait for ntp time sync
|
496
|
|
- #
|
497
|
|
- while [ $((cnt)) -le $((max_cnt)) ]
|
498
|
|
- do
|
499
|
|
- /usr/sbin/ntpd -nq ${ntp_pool} >/dev/null 2>&1
|
500
|
|
- rc=$?
|
501
|
|
- if [ $((rc)) -eq 0 ]
|
502
|
|
- then
|
503
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: get ntp time sync (${adb_ntpsrv}), after ${cnt} loops"
|
504
|
|
- break
|
505
|
|
- fi
|
506
|
|
- if [ $((cnt)) -eq $((max_cnt)) ]
|
507
|
|
- then
|
508
|
|
- ntp_ok="false"
|
509
|
|
- /usr/bin/logger -t "adblock[${pid}]" "error: ntp time sync failed (${adb_ntpsrv})"
|
510
|
|
- printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ntp time sync failed (${adb_ntpsrv})" >> "${adb_logfile}"
|
511
|
|
- restore_msg="time sync failed"
|
512
|
|
- f_restore
|
513
|
|
- fi
|
514
|
|
- sleep 1
|
515
|
|
- cnt=$((cnt + 1))
|
516
|
|
- done
|
|
546
|
+ rc=585
|
|
547
|
+ ntp_ok="false"
|
|
548
|
+ f_log "ntp time sync failed (${adb_ntpsrv# })" "${rc}"
|
|
549
|
+ f_restore
|
517
|
550
|
fi
|
518
|
551
|
}
|
519
|
552
|
|
520
|
|
-#################################################################
|
521
|
|
-# f_dnscheck: dnsmasq health check with newly generated blocklist
|
|
553
|
+####################################################################
|
|
554
|
+# f_dnscheck: dnsmasq health check with newly generated adblock list
|
522
|
555
|
#
|
523
|
556
|
f_dnscheck()
|
524
|
557
|
{
|
525
|
|
- # check 1: dnsmasq startup
|
526
|
|
- #
|
|
558
|
+ local dns_status
|
527
|
559
|
dns_status="$(logread -l 20 -e "dnsmasq" -e "FAILED to start up")"
|
|
560
|
+ rc=${?}
|
528
|
561
|
if [ -z "${dns_status}" ]
|
529
|
562
|
then
|
530
|
|
- # check 2: nslookup probe
|
531
|
|
- #
|
532
|
563
|
dns_status="$(nslookup "${adb_domain}" 2>/dev/null | grep "${adb_ip}")"
|
|
564
|
+ rc=${?}
|
533
|
565
|
if [ -z "${dns_status}" ]
|
534
|
566
|
then
|
535
|
|
- # create backup of new block list only, if both checks are OK and backup enabled
|
536
|
|
- #
|
537
|
567
|
if [ "${backup_ok}" = "true" ]
|
538
|
568
|
then
|
539
|
|
- cp -f "${adb_dnsfile}" "${adb_backupfile}" 2>/dev/null
|
540
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: new block list with ${adb_count} domains loaded, backup generated"
|
|
569
|
+ cp -f "${adb_dnsfile}" "${adb_backupfile}" >/dev/null 2>&1
|
|
570
|
+ f_log "new adblock list with ${adb_count} domains loaded, backup generated"
|
541
|
571
|
else
|
542
|
|
- /usr/bin/logger -t "adblock[${pid}]" "info: new block list with ${adb_count} domains loaded, no backup"
|
|
572
|
+ f_log "new adblock list with ${adb_count} domains loaded, no backup"
|
543
|
573
|
fi
|
544
|
574
|
else
|
545
|
|
- restore_msg="nslookup probe failed"
|
|
575
|
+ f_log "nslookup probe failed" "${rc}"
|
546
|
576
|
f_restore
|
547
|
577
|
fi
|
548
|
578
|
else
|
549
|
|
- restore_msg="dnsmasq probe failed"
|
550
|
|
- f_restore
|
|
579
|
+ f_log "dnsmasq probe failed" "${rc}"
|
|
580
|
+ f_restore
|
551
|
581
|
fi
|
552
|
582
|
}
|
553
|
583
|
|
|
@@ -556,9 +586,10 @@ f_dnscheck()
|
556
|
586
|
#
|
557
|
587
|
f_footer()
|
558
|
588
|
{
|
|
589
|
+ local url
|
559
|
590
|
adb_count="$(wc -l < "${adb_dnsfile}")"
|
560
|
|
- printf "%s\n" "###################################################" >> "${adb_dnsfile}"
|
561
|
|
- printf "%s\n" "# last adblock file update: $(date +"%d.%m.%Y - %T")" >> "${adb_dnsfile}"
|
|
591
|
+ printf "%s\n" "####################################################" >> "${adb_dnsfile}"
|
|
592
|
+ printf "%s\n" "# last adblock list update: $(date +"%d.%m.%Y - %T")" >> "${adb_dnsfile}"
|
562
|
593
|
printf "%s\n" "# ${0##*/} (${adb_version}) - ${adb_count} ad/abuse domains blocked" >> "${adb_dnsfile}"
|
563
|
594
|
printf "%s\n" "# domain blacklist sources:" >> "${adb_dnsfile}"
|
564
|
595
|
for src in ${adb_sources}
|
|
@@ -566,8 +597,7 @@ f_footer()
|
566
|
597
|
url="${src//\&ruleset=*/}"
|
567
|
598
|
printf "%s\n" "# ${url}" >> "${adb_dnsfile}"
|
568
|
599
|
done
|
569
|
|
- printf "%s\n" "###################################################" >> "${adb_dnsfile}"
|
570
|
|
- printf "%s\n" "# domain whitelist source:" >> "${adb_dnsfile}"
|
|
600
|
+ printf "%s\n" "#####" >> "${adb_dnsfile}"
|
571
|
601
|
printf "%s\n" "# ${adb_whitelist}" >> "${adb_dnsfile}"
|
572
|
|
- printf "%s\n" "###################################################" >> "${adb_dnsfile}"
|
|
602
|
+ printf "%s\n" "####################################################" >> "${adb_dnsfile}"
|
573
|
603
|
}
|