Subversion Repositories ALCASAR

Rev

Rev 1912 | Rev 1914 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

#/bin/bash

# $Id: alcasar-bl.sh 1913 2016-05-29 14:44:38Z richard $

# alcasar-bl.sh
# by Franck BOUIJOUX and Richard REY
# This script is distributed under the Gnu General Public License (GPL)

# Gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via Dansguardian)
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (Dansguardian)

DIR_CONF="/usr/local/etc"
CONF_FILE="$DIR_CONF/alcasar.conf"
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`                      # ALCASAR LAN IP address
DIR_tmp="/tmp/blacklists"
DIR_WL_tmp="/tmp/whitelists"
FILE_tmp="/tmp/filesfilter.txt"
FILE_ip_tmp="/tmp/filesipfilter.txt"
DIR_DG="/etc/dansguardian/lists"
DIR_DG_BL="$DIR_DG/blacklists"
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"                         # list of names of the  BL categories
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"                         #'      '               WL      '
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"         #       '       '       BL enabled categories
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"         #       '       '       WL enabled categories
DIR_SHARE="/usr/local/share"
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"                                      # all the BL in the DNSMASQ format
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"                                      # all the WL    '       '       '
DIR_IP_BL="$DIR_SHARE/iptables-bl"                                      # all the IP addresses of the BL 
DIR_IP_WL="$DIR_SHARE/iptables-wl"                                      # IP ossi disabled WL 
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"                      # symbolic link to the domains BL (only enabled categories)
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"                      #       '       '       '       WL      '       '       '
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"                      #       '       '       ip BL (only enabled categories)
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"                      #       '       '       ip WL (ossi and ossi-* imported from ACC)
tmp_DIR_IP_BL="/tmp/ossi-iptables-bl"                                   #IP BL tmp directory to keep ossi files during an update
tmp_DIR_IP_WL="/tmp/ossi-iptables-wl"                                   #IP WL tmp directory to keep ossi files during an update
tmp_DIR_DNS_BL="/tmp/ossi-dnsmasq-bl"                                   #DNS BL tmp directory to keep ossi files during an update
tmp_DIR_DNS_WL="/tmp/ossi-dnsmasq-wl"                                   #DNS WL tmp directory to keep ossi files during an update
#BL tmp directory to keep ossi files
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"                           # conf file of dnsmasq-blacklist
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2`                         # server DNS1 (for WL domain names)
BL_SERVER="dsi.ut-capitole.fr"
SED="/bin/sed -i"

# enable/disable the BL & WL categories
function cat_choice (){
        mkdir $DIR_tmp
        #ip BL ENABLE
        if [ -d $DIR_IP_BL_ENABLED ]
        then
                for file in `ls -1 $DIR_IP_BL_ENABLED | grep -v "^ossi-*"`
                do
                        rm -f $DIR_IP_BL_ENABLED/$file
                done
        else
                mkdir $DIR_IP_BL_ENABLED
                chown root:apache $DIR_IP_BL_ENABLED
                chmod 770 $DIR_IP_BL_ENABLED
        fi

        #dns BL ENABLED
        if [ -d $DIR_DNS_BL_ENABLED ]
        then
                for file in `ls -1 $DIR_DNS_BL_ENABLED | grep -v "^ossi-*"`
                do
                        rm -f $DIR_DNS_BL_ENABLED/$file
                done
        else
                mkdir $DIR_DNS_BL_ENABLED
                chown root:apache $DIR_DNS_BL_ENABLED
                chmod 770 $DIR_DNS_BL_ENABLED
        fi
        #dns BL ossi.conf 
        if [ ! -e $DIR_DNS_BL/ossi.conf ]
        then
                touch $DIR_DNS_BL/ossi.conf
                chown root:apache $DIR_DNS_BL/ossi.conf
                chmod g+w $DIR_DNS_BL/ossi.conf
                if [ ! -e $DIR_DNS_BL_ENABLED/ossi ]
                then
                        ln -s $DIR_DNS_BL/ossi.conf $DIR_DNS_BL_ENABLED/ossi
                fi
        fi

        #ip WL ENABLE
        if [ -d $DIR_IP_WL_ENABLED ]
        then
                for file in `ls -1 $DIR_IP_WL_ENABLED | grep -v "^ossi*"`
                do
                        rm -f $DIR_IP_WL_ENABLED/$file
                done
        else
                mkdir $DIR_IP_WL_ENABLED
                touch $DIR_IP_WL_ENABLED/ossi
                chown -R root:apache $DIR_IP_WL_ENABLED
                chmod 770 $DIR_IP_WL_ENABLED
                chmod g+w $DIR_IP_WL_ENABLED/ossi
        fi

        #dns WL ENABLED
        if [ -d $DIR_DNS_WL_ENABLED ]
        then
                for file in `ls -1 $DIR_DNS_WL_ENABLED | grep -v "^ossi-*"`
                do
                        rm -f $DIR_DNS_WL_ENABLED/$file
                done
        else
                mkdir $DIR_DNS_WL_ENABLED
                chown root:apache $DIR_DNS_WL_ENABLED
                chmod 770 $DIR_DNS_WL_ENABLED
        fi
        #dns WL ossi.conf 
        if [ ! -e $DIR_DNS_WL/ossi.conf ]
        then
                touch $DIR_DNS_WL/ossi.conf
                chown root:apache $DIR_DNS_WL/ossi.conf
                chmod g+w $DIR_DNS_WL/ossi.conf
                if [ ! -e $DIR_DNS_WL_ENABLED/ossi ]
                then
                        ln -s $DIR_DNS_WL/ossi.conf $DIR_DNS_WL_ENABLED/ossi
                fi
        fi

        # update categories with rsync
        if [ ! -e $DIR_CONF/update_cat.conf ]
        then
                touch $DIR_CONF/update_cat.conf
                chown root:apache $DIR_CONF/update_cat.conf
                chmod 660 $DIR_CONF/update_cat.conf
        fi
        $SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
        $SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)

        # process the file $BL_CATEGORIES with the choice of categories 
        for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
        do
                $SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
                $SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
                ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
                ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
                # echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
                echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
        done
        sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
        mv $FILE_tmp $BL_CATEGORIES

        # process the file $WL_CATEGORIES with the choice of categories 
        for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
        do
                $SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
                $SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
                ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
        done
        sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
        mv $FILE_tmp $WL_CATEGORIES

        # restoring ip files and ossi category BL/WL
        mv $DIR_tmp/ossi $DIR_IP_BL
        chown apache $DIR_IP_BL/ossi
        rm -rf $DIR_tmp
}

usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
nb_args=$#
args=$1
if [ $nb_args -eq 0 ]
then
          args="-h"
fi
case $args in
        -\? | -h* | --h*)
                echo "$usage"
                exit 0
                ;;
        # Retrieve Toulouse University BL
        -download | --download)
                rm -rf /tmp/con_ok.html
                `/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
                if [ ! -e /tmp/con_ok.html ]
                then
                        echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
                else 
                        rm -rf /tmp/con_ok.html $DIR_tmp
                        mkdir $DIR_tmp
                        wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
                        md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
                        chown -R apache:apache $DIR_tmp
                fi
                ;;              
        # enable/disable categories (used only during the alcasar install process)
        -cat_choice | --cat_choice)
                cat_choice
                ;;
        # Adapt Toulouse University BL to ALCASAR architecture (dnsmasq + DG + iptables)
        -adapt | --adapt)
                echo -n "Adaptation process of Toulouse University blackList. Please wait : "
                # keep custom files (ossi) only when updating he BL
                if [ -d $DIR_IP_BL -a -d $DIR_IP_WL -a -d $DIR_DNS_BL -a -d $DIR_DNS_WL ]
                then
                        for x in $(ls -1 $DIR_IP_BL | grep "^ossi*")
                        do
                                mv $DIR_IP_BL/$x $tmp_DIR_IP_BL
                        done
                        for x in $(ls -1 $DIR_IP_WL | grep "^ossi*")
                        do
                                mv $DIR_IP_WL/$x $tmp_DIR_IP_WL
                        done
                        for x in $(ls -1 $DIR_DNS_BL | grep "^ossi*")
                        do
                                mv $DIR_DNS_BL/$x $tmp_DIR_DNS_BL
                        done
                        for x in $(ls -1 $DIR_DNS_WL | grep "^ossi*")
                        do
                                mv $DIR_DNS_WL/$x $tmp_DIR_DNS_WL
                        done
                fi
                if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
                then
                        rm -rf $DIR_DG_BL $DIR_IP_BL
                        mkdir $DIR_DG_BL $DIR_IP_BL
                        tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
                fi
                rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
                rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
                touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
                mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
                chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
                chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
                find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
                $SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
                for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
                do
                        categorie=`echo $dir_categorie|cut -d "/" -f6`
                        categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
                        if [ "$categorie_type" == "white" ]
                        then
                                echo "$dir_categorie" >> $WL_CATEGORIES 
                                echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
                        fi
                        echo "$dir_categorie" >> $BL_CATEGORIES # By default all categories are in BL
                done
                rm -f $FILE_tmp
                # Verify that the enabled categories are effectively in the BL (need after an update of the BL)
                for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
                do
                        ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
                        if [ $ok != "1" ] 
                        then
                                $SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
                        fi
                done
                # Creation of DNSMASQ and Iptables BL and WL
                for LIST in $BL_CATEGORIES $WL_CATEGORIES       # for each list (bl and wl)
                do
                        for PATH_FILE in `cat $LIST` # for each category
                        do
                                DOMAINE=`basename $PATH_FILE`
                                echo -n "$DOMAINE, "
                                if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
                                then
                                        touch $PATH_FILE/urls
                                        chown dansguardian:apache $PATH_FILE/urls
                                fi
                                $SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
                                # extract ip addresses for iptables
                                awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
                                # for dnsmask, remove IP addesses, accented characters and commented lines.
                                egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
                                $SED "/[äâëêïîöôüû]/d" $FILE_tmp
                                $SED "/^#.*/d" $FILE_tmp
                                if [ "$LIST" == "$BL_CATEGORIES" ]
                                then
                                        # adapt to the dnsmasq syntax for the blacklist
                                        $SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
                                        mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
                                        mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
                                else
                                        # adapt to the dnsmasq syntax for the whitelist
                                        $SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
                                        mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
                                fi
                        done
                done
                rm -f $FILE_tmp $FILE_ip_tmp
                # Restoring custom files (ossi)
                rm -rf $DIR_tmp
                if [ -d $tmp_DIR_IP_BL -a -d $tmp_DIR_IP_WL -a -d $tmp_DIR_DNS_BL -a -d $tmp_DIR_DNS_WL ]
                then
                        for x in $(ls -1 $tmp_DIR_IP_BL | grep "^ossi*")
                        do
                                mv $tmp_DIR_IP_BL/$x $DIR_IP_BL
                        done
                        for x in $(ls -1 $tmp_DIR_IP_WL | grep "^ossi*")
                        do
                                mv $tmp_DIR_IP_WL/$x $DIR_IP_WL
                        done
                        for x in $(ls -1 $tmp_DIR_DNS_BL | grep "^ossi*")
                        do
                                mv $tmp_DIR_DNS_BL/$x $DIR_DNS_BL
                        done
                        for x in $(ls -1 $tmp_DIR_DNS_WL | grep "^ossi*")
                        do
                                mv $tmp_DIR_DNS_WL/$x $DIR_DNS_WL
                        done
                        rm -rf $tmp_DIR_IP_BL $tmp_DIR_IP_WL $tmp_DIR_DNS_BL $tmp_DIR_DNS_WL
                fi
                echo
                ;;
        # update the categories which are written in "/usr/local/etc/update_cat.conf" with rsync
        -update_cat | --update_cat)
                if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
                then
                        echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
                        cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
                        do
                                CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
                                URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
                                PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
                                rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
                                # Creation of DNSMASQ and Iptables BL and WL
                                DOMAINE=$(basename $PATH_FILE)
                                # correct some synthaxes
                                $SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls
                                # extract ip addresses for iptables
                                awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
                                # for dnsmask, remove IP addresses, accentuated characters and commented lines.
                                egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
                                $SED "/[äâëêïîöôüû]/d" $FILE_tmp
                                $SED "/^#.*/d" $FILE_tmp
                                black=`grep black $PATH_FILE/usage |wc -l`
                                if [ $black == "1" ]
                                then
                                        # adapt to the dnsmasq syntax for the blacklist
                                        $SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
                                        mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
                                        mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
                                else
                                        # adapt to the dnsmasq syntax for the whitelist
                                        $SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
                                        mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
                                fi
                                rm -f $FILE_tmp $FILE_ip_tmp
                        done
                        /usr/bin/systemctl restart dnsmasq-whitelist
                        /usr/bin/systemctl restart dnsmasq-blacklist
                        /usr/bin/systemctl restart dansguardian
                        /usr/local/bin/alcasar-iptables.sh
                else
                          echo -n "/usr/local/etc/update_cat.conf is empty ..."
                fi
                echo 
                ;;
        # reload when categories are changed 
        -reload | --reload)
                # for DG
                chown -R dansguardian:apache $DIR_DG_BL/ossi
                chmod -R g+w $DIR_DG_BL/ossi
                cat_choice
                #  for dnsmasq (rehabited domain names)
                if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
                then
                        for i in `cat $DIR_DG/exceptionsitelist`
                        do
                                $SED "/$i/d" $DIR_DNS_BL/*
                        done
                fi
                cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
                $SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
                $SED "s?.*?server=/&/$DNS1?g" $DIR_DNS_WL/ossi.conf
                
                /usr/bin/systemctl restart dnsmasq-blacklist
                /usr/bin/systemctl restart dnsmasq-whitelist
                /usr/local/bin/alcasar-iptables.sh
                ;;
        *)
                echo "Argument inconnu :$1";
                echo "$usage"
                exit 1
                ;;
esac