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.

305 lines
11 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.3.3"
  12. adb_mincfgver="2.2"
  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="$(cat "${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="$(cat "${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}")"
  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. # finish domain processing, prepare find statement with revised block list source
  189. #
  190. if [ $((rc)) -eq 0 ]
  191. then
  192. if [ -z "${adb_revsrclist}" ]
  193. then
  194. adb_revsrclist="-name ${adb_dnsprefix}.${src_name}"
  195. else
  196. adb_revsrclist="${adb_revsrclist} -o -name ${adb_dnsprefix}.${src_name}"
  197. fi
  198. f_log " domain merging finished"
  199. else
  200. rc=0
  201. rm -f "${adb_dnsfile}"
  202. if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
  203. then
  204. rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
  205. fi
  206. "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
  207. "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=domain merging failed"
  208. f_log " domain merging failed, skipped"
  209. continue
  210. fi
  211. else
  212. rm -f "${adb_dnsfile}"
  213. if [ "${backup_ok}" = "true" ] && [ -r "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz" ]
  214. then
  215. rm -f "${adb_dir_backup}/${adb_dnsprefix}.${src_name}.gz"
  216. fi
  217. "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
  218. "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=empty domain input"
  219. f_log " empty domain input, skipped"
  220. continue
  221. fi
  222. done
  223. # make separate block lists entries unique
  224. #
  225. if [ "${mem_ok}" = "true" ] && [ -n "${adb_revsrclist}" ]
  226. then
  227. f_log "remove duplicates in separate block lists"
  228. # generate a unique overall block list
  229. #
  230. sort -u "${adb_dnsdir}/${adb_dnsprefix}"* > "${adb_tmpdir}/blocklist.overall"
  231. # loop through all separate lists, ordered by size (ascending)
  232. #
  233. for list in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
  234. do
  235. # check overall block list vs. separate block list,
  236. # write all duplicate entries to separate list
  237. #
  238. list="${list/*./}"
  239. sort "${adb_tmpdir}/blocklist.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" | uniq -d > "${adb_tmpdir}/tmp.${list}"
  240. mv -f "${adb_tmpdir}/tmp.${list}" "${adb_dnsdir}/${adb_dnsprefix}.${list}"
  241. # write all unique entries back to overall block list
  242. #
  243. sort "${adb_tmpdir}/blocklist.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" | uniq -u > "${adb_tmpdir}/tmp.overall"
  244. mv -f "${adb_tmpdir}/tmp.overall" "${adb_tmpdir}/blocklist.overall"
  245. done
  246. rm -f "${adb_tmpdir}/blocklist.overall"
  247. fi
  248. # restart & check dnsmasq with newly generated set of block lists
  249. #
  250. if [ -n "${adb_revsrclist}" ] || [ -n "${mv_done}" ] || [ "${rm_done}" = "true" ]
  251. then
  252. "${adb_uci}" -q delete "adblock.global.adb_dnstoggle"
  253. /etc/init.d/dnsmasq restart
  254. sleep 1
  255. check="$(pgrep -f "dnsmasq")"
  256. if [ -n "${check}" ]
  257. then
  258. dns_ok="true"
  259. else
  260. f_log "dnsmasq restart failed, retry without newly generated block lists"
  261. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \;)"
  262. if [ -n "${rm_done}" ]
  263. then
  264. /etc/init.d/dnsmasq restart
  265. sleep 1
  266. check="$(pgrep -f "dnsmasq")"
  267. if [ -n "${check}" ]
  268. then
  269. dns_ok="true"
  270. fi
  271. fi
  272. fi
  273. if [ "${dns_ok}" = "true" ]
  274. then
  275. f_cntconfig
  276. f_log "block lists with overall ${adb_count} domains loaded"
  277. else
  278. rc=100
  279. f_log "dnsmasq restart finally failed, please check 'logread' output"
  280. f_exit
  281. fi
  282. else
  283. f_cntconfig
  284. f_log "block lists with overall ${adb_count} domains are still valid, no update required"
  285. fi
  286. # remove temporary files and exit
  287. #
  288. f_exit