Subversion Repositories ALCASAR

Rev

Rev 386 | Rev 497 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

#/bin/sh
# $Id: alcasar-bl.sh 412 2011-01-03 21:40:09Z richard $

# Script de gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via dansguardian)
# By 3abtux & rexy

DIR_tmp="/tmp/blacklists"
FILE_tmp="/tmp/fileFilter.txt"
DIR_DG="/etc/dansguardian/lists"
DIR_DG_BL="$DIR_DG/blacklists"
BL_CATEGORIES="/usr/local/etc/alcasar-bl-categories"
BL_CATEGORIES_ENABLED="/usr/local/etc/alcasar-bl-categories-enabled"
DIR_DNS_FILTER_AVAILABLE="/usr/local/etc/alcasar-dnsfilter-available"
DIR_DNS_FILTER_ENABLED="/usr/local/etc/alcasar-dnsfilter-enabled"
IP_RETOUR="192.168.182.1"
BL_SERVER="cri.univ-tlse1.fr"
SED="/bin/sed -i"
# Récupération de l'archive de la BL Toulouse
function transfert () {
        mkdir -p $DIR_tmp
        cd $DIR_tmp
        wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
}

# Décompression de la BL (en conservant la WL)
function install () {
        [ -d $DIR_DG ] || mkdir -p $DIR_DG
        [ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
        tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
        [ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
        cd /root
        rm -rf $DIR_tmp
}

# Adaptation de la BL Toulouse à la structure Dnsmasq
function adapt () {
        # On récupère le nom des répertoire (catégories)
        find $DIR_DG_BL/ -type f -name domains > $BL_CATEGORIES
        # On supprime le suffice "/domains"
        $SED "s?\/domains??g" $BL_CATEGORIES
        rm -f $DIR_DNS_FILTER_AVAILABLE/*
        echo -n "Adaptation de la BL Toulouse. Veuillez patienter : "
        # On copie les fichiers de domaine pour chaque catégorie
        for PATH_FILE in `cat $BL_CATEGORIES`
        do
                DOMAINE=`basename $PATH_FILE`
                echo -n "."
                # suppression des @IP, des lignes commentées et des caractères bizarres comme les ô et û ö ü
                egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > /tmp/dnsmasq-bl.tmp
                $SED "/[äâëêïîöôüû]/d" /tmp/dnsmasq-bl.tmp
                $SED "/^#.*/d" /tmp/dnsmasq-bl.tmp
                # Mise en forme dnsmasq
                $SED "s?.*?address=/&/$IP_RETOUR?g" /tmp/dnsmasq-bl.tmp
                mv /tmp/dnsmasq-bl.tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
        done
}

# Permet d'activer/désactiver les catégories de la BL
function cat_choice (){
        # un peu de ménage
        rm -rf $DIR_DNS_FILTER_ENABLED/*
        $SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
        # on adapte le fichier $BL_CATEGORIES au choix de catégorie 
        $SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
        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
        done
        sort -k2n $BL_CATEGORIES > $FILE_tmp
        mv $FILE_tmp $BL_CATEGORIES
        # on affecte les catégories à dansguardian et dnsmasq
        for i in `cat $BL_CATEGORIES_ENABLED`
                do
                ln -s $DIR_DNS_FILTER_AVAILABLE/$i.conf $DIR_DNS_FILTER_ENABLED/$i
                # echo ".Include<$DIR_DANSGUARDIAN/blacklists/$i/domains>" >> $DIR_DANSGUARDIAN/bannedsitelist
                echo ".Include<$DIR_DG_BL/$i/urls>" >> $DIR_DG/bannedurllist
                done
}
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload - --reload }"
nb_args=$#
args=$1
if [ $nb_args -eq 0 ]
then
        nb_args=1
        args="-h"
fi
case $args in
        -\? | -h* | --h*)
                echo "$usage"
                exit 0
                ;;
        # activation du filtrage
        -on | --on)     
                cat_choice
                $SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
                if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # on ne relance pas les processus d'une install
                then
                service dansguardian restart
                service dnsmasq restart
                fi
                ;;
        # désactivation du filtrage
        -off | --off)
                rm -rf $DIR_DNS_FILTER_ENABLED/*
                $SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
                if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # on ne relance pas les processus lors d'une install
                then
                service dansguardian restart
                service dnsmasq restart
                fi
                ;;
        # Mise a jour de la blacklist 'Toulouse' et adaptation à dansguardian et dnsmasq
        -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 
                        transfert
                        install
                        chown -R dansguardian:apache $DIR_DG
                        chmod -R g+w $DIR_DG
                        DATE=`date '+%d %B %Y - %Hh%M'`
                        echo "Univ-tlse du $DATE " > /var/www/html/VERSION-BL
                        rm -rf /tmp/con_ok.html
                fi
                adapt
                ;;              
        # regénération suite à modification (choix catégories ou BL secondaire)
        -reload | --reload)
                adapt
                # pour Dansguardian
                chown -R dansguardian:apache $DIR_DG_BL/ossi
                chmod -R g+w $DIR_DG_BL/ossi
                cat_choice
                service dansguardian restart
                # pour dnsmasq (noms de domaine réhabilités)
                if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
                then
                        for i in `cat $DIR_DG/exceptionsitelist`
                        do
                                $SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
                        done
                fi
                cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
                $SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
                service dnsmasq restart
;;
        *)
                echo "Argument inconnu :$1";
                echo "$usage"
                exit 1
                ;;
esac