Line 1... |
Line 1... |
1 |
#/bin/bash
|
1 |
#/bin/bash
|
- |
|
2 |
|
2 |
# $Id: alcasar-bl.sh 958 2012-07-19 09:01:30Z franck $
|
3 |
# $Id: alcasar-bl.sh 1015 2013-01-27 22:43:29Z richard $
|
3 |
|
4 |
|
4 |
# alcasar-bl.sh
|
5 |
# alcasar-bl.sh
|
5 |
# by Franck BOUIJOUX and Richard REY
|
6 |
# by Franck BOUIJOUX and Richard REY
|
6 |
# This script is distributed under the Gnu General Public License (GPL)
|
7 |
# This script is distributed under the Gnu General Public License (GPL)
|
7 |
|
8 |
|
Line 12... |
Line 13... |
12 |
CONF_FILE="$DIR_CONF/alcasar.conf"
|
13 |
CONF_FILE="$DIR_CONF/alcasar.conf"
|
13 |
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
|
14 |
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
|
14 |
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
|
15 |
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
|
15 |
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
|
16 |
DIR_tmp="/tmp/blacklists"
|
17 |
DIR_tmp="/tmp/blacklists"
|
17 |
FILE_tmp="/tmp/fileFilter.txt"
|
18 |
FILE_tmp="/tmp/filesfilter.txt"
|
- |
|
19 |
FILE_ip_tmp="/tmp/filesipfilter.txt"
|
18 |
DIR_DG="/etc/dansguardian/lists"
|
20 |
DIR_DG="/etc/dansguardian/lists"
|
19 |
DIR_DG_BL="$DIR_DG/blacklists"
|
21 |
DIR_DG_BL="$DIR_DG/blacklists"
|
20 |
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"
|
22 |
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories" # list of names of the BL categories
|
21 |
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"
|
23 |
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories" #' ' WL '
|
22 |
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"
|
24 |
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled" # ' ' BL enabled categories
|
23 |
DIR_DNS_FILTER_AVAILABLE="$DIR_CONF/alcasar-dnsfilter-available"
|
25 |
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled" # ' ' WL enabled categories
|
- |
|
26 |
DIR_SHARE="/usr/local/share"
|
- |
|
27 |
DIR_DNS_BL="$DIR_SHARE/dnsmasq-bl" # all the BL in the DNSMASQ format
|
- |
|
28 |
DIR_DNS_WL="$DIR_SHARE/dnsmasq-wl" # all the WL ' ' '
|
- |
|
29 |
DIR_IP_BL="$DIR_SHARE/iptables-bl" # all the IP addresses of the BL
|
- |
|
30 |
DIR_DNS_BL_ENABLED="$DIR_SHARE/dnsmasq-bl-enabled" # symbolic link to the dnsmasq BL (only enabled categories)
|
24 |
DIR_DNS_FILTER_ENABLED="$DIR_CONF/alcasar-dnsfilter-enabled"
|
31 |
DIR_DNS_WL_ENABLED="$DIR_SHARE/dnsmasq-wl-enabled" # ' ' ' WL ' ' '
|
- |
|
32 |
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled" # ' ' ip BL (only enabled categories)
|
25 |
BL_SERVER="dsi.ut-capitole.fr"
|
33 |
BL_SERVER="dsi.ut-capitole.fr"
|
26 |
SED="/bin/sed -i"
|
34 |
SED="/bin/sed -i"
|
27 |
|
35 |
|
28 |
# Permet d'activer/désactiver les catégories de la BL
|
36 |
# enable/disable the BL categories
|
29 |
function cat_choice (){
|
37 |
function cat_choice (){
|
30 |
# un peu de ménage
|
- |
|
31 |
rm -rf $DIR_DNS_FILTER_ENABLED/*
|
38 |
rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED # cleaning for dnsmasq and iptables
|
32 |
$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist
|
39 |
$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
|
33 |
# on adapte le fichier $BL_CATEGORIES au choix de catégorie
|
40 |
$SED "s?^[^#]?#&?g" $BL_CATEGORIES # cleaning categories file (comment all lines)
|
- |
|
41 |
mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED
|
34 |
$SED "s?^[^#]?#&?g" $BL_CATEGORIES # on commente ce qui ne l'est pas
|
42 |
# process the file $BL_CATEGORIES with the choice of categories
|
35 |
for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED` # on affecte les catégories à dansguardian et dnsmasq
|
43 |
for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
|
36 |
do
|
44 |
do
|
37 |
$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
|
45 |
$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
|
38 |
$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
|
46 |
$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
|
39 |
ln -s $DIR_DNS_FILTER_AVAILABLE/$ENABLE_CATEGORIE.conf $DIR_DNS_FILTER_ENABLED/$ENABLE_CATEGORIE
|
47 |
ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
|
- |
|
48 |
ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
|
40 |
echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist # dansguardian s'occupe du contournement par proxy http ;-)
|
49 |
echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist # we let DG filters domain in order to prevent bypass by proxy http vpn ;-)
|
41 |
echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
|
50 |
echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
|
42 |
done
|
51 |
done
|
43 |
sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
|
52 |
sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
|
44 |
mv $FILE_tmp $BL_CATEGORIES
|
53 |
mv $FILE_tmp $BL_CATEGORIES
|
45 |
}
|
54 |
}
|
Line 51... |
Line 60... |
51 |
service dnsmasq restart
|
60 |
service dnsmasq restart
|
52 |
/usr/local/bin/alcasar-iptables.sh
|
61 |
/usr/local/bin/alcasar-iptables.sh
|
53 |
fi
|
62 |
fi
|
54 |
}
|
63 |
}
|
55 |
function bl_disable (){
|
64 |
function bl_disable (){
|
56 |
rm -rf $DIR_DNS_FILTER_ENABLED/*
|
65 |
rm -rf $DIR_DNS_BL_ENABLED/*
|
57 |
$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
|
66 |
$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
|
58 |
if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
|
67 |
if [ "$PARENT_SCRIPT" != "/usr/local/bin/alcasar-conf.sh" ] # don't launch on install stage
|
59 |
then
|
68 |
then
|
60 |
service dansguardian restart
|
69 |
service dansguardian restart
|
61 |
service dnsmasq restart
|
70 |
service dnsmasq restart
|
Line 81... |
Line 90... |
81 |
case $args in
|
90 |
case $args in
|
82 |
-\? | -h* | --h*)
|
91 |
-\? | -h* | --h*)
|
83 |
echo "$usage"
|
92 |
echo "$usage"
|
84 |
exit 0
|
93 |
exit 0
|
85 |
;;
|
94 |
;;
|
86 |
# activation du filtrage
|
95 |
# enable the filtering
|
87 |
-on | --on)
|
96 |
-on | --on)
|
88 |
cat_choice
|
97 |
cat_choice
|
89 |
$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
|
98 |
$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
|
90 |
bl_enable
|
99 |
bl_enable
|
91 |
;;
|
100 |
;;
|
92 |
# désactivation du filtrage
|
101 |
# disable the filtering
|
93 |
-off | --off)
|
102 |
-off | --off)
|
94 |
$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
|
103 |
$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
|
95 |
bl_disable
|
104 |
bl_disable
|
96 |
;;
|
105 |
;;
|
97 |
# Récupération de l'archive de la BL Toulouse
|
106 |
# Retrieve Toulouse BL
|
98 |
-download | --download)
|
107 |
-download | --download)
|
99 |
rm -rf /tmp/con_ok.html
|
108 |
rm -rf /tmp/con_ok.html
|
100 |
`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
|
109 |
`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
|
101 |
if [ ! -e /tmp/con_ok.html ]
|
110 |
if [ ! -e /tmp/con_ok.html ]
|
102 |
then
|
111 |
then
|
Line 107... |
Line 116... |
107 |
wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
|
116 |
wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
|
108 |
md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
|
117 |
md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
|
109 |
chown -R apache:apache $DIR_tmp
|
118 |
chown -R apache:apache $DIR_tmp
|
110 |
fi
|
119 |
fi
|
111 |
;;
|
120 |
;;
|
112 |
# Adaptation de la BL de Toulouse à notre structure (dnsmasq + DG)
|
121 |
# Adapt Toulouse BL to our structure (dnsmasq + DG)
|
113 |
-adapt | --adapt)
|
122 |
-adapt | --adapt)
|
- |
|
123 |
echo -n "Toulouse BlackList migration process. Please wait : "
|
114 |
if [ -f $DIR_tmp/blacklists.tar.gz ]
|
124 |
if [ -f $DIR_tmp/blacklists.tar.gz ]
|
115 |
then
|
125 |
then
|
116 |
[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
|
126 |
[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
|
117 |
rm -rf $DIR_DG_BL
|
127 |
rm -rf $DIR_DG_BL
|
118 |
mkdir $DIR_DG_BL
|
128 |
mkdir $DIR_DG_BL
|
Line 120... |
Line 130... |
120 |
[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
|
130 |
[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
|
121 |
rm -rf $DIR_tmp
|
131 |
rm -rf $DIR_tmp
|
122 |
chown -R dansguardian:apache $DIR_DG
|
132 |
chown -R dansguardian:apache $DIR_DG
|
123 |
chmod -R g+w $DIR_DG
|
133 |
chmod -R g+w $DIR_DG
|
124 |
fi
|
134 |
fi
|
125 |
rm -f $BL_CATEGORIES $WL_CATEGORIES $DIR_DNS_FILTER_AVAILABLE/*
|
135 |
rm -f $BL_CATEGORIES $WL_CATEGORIES
|
- |
|
136 |
rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
|
126 |
touch $BL_CATEGORIES $WL_CATEGORIES
|
137 |
touch $BL_CATEGORIES $WL_CATEGORIES
|
- |
|
138 |
mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
|
127 |
find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # On récupère le nom des répertoire (catégories)
|
139 |
find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
|
128 |
$SED "s?\/domains??g" $FILE_tmp # On supprime le suffixe "/domains"
|
140 |
$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
|
129 |
for categorie in `cat $FILE_tmp` # creation des deux fichiers de categories (BL / WL)
|
141 |
for dir_categorie in `cat $FILE_tmp` # create the blackist and the whitelist files
|
130 |
do
|
142 |
do
|
131 |
if [ -e $categorie/usage ]
|
- |
|
132 |
then
|
- |
|
133 |
is_whitelist=`grep white $categorie/usage|wc -l`
|
143 |
categorie=`echo $dir_categorie|cut -d "/" -f6`
|
134 |
else
|
- |
|
135 |
is_whitelist=0 # si le fichier 'usage' n'existe pas, on considère que la catégorie est une BL
|
144 |
categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
|
136 |
fi
|
- |
|
137 |
if [ $is_whitelist -eq "0" ]
|
145 |
if [ "$categorie_type" == "white" ]
|
138 |
then
|
146 |
then
|
139 |
echo "$categorie" >> $BL_CATEGORIES
|
147 |
echo "$dir_categorie" >> $WL_CATEGORIES
|
- |
|
148 |
echo "$dir_categorie" >> $WL_CATEGORIES_ENABLED # by default all WL are enabled
|
140 |
else
|
149 |
else
|
141 |
echo "$categorie" >> $WL_CATEGORIES
|
150 |
echo "$dir_categorie" >> $BL_CATEGORIES
|
142 |
fi
|
151 |
fi
|
143 |
done
|
152 |
done
|
144 |
rm -f $FILE_tmp
|
153 |
rm -f $FILE_tmp
|
145 |
echo -n "Toulouse BlackList migration process. Please wait : "
|
154 |
# Creation of DNSMASQ BL and WL
|
146 |
for PATH_FILE in `cat $BL_CATEGORIES` # pour chaque catégorie
|
155 |
for LIST in $BL_CATEGORIES $WL_CATEGORIES # for each list (bl and wl)
|
147 |
do
|
156 |
do
|
- |
|
157 |
for PATH_FILE in `cat $LIST` # for each category
|
- |
|
158 |
do
|
- |
|
159 |
DOMAINE=`basename $PATH_FILE`
|
148 |
echo -n "."
|
160 |
echo -n "$DOMAINE, "
|
149 |
if [ ! -f $PATH_FILE/urls ] # on crée le fichier 'urls' s'il n'existe pas
|
161 |
if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
|
150 |
then
|
162 |
then
|
151 |
touch $PATH_FILE/urls
|
163 |
touch $PATH_FILE/urls
|
152 |
chown dansguardian:apache $PATH_FILE/urls
|
164 |
chown dansguardian:apache $PATH_FILE/urls
|
153 |
fi
|
165 |
fi
|
- |
|
166 |
$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
|
- |
|
167 |
# retrieve the ip addresses for iptables
|
- |
|
168 |
egrep "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_ip_tmp
|
154 |
# suppression des @IP, de caractères acccentués et des lignes commentées
|
169 |
# for dnsmask, remove IP addesses, accented characters and commented lines.
|
155 |
egrep -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_tmp
|
170 |
egrep -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_tmp
|
156 |
$SED "/[äâëêïîöôüû]/d" $FILE_tmp
|
171 |
$SED "/[äâëêïîöôüû]/d" $FILE_tmp
|
157 |
$SED "/^#.*/d" $FILE_tmp
|
172 |
$SED "/^#.*/d" $FILE_tmp
|
- |
|
173 |
# adapt to the dnsmasq syntax
|
158 |
$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp # Mise en forme dnsmasq
|
174 |
$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
|
159 |
DOMAINE=`basename $PATH_FILE`
|
175 |
if [ "$LIST" == "$BL_CATEGORIES" ]
|
- |
|
176 |
then
|
160 |
mv $FILE_tmp $DIR_DNS_FILTER_AVAILABLE/$DOMAINE.conf
|
177 |
mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
|
- |
|
178 |
mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
|
- |
|
179 |
else
|
- |
|
180 |
mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
|
- |
|
181 |
fi
|
- |
|
182 |
done
|
161 |
done
|
183 |
done
|
- |
|
184 |
rm -f $FILE_tmp $FILE_ip_tmp
|
162 |
echo
|
185 |
echo
|
163 |
;;
|
186 |
;;
|
164 |
# regénération suite à modification du choix des catégories
|
187 |
# reload when categories are changed
|
165 |
-reload | --reload)
|
188 |
-reload | --reload)
|
166 |
# pour Dansguardian
|
189 |
# for DG
|
167 |
chown -R dansguardian:apache $DIR_DG_BL/ossi
|
190 |
chown -R dansguardian:apache $DIR_DG_BL/ossi
|
168 |
chmod -R g+w $DIR_DG_BL/ossi
|
191 |
chmod -R g+w $DIR_DG_BL/ossi
|
169 |
cat_choice
|
192 |
cat_choice
|
170 |
# pour dnsmasq (noms de domaine réhabilités)
|
193 |
# for dnsmasq (noms de domaine réhabilités)
|
171 |
if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
|
194 |
if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
|
172 |
then
|
195 |
then
|
173 |
for i in `cat $DIR_DG/exceptionsitelist`
|
196 |
for i in `cat $DIR_DG/exceptionsitelist`
|
174 |
do
|
197 |
do
|
175 |
$SED "/$i/d" $DIR_DNS_FILTER_AVAILABLE/*
|
198 |
$SED "/$i/d" $DIR_DNS_BL/*
|
176 |
done
|
199 |
done
|
177 |
fi
|
200 |
fi
|
178 |
cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_FILTER_AVAILABLE/ossi.conf
|
201 |
cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
|
179 |
$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_FILTER_AVAILABLE/ossi.conf
|
202 |
$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
|
180 |
DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2` # DNS and URLs filter (on/off)
|
203 |
DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2` # DNS and URLs filter (on/off)
|
181 |
DNS_FILTERING=${DNS_FILTERING:=off}
|
204 |
DNS_FILTERING=${DNS_FILTERING:=off}
|
182 |
if [ $DNS_FILTERING = on ]; then
|
205 |
if [ $DNS_FILTERING = on ]; then
|
183 |
bl_enable
|
206 |
bl_enable
|
184 |
else
|
207 |
else
|