Subversion Repositories ALCASAR

Rev

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

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