Subversion Repositories ALCASAR

Rev

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