Subversion Repositories ALCASAR

Rev

Rev 958 | Rev 1042 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
672 richard 1
#/bin/bash
1015 richard 2
 
63 franck 3
# $Id: alcasar-bl.sh 1015 2013-01-27 22:43:29Z richard $
4
 
672 richard 5
# alcasar-bl.sh
6
# by Franck BOUIJOUX and Richard REY
7
# This script is distributed under the Gnu General Public License (GPL)
1 root 8
 
672 richard 9
# Gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via Dansguardian)
10
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (Dansguardian)
11
 
878 richard 12
DIR_CONF="/usr/local/etc"
13
CONF_FILE="$DIR_CONF/alcasar.conf"
854 richard 14
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
15
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
16
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`			# ALCASAR LAN IP address
308 richard 17
DIR_tmp="/tmp/blacklists"
1015 richard 18
FILE_tmp="/tmp/filesfilter.txt"
19
FILE_ip_tmp="/tmp/filesipfilter.txt"
316 richard 20
DIR_DG="/etc/dansguardian/lists"
21
DIR_DG_BL="$DIR_DG/blacklists"
1015 richard 22
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
23
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#'	'		WL	'
24
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
25
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
26
DIR_SHARE="/usr/local/share"
27
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
28
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"					# all the WL	'	'	'
29
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL 
30
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"			# symbolic link to the dnsmasq	BL (only enabled categories)
31
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
32
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)	
878 richard 33
BL_SERVER="dsi.ut-capitole.fr"
1 root 34
SED="/bin/sed -i"
637 richard 35
 
1015 richard 36
# enable/disable the BL categories
308 richard 37
function cat_choice (){
1015 richard 38
	rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED # cleaning for dnsmasq and iptables
39
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
40
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # cleaning categories file (comment all lines)
41
	mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED 
42
	# process the file $BL_CATEGORIES with the choice of categories 
43
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
313 richard 44
	do
1015 richard 45
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
314 richard 46
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
1015 richard 47
		ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
48
		ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
49
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # we let DG filters domain in order to prevent bypass by proxy http vpn ;-)
648 richard 50
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
313 richard 51
	done
878 richard 52
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
313 richard 53
	mv $FILE_tmp $BL_CATEGORIES
308 richard 54
}
615 richard 55
function bl_enable (){
56
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
57
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
58
	then
59
		service dansguardian restart
60
		service dnsmasq restart
61
		/usr/local/bin/alcasar-iptables.sh
62
	fi
63
}
64
function bl_disable (){
1015 richard 65
	rm -rf $DIR_DNS_BL_ENABLED/*
615 richard 66
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
67
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
68
	then
69
		service dansguardian restart
634 richard 70
		service dnsmasq restart
615 richard 71
		/usr/local/bin/alcasar-iptables.sh
72
	fi
73
}
634 richard 74
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload or --reload }"
1 root 75
nb_args=$#
76
args=$1
77
if [ $nb_args -eq 0 ]
78
then
634 richard 79
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
80
	DNS_FILTERING=${DNS_FILTERING:=off}
81
	echo "Set BlackList Filtering to $DNS_FILTERING"
82
	if [ $DNS_FILTERING = on ]; then
83
		cat_choice
84
		bl_enable
85
	else
86
		bl_disable
87
	fi
88
	exit 0
1 root 89
fi
90
case $args in
91
	-\? | -h* | --h*)
92
		echo "$usage"
93
		exit 0
94
		;;
1015 richard 95
	# enable the filtering
386 franck 96
	-on | --on)	
311 richard 97
		cat_choice
612 richard 98
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
615 richard 99
		bl_enable
1 root 100
		;;
1015 richard 101
	# disable the filtering
412 richard 102
	-off | --off)
612 richard 103
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
615 richard 104
		bl_disable
105
		;;
1015 richard 106
	# Retrieve Toulouse BL
386 franck 107
	-download | --download)
1 root 108
		rm -rf /tmp/con_ok.html
109
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
110
		if [ ! -e /tmp/con_ok.html ]
111
		then
112
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
113
		else 
878 richard 114
			rm -rf /tmp/con_ok.html $DIR_tmp
115
			mkdir $DIR_tmp
885 richard 116
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
878 richard 117
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
885 richard 118
			chown -R apache:apache $DIR_tmp
878 richard 119
		fi
120
		;;		
1015 richard 121
	# Adapt Toulouse BL to our structure (dnsmasq + DG)
878 richard 122
	-adapt | --adapt)
1015 richard 123
		echo -n "Toulouse BlackList migration process. Please wait : "
878 richard 124
		if [ -f $DIR_tmp/blacklists.tar.gz ]
125
		then
126
			[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
127
			rm -rf $DIR_DG_BL
128
			mkdir $DIR_DG_BL
129
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
130
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
131
			rm -rf $DIR_tmp
316 richard 132
			chown -R dansguardian:apache $DIR_DG
133
			chmod -R g+w $DIR_DG
685 richard 134
		fi
1015 richard 135
		rm -f $BL_CATEGORIES $WL_CATEGORIES 
136
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
878 richard 137
		touch $BL_CATEGORIES $WL_CATEGORIES
1015 richard 138
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
139
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
140
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
141
		for dir_categorie in `cat $FILE_tmp` # create the blackist and the whitelist files
878 richard 142
		do
1015 richard 143
			categorie=`echo $dir_categorie|cut -d "/" -f6`
144
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
145
			if [ "$categorie_type" == "white" ]
878 richard 146
			then
1015 richard 147
				echo "$dir_categorie" >> $WL_CATEGORIES 
148
				echo "$dir_categorie" >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
878 richard 149
			else
1015 richard 150
				echo "$dir_categorie" >> $BL_CATEGORIES
878 richard 151
			fi
152
		done
153
		rm -f $FILE_tmp
1015 richard 154
		# Creation of DNSMASQ BL and WL
155
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
854 richard 156
		do
1015 richard 157
			for PATH_FILE in `cat $LIST` # for each category
158
			do
159
				DOMAINE=`basename $PATH_FILE`
160
				echo -n "$DOMAINE, "
161
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
162
				then
163
					touch $PATH_FILE/urls
164
					chown dansguardian:apache $PATH_FILE/urls
165
				fi
166
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
167
				# retrieve the ip addresses for iptables
168
				egrep  "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_ip_tmp
169
				# for dnsmask, remove IP addesses, accented characters and commented lines.
170
				egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_tmp
171
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
172
				$SED "/^#.*/d" $FILE_tmp
173
				# adapt to the dnsmasq syntax
174
				$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
175
				if [ "$LIST" == "$BL_CATEGORIES" ]
176
				then
177
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
178
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
179
				else
180
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
181
				fi
182
			done
854 richard 183
		done
1015 richard 184
		rm -f $FILE_tmp $FILE_ip_tmp
854 richard 185
		echo
654 richard 186
		;;
1015 richard 187
	# reload when categories are changed 
386 franck 188
	-reload | --reload)
1015 richard 189
		# for DG
316 richard 190
		chown -R dansguardian:apache $DIR_DG_BL/ossi
191
		chmod -R g+w $DIR_DG_BL/ossi
311 richard 192
		cat_choice
1015 richard 193
		#  for dnsmasq (noms de domaine réhabilités)
316 richard 194
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 195
		then
316 richard 196
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 197
			do
1015 richard 198
				$SED "/$i/d" $DIR_DNS_BL/*
311 richard 199
			done
200
		fi
1015 richard 201
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
202
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
634 richard 203
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
204
		DNS_FILTERING=${DNS_FILTERING:=off}
205
		if [ $DNS_FILTERING = on ]; then
206
			bl_enable
207
		else
208
			bl_disable
209
		fi
503 richard 210
		;;
1 root 211
	*)
212
		echo "Argument inconnu :$1";
213
		echo "$usage"
214
		exit 1
215
		;;
216
esac
217