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.

418 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.91.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 "src_rset=\${rset_${src_name}}"
  174. count="$(printf "%s\n" "${tmp_domains}" | eval "${src_rset}" | 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 -u 2>/dev/null | eval "${adb_dnsformat}" 2>/dev/null > "${adb_dnsfile}"
  204. rc=${?}
  205. else
  206. sort -u "${adb_tmpfile}" 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. printf "%s\n" "##" >> "${adb_dnsfile}"
  224. f_log " domain merging finished"
  225. else
  226. f_log " domain merging failed" "${rc}"
  227. f_restore
  228. fi
  229. else
  230. f_log " empty domain input received"
  231. continue
  232. fi
  233. done
  234. # remove disabled adblock lists and their backups
  235. #
  236. if [ -n "${adb_srclist}" ]
  237. then
  238. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" \( ${adb_srclist} \) -print -exec rm -f "{}" \; 2>/dev/null)"
  239. rc=${?}
  240. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  241. then
  242. f_log "disabled adblock lists removed"
  243. if [ "${backup_ok}" = "true" ]
  244. then
  245. rm_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" \( ${adb_srclist} \) -print -exec rm -f "{}" \; 2>/dev/null)"
  246. rc=${?}
  247. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  248. then
  249. f_log "disabled adblock list backups removed"
  250. elif [ $((rc)) -ne 0 ]
  251. then
  252. f_log "error during removal of disabled adblock list backups" "${rc}"
  253. f_exit
  254. fi
  255. fi
  256. elif [ $((rc)) -ne 0 ]
  257. then
  258. f_log "error during removal of disabled adblock lists" "${rc}"
  259. f_exit
  260. fi
  261. else
  262. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec rm -f "{}" \; 2>/dev/null)"
  263. rc=${?}
  264. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  265. then
  266. f_log "all adblock lists removed"
  267. if [ "${backup_ok}" = "true" ]
  268. then
  269. rm_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec rm -f "{}" \; 2>/dev/null)"
  270. rc=${?}
  271. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  272. then
  273. f_log "all adblock list backups removed"
  274. elif [ $((rc)) -ne 0 ]
  275. then
  276. f_log "error during removal of all adblock list backups" "${rc}"
  277. f_exit
  278. fi
  279. fi
  280. elif [ $((rc)) -ne 0 ]
  281. then
  282. f_log "error during removal of all adblock lists" "${rc}"
  283. f_exit
  284. fi
  285. fi
  286. # partial restore of adblock lists in case of download errors
  287. #
  288. if [ "${backup_ok}" = "true" ] && [ -n "${adb_errsrclist}" ]
  289. then
  290. restore_done="$(find "${adb_backupdir}" -maxdepth 1 -type f \( ${adb_errsrclist} \) -print -exec cp -pf "{}" "${adb_dnsdir}" \; 2>/dev/null)"
  291. rc=${?}
  292. if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
  293. then
  294. f_log "partial restore done"
  295. elif [ $((rc)) -ne 0 ]
  296. then
  297. f_log "error during partial restore" "${rc}"
  298. f_exit
  299. fi
  300. fi
  301. # make separate adblock lists entries unique
  302. #
  303. if [ "${mem_ok}" != "false" ]
  304. then
  305. if [ -n "${adb_revsrclist}" ]
  306. then
  307. f_log "remove duplicates in separate adblock lists"
  308. # generate a temporary unique overall list
  309. #
  310. head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null | sort -u 2>/dev/null > "${adb_dnsdir}/tmp.overall"
  311. # loop through all separate lists, ordered by size (ascending)
  312. #
  313. for list in $(ls -Sr "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null)
  314. do
  315. # check original separate list vs. temporary overall list,
  316. # rewrite only duplicate entries back to temporary separate list
  317. #
  318. list="${list/*./}"
  319. sort "${adb_dnsdir}/tmp.overall" "${adb_dnsdir}/${adb_dnsprefix}.${list}" 2>/dev/null | uniq -d 2>/dev/null > "${adb_dnsdir}/tmp.${list}"
  320. # rewrite only unique entries back to temporary overall list
  321. #
  322. tmp_unique="$(sort "${adb_dnsdir}/tmp.overall" "${adb_dnsdir}/tmp.${list}" 2>/dev/null | uniq -u 2>/dev/null)"
  323. printf "%s\n" "${tmp_unique}" > "${adb_dnsdir}/tmp.overall"
  324. # write unique result back to original separate list (with list footer)
  325. #
  326. tail -qn 3 "${adb_dnsdir}/$adb_dnsprefix.${list}" 2>/dev/null >> "${adb_dnsdir}/tmp.${list}"
  327. mv -f "${adb_dnsdir}/tmp.${list}" "${adb_dnsdir}/${adb_dnsprefix}.${list}" >/dev/null 2>&1
  328. done
  329. rm -f "${adb_dnsdir}/tmp.overall" >/dev/null 2>&1
  330. fi
  331. fi
  332. # set separate list count & get overall count
  333. #
  334. for list in $(ls -Sr "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null)
  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 >/dev/null 2>&1
  355. sleep 1
  356. dns_status="$(ps 2>/dev/null | grep "[d]nsmasq" 2>/dev/null)"
  357. if [ -n "${dns_status}" ]
  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}" \; 2>/dev/null)"
  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