]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | use strict; | |
11 | ||
12 | use IO::Socket; | |
13 | ||
14 | $|=1; | |
15 | ||
16 | my $swroot="/var/ipfire"; | |
17 | my $updcachedir="/srv/web/ipfire/html/updatecache"; | |
18 | my %netsettings=(); | |
19 | my %xlratorsettings=(); | |
20 | my $http_port="81"; | |
21 | my $logfile="/var/log/updatexlrator/cache.log"; | |
22 | my $logging=0; | |
23 | my $passive_mode=0; | |
24 | my $maxusage=0; | |
25 | my $nice=''; | |
26 | my @tmp=(); | |
27 | my $now=''; | |
28 | my $request=''; | |
29 | my $from_local_cache=0; | |
30 | my $dsturl=''; | |
31 | my $hostaddr=''; | |
32 | my $username=''; | |
33 | my $method=''; | |
34 | my @metadata=(); | |
35 | ||
36 | my $sfNoSource = "0"; | |
37 | my $sfOk = "1"; | |
38 | my $sfOutdated = "2"; | |
39 | ||
40 | unless (-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 | ||
50 | readhash("${swroot}/ethernet/settings", \%netsettings); | |
51 | ||
52 | if (-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 | } | |
60 | if (!$maxusage) { $maxusage=75; }; | |
61 | ||
62 | ||
63 | while (<>) { | |
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 | ||
122 | sub 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 | ||
152 | sub writelog | |
153 | { | |
154 | open(LOGFILE,">>$logfile"); | |
155 | print LOGFILE time." $_[0] $_[1] $_[2] $_[3] $_[4]\n"; | |
156 | close(LOGFILE); | |
157 | } | |
158 | ||
159 | # ------------------------------------------------------------------- | |
160 | ||
161 | sub 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 | ||
181 | sub 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 | ||
202 | sub 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 | ||
249 | sub 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 | # ------------------------------------------------------------------- |