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.

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