Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

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