Subversion Repositories ALCASAR

Rev

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

Rev 1386 Rev 1390
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
 
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 
-
 
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
 
42
# enable/disable the BL & WL categories
39
# enable/disable the BL & WL categories
43
function cat_choice (){
40
function cat_choice (){
44
	# saving ip files and ossi category
41
	# saving ip files and ossi category
45
	mkdir $DIR_tmp
42
	mkdir $DIR_tmp
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
61
		ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
58
		ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
62
		ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
59
		ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
63
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
60
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
64
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
61
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
65
	done
62
	done
66
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
63
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
67
	mv $FILE_tmp $BL_CATEGORIES
64
	mv $FILE_tmp $BL_CATEGORIES
68
	# process the file $WL_CATEGORIES with the choice of categories 
65
	# process the file $WL_CATEGORIES with the choice of categories 
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
80
	chown apache $DIR_IP_BL/ossi
76
	chown apache $DIR_IP_BL/ossi
81
	if [ $(find $DIR_tmp -name "ossi-*" | wc -l) -ne 0 ]
77
	if [ $(find $DIR_tmp -name "ossi-*" | wc -l) -ne 0 ]
82
	then
78
	then
83
		mv $DIR_tmp/ossi-* $DIR_IP_BL_ENABLED
79
		mv $DIR_tmp/ossi-* $DIR_IP_BL_ENABLED
84
	fi
80
	fi
85
	rm -rf $DIR_tmp
81
	rm -rf $DIR_tmp
86
}
82
}
87
function bl_enable (){
83
function bl_enable (){
88
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
84
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
89
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
85
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
90
	then
86
	then
91
		service dansguardian restart
87
		service dansguardian restart
92
		service dnsmasq restart
88
		service dnsmasq restart
93
		service dnsmasq-blacklist restart
89
		service dnsmasq-blacklist restart
94
		service dnsmasq-whitelist restart
90
		service dnsmasq-whitelist restart
95
		/usr/local/bin/alcasar-iptables.sh
91
		/usr/local/bin/alcasar-iptables.sh
96
	fi
92
	fi
97
}
93
}
98
function bl_disable (){
94
function bl_disable (){
99
	rm -rf $DIR_DNS_BL_ENABLED/*
95
	rm -rf $DIR_DNS_BL_ENABLED/*
100
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
96
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
101
	$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
97
	$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
102
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist  # remove safe searching
98
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist  # remove safe searching
103
	$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
99
	$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
104
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
100
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
105
	then
101
	then
106
		service dansguardian restart
102
		service dansguardian restart
107
		service dnsmasq restart
103
		service dnsmasq restart
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"
187
fi
115
fi
188
case $args in
116
case $args in
189
	-\? | -h* | --h*)
117
	-\? | -h* | --h*)
190
		echo "$usage"
118
		echo "$usage"
191
		exit 0
119
		exit 0
192
		;;
120
		;;
193
	# Retrieve Toulouse BL
121
	# Retrieve Toulouse BL
194
	-cat_choice | --cat_choice)
122
	-cat_choice | --cat_choice)
195
		cat_choice
123
		cat_choice
196
		;;
124
		;;
197
	-download | --download)
125
	-download | --download)
198
		rm -rf /tmp/con_ok.html
126
		rm -rf /tmp/con_ok.html
199
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
127
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
200
		if [ ! -e /tmp/con_ok.html ]
128
		if [ ! -e /tmp/con_ok.html ]
201
		then
129
		then
202
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
130
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
203
		else 
131
		else 
204
			rm -rf /tmp/con_ok.html $DIR_tmp
132
			rm -rf /tmp/con_ok.html $DIR_tmp
205
			mkdir $DIR_tmp
133
			mkdir $DIR_tmp
206
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
134
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
207
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
135
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
208
			chown -R apache:apache $DIR_tmp
136
			chown -R apache:apache $DIR_tmp
209
		fi
137
		fi
210
		;;		
138
		;;		
211
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
139
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
212
	-adapt | --adapt)
140
	-adapt | --adapt)
213
		echo -n "Toulouse BlackList migration process. Please wait : "
141
		echo -n "Toulouse BlackList migration process. Please wait : "
214
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
142
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
215
		then
143
		then
216
			touch $DIR_SHARE/ossi-ip-wl
144
			touch $DIR_SHARE/ossi-ip-wl
217
			chown apache $DIR_SHARE/ossi-ip-wl
145
			chown apache $DIR_SHARE/ossi-ip-wl
218
		fi
146
		fi
219
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
147
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
220
		then
148
		then
221
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
149
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
222
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
150
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
223
			rm -rf $DIR_DG_BL $DIR_IP_BL
151
			rm -rf $DIR_DG_BL $DIR_IP_BL
224
			mkdir $DIR_DG_BL $DIR_IP_BL
152
			mkdir $DIR_DG_BL $DIR_IP_BL
225
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
153
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
226
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
154
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
227
		fi
155
		fi
228
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
156
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
229
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
157
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
230
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
158
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
231
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
159
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
232
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
160
		chown -R dansguardian:apache $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
161
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
234
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
162
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
235
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
163
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
236
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
164
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
237
		do
165
		do
238
			categorie=`echo $dir_categorie|cut -d "/" -f6`
166
			categorie=`echo $dir_categorie|cut -d "/" -f6`
239
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
167
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
240
			if [ "$categorie_type" == "white" ]
168
			if [ "$categorie_type" == "white" ]
241
			then
169
			then
242
				echo "$dir_categorie" >> $WL_CATEGORIES 
170
				echo "$dir_categorie" >> $WL_CATEGORIES 
243
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
171
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
244
			else
172
			else
245
				echo "$dir_categorie" >> $BL_CATEGORIES
173
				echo "$dir_categorie" >> $BL_CATEGORIES
246
			fi
174
			fi
247
		done
175
		done
248
		rm -f $FILE_tmp
176
		rm -f $FILE_tmp
249
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
177
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
250
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
178
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
251
		do
179
		do
252
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
180
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
253
			if [ $ok != "1" ] 
181
			if [ $ok != "1" ] 
254
			then
182
			then
255
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
183
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
256
			fi
184
			fi
257
		done
185
		done
258
		# Creation of DNSMASQ and Iptables BL and WL
186
		# Creation of DNSMASQ and Iptables BL and WL
259
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
187
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
260
		do
188
		do
261
			for PATH_FILE in `cat $LIST` # for each category
189
			for PATH_FILE in `cat $LIST` # for each category
262
			do
190
			do
263
				DOMAINE=`basename $PATH_FILE`
191
				DOMAINE=`basename $PATH_FILE`
264
				echo -n "$DOMAINE, "
192
				echo -n "$DOMAINE, "
265
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
193
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
266
				then
194
				then
267
					touch $PATH_FILE/urls
195
					touch $PATH_FILE/urls
268
					chown dansguardian:apache $PATH_FILE/urls
196
					chown dansguardian:apache $PATH_FILE/urls
269
				fi
197
				fi
270
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
198
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
271
				# extract ip addresses for iptables
199
				# extract ip addresses for iptables
272
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
200
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
273
				# for dnsmask, remove IP addesses, accented characters and commented lines.
201
				# for dnsmask, remove IP addesses, accented characters and commented lines.
274
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
202
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
275
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
203
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
276
				$SED "/^#.*/d" $FILE_tmp
204
				$SED "/^#.*/d" $FILE_tmp
277
				if [ "$LIST" == "$BL_CATEGORIES" ]
205
				if [ "$LIST" == "$BL_CATEGORIES" ]
278
				then
206
				then
279
					# adapt to the dnsmasq syntax for the blacklist
207
					# adapt to the dnsmasq syntax for the blacklist
280
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
208
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
281
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
209
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
282
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
210
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
283
				else
211
				else
284
					# adapt to the dnsmasq syntax for the whitelist
212
					# adapt to the dnsmasq syntax for the whitelist
285
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
213
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
286
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
214
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
287
				fi
215
				fi
288
			done
216
			done
289
		done
217
		done
290
		rm -f $FILE_tmp $FILE_ip_tmp
218
		rm -f $FILE_tmp $FILE_ip_tmp
291
		# Restoring ossi file of BL IP
219
		# Restoring ossi file of BL IP
292
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
220
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
293
		rm -rf $DIR_tmp
221
		rm -rf $DIR_tmp
294
		echo
222
		echo
295
		;;
223
		;;
296
	# reload when categories are changed 
224
	# reload when categories are changed 
297
	-reload | --reload)
225
	-reload | --reload)
298
		# for DG
226
		# for DG
299
		chown -R dansguardian:apache $DIR_DG_BL/ossi
227
		chown -R dansguardian:apache $DIR_DG_BL/ossi
300
		chmod -R g+w $DIR_DG_BL/ossi
228
		chmod -R g+w $DIR_DG_BL/ossi
301
		cat_choice
229
		cat_choice
302
		#  for dnsmasq (rehabited domain names)
230
		#  for dnsmasq (rehabited domain names)
303
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
231
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
304
		then
232
		then
305
			for i in `cat $DIR_DG/exceptionsitelist`
233
			for i in `cat $DIR_DG/exceptionsitelist`
306
			do
234
			do
307
				$SED "/$i/d" $DIR_DNS_BL/*
235
				$SED "/$i/d" $DIR_DNS_BL/*
308
			done
236
			done
309
		fi
237
		fi
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
		;;
328
esac
248
esac
329
 
249
 
330
 
250