Kernel Update auf 2.6.16.55.
[people/pmueller/ipfire-2.x.git] / config / updxlrator / updxlrator
1 #!/usr/bin/perl
2 #
3 # This code is distributed under the terms of the GPL
4 #
5 # (c) 2006 marco.s
6 #
7 # $Id: updxlrator,v 1.0 2006/10/03 00:00:00 marco.s Exp $
8 #
9
10 use strict;
11
12 use IO::Socket;
13
14 $|=1;
15
16 my $swroot="/var/ipfire";
17 my $updcachedir="/srv/web/ipfire/html/updatecache";
18 my %netsettings=();
19 my %xlratorsettings=();
20 my $http_port="81";
21 my $logfile="/var/log/updatexlrator/cache.log";
22 my $logging=0;
23 my $passive_mode=0;
24 my $maxusage=0;
25 my $nice='';
26 my @tmp=();
27 my $now='';
28 my $request='';
29 my $from_local_cache=0;
30 my $dsturl='';
31 my $hostaddr='';
32 my $username='';
33 my $method='';
34 my @metadata=();
35
36 my $sfNoSource = "0";
37 my $sfOk = "1";
38 my $sfOutdated = "2";
39
40 unless (-d "$updcachedir/metadata")
41 {
42 unless (-d "$updcachedir") { mkdir "$updcachedir"; }
43 mkdir "$updcachedir/metadata";
44 system("chown nobody.squid $updcachedir");
45 system("chmod 775 $updcachedir");
46 system("chown nobody.squid $updcachedir/metadata");
47 system("chmod 775 $updcachedir/metadata");
48 }
49
50 readhash("${swroot}/ethernet/settings", \%netsettings);
51
52 if (-e "$swroot/updatexlrator/settings")
53 {
54 &readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
55 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
56 if ($xlratorsettings{'PASSIVE_MODE'} eq 'on') { $passive_mode=1; };
57 $maxusage=$xlratorsettings{'MAX_DISK_USAGE'};
58 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/bin/nice --adjustment=15 '; };
59 }
60 if (!$maxusage) { $maxusage=75; };
61
62
63 while (<>) {
64
65 $request=$_;
66 $from_local_cache=0;
67
68 @tmp=split(/ /,$request);
69 chomp(@tmp);
70
71 $dsturl =$tmp[0];
72 $hostaddr=$tmp[1]; while ($hostaddr =~ /.*\/$/) { chop $hostaddr; }
73 $username=$tmp[2]; if ($username eq '') { $username='-'; };
74 $method =$tmp[3];
75
76 if (($method eq 'GET') || ($method eq 'HEAD'))
77 {
78
79 # -----------------------------------------------------------
80 # Section: Windows Update / Windows Downloads
81 # -----------------------------------------------------------
82
83 if (
84 (($dsturl =~ m@^http://[^/]*\.microsoft\.com/.*\.(exe|psf|msi)$@i) ||
85 ($dsturl =~ m@^http://[^/]*\.windowsupdate\.com/.*\.(exe|psf|cab)$@i))
86 && ($dsturl !~ m@^http://[^/]*\.microsoft\.com/.*(/autoupd|selfupdate/).*\.cab@i)
87 && ($dsturl !~ m@\&@)
88 )
89 {
90 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Microsoft");
91 }
92
93 # -----------------------------------------------------------
94 # Section: Adobe Downloads
95 # -----------------------------------------------------------
96
97 if ($dsturl =~ m@^http://(ar)?download\.adobe\.com/.*\.(exe|bin|dmg|idx|gz)$@i)
98 {
99 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Adobe");
100 }
101
102 # -----------------------------------------------------------
103 # Section: Symantec Downloads
104 # -----------------------------------------------------------
105
106 if ($dsturl =~ m@^[f|h]t?tp://[^/]*\.symantec(liveupdate)?\.com/.*\.(exe|zip|xdb)$@i)
107 {
108 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Symantec");
109 }
110
111 # -----------------------------------------------------------
112 # Section: Avira Downloads
113 # -----------------------------------------------------------
114
115 if ($dsturl =~ m@^http://dl[0-9]\.avgate\.net/.*\.(htm|html|gz)$@i)
116 {
117 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Avira");
118 }
119
120 # -----------------------------------------------------------
121 # Section: Avast Downloads
122 # -----------------------------------------------------------
123
124 if ($dsturl =~ m@^http://download[0-99]\.avast\.com/.*\.(exe|zip|vps|stamp|vpu)$@i)
125 {
126 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Avast");
127 }
128
129 # -----------------------------------------------------------
130 # Section: IPFire Downloads
131 # -----------------------------------------------------------
132
133 if ($dsturl =~ m@^[f|h]t?tp://.*\.(ipfire)$@i)
134 {
135 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"IPFire");
136 }
137
138 # -----------------------------------------------------------
139 # Section: Linux Downloads
140 # -----------------------------------------------------------
141
142 if ($dsturl =~ m@^[f|h]t?tp://.*\.(deb|rpm)$@i)
143 {
144 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Linux");
145 }
146
147 # -----------------------------------------------------------
148
149 # -----------------------------------------------------------
150 # Section: Apple Downloads
151 # -----------------------------------------------------------
152
153 if ($dsturl =~ m@^[f|h]t?tp://swcdn\.apple.*\.(pkm|tar)$@i)
154 {
155 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Apple");
156 }
157
158 # -----------------------------------------------------------
159 # Section: Kaspersky Downloads
160 # -----------------------------------------------------------
161
162 if ($dsturl =~ m@^http://dnl-.*\.kaspersky-labs\.com\/(diffs|bases|AutoPatches).*$@i)
163 {
164 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Kaspersky");
165 }
166
167 # -----------------------------------------------------------
168
169 }
170
171 if ($from_local_cache) { $request="http://$netsettings{'GREEN_ADDRESS'}:$http_port/updatecache/".substr($dsturl,rindex($dsturl,"/")+1)." $hostaddr $username $method\n"; }
172
173 print $request;
174 }
175
176 # -------------------------------------------------------------------
177
178 sub readhash
179 {
180 my $filename = $_[0];
181 my $hash = $_[1];
182 my ($var, $val);
183
184 if (-e $filename)
185 {
186 open(FILE, $filename) or die "Unable to read file $filename";
187 while (<FILE>)
188 {
189 chop;
190 ($var, $val) = split /=/, $_, 2;
191 if ($var)
192 {
193 $val =~ s/^\'//g;
194 $val =~ s/\'$//g;
195
196 # Untaint variables read from hash
197 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
198 $val =~ /([\w\W]*)/; $val = $1;
199 $hash->{$var} = $val;
200 }
201 }
202 close FILE;
203 }
204 }
205
206 # -------------------------------------------------------------------
207
208 sub writelog
209 {
210 open(LOGFILE,">>$logfile");
211 print LOGFILE time." $_[0] $_[1] $_[2] $_[3] $_[4]\n";
212 close(LOGFILE);
213 }
214
215 # -------------------------------------------------------------------
216
217 sub diskfree
218 {
219 open(DF,"/bin/df --block-size=1 $_[0]|");
220 while(<DF>)
221 {
222 unless ($_ =~ m/^Filesystem/ )
223 {
224 my ($device,$size,$used,$free,$percent,$mount) = split;
225 if ($free =~ m/^(\d+)$/)
226 {
227 close DF;
228 return $free;
229 }
230 }
231 }
232 close DF;
233 }
234
235 # -------------------------------------------------------------------
236
237 sub diskusage
238 {
239 open(DF,"/bin/df $_[0]|");
240 while(<DF>)
241 {
242 unless ($_ =~ m/^Filesystem/ )
243 {
244 my ($device,$size,$used,$free,$percent,$mount) = split;
245 if ($percent =~ m/^(\d+)%$/)
246 {
247 close DF;
248 $percent =~ s/%$//;
249 return $percent;
250 }
251 }
252 }
253 close DF;
254 }
255
256 # -------------------------------------------------------------------
257
258 sub getdownloadsize
259 {
260 my $remote=0;
261 my @response=();
262 my $contentlength=0;
263
264 my $url = $_[0];
265
266 $url =~ s@^(.*)://([^/]*)@@;
267
268 my $proto = $1;
269 my $fqhn = $2;
270
271 if ((-e "$swroot/red/active") && ($proto eq 'http'))
272 {
273 $remote = IO::Socket::INET->new(
274 PeerHost => $fqhn,
275 PeerPort => 'http(80)',
276 Timeout => 1
277 );
278 }
279
280 if ($remote)
281 {
282 print $remote "HEAD $url HTTP/1.0\n";
283 print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n";
284 print $remote "Host: $fqhn\n";
285 print $remote "Accept: */*\n\n";
286 while (<$remote>) { push(@response,$_); }
287 close $remote;
288 if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/)
289 {
290 foreach (@response)
291 {
292 if (/^Content-Length: /i)
293 {
294 s/^Content-Length: //i;
295 $contentlength=$_;
296 }
297 }
298 }
299 }
300 return $contentlength;
301 }
302
303 # -------------------------------------------------------------------
304
305 sub cache_access
306 {
307 my $updsource="UPDCACHE";
308 my $updfile='';
309 my $do_redirect=0;
310
311 $_[0] =~ s@\%2f@/@ig;
312 $updfile = substr($_[0],rindex($_[0],"/")+1);
313
314 if (!-e "$updcachedir/metadata/$updfile")
315 {
316 open(FILE,">$updcachedir/metadata/$updfile");
317 print FILE "$_[0]\n$_[3]\n$sfOutdated\n0\n";
318 close(FILE);
319 }
320
321 if (-e "$updcachedir/$updfile")
322 {
323 open(FILE,">>$updcachedir/metadata/$updfile");
324 print FILE time."\n";
325 close(FILE);
326 $do_redirect=1;
327 }
328 else
329 {
330 $updsource="DLSOURCE";
331 if ((!$passive_mode) && (&diskusage($updcachedir) <= $maxusage) && (&getdownloadsize <= &diskfree($updcachedir)) && (!-e "$updcachedir/download/$updfile"))
332 {
333 system("$nice/var/ipfire/updatexlrator/bin/download $_[0] &");
334 }
335 }
336
337 if ($logging) { &writelog($_[1],$_[2],$_[3],$updsource,$_[0]); }
338
339 return $do_redirect;
340 }
341
342 # -------------------------------------------------------------------