Subversion Repositories ALCASAR

Rev

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

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