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