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.

277 lines
9.8 KiB

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