Subversion Repositories ALCASAR

Rev

Rev 2187 | Rev 2193 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2187 Rev 2192
Line 1... Line 1...
1
#/bin/bash
1
#/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 2187 2017-04-27 22:18:45Z richard $
3
# $Id: alcasar-bl.sh 2192 2017-05-01 19:52:53Z richard $
4
 
4
 
5
# alcasar-bl.sh
5
# alcasar-bl.sh
6
# by Franck BOUIJOUX and Richard REY
6
# by Franck BOUIJOUX and Richard REY
7
# This script is distributed under the Gnu General Public License (GPL)
7
# This script is distributed under the Gnu General Public License (GPL)
8
 
8
 
Line 87... Line 87...
87
	mv $FILE_tmp $WL_CATEGORIES
87
	mv $FILE_tmp $WL_CATEGORIES
88
	chown root:apache $WL_CATEGORIES
88
	chown root:apache $WL_CATEGORIES
89
	chmod 660 $WL_CATEGORIES
89
	chmod 660 $WL_CATEGORIES
90
}
90
}
91
 
91
 
-
 
92
# cleaning file and split it ("domains" in $FILE_tmp & "IP" in $FILE_ip_tmp)
-
 
93
function clean_split (){
-
 
94
	$SED '/^#.*/d' $FILE_tmp # remove commented lines
-
 
95
	$SED '/^\s*$/d' $FILE_tmp # remove empty lines
-
 
96
	$SED '/[äâëêïîöôüû@,]/d' $FILE_tmp # remove line with "chelou" characters
-
 
97
	# extract ip addresses for iptables.
-
 
98
	awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $FILE_tmp > $FILE_ip_tmp
-
 
99
	# extract domain names for dnsmasq.
-
 
100
	$SED -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!p' $FILE_tmp
-
 
101
	# Retrieve max Top Level Domain for domain name synthax
-
 
102
	#MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
-
 
103
	#if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
-
 
104
	#	MAX_TLD=18
-
 
105
	#fi
-
 
106
	# search for correction	egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
-
 
107
}
-
 
108
 
92
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
109
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
93
nb_args=$#
110
nb_args=$#
94
args=$1
111
args=$1
95
if [ $nb_args -eq 0 ]
112
if [ $nb_args -eq 0 ]
96
then
113
then
Line 131... Line 148...
131
				mv $DIR_DG_BL/$x $DIR_tmp
148
				mv $DIR_DG_BL/$x $DIR_tmp
132
			done
149
			done
133
			rm -rf $DIR_DG_BL $DIR_IP_BL
150
			rm -rf $DIR_DG_BL $DIR_IP_BL
134
			mkdir $DIR_DG_BL $DIR_IP_BL
151
			mkdir $DIR_DG_BL $DIR_IP_BL
135
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
152
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
136
			chown -R root:apache $DIR_DG
153
			chown -R dansguardian:apache $DIR_DG
137
			chmod -R 770 $DIR_DG
154
			chmod -R 770 $DIR_DG
138
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
155
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
139
			# Add the custom categories (ossi-tor_nodes) to the usage file
156
			# Add the custom categories (ossi-tor_nodes) to the usage file
140
			cat << EOF >> $DIR_DG_BL/global_usage 
157
			cat << EOF >> $DIR_DG_BL/global_usage 
141
 
158
 
Line 217... Line 234...
217
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
234
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
218
				then
235
				then
219
					touch $PATH_FILE/urls
236
					touch $PATH_FILE/urls
220
					chown dansguardian:apache $PATH_FILE/urls
237
					chown dansguardian:apache $PATH_FILE/urls
221
				fi
238
				fi
222
				$SED "/[äâëêïîöôüû@]/d" $FILE_tmp
-
 
223
				$SED "/^#.*/d" $FILE_tmp
239
				cp $PATH_FILE/domains  $FILE_tmp
224
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
-
 
225
				# extract ip addresses for iptables
-
 
226
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
-
 
227
				# for dnsmask, remove IP addesses, accented characters and commented lines.
240
				clean_split # clean ossi custom files & split them for dnsmasq and for iptables
228
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
-
 
229
				if [ "$LIST" == "$BL_CATEGORIES" ]
241
				if [ "$LIST" == "$BL_CATEGORIES" ]
230
				then
242
				then
231
					# adapt to the dnsmasq syntax for the blacklist
243
					# adapt to the dnsmasq syntax for the blacklist
232
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
244
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
233
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
245
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
Line 244... Line 256...
244
	       	chmod 770 $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
256
	       	chmod 770 $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
245
		chmod -f 660 $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
257
		chmod -f 660 $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
246
		rm -f $FILE_tmp $FILE_ip_tmp
258
		rm -f $FILE_tmp $FILE_ip_tmp
247
		rm -rf $DIR_tmp
259
		rm -rf $DIR_tmp
248
		;;
260
		;;
249
	# update the categories which are written in "/usr/local/etc/update_cat.conf" with rsync
261
	# Rsync the categories which are listed in "/usr/local/etc/update_cat.conf". Cron run this function every 12h
250
	-update_cat | --update_cat)
262
	-update_cat | --update_cat)
251
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
263
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
252
		then
264
		then
253
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
265
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
254
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
266
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
Line 257... Line 269...
257
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
269
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
258
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
270
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
259
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
271
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
260
				# Creation of DNSMASQ and Iptables BL and WL
272
				# Creation of DNSMASQ and Iptables BL and WL
261
				DOMAIN=$(basename $PATH_FILE)
273
				DOMAIN=$(basename $PATH_FILE)
262
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correction 'coma' instead of 'dot'
-
 
263
				$SED "/^$/d" $PATH_FILE/domains $PATH_FILE/urls # remove empty lines
274
				cp $PATH_FILE/domains $FILE_tmp
264
				# extract ip addresses for iptables
-
 
265
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
-
 
266
				# for dnsmask, remove IP addresses, accentuated characters and commented lines.
275
				clean_split  # clean ossi custom files & split them for dnsmasq and for iptables
267
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
-
 
268
				$SED "/[äâëêïîöôüû@]/d" $FILE_tmp
-
 
269
				$SED "/^#.*/d" $FILE_tmp
-
 
270
				black=`grep black $PATH_FILE/usage |wc -l`
276
				black=`grep black $PATH_FILE/usage |wc -l`
271
				if [ $black == "1" ]
277
				if [ $black == "1" ]
272
				then
278
				then
273
					# adapt to the dnsmasq syntax for the blacklist
279
					# adapt to the dnsmasq syntax for the blacklist
274
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
280
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
Line 308... Line 314...
308
		do
314
		do
309
			rm -f $dir/ossi*
315
			rm -f $dir/ossi*
310
		done
316
		done
311
		find $DIR_DG_BL/ -type f -name domains | grep ossi- > $FILE_tmp # retrieve ossi directories name where a domain file exist
317
		find $DIR_DG_BL/ -type f -name domains | grep ossi- > $FILE_tmp # retrieve ossi directories name where a domain file exist
312
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
318
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
313
		
-
 
314
		#retrieve max Top Level Domain for domain name synthax
-
 
315
		MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
-
 
316
		if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
-
 
317
			MAX_TLD=18
-
 
318
		fi
-
 
319
		
-
 
320
		for ossi_custom_dir in `cat $FILE_tmp` # create the blacklist and the whitelist files
319
		for ossi_custom_dir in `cat $FILE_tmp` # create the blacklist and the whitelist files
321
		do
320
		do
-
 
321
			chown -R root:apache $ossi_custom_dir
-
 
322
			chmod 770 $ossi_custom_dir
322
			ossi_categorie=`echo $ossi_custom_dir|cut -d "/" -f6`
323
			ossi_categorie=`echo $ossi_custom_dir|cut -d "/" -f6`
323
			short_categorie=`echo "$ossi_categorie" | cut -d"-" -f2`
324
			short_categorie=`echo "$ossi_categorie" | cut -d"-" -f2`
324
			if [ $short_categorie == "bl" ]
325
			if [ $short_categorie == "bl" ]
325
			then
326
			then
326
				categorie_type="black"
327
				categorie_type="black"
327
			else
328
			else
328
				categorie_type="white"
329
				categorie_type="white"
329
			fi
330
			fi
330
			$SED "s/\.\{2,10\}/\./g" $ossi_custom_dir/domains $ossi_custom_dir/urls # correction 'coma' instead of 'dot'
-
 
331
			$SED "/^$/d" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove empty lines
-
 
332
			$SED "s/\r//" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove Windows <CR>
331
			$SED "s/\r//" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove Windows <CR> from custom file
333
			# extract ip addresses for iptables
332
			cp $ossi_custom_dir/domains $FILE_tmp
334
			awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $ossi_custom_dir/domains > $FILE_ip_tmp
-
 
335
 			egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $ossi_custom_dir/domains > $FILE_tmp
333
			clean_split # clean ossi custom files & split them for dnsmasq and for iptables
336
# search for correction	egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
-
 
337
			if [ $categorie_type == "white" ]
334
			if [ $categorie_type == "white" ]
338
			then
335
			then
339
			# adapt the file to the dnsmasq syntax and enable it if needed
336
			# adapt the file to the dnsmasq syntax and enable it if needed
340
			# for the WL
337
			# for the WL
341
				$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
338
				$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp