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