]> git.ipfire.org Git - ipfire-2.x.git/blame - config/urlfilter/autoupdate.pl
Updated and patched squidGuard
[ipfire-2.x.git] / config / urlfilter / autoupdate.pl
CommitLineData
10e4f239 1#!/usr/bin/perl
10e4f239 2#
1465b127
MT
3# This code is distributed under the terms of the GPL
4#
5# (c) 2004-2007 marco.s - http://www.urlfilter.net
6#
7# $Id: autoupdate.pl,v 1.1 2007/03/14 00:00:00 marco.s Exp $
10e4f239
MT
8#
9use strict;
10
11my $make_clean = 1;
12
13my $swroot = "/var/ipfire";
14my $target = "$swroot/urlfilter/download";
1465b127 15my $tempdb = "$target/blacklists";
10e4f239
MT
16my $dbdir = "$swroot/urlfilter/blacklists";
17
18my $sourceurlfile = "$swroot/urlfilter/autoupdate/autoupdate.urls";
19my $updconffile = "$swroot/urlfilter/autoupdate/autoupdate.conf";
20my $updflagfile = "$swroot/urlfilter/blacklists/.autoupdate.last";
21
22my %cgiparams;
23my %updatesettings;
10e4f239
MT
24my $blacklist_url;
25my $blacklist_src;
26my $source_url;
27my $source_name;
28my @source_urllist;
29
1465b127 30my @categories;
10e4f239
MT
31my $blacklist;
32my $category;
33
34my $exitcode = 1;
35
36if (-e "$sourceurlfile")
37{
38 open(FILE, $sourceurlfile);
39 @source_urllist = <FILE>;
40 close(FILE);
41}
42
43if (-e "$updconffile") { &readhash("$updconffile", \%updatesettings); }
44
45if (@ARGV[0] =~ m@^(f|h)tt?ps?://@) { $updatesettings{'UPDATE_SOURCE'} = @ARGV[0]; }
46
47if ($updatesettings{'UPDATE_SOURCE'} eq 'custom')
48{
49 $blacklist_url=$updatesettings{'CUSTOM_UPDATE_URL'};
50} else {
51 $blacklist_url=$updatesettings{'UPDATE_SOURCE'};
52 foreach (@source_urllist)
53 {
54 chomp;
55 $source_name = substr($_,0,rindex($_,","));
56 $source_url = substr($_,index($_,",")+1);
57 if ($blacklist_url eq $source_url) { $blacklist_src=$source_name; }
58 }
59}
60
61if ($blacklist_src eq '') { $blacklist_src="custom source URL"; }
62
63$blacklist_url =~ s/\&/\\\&/;
64
65$blacklist=substr($blacklist_url,rindex($blacklist_url,"/")+1);
66if (($blacklist =~ /\?/) || (!($blacklist =~ /\.t(ar\.)?gz$/))) { $blacklist = 'blacklist.tar.gz'; }
67$blacklist=$target.'/'.$blacklist;
68
69unless ($blacklist_url eq '')
70{
71
72 if (-d $target) { system("rm -rf $target"); }
73 system("mkdir $target");
74
75 system("/usr/bin/wget -o $target/wget.log -O $blacklist $blacklist_url");
76
77 if (-e $blacklist)
78 {
79 system("/bin/tar --no-same-owner -xzf $blacklist -C $target");
1465b127
MT
80 if (-d "$target/BL") { system ("mv $target/BL $target/blacklists"); }
81 if (-d "$tempdb")
10e4f239 82 {
1465b127
MT
83 undef(@categories);
84 &getblockcategory ($tempdb);
85 foreach (@categories) { $_ = substr($_,length($tempdb)+1); }
86
10e4f239
MT
87 open(FILE, ">$target/update.conf");
88 flock FILE, 2;
89 print FILE "logdir $target\n";
1465b127
MT
90 print FILE "dbhome $tempdb\n\n";
91
92 foreach $category (@categories) {
93 $blacklist = $category;
94 $category =~ s/\//_/g;
95 print FILE "dest $category {\n";
96 if (-s "$tempdb/$blacklist/domains") {
97 print FILE " domainlist $blacklist\/domains\n";
10e4f239 98 }
1465b127
MT
99 if (-s "$tempdb/$blacklist/urls") {
100 print FILE " urllist $blacklist\/urls\n";
101 }
102 print FILE "}\n\n";
103 $category = $blacklist;
10e4f239 104 }
1465b127 105
10e4f239
MT
106 print FILE "acl {\n";
107 print FILE " default {\n";
108 print FILE " pass none\n";
109 print FILE " }\n";
110 print FILE "}\n";
111 close FILE;
112
0bd7d00b 113 system("/usr/bin/squidGuard -d -c $target/update.conf -C all");
10e4f239
MT
114
115 system("cp -r $target/blacklists/* $dbdir");
116
117 system("chown -R nobody.nobody $dbdir");
118
1465b127 119 &setpermissions ($dbdir);
10e4f239
MT
120
121 system("touch $updflagfile");
122 system("chown nobody.nobody $updflagfile");
123
0bd7d00b 124 system("/etc/init.d/squid restart");
10e4f239
MT
125
126 system("logger -t installpackage[urlfilter] \"URL filter blacklist - Update from $blacklist_src completed\"");
127
128 $exitcode = 0;
129
130 } else {
131 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: Not a valid URL filter blacklist\"");
132 }
133 } else {
134 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: Unable to retrieve blacklist from $blacklist_src\"");
135 }
136
137} else {
138 system("logger -t installpackage[urlfilter] \"URL filter blacklist - ERROR: No update source defined\"");
139}
140
141if ((-d $target) && ($make_clean)) { system("rm -rf $target"); }
142
143exit $exitcode;
144
145# -------------------------------------------------------------------
146
147sub readhash
148{
149 my $filename = $_[0];
150 my $hash = $_[1];
151 my ($var, $val);
152
153 if (-e $filename)
154 {
155 open(FILE, $filename) or die "Unable to read file $filename";
156 while (<FILE>)
157 {
158 chop;
159 ($var, $val) = split /=/, $_, 2;
160 if ($var)
161 {
162 $val =~ s/^\'//g;
163 $val =~ s/\'$//g;
164
165 # Untaint variables read from hash
166 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
167 $val =~ /([\w\W]*)/; $val = $1;
168 $hash->{$var} = $val;
169 }
170 }
171 close FILE;
172 }
173}
174
175# -------------------------------------------------------------------
1465b127
MT
176
177sub getblockcategory
178{
179 foreach $category (<$_[0]/*>)
180 {
181 if (-d $category)
182 {
183 if ((-s "$category/domains") || (-s "$category/urls"))
184 {
185 unless ($category =~ /\bcustom\b/) { push(@categories,$category); }
186 }
187 &getblockcategory ($category);
188 }
189 }
190}
191
192# -------------------------------------------------------------------
193
194sub setpermissions
195{
196 my $bldir = $_[0];
197
198 foreach $category (<$bldir/*>)
199 {
200 if (-d $category){
201 system("chmod 755 $category &> /dev/null");
202 foreach $blacklist (<$category/*>)
203 {
204 if (-f $blacklist) { system("chmod 644 $blacklist &> /dev/null"); }
205 if (-d $blacklist) { system("chmod 755 $blacklist &> /dev/null"); }
206 }
207 system("chmod 666 $category/*.db &> /dev/null");
208 &setpermissions ($category);
209 }
210 }
211}
212
213# -------------------------------------------------------------------