Subversion Repositories ALCASAR

Rev

Rev 854 | Rev 885 | 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 878 2012-06-03 16:07:40Z 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
 
878 richard 11
DIR_CONF="/usr/local/etc"
12
CONF_FILE="$DIR_CONF/alcasar.conf"
854 richard 13
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
14
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
15
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`			# ALCASAR LAN IP address
308 richard 16
DIR_tmp="/tmp/blacklists"
17
FILE_tmp="/tmp/fileFilter.txt"
316 richard 18
DIR_DG="/etc/dansguardian/lists"
19
DIR_DG_BL="$DIR_DG/blacklists"
878 richard 20
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"
21
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"
22
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"
23
DIR_DNS_FILTER_AVAILABLE="$DIR_CONF/alcasar-dnsfilter-available"
24
DIR_DNS_FILTER_ENABLED="$DIR_CONF/alcasar-dnsfilter-enabled"
25
BL_SERVER="dsi.ut-capitole.fr"
1 root 26
SED="/bin/sed -i"
637 richard 27
 
308 richard 28
# Permet d'activer/désactiver les catégories de la BL
29
function cat_choice (){
311 richard 30
	# un peu de ménage
31
	rm -rf $DIR_DNS_FILTER_ENABLED/*
316 richard 32
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
313 richard 33
	# on adapte le fichier $BL_CATEGORIES au choix de catégorie 
34
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
648 richard 35
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` # on affecte les catégories à dansguardian et dnsmasq
313 richard 36
	do
314 richard 37
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
38
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
648 richard 39
		ln -s $DIR_DNS_FILTER_AVAILABLE/$ENABLE_CATEGORIE.conf $DIR_DNS_FILTER_ENABLED/$ENABLE_CATEGORIE
854 richard 40
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # dansguardian s'occupe du contournement par proxy http ;-)
648 richard 41
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
313 richard 42
	done
878 richard 43
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
313 richard 44
	mv $FILE_tmp $BL_CATEGORIES
308 richard 45
}
615 richard 46
function bl_enable (){
47
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
48
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
49
	then
50
		service dansguardian restart
51
		service dnsmasq restart
52
		/usr/local/bin/alcasar-iptables.sh
53
	fi
54
}
55
function bl_disable (){
56
	rm -rf $DIR_DNS_FILTER_ENABLED/*
57
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
58
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
59
	then
60
		service dansguardian restart
634 richard 61
		service dnsmasq restart
615 richard 62
		/usr/local/bin/alcasar-iptables.sh
63
	fi
64
}
634 richard 65
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload or --reload }"
1 root 66
nb_args=$#
67
args=$1
68
if [ $nb_args -eq 0 ]
69
then
634 richard 70
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
71
	DNS_FILTERING=${DNS_FILTERING:=off}
72
	echo "Set BlackList Filtering to $DNS_FILTERING"
73
	if [ $DNS_FILTERING = on ]; then
74
		cat_choice
75
		bl_enable
76
	else
77
		bl_disable
78
	fi
79
	exit 0
1 root 80
fi
81
case $args in
82
	-\? | -h* | --h*)
83
		echo "$usage"
84
		exit 0
85
		;;
308 richard 86
	# activation du filtrage
386 franck 87
	-on | --on)	
311 richard 88
		cat_choice
612 richard 89
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
615 richard 90
		bl_enable
1 root 91
		;;
308 richard 92
	# désactivation du filtrage
412 richard 93
	-off | --off)
612 richard 94
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
615 richard 95
		bl_disable
96
		;;
878 richard 97
	# Récupération de l'archive de la BL Toulouse
386 franck 98
	-download | --download)
1 root 99
		rm -rf /tmp/con_ok.html
100
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
101
		if [ ! -e /tmp/con_ok.html ]
102
		then
103
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
104
		else 
878 richard 105
			rm -rf /tmp/con_ok.html $DIR_tmp
106
			mkdir $DIR_tmp
107
			cd $DIR_tmp
108
			wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
109
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
110
			cd /root
111
		fi
112
		;;		
113
	# Adaptation de la BL de Toulouse à notre structure (dnsmasq + DG)
114
	-adapt | --adapt)
115
		if [ -f $DIR_tmp/blacklists.tar.gz ]
116
		then
117
			[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
118
			rm -rf $DIR_DG_BL
119
			mkdir $DIR_DG_BL
120
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
121
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
122
			rm -rf $DIR_tmp
316 richard 123
			chown -R dansguardian:apache $DIR_DG
124
			chmod -R g+w $DIR_DG
685 richard 125
		fi
878 richard 126
		rm -f $BL_CATEGORIES $WL_CATEGORIES $DIR_DNS_FILTER_AVAILABLE/*
127
		touch $BL_CATEGORIES $WL_CATEGORIES
128
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # On récupère le nom des répertoire (catégories)
129
		$SED "s?\/domains??g" $FILE_tmp # On supprime le suffixe "/domains"
130
		for categorie in `cat $FILE_tmp` # creation des deux fichiers de categories (BL / WL)
131
		do
132
			if [ -e $categorie/usage ]
133
			then
134
				is_whitelist=`grep white $categorie/usage|wc -l`
135
			else
136
				is_whitelist=0 # si le fichier 'usage' n'existe pas, on considère que la catégorie est une BL
137
			fi
138
			if [ $is_whitelist -eq "0" ]
139
			then
140
				echo "$categorie" >> $BL_CATEGORIES
141
			else
142
				echo "$categorie" >> $WL_CATEGORIES
143
			fi
144
		done
145
		rm -f $FILE_tmp
854 richard 146
		echo -n "Toulouse BlackList migration process. Please wait : "
147
		for PATH_FILE in `cat $BL_CATEGORIES`  # pour chaque catégorie
148
		do
149
			echo -n "."
150
		  	if [ ! -f $PATH_FILE/urls ] # on crée le fichier 'urls' s'il n'existe pas
151
			then
152
				touch $PATH_FILE/urls
153
				chown dansguardian:apache $PATH_FILE/urls
154
			fi
155
			# suppression des @IP, de caractères acccentués et des lignes commentées
878 richard 156
			egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_tmp
157
			$SED "/[äâëêïîöôüû]/d" $FILE_tmp
158
			$SED "/^#.*/d" $FILE_tmp
159
			$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp  # Mise en forme dnsmasq
854 richard 160
			DOMAINE=`basename $PATH_FILE`
878 richard 161
			mv $FILE_tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
854 richard 162
		done
163
		echo
654 richard 164
		;;
878 richard 165
	# regénération suite à modification du choix des catégories 
386 franck 166
	-reload | --reload)
308 richard 167
		# pour Dansguardian
316 richard 168
		chown -R dansguardian:apache $DIR_DG_BL/ossi
169
		chmod -R g+w $DIR_DG_BL/ossi
311 richard 170
		cat_choice
171
		# pour dnsmasq (noms de domaine réhabilités)
316 richard 172
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 173
		then
316 richard 174
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 175
			do
313 richard 176
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
311 richard 177
			done
178
		fi
316 richard 179
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
854 richard 180
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
634 richard 181
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
182
		DNS_FILTERING=${DNS_FILTERING:=off}
183
		if [ $DNS_FILTERING = on ]; then
184
			bl_enable
185
		else
186
			bl_disable
187
		fi
503 richard 188
		;;
1 root 189
	*)
190
		echo "Argument inconnu :$1";
191
		echo "$usage"
192
		exit 1
193
		;;
194
esac
195