Subversion Repositories ALCASAR

Rev

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

Rev 1827 Rev 1852
1
#/bin/bash
1
#/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 1827 2016-04-19 09:47:29Z raphael.pion $
3
# $Id: alcasar-bl.sh 1852 2016-05-01 18:46:46Z raphael.pion $
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
FILE_tmp="/tmp/filesfilter.txt"
19
FILE_tmp="/tmp/filesfilter.txt"
19
FILE_ip_tmp="/tmp/filesipfilter.txt"
20
FILE_ip_tmp="/tmp/filesipfilter.txt"
20
DIR_DG="/etc/dansguardian/lists"
21
DIR_DG="/etc/dansguardian/lists"
21
DIR_DG_BL="$DIR_DG/blacklists"
22
DIR_DG_BL="$DIR_DG/blacklists"
22
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
23
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#'	'		WL	'
24
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#'	'		WL	'
24
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
25
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
26
OSSI_DOMAINS_WL="$DIR_DG/blacklists/ossi/domains_wl"			# Domain names for the ossi category
27
OSSI_DOMAINS_WL="$DIR_DG/blacklists/ossi/domains_wl"			# Domain names for the ossi category
27
DIR_SHARE="/usr/local/share"
28
DIR_SHARE="/usr/local/share"
28
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
29
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl"					# all the BL in the DNSMASQ format
29
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"					# all the WL	'	'	'
30
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl"					# all the WL	'	'	'
30
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL 
31
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL 
31
DIR_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)
32
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
33
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled"			#	'	'	'	WL	'	'	'
33
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)
-
 
36
DIR_IP_WL="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
34
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"				# conf file of dnsmasq-blacklist
37
DNSMASQ_BL_CONF="/etc/dnsmasq-blacklist.conf"				# conf file of dnsmasq-blacklist
35
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
38
DNS1=`grep "DNS1" $CONF_FILE | cut -d '=' -f 2` 			# server DNS1 (for WL domain names)
36
BL_SERVER="dsi.ut-capitole.fr"
39
BL_SERVER="dsi.ut-capitole.fr"
37
SED="/bin/sed -i"
40
SED="/bin/sed -i"
38
 
41
 
39
# enable/disable the BL & WL categories
42
# enable/disable the BL & WL categories
40
function cat_choice (){
43
function cat_choice (){
41
	# saving ossi category
44
	# saving ossi category
42
	mkdir $DIR_tmp
45
	mkdir $DIR_tmp
43
	cp $DIR_IP_BL/ossi $DIR_tmp
46
	cp $DIR_IP_BL/ossi $DIR_tmp
-
 
47
	
44
	if [ -d $DIR_IP_BL_ENABLED ]
48
	if [ -d $DIR_IP_BL_ENABLED ]
45
	then
49
	then
46
		for file in `ls -1 $DIR_IP_BL_ENABLED | grep -v "^ossi-*"`
50
		for file in `ls -1 $DIR_IP_BL_ENABLED | grep -v "^ossi-*"`
47
		do
51
		do
48
			rm -f $DIR_IP_BL_ENABLED/$file
52
			rm -f $DIR_IP_BL_ENABLED/$file
49
		done
53
		done
50
	else
54
	else
51
		mkdir $DIR_IP_BL_ENABLED
55
		mkdir $DIR_IP_BL_ENABLED
52
		chown apache $DIR_IP_BL_ENABLED
56
		chown apache $DIR_IP_BL_ENABLED
53
	fi
57
	fi
-
 
58
	
-
 
59
	if [ -d $DIR_IP_WL_ENABLED ]
-
 
60
	then
-
 
61
		for file in `ls -1 $DIR_IP_WL_ENABLED | grep -v "^ossi-*"`
-
 
62
		do
-
 
63
			rm -f $DIR_IP_WL_ENABLED/$file
-
 
64
		done
-
 
65
	else
-
 
66
		mkdir $DIR_IP_WL_ENABLED
-
 
67
		chown apache $DIR_IP_WL_ENABLED
-
 
68
	fi
-
 
69
	
54
	if [ -d $DIR_DNS_BL_ENABLED ]
70
	if [ -d $DIR_DNS_BL_ENABLED ]
55
	then
71
	then
56
		for file in `ls -1 $DIR_DNS_BL_ENABLED | grep -v "^ossi-*"`
72
		for file in `ls -1 $DIR_DNS_BL_ENABLED | grep -v "^ossi-*"`
57
		do
73
		do
58
			rm -f $DIR_DNS_BL_ENABLED/$file
74
			rm -f $DIR_DNS_BL_ENABLED/$file
59
		done
75
		done
60
	else
76
	else
61
		mkdir $DIR_DNS_BL_ENABLED
77
		mkdir $DIR_DNS_BL_ENABLED
62
		chown apache $DIR_DNS_BL_ENABLED
78
		chown apache $DIR_DNS_BL_ENABLED
63
	fi
79
	fi
-
 
80
	if [ -d $DIR_DNS_WL_ENABLED ]
-
 
81
	then
-
 
82
		for file in `ls -1 $DIR_DNS_WL_ENABLED | grep -v "^ossi-*"`
-
 
83
		do
64
	rm -rf $DIR_DNS_WL_ENABLED # cleaning for dnsmasq and iptables
84
			rm -f $DIR_DNS_WL_ENABLED/$file
-
 
85
		done
-
 
86
	else
-
 
87
		mkdir $DIR_DNS_WL_ENABLED
-
 
88
		chown apache $DIR_DNS_WL_ENABLED
-
 
89
	fi
65
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
90
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
66
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
91
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
67
	mkdir $DIR_DNS_WL_ENABLED
-
 
68
	# process the file $BL_CATEGORIES with the choice of categories 
92
	# process the file $BL_CATEGORIES with the choice of categories 
69
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
93
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
70
	do
94
	do
71
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
95
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES 
72
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
96
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
73
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
97
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
74
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
98
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
75
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
99
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by dnsmasq
76
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
100
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
77
	done
101
	done
78
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
102
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
79
	mv $FILE_tmp $BL_CATEGORIES
103
	mv $FILE_tmp $BL_CATEGORIES
80
	# process the file $WL_CATEGORIES with the choice of categories 
104
	# process the file $WL_CATEGORIES with the choice of categories 
81
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
105
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED` 
82
	do
106
	do
83
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
107
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES 
84
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
108
		$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
109
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
86
	done
110
	done
87
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
111
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
88
	mv $FILE_tmp $WL_CATEGORIES
112
	mv $FILE_tmp $WL_CATEGORIES
-
 
113
	
89
	# restoring ip files and ossi category
114
	# restoring ip files and ossi category BL/WL
90
	mv $DIR_tmp/ossi $DIR_IP_BL
115
	mv $DIR_tmp/ossi $DIR_IP_BL
91
	chown apache $DIR_IP_BL/ossi
116
	chown apache $DIR_IP_BL/ossi
92
	rm -rf $DIR_tmp
117
	rm -rf $DIR_tmp
-
 
118
	
93
}
119
}
94
 
120
 
95
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
121
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
96
nb_args=$#
122
nb_args=$#
97
args=$1
123
args=$1
98
if [ $nb_args -eq 0 ]
124
if [ $nb_args -eq 0 ]
99
then
125
then
100
	  args="-h"
126
	  args="-h"
101
fi
127
fi
102
case $args in
128
case $args in
103
	-\? | -h* | --h*)
129
	-\? | -h* | --h*)
104
		echo "$usage"
130
		echo "$usage"
105
		exit 0
131
		exit 0
106
		;;
132
		;;
107
	# Retrieve Toulouse BL
133
	# Retrieve Toulouse BL
108
	-download | --download)
134
	-download | --download)
109
		rm -rf /tmp/con_ok.html
135
		rm -rf /tmp/con_ok.html
110
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
136
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
111
		if [ ! -e /tmp/con_ok.html ]
137
		if [ ! -e /tmp/con_ok.html ]
112
		then
138
		then
113
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
139
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
114
		else 
140
		else 
115
			rm -rf /tmp/con_ok.html $DIR_tmp
141
			rm -rf /tmp/con_ok.html $DIR_tmp
116
			mkdir $DIR_tmp
142
			mkdir $DIR_tmp
117
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
143
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
118
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
144
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
119
			chown -R apache:apache $DIR_tmp
145
			chown -R apache:apache $DIR_tmp
120
		fi
146
		fi
121
		;;		
147
		;;		
122
	# enable/disable categories (used only during the alcasar install process)
148
	# enable/disable categories (used only during the alcasar install process)
123
	-cat_choice | --cat_choice)
149
	-cat_choice | --cat_choice)
124
		cat_choice
150
		cat_choice
125
		;;
151
		;;
126
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
152
	# Adapt Toulouse BL to ALCASAR architecture (dnsmasq + DG + iptables)
127
	-adapt | --adapt)
153
	-adapt | --adapt)
128
		echo -n "Toulouse BlackList migration process. Please wait : "
154
		echo -n "Toulouse BlackList migration process. Please wait : "
129
		if [ ! -e $DIR_SHARE/ossi-ip-wl ]
155
		if [ ! -e $DIR_IP_WL_ENABLED/ossi ]
130
		then
156
		then
131
			touch $DIR_SHARE/ossi-ip-wl
157
			touch $DIR_IP_WL_ENABLED/ossi
132
			chown apache $DIR_SHARE/ossi-ip-wl
158
			chown apache $DIR_IP_WL_ENABLED/ossi
133
		fi
159
		fi
134
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
160
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
135
		then
161
		then
136
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
162
			[ -d $DIR_DG_BL/ossi ] && mv $DIR_DG_BL/ossi $DIR_tmp
137
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
163
			[ -e $DIR_IP_BL/ossi ] && mv $DIR_IP_BL/ossi $DIR_tmp/ossi-ip-bl
138
			rm -rf $DIR_DG_BL $DIR_IP_BL
164
			rm -rf $DIR_DG_BL $DIR_IP_BL
139
			mkdir $DIR_DG_BL $DIR_IP_BL
165
			mkdir $DIR_DG_BL $DIR_IP_BL
140
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
166
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
141
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
167
			[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
142
		fi
168
		fi
143
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
169
		rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
144
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
170
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
145
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
171
		touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
146
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
172
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
147
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
173
		chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
148
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
174
		chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
149
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
175
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
150
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
176
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
151
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
177
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
152
		do
178
		do
153
			categorie=`echo $dir_categorie|cut -d "/" -f6`
179
			categorie=`echo $dir_categorie|cut -d "/" -f6`
154
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
180
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
155
			if [ "$categorie_type" == "white" ]
181
			if [ "$categorie_type" == "white" ]
156
			then
182
			then
157
				echo "$dir_categorie" >> $WL_CATEGORIES 
183
				echo "$dir_categorie" >> $WL_CATEGORIES 
158
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
184
				echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED  # by default all WL are enabled 
159
			fi
185
			fi
160
			echo "$dir_categorie" >> $BL_CATEGORIES # By default all categories are in BL
186
			echo "$dir_categorie" >> $BL_CATEGORIES # By default all categories are in BL
161
		done
187
		done
162
		rm -f $FILE_tmp
188
		rm -f $FILE_tmp
163
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
189
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
164
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
190
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` 
165
		do
191
		do
166
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
192
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
167
			if [ $ok != "1" ] 
193
			if [ $ok != "1" ] 
168
			then
194
			then
169
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
195
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
170
			fi
196
			fi
171
		done
197
		done
172
		# Creation of DNSMASQ and Iptables BL and WL
198
		# Creation of DNSMASQ and Iptables BL and WL
173
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
199
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
174
		do
200
		do
175
			for PATH_FILE in `cat $LIST` # for each category
201
			for PATH_FILE in `cat $LIST` # for each category
176
			do
202
			do
177
				DOMAINE=`basename $PATH_FILE`
203
				DOMAINE=`basename $PATH_FILE`
178
				echo -n "$DOMAINE, "
204
				echo -n "$DOMAINE, "
179
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
205
		  		if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
180
				then
206
				then
181
					touch $PATH_FILE/urls
207
					touch $PATH_FILE/urls
182
					chown dansguardian:apache $PATH_FILE/urls
208
					chown dansguardian:apache $PATH_FILE/urls
183
				fi
209
				fi
184
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
210
				$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
185
				# extract ip addresses for iptables
211
				# extract ip addresses for iptables
186
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
212
				awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add blacklist_ip_blocked " $0}' $PATH_FILE/domains > $FILE_ip_tmp
187
				# for dnsmask, remove IP addesses, accented characters and commented lines.
213
				# for dnsmask, remove IP addesses, accented characters and commented lines.
188
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
214
				egrep  -v "^([0-9]{1,3}\.){3}[0-9]{1,3}$" $PATH_FILE/domains > $FILE_tmp
189
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
215
				$SED "/[äâëêïîöôüû]/d" $FILE_tmp
190
				$SED "/^#.*/d" $FILE_tmp
216
				$SED "/^#.*/d" $FILE_tmp
191
				if [ "$LIST" == "$BL_CATEGORIES" ]
217
				if [ "$LIST" == "$BL_CATEGORIES" ]
192
				then
218
				then
193
					# adapt to the dnsmasq syntax for the blacklist
219
					# adapt to the dnsmasq syntax for the blacklist
194
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
220
					$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp 
195
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
221
					mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
196
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
222
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
197
				else
223
				else
198
					# adapt to the dnsmasq syntax for the whitelist
224
					# adapt to the dnsmasq syntax for the whitelist
199
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
225
					$SED "s?.*?server=/&/$DNS1?g" $FILE_tmp 
200
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
226
					mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
201
				fi
227
				fi
202
			done
228
			done
203
		done
229
		done
204
		rm -f $FILE_tmp $FILE_ip_tmp
230
		rm -f $FILE_tmp $FILE_ip_tmp
205
		# Restoring ossi file of BL IP
231
		# Restoring ossi file of BL IP
206
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
232
		[ -e $DIR_tmp/ossi-ip-bl ] && mv $DIR_tmp/ossi-ip-bl $DIR_IP_BL/ossi
207
		rm -rf $DIR_tmp
233
		rm -rf $DIR_tmp
208
		echo
234
		echo
209
		;;
235
		;;
210
	# reload when categories are changed 
236
	# reload when categories are changed 
211
	-reload | --reload)
237
	-reload | --reload)
212
		# for DG
238
		# for DG
213
		chown -R dansguardian:apache $DIR_DG_BL/ossi
239
		chown -R dansguardian:apache $DIR_DG_BL/ossi
214
		chmod -R g+w $DIR_DG_BL/ossi
240
		chmod -R g+w $DIR_DG_BL/ossi
215
		cat_choice
241
		cat_choice
216
		#  for dnsmasq (rehabited domain names)
242
		#  for dnsmasq (rehabited domain names)
217
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
243
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
218
		then
244
		then
219
			for i in `cat $DIR_DG/exceptionsitelist`
245
			for i in `cat $DIR_DG/exceptionsitelist`
220
			do
246
			do
221
				$SED "/$i/d" $DIR_DNS_BL/*
247
				$SED "/$i/d" $DIR_DNS_BL/*
222
			done
248
			done
223
		fi
249
		fi
224
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
250
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
225
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
251
		$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
226
		cp -f $OSSI_DOMAINS_WL $DIR_DNS_WL/ossi.conf
252
		cp -f $OSSI_DOMAINS_WL $DIR_DNS_WL/ossi.conf
227
		$SED "s?.*?server=/&/$DNS1?g" $DIR_DNS_WL/ossi.conf
253
		$SED "s?.*?server=/&/$DNS1?g" $DIR_DNS_WL/ossi.conf
228
		ln -s $DIR_DNS_WL/ossi.conf $DIR_DNS_WL_ENABLED/ossi
254
		ln -s $DIR_DNS_WL/ossi.conf $DIR_DNS_WL_ENABLED/ossi
229
		
255
		
230
		/usr/bin/systemctl restart dnsmasq-blacklist
256
		/usr/bin/systemctl restart dnsmasq-blacklist
231
		/usr/bin/systemctl restart dnsmasq-whitelist
257
		/usr/bin/systemctl restart dnsmasq-whitelist
232
		/usr/local/bin/alcasar-iptables.sh
258
		/usr/local/bin/alcasar-iptables.sh
233
		;;
259
		;;
234
	*)
260
	*)
235
		echo "Argument inconnu :$1";
261
		echo "Argument inconnu :$1";
236
		echo "$usage"
262
		echo "$usage"
237
		exit 1
263
		exit 1
238
		;;
264
		;;
239
esac
265
esac
240
 
266
 
241
 
267