Subversion Repositories ALCASAR

Rev

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

Rev Author Line No. Line
672 richard 1
#/bin/bash
63 franck 2
# $Id: alcasar-bl.sh 672 2011-07-08 15:34:22Z richard $
3
 
672 richard 4
# alcasar-bl.sh
5
# by Franck BOUIJOUX and Richard REY
6
# This script is distributed under the Gnu General Public License (GPL)
1 root 7
 
672 richard 8
# Gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via Dansguardian)
9
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (Dansguardian)
10
 
612 richard 11
CONF_FILE="/usr/local/etc/alcasar.conf"
308 richard 12
DIR_tmp="/tmp/blacklists"
13
FILE_tmp="/tmp/fileFilter.txt"
316 richard 14
DIR_DG="/etc/dansguardian/lists"
15
DIR_DG_BL="$DIR_DG/blacklists"
313 richard 16
BL_CATEGORIES="/usr/local/etc/alcasar-bl-categories"
17
BL_CATEGORIES_ENABLED="/usr/local/etc/alcasar-bl-categories-enabled"
308 richard 18
DIR_DNS_FILTER_AVAILABLE="/usr/local/etc/alcasar-dnsfilter-available"
309 richard 19
DIR_DNS_FILTER_ENABLED="/usr/local/etc/alcasar-dnsfilter-enabled"
634 richard 20
IP_RETOUR="20.0.0.1"
1 root 21
BL_SERVER="cri.univ-tlse1.fr"
22
SED="/bin/sed -i"
637 richard 23
 
308 richard 24
# Récupération de l'archive de la BL Toulouse
1 root 25
function transfert () {
26
	mkdir -p $DIR_tmp
27
	cd $DIR_tmp
28
	wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
29
}
308 richard 30
# Décompression de la BL (en conservant la WL)
1 root 31
function install () {
316 richard 32
	[ -d $DIR_DG ] || mkdir -p $DIR_DG
33
	[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
648 richard 34
	[ -d $DIR_DG_BL/ip ] && mv -f $DIR_DG_BL/ip $DIR_tmp
316 richard 35
	tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
36
	[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
648 richard 37
	[ -d $DIR_tmp/ip ] && mv -f $DIR_tmp/ip $DIR_DG_BL/
1 root 38
	cd /root
39
	rm -rf $DIR_tmp
40
}
308 richard 41
# Adaptation de la BL Toulouse à la structure Dnsmasq
42
function adapt () {
311 richard 43
	# On récupère le nom des répertoire (catégories)
316 richard 44
	find $DIR_DG_BL/ -type f -name domains > $BL_CATEGORIES
311 richard 45
	# On supprime le suffice "/domains"
313 richard 46
	$SED "s?\/domains??g" $BL_CATEGORIES
311 richard 47
	rm -f $DIR_DNS_FILTER_AVAILABLE/*
607 richard 48
	echo -n "Toulouse BlackList migration process. Please wait : "
589 richard 49
	# pour chaque catégorie
313 richard 50
	for PATH_FILE in `cat $BL_CATEGORIES`
311 richard 51
	do
52
		echo -n "."
648 richard 53
		# on crée le fichier 'urls' s'il n'existe pas
589 richard 54
		if [ ! -f $PATH_FILE/urls ]
55
		then
56
			touch $PATH_FILE/urls
57
			chown dansguardian:apache $PATH_FILE/urls
58
		fi
648 richard 59
		# suppression des @IP, des lignes commentées et des caractères bizarres comme les ô et û ö ü
311 richard 60
		egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > /tmp/dnsmasq-bl.tmp
61
		$SED "/[äâëêïîöôüû]/d" /tmp/dnsmasq-bl.tmp
62
		$SED "/^#.*/d" /tmp/dnsmasq-bl.tmp
63
		# Mise en forme dnsmasq
64
		$SED "s?.*?address=/&/$IP_RETOUR?g" /tmp/dnsmasq-bl.tmp
589 richard 65
        	DOMAINE=`basename $PATH_FILE`
311 richard 66
		mv /tmp/dnsmasq-bl.tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
67
	done
568 richard 68
	echo
308 richard 69
}
70
# Permet d'activer/désactiver les catégories de la BL
71
function cat_choice (){
311 richard 72
	# un peu de ménage
73
	rm -rf $DIR_DNS_FILTER_ENABLED/*
316 richard 74
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
313 richard 75
	# on adapte le fichier $BL_CATEGORIES au choix de catégorie 
76
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
648 richard 77
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` # on affecte les catégories à dansguardian et dnsmasq
313 richard 78
	do
314 richard 79
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
80
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
648 richard 81
		ln -s $DIR_DNS_FILTER_AVAILABLE/$ENABLE_CATEGORIE.conf $DIR_DNS_FILTER_ENABLED/$ENABLE_CATEGORIE
82
		# echo ".Include<$DIR_DANSGUARDIAN/blacklists/$ENABLE_CATEGORIES/domains>" >> $DIR_DANSGUARDIAN/bannedsitelist  # dansguardian ne s'occupe plus des noms de domaine
83
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
313 richard 84
	done
314 richard 85
	sort -k2n $BL_CATEGORIES > $FILE_tmp
313 richard 86
	mv $FILE_tmp $BL_CATEGORIES
308 richard 87
}
615 richard 88
function bl_enable (){
89
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
90
	$SED "s?^#\"?\"?g" $DIR_DG/urlregexplist # Enable 'safesearch'
91
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
92
	then
93
		service dansguardian restart
94
		service dnsmasq restart
95
		/usr/local/bin/alcasar-iptables.sh
96
	fi
97
}
98
function bl_disable (){
99
	rm -rf $DIR_DNS_FILTER_ENABLED/*
100
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
101
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist # Disable 'safesearch'
102
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
103
	then
104
		service dansguardian restart
634 richard 105
		service dnsmasq restart
615 richard 106
		/usr/local/bin/alcasar-iptables.sh
107
	fi
108
}
634 richard 109
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload or --reload }"
1 root 110
nb_args=$#
111
args=$1
112
if [ $nb_args -eq 0 ]
113
then
634 richard 114
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
115
	DNS_FILTERING=${DNS_FILTERING:=off}
116
	echo "Set BlackList Filtering to $DNS_FILTERING"
117
	if [ $DNS_FILTERING = on ]; then
118
		cat_choice
119
		bl_enable
120
	else
121
		bl_disable
122
	fi
123
	exit 0
1 root 124
fi
125
case $args in
126
	-\? | -h* | --h*)
127
		echo "$usage"
128
		exit 0
129
		;;
308 richard 130
	# activation du filtrage
386 franck 131
	-on | --on)	
311 richard 132
		cat_choice
612 richard 133
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
615 richard 134
		bl_enable
1 root 135
		;;
308 richard 136
	# désactivation du filtrage
412 richard 137
	-off | --off)
612 richard 138
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
615 richard 139
		bl_disable
140
		;;
308 richard 141
	# Mise a jour de la blacklist 'Toulouse' et adaptation à dansguardian et dnsmasq
386 franck 142
	-download | --download)
1 root 143
		rm -rf /tmp/con_ok.html
144
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
145
		if [ ! -e /tmp/con_ok.html ]
146
		then
147
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
148
		else 
149
			transfert
150
			install
316 richard 151
			chown -R dansguardian:apache $DIR_DG
152
			chmod -R g+w $DIR_DG
1 root 153
			DATE=`date '+%d %B %Y - %Hh%M'`
34 richard 154
			echo "Univ-tlse du $DATE " > /var/www/html/VERSION-BL
1 root 155
			rm -rf /tmp/con_ok.html
156
		fi
654 richard 157
		;;		
158
	# Adaptation de laBL à notre structure 'dnsmasq + DG)
159
	-adapt | --adapt)
308 richard 160
		adapt
654 richard 161
		cat_choice
162
		;;
313 richard 163
	# regénération suite à modification (choix catégories ou BL secondaire)
386 franck 164
	-reload | --reload)
359 richard 165
		adapt
308 richard 166
		# pour Dansguardian
316 richard 167
		chown -R dansguardian:apache $DIR_DG_BL/ossi
168
		chmod -R g+w $DIR_DG_BL/ossi
311 richard 169
		cat_choice
170
		# pour dnsmasq (noms de domaine réhabilités)
316 richard 171
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 172
		then
316 richard 173
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 174
			do
313 richard 175
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
311 richard 176
			done
177
		fi
316 richard 178
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
308 richard 179
		$SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
648 richard 180
		IP_PURE=`grep '^ip' $BL_CATEGORIES_ENABLED|wc -l`
181
		if [ $IP_PURE -eq "1" ]; then	# filtrage des url sans nom de domaine
182
			$SED "s/^\#\*ip$/*ip/g" $DIR_DG/bannedsitelist
183
		else
184
			$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist
185
		fi
634 richard 186
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
187
		DNS_FILTERING=${DNS_FILTERING:=off}
188
		if [ $DNS_FILTERING = on ]; then
189
			bl_enable
190
		else
191
			bl_disable
192
		fi
503 richard 193
		;;
1 root 194
	*)
195
		echo "Argument inconnu :$1";
196
		echo "$usage"
197
		exit 1
198
		;;
199
esac
200