Subversion Repositories ALCASAR

Rev

Rev 311 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1 root 1
#/bin/sh
63 franck 2
# $Id: alcasar-bl.sh 313 2010-11-08 22:41:19Z richard $
3
 
308 richard 4
# Script de gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via dansguardian)
5
# By 3abtux & rexy
1 root 6
 
308 richard 7
DIR_tmp="/tmp/blacklists"
8
FILE_tmp="/tmp/fileFilter.txt"
311 richard 9
DIR_DANSGUARDIAN="/etc/dansguardian/lists"
313 richard 10
BL_CATEGORIES="/usr/local/etc/alcasar-bl-categories"
11
BL_CATEGORIES_ENABLED="/usr/local/etc/alcasar-bl-categories-enabled"
308 richard 12
DIR_DNS_FILTER_AVAILABLE="/usr/local/etc/alcasar-dnsfilter-available"
309 richard 13
DIR_DNS_FILTER_ENABLED="/usr/local/etc/alcasar-dnsfilter-enabled"
14
IP_RETOUR="192.168.182.1"
1 root 15
BL_SERVER="cri.univ-tlse1.fr"
16
SED="/bin/sed -i"
308 richard 17
# Récupération de l'archive de la BL Toulouse
1 root 18
function transfert () {
19
	mkdir -p $DIR_tmp
20
	cd $DIR_tmp
21
	wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
22
}
23
 
308 richard 24
# Décompression de la BL (en conservant la WL)
1 root 25
function install () {
26
	[ -d $DIR_DANSGUARDIAN ] || mkdir -p $DIR_DANSGUARDIAN
27
	[ -d $DIR_DANSGUARDIAN/blacklists/ossi ] && mv -f $DIR_DANSGUARDIAN/blacklists/ossi $DIR_tmp
311 richard 28
	tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DANSGUARDIAN/
1 root 29
	[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DANSGUARDIAN/blacklists/
30
	cd /root
31
	rm -rf $DIR_tmp
32
}
33
 
308 richard 34
# Adaptation de la BL Toulouse à la structure Dnsmasq
35
function adapt () {
311 richard 36
	# On récupère le nom des répertoire (catégories)
313 richard 37
	find $DIR_DANSGUARDIAN -type f -name domains > $BL_CATEGORIES
311 richard 38
	# On supprime le suffice "/domains"
313 richard 39
	$SED "s?\/domains??g" $BL_CATEGORIES
311 richard 40
	rm -f $DIR_DNS_FILTER_AVAILABLE/*
41
	echo -n "Adaptation de la BL Toulouse. Veuillez patienter : "
42
	# On copie les fichiers de domaine pour chaque catégorie
313 richard 43
	for PATH_FILE in `cat $BL_CATEGORIES`
311 richard 44
	do
45
        	DOMAINE=`basename $PATH_FILE`
46
		echo -n "."
47
		# suppression des @IP, des lignes commentées et des caractères bizarres comme les ô et û ö ü
48
		egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > /tmp/dnsmasq-bl.tmp
49
		$SED "/[äâëêïîöôüû]/d" /tmp/dnsmasq-bl.tmp
50
		$SED "/^#.*/d" /tmp/dnsmasq-bl.tmp
51
		# Mise en forme dnsmasq
52
		$SED "s?.*?address=/&/$IP_RETOUR?g" /tmp/dnsmasq-bl.tmp
53
		mv /tmp/dnsmasq-bl.tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
54
	done
308 richard 55
}
56
 
57
# Permet d'activer/désactiver les catégories de la BL
58
function cat_choice (){
311 richard 59
	# un peu de ménage
60
	rm -rf $DIR_DNS_FILTER_ENABLED/*
61
	$SED "/\.Include/d" $DIR_DANSGUARDIAN/bannedsitelist $DIR_DANSGUARDIAN/bannedurllist
313 richard 62
	# on adapte le fichier $BL_CATEGORIES au choix de catégorie 
63
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
64
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
65
	do
66
		$SED "/^.*$ENABLE_CATEGORIE/d" $BL_CATEGORIES
67
		$SED "1i\/etc\/dansguardian\/lists\/blacklist\/$ENABLE_CATEGORIE" $BL_CATEGORIES
68
	done
69
	sort -o $FILE_tmp $BL_CATEGORIES 
70
	mv $FILE_tmp $BL_CATEGORIES
71
	# on affecte les catégories à dansguardian et dnsmasq
72
	for i in `cat $BL_CATEGORIES_ENABLED`
311 richard 73
		do
74
		ln -s $DIR_DNS_FILTER_AVAILABLE/$i.conf $DIR_DNS_FILTER_ENABLED/$i
75
		# echo ".Include<$DIR_DANSGUARDIAN/blacklists/$i/domains>" >> $DIR_DANSGUARDIAN/bannedsitelist
76
		echo ".Include<$DIR_DANSGUARDIAN/blacklists/$i/urls>" >> $DIR_DANSGUARDIAN/bannedurllist
77
		done
308 richard 78
}
1 root 79
usage="Usage: alcasar-bl.sh -on | -off | -download| -reload"
80
nb_args=$#
81
args=$1
82
if [ $nb_args -eq 0 ]
83
then
84
	nb_args=1
85
	args="-h"
86
fi
87
case $args in
88
	-\? | -h* | --h*)
89
		echo "$usage"
90
		exit 0
91
		;;
308 richard 92
	# activation du filtrage
1 root 93
	-on)	
311 richard 94
		cat_choice
1 root 95
		$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
311 richard 96
		service dansguardian restart
260 richard 97
		service dnsmasq restart
1 root 98
		;;
308 richard 99
	# désactivation du filtrage
1 root 100
	-off)
311 richard 101
		rm -rf $DIR_DNS_FILTER_ENABLED/*
1 root 102
		$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
103
		service dansguardian reload
260 richard 104
		service dnsmasq restart
1 root 105
		;;
308 richard 106
	# Mise a jour de la blacklist 'Toulouse' et adaptation à dansguardian et dnsmasq
1 root 107
	-download)
108
		rm -rf /tmp/con_ok.html
109
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
110
		if [ ! -e /tmp/con_ok.html ]
111
		then
112
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
113
		else 
114
			transfert
115
			install
116
			chown -R dansguardian:apache $DIR_DANSGUARDIAN
117
			chmod -R g+w $DIR_DANSGUARDIAN
118
			DATE=`date '+%d %B %Y - %Hh%M'`
34 richard 119
			echo "Univ-tlse du $DATE " > /var/www/html/VERSION-BL
1 root 120
			rm -rf /tmp/con_ok.html
121
		fi
308 richard 122
		adapt
1 root 123
		;;		
313 richard 124
	# regénération suite à modification (choix catégories ou BL secondaire)
1 root 125
	-reload)
308 richard 126
		# pour Dansguardian
1 root 127
		chown -R dansguardian:apache $DIR_DANSGUARDIAN/blacklists/ossi
128
		chmod -R g+w $DIR_DANSGUARDIAN/blacklists/ossi
311 richard 129
		cat_choice
1 root 130
		service dansguardian reload
311 richard 131
		# pour dnsmasq (noms de domaine réhabilités)
132
		if [ `wc -w $DIR_DANSGUARDIAN/exceptionsitelist|cut -d " " -f1` != "0" ]
133
		then
134
			for i in `cat $DIR_DANSGUARDIAN/exceptionsitelist`
135
			do
313 richard 136
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
311 richard 137
			done
138
		fi
313 richard 139
		cp -f $DIR_DANSGUARDIAN/blacklists/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
308 richard 140
		$SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
141
		service dnsmasq reload
142
;;
1 root 143
	*)
144
		echo "Argument inconnu :$1";
145
		echo "$usage"
146
		exit 1
147
		;;
148
esac
149