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.

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