Subversion Repositories ALCASAR

Rev

Rev 499 | 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 503 2011-03-11 22:12:32Z 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
}
386 franck 80
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload - --reload }"
1 root 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
386 franck 94
	-on | --on)	
311 richard 95
		cat_choice
1 root 96
		$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
497 richard 97
		$SED "s?^#\"?\"?g" $DIR_DG/urlregexplist # Enable 'safesearch'
98
		if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # on ne relance pas les processus lors d'une install
316 richard 99
		then
311 richard 100
		service dansguardian restart
499 richard 101
		service dnsmasq stop
102
		sleep 1
103
		service dnsmasq start
316 richard 104
		fi
1 root 105
		;;
308 richard 106
	# désactivation du filtrage
412 richard 107
	-off | --off)
311 richard 108
		rm -rf $DIR_DNS_FILTER_ENABLED/*
1 root 109
		$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
497 richard 110
		$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist # Disable 'safesearch'
323 richard 111
		if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # on ne relance pas les processus lors d'une install
316 richard 112
		then
113
		service dansguardian restart
499 richard 114
		service dnsmasq stop
115
		sleep 1
116
		service dnsmasq start
316 richard 117
		fi
1 root 118
		;;
308 richard 119
	# Mise a jour de la blacklist 'Toulouse' et adaptation à dansguardian et dnsmasq
386 franck 120
	-download | --download)
1 root 121
		rm -rf /tmp/con_ok.html
122
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
123
		if [ ! -e /tmp/con_ok.html ]
124
		then
125
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
126
		else 
127
			transfert
128
			install
316 richard 129
			chown -R dansguardian:apache $DIR_DG
130
			chmod -R g+w $DIR_DG
1 root 131
			DATE=`date '+%d %B %Y - %Hh%M'`
34 richard 132
			echo "Univ-tlse du $DATE " > /var/www/html/VERSION-BL
1 root 133
			rm -rf /tmp/con_ok.html
134
		fi
308 richard 135
		adapt
1 root 136
		;;		
313 richard 137
	# regénération suite à modification (choix catégories ou BL secondaire)
386 franck 138
	-reload | --reload)
359 richard 139
		adapt
308 richard 140
		# pour Dansguardian
316 richard 141
		chown -R dansguardian:apache $DIR_DG_BL/ossi
142
		chmod -R g+w $DIR_DG_BL/ossi
311 richard 143
		cat_choice
314 richard 144
		service dansguardian restart
311 richard 145
		# pour dnsmasq (noms de domaine réhabilités)
316 richard 146
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 147
		then
316 richard 148
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 149
			do
313 richard 150
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
311 richard 151
			done
152
		fi
316 richard 153
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
308 richard 154
		$SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
499 richard 155
		service dnsmasq stop
156
		sleep 1
157
		service dnsmasq start
503 richard 158
		;;
1 root 159
	*)
160
		echo "Argument inconnu :$1";
161
		echo "$usage"
162
		exit 1
163
		;;
164
esac
165