Subversion Repositories ALCASAR

Rev

Rev 1383 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 1383 Rev 1386
1
#/bin/bash
1
#/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 1383 2014-06-11 22:30:51Z richard $
3
# $Id: alcasar-bl.sh 1386 2014-06-12 14:53:07Z 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
 
9
# Gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via Dansguardian)
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)
10
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (Dansguardian)
11
 
11
 
12
DIR_CONF="/usr/local/etc"
12
DIR_CONF="/usr/local/etc"
13
CONF_FILE="$DIR_CONF/alcasar.conf"
13
CONF_FILE="$DIR_CONF/alcasar.conf"
14
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
14
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
15
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
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
16
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`			# ALCASAR LAN IP address
17
DIR_tmp="/tmp/blacklists"
17
DIR_tmp="/tmp/blacklists"
18
FILE_tmp="/tmp/filesfilter.txt"
18
FILE_tmp="/tmp/filesfilter.txt"
19
FILE_ip_tmp="/tmp/filesipfilter.txt"
19
FILE_ip_tmp="/tmp/filesipfilter.txt"
20
DIR_DG="/etc/dansguardian/lists"
20
DIR_DG="/etc/dansguardian/lists"
21
DIR_DG_BL="$DIR_DG/blacklists"
21
DIR_DG_BL="$DIR_DG/blacklists"
22
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
22
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
23
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#'	'		WL	'
23
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#'	'		WL	'
24
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
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
25
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
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 
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
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)
33
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	'	'	'
34
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)
35
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)
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
37
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)
38
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
39
BL_SERVER="dsi.ut-capitole.fr"
39
BL_SERVER="dsi.ut-capitole.fr"
40
SED="/bin/sed -i"
40
SED="/bin/sed -i"
41
 
41
 
42
# enable/disable the BL & WL categories
42
# enable/disable the BL & WL categories
43
function cat_choice (){
43
function cat_choice (){
44
	# saving ip files and ossi category
44
	# saving ip files and ossi category
45
	mkdir $DIR_tmp
45
	mkdir $DIR_tmp
-
 
46
	if [ $(find $DIR_IP_BL_ENABLED -name "ossi-*" | wc -l) -ne 0 ]
-
 
47
	then
46
	cp $DIR_IP_BL_ENABLED/ossi-* $DIR_tmp
48
		cp $DIR_IP_BL_ENABLED/ossi-* $DIR_tmp
-
 
49
	fi
47
	cp $DIR_IP_BL/ossi $DIR_tmp
50
	cp $DIR_IP_BL/ossi $DIR_tmp
48
	rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED # cleaning for dnsmasq and iptables
51
	rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED # cleaning for dnsmasq and iptables
49
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
52
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
50
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
53
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
51
	mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
54
	mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
52
	chown apache $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
55
	chown apache $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
53
	# process the file $BL_CATEGORIES with the choice of categories 
56
	# process the file $BL_CATEGORIES with the choice of categories 
54
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
57
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
55
	do
58
	do
56
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
59
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
57
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
60
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
58
		ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
61
		ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
59
		ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
62
		ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
60
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
63
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
61
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
64
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
62
	done
65
	done
63
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
66
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
64
	mv $FILE_tmp $BL_CATEGORIES
67
	mv $FILE_tmp $BL_CATEGORIES
65
	# process the file $WL_CATEGORIES with the choice of categories 
68
	# process the file $WL_CATEGORIES with the choice of categories 
66
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
69
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
67
	do
70
	do
68
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
71
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
69
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
72
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
70
		ln -s $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
73
		ln -s $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
71
		ln -s $DIR_IP_WL/$ENABLE_CATEGORIE $DIR_IP_WL_ENABLED/$ENABLE_CATEGORIE
74
		ln -s $DIR_IP_WL/$ENABLE_CATEGORIE $DIR_IP_WL_ENABLED/$ENABLE_CATEGORIE
72
	done
75
	done
73
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
76
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
74
	mv $FILE_tmp $WL_CATEGORIES
77
	mv $FILE_tmp $WL_CATEGORIES
75
	# restoring ip files and ossi category
78
	# restoring ip files and ossi category
76
	mv $DIR_tmp/ossi $DIR_IP_BL
79
	mv $DIR_tmp/ossi $DIR_IP_BL
77
	chown apache $DIR_IP_BL/ossi
80
	chown apache $DIR_IP_BL/ossi
-
 
81
	if [ $(find $DIR_tmp -name "ossi-*" | wc -l) -ne 0 ]
-
 
82
	then
78
	mv $DIR_tmp/ossi-* $DIR_IP_BL_ENABLED
83
		mv $DIR_tmp/ossi-* $DIR_IP_BL_ENABLED
-
 
84
	fi
79
	rm -rf $DIR_tmp
85
	rm -rf $DIR_tmp
80
}
86
}
81
function bl_enable (){
87
function bl_enable (){
82
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
88
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
83
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
89
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
84
	then
90
	then
85
		service dansguardian restart
91
		service dansguardian restart
86
		service dnsmasq restart
92
		service dnsmasq restart
87
		service dnsmasq-blacklist restart
93
		service dnsmasq-blacklist restart
88
		service dnsmasq-whitelist restart
94
		service dnsmasq-whitelist restart
89
		/usr/local/bin/alcasar-iptables.sh
95
		/usr/local/bin/alcasar-iptables.sh
90
	fi
96
	fi
91
}
97
}
92
function bl_disable (){
98
function bl_disable (){
93
	rm -rf $DIR_DNS_BL_ENABLED/*
99
	rm -rf $DIR_DNS_BL_ENABLED/*
94
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
100
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
95
	$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
101
	$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
96
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist  # remove safe searching
102
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist  # remove safe searching
97
	$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
103
	$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
98
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
104
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
99
	then
105
	then
100
		service dansguardian restart
106
		service dansguardian restart
101
		service dnsmasq restart
107
		service dnsmasq restart
102
		service dnsmasq-blacklist restart
108
		service dnsmasq-blacklist restart
103
		service dnsmasq-whitelist restart
109
		service dnsmasq-whitelist restart
104
		/usr/local/bin/alcasar-iptables.sh
110
		/usr/local/bin/alcasar-iptables.sh
105
	fi
111
	fi
106
}
112
}
107
function ip_retrieving (){
113
function ip_retrieving (){
108
	# retrieving IPs of all domain names for the whitelist
114
	# retrieving IPs of all domain names for the whitelist
109
	index=0
115
	index=0
110
	if [ ! -d $DIR_IP_WL ]
116
	if [ ! -d $DIR_IP_WL ]
111
	then
117
	then
112
		mkdir $DIR_IP_WL
118
		mkdir $DIR_IP_WL
-
 
119
		touch $DIR_IP_WL/ossi
-
 
120
		chown apache $DIR_IP_WL/ossi
113
	else
121
	else
-
 
122
		# delete old IPs
-
 
123
		mkdir $DIR_tmp
-
 
124
		cp $DIR_IP_WL/ossi $DIR_tmp
114
		rm -rf $DIR_IP_WL/*
125
		rm -rf $DIR_IP_WL/*
-
 
126
		cp $DIR_tmp/ossi $DIR_IP_WL
-
 
127
		rm -rf $DIR_tmp
115
	fi
128
	fi
116
	echo "Retrieving IPs :"
129
	echo "Retrieving IPs :"
117
	cd $DIR_DNS_WL
130
	cd $DIR_DNS_WL
118
	for category in `ls | cut -d '.' -f 1`
131
	for category in `ls | cut -d '.' -f 1`
119
	do
132
	do
120
		echo -n "$category :"
133
		echo -n "$category :"
121
		for domain in `cat $category.conf | cut -d '/' -f 2`
134
		for domain in `cat $category.conf | cut -d '/' -f 2`
122
		do
135
		do
123
			echo `host $domain | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'` >> $DIR_IP_WL/$category.tmp &
136
			echo `host $domain | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'` >> $DIR_IP_WL/$category.tmp &
124
			((index++))
137
			((index++))
125
			echo -n "."
138
			echo -n "."
126
			if [ $index -eq 100 ]
139
			if [ $index -eq 100 ]
127
			then
140
			then
128
				index=0
141
				index=0
129
				sleep 1
142
				sleep 1
130
			fi
143
			fi
131
		done
144
		done
132
	done
145
	done
133
	echo "done"
146
	echo "done"
134
	sleep 2
147
	sleep 5
135
	cd $DIR_IP_WL
148
	cd $DIR_IP_WL
136
	for category in `ls`
149
	for category in `ls`
137
	do
150
	do
138
		# one IP per line
151
		# one IP per line
139
		$SED 's/ /\n/g' $category
152
		$SED 's/ /\n/g' $category
140
		# add SET syntax
153
		# add SET syntax
141
		$SED 's/^/add whitelist_ip_allowed /g' $category
154
		$SED 's/^/add whitelist_ip_allowed /g' $category
142
		# delete empty lines
155
		# delete empty lines
143
		$SED '/^$/d' $category
156
		$SED '/^$/d' $category
144
		# delete false entries
157
		# delete false entries
145
		$SED -r '/([0-9]{1,3}.){3}[0-9]{1,3}/!d' $category
158
		$SED -r '/([0-9]{1,3}.){3}[0-9]{1,3}/!d' $category
146
		# delete duplicate lines
159
		# delete duplicate lines
147
		sort -u $category > ${category%%.*}
160
		sort -u $category > ${category%%.*}
148
	done
161
	done
149
	rm -f *.tmp
162
	rm -f *.tmp
150
}
163
}
151
function ip_retrieving_ossi (){
164
function ip_retrieving_ossi (){
152
	# retrieving IPs of all domain names for the ossi category
165
	# retrieving IPs of all domain names for the ossi category
153
	> $OSSI_WL_IP
166
	> $OSSI_WL_IP
154
	for domain in `cat $OSSI_DOMAINS_WL`
167
	for domain in `cat $OSSI_DOMAINS_WL`
155
	do
168
	do
156
		echo `host $domain | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'` >> $OSSI_WL_IP &
169
		echo `host $domain | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'` >> $OSSI_WL_IP &
157
	done
170
	done
158
	sleep 2
171
	sleep 5
159
	# one IP per line
172
	# one IP per line
160
	$SED 's/ /\n/g' $OSSI_WL_IP
173
	$SED 's/ /\n/g' $OSSI_WL_IP
161
	# add SET syntax
174
	# add SET syntax
162
	$SED 's/^/add whitelist_ip_allowed /g' $OSSI_WL_IP
175
	$SED 's/^/add whitelist_ip_allowed /g' $OSSI_WL_IP
163
	# delete empty lines
176
	# delete empty lines
164
	$SED '/^$/d' $OSSI_WL_IP
177
	$SED '/^$/d' $OSSI_WL_IP
165
	# delete false entries
178
	# delete false entries
166
	$SED -r '/([0-9]{1,3}.){3}[0-9]{1,3}/!d' $OSSI_WL_IP
179
	$SED -r '/([0-9]{1,3}.){3}[0-9]{1,3}/!d' $OSSI_WL_IP
167
}
180
}
168
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 }"
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 }"
169
nb_args=$#
182
nb_args=$#
170
args=$1
183
args=$1
171
if [ $nb_args -eq 0 ]
184
if [ $nb_args -eq 0 ]
172
then
185
then
173
	  args="-h"
186
	  args="-h"
174
fi
187
fi
175
case $args in
188
case $args in
176
	-\? | -h* | --h*)
189
	-\? | -h* | --h*)
177
		echo "$usage"
190
		echo "$usage"
178
		exit 0
191
		exit 0
179
		;;
192
		;;
180
	# Retrieve Toulouse BL
193
	# Retrieve Toulouse BL
181
	-cat_choice | --cat_choice)
194
	-cat_choice | --cat_choice)
182
		cat_choice
195
		cat_choice
183
		;;
196
		;;
184
	-download | --download)
197
	-download | --download)
185
		rm -rf /tmp/con_ok.html
198
		rm -rf /tmp/con_ok.html
186
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
199
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
187
		if [ ! -e /tmp/con_ok.html ]
200
		if [ ! -e /tmp/con_ok.html ]
188
		then
201
		then
189
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
202
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
190
		else 
203
		else 
191
			rm -rf /tmp/con_ok.html $DIR_tmp
204
			rm -rf /tmp/con_ok.html $DIR_tmp
192
			mkdir $DIR_tmp
205
			mkdir $DIR_tmp
193
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
206
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
194
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
207
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
195
			chown -R apache:apache $DIR_tmp
208
			chown -R apache:apache $DIR_tmp
196
		fi
209
		fi
197
		;;		
210
		;;		
198
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
211
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
199
	-adapt | --adapt)
212
	-adapt | --adapt)
200
		echo -n "Toulouse BlackList migration process. Please wait : "
213
		echo -n "Toulouse BlackList migration process. Please wait : "
201
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
214
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
202
		then
215
		then
203
			touch $DIR_SHARE/ossi-ip-wl
216
			touch $DIR_SHARE/ossi-ip-wl
204
			chown apache $DIR_SHARE/ossi-ip-wl
217
			chown apache $DIR_SHARE/ossi-ip-wl
205
		fi
218
		fi
206
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
219
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
207
		then
220
		then
208
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
221
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
209
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
222
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
210
			rm -rf $DIR_DG_BL $DIR_IP_BL
223
			rm -rf $DIR_DG_BL $DIR_IP_BL
211
			mkdir $DIR_DG_BL $DIR_IP_BL
224
			mkdir $DIR_DG_BL $DIR_IP_BL
212
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
225
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
213
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
226
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
214
		fi
227
		fi
215
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
228
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
216
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
229
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
217
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
230
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
218
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
231
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
219
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
232
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
220
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
233
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
221
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
234
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
222
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
235
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
223
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
236
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
224
		do
237
		do
225
			categorie=`echo $dir_categorie|cut -d "/" -f6`
238
			categorie=`echo $dir_categorie|cut -d "/" -f6`
226
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
239
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
227
			if [ "$categorie_type" == "white" ]
240
			if [ "$categorie_type" == "white" ]
228
			then
241
			then
229
				echo "$dir_categorie" >> $WL_CATEGORIES 
242
				echo "$dir_categorie" >> $WL_CATEGORIES 
230
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
243
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
231
			else
244
			else
232
				echo "$dir_categorie" >> $BL_CATEGORIES
245
				echo "$dir_categorie" >> $BL_CATEGORIES
233
			fi
246
			fi
234
		done
247
		done
235
		rm -f $FILE_tmp
248
		rm -f $FILE_tmp
236
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
249
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
237
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
250
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
238
		do
251
		do
239
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
252
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
240
			if [ $ok != "1" ] 
253
			if [ $ok != "1" ] 
241
			then
254
			then
242
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
255
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
243
			fi
256
			fi
244
		done
257
		done
245
		# Creation of DNSMASQ and Iptables BL and WL
258
		# Creation of DNSMASQ and Iptables BL and WL
246
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
259
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
247
		do
260
		do
248
			for PATH_FILE in `cat $LIST` # for each category
261
			for PATH_FILE in `cat $LIST` # for each category
249
			do
262
			do
250
				DOMAINE=`basename $PATH_FILE`
263
				DOMAINE=`basename $PATH_FILE`
251
				echo -n "$DOMAINE, "
264
				echo -n "$DOMAINE, "
252
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
265
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
253
				then
266
				then
254
					touch $PATH_FILE/urls
267
					touch $PATH_FILE/urls
255
					chown dansguardian:apache $PATH_FILE/urls
268
					chown dansguardian:apache $PATH_FILE/urls
256
				fi
269
				fi
257
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
270
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
258
				# extract ip addresses for iptables
271
				# extract ip addresses for iptables
259
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
272
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
260
				# for dnsmask, remove IP addesses, accented characters and commented lines.
273
				# for dnsmask, remove IP addesses, accented characters and commented lines.
261
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
274
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
262
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
275
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
263
				$SED "/^#.*/d" $FILE_tmp
276
				$SED "/^#.*/d" $FILE_tmp
264
				if [ "$LIST" == "$BL_CATEGORIES" ]
277
				if [ "$LIST" == "$BL_CATEGORIES" ]
265
				then
278
				then
266
					# adapt to the dnsmasq syntax for the blacklist
279
					# adapt to the dnsmasq syntax for the blacklist
267
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
280
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
268
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
281
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
269
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
282
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
270
				else
283
				else
271
					# adapt to the dnsmasq syntax for the whitelist
284
					# adapt to the dnsmasq syntax for the whitelist
272
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
285
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
273
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
286
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
274
				fi
287
				fi
275
			done
288
			done
276
		done
289
		done
277
		rm -f $FILE_tmp $FILE_ip_tmp
290
		rm -f $FILE_tmp $FILE_ip_tmp
278
		# Restoring ossi file of BL IP
291
		# Restoring ossi file of BL IP
279
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
292
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
280
		rm -rf $DIR_tmp
293
		rm -rf $DIR_tmp
281
		echo
294
		echo
282
		;;
295
		;;
283
	# reload when categories are changed 
296
	# reload when categories are changed 
284
	-reload | --reload)
297
	-reload | --reload)
285
		# for DG
298
		# for DG
286
		chown -R dansguardian:apache $DIR_DG_BL/ossi
299
		chown -R dansguardian:apache $DIR_DG_BL/ossi
287
		chmod -R g+w $DIR_DG_BL/ossi
300
		chmod -R g+w $DIR_DG_BL/ossi
288
		cat_choice
301
		cat_choice
289
		#  for dnsmasq (rehabited domain names)
302
		#  for dnsmasq (rehabited domain names)
290
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
303
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
291
		then
304
		then
292
			for i in `cat $DIR_DG/exceptionsitelist`
305
			for i in `cat $DIR_DG/exceptionsitelist`
293
			do
306
			do
294
				$SED "/$i/d" $DIR_DNS_BL/*
307
				$SED "/$i/d" $DIR_DNS_BL/*
295
			done
308
			done
296
		fi
309
		fi
297
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
310
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
298
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
311
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
299
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
312
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
300
		bl_enable
313
		bl_enable
301
		;;
314
		;;
302
	# retrieving IPs of all domain names for the whitelist
315
	# retrieving IPs of all domain names for the whitelist
303
	-ip_retrieving | --ip_retrieving)	
316
	-ip_retrieving | --ip_retrieving)	
304
		ip_retrieving
317
		ip_retrieving
305
		;;
318
		;;
306
	# retrieving IPs of all domain names for the whitelist ossi category
319
	# retrieving IPs of all domain names for the whitelist ossi category
307
	-ip_retrieving_ossi | --ip_retrieving_ossi)	
320
	-ip_retrieving_ossi | --ip_retrieving_ossi)	
308
		ip_retrieving_ossi
321
		ip_retrieving_ossi
309
		;;
322
		;;
310
	*)
323
	*)
311
		echo "Argument inconnu :$1";
324
		echo "Argument inconnu :$1";
312
		echo "$usage"
325
		echo "$usage"
313
		exit 1
326
		exit 1
314
		;;
327
		;;
315
esac
328
esac
316
 
329
 
317
 
330