Subversion Repositories ALCASAR

Rev

Rev 634 | Rev 648 | 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 637 2011-06-16 21:56:59Z 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"
634 richard 16
IP_RETOUR="20.0.0.1"
1 root 17
BL_SERVER="cri.univ-tlse1.fr"
18
SED="/bin/sed -i"
637 richard 19
 
308 richard 20
# Récupération de l'archive de la BL Toulouse
1 root 21
function transfert () {
22
	mkdir -p $DIR_tmp
23
	cd $DIR_tmp
24
	wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
25
}
26
 
308 richard 27
# Décompression de la BL (en conservant la WL)
1 root 28
function install () {
316 richard 29
	[ -d $DIR_DG ] || mkdir -p $DIR_DG
30
	[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
31
	tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
32
	[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
1 root 33
	cd /root
34
	rm -rf $DIR_tmp
35
}
36
 
308 richard 37
# Adaptation de la BL Toulouse à la structure Dnsmasq
38
function adapt () {
311 richard 39
	# On récupère le nom des répertoire (catégories)
316 richard 40
	find $DIR_DG_BL/ -type f -name domains > $BL_CATEGORIES
311 richard 41
	# On supprime le suffice "/domains"
313 richard 42
	$SED "s?\/domains??g" $BL_CATEGORIES
311 richard 43
	rm -f $DIR_DNS_FILTER_AVAILABLE/*
607 richard 44
	echo -n "Toulouse BlackList migration process. Please wait : "
589 richard 45
	# pour chaque catégorie
313 richard 46
	for PATH_FILE in `cat $BL_CATEGORIES`
311 richard 47
	do
48
		echo -n "."
589 richard 49
		# on crée le le fichier 'urls' s'il n'existe pas
50
		if [ ! -f $PATH_FILE/urls ]
51
		then
52
			touch $PATH_FILE/urls
53
			chown dansguardian:apache $PATH_FILE/urls
54
		fi
55
		# on suppression des @IP, des lignes commentées et des caractères bizarres comme les ô et û ö ü
311 richard 56
		egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > /tmp/dnsmasq-bl.tmp
57
		$SED "/[äâëêïîöôüû]/d" /tmp/dnsmasq-bl.tmp
58
		$SED "/^#.*/d" /tmp/dnsmasq-bl.tmp
59
		# Mise en forme dnsmasq
60
		$SED "s?.*?address=/&/$IP_RETOUR?g" /tmp/dnsmasq-bl.tmp
589 richard 61
        	DOMAINE=`basename $PATH_FILE`
311 richard 62
		mv /tmp/dnsmasq-bl.tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
63
	done
568 richard 64
	echo
308 richard 65
}
66
 
67
# Permet d'activer/désactiver les catégories de la BL
68
function cat_choice (){
311 richard 69
	# un peu de ménage
70
	rm -rf $DIR_DNS_FILTER_ENABLED/*
316 richard 71
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
313 richard 72
	# on adapte le fichier $BL_CATEGORIES au choix de catégorie 
73
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
74
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
75
	do
314 richard 76
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
77
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
313 richard 78
	done
314 richard 79
	sort -k2n $BL_CATEGORIES > $FILE_tmp
313 richard 80
	mv $FILE_tmp $BL_CATEGORIES
81
	# on affecte les catégories à dansguardian et dnsmasq
82
	for i in `cat $BL_CATEGORIES_ENABLED`
311 richard 83
		do
84
		ln -s $DIR_DNS_FILTER_AVAILABLE/$i.conf $DIR_DNS_FILTER_ENABLED/$i
85
		# echo ".Include<$DIR_DANSGUARDIAN/blacklists/$i/domains>" >> $DIR_DANSGUARDIAN/bannedsitelist
316 richard 86
		echo ".Include<$DIR_DG_BL/$i/urls>" >> $DIR_DG/bannedurllist
311 richard 87
		done
308 richard 88
}
615 richard 89
function bl_enable (){
90
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
91
	$SED "s?^#\"?\"?g" $DIR_DG/urlregexplist # Enable 'safesearch'
92
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
93
	then
94
		service dansguardian restart
95
		service dnsmasq restart
96
		/usr/local/bin/alcasar-iptables.sh
97
	fi
98
}
99
function bl_disable (){
100
	rm -rf $DIR_DNS_FILTER_ENABLED/*
101
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
102
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist # Disable 'safesearch'
103
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
104
	then
105
		service dansguardian restart
634 richard 106
		service dnsmasq restart
615 richard 107
		/usr/local/bin/alcasar-iptables.sh
108
	fi
109
}
634 richard 110
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload or --reload }"
1 root 111
nb_args=$#
112
args=$1
113
if [ $nb_args -eq 0 ]
114
then
634 richard 115
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
116
	DNS_FILTERING=${DNS_FILTERING:=off}
117
	echo "Set BlackList Filtering to $DNS_FILTERING"
118
	if [ $DNS_FILTERING = on ]; then
119
		cat_choice
120
		bl_enable
121
	else
122
		bl_disable
123
	fi
124
	exit 0
1 root 125
fi
126
case $args in
127
	-\? | -h* | --h*)
128
		echo "$usage"
129
		exit 0
130
		;;
308 richard 131
	# activation du filtrage
386 franck 132
	-on | --on)	
311 richard 133
		cat_choice
612 richard 134
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
615 richard 135
		bl_enable
1 root 136
		;;
308 richard 137
	# désactivation du filtrage
412 richard 138
	-off | --off)
612 richard 139
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
615 richard 140
		bl_disable
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
167
		# pour dnsmasq (noms de domaine réhabilités)
316 richard 168
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 169
		then
316 richard 170
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 171
			do
313 richard 172
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
311 richard 173
			done
174
		fi
316 richard 175
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
308 richard 176
		$SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
634 richard 177
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
178
		DNS_FILTERING=${DNS_FILTERING:=off}
179
		if [ $DNS_FILTERING = on ]; then
180
			bl_enable
181
		else
182
			bl_disable
183
		fi
503 richard 184
		;;
1 root 185
	*)
186
		echo "Argument inconnu :$1";
187
		echo "$usage"
188
		exit 1
189
		;;
190
esac
191