Subversion Repositories ALCASAR

Rev

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

Rev 1930 Rev 1931
1
#/bin/bash
1
#/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 1930 2016-06-08 22:00:48Z richard $
3
# $Id: alcasar-bl.sh 1931 2016-06-09 21:09:15Z 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
DIR_WL_tmp="/tmp/whitelists"
18
DIR_WL_tmp="/tmp/whitelists"
19
FILE_tmp="/tmp/filesfilter.txt"
19
FILE_tmp="/tmp/filesfilter.txt"
20
FILE_ip_tmp="/tmp/filesipfilter.txt"
20
FILE_ip_tmp="/tmp/filesipfilter.txt"
21
DIR_DG="/etc/dansguardian/lists"
21
DIR_DG="/etc/dansguardian/lists"
22
DIR_DG_BL="$DIR_DG/blacklists"
22
DIR_DG_BL="$DIR_DG/blacklists"
23
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
23
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
24
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#	'	'	WL categories
24
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#	'	'	WL categories
25
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
25
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
26
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
26
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
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"					# IP ossi disabled WL 
31
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# IP ossi disabled WL 
32
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
32
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
33
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
33
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'
34
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
34
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
35
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
35
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
36
tmp_DIR_IP_BL="/tmp/ossi-iptables-bl"					#IP BL tmp directory to keep ossi files during an update
-
 
37
tmp_DIR_IP_WL="/tmp/ossi-iptables-wl"					#IP WL tmp directory to keep ossi files during an update
-
 
38
tmp_DIR_DNS_BL="/tmp/ossi-dnsmasq-bl"					#DNS BL tmp directory to keep ossi files during an update
-
 
39
tmp_DIR_DNS_WL="/tmp/ossi-dnsmasq-wl"					#DNS WL tmp directory to keep ossi files during an update
-
 
40
#BL tmp directory to keep ossi files
-
 
41
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"				# conf file of dnsmasq-blacklist
-
 
42
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
36
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
43
BL_SERVER="dsi.ut-capitole.fr"
37
BL_SERVER="dsi.ut-capitole.fr"
44
SED="/bin/sed -i"
38
SED="/bin/sed -i"
45
 
39
 
46
# enable/disable the BL & WL categories
40
# enable/disable the BL & WL categories
47
function cat_choice (){
41
function cat_choice (){
48
	mkdir $DIR_tmp
42
	mkdir -p $DIR_tmp
49
	for dir_bl in $DIR_IP_BL_ENABLED $DIR_DNS_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_WL_ENABLED 
43
	for LIST in $DIR_IP_BL_ENABLED $DIR_DNS_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_WL_ENABLED
50
	do
44
	do
51
		rm -f $dir_bl/*
45
		rm -rf $LIST/*
52
	done
46
	done
53
	# update categories with rsync
47
	# update categories with rsync
54
	if [ ! -e $DIR_CONF/update_cat.conf ]
48
	if [ ! -e $DIR_CONF/update_cat.conf ]
55
	then
49
	then
56
		touch $DIR_CONF/update_cat.conf
50
		touch $DIR_CONF/update_cat.conf
57
		chown root:apache $DIR_CONF/update_cat.conf
51
		chown root:apache $DIR_CONF/update_cat.conf
58
		chmod 660 $DIR_CONF/update_cat.conf
52
		chmod 660 $DIR_CONF/update_cat.conf
59
	fi
53
	fi
60
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
54
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
61
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
55
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
62
 
56
 
63
	# process the file $BL_CATEGORIES with the choice of categories 
57
	# process the file $BL_CATEGORIES with the choice of categories 
64
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
58
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
65
	do
59
	do
66
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
60
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
67
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
61
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
68
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
62
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
69
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
63
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
70
		# 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/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
71
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
65
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
72
	done
66
	done
-
 
67
	# add ossi categories 
-
 
68
	for OSSI_CATEGORIE in `ls $DIR_DNS_BL | grep ossi`
-
 
69
	do
-
 
70
		ln -sf $DIR_DNS_BL/$OSSI_CATEGORIE $DIR_DNS_BL_ENABLED/$OSSI_CATEGORIE
-
 
71
	done
-
 
72
	for OSSI_CATEGORIE in `ls $DIR_IP_BL | grep ossi`
-
 
73
	do
-
 
74
		ln -sf $DIR_IP_BL/$OSSI_CATEGORIE $DIR_IP_BL_ENABLED/$OSSI_CATEGORIE
-
 
75
	done
-
 
76
 
73
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
77
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
74
	mv $FILE_tmp $BL_CATEGORIES
78
	mv $FILE_tmp $BL_CATEGORIES
75
 
79
 
76
	# process the file $WL_CATEGORIES with the choice of categories 
80
	# process the file $WL_CATEGORIES with the choice of categories 
77
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
81
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
78
	do
82
	do
79
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
83
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
80
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
84
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
81
		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
82
	done
86
	done
-
 
87
	# add ossi categories 
-
 
88
	for OSSI_CATEGORIE in `ls $DIR_DNS_WL | grep ossi`
-
 
89
	do
-
 
90
		ln -sf $DIR_DNS_WL/$OSSI_CATEGORIE $DIR_DNS_WL_ENABLED/$OSSI_CATEGORIE
-
 
91
	done
-
 
92
	for OSSI_CATEGORIE in `ls $DIR_IP_WL | grep ossi`
-
 
93
	do
-
 
94
		ln -sf $DIR_IP_WL/$OSSI_CATEGORIE $DIR_IP_WL_ENABLED/$OSSI_CATEGORIE
-
 
95
	done
83
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
96
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
84
	mv $FILE_tmp $WL_CATEGORIES
97
	mv $FILE_tmp $WL_CATEGORIES
85
}
98
}
86
 
99
 
87
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
100
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
88
nb_args=$#
101
nb_args=$#
89
args=$1
102
args=$1
90
if [ $nb_args -eq 0 ]
103
if [ $nb_args -eq 0 ]
91
then
104
then
92
	  args="-h"
105
	  args="-h"
93
fi
106
fi
94
case $args in
107
case $args in
95
	-\? | -h* | --h*)
108
	-\? | -h* | --h*)
96
		echo "$usage"
109
		echo "$usage"
97
		exit 0
110
		exit 0
98
		;;
111
		;;
99
	# Retrieve Toulouse University BL
112
	# Retrieve Toulouse University BL
100
	-download | --download)
113
	-download | --download)
101
		rm -rf /tmp/con_ok.html
114
		rm -rf /tmp/con_ok.html
102
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
115
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
103
		if [ ! -e /tmp/con_ok.html ]
116
		if [ ! -e /tmp/con_ok.html ]
104
		then
117
		then
105
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
118
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
106
		else 
119
		else 
107
			rm -rf /tmp/con_ok.html $DIR_tmp
120
			rm -rf /tmp/con_ok.html $DIR_tmp
108
			mkdir $DIR_tmp
121
			mkdir $DIR_tmp
109
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
122
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
110
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
123
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
111
			chown -R apache:apache $DIR_tmp
124
			chown -R apache:apache $DIR_tmp
112
		fi
125
		fi
113
		;;		
126
		;;		
114
	# enable/disable categories (used only during the alcasar install process)
127
	# enable/disable categories (used only during the alcasar install process)
115
	-cat_choice | --cat_choice)
128
	-cat_choice | --cat_choice)
116
		cat_choice
129
		cat_choice
117
		;;
130
		;;
118
	# Adapt Toulouse University BL to ALCASAR architecture (dnsmasq + DG + iptables)
131
	# Adapt Toulouse University BL to ALCASAR architecture (dnsmasq + DG + iptables)
119
	-adapt | --adapt)
132
	-adapt | --adapt)
120
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
133
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
121
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
134
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
122
		then
135
		then
123
			# keep custom files (ossi) 
136
			# keep custom files (ossi) 
124
			for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
137
			for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
125
			do
138
			do
126
				mv $DIR_DG_BL/$x $DIR_tmp
139
				mv $DIR_DG_BL/$x $DIR_tmp
127
			done
140
			done
128
			rm -rf $DIR_DG_BL $DIR_IP_BL
141
			rm -rf $DIR_DG_BL $DIR_IP_BL
129
			mkdir $DIR_DG_BL $DIR_IP_BL
142
			mkdir $DIR_DG_BL $DIR_IP_BL
130
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
143
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
131
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
144
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
132
			cat << EOF >> $DIR_DG_BL/global_usage 
145
			cat << EOF >> $DIR_DG_BL/global_usage 
133
 
146
 
134
NAME: ossi-bl
147
NAME: ossi-bl
135
DEFAULT_TYPE: black
148
DEFAULT_TYPE: black
136
SOURCE: ALCASAR Team
149
SOURCE: ALCASAR Team
137
DESC FR: sites blacklistés ajoutés localement
150
DESC FR: sites blacklistés ajoutés localement
138
NAME RU: ossi-bl
151
NAME RU: ossi-bl
139
NAME EN: blacklisted sites add locally
152
NAME EN: blacklisted sites add locally
140
NAME FR: ossi-bl
153
NAME FR: ossi-bl
141
NAME IT: ossi-bl
154
NAME IT: ossi-bl
142
NAME NL: ossi-bl
155
NAME NL: ossi-bl
143
NAME DE: ossi-bl
156
NAME DE: ossi-bl
144
NAME ES: ossi-bl
157
NAME ES: ossi-bl
145
 
158
 
146
NAME: ossi-wl
159
NAME: ossi-wl
147
DEFAULT_TYPE: white
160
DEFAULT_TYPE: white
148
SOURCE: ALCASAR Team
161
SOURCE: ALCASAR Team
149
DESC FR: sites autorisés ajoutés localement
162
DESC FR: sites autorisés ajoutés localement
150
NAME RU: ossi-wl
163
NAME RU: ossi-wl
151
NAME EN: whitelisted sites add locally
164
NAME EN: whitelisted sites add locally
152
NAME FR: ossi-wl
165
NAME FR: ossi-wl
153
NAME IT: ossi-wl
166
NAME IT: ossi-wl
154
NAME NL: ossi-wl
167
NAME NL: ossi-wl
155
NAME DE: ossi-wl
168
NAME DE: ossi-wl
156
NAME ES: ossi-wl
169
NAME ES: ossi-wl
157
EOF
170
EOF
158
			# Retrieve custom files (ossi)
171
			# Retrieve custom files (ossi)
159
			for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
172
			for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
160
			do
173
			do
161
				mv $DIR_tmp/$x $DIR_DG_BL
174
				mv $DIR_tmp/$x $DIR_DG_BL
162
			done
175
			done
163
		fi
176
		fi
164
		rm -f $BL_CATEGORIES $WL_CATEGORIES
177
		rm -f $BL_CATEGORIES $WL_CATEGORIES
165
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
178
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
166
		touch $BL_CATEGORIES $WL_CATEGORIES
179
		touch $BL_CATEGORIES $WL_CATEGORIES
167
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
180
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
168
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
181
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
169
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
182
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
170
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
183
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
171
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
184
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
172
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
185
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
173
		do
186
		do
174
			categorie=`echo $dir_categorie|cut -d "/" -f6`
187
			categorie=`echo $dir_categorie|cut -d "/" -f6`
175
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
188
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
176
			if [ "$categorie_type" == "white" ]
189
			if [ "$categorie_type" == "white" ]
177
			then
190
			then
178
				echo "$dir_categorie" >> $WL_CATEGORIES 
191
				echo "$dir_categorie" >> $WL_CATEGORIES 
179
			else
192
			else
180
				echo "$dir_categorie" >> $BL_CATEGORIES
193
				echo "$dir_categorie" >> $BL_CATEGORIES
181
			fi
194
			fi
182
		done
195
		done
183
		rm -f $FILE_tmp
196
		rm -f $FILE_tmp
184
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
197
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
185
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
198
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
186
		do
199
		do
187
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
200
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
188
			if [ $ok != "1" ] 
201
			if [ $ok != "1" ] 
189
			then
202
			then
190
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
203
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
191
			fi
204
			fi
192
		done
205
		done
193
		# Verify that the enabled categories are effectively in the WL (need after an update of the WL)
206
		# Verify that the enabled categories are effectively in the WL (need after an update of the WL)
194
		for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
207
		for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
195
		do
208
		do
196
			ok=`grep /$ENABLE_CATEGORIE$ $WL_CATEGORIES|wc -l`
209
			ok=`grep /$ENABLE_CATEGORIE$ $WL_CATEGORIES|wc -l`
197
			if [ $ok != "1" ] 
210
			if [ $ok != "1" ] 
198
			then
211
			then
199
				$SED "/^$ENABLE_CATEGORIE$/d" $WL_CATEGORIES_ENABLED
212
				$SED "/^$ENABLE_CATEGORIE$/d" $WL_CATEGORIES_ENABLED
200
			fi
213
			fi
201
		done
214
		done
202
		# Creation of DNSMASQ and Iptables BL and WL
215
		# Creation of DNSMASQ and Iptables BL and WL
203
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
216
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
204
		do
217
		do
205
			for PATH_FILE in `cat $LIST` # for each category
218
			for PATH_FILE in `cat $LIST` # for each category
206
			do
219
			do
207
				DOMAINE=`basename $PATH_FILE`
220
				DOMAINE=`basename $PATH_FILE`
208
				echo -n "$DOMAINE, "
221
				echo -n "$DOMAINE, "
209
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
222
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
210
				then
223
				then
211
					touch $PATH_FILE/urls
224
					touch $PATH_FILE/urls
212
					chown dansguardian:apache $PATH_FILE/urls
225
					chown dansguardian:apache $PATH_FILE/urls
213
				fi
226
				fi
214
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
227
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
215
				# extract ip addresses for iptables
228
				# extract ip addresses for iptables
216
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
229
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
217
				# for dnsmask, remove IP addesses, accented characters and commented lines.
230
				# for dnsmask, remove IP addesses, accented characters and commented lines.
218
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
231
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
219
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
232
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
220
				$SED "/^#.*/d" $FILE_tmp
233
				$SED "/^#.*/d" $FILE_tmp
221
				if [ "$LIST" == "$BL_CATEGORIES" ]
234
				if [ "$LIST" == "$BL_CATEGORIES" ]
222
				then
235
				then
223
					# adapt to the dnsmasq syntax for the blacklist
236
					# adapt to the dnsmasq syntax for the blacklist
224
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
237
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
225
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
238
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
226
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
239
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
227
				else
240
				else
228
					# adapt to the dnsmasq syntax for the whitelist
241
					# adapt to the dnsmasq syntax for the whitelist
229
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
242
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
230
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
243
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
231
				fi
244
				fi
232
			done
245
			done
233
		done
246
		done
234
		rm -f $FILE_tmp $FILE_ip_tmp
247
		rm -f $FILE_tmp $FILE_ip_tmp
235
		rm -rf $DIR_tmp
248
		rm -rf $DIR_tmp
236
		;;
249
		;;
237
	# update the categories which are written in "/usr/local/etc/update_cat.conf" with rsync
250
	# update the categories which are written in "/usr/local/etc/update_cat.conf" with rsync
238
	-update_cat | --update_cat)
251
	-update_cat | --update_cat)
239
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
252
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
240
		then
253
		then
241
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
254
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
242
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
255
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
243
			do
256
			do
244
				CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
257
				CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
245
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
258
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
246
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
259
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
247
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
260
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
248
				# Creation of DNSMASQ and Iptables BL and WL
261
				# Creation of DNSMASQ and Iptables BL and WL
249
				DOMAINE=$(basename $PATH_FILE)
262
				DOMAINE=$(basename $PATH_FILE)
250
				# correct some synthaxes
263
				# correct some synthaxes
251
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls
264
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls
252
				# extract ip addresses for iptables
265
				# extract ip addresses for iptables
253
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
266
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
254
				# for dnsmask, remove IP addresses, accentuated characters and commented lines.
267
				# for dnsmask, remove IP addresses, accentuated characters and commented lines.
255
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
268
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
256
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
269
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
257
				$SED "/^#.*/d" $FILE_tmp
270
				$SED "/^#.*/d" $FILE_tmp
258
				black=`grep black $PATH_FILE/usage |wc -l`
271
				black=`grep black $PATH_FILE/usage |wc -l`
259
				if [ $black == "1" ]
272
				if [ $black == "1" ]
260
				then
273
				then
261
					# adapt to the dnsmasq syntax for the blacklist
274
					# adapt to the dnsmasq syntax for the blacklist
262
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
275
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
263
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
276
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
264
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
277
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
265
				else
278
				else
266
					# adapt to the dnsmasq syntax for the whitelist
279
					# adapt to the dnsmasq syntax for the whitelist
267
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
280
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
268
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
281
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
269
				fi
282
				fi
270
				rm -f $FILE_tmp $FILE_ip_tmp
283
				rm -f $FILE_tmp $FILE_ip_tmp
271
			done
284
			done
272
			/usr/bin/systemctl restart dnsmasq-whitelist
285
			/usr/bin/systemctl restart dnsmasq-whitelist
273
			/usr/bin/systemctl restart dnsmasq-blacklist
286
			/usr/bin/systemctl restart dnsmasq-blacklist
274
			/usr/bin/systemctl restart dansguardian
287
			/usr/bin/systemctl restart dansguardian
275
			/usr/local/bin/alcasar-iptables.sh
288
			/usr/local/bin/alcasar-iptables.sh
276
		else
289
		else
277
			  echo -n "/usr/local/etc/update_cat.conf is empty ..."
290
			  echo -n "/usr/local/etc/update_cat.conf is empty ..."
278
		fi
291
		fi
279
		echo 
292
		echo 
280
		;;
293
		;;
281
	# reload when categories are changed 
294
	# reload when categories are changed 
282
	-reload | --reload)
295
	-reload | --reload)
283
		# for DG
296
		# for DG
284
		cat_choice
297
		cat_choice
285
		#  for dnsmasq (rehabited domain names)
298
		#  for dnsmasq (rehabited domain names)
286
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
299
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
287
		then
300
		then
288
			for i in `cat $DIR_DG/exceptionsitelist`
301
			for i in `cat $DIR_DG/exceptionsitelist`
289
			do
302
			do
290
				$SED "/$i/d" $DIR_DNS_BL/*
303
				$SED "/$i/d" $DIR_DNS_BL/*
291
			done
304
			done
292
		fi
305
		fi
293
		/usr/bin/systemctl restart dnsmasq-blacklist
306
		/usr/bin/systemctl restart dnsmasq-blacklist
294
		/usr/bin/systemctl restart dnsmasq-whitelist
307
		/usr/bin/systemctl restart dnsmasq-whitelist
295
		/usr/local/bin/alcasar-iptables.sh
308
		/usr/local/bin/alcasar-iptables.sh
296
		;;
309
		;;
297
	*)
310
	*)
298
		echo "Argument inconnu :$1";
311
		echo "Argument inconnu :$1";
299
		echo "$usage"
312
		echo "$usage"
300
		exit 1
313
		exit 1
301
		;;
314
		;;
302
esac
315
esac
303
 
316
 
304
 
317
 
305
 
318
 
306
 
319
 
307
 
320