Subversion Repositories ALCASAR

Rev

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

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