Subversion Repositories ALCASAR

Rev

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

Rev 654 Rev 672
1
#/bin/sh
1
#/bin/bash
2
# $Id: alcasar-bl.sh 654 2011-06-27 19:30:28Z richard $
2
# $Id: alcasar-bl.sh 672 2011-07-08 15:34:22Z richard $
3
 
3
 
-
 
4
# alcasar-bl.sh
-
 
5
# by Franck BOUIJOUX and Richard REY
-
 
6
# This script is distributed under the Gnu General Public License (GPL)
-
 
7
 
4
# Script de gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via dansguardian)
8
# Gestion de la BL pour le filtrage de domaine (via dnsmasq) et d'URL (via Dansguardian)
5
# By 3abtux & rexy
9
# Manage the BL for DnsBlackHole (dnsmasq) and URL filtering (Dansguardian)
6
 
10
 
7
CONF_FILE="/usr/local/etc/alcasar.conf"
11
CONF_FILE="/usr/local/etc/alcasar.conf"
8
DIR_tmp="/tmp/blacklists"
12
DIR_tmp="/tmp/blacklists"
9
FILE_tmp="/tmp/fileFilter.txt"
13
FILE_tmp="/tmp/fileFilter.txt"
10
DIR_DG="/etc/dansguardian/lists"
14
DIR_DG="/etc/dansguardian/lists"
11
DIR_DG_BL="$DIR_DG/blacklists"
15
DIR_DG_BL="$DIR_DG/blacklists"
12
BL_CATEGORIES="/usr/local/etc/alcasar-bl-categories"
16
BL_CATEGORIES="/usr/local/etc/alcasar-bl-categories"
13
BL_CATEGORIES_ENABLED="/usr/local/etc/alcasar-bl-categories-enabled"
17
BL_CATEGORIES_ENABLED="/usr/local/etc/alcasar-bl-categories-enabled"
14
DIR_DNS_FILTER_AVAILABLE="/usr/local/etc/alcasar-dnsfilter-available"
18
DIR_DNS_FILTER_AVAILABLE="/usr/local/etc/alcasar-dnsfilter-available"
15
DIR_DNS_FILTER_ENABLED="/usr/local/etc/alcasar-dnsfilter-enabled"
19
DIR_DNS_FILTER_ENABLED="/usr/local/etc/alcasar-dnsfilter-enabled"
16
IP_RETOUR="20.0.0.1"
20
IP_RETOUR="20.0.0.1"
17
BL_SERVER="cri.univ-tlse1.fr"
21
BL_SERVER="cri.univ-tlse1.fr"
18
SED="/bin/sed -i"
22
SED="/bin/sed -i"
19
 
23
 
20
# Récupération de l'archive de la BL Toulouse
24
# Récupération de l'archive de la BL Toulouse
21
function transfert () {
25
function transfert () {
22
	mkdir -p $DIR_tmp
26
	mkdir -p $DIR_tmp
23
	cd $DIR_tmp
27
	cd $DIR_tmp
24
	wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
28
	wget http://$BL_SERVER/blacklists/download/blacklists.tar.gz
25
}
29
}
26
# Décompression de la BL (en conservant la WL)
30
# Décompression de la BL (en conservant la WL)
27
function install () {
31
function install () {
28
	[ -d $DIR_DG ] || mkdir -p $DIR_DG
32
	[ -d $DIR_DG ] || mkdir -p $DIR_DG
29
	[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
33
	[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
30
	[ -d $DIR_DG_BL/ip ] && mv -f $DIR_DG_BL/ip $DIR_tmp
34
	[ -d $DIR_DG_BL/ip ] && mv -f $DIR_DG_BL/ip $DIR_tmp
31
	tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
35
	tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
32
	[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
36
	[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
33
	[ -d $DIR_tmp/ip ] && mv -f $DIR_tmp/ip $DIR_DG_BL/
37
	[ -d $DIR_tmp/ip ] && mv -f $DIR_tmp/ip $DIR_DG_BL/
34
	cd /root
38
	cd /root
35
	rm -rf $DIR_tmp
39
	rm -rf $DIR_tmp
36
}
40
}
37
# Adaptation de la BL Toulouse à la structure Dnsmasq
41
# Adaptation de la BL Toulouse à la structure Dnsmasq
38
function adapt () {
42
function adapt () {
39
	# On récupère le nom des répertoire (catégories)
43
	# On récupère le nom des répertoire (catégories)
40
	find $DIR_DG_BL/ -type f -name domains > $BL_CATEGORIES
44
	find $DIR_DG_BL/ -type f -name domains > $BL_CATEGORIES
41
	# On supprime le suffice "/domains"
45
	# On supprime le suffice "/domains"
42
	$SED "s?\/domains??g" $BL_CATEGORIES
46
	$SED "s?\/domains??g" $BL_CATEGORIES
43
	rm -f $DIR_DNS_FILTER_AVAILABLE/*
47
	rm -f $DIR_DNS_FILTER_AVAILABLE/*
44
	echo -n "Toulouse BlackList migration process. Please wait : "
48
	echo -n "Toulouse BlackList migration process. Please wait : "
45
	# pour chaque catégorie
49
	# pour chaque catégorie
46
	for PATH_FILE in `cat $BL_CATEGORIES`
50
	for PATH_FILE in `cat $BL_CATEGORIES`
47
	do
51
	do
48
		echo -n "."
52
		echo -n "."
49
		# on crée le fichier 'urls' s'il n'existe pas
53
		# on crée le fichier 'urls' s'il n'existe pas
50
		if [ ! -f $PATH_FILE/urls ]
54
		if [ ! -f $PATH_FILE/urls ]
51
		then
55
		then
52
			touch $PATH_FILE/urls
56
			touch $PATH_FILE/urls
53
			chown dansguardian:apache $PATH_FILE/urls
57
			chown dansguardian:apache $PATH_FILE/urls
54
		fi
58
		fi
55
		# suppression des @IP, des lignes commentées et des caractères bizarres comme les ô et û ö ü
59
		# suppression des @IP, des lignes commentées et des caractères bizarres comme les ô et û ö ü
56
		egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > /tmp/dnsmasq-bl.tmp
60
		egrep  -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > /tmp/dnsmasq-bl.tmp
57
		$SED "/[äâëêïîöôüû]/d" /tmp/dnsmasq-bl.tmp
61
		$SED "/[äâëêïîöôüû]/d" /tmp/dnsmasq-bl.tmp
58
		$SED "/^#.*/d" /tmp/dnsmasq-bl.tmp
62
		$SED "/^#.*/d" /tmp/dnsmasq-bl.tmp
59
		# Mise en forme dnsmasq
63
		# Mise en forme dnsmasq
60
		$SED "s?.*?address=/&/$IP_RETOUR?g" /tmp/dnsmasq-bl.tmp
64
		$SED "s?.*?address=/&/$IP_RETOUR?g" /tmp/dnsmasq-bl.tmp
61
        	DOMAINE=`basename $PATH_FILE`
65
        	DOMAINE=`basename $PATH_FILE`
62
		mv /tmp/dnsmasq-bl.tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
66
		mv /tmp/dnsmasq-bl.tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
63
	done
67
	done
64
	echo
68
	echo
65
}
69
}
66
# Permet d'activer/désactiver les catégories de la BL
70
# Permet d'activer/désactiver les catégories de la BL
67
function cat_choice (){
71
function cat_choice (){
68
	# un peu de ménage
72
	# un peu de ménage
69
	rm -rf $DIR_DNS_FILTER_ENABLED/*
73
	rm -rf $DIR_DNS_FILTER_ENABLED/*
70
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
74
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
71
	# on adapte le fichier $BL_CATEGORIES au choix de catégorie 
75
	# on adapte le fichier $BL_CATEGORIES au choix de catégorie 
72
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
76
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
73
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` # on affecte les catégories à dansguardian et dnsmasq
77
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` # on affecte les catégories à dansguardian et dnsmasq
74
	do
78
	do
75
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
79
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
76
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
80
		$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
77
		ln -s $DIR_DNS_FILTER_AVAILABLE/$ENABLE_CATEGORIE.conf $DIR_DNS_FILTER_ENABLED/$ENABLE_CATEGORIE
81
		ln -s $DIR_DNS_FILTER_AVAILABLE/$ENABLE_CATEGORIE.conf $DIR_DNS_FILTER_ENABLED/$ENABLE_CATEGORIE
78
		# echo ".Include<$DIR_DANSGUARDIAN/blacklists/$ENABLE_CATEGORIES/domains>" >> $DIR_DANSGUARDIAN/bannedsitelist  # dansguardian ne s'occupe plus des noms de domaine
82
		# echo ".Include<$DIR_DANSGUARDIAN/blacklists/$ENABLE_CATEGORIES/domains>" >> $DIR_DANSGUARDIAN/bannedsitelist  # dansguardian ne s'occupe plus des noms de domaine
79
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
83
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
80
	done
84
	done
81
	sort -k2n $BL_CATEGORIES > $FILE_tmp
85
	sort -k2n $BL_CATEGORIES > $FILE_tmp
82
	mv $FILE_tmp $BL_CATEGORIES
86
	mv $FILE_tmp $BL_CATEGORIES
83
}
87
}
84
function bl_enable (){
88
function bl_enable (){
85
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
89
	$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
86
	$SED "s?^#\"?\"?g" $DIR_DG/urlregexplist # Enable 'safesearch'
90
	$SED "s?^#\"?\"?g" $DIR_DG/urlregexplist # Enable 'safesearch'
87
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
91
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
88
	then
92
	then
89
		service dansguardian restart
93
		service dansguardian restart
90
		service dnsmasq restart
94
		service dnsmasq restart
91
		/usr/local/bin/alcasar-iptables.sh
95
		/usr/local/bin/alcasar-iptables.sh
92
	fi
96
	fi
93
}
97
}
94
function bl_disable (){
98
function bl_disable (){
95
	rm -rf $DIR_DNS_FILTER_ENABLED/*
99
	rm -rf $DIR_DNS_FILTER_ENABLED/*
96
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
100
	$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
97
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist # Disable 'safesearch'
101
	$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist # Disable 'safesearch'
98
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
102
	if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
99
	then
103
	then
100
		service dansguardian restart
104
		service dansguardian restart
101
		service dnsmasq restart
105
		service dnsmasq restart
102
		/usr/local/bin/alcasar-iptables.sh
106
		/usr/local/bin/alcasar-iptables.sh
103
	fi
107
	fi
104
}
108
}
105
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload or --reload }"
109
usage="Usage: alcasar-bl.sh {-on or --on} | { -off or --off } | { -download or --download } | { -reload or --reload }"
106
nb_args=$#
110
nb_args=$#
107
args=$1
111
args=$1
108
if [ $nb_args -eq 0 ]
112
if [ $nb_args -eq 0 ]
109
then
113
then
110
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
114
	DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
111
	DNS_FILTERING=${DNS_FILTERING:=off}
115
	DNS_FILTERING=${DNS_FILTERING:=off}
112
	echo "Set BlackList Filtering to $DNS_FILTERING"
116
	echo "Set BlackList Filtering to $DNS_FILTERING"
113
	if [ $DNS_FILTERING = on ]; then
117
	if [ $DNS_FILTERING = on ]; then
114
		cat_choice
118
		cat_choice
115
		bl_enable
119
		bl_enable
116
	else
120
	else
117
		bl_disable
121
		bl_disable
118
	fi
122
	fi
119
	exit 0
123
	exit 0
120
fi
124
fi
121
case $args in
125
case $args in
122
	-\? | -h* | --h*)
126
	-\? | -h* | --h*)
123
		echo "$usage"
127
		echo "$usage"
124
		exit 0
128
		exit 0
125
		;;
129
		;;
126
	# activation du filtrage
130
	# activation du filtrage
127
	-on | --on)	
131
	-on | --on)	
128
		cat_choice
132
		cat_choice
129
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
133
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
130
		bl_enable
134
		bl_enable
131
		;;
135
		;;
132
	# désactivation du filtrage
136
	# désactivation du filtrage
133
	-off | --off)
137
	-off | --off)
134
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
138
		$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
135
		bl_disable
139
		bl_disable
136
		;;
140
		;;
137
	# Mise a jour de la blacklist 'Toulouse' et adaptation à dansguardian et dnsmasq
141
	# Mise a jour de la blacklist 'Toulouse' et adaptation à dansguardian et dnsmasq
138
	-download | --download)
142
	-download | --download)
139
		rm -rf /tmp/con_ok.html
143
		rm -rf /tmp/con_ok.html
140
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
144
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
141
		if [ ! -e /tmp/con_ok.html ]
145
		if [ ! -e /tmp/con_ok.html ]
142
		then
146
		then
143
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
147
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
144
		else 
148
		else 
145
			transfert
149
			transfert
146
			install
150
			install
147
			chown -R dansguardian:apache $DIR_DG
151
			chown -R dansguardian:apache $DIR_DG
148
			chmod -R g+w $DIR_DG
152
			chmod -R g+w $DIR_DG
149
			DATE=`date '+%d %B %Y - %Hh%M'`
153
			DATE=`date '+%d %B %Y - %Hh%M'`
150
			echo "Univ-tlse du $DATE " > /var/www/html/VERSION-BL
154
			echo "Univ-tlse du $DATE " > /var/www/html/VERSION-BL
151
			rm -rf /tmp/con_ok.html
155
			rm -rf /tmp/con_ok.html
152
		fi
156
		fi
153
		;;		
157
		;;		
154
	# Adaptation de laBL à notre structure 'dnsmasq + DG)
158
	# Adaptation de laBL à notre structure 'dnsmasq + DG)
155
	-adapt | --adapt)
159
	-adapt | --adapt)
156
		adapt
160
		adapt
157
		cat_choice
161
		cat_choice
158
		;;
162
		;;
159
	# regénération suite à modification (choix catégories ou BL secondaire)
163
	# regénération suite à modification (choix catégories ou BL secondaire)
160
	-reload | --reload)
164
	-reload | --reload)
161
		adapt
165
		adapt
162
		# pour Dansguardian
166
		# pour Dansguardian
163
		chown -R dansguardian:apache $DIR_DG_BL/ossi
167
		chown -R dansguardian:apache $DIR_DG_BL/ossi
164
		chmod -R g+w $DIR_DG_BL/ossi
168
		chmod -R g+w $DIR_DG_BL/ossi
165
		cat_choice
169
		cat_choice
166
		# pour dnsmasq (noms de domaine réhabilités)
170
		# pour dnsmasq (noms de domaine réhabilités)
167
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
171
		if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
168
		then
172
		then
169
			for i in `cat $DIR_DG/exceptionsitelist`
173
			for i in `cat $DIR_DG/exceptionsitelist`
170
			do
174
			do
171
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
175
				$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
172
			done
176
			done
173
		fi
177
		fi
174
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
178
		cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
175
		$SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
179
		$SED "s?.*?address=/&/$IP_RETOUR?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
176
		IP_PURE=`grep '^ip' $BL_CATEGORIES_ENABLED|wc -l`
180
		IP_PURE=`grep '^ip' $BL_CATEGORIES_ENABLED|wc -l`
177
		if [ $IP_PURE -eq "1" ]; then	# filtrage des url sans nom de domaine
181
		if [ $IP_PURE -eq "1" ]; then	# filtrage des url sans nom de domaine
178
			$SED "s/^\#\*ip$/*ip/g" $DIR_DG/bannedsitelist
182
			$SED "s/^\#\*ip$/*ip/g" $DIR_DG/bannedsitelist
179
		else
183
		else
180
			$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist
184
			$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist
181
		fi
185
		fi
182
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
186
		DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2`		# DNS and URLs filter (on/off)
183
		DNS_FILTERING=${DNS_FILTERING:=off}
187
		DNS_FILTERING=${DNS_FILTERING:=off}
184
		if [ $DNS_FILTERING = on ]; then
188
		if [ $DNS_FILTERING = on ]; then
185
			bl_enable
189
			bl_enable
186
		else
190
		else
187
			bl_disable
191
			bl_disable
188
		fi
192
		fi
189
		;;
193
		;;
190
	*)
194
	*)
191
		echo "Argument inconnu :$1";
195
		echo "Argument inconnu :$1";
192
		echo "$usage"
196
		echo "$usage"
193
		exit 1
197
		exit 1
194
		;;
198
		;;
195
esac
199
esac
196
 
200
 
197
 
201