]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/updxlrator/updxlrator
Upgrades madwifi to a newer version hopefully that solves some ath
[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#
5# (c) 2006 marco.s
6#
7# $Id: updxlrator,v 1.0 2006/10/03 00:00:00 marco.s Exp $
8#
9
10use strict;
11
12use IO::Socket;
13
14$|=1;
15
16my $swroot="/var/ipfire";
381f2e71 17my $updcachedir="/var/updatecache";
46c01c09
MT
18my %netsettings=();
19my %xlratorsettings=();
20my $http_port="81";
21my $logfile="/var/log/updatexlrator/cache.log";
22my $logging=0;
23my $passive_mode=0;
24my $maxusage=0;
25my $nice='';
26my @tmp=();
27my $now='';
28my $request='';
29my $from_local_cache=0;
30my $dsturl='';
31my $hostaddr='';
32my $username='';
33my $method='';
34my @metadata=();
35
36my $sfNoSource = "0";
37my $sfOk = "1";
38my $sfOutdated = "2";
39
40unless (-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
50readhash("${swroot}/ethernet/settings", \%netsettings);
51
52if (-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'};
69dcc425 58 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/bin/nice --adjustment=15 '; };
46c01c09
MT
59}
60if (!$maxusage) { $maxusage=75; };
61
62
63while (<>) {
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 }
5b2a12ff
MT
110
111 # -----------------------------------------------------------
112 # Section: Avira Downloads
46c01c09 113 # -----------------------------------------------------------
5b2a12ff
MT
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");
186e3d2c
MT
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");
5b2a12ff
MT
136 }
137
138 # -----------------------------------------------------------
186e3d2c
MT
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 # -----------------------------------------------------------
2dfb38e6
CS
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 }
4ae86474
AF
157
158 # -----------------------------------------------------------
159 # Section: AVG Downloads
160 # -----------------------------------------------------------
161
162 if ($dsturl =~ m@^http://[^/]*\.(grisoft|avg)\.com/.*\.(bin)$@i)
163 {
164 $from_local_cache = &cache_access($dsturl,$hostaddr,$username,"AVG");
165 }
166
2dfb38e6 167
d6d6d598 168 # -----------------------------------------------------------
98ab7f50
CS
169 # Section: Kaspersky Downloads
170 # not working properly
d6d6d598
CS
171 # -----------------------------------------------------------
172
98ab7f50
CS
173 #if ($dsturl =~ m@^http://dnl-.*\.kaspersky-labs\.com\/(diffs|bases|AutoPatches).*$@i)
174 #{
175 #$from_local_cache = &cache_access($dsturl,$hostaddr,$username,"Kaspersky");
176 #}
94774b8a 177
2dfb38e6 178 # -----------------------------------------------------------
46c01c09
MT
179
180 }
181
182 if ($from_local_cache) { $request="http://$netsettings{'GREEN_ADDRESS'}:$http_port/updatecache/".substr($dsturl,rindex($dsturl,"/")+1)." $hostaddr $username $method\n"; }
183
184 print $request;
185}
186
187# -------------------------------------------------------------------
188
189sub readhash
190{
191 my $filename = $_[0];
192 my $hash = $_[1];
193 my ($var, $val);
194
195 if (-e $filename)
196 {
197 open(FILE, $filename) or die "Unable to read file $filename";
198 while (<FILE>)
199 {
200 chop;
201 ($var, $val) = split /=/, $_, 2;
202 if ($var)
203 {
204 $val =~ s/^\'//g;
205 $val =~ s/\'$//g;
206
207 # Untaint variables read from hash
208 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
209 $val =~ /([\w\W]*)/; $val = $1;
210 $hash->{$var} = $val;
211 }
212 }
213 close FILE;
214 }
215}
216
217# -------------------------------------------------------------------
218
219sub writelog
220{
221 open(LOGFILE,">>$logfile");
222 print LOGFILE time." $_[0] $_[1] $_[2] $_[3] $_[4]\n";
223 close(LOGFILE);
224}
225
226# -------------------------------------------------------------------
227
228sub diskfree
229{
230 open(DF,"/bin/df --block-size=1 $_[0]|");
231 while(<DF>)
232 {
233 unless ($_ =~ m/^Filesystem/ )
234 {
235 my ($device,$size,$used,$free,$percent,$mount) = split;
236 if ($free =~ m/^(\d+)$/)
237 {
238 close DF;
239 return $free;
240 }
241 }
242 }
243 close DF;
244}
245
246# -------------------------------------------------------------------
247
248sub diskusage
249{
250 open(DF,"/bin/df $_[0]|");
251 while(<DF>)
252 {
253 unless ($_ =~ m/^Filesystem/ )
254 {
255 my ($device,$size,$used,$free,$percent,$mount) = split;
256 if ($percent =~ m/^(\d+)%$/)
257 {
258 close DF;
259 $percent =~ s/%$//;
260 return $percent;
261 }
262 }
263 }
264 close DF;
265}
266
267# -------------------------------------------------------------------
268
269sub getdownloadsize
270{
271 my $remote=0;
272 my @response=();
273 my $contentlength=0;
274
275 my $url = $_[0];
276
277 $url =~ s@^(.*)://([^/]*)@@;
278
279 my $proto = $1;
280 my $fqhn = $2;
281
282 if ((-e "$swroot/red/active") && ($proto eq 'http'))
283 {
284 $remote = IO::Socket::INET->new(
285 PeerHost => $fqhn,
286 PeerPort => 'http(80)',
287 Timeout => 1
288 );
289 }
290
291 if ($remote)
292 {
293 print $remote "HEAD $url HTTP/1.0\n";
294 print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n";
295 print $remote "Host: $fqhn\n";
296 print $remote "Accept: */*\n\n";
297 while (<$remote>) { push(@response,$_); }
298 close $remote;
299 if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/)
300 {
301 foreach (@response)
302 {
303 if (/^Content-Length: /i)
304 {
305 s/^Content-Length: //i;
306 $contentlength=$_;
307 }
308 }
309 }
310 }
311 return $contentlength;
312}
313
314# -------------------------------------------------------------------
315
316sub cache_access
317{
318 my $updsource="UPDCACHE";
319 my $updfile='';
320 my $do_redirect=0;
321
322 $_[0] =~ s@\%2f@/@ig;
323 $updfile = substr($_[0],rindex($_[0],"/")+1);
324
325 if (!-e "$updcachedir/metadata/$updfile")
326 {
327 open(FILE,">$updcachedir/metadata/$updfile");
328 print FILE "$_[0]\n$_[3]\n$sfOutdated\n0\n";
329 close(FILE);
330 }
331
332 if (-e "$updcachedir/$updfile")
333 {
334 open(FILE,">>$updcachedir/metadata/$updfile");
335 print FILE time."\n";
336 close(FILE);
337 $do_redirect=1;
338 }
339 else
340 {
341 $updsource="DLSOURCE";
342 if ((!$passive_mode) && (&diskusage($updcachedir) <= $maxusage) && (&getdownloadsize <= &diskfree($updcachedir)) && (!-e "$updcachedir/download/$updfile"))
343 {
344 system("$nice/var/ipfire/updatexlrator/bin/download $_[0] &");
345 }
346 }
347
348 if ($logging) { &writelog($_[1],$_[2],$_[3],$updsource,$_[0]); }
349
350 return $do_redirect;
351}
352
353# -------------------------------------------------------------------