You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
13 KiB

  1. #!/bin/sh
  2. # ad/abuse domain blocking script for dnsmasq/openwrt
  3. # written by Dirk Brenken (openwrt@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # You should have received a copy of the GNU General Public License
  6. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  7. # set the C locale
  8. #
  9. LC_ALL=C
  10. # script debug switch (disabled by default)
  11. # set 'DEBUG=1' to enable script debugging
  12. #
  13. DEBUG=0
  14. if [ $((DEBUG)) -eq 0 ]
  15. then
  16. exec 2>/dev/null
  17. fi
  18. # pid handling
  19. #
  20. adb_pid="${$}"
  21. adb_pidfile="/var/run/adblock.pid"
  22. if [ -r "${adb_pidfile}" ]
  23. then
  24. rc=255
  25. /usr/bin/logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))"
  26. exit ${rc}
  27. else
  28. printf "${adb_pid}" > "${adb_pidfile}"
  29. fi
  30. # get current directory, script- and openwrt version
  31. #
  32. adb_scriptdir="${0%/*}"
  33. adb_scriptver="1.0.2"
  34. openwrt_version="$(cat /etc/openwrt_version)"
  35. # source in adblock function library
  36. #
  37. if [ -r "${adb_scriptdir}/adblock-helper.sh" ]
  38. then
  39. . "${adb_scriptdir}/adblock-helper.sh"
  40. else
  41. rc=254
  42. /usr/bin/logger -s -t "adblock[${adb_pid}] error" "adblock function library not found"
  43. rm -f "${adb_pidfile}"
  44. exit ${rc}
  45. fi
  46. # call trap function on error signals (HUP, INT, QUIT, BUS, SEGV, TERM)
  47. #
  48. trap "rc=250; f_log 'error signal received/trapped' '${rc}'; f_exit" 1 2 3 10 11 15
  49. # load environment
  50. #
  51. f_envload
  52. # start logging
  53. #
  54. f_log "domain adblock processing started (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  55. # parse environment
  56. #
  57. f_envparse
  58. # check environment
  59. #
  60. f_envcheck
  61. # start shallalist (pre-)processing
  62. #
  63. if [ -n "${adb_arc_shalla}" ]
  64. then
  65. # start shallalist processing
  66. #
  67. shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
  68. shalla_file="${adb_tmpdir}/shallalist.txt"
  69. src_name="shalla"
  70. adb_dnsfile="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
  71. if [ -r "${adb_dnsfile}" ]
  72. then
  73. list_time="$(awk '$0 ~ /^# last modified/ {printf substr($0,18)}' "${adb_dnsfile}")"
  74. fi
  75. f_log "=> (pre-)processing adblock source '${src_name}'"
  76. # only process shallalist archive with updated timestamp,
  77. # extract and merge only domains of selected shallalist categories
  78. #
  79. shalla_time="$(${adb_fetch} ${wget_parm} --server-response --spider "${adb_arc_shalla}" 2>&1 | awk '$0 ~ /Last-Modified/ {printf substr($0,18)}')"
  80. if [ -z "${shalla_time}" ]
  81. then
  82. shalla_time="$(date)"
  83. f_log " no online timestamp received, current date will be used"
  84. fi
  85. if [ -z "${list_time}" ] || [ "${list_time}" != "${shalla_time}" ]
  86. then
  87. ${adb_fetch} ${wget_parm} --output-document="${shalla_archive}" "${adb_arc_shalla}"
  88. rc=${?}
  89. if [ $((rc)) -eq 0 ]
  90. then
  91. > "${shalla_file}"
  92. for category in ${adb_cat_shalla}
  93. do
  94. tar -xOzf "${shalla_archive}" BL/${category}/domains >> "${shalla_file}"
  95. rc=${?}
  96. if [ $((rc)) -ne 0 ]
  97. then
  98. f_log " archive extraction failed (${category})"
  99. break
  100. fi
  101. done
  102. # remove temporary files
  103. #
  104. rm -f "${shalla_archive}"
  105. rm -rf "${adb_tmpdir}/BL"
  106. if [ $((rc)) -eq 0 ]
  107. then
  108. adb_sources="${adb_sources} ${shalla_file}&ruleset=rset_shalla"
  109. f_log " source archive (pre-)processing finished"
  110. else
  111. rc=0
  112. adb_srclist="! -name ${adb_dnsprefix}.${src_name}"
  113. adb_errsrclist="-name ${adb_dnsprefix}.${src_name}"
  114. fi
  115. else
  116. rc=0
  117. adb_srclist="! -name ${adb_dnsprefix}.${src_name}"
  118. adb_errsrclist="-name ${adb_dnsprefix}.${src_name}"
  119. f_log " source archive download failed"
  120. fi
  121. else
  122. adb_srclist="! -name ${adb_dnsprefix}.${src_name}"
  123. f_log " source archive doesn't change, no update required"
  124. fi
  125. fi
  126. # add blacklist source to active adblock domain sources
  127. #
  128. if [ -s "${adb_blacklist}" ]
  129. then
  130. adb_sources="${adb_sources} ${adb_blacklist}&ruleset=rset_blacklist"
  131. fi
  132. # loop through active adblock domain sources,
  133. # download sources, prepare output and store all extracted domains in temp file
  134. #
  135. for src in ${adb_sources}
  136. do
  137. url="${src/\&ruleset=*/}"
  138. src_name="${src/*\&ruleset=rset_/}"
  139. adb_dnsfile="${adb_dnsdir}/${adb_dnsprefix}.${src_name}"
  140. if [ -r "${adb_dnsfile}" ]
  141. then
  142. list_time="$(awk '$0 ~ /^# last modified/ {printf substr($0,18)}' "${adb_dnsfile}")"
  143. fi
  144. f_log "=> processing adblock source '${src_name}'"
  145. # prepare find statement with active adblock list sources
  146. #
  147. if [ -z "${adb_srclist}" ]
  148. then
  149. adb_srclist="! -name ${adb_dnsprefix}.${src_name}"
  150. else
  151. adb_srclist="${adb_srclist} -a ! -name ${adb_dnsprefix}.${src_name}"
  152. fi
  153. # only download adblock list with newer/updated timestamp
  154. #
  155. if [ "${src_name}" = "blacklist" ]
  156. then
  157. url_time="$(date -r "${adb_blacklist}")"
  158. elif [ "${src_name}" = "shalla" ]
  159. then
  160. url_time="${shalla_time}"
  161. else
  162. url_time="$(${adb_fetch} ${wget_parm} --server-response --spider "${url}" 2>&1 | awk '$0 ~ /Last-Modified/ {printf substr($0,18)}')"
  163. fi
  164. if [ -z "${url_time}" ]
  165. then
  166. url_time="$(date)"
  167. f_log " no online timestamp received, current date will be used"
  168. fi
  169. if [ -z "${list_time}" ] || [ "${list_time}" != "${url_time}" ]
  170. then
  171. if [ "${src_name}" = "blacklist" ]
  172. then
  173. tmp_domains="$(cat "${adb_blacklist}")"
  174. rc=${?}
  175. elif [ "${src_name}" = "shalla" ]
  176. then
  177. tmp_domains="$(cat "${shalla_file}")"
  178. rc=${?}
  179. else
  180. tmp_domains="$(${adb_fetch} ${wget_parm} --output-document=- "${url}")"
  181. rc=${?}
  182. fi
  183. else
  184. f_log " source doesn't change, no update required"
  185. continue
  186. fi
  187. # check download result and prepare domain output by regex patterns
  188. #
  189. if [ $((rc)) -eq 0 ] && [ -n "${tmp_domains}" ]
  190. then
  191. eval "src_rset=\${rset_${src_name}}"
  192. count="$(printf "%s\n" "${tmp_domains}" | eval "${src_rset}" | tee "${adb_tmpfile}" | wc -l)"
  193. f_log " source download finished (${count} entries)"
  194. if [ "${src_name}" = "shalla" ]
  195. then
  196. rm -f "${shalla_file}"
  197. fi
  198. unset tmp_domains
  199. elif [ $((rc)) -eq 0 ] && [ -z "${tmp_domains}" ]
  200. then
  201. f_log " empty source download finished"
  202. continue
  203. else
  204. rc=0
  205. if [ -z "${adb_errsrclist}" ]
  206. then
  207. adb_errsrclist="-name ${adb_dnsprefix}.${src_name}"
  208. else
  209. adb_errsrclist="${adb_errsrclist} -o -name ${adb_dnsprefix}.${src_name}"
  210. fi
  211. f_log " source download failed"
  212. continue
  213. fi
  214. # remove whitelist domains, sort domains and make them unique,
  215. # finally rewrite ad/abuse domain information to separate dnsmasq files
  216. #
  217. if [ $((count)) -gt 0 ] && [ -n "${adb_tmpfile}" ]
  218. then
  219. if [ -s "${adb_whitelist}" ]
  220. then
  221. grep -Fvxf "${adb_whitelist}" "${adb_tmpfile}" | sort -u | eval "${adb_dnsformat}" > "${adb_dnsfile}"
  222. rc=${?}
  223. else
  224. sort -u "${adb_tmpfile}" | eval "${adb_dnsformat}" > "${adb_dnsfile}"
  225. rc=${?}
  226. fi
  227. # prepare find statement with revised adblock list sources
  228. #
  229. if [ -z "${adb_revsrclist}" ]
  230. then
  231. adb_revsrclist="-name ${adb_dnsprefix}.${src_name}"
  232. else
  233. adb_revsrclist="${adb_revsrclist} -o -name ${adb_dnsprefix}.${src_name}"
  234. fi
  235. # write preliminary footer
  236. #
  237. if [ $((rc)) -eq 0 ]
  238. then
  239. printf "%s\n" "#---------------------------------------------" >> "${adb_dnsfile}"
  240. printf "%s\n" "# last modified: ${url_time}" >> "${adb_dnsfile}"
  241. printf "%s\n" "##" >> "${adb_dnsfile}"
  242. f_log " domain merging finished"
  243. else
  244. f_log " domain merging failed" "${rc}"
  245. f_restore
  246. fi
  247. else
  248. f_log " empty domain input received"
  249. continue
  250. fi
  251. done
  252. # remove disabled adblock lists and their backups
  253. #
  254. if [ -n "${adb_srclist}" ]
  255. then
  256. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" \( ${adb_srclist} \) -print -exec rm -f "{}" \;)"
  257. rc=${?}
  258. else
  259. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec rm -f "{}" \;)"
  260. rc=${?}
  261. fi
  262. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  263. then
  264. f_log "disabled adblock lists removed"
  265. if [ "${backup_ok}" = "true" ]
  266. then
  267. if [ -n "${adb_srclist}" ]
  268. then
  269. rm_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" \( ${adb_srclist} \) -print -exec rm -f "{}" \;)"
  270. rc=${?}
  271. else
  272. rm_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec rm -f "{}" \;)"
  273. rc=${?}
  274. fi
  275. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  276. then
  277. f_log "disabled adblock list backups removed"
  278. elif [ $((rc)) -ne 0 ]
  279. then
  280. f_log "error during removal of disabled adblock list backups" "${rc}"
  281. f_exit
  282. fi
  283. fi
  284. elif [ $((rc)) -ne 0 ]
  285. then
  286. f_log "error during removal of disabled adblock lists" "${rc}"
  287. f_exit
  288. fi
  289. # partial restore of adblock lists in case of download errors
  290. #
  291. if [ "${backup_ok}" = "true" ] && [ -n "${adb_errsrclist}" ]
  292. then
  293. restore_done="$(find "${adb_backupdir}" -maxdepth 1 -type f \( ${adb_errsrclist} \) -print -exec cp -pf "{}" "${adb_dnsdir}" \;)"
  294. rc=${?}
  295. if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
  296. then
  297. f_log "partial restore done"
  298. elif [ $((rc)) -ne 0 ]
  299. then
  300. f_log "error during partial restore" "${rc}"
  301. f_exit
  302. fi
  303. fi
  304. # make separate adblock lists entries unique
  305. #
  306. if [ "${mem_ok}" = "true" ] && [ -n "${adb_revsrclist}" ]
  307. then
  308. f_log "remove duplicates in separate adblock lists"
  309. # generate a unique overall block list
  310. #
  311. head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* | sort -u > "${adb_tmpdir}/blocklist.overall"
  312. # loop through all separate lists, ordered by size (ascending)
  313. #
  314. for list in $(ls -Sr "${adb_dnsdir}/${adb_dnsprefix}."*)
  315. do
  316. # check overall block list vs. separate block list,
  317. # write only duplicate entries to a temporary separate list
  318. #
  319. list="${list/*./}"
  320. sort "${adb_tmpdir}/blocklist.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" | uniq -d > "${adb_tmpdir}/tmp.${list}"
  321. # write only unique entries back to overall block list
  322. #
  323. sort "${adb_tmpdir}/blocklist.overall" "${adb_tmpdir}/tmp.${list}" | uniq -u > "${adb_tmpdir}/tmp.overall"
  324. mv -f "${adb_tmpdir}/tmp.overall" "${adb_tmpdir}/blocklist.overall"
  325. # write unique result back to original separate list
  326. #
  327. tail -qn 3 "${adb_dnsdir}/${adb_dnsprefix}.${list}" >> "${adb_tmpdir}/tmp.${list}"
  328. mv -f "${adb_tmpdir}/tmp.${list}" "${adb_dnsdir}/${adb_dnsprefix}.${list}"
  329. done
  330. rm -f "${adb_tmpdir}/blocklist.overall"
  331. fi
  332. # set separate list count & get overall count
  333. #
  334. for list in $(ls -Sr "${adb_dnsdir}/${adb_dnsprefix}."*)
  335. do
  336. list="${list/*./}"
  337. count="$(head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}.${list}" | wc -l)"
  338. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  339. then
  340. count=$((count / 2))
  341. fi
  342. if [ "$(tail -qn 1 "${adb_dnsdir}/${adb_dnsprefix}.${list}")" = "##" ]
  343. then
  344. last_line="# ${0##*/} (${adb_scriptver}) - ${count} ad\/abuse domains blocked"
  345. sed -i "s/^##$/${last_line}/" "${adb_dnsdir}/${adb_dnsprefix}.${list}"
  346. fi
  347. adb_count=$((adb_count + count))
  348. done
  349. # restart dnsmasq with newly generated or deleted adblock lists,
  350. # check dnsmasq startup afterwards
  351. #
  352. if [ -n "${adb_revsrclist}" ] || [ -n "${rm_done}" ] || [ -n "${restore_done}" ]
  353. then
  354. /etc/init.d/dnsmasq restart
  355. sleep 1
  356. rc="$(ps | grep -q "[d]nsmasq"; printf ${?})"
  357. if [ $((rc)) -eq 0 ]
  358. then
  359. f_log "adblock lists with overall ${adb_count} domains loaded"
  360. else
  361. rc=100
  362. f_log "dnsmasq restart failed, please check 'logread' output" "${rc}"
  363. f_restore
  364. fi
  365. else
  366. f_log "adblock lists with overall ${adb_count} domains are still valid, no dnsmasq restart required"
  367. fi
  368. # create adblock list backups
  369. #
  370. if [ "${backup_ok}" = "true" ] && [ -n "${adb_revsrclist}" ] && [ "$(printf "${adb_dnsdir}/${adb_dnsprefix}."*)" != "${adb_dnsdir}/${adb_dnsprefix}.*" ]
  371. then
  372. backup_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec cp -pf "{}" "${adb_backupdir}" \;)"
  373. rc=${?}
  374. if [ $((rc)) -eq 0 ] && [ -n "${backup_done}" ]
  375. then
  376. f_log "new adblock list backups generated"
  377. elif [ $((rc)) -ne 0 ]
  378. then
  379. f_log "error during backup of adblock lists" "${rc}"
  380. f_exit
  381. fi
  382. fi
  383. # remove temporary files and exit
  384. #
  385. f_exit