]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/updxlrator/updxlrator
Am Pakfire weitergearbeitet.
[people/pmueller/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";
17my $updcachedir="/srv/web/ipfire/html/updatecache";
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'};
58 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/usr/bin/nice --adjustment=15 '; };
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");
127 }
128
129 # -----------------------------------------------------------
46c01c09
MT
130
131 }
132
133 if ($from_local_cache) { $request="http://$netsettings{'GREEN_ADDRESS'}:$http_port/updatecache/".substr($dsturl,rindex($dsturl,"/")+1)." $hostaddr $username $method\n"; }
134
135 print $request;
136}
137
138# -------------------------------------------------------------------
139
140sub readhash
141{
142 my $filename = $_[0];
143 my $hash = $_[1];
144 my ($var, $val);
145
146 if (-e $filename)
147 {
148 open(FILE, $filename) or die "Unable to read file $filename";
149 while (<FILE>)
150 {
151 chop;
152 ($var, $val) = split /=/, $_, 2;
153 if ($var)
154 {
155 $val =~ s/^\'//g;
156 $val =~ s/\'$//g;
157
158 # Untaint variables read from hash
159 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
160 $val =~ /([\w\W]*)/; $val = $1;
161 $hash->{$var} = $val;
162 }
163 }
164 close FILE;
165 }
166}
167
168# -------------------------------------------------------------------
169
170sub writelog
171{
172 open(LOGFILE,">>$logfile");
173 print LOGFILE time." $_[0] $_[1] $_[2] $_[3] $_[4]\n";
174 close(LOGFILE);
175}
176
177# -------------------------------------------------------------------
178
179sub diskfree
180{
181 open(DF,"/bin/df --block-size=1 $_[0]|");
182 while(<DF>)
183 {
184 unless ($_ =~ m/^Filesystem/ )
185 {
186 my ($device,$size,$used,$free,$percent,$mount) = split;
187 if ($free =~ m/^(\d+)$/)
188 {
189 close DF;
190 return $free;
191 }
192 }
193 }
194 close DF;
195}
196
197# -------------------------------------------------------------------
198
199sub diskusage
200{
201 open(DF,"/bin/df $_[0]|");
202 while(<DF>)
203 {
204 unless ($_ =~ m/^Filesystem/ )
205 {
206 my ($device,$size,$used,$free,$percent,$mount) = split;
207 if ($percent =~ m/^(\d+)%$/)
208 {
209 close DF;
210 $percent =~ s/%$//;
211 return $percent;
212 }
213 }
214 }
215 close DF;
216}
217
218# -------------------------------------------------------------------
219
220sub getdownloadsize
221{
222 my $remote=0;
223 my @response=();
224 my $contentlength=0;
225
226 my $url = $_[0];
227
228 $url =~ s@^(.*)://([^/]*)@@;
229
230 my $proto = $1;
231 my $fqhn = $2;
232
233 if ((-e "$swroot/red/active") && ($proto eq 'http'))
234 {
235 $remote = IO::Socket::INET->new(
236 PeerHost => $fqhn,
237 PeerPort => 'http(80)',
238 Timeout => 1
239 );
240 }
241
242 if ($remote)
243 {
244 print $remote "HEAD $url HTTP/1.0\n";
245 print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n";
246 print $remote "Host: $fqhn\n";
247 print $remote "Accept: */*\n\n";
248 while (<$remote>) { push(@response,$_); }
249 close $remote;
250 if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/)
251 {
252 foreach (@response)
253 {
254 if (/^Content-Length: /i)
255 {
256 s/^Content-Length: //i;
257 $contentlength=$_;
258 }
259 }
260 }
261 }
262 return $contentlength;
263}
264
265# -------------------------------------------------------------------
266
267sub cache_access
268{
269 my $updsource="UPDCACHE";
270 my $updfile='';
271 my $do_redirect=0;
272
273 $_[0] =~ s@\%2f@/@ig;
274 $updfile = substr($_[0],rindex($_[0],"/")+1);
275
276 if (!-e "$updcachedir/metadata/$updfile")
277 {
278 open(FILE,">$updcachedir/metadata/$updfile");
279 print FILE "$_[0]\n$_[3]\n$sfOutdated\n0\n";
280 close(FILE);
281 }
282
283 if (-e "$updcachedir/$updfile")
284 {
285 open(FILE,">>$updcachedir/metadata/$updfile");
286 print FILE time."\n";
287 close(FILE);
288 $do_redirect=1;
289 }
290 else
291 {
292 $updsource="DLSOURCE";
293 if ((!$passive_mode) && (&diskusage($updcachedir) <= $maxusage) && (&getdownloadsize <= &diskfree($updcachedir)) && (!-e "$updcachedir/download/$updfile"))
294 {
295 system("$nice/var/ipfire/updatexlrator/bin/download $_[0] &");
296 }
297 }
298
299 if ($logging) { &writelog($_[1],$_[2],$_[3],$updsource,$_[0]); }
300
301 return $do_redirect;
302}
303
304# -------------------------------------------------------------------