Subversion Repositories ALCASAR

Rev

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

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