]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/urlfilter/autoupdate.pl
Connections.cgi neu geschrieben.
[people/pmueller/ipfire-2.x.git] / config / urlfilter / autoupdate.pl
1 #!/usr/bin/perl
2 #
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 $
8 #
9 use strict;
10
11 my $make_clean = 1;
12
13 my $swroot = "/var/ipfire";
14 my $target = "$swroot/urlfilter/download";
15 my $tempdb = "$target/blacklists";
16 my $dbdir = "$swroot/urlfilter/blacklists";
17
18 my $sourceurlfile = "$swroot/urlfilter/autoupdate/autoupdate.urls";
19 my $updconffile = "$swroot/urlfilter/autoupdate/autoupdate.conf";
20 my $updflagfile = "$swroot/urlfilter/blacklists/.autoupdate.last";
21
22 my %cgiparams;
23 my %updatesettings;
24 my $blacklist_url;
25 my $blacklist_src;
26 my $source_url;
27 my $source_name;
28 my @source_urllist;
29
30 my @categories;
31 my $blacklist;
32 my $category;
33
34 my $exitcode = 1;
35
36 if (-e "$sourceurlfile")
37 {
38 open(FILE, $sourceurlfile);
39 @source_urllist = <FILE>;
40 close(FILE);
41 }
42
43 if (-e "$updconffile") { &readhash("$updconffile", \%updatesettings); }
44
45 if (@ARGV[0] =~ m@^(f|h)tt?ps?://@) { $updatesettings{'UPDATE_SOURCE'} = @ARGV[0]; }
46
47 if ($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
61 if ($blacklist_src eq '') { $blacklist_src="custom source URL"; }
62
63 $blacklist_url =~ s/\&/\\\&/;
64
65 $blacklist=substr($blacklist_url,rindex($blacklist_url,"/")+1);
66 if (($blacklist =~ /\?/) || (!($blacklist =~ /\.t(ar\.)?gz$/))) { $blacklist = 'blacklist.tar.gz'; }
67 $blacklist=$target.'/'.$blacklist;
68
69 unless ($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");
80 if (-d "$target/BL") { system ("mv $target/BL $target/blacklists"); }
81 if (-d "$tempdb")
82 {
83 undef(@categories);
84 &getblockcategory ($tempdb);
85 foreach (@categories) { $_ = substr($_,length($tempdb)+1); }
86
87 open(FILE, ">$target/update.conf");
88 flock FILE, 2;
89 print FILE "logdir $target\n";
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";
98 }
99 if (-s "$tempdb/$blacklist/urls") {
100 print FILE " urllist $blacklist\/urls\n";
101 }
102 print FILE "}\n\n";
103 $category = $blacklist;
104 }
105
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
113 system("/usr/sbin/squidGuard -d -c $target/update.conf -C all");
114
115 system("cp -r $target/blacklists/* $dbdir");
116
117 system("chown -R nobody.nobody $dbdir");
118
119 &setpermissions ($dbdir);
120
121 system("touch $updflagfile");
122 system("chown nobody.nobody $updflagfile");
123
124 system("/usr/local/bin/restartsquid");
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
141 if ((-d $target) && ($make_clean)) { system("rm -rf $target"); }
142
143 exit $exitcode;
144
145 # -------------------------------------------------------------------
146
147 sub 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 # -------------------------------------------------------------------
176
177 sub 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
194 sub 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 # -------------------------------------------------------------------