Subversion Repositories ALCASAR

Rev

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

Rev 2675 Rev 2678
1
#!/bin/bash
1
#!/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 2675 2018-12-14 09:56:50Z lucas.echard $
3
# $Id: alcasar-bl.sh 2678 2018-12-16 20:57:08Z lucas.echard $
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 E2guardian)
9
# Gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via E2guardian)
10
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (E2guardian)
10
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (E2guardian)
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/e2guardian/lists"
21
DIR_DG="/etc/e2guardian/lists"
22
DIR_DG_BL="$DIR_DG/blacklists"
22
DIR_DG_BL="$DIR_DG/blacklists"
23
GLOBAL_USAGE="$DIR_CONF/alcasar-global-usage"				# file containing the description of the lists
23
GLOBAL_USAGE="$DIR_CONF/alcasar-global-usage"				# file containing the description of the lists
24
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
24
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
25
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#	'	'	WL categories
25
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#	'	'	WL categories
26
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
26
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
27
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
27
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
28
DIR_SHARE="/usr/local/share"
28
DIR_SHARE="/usr/local/share"
29
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
29
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
30
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"					# all the WL	'	'	'
30
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"					# all the WL	'	'	'
31
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL
31
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL
32
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# IP ossi disabled WL
32
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# IP ossi disabled WL
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 (ossi and ossi-* imported from ACC)
36
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
37
DNS1=`grep ^DNS1= $CONF_FILE | cut -d'=' -f2-` 			# server DNS1 (for WL domain names)
37
DNS1=`grep ^DNS1= $CONF_FILE | cut -d'=' -f2-` 			# server DNS1 (for WL domain names)
38
BL_SERVER="dsi.ut-capitole.fr"
38
BL_SERVER="dsi.ut-capitole.fr"
39
SED="/bin/sed -i"
39
SED="/bin/sed -i"
40
 
40
 
41
# enable/disable the BL & WL categories
41
# enable/disable the BL & WL categories
42
function cat_choice (){
42
function cat_choice (){
43
	mkdir -p $DIR_tmp
43
	mkdir -p $DIR_tmp
44
	for LIST in $DIR_IP_BL_ENABLED $DIR_DNS_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_WL_ENABLED
44
	for LIST in $DIR_IP_BL_ENABLED $DIR_DNS_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_WL_ENABLED
45
	do
45
	do
46
		if [ ! -e $LIST ] #  only on install stage
46
		if [ ! -e $LIST ] #  only on install stage
47
		then
47
		then
48
			mkdir $LIST
48
			mkdir $LIST
49
		else
49
		else
50
			rm -rf $LIST/*
50
			rm -rf $LIST/*
51
		fi
51
		fi
52
		chown root:apache $LIST
52
		chown root:apache $LIST
53
		chmod 770 $LIST
53
		chmod 770 $LIST
54
	done
54
	done
55
	# update categories with rsync
55
	# update categories with rsync
56
	if [ ! -e $DIR_CONF/update_cat.conf ]
56
	if [ ! -e $DIR_CONF/update_cat.conf ]
57
	then
57
	then
58
		touch $DIR_CONF/update_cat.conf
58
		touch $DIR_CONF/update_cat.conf
59
		chown root:apache $DIR_CONF/update_cat.conf
59
		chown root:apache $DIR_CONF/update_cat.conf
60
		chmod 660 $DIR_CONF/update_cat.conf
60
		chmod 660 $DIR_CONF/update_cat.conf
61
	fi
61
	fi
62
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
62
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
63
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
63
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
64
 
64
 
65
	# process the file $BL_CATEGORIES with the choice of categories
65
	# process the file $BL_CATEGORIES with the choice of categories
66
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
66
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
67
	do
67
	do
68
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
68
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
69
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
69
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
70
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
70
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
71
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
71
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
72
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
72
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
73
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
73
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
74
	done
74
	done
75
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
75
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
76
	mv $FILE_tmp $BL_CATEGORIES
76
	mv $FILE_tmp $BL_CATEGORIES
77
	sort +0.0 -0.2 $BL_CATEGORIES_ENABLED -o $FILE_tmp
77
	sort +0.0 -0.2 $BL_CATEGORIES_ENABLED -o $FILE_tmp
78
	mv $FILE_tmp $BL_CATEGORIES_ENABLED
78
	mv $FILE_tmp $BL_CATEGORIES_ENABLED
79
	chown root:apache $BL_CATEGORIES $BL_CATEGORIES_ENABLED
79
	chown root:apache $BL_CATEGORIES $BL_CATEGORIES_ENABLED
80
	chmod 660 $BL_CATEGORIES $BL_CATEGORIES_ENABLED
80
	chmod 660 $BL_CATEGORIES $BL_CATEGORIES_ENABLED
81
 
81
 
82
# process the file $WL_CATEGORIES with the choice of categories
82
# process the file $WL_CATEGORIES with the choice of categories
83
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
83
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
84
	do
84
	do
85
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES
85
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES
86
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
86
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
87
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
87
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
88
	done
88
	done
89
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
89
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
90
	mv $FILE_tmp $WL_CATEGORIES
90
	mv $FILE_tmp $WL_CATEGORIES
91
	sort +0.0 -0.2 $WL_CATEGORIES_ENABLED -o $FILE_tmp
91
	sort +0.0 -0.2 $WL_CATEGORIES_ENABLED -o $FILE_tmp
92
	mv $FILE_tmp $WL_CATEGORIES_ENABLED
92
	mv $FILE_tmp $WL_CATEGORIES_ENABLED
93
	chown root:apache $WL_CATEGORIES $WL_CATEGORIES_ENABLED
93
	chown root:apache $WL_CATEGORIES $WL_CATEGORIES_ENABLED
94
	chmod 660 $WL_CATEGORIES $WL_CATEGORIES_ENABLED
94
	chmod 660 $WL_CATEGORIES $WL_CATEGORIES_ENABLED
95
}
95
}
96
 
96
 
97
# cleaning file and split it ("domains" in $FILE_tmp & "IP" in $FILE_ip_tmp)
97
# cleaning file and split it ("domains" in $FILE_tmp & "IP" in $FILE_ip_tmp)
98
function clean_split (){
98
function clean_split (){
99
	$SED '/^#.*/d' $FILE_tmp # remove commented lines
99
	$SED '/^#.*/d' $FILE_tmp # remove commented lines
100
	$SED '/^\s*$/d' $FILE_tmp # remove empty lines
100
	$SED '/^\s*$/d' $FILE_tmp # remove empty lines
101
	$SED '/[äâëêïîöôüû@,]/d' $FILE_tmp # remove line with "chelou" characters
101
	$SED '/[äâëêïîöôüû@,]/d' $FILE_tmp # remove line with "chelou" characters
102
	# extract ip addresses for iptables.
102
	# extract ip addresses for iptables.
103
	awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $FILE_tmp > $FILE_ip_tmp
103
	awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $FILE_tmp > $FILE_ip_tmp
104
	# extract domain names for dnsmasq.
104
	# extract domain names for dnsmasq.
105
	$SED -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!p' $FILE_tmp
105
	$SED -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!p' $FILE_tmp
106
	# Retrieve max Top Level Domain for domain name synthax
106
	# Retrieve max Top Level Domain for domain name synthax
107
	#MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
107
	#MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
108
	#if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
108
	#if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
109
	#	MAX_TLD=18
109
	#	MAX_TLD=18
110
	#fi
110
	#fi
111
	# search for correction	egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
111
	# search for correction	egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
112
}
112
}
113
 
113
 
114
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
114
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
115
nb_args=$#
115
nb_args=$#
116
args=$1
116
args=$1
117
if [ $nb_args -eq 0 ]
117
if [ $nb_args -eq 0 ]
118
then
118
then
119
	args="-h"
119
	args="-h"
120
fi
120
fi
121
case $args in
121
case $args in
122
	-\? | -h* | --h*)
122
	-\? | -h* | --h*)
123
		echo "$usage"
123
		echo "$usage"
124
		exit 0
124
		exit 0
125
		;;
125
		;;
126
	# Retrieve Toulouse University BL
126
	# Retrieve Toulouse University BL
127
	-download | --download)
127
	-download | --download)
128
		rm -rf /tmp/con_ok.html
128
		rm -rf /tmp/con_ok.html
129
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
129
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
130
		if [ ! -e /tmp/con_ok.html ]
130
		if [ ! -e /tmp/con_ok.html ]
131
		then
131
		then
132
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
132
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
133
		else
133
		else
134
			rm -rf /tmp/con_ok.html $DIR_tmp
134
			rm -rf /tmp/con_ok.html $DIR_tmp
135
			mkdir $DIR_tmp
135
			mkdir $DIR_tmp
136
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
136
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
137
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
137
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
138
			chown -R apache:apache $DIR_tmp
138
			chown -R apache:apache $DIR_tmp
139
		fi
139
		fi
140
		;;
140
		;;
141
	# enable/disable categories (used only during the alcasar install process)
141
	# enable/disable categories (used only during the alcasar install process)
142
	-cat_choice | --cat_choice)
142
	-cat_choice | --cat_choice)
143
		cat_choice
143
		cat_choice
144
		;;
144
		;;
145
	# Adapt Toulouse University BL to ALCASAR architecture (dnsmasq + DG + iptables)
145
	# Adapt Toulouse University BL to ALCASAR architecture (dnsmasq + DG + iptables)
146
	-adapt | --adapt)
146
	-adapt | --adapt)
147
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
147
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
148
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
148
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
149
		then
149
		then
150
			# keep custom files (ossi)
150
			# keep custom files (ossi)
151
			for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
151
			for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
152
			do
152
			do
153
				mv $DIR_DG_BL/$x $DIR_tmp
153
				mv $DIR_DG_BL/$x $DIR_tmp
154
			done
154
			done
155
			rm -rf $DIR_DG_BL $DIR_IP_BL
155
			rm -rf $DIR_DG_BL $DIR_IP_BL
156
			mkdir $DIR_DG_BL $DIR_IP_BL
156
			mkdir $DIR_DG_BL $DIR_IP_BL
157
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
157
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
-
 
158
			# replace the global_usage file of the archive
-
 
159
			cp -f $GLOBAL_USAGE $DIR_DG_BL/global_usage
158
			chown -R e2guardian:apache $DIR_DG
160
			chown -R e2guardian:apache $DIR_DG
159
			chmod -R 770 $DIR_DG
161
			chmod -R 770 $DIR_DG
-
 
162
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
-
 
163
			# Add the custom categories (ossi-tor_nodes) to the usage file
-
 
164
			cat <<EOF >> $DIR_DG_BL/global_usage
-
 
165
 
-
 
166
NAME: ossi-bl
-
 
167
DEFAULT_TYPE: black
-
 
168
SOURCE: ALCASAR Team
-
 
169
DESC FR: sites blacklistés ajoutés localement
-
 
170
DESC EN: blacklisted sites add locally
-
 
171
NAME FR: ossi-bl
-
 
172
NAME EN: ossi-bl
-
 
173
 
-
 
174
NAME: ossi-wl
-
 
175
DEFAULT_TYPE: white
-
 
176
SOURCE: ALCASAR Team
-
 
177
DESC FR: sites autorisés ajoutés localement
-
 
178
DESC EN: whitelisted sites add locally
-
 
179
NAME FR: ossi-wl
-
 
180
NAME EN: ossi-wl
-
 
181
 
-
 
182
NAME: ossi-bl-tor_nodes
-
 
183
DEFAULT_TYPE: black
-
 
184
SOURCE: ALCASAR Team
-
 
185
DESC FR: Adresses IP des noeuds (routeurs) d'entrée du réseau TOR
-
 
186
DESC EN: IP addresses of input TOR nodes (routers)
-
 
187
NAME FR: Noeuds TOR
-
 
188
NAME EN: TOR nodes
-
 
189
EOF
160
			# Retrieve custom files (ossi)
190
			# Retrieve custom files (ossi)
161
			for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
191
			for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
162
			do
192
			do
163
				mv $DIR_tmp/$x $DIR_DG_BL
193
				mv $DIR_tmp/$x $DIR_DG_BL
164
			done
194
			done
165
		fi
195
		fi
166
		rm -f $BL_CATEGORIES $WL_CATEGORIES
196
		rm -f $BL_CATEGORIES $WL_CATEGORIES
167
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
197
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
168
		rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
198
		rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
169
		touch $BL_CATEGORIES $WL_CATEGORIES
199
		touch $BL_CATEGORIES $WL_CATEGORIES
170
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
200
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
171
		mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
201
		mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
172
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
202
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
173
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
203
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
174
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
204
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
175
		do
205
		do
176
			categorie=`echo $dir_categorie|cut -d "/" -f6`
206
			categorie=`echo $dir_categorie|cut -d "/" -f6`
177
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie\$ $GLOBAL_USAGE | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
207
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie\$ $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
178
			if [ "$categorie_type" == "white" ]
208
			if [ "$categorie_type" == "white" ]
179
			then
209
			then
180
				echo "$dir_categorie" >> $WL_CATEGORIES
210
				echo "$dir_categorie" >> $WL_CATEGORIES
181
			else
211
			else
182
				echo "$dir_categorie" >> $BL_CATEGORIES
212
				echo "$dir_categorie" >> $BL_CATEGORIES
183
			fi
213
			fi
184
		done
214
		done
185
		rm -f $FILE_tmp
215
		rm -f $FILE_tmp
186
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
216
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
187
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
217
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
188
		do
218
		do
189
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
219
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
190
			if [ $ok != "1" ]
220
			if [ $ok != "1" ]
191
			then
221
			then
192
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
222
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
193
			fi
223
			fi
194
		done
224
		done
195
		# Verify that the enabled categories are effectively in the WL (need after an update of the WL)
225
		# Verify that the enabled categories are effectively in the WL (need after an update of the WL)
196
		for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
226
		for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
197
		do
227
		do
198
			ok=`grep /$ENABLE_CATEGORIE$ $WL_CATEGORIES|wc -l`
228
			ok=`grep /$ENABLE_CATEGORIE$ $WL_CATEGORIES|wc -l`
199
			if [ $ok != "1" ]
229
			if [ $ok != "1" ]
200
			then
230
			then
201
				$SED "/^$ENABLE_CATEGORIE$/d" $WL_CATEGORIES_ENABLED
231
				$SED "/^$ENABLE_CATEGORIE$/d" $WL_CATEGORIES_ENABLED
202
			fi
232
			fi
203
		done
233
		done
204
		# Creation of DNSMASQ and Iptables BL and WL
234
		# Creation of DNSMASQ and Iptables BL and WL
205
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
235
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
206
		do
236
		do
207
			for PATH_FILE in `cat $LIST` # for each category
237
			for PATH_FILE in `cat $LIST` # for each category
208
			do
238
			do
209
				DOMAIN=`basename $PATH_FILE`
239
				DOMAIN=`basename $PATH_FILE`
210
				echo -n "$DOMAIN, "
240
				echo -n "$DOMAIN, "
211
				if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
241
				if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
212
				then
242
				then
213
					touch $PATH_FILE/urls
243
					touch $PATH_FILE/urls
214
					chown e2guardian:apache $PATH_FILE/urls
244
					chown e2guardian:apache $PATH_FILE/urls
215
				fi
245
				fi
216
				cp $PATH_FILE/domains $FILE_tmp
246
				cp $PATH_FILE/domains $FILE_tmp
217
				clean_split # clean ossi custom files & split them for dnsmasq and for iptables
247
				clean_split # clean ossi custom files & split them for dnsmasq and for iptables
218
				if [ "$LIST" == "$BL_CATEGORIES" ]
248
				if [ "$LIST" == "$BL_CATEGORIES" ]
219
				then
249
				then
220
					# adapt to the dnsmasq syntax for the blacklist
250
					# adapt to the dnsmasq syntax for the blacklist
221
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
251
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
222
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
252
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
223
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
253
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
224
				else
254
				else
225
					# adapt to the dnsmasq syntax for the whitelist
255
					# adapt to the dnsmasq syntax for the whitelist
226
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp
256
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp
227
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
257
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
228
				fi
258
				fi
229
			done
259
			done
230
		done
260
		done
231
		echo
261
		echo
232
		chown -R root:apache $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
262
		chown -R root:apache $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
233
		chmod 770 $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
263
		chmod 770 $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
234
		chmod -f 660 $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
264
		chmod -f 660 $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
235
		rm -f $FILE_tmp $FILE_ip_tmp
265
		rm -f $FILE_tmp $FILE_ip_tmp
236
		rm -rf $DIR_tmp
266
		rm -rf $DIR_tmp
237
		;;
267
		;;
238
	# Rsync the categories which are listed in "/usr/local/etc/update_cat.conf". Cron run this function every 12h
268
	# Rsync the categories which are listed in "/usr/local/etc/update_cat.conf". Cron run this function every 12h
239
	-update_cat | --update_cat)
269
	-update_cat | --update_cat)
240
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
270
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
241
		then
271
		then
242
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
272
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
243
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
273
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
244
			do
274
			do
245
				CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
275
				CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
246
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
276
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
247
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
277
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
248
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
278
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
249
				# Creation of DNSMASQ and Iptables BL and WL
279
				# Creation of DNSMASQ and Iptables BL and WL
250
				DOMAIN=$(basename $PATH_FILE)
280
				DOMAIN=$(basename $PATH_FILE)
251
				cp $PATH_FILE/domains $FILE_tmp
281
				cp $PATH_FILE/domains $FILE_tmp
252
				clean_split  # clean ossi custom files & split them for dnsmasq and for iptables
282
				clean_split  # clean ossi custom files & split them for dnsmasq and for iptables
253
				black=`grep black $PATH_FILE/usage |wc -l`
283
				black=`grep black $PATH_FILE/usage |wc -l`
254
				if [ $black == "1" ]
284
				if [ $black == "1" ]
255
				then
285
				then
256
					# adapt to the dnsmasq syntax for the blacklist
286
					# adapt to the dnsmasq syntax for the blacklist
257
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
287
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
258
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
288
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
259
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
289
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
260
				else
290
				else
261
					# adapt to the dnsmasq syntax for the whitelist
291
					# adapt to the dnsmasq syntax for the whitelist
262
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp
292
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp
263
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
293
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
264
					mv $FILE_ip_tmp $DIR_IP_WL/$DOMAIN
294
					mv $FILE_ip_tmp $DIR_IP_WL/$DOMAIN
265
				fi
295
				fi
266
				rm -f $FILE_tmp $FILE_ip_tmp
296
				rm -f $FILE_tmp $FILE_ip_tmp
267
			done
297
			done
268
			/usr/bin/systemctl restart dnsmasq-whitelist
298
			/usr/bin/systemctl restart dnsmasq-whitelist
269
			/usr/bin/systemctl restart dnsmasq-blacklist
299
			/usr/bin/systemctl restart dnsmasq-blacklist
270
			/usr/bin/systemctl restart e2guardian
300
			/usr/bin/systemctl restart e2guardian
271
			/usr/local/bin/alcasar-iptables.sh
301
			/usr/local/bin/alcasar-iptables.sh
272
		else
302
		else
273
			echo -n "/usr/local/etc/update_cat.conf is empty ..."
303
			echo -n "/usr/local/etc/update_cat.conf is empty ..."
274
		fi
304
		fi
275
		echo
305
		echo
276
		;;
306
		;;
277
	# reload when selected categories are changed or when ossi change his custom files
307
	# reload when selected categories are changed or when ossi change his custom files
278
	-reload | --reload)
308
	-reload | --reload)
279
		# for DG
309
		# for DG
280
		cat_choice
310
		cat_choice
281
		#  for dnsmasq (rehabited domain names)
311
		#  for dnsmasq (rehabited domain names)
282
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
312
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
283
		then
313
		then
284
			rm -f $DIR_DNS_BL_ENABLED/authorized-ossi-bl $DIR_DNS_BL/authorized-ossi-bl.conf
314
			rm -f $DIR_DNS_BL_ENABLED/authorized-ossi-bl $DIR_DNS_BL/authorized-ossi-bl.conf
285
			touch $DIR_DNS_BL/authorized-ossi-bl.conf
315
			touch $DIR_DNS_BL/authorized-ossi-bl.conf
286
			for i in `cat $DIR_DG/exceptionsitelist`
316
			for i in `cat $DIR_DG/exceptionsitelist`
287
			do
317
			do
288
				$SED "/$i/d" $DIR_DNS_BL/*
318
				$SED "/$i/d" $DIR_DNS_BL/*
289
				echo "server=/$i/#" >> $DIR_DNS_BL/authorized-ossi-bl.conf
319
				echo "server=/$i/#" >> $DIR_DNS_BL/authorized-ossi-bl.conf
290
			done
320
			done
291
			ln -s $DIR_DNS_BL/authorized-ossi-bl.conf $DIR_DNS_BL_ENABLED/authorized-ossi-bl
321
			ln -s $DIR_DNS_BL/authorized-ossi-bl.conf $DIR_DNS_BL_ENABLED/authorized-ossi-bl
292
		fi
322
		fi
293
		# adapt OSSI BL & WL custom files
323
		# adapt OSSI BL & WL custom files
294
		for dir in $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
324
		for dir in $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
295
		do
325
		do
296
			rm -f $dir/ossi*
326
			rm -f $dir/ossi*
297
		done
327
		done
298
		find $DIR_DG_BL/ -type f -name domains | grep ossi- > $FILE_tmp # retrieve ossi directories name where a domain file exist
328
		find $DIR_DG_BL/ -type f -name domains | grep ossi- > $FILE_tmp # retrieve ossi directories name where a domain file exist
299
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
329
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
300
		for ossi_custom_dir in `cat $FILE_tmp` # create the blacklist and the whitelist files
330
		for ossi_custom_dir in `cat $FILE_tmp` # create the blacklist and the whitelist files
301
		do
331
		do
302
			chown -R root:apache $ossi_custom_dir
332
			chown -R root:apache $ossi_custom_dir
303
			chmod 770 $ossi_custom_dir
333
			chmod 770 $ossi_custom_dir
304
			ossi_categorie=`echo $ossi_custom_dir|cut -d "/" -f6`
334
			ossi_categorie=`echo $ossi_custom_dir|cut -d "/" -f6`
305
			short_categorie=`echo "$ossi_categorie" | cut -d"-" -f2`
335
			short_categorie=`echo "$ossi_categorie" | cut -d"-" -f2`
306
			if [ $short_categorie == "bl" ]
336
			if [ $short_categorie == "bl" ]
307
			then
337
			then
308
				categorie_type="black"
338
				categorie_type="black"
309
			else
339
			else
310
				categorie_type="white"
340
				categorie_type="white"
311
			fi
341
			fi
312
			$SED "s/\r//" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove Windows <CR> from custom file
342
			$SED "s/\r//" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove Windows <CR> from custom file
313
			cp $ossi_custom_dir/domains $FILE_tmp
343
			cp $ossi_custom_dir/domains $FILE_tmp
314
			clean_split # clean ossi custom files & split them for dnsmasq and for iptables
344
			clean_split # clean ossi custom files & split them for dnsmasq and for iptables
315
			if [ $categorie_type == "white" ]
345
			if [ $categorie_type == "white" ]
316
			then
346
			then
317
			# adapt the file to the dnsmasq syntax and enable it if needed
347
			# adapt the file to the dnsmasq syntax and enable it if needed
318
			# for the WL
348
			# for the WL
319
				$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp
349
				$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp
320
				mv $FILE_tmp $DIR_DNS_WL/$ossi_categorie.conf
350
				mv $FILE_tmp $DIR_DNS_WL/$ossi_categorie.conf
321
				mv $FILE_ip_tmp $DIR_IP_WL/$ossi_categorie
351
				mv $FILE_ip_tmp $DIR_IP_WL/$ossi_categorie
322
				enabled=`grep ^$ossi_categorie$ $WL_CATEGORIES_ENABLED | wc -l`
352
				enabled=`grep ^$ossi_categorie$ $WL_CATEGORIES_ENABLED | wc -l`
323
				if [ $enabled == "1" ]
353
				if [ $enabled == "1" ]
324
				then
354
				then
325
					$SED "/\/$ossi_categorie$/d" $WL_CATEGORIES
355
					$SED "/\/$ossi_categorie$/d" $WL_CATEGORIES
326
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $WL_CATEGORIES
356
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $WL_CATEGORIES
327
					ln -sf $DIR_DNS_WL/$ossi_categorie.conf $DIR_DNS_WL_ENABLED/$ossi_categorie
357
					ln -sf $DIR_DNS_WL/$ossi_categorie.conf $DIR_DNS_WL_ENABLED/$ossi_categorie
328
					ln -sf $DIR_IP_WL/$ossi_categorie $DIR_IP_WL_ENABLED/$ossi_categorie
358
					ln -sf $DIR_IP_WL/$ossi_categorie $DIR_IP_WL_ENABLED/$ossi_categorie
329
				fi
359
				fi
330
			else
360
			else
331
			# for the BL
361
			# for the BL
332
				$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
362
				$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
333
				mv $FILE_tmp $DIR_DNS_BL/$ossi_categorie.conf
363
				mv $FILE_tmp $DIR_DNS_BL/$ossi_categorie.conf
334
				mv $FILE_ip_tmp $DIR_IP_BL/$ossi_categorie
364
				mv $FILE_ip_tmp $DIR_IP_BL/$ossi_categorie
335
				enabled=`grep ^$ossi_categorie$ $BL_CATEGORIES_ENABLED | wc -l`
365
				enabled=`grep ^$ossi_categorie$ $BL_CATEGORIES_ENABLED | wc -l`
336
				if [ $enabled == "1" ]
366
				if [ $enabled == "1" ]
337
				then
367
				then
338
					$SED "/\/$ossi_categorie$/d" $BL_CATEGORIES
368
					$SED "/\/$ossi_categorie$/d" $BL_CATEGORIES
339
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $BL_CATEGORIES
369
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $BL_CATEGORIES
340
					ln -sf $DIR_DNS_BL/$ossi_categorie.conf $DIR_DNS_BL_ENABLED/$ossi_categorie
370
					ln -sf $DIR_DNS_BL/$ossi_categorie.conf $DIR_DNS_BL_ENABLED/$ossi_categorie
341
					ln -sf $DIR_IP_BL/$ossi_categorie $DIR_IP_BL_ENABLED/$ossi_categorie
371
					ln -sf $DIR_IP_BL/$ossi_categorie $DIR_IP_BL_ENABLED/$ossi_categorie
342
				fi
372
				fi
343
			fi
373
			fi
344
		done
374
		done
345
		for file in $BL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES $WL_CATEGORIES_ENABLED
375
		for file in $BL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES $WL_CATEGORIES_ENABLED
346
		do
376
		do
347
			sort +0.0 -0.2 $file -o $FILE_tmp
377
			sort +0.0 -0.2 $file -o $FILE_tmp
348
			mv $FILE_tmp $file
378
			mv $FILE_tmp $file
349
			chown root:apache $file
379
			chown root:apache $file
350
			chmod 660 $file
380
			chmod 660 $file
351
		done
381
		done
352
		chown -R root:apache $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
382
		chown -R root:apache $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
353
		chmod 660 $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
383
		chmod 660 $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
354
		if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
384
		if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
355
		then
385
		then
356
			/usr/bin/systemctl restart dnsmasq-blacklist
386
			/usr/bin/systemctl restart dnsmasq-blacklist
357
			/usr/bin/systemctl restart dnsmasq-whitelist
387
			/usr/bin/systemctl restart dnsmasq-whitelist
358
			/usr/bin/systemctl restart e2guardian
388
			/usr/bin/systemctl restart e2guardian
359
			/usr/local/bin/alcasar-iptables.sh
389
			/usr/local/bin/alcasar-iptables.sh
360
		fi
390
		fi
361
		;;
391
		;;
362
	*)
392
	*)
363
		echo "Argument inconnu :$1";
393
		echo "Argument inconnu :$1";
364
		echo "$usage"
394
		echo "$usage"
365
		exit 1
395
		exit 1
366
		;;
396
		;;
367
esac
397
esac
368
 
398