c1e467d7c254ea5c850fcd2684c08926d2144de2
[feed/packages.git] / net / adblock / files / adblock-update.sh
1 #!/bin/sh
2 # dns based ad/abuse domain blocking script
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 # prepare environment
10 #
11 adb_pid="${$}"
12 adb_pidfile="/var/run/adblock.pid"
13 adb_scriptver="1.4.9"
14 adb_mincfgver="2.5"
15 adb_scriptdir="${0%/*}"
16 if [ -r "${adb_pidfile}" ]
17 then
18 rc=255
19 logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))"
20 exit ${rc}
21 else
22 printf "${adb_pid}" > "${adb_pidfile}"
23 if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
24 then
25 . "${adb_scriptdir}/adblock-helper.sh"
26 f_envload
27 else
28 rc=254
29 logger -s -t "adblock[${adb_pid}] error" "adblock function library not found"
30 rm -f "${adb_pidfile}"
31 exit ${rc}
32 fi
33 fi
34
35 # call trap function on error signals (HUP, INT, QUIT, BUS, SEGV, TERM)
36 #
37 trap "rc=250; f_log 'error signal received/trapped'; f_exit" 1 2 3 10 11 15
38
39 # check environment
40 #
41 f_envcheck
42
43 # main loop for all block list sources
44 #
45 for src_name in ${adb_sources}
46 do
47 # check disabled sources
48 #
49 eval "enabled=\"\${enabled_${src_name}}\""
50 if [ "${enabled}" = "0" ]
51 then
52 if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
53 then
54 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
55 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
56 then
57 rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
58 fi
59 rm_done="true"
60 f_log "=> disabled source '${src_name}' removed"
61 fi
62 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
63 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_timestamp"
64 continue
65 fi
66
67 f_log "=> processing source '${src_name}'"
68 eval "url=\"\${adb_src_${src_name}}\""
69 eval "src_rset=\"\${adb_src_rset_${src_name}}\""
70 eval "list_time=\"\${CONFIG_${src_name}_adb_src_timestamp}\""
71 adb_dnsfile="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
72
73 # check 'url' and 'src_rset' values
74 #
75 if [ -z "${url}" ] || [ -z "${src_rset}" ]
76 then
77 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=broken config"
78 f_log " broken source configuration, skipped"
79 continue
80 fi
81
82 # download only block list with newer/updated timestamp
83 #
84 if [ "${src_name}" = "blacklist" ]
85 then
86 url_time="$(date -r "${url}")"
87 else
88 url_time="$(${adb_fetch} ${fetch_parm} ${response_parm} "${url}" 2>&1 | awk '$0 ~ /Last-Modified/ {printf substr($0,18)}')"
89 fi
90 if [ -z "${url_time}" ]
91 then
92 url_time="$(date)"
93 f_log " no online timestamp"
94 fi
95 if [ -z "${list_time}" ] || [ "${list_time}" != "${url_time}" ] || [ ! -r "${adb_dnsfile}" ] ||\
96 ([ "${backup_ok}" = "true" ] && [ ! -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ])
97 then
98 if [ "${src_name}" = "blacklist" ]
99 then
100 tmp_domains="$(strings -n 1 "${url}")"
101 elif [ "${src_name}" = "shalla" ]
102 then
103 shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
104 shalla_file="${adb_tmpdir}/shallalist.txt"
105 "${adb_fetch}" ${fetch_parm} -O "${shalla_archive}" "${url}"
106 rc=${?}
107 if [ $((rc)) -eq 0 ]
108 then
109 > "${shalla_file}"
110 for category in ${adb_src_cat_shalla}
111 do
112 tar -xOzf "${shalla_archive}" BL/${category}/domains >> "${shalla_file}"
113 rc=${?}
114 if [ $((rc)) -ne 0 ]
115 then
116 f_log " archive extraction failed (${category})"
117 break
118 fi
119 done
120 tmp_domains="$(strings -n 1 "${shalla_file}")"
121 rm -rf "${adb_tmpdir}/BL"
122 rm -f "${shalla_archive}"
123 rm -f "${shalla_file}"
124 fi
125 else
126 tmp_domains="$(${adb_fetch} ${fetch_parm} -O- "${url}" | strings -n 1)"
127 fi
128 rc=${?}
129 else
130 f_log " source doesn't change, skipped"
131 continue
132 fi
133
134 # check download result and prepare domain output, backup/restore if needed
135 #
136 if [ $((rc)) -eq 0 ] && [ -n "${tmp_domains}" ]
137 then
138 count="$(printf "%s\n" "${tmp_domains}" | awk "${src_rset}" | tee "${adb_tmpfile}" | wc -l)"
139 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=${url_time}"
140 if [ "${backup_ok}" = "true" ]
141 then
142 gzip -cf "${adb_tmpfile}" > "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
143 fi
144 f_log " source download finished (${count} entries)"
145 unset tmp_domains
146 elif [ $((rc)) -eq 0 ] && [ -z "${tmp_domains}" ]
147 then
148 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
149 then
150 gunzip -cf "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
151 count="$(wc -l < "${adb_tmpfile}")"
152 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored"
153 f_log " empty source download, restored (${count} entries)"
154 else
155 if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
156 then
157 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
158 rm_done="true"
159 fi
160 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
161 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty download"
162 f_log " empty source download, skipped"
163 continue
164 fi
165 else
166 rc=0
167 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
168 then
169 gunzip -cf "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
170 count="$(wc -l < "${adb_tmpfile}")"
171 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored"
172 f_log " source download failed, restored (${count} entries)"
173 else
174 if [ -r "${adb_dnsdir}/${adb_dnsprefix}.${src_name}" ]
175 then
176 rm -f "${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
177 rm_done="true"
178 fi
179 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
180 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=download failed"
181 f_log " source download failed, skipped"
182 continue
183 fi
184 fi
185
186 # remove whitelist domains, sort domains and make them unique,
187 # rewrite ad/abuse domain information to separate dnsmasq files
188 #
189 if [ $((count)) -gt 0 ] && [ -n "${adb_tmpfile}" ]
190 then
191 if [ -s "${adb_tmpdir}/tmp.whitelist" ]
192 then
193 grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpfile}" | sort -u | eval "${adb_dnsformat}" > "${adb_dnsfile}"
194 else
195 sort -u "${adb_tmpfile}" | eval "${adb_dnsformat}" > "${adb_dnsfile}"
196 fi
197 rc=${?}
198 if [ $((rc)) -eq 0 ]
199 then
200 rev_done="true"
201 f_log " domain merging finished"
202 else
203 rc=0
204 rm -f "${adb_dnsfile}"
205 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
206 then
207 rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
208 fi
209 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
210 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=domain merging failed"
211 f_log " domain merging failed, skipped"
212 continue
213 fi
214 else
215 rm -f "${adb_dnsfile}"
216 if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
217 then
218 rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
219 fi
220 "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
221 "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty domain input"
222 f_log " empty domain input, skipped"
223 continue
224 fi
225 done
226
227 # overall sort, make block list entries unique
228 #
229 if [ "${rev_done}" = "true" ] && [ "${mem_ok}" = "true" ]
230 then
231 f_log "remove duplicates in separate block lists"
232 for list in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
233 do
234 list="${list/*./}"
235 if [ -s "${adb_tmpdir}/blocklist.overall" ]
236 then
237 sort "${adb_tmpdir}/blocklist.overall" "${adb_tmpdir}/blocklist.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" | uniq -u > "${adb_tmpdir}/tmp.blocklist"
238 cat "${adb_tmpdir}/tmp.blocklist" > "${adb_dnsdir}/${adb_dnsprefix}.${list}"
239 fi
240 cat "${adb_dnsdir}/${adb_dnsprefix}.${list}" >> "${adb_tmpdir}/blocklist.overall"
241 done
242 fi
243
244 # restart & check dnsmasq with generated set of block lists
245 #
246 if [ "${rev_done}" = "true" ] || [ "${rm_done}" = "true" ] || [ -n "${mv_done}" ]
247 then
248 "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
249 /etc/init.d/dnsmasq restart
250 sleep 1
251 check="$(pgrep -f "dnsmasq")"
252 if [ -n "${check}" ]
253 then
254 f_cntconfig
255 f_log "block lists with overall ${adb_count} domains loaded"
256 else
257 f_rmdns
258 sleep 1
259 check="$(pgrep -f "dnsmasq")"
260 if [ -n "${check}" ]
261 then
262 f_log "dnsmasq restart without block lists succeeded, please check your configuration"
263 else
264 f_log "dnsmasq restart without block lists failed, please check your configuration"
265 fi
266 rc=100
267 f_exit
268 fi
269 else
270 f_cntconfig
271 f_log "block lists with overall ${adb_count} domains are still valid, no update required"
272 fi
273
274 # remove temporary files and exit
275 #
276 f_exit