Subversion Repositories ALCASAR

Rev

Rev 1059 | Rev 1071 | 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 1060 2013-03-31 18:10:30Z 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
 
1042 richard 36
# enable/disable the BL & WL 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
1042 richard 40
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
1015 richard 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
1057 richard 47
		ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
1015 richard 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
1042 richard 54
	# process the file $WL_CATEGORIES with the choice of categories 
55
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
56
	do
57
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
58
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
59
		ln -s $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
60
	done
61
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
62
	mv $FILE_tmp $WL_CATEGORIES
308 richard 63
}
615 richard 64
function bl_enable (){
65
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
1060 richard 66
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
615 richard 67
	then
68
		service dansguardian restart
69
		service dnsmasq restart
70
		/usr/local/bin/alcasar-iptables.sh
71
	fi
72
}
73
function bl_disable (){
1015 richard 74
	rm -rf $DIR_DNS_BL_ENABLED/*
615 richard 75
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
1060 richard 76
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
615 richard 77
	then
78
		service dansguardian restart
634 richard 79
		service dnsmasq restart
615 richard 80
		/usr/local/bin/alcasar-iptables.sh
81
	fi
82
}
1059 richard 83
usage="Usage: alcasar-bl.sh { -on or --on } | { -off or --off } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
1 root 84
nb_args=$#
85
args=$1
86
if [ $nb_args -eq 0 ]
87
then
634 richard 88
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
89
	DNS_FILTERING=${DNS_FILTERING:=off}
90
	echo "Set BlackList Filtering to $DNS_FILTERING"
91
	if [ $DNS_FILTERING = on ]; then
92
		cat_choice
93
		bl_enable
94
	else
95
		bl_disable
96
	fi
97
	exit 0
1 root 98
fi
99
case $args in
100
	-\? | -h* | --h*)
101
		echo "$usage"
102
		exit 0
103
		;;
1015 richard 104
	# enable the filtering
386 franck 105
	-on | --on)	
311 richard 106
		cat_choice
612 richard 107
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
615 richard 108
		bl_enable
1 root 109
		;;
1015 richard 110
	# disable the filtering
412 richard 111
	-off | --off)
612 richard 112
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
615 richard 113
		bl_disable
114
		;;
1015 richard 115
	# Retrieve Toulouse BL
386 franck 116
	-download | --download)
1 root 117
		rm -rf /tmp/con_ok.html
118
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
119
		if [ ! -e /tmp/con_ok.html ]
120
		then
121
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
122
		else 
878 richard 123
			rm -rf /tmp/con_ok.html $DIR_tmp
124
			mkdir $DIR_tmp
885 richard 125
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
878 richard 126
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
885 richard 127
			chown -R apache:apache $DIR_tmp
878 richard 128
		fi
129
		;;		
1015 richard 130
	# Adapt Toulouse BL to our structure (dnsmasq + DG)
878 richard 131
	-adapt | --adapt)
1015 richard 132
		echo -n "Toulouse BlackList migration process. Please wait : "
878 richard 133
		if [ -f $DIR_tmp/blacklists.tar.gz ]
134
		then
135
			[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
136
			rm -rf $DIR_DG_BL
137
			mkdir $DIR_DG_BL
138
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
139
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
140
			rm -rf $DIR_tmp
685 richard 141
		fi
1042 richard 142
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
1015 richard 143
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
1042 richard 144
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
1015 richard 145
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
1042 richard 146
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
147
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
1015 richard 148
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
149
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
1042 richard 150
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
878 richard 151
		do
1015 richard 152
			categorie=`echo $dir_categorie|cut -d "/" -f6`
153
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
154
			if [ "$categorie_type" == "white" ]
878 richard 155
			then
1015 richard 156
				echo "$dir_categorie" >> $WL_CATEGORIES 
1042 richard 157
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
878 richard 158
			else
1015 richard 159
				echo "$dir_categorie" >> $BL_CATEGORIES
878 richard 160
			fi
161
		done
162
		rm -f $FILE_tmp
1057 richard 163
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
164
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
165
		do
166
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
167
			if [ $ok != "1" ] 
168
			then
169
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
170
			fi
171
		done
1015 richard 172
		# Creation of DNSMASQ BL and WL
173
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
854 richard 174
		do
1015 richard 175
			for PATH_FILE in `cat $LIST` # for each category
176
			do
177
				DOMAINE=`basename $PATH_FILE`
178
				echo -n "$DOMAINE, "
179
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
180
				then
181
					touch $PATH_FILE/urls
182
					chown dansguardian:apache $PATH_FILE/urls
183
				fi
184
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
185
				# retrieve the ip addresses for iptables
186
				egrep  "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_ip_tmp
187
				# for dnsmask, remove IP addesses, accented characters and commented lines.
188
				egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_tmp
189
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
190
				$SED "/^#.*/d" $FILE_tmp
191
				# adapt to the dnsmasq syntax
192
				$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
193
				if [ "$LIST" == "$BL_CATEGORIES" ]
194
				then
195
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
196
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
197
				else
198
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
199
				fi
200
			done
854 richard 201
		done
1015 richard 202
		rm -f $FILE_tmp $FILE_ip_tmp
854 richard 203
		echo
654 richard 204
		;;
1015 richard 205
	# reload when categories are changed 
386 franck 206
	-reload | --reload)
1015 richard 207
		# for DG
316 richard 208
		chown -R dansguardian:apache $DIR_DG_BL/ossi
209
		chmod -R g+w $DIR_DG_BL/ossi
311 richard 210
		cat_choice
1042 richard 211
		#  for dnsmasq (rehabited domain names)
316 richard 212
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 213
		then
316 richard 214
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 215
			do
1015 richard 216
				$SED "/$i/d" $DIR_DNS_BL/*
311 richard 217
			done
218
		fi
1015 richard 219
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
220
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
1042 richard 221
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
634 richard 222
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
223
		DNS_FILTERING=${DNS_FILTERING:=off}
224
		if [ $DNS_FILTERING = on ]; then
225
			bl_enable
226
		else
227
			bl_disable
228
		fi
503 richard 229
		;;
1 root 230
	*)
231
		echo "Argument inconnu :$1";
232
		echo "$usage"
233
		exit 1
234
		;;
235
esac
236