]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/updxlrator/download
Merge branch 'master' of git://git.ipfire.org/ipfire-2.x
[people/pmueller/ipfire-2.x.git] / config / updxlrator / download
CommitLineData
46c01c09
MT
1#!/usr/bin/perl
2#
3# This code is distributed under the terms of the GPL
4#
0a638396 5# (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
46c01c09 6#
0a638396 7# $Id: download,v 2.0 2008/04/06 00:00:00 marco.s Exp $
46c01c09
MT
8#
9
10use strict;
0a638396 11use HTTP::Date;
46c01c09 12
0a638396
CS
13my $swroot='/var/ipfire';
14my $apphome="$swroot/updatexlrator";
46c01c09 15my $logfile="/var/log/updatexlrator/download.log";
0a638396
CS
16my $logging=0;
17my $repository='/var/updatecache';
18my $login='';
19my $dlrate='';
20my $uuid='';
21my $wget="$apphome/bin/wget";
22my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
23my %xlratorsettings=();
24my %proxysettings=();
25my @http_header=();
26my $remote_mtime=0;
27my $updatefile='';
28my $unique=0;
29my $mirror=1;
46c01c09
MT
30
31my $sfOk="1";
32
0a638396
CS
33my $vendorid = @ARGV[0]; if ($vendorid eq '') { exit; }
34my $sourceurl = @ARGV[1]; if ($sourceurl eq '') { exit; }
35my $cfmirror = @ARGV[2]; if ($cfmirror eq '') { exit; }
46c01c09 36
0a638396 37umask(0002);
46c01c09 38
0a638396
CS
39$sourceurl =~ s@\%2f@/@ig;
40$sourceurl =~ s@\%7e@~@ig;
41$updatefile = substr($sourceurl,rindex($sourceurl,"/")+1);
42$updatefile =~ s@\%20@ @ig;
43$vendorid =~ tr/A-Z/a-z/;
46c01c09 44
0a638396 45unless (-d "$repository/download/$vendorid")
46c01c09 46{
0a638396
CS
47 system("mkdir $repository/download/$vendorid");
48 system("$apphome/bin/setperms download/$vendorid");
49}
50
51exit if (-e "$repository/download/$vendorid/$updatefile");
52
53system("touch $repository/download/$vendorid/$updatefile");
54
55if ($cfmirror)
56{
57 $uuid = `echo $updatefile | md5sum`;
58} else {
59 $uuid = `echo $sourceurl | md5sum`;
60}
61
62$uuid =~ s/[^0-9a-f]//g;
63$uuid =~ s/([a-f\d]{8})([a-f\d]{4})([a-f\d]{4})([a-f\d]{4})([a-f\d]{12})/$1-$2-$3-$4-$5/;
64
65if (-e "$swroot/updatexlrator/settings")
66{
67 &readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
68 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
69 if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} ne '') { $dlrate = "--limit-rate=" . int($xlratorsettings{'MAX_DOWNLOAD_RATE'} / 8) . "k" };
70}
71
72if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
73
74if (-e "$swroot/proxy/advanced/settings")
75{
76 %proxysettings=();
77 &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
78}
79
80if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
46c01c09 81{
0a638396
CS
82 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
83 if ($proxysettings{'UPSTREAM_PASSWORD'})
84 {
85 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
86 }
46c01c09
MT
87}
88
0a638396
CS
89if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} eq '')
90{
91 &writelog("Retrieving file for local cache: $updatefile");
92} else {
93 &writelog("Retrieving file for local cache at max. " . $xlratorsettings{'MAX_DOWNLOAD_RATE'} . " kBit/s: $updatefile");
94}
46c01c09 95
0a638396
CS
96$ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
97@http_header = `$wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
98$ENV{'http_proxy'} = '';
46c01c09 99
0a638396
CS
100foreach (@http_header)
101{
102 chomp;
103 if (/^\s*Content-Length:\s/) { s/[^0-9]//g; &writelog("Remote file size: $_ bytes"); }
104 if (/^\s*Last-Modified:\s/) { s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($_); &writelog("Remote file date: $_"); }
105}
46c01c09 106
0a638396
CS
107$ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
108unlink "$repository/download/$vendorid/$updatefile";
109$_ = system("$wget $login $dlrate --user-agent=\"$useragent\" -q -nc -P $repository/download/$vendorid $sourceurl");
110$ENV{'http_proxy'} = '';
46c01c09 111
0a638396
CS
112if ($_ == 0)
113{
114 &writelog("Download finished with result code: OK");
115
116 unless (-d "$repository/$vendorid")
117 {
118 system("mkdir $repository/$vendorid");
119 system("$apphome/bin/setperms $vendorid");
120 }
121
122 unless (-d "$repository/$vendorid/$uuid")
123 {
124 system("mkdir $repository/$vendorid/$uuid");
125 system("$apphome/bin/setperms $vendorid/$uuid");
126 }
127
128 &writelog("Moving file to the cache directory: $vendorid/$uuid");
129 $updatefile =~ s@ @\\ @ig;
130 system("mv $repository/download/$vendorid/$updatefile $repository/$vendorid/$uuid");
131 # Workaround for IPCop's mv bug:
132 utime time,$remote_mtime,"$repository/$vendorid/$uuid/$updatefile";
133 $updatefile =~ s@\\ @ @ig;
134
135 &setcachestatus("$repository/$vendorid/$uuid/source.url",$sourceurl);
136 &setcachestatus("$repository/$vendorid/$uuid/status",$sfOk);
137 &setcachestatus("$repository/$vendorid/$uuid/checkup.log",time);
138 &setcachestatus("$repository/$vendorid/$uuid/access.log",time);
139
140 system("$apphome/bin/setperms $vendorid/$uuid/*");
141
142} else {
143 &writelog("Download finished with result code: ERROR");
144 if (-e "$repository/download/$vendorid/$updatefile") { unlink ("$repository/download/$vendorid/$updatefile"); }
145}
146
147
148# -------------------------------------------------------------------
149
150sub readhash
151{
152 my $filename = $_[0];
153 my $hash = $_[1];
154 my ($var, $val);
155
156 if (-e $filename)
157 {
158 open(FILE, $filename) or die "Unable to read file $filename";
159 while (<FILE>)
160 {
161 chop;
162 ($var, $val) = split /=/, $_, 2;
163 if ($var)
164 {
165 $val =~ s/^\'//g;
166 $val =~ s/\'$//g;
167
168 # Untaint variables read from hash
169 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
170 $val =~ /([\w\W]*)/; $val = $1;
171 $hash->{$var} = $val;
172 }
173 }
174 close FILE;
175 }
176}
177
178# -------------------------------------------------------------------
46c01c09
MT
179
180sub writelog
181{
0a638396
CS
182 if ($logging)
183 {
184 open (LOGFILE,">>$logfile");
185 my @now = localtime(time);
186 printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d [%d] %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$$,$_[0];
187 close LOGFILE;
188 }
189}
190
191# -------------------------------------------------------------------
192
193sub setcachestatus
194{
195 open (FILE,">$_[0]");
196 print FILE "$_[1]\n";
197 close FILE;
46c01c09
MT
198}
199
0a638396 200# -------------------------------------------------------------------