#!/bin/bash
#  _   _           _       _         ____  _            _    _     _     _  
# | | | |_ __   __| | __ _| |_ ___  | __ )| | __ _  ___| | _| |   (_)___| |_
# | | | | '_ \ / _` |/ _` | __/ _ \ |  _ \| |/ _` |/ __| |/ / |   | / __| __|
# | |_| | |_) | (_| | (_| | ||  __/ | |_) | | (_| | (__|   <| |___| \__ \ |_
#  \___/| .__/ \__,_|\__,_|\__\___| |____/|_|\__,_|\___|_|\_\_____|_|___/\__|
#       |_|                                                                  
#
# This script update your blacklist for dansguardian, just add your category for update
# Please execute this script manually for debbug before conf your crontab
#
# Install this script in a crontab, for example:
# 0 4 * * * /sbin/update_blacklist.sh &> /dev/null
#
# Depend:
# * curl, bash, tar, sed, grep, wget, sort
#
# Made for : Jhonever León    > jhoneverleon at gmail dot com
#          : Edwind Contreras > richzendy at gmail dot com
#                                                                                                        
# Este programa es protegido por GNU/GPl, si no sabe lo que es, obtenga una copia en castellano en:      
#                                                                                                        
# http://www.es.gnu.org/modules/content/print.php?id=8
#                                                                                                        
# Por favor distribuya este programa a quien sea por el medio que sea... HAGALO!                                
# Por favor modifique este programa y comparta las modificaciones.                                        
# Use este programa como le de la gana.                                                                  
#                              
# Version 1.0                                                                          
###########################################################################################################



# Categorias actualmente en funcionamiento
# Categories running in your proxy
CATEGORIAS='ads audio-video dialers filehosting games malware phishing proxy warez webmail adult chat entertainment gambling instantmessaging onlinegames porn socialnetworking virusinfected weather'

# Descargar información para el nuevo archivo
# Download info for new blacklist file

INFO_BLACKLIST=`curl -s 'http://urlblacklist.com/cgi-bin/commercialdownload.pl?type=information&file=bigblacklist'`

# Crear variables
# Create variables
PATH_ORIG='/etc/dansguardian/blacklists'
PATH_TMP='/tmp/blacklists'
FECHA_ACTUAL=`echo $INFO_BLACKLIST | cut -f 1 -d , | sed 's/"//g'`
SUMA_MD5=`echo $INFO_BLACKLIST | cut -f 2 -d , | sed 's/"//g'`
SIZE=`echo $INFO_BLACKLIST | cut -f 3 -d , | sed 's/"//g'`

if  [ -s /etc/dansguardian/last_blacklist_update ] ; then

FECHA_LAST=`cat /etc/dansguardian/last_blacklist_update`

if [ -z $FECHA_ACTUAL ] || [ -z $SUMA_MD5 ] || [ -z $SIZE ] ; then

echo "ERROR: Download error in info file..."

else

if [ "$FECHA_ACTUAL" != $FECHA_LAST ] ; then

echo "Downloading blacklist file..."
cd /tmp
/usr/bin/wget -c 'http://urlblacklist.com/cgi-bin/commercialdownload.pl?type=download&file=bigblacklist' -O bigblacklist.tar.gz &> /dev/null

SUMA_MD5_NEW=`md5sum /tmp/bigblacklist.tar.gz | cut -f 1 -d " "`

if [ "$SUMA_MD5_NEW" == $SUMA_MD5 ] ; then

echo "Extracting blacklist file..."
/bin/tar -xvzf bigblacklist.tar.gz


# Revisar categorias

for i in $CATEGORIAS ; do
echo "Processing Category => $i"

/bin/sort -u $PATH_TMP/$i/domains $PATH_ORIG/$i/domains > /tmp/domains
/bin/mv -f /tmp/domains $PATH_ORIG/$i/domains

/bin/sort -u $PATH_TMP/$i/urls $PATH_ORIG/$i/urls > /tmp/urls
/bin/mv -f /tmp/urls $PATH_ORIG/$i/urls

echo "Update in my blackist file for category => $i, finished..."

done
echo $FECHA_ACTUAL > /etc/dansguardian/last_blacklist_update
/etc/init.d/dansguardian restart
else
echo "ERROR: md5 sum not valid!!! or Download attempts exceeded for this IP today - try again tomorrow"
fi
else
echo "No new blacklist updates in internet!"
fi
fi
else
echo "File => /etc/dansguardian/last_blacklist_update no detect, creating..."
echo '1971-01-01' > /etc/dansguardian/last_blacklist_update
echo "Run this script again, please :-D"
fi