Subversion Repositories ALCASAR

Rev

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

Rev 1395 Rev 1416
1
#/bin/bash
1
#/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 1395 2014-06-19 16:13:27Z richard $
3
# $Id: alcasar-bl.sh 1416 2014-07-22 17:12: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_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)
32
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
32
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
33
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)
34
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
35
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)
36
BL_SERVER="dsi.ut-capitole.fr"
36
BL_SERVER="dsi.ut-capitole.fr"
37
SED="/bin/sed -i"
37
SED="/bin/sed -i"
38
 
38
 
39
# enable/disable the BL & WL categories
39
# enable/disable the BL & WL categories
40
function cat_choice (){
40
function cat_choice (){
41
	# saving ossi category
41
	# saving ossi category
42
	mkdir $DIR_tmp
42
	mkdir $DIR_tmp
43
	cp $DIR_IP_BL/ossi $DIR_tmp
43
	cp $DIR_IP_BL/ossi $DIR_tmp
44
	if [ -d $DIR_IP_BL_ENABLED ]
44
	if [ -d $DIR_IP_BL_ENABLED ]
45
	then
45
	then
46
		for file in `ls -1 $DIR_IP_BL_ENABLED | grep -v "^ossi-*"`
46
		for file in `ls -1 $DIR_IP_BL_ENABLED | grep -v "^ossi-*"`
47
		do
47
		do
48
			rm -f $DIR_IP_BL_ENABLED/$file
48
			rm -f $DIR_IP_BL_ENABLED/$file
49
		done
49
		done
50
	else
50
	else
51
		mkdir $DIR_IP_BL_ENABLED
51
		mkdir $DIR_IP_BL_ENABLED
52
		chown apache $DIR_IP_BL_ENABLED
52
		chown apache $DIR_IP_BL_ENABLED
53
	fi
53
	fi
-
 
54
	if [ -d $DIR_DNS_BL_ENABLED ]
-
 
55
	then
-
 
56
		for file in `ls -1 $DIR_DNS_BL_ENABLED | grep -v "^ossi-*"`
-
 
57
		do
-
 
58
			rm -f $DIR_DNS_BL_ENABLED/$file
-
 
59
		done
-
 
60
	else
-
 
61
		mkdir $DIR_DNS_BL_ENABLED
-
 
62
		chown apache $DIR_DNS_BL_ENABLED
-
 
63
	fi
54
	rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED # cleaning for dnsmasq and iptables
64
	rm -rf $DIR_DNS_WL_ENABLED # cleaning for dnsmasq and iptables
55
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
65
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
56
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
66
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
57
	mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED
67
	mkdir $DIR_DNS_WL_ENABLED
58
	# process the file $BL_CATEGORIES with the choice of categories 
68
	# process the file $BL_CATEGORIES with the choice of categories 
59
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
69
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
60
	do
70
	do
61
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
71
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
62
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
72
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
63
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
73
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
64
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
74
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
65
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
75
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
66
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
76
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
67
	done
77
	done
68
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
78
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
69
	mv $FILE_tmp $BL_CATEGORIES
79
	mv $FILE_tmp $BL_CATEGORIES
70
	# process the file $WL_CATEGORIES with the choice of categories 
80
	# process the file $WL_CATEGORIES with the choice of categories 
71
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
81
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
72
	do
82
	do
73
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
83
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
74
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
84
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
75
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
85
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
76
	done
86
	done
77
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
87
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
78
	mv $FILE_tmp $WL_CATEGORIES
88
	mv $FILE_tmp $WL_CATEGORIES
79
	# restoring ip files and ossi category
89
	# restoring ip files and ossi category
80
	mv $DIR_tmp/ossi $DIR_IP_BL
90
	mv $DIR_tmp/ossi $DIR_IP_BL
81
	chown apache $DIR_IP_BL/ossi
91
	chown apache $DIR_IP_BL/ossi
82
	rm -rf $DIR_tmp
92
	rm -rf $DIR_tmp
83
}
93
}
84
function bl_enable (){
94
function bl_enable (){
85
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
95
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
86
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
96
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
87
	then
97
	then
88
		service dansguardian restart
98
		/bin/systemctl restart dansguardian
89
		service dnsmasq restart
99
		/bin/systemctl restart dnsmasq
90
		service dnsmasq-blacklist restart
100
		/bin/systemctl restart dnsmasq-blacklist
91
		service dnsmasq-whitelist restart
101
		/bin/systemctl restart dnsmasq-whitelist
92
		/usr/local/bin/alcasar-iptables.sh
102
		/usr/local/bin/alcasar-iptables.sh
93
	fi
103
	fi
94
}
104
}
95
function bl_disable (){
105
function bl_disable (){
96
	rm -rf $DIR_DNS_BL_ENABLED/*
106
	rm -rf $DIR_DNS_BL_ENABLED/*
97
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
107
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
98
	$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
108
	$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
99
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist  # remove safe searching
109
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist  # remove safe searching
100
	$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
110
	$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
101
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
111
	if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
102
	then
112
	then
103
		service dansguardian restart
113
		/bin/systemctl restart dansguardian
104
		service dnsmasq restart
114
		/bin/systemctl restart dnsmasq
105
		service dnsmasq-blacklist restart
115
		/bin/systemctl restart dnsmasq-blacklist
106
		service dnsmasq-whitelist restart
116
		/bin/systemctl restart dnsmasq-whitelist
107
		/usr/local/bin/alcasar-iptables.sh
117
		/usr/local/bin/alcasar-iptables.sh
108
	fi
118
	fi
109
}
119
}
110
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
120
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
111
nb_args=$#
121
nb_args=$#
112
args=$1
122
args=$1
113
if [ $nb_args -eq 0 ]
123
if [ $nb_args -eq 0 ]
114
then
124
then
115
	  args="-h"
125
	  args="-h"
116
fi
126
fi
117
case $args in
127
case $args in
118
	-\? | -h* | --h*)
128
	-\? | -h* | --h*)
119
		echo "$usage"
129
		echo "$usage"
120
		exit 0
130
		exit 0
121
		;;
131
		;;
122
	# Retrieve Toulouse BL
132
	# Retrieve Toulouse BL
123
	-cat_choice | --cat_choice)
133
	-cat_choice | --cat_choice)
124
		cat_choice
134
		cat_choice
125
		;;
135
		;;
126
	-download | --download)
136
	-download | --download)
127
		rm -rf /tmp/con_ok.html
137
		rm -rf /tmp/con_ok.html
128
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
138
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
129
		if [ ! -e /tmp/con_ok.html ]
139
		if [ ! -e /tmp/con_ok.html ]
130
		then
140
		then
131
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
141
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
132
		else 
142
		else 
133
			rm -rf /tmp/con_ok.html $DIR_tmp
143
			rm -rf /tmp/con_ok.html $DIR_tmp
134
			mkdir $DIR_tmp
144
			mkdir $DIR_tmp
135
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
145
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
136
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
146
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
137
			chown -R apache:apache $DIR_tmp
147
			chown -R apache:apache $DIR_tmp
138
		fi
148
		fi
139
		;;		
149
		;;		
140
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
150
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
141
	-adapt | --adapt)
151
	-adapt | --adapt)
142
		echo -n "Toulouse BlackList migration process. Please wait : "
152
		echo -n "Toulouse BlackList migration process. Please wait : "
143
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
153
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
144
		then
154
		then
145
			touch $DIR_SHARE/ossi-ip-wl
155
			touch $DIR_SHARE/ossi-ip-wl
146
			chown apache $DIR_SHARE/ossi-ip-wl
156
			chown apache $DIR_SHARE/ossi-ip-wl
147
		fi
157
		fi
148
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
158
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
149
		then
159
		then
150
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
160
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
151
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
161
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
152
			rm -rf $DIR_DG_BL $DIR_IP_BL
162
			rm -rf $DIR_DG_BL $DIR_IP_BL
153
			mkdir $DIR_DG_BL $DIR_IP_BL
163
			mkdir $DIR_DG_BL $DIR_IP_BL
154
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
164
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
155
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
165
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
156
		fi
166
		fi
157
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
167
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
158
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
168
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
159
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
169
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
160
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
170
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
161
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
171
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
162
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
172
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
163
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
173
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
164
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
174
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
165
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
175
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
166
		do
176
		do
167
			categorie=`echo $dir_categorie|cut -d "/" -f6`
177
			categorie=`echo $dir_categorie|cut -d "/" -f6`
168
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
178
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
169
			if [ "$categorie_type" == "white" ]
179
			if [ "$categorie_type" == "white" ]
170
			then
180
			then
171
				echo "$dir_categorie" >> $WL_CATEGORIES 
181
				echo "$dir_categorie" >> $WL_CATEGORIES 
172
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
182
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
173
			else
183
			else
174
				echo "$dir_categorie" >> $BL_CATEGORIES
184
				echo "$dir_categorie" >> $BL_CATEGORIES
175
			fi
185
			fi
176
		done
186
		done
177
		rm -f $FILE_tmp
187
		rm -f $FILE_tmp
178
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
188
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
179
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
189
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
180
		do
190
		do
181
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
191
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
182
			if [ $ok != "1" ] 
192
			if [ $ok != "1" ] 
183
			then
193
			then
184
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
194
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
185
			fi
195
			fi
186
		done
196
		done
187
		# Creation of DNSMASQ and Iptables BL and WL
197
		# Creation of DNSMASQ and Iptables BL and WL
188
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
198
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
189
		do
199
		do
190
			for PATH_FILE in `cat $LIST` # for each category
200
			for PATH_FILE in `cat $LIST` # for each category
191
			do
201
			do
192
				DOMAINE=`basename $PATH_FILE`
202
				DOMAINE=`basename $PATH_FILE`
193
				echo -n "$DOMAINE, "
203
				echo -n "$DOMAINE, "
194
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
204
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
195
				then
205
				then
196
					touch $PATH_FILE/urls
206
					touch $PATH_FILE/urls
197
					chown dansguardian:apache $PATH_FILE/urls
207
					chown dansguardian:apache $PATH_FILE/urls
198
				fi
208
				fi
199
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
209
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
200
				# extract ip addresses for iptables
210
				# extract ip addresses for iptables
201
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
211
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
202
				# for dnsmask, remove IP addesses, accented characters and commented lines.
212
				# for dnsmask, remove IP addesses, accented characters and commented lines.
203
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
213
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
204
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
214
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
205
				$SED "/^#.*/d" $FILE_tmp
215
				$SED "/^#.*/d" $FILE_tmp
206
				if [ "$LIST" == "$BL_CATEGORIES" ]
216
				if [ "$LIST" == "$BL_CATEGORIES" ]
207
				then
217
				then
208
					# adapt to the dnsmasq syntax for the blacklist
218
					# adapt to the dnsmasq syntax for the blacklist
209
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
219
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
210
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
220
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
211
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
221
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
212
				else
222
				else
213
					# adapt to the dnsmasq syntax for the whitelist
223
					# adapt to the dnsmasq syntax for the whitelist
214
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
224
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
215
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
225
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
216
				fi
226
				fi
217
			done
227
			done
218
		done
228
		done
219
		rm -f $FILE_tmp $FILE_ip_tmp
229
		rm -f $FILE_tmp $FILE_ip_tmp
220
		# Restoring ossi file of BL IP
230
		# Restoring ossi file of BL IP
221
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
231
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
222
		rm -rf $DIR_tmp
232
		rm -rf $DIR_tmp
223
		echo
233
		echo
224
		;;
234
		;;
225
	# reload when categories are changed 
235
	# reload when categories are changed 
226
	-reload | --reload)
236
	-reload | --reload)
227
		# for DG
237
		# for DG
228
		chown -R dansguardian:apache $DIR_DG_BL/ossi
238
		chown -R dansguardian:apache $DIR_DG_BL/ossi
229
		chmod -R g+w $DIR_DG_BL/ossi
239
		chmod -R g+w $DIR_DG_BL/ossi
230
		cat_choice
240
		cat_choice
231
		#  for dnsmasq (rehabited domain names)
241
		#  for dnsmasq (rehabited domain names)
232
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
242
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
233
		then
243
		then
234
			for i in `cat $DIR_DG/exceptionsitelist`
244
			for i in `cat $DIR_DG/exceptionsitelist`
235
			do
245
			do
236
				$SED "/$i/d" $DIR_DNS_BL/*
246
				$SED "/$i/d" $DIR_DNS_BL/*
237
			done
247
			done
238
		fi
248
		fi
239
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
249
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
240
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
250
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
241
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
251
		cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
242
		bl_enable
252
		bl_enable
243
		;;
253
		;;
244
	*)
254
	*)
245
		echo "Argument inconnu :$1";
255
		echo "Argument inconnu :$1";
246
		echo "$usage"
256
		echo "$usage"
247
		exit 1
257
		exit 1
248
		;;
258
		;;
249
esac
259
esac
250
 
260
 
251
 
261