]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/updxlrator/updxlrator
core81: set need reboot flag and restart apache.
[people/teissler/ipfire-2.x.git] / config / updxlrator / updxlrator
CommitLineData
46c01c09
MT
1#!/usr/bin/perl
2#
3# This code is distributed under the terms of the GPL
4#
4f4092e3 5# (c) 2006-2009 marco.s - http://update-accelerator.advproxy.net
46c01c09 6#
4f4092e3
CS
7# Portions (c) 2008 by dotzball - http://www.blockouttraffic.de
8#
9# $Id: updxlrator,v 2.1 2009/01/10 00:00:00 marco.s Exp $
46c01c09 10#
4f6b044f
JIW
11# ChangeLog:
12#
13# 2012-10-26: nightshift - move curly bracket to capture AVG download source.
14# - Adding BIG HINT for new update source#
15#
46c01c09
MT
16
17use strict;
0a638396 18use HTTP::Date;
46c01c09
MT
19
20$|=1;
21
22my $swroot="/var/ipfire";
381f2e71 23my $updcachedir="/var/updatecache";
0a638396
CS
24my $apphome="/var/ipfire/updatexlrator";
25my $logfile="/var/log/updatexlrator/cache.log";
4f4092e3 26my $wget="/usr/bin/wget";
0a638396
CS
27my $debug=(-e "$apphome/debug");
28my $http_port='81';
46c01c09 29my %netsettings=();
0a638396 30my %proxysettings=();
46c01c09 31my %xlratorsettings=();
46c01c09
MT
32my $logging=0;
33my $passive_mode=0;
34my $maxusage=0;
35my $nice='';
36my @tmp=();
46c01c09 37my $request='';
0a638396
CS
38my $xlrator_url=0;
39my $source_url='';
46c01c09
MT
40my $hostaddr='';
41my $username='';
42my $method='';
0a638396
CS
43my $unique = 0;
44my $mirror = 1;
46c01c09
MT
45
46readhash("${swroot}/ethernet/settings", \%netsettings);
47
48if (-e "$swroot/updatexlrator/settings")
49{
50 &readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
51 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
52 if ($xlratorsettings{'PASSIVE_MODE'} eq 'on') { $passive_mode=1; };
53 $maxusage=$xlratorsettings{'MAX_DISK_USAGE'};
646750c5 54 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/bin/nice --adjustment=15 '; };
46c01c09
MT
55}
56if (!$maxusage) { $maxusage=75; };
57
4f4092e3
CS
58# dotzball: check for dead downloads
59system("$apphome/bin/checkdeaddl &");
46c01c09
MT
60
61while (<>) {
62
63 $request=$_;
46c01c09
MT
64
65 @tmp=split(/ /,$request);
66 chomp(@tmp);
67
0a638396
CS
68 $source_url = $tmp[0];
69 $hostaddr = $tmp[1]; while ($hostaddr =~ /.*\/$/) { chop $hostaddr; }
70 $username = $tmp[2]; if ($username eq '') { $username='-'; };
71 $method = $tmp[3];
72
9663c20e 73 $xlrator_url = "";
46c01c09
MT
74
75 if (($method eq 'GET') || ($method eq 'HEAD'))
76 {
77
78 # -----------------------------------------------------------
79 # Section: Windows Update / Windows Downloads
80 # -----------------------------------------------------------
81
82 if (
bc689853
JIW
83 (($source_url =~ m@^http://[^/]*\.microsoft\.com/.*\.(exe|psf|msi|msp|msu|cab)$@i) ||
84 ($source_url =~ m@^http://[^/]*\.windowsupdate\.com/.*\.(exe|psf|msi|msp|msu|cab)$@i))
0a638396
CS
85 && ($source_url !~ m@^http://[^/]*\.microsoft\.com/.*(/autoupd|selfupdate/).*\.cab@i)
86 && ($source_url !~ m@\&@)
46c01c09
MT
87 )
88 {
0a638396 89 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Microsoft",$unique);
46c01c09
MT
90 }
91
92 # -----------------------------------------------------------
93 # Section: Adobe Downloads
94 # -----------------------------------------------------------
95
4f6b044f 96
0a638396
CS
97 if (
98 ($source_url =~ m@^http://(ar)?download\.adobe\.com/.*\.(exe|msi|bin|dmg|idx|gz)$@i) ||
99 ($source_url =~ m@^http://swupdl\.adobe\.com/updates/.*\.(exe|msi|bin|dmg|idx|gz|[a-z][a-z]_[A-Z][A-Z])$@i) ||
100 ($source_url =~ m@^http://swupmf\.adobe\.com/manifest/.*\.upd$@i)
101 )
102 {
103 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Adobe",$unique);
104 }
105
106 # -----------------------------------------------------------
107 # Section: Linux Downloads
108 # -----------------------------------------------------------
109
8dbba0e4 110 if (
77fa792f 111 ($source_url =~ m@^[h|f]t?tp://[^?]+\.(pkg\.tar\.xz|deb|rpm)$@i) ||
1235b1f1 112 ($source_url =~ m@^[h|f]t?tp://[^?]+/distfiles/[^?]+\.(tar\.gz|tar\.bz2|tar\.xz|tgz|zip|patch\.bz2|gz|docx|patch|pdf|exe)$@i)
8dbba0e4 113 )
46c01c09 114 {
0a638396
CS
115 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Linux",$mirror);
116 }
117
118 # -----------------------------------------------------------
119 # Section: Trend Micro Downloads
120 # -----------------------------------------------------------
121
122 if (
123 ($source_url =~ m@^http://[^/]*\.trendmicro\.com/activeupdate/.*@i) &&
124 ($source_url !~ m@.*/tmnotify\.dat$@i) &&
125 ($source_url !~ m@.*/ini_xml\.zip$@i) &&
126 ($source_url !~ m@.*/server\.ini$@i)
127 )
128 {
129 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"TrendMicro",$mirror);
46c01c09
MT
130 }
131
132 # -----------------------------------------------------------
133 # Section: Symantec Downloads
134 # -----------------------------------------------------------
135
0a638396 136 if ($source_url =~ m@^[h|f]tt?p://[^/]*\.symantec(liveupdate)?\.com/.*\.(exe|zip|vdb|xdb)$@i)
46c01c09 137 {
0a638396 138 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Symantec",$unique);
46c01c09 139 }
0a638396 140
5b2a12ff 141 # -----------------------------------------------------------
0a638396 142 # Section: Apple Downloads
46c01c09 143 # -----------------------------------------------------------
0a638396
CS
144
145 if (
4f4092e3 146 (($source_url =~ m@^http://swcdn\.apple\.com/content/downloads/.*\.(tar|pkg)$@i) ||
0a638396
CS
147 ($source_url =~ m@^http://appldnld\.apple\.com\.edgesuite\.net/.*\.(exe|dmg)$@i) ||
148 ($source_url =~ m@^http://.*\.g.akamai.net/.*/3093/1/.*\.(tar|pkg|dmg|exe)$@i))
149 )
5b2a12ff 150 {
0a638396 151 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Apple",$unique);
5b2a12ff 152 }
0a638396
CS
153
154 # -----------------------------------------------------------
155 # Section: Avast Downloads
156 # -----------------------------------------------------------
157
5150d373 158 if ($source_url =~ m@^http://(ion|download)[\d]+\.avast\.com/.*\.(exe|vpu|vpx)$@i)
0a638396
CS
159 {
160 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Avast",$mirror);
186e3d2c 161 }
0a638396 162
219c963e
JIW
163 # -----------------------------------------------------------
164 # Section: Mozilla Downloads
165 # -----------------------------------------------------------
166
167 if ($source_url=~ m@^http://.*\.mozilla\.net/.*\.((complete|partial)\.mar|exe)$@i)
168 {
169 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Mozilla",$unique);
170 }
171
0a638396 172 # -----------------------------------------------------------
7b59d838 173 # Section: Mcafee
0a638396 174 # -----------------------------------------------------------
186e3d2c 175
7b59d838
JIW
176 if ($source_url =~ m@^http://update\.nai\.com/.*\.(mcs|z|gem|dat|zip)$@i)
177 {
178 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"mcafee",$mirror);
179 }
186e3d2c 180
0a638396
CS
181 # -----------------------------------------------------------
182 # Section: Avira Downloads
183 # -----------------------------------------------------------
184
8dbba0e4
CS
185 if (
186 ($source_url =~ m@^http://dl[0-9]\.avgate\.net/.*\.(htm|html|gz)$@i) ||
187 ($source_url =~ m@^http://80.190.130.19[4-5]/update/.*\.(htm|html|gz)$@i) ||
188 ($source_url =~ m@^http://62.146.64.14[6-7]/update/.*\.(htm|html|gz)$@i)
189 )
0a638396
CS
190 {
191 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Avira",$mirror);
2dfb38e6 192 }
4ae86474
AF
193
194 # -----------------------------------------------------------
0a638396 195 # Section: IPFire Downloads
4ae86474
AF
196 # -----------------------------------------------------------
197
0a638396 198 if ($source_url =~ m@^[f|h]t?tp://.*\.(ipfire)$@i)
4ae86474 199 {
0a638396 200 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"IPFire",$mirror);
4ae86474
AF
201 }
202
0a638396
CS
203 # -----------------------------------------------------------
204 # Section: AVG Downloads
205 # -----------------------------------------------------------
46c01c09 206
4f4092e3
CS
207# if ($source_url =~ m@^http://[^/]*\.(grisoft|avg)\.com/.*\.(bin)$@i)
208# {
209# $xlrator_url = &check_cache($source_url,$hostaddr,$username,"AVG",$mirror);
210# }
46c01c09 211
4f6b044f
JIW
212# ----------- ADD NEW SOURCES BEFORE THIS LINE !!! ------------------
213 }
214
9663c20e
MT
215 if ($xlrator_url) {
216 print "OK rewrite-url=\"$xlrator_url\"\n";
217 } else {
218 print "ERR\n";
219 }
46c01c09
MT
220}
221
222# -------------------------------------------------------------------
223
224sub readhash
225{
226 my $filename = $_[0];
227 my $hash = $_[1];
228 my ($var, $val);
229
230 if (-e $filename)
231 {
232 open(FILE, $filename) or die "Unable to read file $filename";
233 while (<FILE>)
234 {
235 chop;
236 ($var, $val) = split /=/, $_, 2;
237 if ($var)
238 {
239 $val =~ s/^\'//g;
240 $val =~ s/\'$//g;
241
242 # Untaint variables read from hash
243 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
244 $val =~ /([\w\W]*)/; $val = $1;
245 $hash->{$var} = $val;
246 }
247 }
248 close FILE;
249 }
250}
251
252# -------------------------------------------------------------------
253
254sub writelog
255{
0a638396
CS
256 if ($logging)
257 {
258 open(LOGFILE,">>$logfile");
259 print LOGFILE time." $_[0] $_[1] $_[2] $_[3] $_[4]\n";
260 close(LOGFILE);
261 }
46c01c09
MT
262}
263
264# -------------------------------------------------------------------
265
0a638396 266sub debuglog
46c01c09 267{
0a638396 268 if ($debug)
46c01c09 269 {
0a638396
CS
270 open(LOGFILE,">>/var/log/updatexlrator/debug.log");
271 my @now = localtime(time);
272 printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d [%d] [%s] %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$$,"updxlrator",$_[0];
273 close(LOGFILE);
46c01c09 274 }
0a638396
CS
275}
276
277# -------------------------------------------------------------------
278
279sub setcachestatus
280{
281 open (FILE,">>$_[0]");
282 print FILE "$_[1]\n";
283 close FILE;
284}
285
286# -------------------------------------------------------------------
287
288sub diskfree
289{
290 open(DF,"/bin/df --block-size=1 $_[0]|");
291 my @dfdata = <DF>;
46c01c09 292 close DF;
0a638396
CS
293 shift(@dfdata);
294 chomp(@dfdata);
295 my $dfstr = join(' ',@dfdata);
296 my ($device,$size,$used,$free,$percent,$mount) = split(' ',$dfstr);
297 if ($free =~ m/^(\d+)$/)
298 {
299 return $free;
300 }
46c01c09
MT
301}
302
303# -------------------------------------------------------------------
304
305sub diskusage
306{
307 open(DF,"/bin/df $_[0]|");
0a638396
CS
308 my @dfdata = <DF>;
309 close DF;
310 shift(@dfdata);
311 chomp(@dfdata);
312 my $dfstr = join(' ',@dfdata);
313 my ($device,$size,$used,$free,$percent,$mount) = split(' ',$dfstr);
314 if ($percent =~ m/^(\d+)%$/)
46c01c09 315 {
0a638396
CS
316 $percent =~ s/%$//;
317 return $percent;
46c01c09 318 }
46c01c09
MT
319}
320
321# -------------------------------------------------------------------
322
0a638396 323sub getmtime
46c01c09 324{
0a638396 325 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]);
46c01c09 326
0a638396
CS
327 return $mtime;
328}
46c01c09 329
0a638396 330# -------------------------------------------------------------------
46c01c09 331
0a638396
CS
332sub check_cache
333{
334 my $updsource="UPDCACHE";
335 my $updfile='';
336 my $cacheurl='';
337 my $vendorid='';
338 my $uuid='';
339 my @http_header=();
340 my $remote_size=0;
341 my $remote_mtime=0;
342 my $login='';
343 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
344
345 my $sourceurl=$_[0];
346 my $cfmirror=$_[4];
347
348 $sourceurl =~ s@\%2f@/@ig;
349 $updfile = substr($sourceurl,rindex($sourceurl,"/")+1);
350 $updfile =~ s@\%20@ @ig;
351
352 if ($cfmirror)
46c01c09 353 {
0a638396
CS
354 $uuid = `echo $updfile | md5sum`;
355 } else {
356 $uuid = `echo $sourceurl | md5sum`;
46c01c09
MT
357 }
358
0a638396
CS
359 $uuid =~ s/[^0-9a-f]//g;
360 $uuid =~ s/([a-f\d]{8})([a-f\d]{4})([a-f\d]{4})([a-f\d]{4})([a-f\d]{12})/$1-$2-$3-$4-$5/;
361
362 $vendorid = $_[3];
363 $vendorid =~ tr/A-Z/a-z/;
364
365 &debuglog("Processing URL $sourceurl");
366 &debuglog("Vendor ID is $vendorid");
367 &debuglog("UUID is $uuid");
368
369 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
46c01c09 370 {
0a638396
CS
371 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
372 if ($proxysettings{'UPSTREAM_PASSWORD'})
46c01c09 373 {
0a638396 374 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
46c01c09
MT
375 }
376 }
46c01c09 377
0a638396 378 if ($proxysettings{'UPSTREAM_PROXY'}) { &debuglog("Using upstream proxy $proxysettings{'UPSTREAM_PROXY'}"); }
46c01c09 379
0a638396 380 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
4f4092e3 381 @http_header = `$wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
0a638396 382 $ENV{'http_proxy'} = '';
46c01c09 383
0a638396 384 foreach (@http_header)
46c01c09 385 {
0a638396
CS
386 chomp;
387 if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; }
388 if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($remote_mtime) }
46c01c09
MT
389 }
390
0a638396
CS
391 if (
392 (-e "$updcachedir/$vendorid/$uuid/$updfile") &&
393 ($remote_size == (-s "$updcachedir/$vendorid/$uuid/$updfile")) &&
394 ($remote_mtime == &getmtime("$updcachedir/$vendorid/$uuid/$updfile"))
395 )
46c01c09 396 {
0a638396
CS
397 &debuglog("File exists in cache and is up to date");
398 &debuglog("Retrieving file from cache ($updsource)");
399 &setcachestatus("$updcachedir/$vendorid/$uuid/access.log",time);
0a638396 400 $cacheurl="http://$netsettings{'GREEN_ADDRESS'}:$http_port/updatecache/$vendorid/$uuid/$updfile";
46c01c09
MT
401 }
402 else
403 {
0a638396
CS
404 if (-e "$updcachedir/$vendorid/$uuid/$updfile")
405 {
406 &debuglog("Local filesize: " . (-s "$updcachedir/$vendorid/$uuid/$updfile"));
407 &debuglog("Local timestamp: " . &getmtime("$updcachedir/$vendorid/$uuid/$updfile"));
408 } else { &debuglog("File not found in cache"); }
46c01c09 409 $updsource="DLSOURCE";
0a638396
CS
410 &debuglog("Remote filesize: $remote_size");
411 &debuglog("Remote timestamp: $remote_mtime");
412 &debuglog("Free disk space: " . &diskfree($updcachedir));
413 &debuglog("Disk usage: " . &diskusage($updcachedir) . "% (max. $maxusage%)");
414 if (-e "$updcachedir/download/$vendorid/$updfile") { &debuglog("File download/$vendorid/$updfile exists"); }
415 &debuglog("Retrieving file from source ($updsource)");
416 if ((!$passive_mode) && (&diskusage($updcachedir) <= $maxusage) && ($remote_size <= &diskfree($updcachedir)) && (!-e "$updcachedir/download/$vendorid/$updfile"))
46c01c09 417 {
0a638396
CS
418 &debuglog("Running command $nice$apphome/bin/download $vendorid $sourceurl $cfmirror &");
419 system("$nice$apphome/bin/download $vendorid $sourceurl $cfmirror &");
46c01c09 420 }
0a638396 421 $cacheurl=$sourceurl;
46c01c09
MT
422 }
423
0a638396 424 &writelog($_[1],$_[2],$_[3],$updsource,$sourceurl);
46c01c09 425
0a638396 426 return $cacheurl;
46c01c09
MT
427}
428
429# -------------------------------------------------------------------