672 |
richard |
1 |
#/bin/bash
|
1015 |
richard |
2 |
|
63 |
franck |
3 |
# $Id: alcasar-bl.sh 1071 2013-04-14 19:58:17Z richard $
|
|
|
4 |
|
672 |
richard |
5 |
# alcasar-bl.sh
|
|
|
6 |
# by Franck BOUIJOUX and Richard REY
|
|
|
7 |
# This script is distributed under the Gnu General Public License (GPL)
|
1 |
root |
8 |
|
672 |
richard |
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)
|
|
|
11 |
|
878 |
richard |
12 |
DIR_CONF="/usr/local/etc"
|
|
|
13 |
CONF_FILE="$DIR_CONF/alcasar.conf"
|
854 |
richard |
14 |
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
|
|
|
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
|
308 |
richard |
17 |
DIR_tmp="/tmp/blacklists"
|
1015 |
richard |
18 |
FILE_tmp="/tmp/filesfilter.txt"
|
|
|
19 |
FILE_ip_tmp="/tmp/filesipfilter.txt"
|
316 |
richard |
20 |
DIR_DG="/etc/dansguardian/lists"
|
|
|
21 |
DIR_DG_BL="$DIR_DG/blacklists"
|
1015 |
richard |
22 |
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories" # list of names of the BL categories
|
|
|
23 |
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories" #' ' WL '
|
|
|
24 |
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 |
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)
|
|
|
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)
|
1071 |
richard |
33 |
DNSMASQ_BL_CONF="/etc/dnsmasq-blackhole.conf" # conf file of dnsmasq-blackhole
|
878 |
richard |
34 |
BL_SERVER="dsi.ut-capitole.fr"
|
1 |
root |
35 |
SED="/bin/sed -i"
|
637 |
richard |
36 |
|
1042 |
richard |
37 |
# enable/disable the BL & WL categories
|
308 |
richard |
38 |
function cat_choice (){
|
1015 |
richard |
39 |
rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED # cleaning for dnsmasq and iptables
|
|
|
40 |
$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
|
1042 |
richard |
41 |
$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
|
1015 |
richard |
42 |
mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED
|
|
|
43 |
# process the file $BL_CATEGORIES with the choice of categories
|
|
|
44 |
for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
|
313 |
richard |
45 |
do
|
1015 |
richard |
46 |
$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
|
314 |
richard |
47 |
$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
|
1057 |
richard |
48 |
ln -s $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
|
1015 |
richard |
49 |
ln -s $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
|
|
|
50 |
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 ;-)
|
648 |
richard |
51 |
echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
|
313 |
richard |
52 |
done
|
878 |
richard |
53 |
sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
|
313 |
richard |
54 |
mv $FILE_tmp $BL_CATEGORIES
|
1042 |
richard |
55 |
# process the file $WL_CATEGORIES with the choice of categories
|
|
|
56 |
for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
|
|
|
57 |
do
|
|
|
58 |
$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES
|
|
|
59 |
$SED "1i\/etc\/dansguardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
|
|
|
60 |
ln -s $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
|
|
|
61 |
done
|
|
|
62 |
sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
|
|
|
63 |
mv $FILE_tmp $WL_CATEGORIES
|
308 |
richard |
64 |
}
|
615 |
richard |
65 |
function bl_enable (){
|
|
|
66 |
$SED "s/^reportinglevel =.*/reportinglevel = 3/g" /etc/dansguardian/dansguardian.conf
|
1060 |
richard |
67 |
if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
|
615 |
richard |
68 |
then
|
|
|
69 |
service dansguardian restart
|
|
|
70 |
service dnsmasq restart
|
|
|
71 |
/usr/local/bin/alcasar-iptables.sh
|
|
|
72 |
fi
|
|
|
73 |
}
|
|
|
74 |
function bl_disable (){
|
1015 |
richard |
75 |
rm -rf $DIR_DNS_BL_ENABLED/*
|
615 |
richard |
76 |
$SED "s/^reportinglevel =.*/reportinglevel = -1/g" /etc/dansguardian/dansguardian.conf
|
1071 |
richard |
77 |
$SED "/google/d" $DNSMASQ_BL_CONF # remove nosslsearch server
|
|
|
78 |
$SED "s?^[^#]?#&?g" $DIR_DG/urlregexplist # remove safe searching
|
|
|
79 |
$SED "s/^\*ip$/#*ip/g" $DIR_DG/bannedsitelist # remove pureip browsing
|
1060 |
richard |
80 |
if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
|
615 |
richard |
81 |
then
|
|
|
82 |
service dansguardian restart
|
634 |
richard |
83 |
service dnsmasq restart
|
615 |
richard |
84 |
/usr/local/bin/alcasar-iptables.sh
|
|
|
85 |
fi
|
|
|
86 |
}
|
1059 |
richard |
87 |
usage="Usage: alcasar-bl.sh { -on or --on } | { -off or --off } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload }"
|
1 |
root |
88 |
nb_args=$#
|
|
|
89 |
args=$1
|
|
|
90 |
if [ $nb_args -eq 0 ]
|
|
|
91 |
then
|
634 |
richard |
92 |
DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2` # DNS and URLs filter (on/off)
|
|
|
93 |
DNS_FILTERING=${DNS_FILTERING:=off}
|
|
|
94 |
echo "Set BlackList Filtering to $DNS_FILTERING"
|
|
|
95 |
if [ $DNS_FILTERING = on ]; then
|
|
|
96 |
cat_choice
|
|
|
97 |
bl_enable
|
|
|
98 |
else
|
|
|
99 |
bl_disable
|
|
|
100 |
fi
|
|
|
101 |
exit 0
|
1 |
root |
102 |
fi
|
|
|
103 |
case $args in
|
|
|
104 |
-\? | -h* | --h*)
|
|
|
105 |
echo "$usage"
|
|
|
106 |
exit 0
|
|
|
107 |
;;
|
1015 |
richard |
108 |
# enable the filtering
|
386 |
franck |
109 |
-on | --on)
|
311 |
richard |
110 |
cat_choice
|
612 |
richard |
111 |
$SED "s?^DNS_FILTERING.*?DNS_FILTERING=on?g" $CONF_FILE
|
615 |
richard |
112 |
bl_enable
|
1 |
root |
113 |
;;
|
1015 |
richard |
114 |
# disable the filtering
|
412 |
richard |
115 |
-off | --off)
|
612 |
richard |
116 |
$SED "s?^DNS_FILTERING.*?DNS_FILTERING=off?g" $CONF_FILE
|
615 |
richard |
117 |
bl_disable
|
|
|
118 |
;;
|
1015 |
richard |
119 |
# Retrieve Toulouse BL
|
386 |
franck |
120 |
-download | --download)
|
1 |
root |
121 |
rm -rf /tmp/con_ok.html
|
|
|
122 |
`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
|
|
|
123 |
if [ ! -e /tmp/con_ok.html ]
|
|
|
124 |
then
|
|
|
125 |
echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
|
|
|
126 |
else
|
878 |
richard |
127 |
rm -rf /tmp/con_ok.html $DIR_tmp
|
|
|
128 |
mkdir $DIR_tmp
|
885 |
richard |
129 |
wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
|
878 |
richard |
130 |
md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
|
885 |
richard |
131 |
chown -R apache:apache $DIR_tmp
|
878 |
richard |
132 |
fi
|
|
|
133 |
;;
|
1015 |
richard |
134 |
# Adapt Toulouse BL to our structure (dnsmasq + DG)
|
878 |
richard |
135 |
-adapt | --adapt)
|
1015 |
richard |
136 |
echo -n "Toulouse BlackList migration process. Please wait : "
|
878 |
richard |
137 |
if [ -f $DIR_tmp/blacklists.tar.gz ]
|
|
|
138 |
then
|
|
|
139 |
[ -d $DIR_DG_BL/ossi ] && mv -f $DIR_DG_BL/ossi $DIR_tmp
|
|
|
140 |
rm -rf $DIR_DG_BL
|
|
|
141 |
mkdir $DIR_DG_BL
|
|
|
142 |
tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
|
|
|
143 |
[ -d $DIR_tmp/ossi ] && mv -f $DIR_tmp/ossi $DIR_DG_BL/
|
|
|
144 |
rm -rf $DIR_tmp
|
685 |
richard |
145 |
fi
|
1042 |
richard |
146 |
rm -f $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
|
1015 |
richard |
147 |
rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
|
1042 |
richard |
148 |
touch $BL_CATEGORIES $WL_CATEGORIES $WL_CATEGORIES_ENABLED
|
1015 |
richard |
149 |
mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL
|
1042 |
richard |
150 |
chown -R dansguardian:apache $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
|
|
|
151 |
chmod -R g+w $DIR_DG $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED
|
1015 |
richard |
152 |
find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
|
|
|
153 |
$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
|
1042 |
richard |
154 |
for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
|
878 |
richard |
155 |
do
|
1015 |
richard |
156 |
categorie=`echo $dir_categorie|cut -d "/" -f6`
|
|
|
157 |
categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
|
|
|
158 |
if [ "$categorie_type" == "white" ]
|
878 |
richard |
159 |
then
|
1015 |
richard |
160 |
echo "$dir_categorie" >> $WL_CATEGORIES
|
1042 |
richard |
161 |
echo `basename $dir_categorie` >> $WL_CATEGORIES_ENABLED # by default all WL are enabled
|
878 |
richard |
162 |
else
|
1015 |
richard |
163 |
echo "$dir_categorie" >> $BL_CATEGORIES
|
878 |
richard |
164 |
fi
|
|
|
165 |
done
|
|
|
166 |
rm -f $FILE_tmp
|
1057 |
richard |
167 |
# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
|
|
|
168 |
for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
|
|
|
169 |
do
|
|
|
170 |
ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
|
|
|
171 |
if [ $ok != "1" ]
|
|
|
172 |
then
|
|
|
173 |
$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
|
|
|
174 |
fi
|
|
|
175 |
done
|
1015 |
richard |
176 |
# Creation of DNSMASQ BL and WL
|
|
|
177 |
for LIST in $BL_CATEGORIES $WL_CATEGORIES # for each list (bl and wl)
|
854 |
richard |
178 |
do
|
1015 |
richard |
179 |
for PATH_FILE in `cat $LIST` # for each category
|
|
|
180 |
do
|
|
|
181 |
DOMAINE=`basename $PATH_FILE`
|
|
|
182 |
echo -n "$DOMAINE, "
|
|
|
183 |
if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
|
|
|
184 |
then
|
|
|
185 |
touch $PATH_FILE/urls
|
|
|
186 |
chown dansguardian:apache $PATH_FILE/urls
|
|
|
187 |
fi
|
|
|
188 |
$SED "s/\.\{2,10\}/\./g" $PATH_FILE/domains $PATH_FILE/urls # correct some syntax errors
|
|
|
189 |
# retrieve the ip addresses for iptables
|
|
|
190 |
egrep "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_ip_tmp
|
|
|
191 |
# for dnsmask, remove IP addesses, accented characters and commented lines.
|
|
|
192 |
egrep -v "([0-9]{1,3}\.){3}[0-9]{1,3}" $PATH_FILE/domains > $FILE_tmp
|
|
|
193 |
$SED "/[äâëêïîöôüû]/d" $FILE_tmp
|
|
|
194 |
$SED "/^#.*/d" $FILE_tmp
|
|
|
195 |
# adapt to the dnsmasq syntax
|
|
|
196 |
$SED "s?.*?address=/&/$PRIVATE_IP?g" $FILE_tmp
|
|
|
197 |
if [ "$LIST" == "$BL_CATEGORIES" ]
|
|
|
198 |
then
|
|
|
199 |
mv $FILE_tmp $DIR_DNS_BL/$DOMAINE.conf
|
|
|
200 |
mv $FILE_ip_tmp $DIR_IP_BL/$DOMAINE
|
|
|
201 |
else
|
|
|
202 |
mv $FILE_tmp $DIR_DNS_WL/$DOMAINE.conf
|
|
|
203 |
fi
|
|
|
204 |
done
|
854 |
richard |
205 |
done
|
1015 |
richard |
206 |
rm -f $FILE_tmp $FILE_ip_tmp
|
854 |
richard |
207 |
echo
|
654 |
richard |
208 |
;;
|
1015 |
richard |
209 |
# reload when categories are changed
|
386 |
franck |
210 |
-reload | --reload)
|
1015 |
richard |
211 |
# for DG
|
316 |
richard |
212 |
chown -R dansguardian:apache $DIR_DG_BL/ossi
|
|
|
213 |
chmod -R g+w $DIR_DG_BL/ossi
|
311 |
richard |
214 |
cat_choice
|
1042 |
richard |
215 |
# for dnsmasq (rehabited domain names)
|
316 |
richard |
216 |
if [ `wc -w $DIR_DG/exceptionsitelist|cut -d " " -f1` != "0" ]
|
311 |
richard |
217 |
then
|
316 |
richard |
218 |
for i in `cat $DIR_DG/exceptionsitelist`
|
311 |
richard |
219 |
do
|
1015 |
richard |
220 |
$SED "/$i/d" $DIR_DNS_BL/*
|
311 |
richard |
221 |
done
|
|
|
222 |
fi
|
1015 |
richard |
223 |
cp -f $DIR_DG_BL/ossi/domains $DIR_DNS_BL/ossi.conf
|
|
|
224 |
$SED "s?.*?address=/&/$PRIVATE_IP?g" $DIR_DNS_BL/ossi.conf
|
1042 |
richard |
225 |
cp -f $DIR_DG_BL/ossi/domains_wl $DIR_DNS_WL/ossi.conf
|
634 |
richard |
226 |
DNS_FILTERING=`grep DNS_FILTERING $CONF_FILE|cut -d"=" -f2` # DNS and URLs filter (on/off)
|
|
|
227 |
DNS_FILTERING=${DNS_FILTERING:=off}
|
|
|
228 |
if [ $DNS_FILTERING = on ]; then
|
|
|
229 |
bl_enable
|
|
|
230 |
else
|
|
|
231 |
bl_disable
|
|
|
232 |
fi
|
503 |
richard |
233 |
;;
|
1 |
root |
234 |
*)
|
|
|
235 |
echo "Argument inconnu :$1";
|
|
|
236 |
echo "$usage"
|
|
|
237 |
exit 1
|
|
|
238 |
;;
|
|
|
239 |
esac
|
|
|
240 |
|