Subversion Repositories ALCASAR

Rev

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