Subversion Repositories ALCASAR

Rev

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

Rev 1386 Rev 1390
Line 1... Line 1...
1
#/bin/bash
1
#/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 1386 2014-06-12 14:53:07Z richard $
3
# $Id: alcasar-bl.sh 1390 2014-06-17 12:37:37Z 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 26... Line 26...
26
OSSI_DOMAINS_WL="$DIR_DG/blacklists/ossi/domains_wl"			# Domain names for the ossi category
26
OSSI_DOMAINS_WL="$DIR_DG/blacklists/ossi/domains_wl"			# Domain names for the ossi category
27
DIR_SHARE="/usr/local/share"
27
DIR_SHARE="/usr/local/share"
28
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
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	'	'	'
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 
30
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL 
31
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# all the IP addresses of the WL 
-
 
32
OSSI_WL_IP="$DIR_IP_WL/ossi"						# all the IP addresses of the WL ossi category
-
 
33
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
31
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
34
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
32
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
35
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
33
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
36
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (only enabled categories)
-
 
37
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"				# conf file of dnsmasq-blacklist
34
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"				# conf file of dnsmasq-blacklist
38
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
35
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
39
BL_SERVER="dsi.ut-capitole.fr"
36
BL_SERVER="dsi.ut-capitole.fr"
40
SED="/bin/sed -i"
37
SED="/bin/sed -i"
41
 
38
 
Line 46... Line 43...
46
	if [ $(find $DIR_IP_BL_ENABLED -name "ossi-*" | wc -l) -ne 0 ]
43
	if [ $(find $DIR_IP_BL_ENABLED -name "ossi-*" | wc -l) -ne 0 ]
47
	then
44
	then
48
		cp $DIR_IP_BL_ENABLED/ossi-* $DIR_tmp
45
		cp $DIR_IP_BL_ENABLED/ossi-* $DIR_tmp
49
	fi
46
	fi
50
	cp $DIR_IP_BL/ossi $DIR_tmp
47
	cp $DIR_IP_BL/ossi $DIR_tmp
51
	rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED # cleaning for dnsmasq and iptables
48
	rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED # cleaning for dnsmasq and iptables
52
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
49
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
53
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
50
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
54
	mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
51
	mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED
55
	chown apache $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
52
	chown apache $DIR_IP_BL_ENABLED
56
	# process the file $BL_CATEGORIES with the choice of categories 
53
	# process the file $BL_CATEGORIES with the choice of categories 
57
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
54
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
58
	do
55
	do
59
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
56
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
60
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
57
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
Line 69... Line 66...
69
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
66
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
70
	do
67
	do
71
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
68
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
72
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
69
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
73
		ln -s $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
70
		ln -s $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
74
		ln -s $DIR_IP_WL/$ENABLE_CATEGORIE $DIR_IP_WL_ENABLED/$ENABLE_CATEGORIE
-
 
75
	done
71
	done
76
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
72
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
77
	mv $FILE_tmp $WL_CATEGORIES
73
	mv $FILE_tmp $WL_CATEGORIES
78
	# restoring ip files and ossi category
74
	# restoring ip files and ossi category
79
	mv $DIR_tmp/ossi $DIR_IP_BL
75
	mv $DIR_tmp/ossi $DIR_IP_BL
Line 108... Line 104...
108
		service dnsmasq-blacklist restart
104
		service dnsmasq-blacklist restart
109
		service dnsmasq-whitelist restart
105
		service dnsmasq-whitelist restart
110
		/usr/local/bin/alcasar-iptables.sh
106
		/usr/local/bin/alcasar-iptables.sh
111
	fi
107
	fi
112
}
108
}
113
function ip_retrieving (){
-
 
114
	# retrieving IPs of all domain names for the whitelist
-
 
115
	index=0
-
 
116
	if [ ! -d $DIR_IP_WL ]
-
 
117
	then
-
 
118
		mkdir $DIR_IP_WL
-
 
119
		touch $DIR_IP_WL/ossi
-
 
120
		chown apache $DIR_IP_WL/ossi
-
 
121
	else
-
 
122
		# delete old IPs
-
 
123
		mkdir $DIR_tmp
-
 
124
		cp $DIR_IP_WL/ossi $DIR_tmp
-
 
125
		rm -rf $DIR_IP_WL/*
-
 
126
		cp $DIR_tmp/ossi $DIR_IP_WL
-
 
127
		rm -rf $DIR_tmp
-
 
128
	fi
-
 
129
	echo "Retrieving IPs :"
-
 
130
	cd $DIR_DNS_WL
-
 
131
	for category in `ls | cut -d '.' -f 1`
-
 
132
	do
-
 
133
		echo -n "$category :"
-
 
134
		for domain in `cat $category.conf | cut -d '/' -f 2`
-
 
135
		do
-
 
136
			echo `host $domain | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'` >> $DIR_IP_WL/$category.tmp &
-
 
137
			((index++))
-
 
138
			echo -n "."
-
 
139
			if [ $index -eq 100 ]
-
 
140
			then
-
 
141
				index=0
-
 
142
				sleep 1
-
 
143
			fi
-
 
144
		done
-
 
145
	done
-
 
146
	echo "done"
-
 
147
	sleep 5
-
 
148
	cd $DIR_IP_WL
-
 
149
	for category in `ls`
-
 
150
	do
-
 
151
		# one IP per line
-
 
152
		$SED 's/ /\n/g' $category
-
 
153
		# add SET syntax
-
 
154
		$SED 's/^/add whitelist_ip_allowed /g' $category
-
 
155
		# delete empty lines
-
 
156
		$SED '/^$/d' $category
-
 
157
		# delete false entries
-
 
158
		$SED -r '/([0-9]{1,3}.){3}[0-9]{1,3}/!d' $category
-
 
159
		# delete duplicate lines
-
 
160
		sort -u $category > ${category%%.*}
-
 
161
	done
-
 
162
	rm -f *.tmp
-
 
163
}
-
 
164
function ip_retrieving_ossi (){
-
 
165
	# retrieving IPs of all domain names for the ossi category
-
 
166
	> $OSSI_WL_IP
-
 
167
	for domain in `cat $OSSI_DOMAINS_WL`
-
 
168
	do
-
 
169
		echo `host $domain | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'` >> $OSSI_WL_IP &
-
 
170
	done
-
 
171
	sleep 5
-
 
172
	# one IP per line
-
 
173
	$SED 's/ /\n/g' $OSSI_WL_IP
-
 
174
	# add SET syntax
-
 
175
	$SED 's/^/add whitelist_ip_allowed /g' $OSSI_WL_IP
-
 
176
	# delete empty lines
-
 
177
	$SED '/^$/d' $OSSI_WL_IP
-
 
178
	# delete false entries
-
 
179
	$SED -r '/([0-9]{1,3}.){3}[0-9]{1,3}/!d' $OSSI_WL_IP
-
 
180
}
-
 
181
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -ip_retrieving or --ip_retrieving } | { -ip_retrieving_ossi or --ip_retrieving_ossi }"
109
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
182
nb_args=$#
110
nb_args=$#
183
args=$1
111
args=$1
184
if [ $nb_args -eq 0 ]
112
if [ $nb_args -eq 0 ]
185
then
113
then
186
	  args="-h"
114
	  args="-h"
Line 310... Line 238...
310
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
238
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
311
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
239
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
312
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
240
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
313
		bl_enable
241
		bl_enable
314
		;;
242
		;;
315
	# retrieving IPs of all domain names for the whitelist
-
 
316
	-ip_retrieving | --ip_retrieving)	
-
 
317
		ip_retrieving
-
 
318
		;;
-
 
319
	# retrieving IPs of all domain names for the whitelist ossi category
-
 
320
	-ip_retrieving_ossi | --ip_retrieving_ossi)	
-
 
321
		ip_retrieving_ossi
-
 
322
		;;
-
 
323
	*)
243
	*)
324
		echo "Argument inconnu :$1";
244
		echo "Argument inconnu :$1";
325
		echo "$usage"
245
		echo "$usage"
326
		exit 1
246
		exit 1
327
		;;
247
		;;