Subversion Repositories ALCASAR

Rev

Rev 1914 | Rev 1926 | 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 1925 2016-06-04 18:38:51Z 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"
1852 raphael.pi 18
DIR_WL_tmp="/tmp/whitelists"
1015 richard 19
FILE_tmp="/tmp/filesfilter.txt"
20
FILE_ip_tmp="/tmp/filesipfilter.txt"
316 richard 21
DIR_DG="/etc/dansguardian/lists"
22
DIR_DG_BL="$DIR_DG/blacklists"
1015 richard 23
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
24
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#'	'		WL	'
25
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
26
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
27
DIR_SHARE="/usr/local/share"
28
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
29
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"					# all the WL	'	'	'
30
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL 
1899 raphael.pi 31
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# IP ossi disabled WL 
1366 richard 32
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
1015 richard 33
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
1377 richard 34
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
1852 raphael.pi 35
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
1899 raphael.pi 36
tmp_DIR_IP_BL="/tmp/ossi-iptables-bl"					#IP BL tmp directory to keep ossi files during an update
37
tmp_DIR_IP_WL="/tmp/ossi-iptables-wl"					#IP WL tmp directory to keep ossi files during an update
38
tmp_DIR_DNS_BL="/tmp/ossi-dnsmasq-bl"					#DNS BL tmp directory to keep ossi files during an update
39
tmp_DIR_DNS_WL="/tmp/ossi-dnsmasq-wl"					#DNS WL tmp directory to keep ossi files during an update
40
#BL tmp directory to keep ossi files
1365 richard 41
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"				# conf file of dnsmasq-blacklist
42
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
878 richard 43
BL_SERVER="dsi.ut-capitole.fr"
1 root 44
SED="/bin/sed -i"
637 richard 45
 
1042 richard 46
# enable/disable the BL & WL categories
308 richard 47
function cat_choice (){
1339 richard 48
	mkdir $DIR_tmp
1864 raphael.pi 49
	#ip BL ENABLE
1393 richard 50
	if [ -d $DIR_IP_BL_ENABLED ]
1386 richard 51
	then
1393 richard 52
		for file in `ls -1 $DIR_IP_BL_ENABLED | grep -v "^ossi-*"`
53
		do
54
			rm -f $DIR_IP_BL_ENABLED/$file
55
		done
56
	else
57
		mkdir $DIR_IP_BL_ENABLED
1913 richard 58
		chown root:apache $DIR_IP_BL_ENABLED
59
		chmod 770 $DIR_IP_BL_ENABLED
1386 richard 60
	fi
1913 richard 61
 
1864 raphael.pi 62
	#dns BL ENABLED
63
	if [ -d $DIR_DNS_BL_ENABLED ]
64
	then
65
		for file in `ls -1 $DIR_DNS_BL_ENABLED | grep -v "^ossi-*"`
66
		do
67
			rm -f $DIR_DNS_BL_ENABLED/$file
68
		done
69
	else
70
		mkdir $DIR_DNS_BL_ENABLED
1913 richard 71
		chown root:apache $DIR_DNS_BL_ENABLED
72
		chmod 770 $DIR_DNS_BL_ENABLED
1864 raphael.pi 73
	fi
1913 richard 74
	#dns BL ossi.conf 
75
	if [ ! -e $DIR_DNS_BL/ossi.conf ]
1899 raphael.pi 76
	then
1913 richard 77
		touch $DIR_DNS_BL/ossi.conf
78
		chown root:apache $DIR_DNS_BL/ossi.conf
79
		chmod g+w $DIR_DNS_BL/ossi.conf
80
		if [ ! -e $DIR_DNS_BL_ENABLED/ossi ]
81
		then
82
			ln -s $DIR_DNS_BL/ossi.conf $DIR_DNS_BL_ENABLED/ossi
83
		fi
1899 raphael.pi 84
	fi
1913 richard 85
 
1864 raphael.pi 86
	#ip WL ENABLE
1852 raphael.pi 87
	if [ -d $DIR_IP_WL_ENABLED ]
88
	then
1864 raphael.pi 89
		for file in `ls -1 $DIR_IP_WL_ENABLED | grep -v "^ossi*"`
1852 raphael.pi 90
		do
91
			rm -f $DIR_IP_WL_ENABLED/$file
92
		done
93
	else
94
		mkdir $DIR_IP_WL_ENABLED
1864 raphael.pi 95
		touch $DIR_IP_WL_ENABLED/ossi
1913 richard 96
		chown -R root:apache $DIR_IP_WL_ENABLED
97
		chmod 770 $DIR_IP_WL_ENABLED
1903 raphael.pi 98
		chmod g+w $DIR_IP_WL_ENABLED/ossi
1857 raphael.pi 99
	fi
100
 
1864 raphael.pi 101
	#dns WL ENABLED
1852 raphael.pi 102
	if [ -d $DIR_DNS_WL_ENABLED ]
103
	then
104
		for file in `ls -1 $DIR_DNS_WL_ENABLED | grep -v "^ossi-*"`
105
		do
106
			rm -f $DIR_DNS_WL_ENABLED/$file
107
		done
108
	else
109
		mkdir $DIR_DNS_WL_ENABLED
1913 richard 110
		chown root:apache $DIR_DNS_WL_ENABLED
111
		chmod 770 $DIR_DNS_WL_ENABLED
1852 raphael.pi 112
	fi
1864 raphael.pi 113
	#dns WL ossi.conf 
114
	if [ ! -e $DIR_DNS_WL/ossi.conf ]
115
	then
116
		touch $DIR_DNS_WL/ossi.conf
1913 richard 117
		chown root:apache $DIR_DNS_WL/ossi.conf
1903 raphael.pi 118
		chmod g+w $DIR_DNS_WL/ossi.conf
1886 raphael.pi 119
		if [ ! -e $DIR_DNS_WL_ENABLED/ossi ]
120
		then
1913 richard 121
			ln -s $DIR_DNS_WL/ossi.conf $DIR_DNS_WL_ENABLED/ossi
1886 raphael.pi 122
		fi
1864 raphael.pi 123
	fi
124
 
1870 raphael.pi 125
	# update categories with rsync
126
	if [ ! -e $DIR_CONF/update_cat.conf ]
127
	then
128
		touch $DIR_CONF/update_cat.conf
129
		chown root:apache $DIR_CONF/update_cat.conf
130
		chmod 660 $DIR_CONF/update_cat.conf
131
	fi
1015 richard 132
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
1042 richard 133
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
1912 richard 134
 
1015 richard 135
	# process the file $BL_CATEGORIES with the choice of categories 
136
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
313 richard 137
	do
1015 richard 138
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
314 richard 139
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
1395 richard 140
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
141
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
1293 richard 142
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
648 richard 143
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
313 richard 144
	done
878 richard 145
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
313 richard 146
	mv $FILE_tmp $BL_CATEGORIES
1912 richard 147
 
1042 richard 148
	# process the file $WL_CATEGORIES with the choice of categories 
149
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
150
	do
151
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
152
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
1395 richard 153
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
1042 richard 154
	done
155
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
156
	mv $FILE_tmp $WL_CATEGORIES
308 richard 157
}
1482 richard 158
 
1870 raphael.pi 159
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
1 root 160
nb_args=$#
161
args=$1
162
if [ $nb_args -eq 0 ]
163
then
1369 richard 164
	  args="-h"
1 root 165
fi
166
case $args in
167
	-\? | -h* | --h*)
168
		echo "$usage"
169
		exit 0
170
		;;
1873 richard 171
	# Retrieve Toulouse University BL
386 franck 172
	-download | --download)
1 root 173
		rm -rf /tmp/con_ok.html
174
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
175
		if [ ! -e /tmp/con_ok.html ]
176
		then
177
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
178
		else 
878 richard 179
			rm -rf /tmp/con_ok.html $DIR_tmp
180
			mkdir $DIR_tmp
885 richard 181
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
878 richard 182
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
885 richard 183
			chown -R apache:apache $DIR_tmp
878 richard 184
		fi
185
		;;		
1488 richard 186
	# enable/disable categories (used only during the alcasar install process)
187
	-cat_choice | --cat_choice)
188
		cat_choice
189
		;;
1873 richard 190
	# Adapt Toulouse University BL to ALCASAR architecture (dnsmasq + DG + iptables)
878 richard 191
	-adapt | --adapt)
1873 richard 192
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
1925 richard 193
		# keep custom files (ossi) 
194
		for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
195
		do
196
			mv $DIR_DG_BL/$x $DIR_tmp
197
		done
1371 richard 198
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
878 richard 199
		then
1369 richard 200
			rm -rf $DIR_DG_BL $DIR_IP_BL
201
			mkdir $DIR_DG_BL $DIR_IP_BL
878 richard 202
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
685 richard 203
		fi
1925 richard 204
		# Retrieve custom files (ossi)
205
		for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
206
		do
207
			mv $DIR_tmp/$x $DIR_DG_BL
208
		done
209
 
1042 richard 210
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
1899 raphael.pi 211
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
1042 richard 212
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
1899 raphael.pi 213
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
214
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
215
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
1015 richard 216
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
217
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
1042 richard 218
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
878 richard 219
		do
1015 richard 220
			categorie=`echo $dir_categorie|cut -d "/" -f6`
221
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
222
			if [ "$categorie_type" == "white" ]
878 richard 223
			then
1015 richard 224
				echo "$dir_categorie" >> $WL_CATEGORIES 
1042 richard 225
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
878 richard 226
			fi
1811 richard 227
			echo "$dir_categorie" >> $BL_CATEGORIES # By default all categories are in BL
878 richard 228
		done
229
		rm -f $FILE_tmp
1057 richard 230
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
231
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
232
		do
233
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
234
			if [ $ok != "1" ] 
235
			then
236
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
237
			fi
238
		done
1293 richard 239
		# Creation of DNSMASQ and Iptables BL and WL
1015 richard 240
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
854 richard 241
		do
1015 richard 242
			for PATH_FILE in `cat $LIST` # for each category
243
			do
244
				DOMAINE=`basename $PATH_FILE`
245
				echo -n "$DOMAINE, "
246
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
247
				then
248
					touch $PATH_FILE/urls
249
					chown dansguardian:apache $PATH_FILE/urls
250
				fi
251
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
1383 richard 252
				# extract ip addresses for iptables
1867 raphael.pi 253
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
1015 richard 254
				# for dnsmask, remove IP addesses, accented characters and commented lines.
1293 richard 255
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
1015 richard 256
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
257
				$SED "/^#.*/d" $FILE_tmp
258
				if [ "$LIST" == "$BL_CATEGORIES" ]
259
				then
1365 richard 260
					# adapt to the dnsmasq syntax for the blacklist
261
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
1015 richard 262
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
263
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
264
				else
1365 richard 265
					# adapt to the dnsmasq syntax for the whitelist
266
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
1015 richard 267
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
268
				fi
269
			done
854 richard 270
		done
1015 richard 271
		rm -f $FILE_tmp $FILE_ip_tmp
1912 richard 272
		# Restoring custom files (ossi)
1339 richard 273
		rm -rf $DIR_tmp
1902 raphael.pi 274
		if [ -d $tmp_DIR_IP_BL -a -d $tmp_DIR_IP_WL -a -d $tmp_DIR_DNS_BL -a -d $tmp_DIR_DNS_WL ]
1900 raphael.pi 275
		then
276
			for x in $(ls -1 $tmp_DIR_IP_BL | grep "^ossi*")
277
			do
278
				mv $tmp_DIR_IP_BL/$x $DIR_IP_BL
279
			done
280
			for x in $(ls -1 $tmp_DIR_IP_WL | grep "^ossi*")
281
			do
282
				mv $tmp_DIR_IP_WL/$x $DIR_IP_WL
283
			done
284
			for x in $(ls -1 $tmp_DIR_DNS_BL | grep "^ossi*")
285
			do
286
				mv $tmp_DIR_DNS_BL/$x $DIR_DNS_BL
287
			done
288
			for x in $(ls -1 $tmp_DIR_DNS_WL | grep "^ossi*")
289
			do
290
				mv $tmp_DIR_DNS_WL/$x $DIR_DNS_WL
291
			done
292
			rm -rf $tmp_DIR_IP_BL $tmp_DIR_IP_WL $tmp_DIR_DNS_BL $tmp_DIR_DNS_WL
293
		fi
854 richard 294
		echo
654 richard 295
		;;
1898 richard 296
	# update the categories which are written in "/usr/local/etc/update_cat.conf" with rsync
297
	-update_cat | --update_cat)
1874 raphael.pi 298
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
299
		then
1898 richard 300
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
301
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
302
			do
303
				CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
304
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
305
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
306
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
307
				# Creation of DNSMASQ and Iptables BL and WL
308
				DOMAINE=$(basename $PATH_FILE)
309
				# correct some synthaxes
310
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls
311
				# extract ip addresses for iptables
312
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
313
				# for dnsmask, remove IP addresses, accentuated characters and commented lines.
314
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
315
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
316
				$SED "/^#.*/d" $FILE_tmp
317
				black=`grep black $PATH_FILE/usage |wc -l`
318
				if [ $black == "1" ]
319
				then
320
					# adapt to the dnsmasq syntax for the blacklist
321
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
322
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
323
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
324
				else
325
					# adapt to the dnsmasq syntax for the whitelist
326
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
327
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
328
				fi
329
				rm -f $FILE_tmp $FILE_ip_tmp
330
			done
331
			/usr/bin/systemctl restart dnsmasq-whitelist
332
			/usr/bin/systemctl restart dnsmasq-blacklist
333
			/usr/bin/systemctl restart dansguardian
334
			/usr/local/bin/alcasar-iptables.sh
1867 raphael.pi 335
		else
1898 richard 336
			  echo -n "/usr/local/etc/update_cat.conf is empty ..."
1867 raphael.pi 337
		fi
1874 raphael.pi 338
		echo 
1867 raphael.pi 339
		;;
1015 richard 340
	# reload when categories are changed 
386 franck 341
	-reload | --reload)
1015 richard 342
		# for DG
311 richard 343
		cat_choice
1042 richard 344
		#  for dnsmasq (rehabited domain names)
316 richard 345
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
311 richard 346
		then
316 richard 347
			for i in `cat $DIR_DG/exceptionsitelist`
311 richard 348
			do
1015 richard 349
				$SED "/$i/d" $DIR_DNS_BL/*
311 richard 350
			done
351
		fi
1574 richard 352
		/usr/bin/systemctl restart dnsmasq-blacklist
353
		/usr/bin/systemctl restart dnsmasq-whitelist
1488 richard 354
		/usr/local/bin/alcasar-iptables.sh
503 richard 355
		;;
1 root 356
	*)
357
		echo "Argument inconnu :$1";
358
		echo "$usage"
359
		exit 1
360
		;;
361
esac
362
 
1857 raphael.pi 363
 
1867 raphael.pi 364
 
1874 raphael.pi 365