|
@@ -0,0 +1,434 @@
|
|
1
|
+##############################################
|
|
2
|
+# function library used by adblock-update.sh #
|
|
3
|
+# written by Dirk Brenken (dirk@brenken.org) #
|
|
4
|
+##############################################
|
|
5
|
+
|
|
6
|
+#############################################
|
|
7
|
+# f_envcheck: check environment prerequisites
|
|
8
|
+#
|
|
9
|
+f_envcheck()
|
|
10
|
+{
|
|
11
|
+ # source in json helpers library
|
|
12
|
+ #
|
|
13
|
+ if [ -r "/usr/share/libubox/jshn.sh" ]
|
|
14
|
+ then
|
|
15
|
+ . "/usr/share/libubox/jshn.sh"
|
|
16
|
+ else
|
|
17
|
+ /usr/bin/logger -t "adblock[${pid}]" "json helpers library not found"
|
|
18
|
+ f_deltemp
|
|
19
|
+ exit 10
|
|
20
|
+ fi
|
|
21
|
+
|
|
22
|
+ # check adblock network device configuration
|
|
23
|
+ #
|
|
24
|
+ if [ ! -d "/sys/class/net/${adb_dev}" ]
|
|
25
|
+ then
|
|
26
|
+ /usr/bin/logger -t "adblock[${pid}]" "invalid adblock network device input (${adb_dev})"
|
|
27
|
+ f_deltemp
|
|
28
|
+ exit 15
|
|
29
|
+ fi
|
|
30
|
+
|
|
31
|
+ # check adblock network interface configuration
|
|
32
|
+ #
|
|
33
|
+ check_if="$(printf "${adb_if}" | sed -n '/[^_0-9A-Za-z]/p')"
|
|
34
|
+ banned_if="$(printf "${adb_if}" | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p')"
|
|
35
|
+ if [ -n "${check_if}" ] || [ -n "${banned_if}" ]
|
|
36
|
+ then
|
|
37
|
+ /usr/bin/logger -t "adblock[${pid}]" "invalid adblock network interface input (${adb_if})"
|
|
38
|
+ f_deltemp
|
|
39
|
+ exit 20
|
|
40
|
+ fi
|
|
41
|
+
|
|
42
|
+ # check adblock ip address configuration
|
|
43
|
+ #
|
|
44
|
+ check_ip="$(printf "${adb_ip}" | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p')"
|
|
45
|
+ if [ -z "${check_ip}" ]
|
|
46
|
+ then
|
|
47
|
+ /usr/bin/logger -t "adblock[${pid}]" "invalid adblock ip address input (${adb_ip})"
|
|
48
|
+ f_deltemp
|
|
49
|
+ exit 25
|
|
50
|
+ fi
|
|
51
|
+
|
|
52
|
+ # check adblock blacklist/whitelist configuration
|
|
53
|
+ #
|
|
54
|
+ if [ ! -r "${adb_blacklist}" ] || [ ! -r "${adb_whitelist}" ]
|
|
55
|
+ then
|
|
56
|
+ /usr/bin/logger -t "adblock[${pid}]" "adblock blacklist or whitelist not found"
|
|
57
|
+ f_deltemp
|
|
58
|
+ exit 30
|
|
59
|
+ fi
|
|
60
|
+
|
|
61
|
+ # check wan update configuration
|
|
62
|
+ #
|
|
63
|
+ if [ -n "${wan_dev}" ]
|
|
64
|
+ then
|
|
65
|
+ wan_ok="true"
|
|
66
|
+ else
|
|
67
|
+ wan_ok="false"
|
|
68
|
+ /usr/bin/logger -t "adblock[${pid}]" "wan update check will be disabled"
|
|
69
|
+ fi
|
|
70
|
+
|
|
71
|
+ # check ntp sync configuration
|
|
72
|
+ #
|
|
73
|
+ if [ -n "${ntp_srv}" ]
|
|
74
|
+ then
|
|
75
|
+ ntp_ok="true"
|
|
76
|
+ else
|
|
77
|
+ ntp_ok="false"
|
|
78
|
+ /usr/bin/logger -t "adblock[${pid}]" "ntp time sync will be disabled"
|
|
79
|
+ fi
|
|
80
|
+
|
|
81
|
+ # check backup configuration
|
|
82
|
+ #
|
|
83
|
+ if [ -n "${backup_dir}" ] && [ -d "${backup_dir}" ]
|
|
84
|
+ then
|
|
85
|
+ backup_ok="true"
|
|
86
|
+ mounts="${backup_dir} ${tmp_dir}"
|
|
87
|
+ else
|
|
88
|
+ backup_ok="false"
|
|
89
|
+ mounts="${tmp_dir}"
|
|
90
|
+ /usr/bin/logger -t "adblock[${pid}]" "backup/restore will be disabled"
|
|
91
|
+ fi
|
|
92
|
+
|
|
93
|
+ # check error log configuration
|
|
94
|
+ #
|
|
95
|
+ if [ "${log_file}" = "/dev/stdout" ]
|
|
96
|
+ then
|
|
97
|
+ log_ok="true"
|
|
98
|
+ log_file="/proc/self/fd/1"
|
|
99
|
+ elif [ -n "${log_file}" ] && [ "${backup_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
|
|
100
|
+ then
|
|
101
|
+ log_ok="true"
|
|
102
|
+ else
|
|
103
|
+ log_ok="false"
|
|
104
|
+ log_file="/dev/null"
|
|
105
|
+ /usr/bin/logger -t "adblock[${pid}]" "error logging will be disabled"
|
|
106
|
+ fi
|
|
107
|
+
|
|
108
|
+ # check dns query log configuration
|
|
109
|
+ #
|
|
110
|
+ if [ -n "${query_file}" ] && [ "${backup_ok}" = "true" ]
|
|
111
|
+ then
|
|
112
|
+ # check find capabilities
|
|
113
|
+ #
|
|
114
|
+ base="$(find --help 2>&1 | grep "mtime")"
|
|
115
|
+ if [[ -z "${base}" ]]
|
|
116
|
+ then
|
|
117
|
+ query_ok="false"
|
|
118
|
+ /usr/bin/logger -t "adblock[${pid}]" "no 'find/mtime' support, dns query logging will be disabled"
|
|
119
|
+ else
|
|
120
|
+ query_ok="true"
|
|
121
|
+ fi
|
|
122
|
+ else
|
|
123
|
+ query_ok="false"
|
|
124
|
+ /usr/bin/logger -t "adblock[${pid}]" "dns query logging will be disabled"
|
|
125
|
+ fi
|
|
126
|
+
|
|
127
|
+ # check shallalist configuration
|
|
128
|
+ #
|
|
129
|
+ check_shalla="$(printf "${adb_source}" | sed -n '/.*shallalist.txt.*/p')"
|
|
130
|
+ if [ -n "${check_shalla}" ]
|
|
131
|
+ then
|
|
132
|
+ shalla_ok="true"
|
|
133
|
+ else
|
|
134
|
+ shalla_ok="false"
|
|
135
|
+ /usr/bin/logger -t "adblock[${pid}]" "shallalist processing will be disabled"
|
|
136
|
+ fi
|
|
137
|
+
|
|
138
|
+ # check mount points & space requirements
|
|
139
|
+ #
|
|
140
|
+ for mp in ${mounts}
|
|
141
|
+ do
|
|
142
|
+ df "${mp}" 2>/dev/null |\
|
|
143
|
+ tail -n1 |\
|
|
144
|
+ while read filesystem overall used available scrap
|
|
145
|
+ do
|
|
146
|
+ av_space="${available}"
|
|
147
|
+ if [ $((av_space)) -eq 0 ]
|
|
148
|
+ then
|
|
149
|
+ /usr/bin/logger -t "adblock[${pid}]" "no space left on device, not mounted (${mp})"
|
|
150
|
+ exit 35
|
|
151
|
+ elif [ $((av_space)) -lt $((min_space)) ]
|
|
152
|
+ then
|
|
153
|
+ /usr/bin/logger -t "adblock[${pid}]" "not enough space on device (${mp})"
|
|
154
|
+ exit 40
|
|
155
|
+ fi
|
|
156
|
+ done
|
|
157
|
+ # subshell return code handling
|
|
158
|
+ #
|
|
159
|
+ rc=$?
|
|
160
|
+ if [ $((rc)) -ne 0 ]
|
|
161
|
+ then
|
|
162
|
+ f_deltemp
|
|
163
|
+ exit ${rc}
|
|
164
|
+ fi
|
|
165
|
+ done
|
|
166
|
+
|
|
167
|
+ # get list with all installed packages
|
|
168
|
+ #
|
|
169
|
+ pkg_list="$(opkg list-installed 2>/dev/null)"
|
|
170
|
+
|
|
171
|
+ # check openwrt release
|
|
172
|
+ #
|
|
173
|
+ base="$(printf "${pkg_list}" | grep "^base-files" | sed 's/\(.*r\)//g')"
|
|
174
|
+ if [ $((base)) -lt $((min_release)) ]
|
|
175
|
+ then
|
|
176
|
+ /usr/bin/logger -t "adblock[${pid}]" "openwrt (r${wrt_release}) seems to be too old"
|
|
177
|
+ f_deltemp
|
|
178
|
+ exit 45
|
|
179
|
+ fi
|
|
180
|
+
|
|
181
|
+ # check curl package dependency
|
|
182
|
+ #
|
|
183
|
+ base="$(printf "${pkg_list}" | grep "^curl")"
|
|
184
|
+ if [ -z "${base}" ]
|
|
185
|
+ then
|
|
186
|
+ /usr/bin/logger -t "adblock[${pid}]" "curl package not found"
|
|
187
|
+ f_deltemp
|
|
188
|
+ exit 50
|
|
189
|
+ fi
|
|
190
|
+
|
|
191
|
+ # check wget package dependency
|
|
192
|
+ #
|
|
193
|
+ base="$(printf "${pkg_list}" | grep "^wget")"
|
|
194
|
+ if [ -z "${base}" ]
|
|
195
|
+ then
|
|
196
|
+ /usr/bin/logger -t "adblock[${pid}]" "wget package not found"
|
|
197
|
+ f_deltemp
|
|
198
|
+ exit 55
|
|
199
|
+ fi
|
|
200
|
+
|
|
201
|
+ # check dynamic/volatile adblock network interface configuration
|
|
202
|
+ #
|
|
203
|
+ rc="$(ifstatus "${adb_if}" >/dev/null 2>&1; printf $?)"
|
|
204
|
+ if [ $((rc)) -ne 0 ]
|
|
205
|
+ then
|
|
206
|
+ json_init
|
|
207
|
+ json_add_string name "${adb_if}"
|
|
208
|
+ json_add_string ifname "${adb_dev}"
|
|
209
|
+ json_add_string proto "static"
|
|
210
|
+ json_add_array ipaddr
|
|
211
|
+ json_add_string "" "${adb_ip}"
|
|
212
|
+ json_close_array
|
|
213
|
+ json_close_object
|
|
214
|
+ ubus call network add_dynamic "$(json_dump)"
|
|
215
|
+ /usr/bin/logger -t "adblock[${pid}]" "created new dynamic/volatile network interface (${adb_if}, ${adb_ip})"
|
|
216
|
+ fi
|
|
217
|
+
|
|
218
|
+ # check adblock uhttpd instance configuration
|
|
219
|
+ #
|
|
220
|
+ if [ -z "$(uci -q get uhttpd.${adb_if} 2>/dev/null)" ]
|
|
221
|
+ then
|
|
222
|
+ uci -q set uhttpd.${adb_if}="uhttpd"
|
|
223
|
+ uci -q set uhttpd.${adb_if}.listen_http="${adb_ip}:80"
|
|
224
|
+ uci -q set uhttpd.${adb_if}.home="/www/adblock"
|
|
225
|
+ uci -q set uhttpd.${adb_if}.error_page="/adblock.html"
|
|
226
|
+ uci -q commit uhttpd
|
|
227
|
+ /etc/init.d/uhttpd reload
|
|
228
|
+ /usr/bin/logger -t "adblock[${pid}]" "created new uhttpd instance (${adb_if}, ${adb_ip}) in /etc/config/uhttpd"
|
|
229
|
+ fi
|
|
230
|
+}
|
|
231
|
+
|
|
232
|
+###################################################
|
|
233
|
+# f_deltemp: delete temporary files and directories
|
|
234
|
+f_deltemp()
|
|
235
|
+{
|
|
236
|
+ if [ -f "${tmp_file}" ]
|
|
237
|
+ then
|
|
238
|
+ rm -f "${tmp_file}" 2>/dev/null
|
|
239
|
+ fi
|
|
240
|
+ if [ -d "${tmp_dir}" ]
|
|
241
|
+ then
|
|
242
|
+ rm -rf "${tmp_dir}" 2>/dev/null
|
|
243
|
+ fi
|
|
244
|
+}
|
|
245
|
+
|
|
246
|
+################################################################
|
|
247
|
+# f_remove: remove temporary files, start and maintain query log
|
|
248
|
+#
|
|
249
|
+f_remove()
|
|
250
|
+{
|
|
251
|
+ # delete temporary files and directories
|
|
252
|
+ #
|
|
253
|
+ f_deltemp
|
|
254
|
+
|
|
255
|
+ # kill existing domain query log background process,
|
|
256
|
+ # housekeeping and start of a new process on daily basis
|
|
257
|
+ #
|
|
258
|
+ if [ "${query_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
|
|
259
|
+ then
|
|
260
|
+ query_date="$(date "+%Y%m%d")"
|
|
261
|
+ if [ -s "${query_pid}" ] && [ ! -f "${query_file}.${query_date}" ]
|
|
262
|
+ then
|
|
263
|
+ kill -9 $(< "${query_pid}") 2>/dev/null
|
|
264
|
+ > "${query_pid}"
|
|
265
|
+ find "${backup_dir}" -maxdepth 1 -type f -mtime +${query_history} -name "${query_name}.*" -exec rm -f {} \; 2>/dev/null
|
|
266
|
+ /usr/bin/logger -t "adblock[${pid}]" "kill old query log background process and do logfile housekeeping"
|
|
267
|
+ fi
|
|
268
|
+ if [ ! -s "${query_pid}" ]
|
|
269
|
+ then
|
|
270
|
+ ( logread -f 2>/dev/null & printf -n "$!" > "${query_pid}" ) | egrep -o "(query\[A\].*)|([a-z0-9\.\-]* is ${query_ip}$)" >> "${query_file}.${query_date}" &
|
|
271
|
+ /usr/bin/logger -t "adblock[${pid}]" "start new domain query log background process"
|
|
272
|
+ fi
|
|
273
|
+ fi
|
|
274
|
+
|
|
275
|
+ # final log entry
|
|
276
|
+ #
|
|
277
|
+ /usr/bin/logger -t "adblock[${pid}]" "domain adblock processing finished (${script_ver})"
|
|
278
|
+}
|
|
279
|
+
|
|
280
|
+#####################################################
|
|
281
|
+# f_restore: if available, restore last adlist backup
|
|
282
|
+#
|
|
283
|
+f_restore()
|
|
284
|
+{
|
|
285
|
+ if [ "${backup_ok}" = "true" ] && [ -f "${backup_file}" ]
|
|
286
|
+ then
|
|
287
|
+ cp -f "${backup_file}" "${dns_file}" 2>/dev/null
|
|
288
|
+ /usr/bin/logger -t "adblock[${pid}]" "${restore_msg}, adlist backup restored"
|
|
289
|
+ printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, adlist backup restored" >> "${log_file}"
|
|
290
|
+ else
|
|
291
|
+ > "${dns_file}"
|
|
292
|
+ /usr/bin/logger -t "adblock[${pid}]" "${restore_msg}, empty adlist generated"
|
|
293
|
+ printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ${restore_msg}, empty adlist generated" >> "${log_file}"
|
|
294
|
+ fi
|
|
295
|
+ # restart dnsmasq
|
|
296
|
+ #
|
|
297
|
+ /etc/init.d/dnsmasq restart >/dev/null 2>&1
|
|
298
|
+
|
|
299
|
+ # remove files and exit
|
|
300
|
+ #
|
|
301
|
+ f_remove
|
|
302
|
+ exit 100
|
|
303
|
+}
|
|
304
|
+
|
|
305
|
+#######################################################
|
|
306
|
+# f_wancheck: check for usable adblock update interface
|
|
307
|
+#
|
|
308
|
+f_wancheck()
|
|
309
|
+{
|
|
310
|
+ if [ "${wan_ok}" = "true" ]
|
|
311
|
+ then
|
|
312
|
+ # wait for wan update interface(s)
|
|
313
|
+ #
|
|
314
|
+ while [ $((cnt)) -le $((max_cnt)) ]
|
|
315
|
+ do
|
|
316
|
+ for dev in ${wan_dev}
|
|
317
|
+ do
|
|
318
|
+ dev_out=$(< /sys/class/net/${dev}/operstate 2>/dev/null)
|
|
319
|
+ if [[ "${dev_out}" = "up" ]]
|
|
320
|
+ then
|
|
321
|
+ /usr/bin/logger -t "adblock[${pid}]" "get wan/update interface: ${dev}, after ${cnt} loops"
|
|
322
|
+ break 2
|
|
323
|
+ elif [ $((cnt)) -eq $((max_cnt)) ]
|
|
324
|
+ then
|
|
325
|
+ /usr/bin/logger -t "adblock[${pid}]" "no wan/update interface(s) found (${wan_dev})"
|
|
326
|
+ printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: no wan/update interface(s) found (${wan_dev})" >> "${log_file}"
|
|
327
|
+ restore_msg="no wan/update interface(s)"
|
|
328
|
+ f_restore
|
|
329
|
+ fi
|
|
330
|
+ done
|
|
331
|
+ sleep 1
|
|
332
|
+ cnt=$((cnt + 1))
|
|
333
|
+ done
|
|
334
|
+ fi
|
|
335
|
+}
|
|
336
|
+
|
|
337
|
+#####################################
|
|
338
|
+# f_ntpcheck: check/get ntp time sync
|
|
339
|
+#
|
|
340
|
+f_ntpcheck()
|
|
341
|
+{
|
|
342
|
+ if [ "${ntp_ok}" = "true" ]
|
|
343
|
+ then
|
|
344
|
+ # prepare ntp server pool
|
|
345
|
+ #
|
|
346
|
+ unset ntp_pool
|
|
347
|
+ for srv in ${ntp_srv}
|
|
348
|
+ do
|
|
349
|
+ ntp_pool="${ntp_pool} -p ${srv}"
|
|
350
|
+ done
|
|
351
|
+
|
|
352
|
+ # wait for ntp time sync
|
|
353
|
+ #
|
|
354
|
+ while [ $((cnt)) -le $((max_cnt)) ]
|
|
355
|
+ do
|
|
356
|
+ /usr/sbin/ntpd -nq ${ntp_pool} >/dev/null 2>&1
|
|
357
|
+ rc=$?
|
|
358
|
+ if [ $((rc)) -eq 0 ]
|
|
359
|
+ then
|
|
360
|
+ /usr/bin/logger -t "adblock[${pid}]" "get ntp time sync (${ntp_srv}), after ${cnt} loops"
|
|
361
|
+ break
|
|
362
|
+ elif [ $((cnt)) -eq $((max_cnt)) ]
|
|
363
|
+ then
|
|
364
|
+ ntp_ok="false"
|
|
365
|
+ /usr/bin/logger -t "adblock[${pid}]" "ntp time sync failed (${ntp_srv})"
|
|
366
|
+ printf "$(/bin/date "+%d.%m.%Y %H:%M:%S") - error: ntp time sync failed (${ntp_srv})" >> "${log_file}"
|
|
367
|
+ restore_msg="time sync failed"
|
|
368
|
+ f_restore
|
|
369
|
+ fi
|
|
370
|
+ sleep 1
|
|
371
|
+ cnt=$((cnt + 1))
|
|
372
|
+ done
|
|
373
|
+ fi
|
|
374
|
+}
|
|
375
|
+
|
|
376
|
+#################################################################
|
|
377
|
+# f_dnscheck: dnsmasq health check with newly generated blocklist
|
|
378
|
+#
|
|
379
|
+f_dnscheck()
|
|
380
|
+{
|
|
381
|
+ # check 1: dnsmasq startup
|
|
382
|
+ #
|
|
383
|
+ dns_status="$(logread -l 20 -e "dnsmasq" -e "FAILED to start up")"
|
|
384
|
+ if [ -z "${dns_status}" ]
|
|
385
|
+ then
|
|
386
|
+ # check 2: nslookup probe
|
|
387
|
+ #
|
|
388
|
+ dns_status="$(nslookup "${check_domain}" 2>/dev/null | grep "${adb_ip}")"
|
|
389
|
+ if [ -z "${dns_status}" ]
|
|
390
|
+ then
|
|
391
|
+ # create backup of new block list only, if both checks are OK and backup enabled
|
|
392
|
+ #
|
|
393
|
+ if [ "${backup_ok}" = "true" ]
|
|
394
|
+ then
|
|
395
|
+ cp -f "${dns_file}" "${backup_file}" 2>/dev/null
|
|
396
|
+ /usr/bin/logger -t "adblock[${pid}]" "new block list with ${adb_count} domains loaded, backup generated"
|
|
397
|
+ else
|
|
398
|
+ /usr/bin/logger -t "adblock[${pid}]" "new block list with ${adb_count} domains loaded"
|
|
399
|
+ fi
|
|
400
|
+ else
|
|
401
|
+ restore_msg="nslookup probe failed"
|
|
402
|
+ f_restore
|
|
403
|
+ fi
|
|
404
|
+ else
|
|
405
|
+ restore_msg="dnsmasq probe failed"
|
|
406
|
+ f_restore
|
|
407
|
+ fi
|
|
408
|
+}
|
|
409
|
+
|
|
410
|
+##########################################################
|
|
411
|
+# f_footer: write footer with a few statistics to dns file
|
|
412
|
+#
|
|
413
|
+f_footer()
|
|
414
|
+{
|
|
415
|
+ # count result of merged domain entries
|
|
416
|
+ #
|
|
417
|
+ adb_count="$(wc -l < "${dns_file}")"
|
|
418
|
+
|
|
419
|
+ # write file footer with timestamp and merged ad count sum
|
|
420
|
+ #
|
|
421
|
+ printf "%s\n" "###################################################" >> "${dns_file}"
|
|
422
|
+ printf "%s\n" "# last adblock file update: $(date +"%d.%m.%Y - %T")" >> "${dns_file}"
|
|
423
|
+ printf "%s\n" "# ${0##*/} (${script_ver}) - ${adb_count} ad/abuse domains blocked" >> "${dns_file}"
|
|
424
|
+ printf "%s\n" "# domain blacklist sources:" >> "${dns_file}"
|
|
425
|
+ for src in ${adb_source}
|
|
426
|
+ do
|
|
427
|
+ url="$(printf "${src}" | sed 's/\(\&ruleset=.*\)//g')"
|
|
428
|
+ printf "%s\n" "# ${url}" >> "${dns_file}"
|
|
429
|
+ done
|
|
430
|
+ printf "%s\n" "###################################################" >> "${dns_file}"
|
|
431
|
+ printf "%s\n" "# domain whitelist source:" >> "${dns_file}"
|
|
432
|
+ printf "%s\n" "# ${adb_whitelist}" >> "${dns_file}"
|
|
433
|
+ printf "%s\n" "###################################################" >> "${dns_file}"
|
|
434
|
+}
|