]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/updxlrator/download
Merge branch 'master' of git://git.ipfire.org/ipfire-2.x
[people/pmueller/ipfire-2.x.git] / config / updxlrator / download
1 #!/usr/bin/perl
2 #
3 # This code is distributed under the terms of the GPL
4 #
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
6 #
7 # $Id: download,v 2.0 2008/04/06 00:00:00 marco.s Exp $
8 #
9
10 use strict;
11 use HTTP::Date;
12
13 my $swroot='/var/ipfire';
14 my $apphome="$swroot/updatexlrator";
15 my $logfile="/var/log/updatexlrator/download.log";
16 my $logging=0;
17 my $repository='/var/updatecache';
18 my $login='';
19 my $dlrate='';
20 my $uuid='';
21 my $wget="$apphome/bin/wget";
22 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
23 my %xlratorsettings=();
24 my %proxysettings=();
25 my @http_header=();
26 my $remote_mtime=0;
27 my $updatefile='';
28 my $unique=0;
29 my $mirror=1;
30
31 my $sfOk="1";
32
33 my $vendorid = @ARGV[0]; if ($vendorid eq '') { exit; }
34 my $sourceurl = @ARGV[1]; if ($sourceurl eq '') { exit; }
35 my $cfmirror = @ARGV[2]; if ($cfmirror eq '') { exit; }
36
37 umask(0002);
38
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/;
44
45 unless (-d "$repository/download/$vendorid")
46 {
47 system("mkdir $repository/download/$vendorid");
48 system("$apphome/bin/setperms download/$vendorid");
49 }
50
51 exit if (-e "$repository/download/$vendorid/$updatefile");
52
53 system("touch $repository/download/$vendorid/$updatefile");
54
55 if ($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
65 if (-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
72 if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
73
74 if (-e "$swroot/proxy/advanced/settings")
75 {
76 %proxysettings=();
77 &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
78 }
79
80 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
81 {
82 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
83 if ($proxysettings{'UPSTREAM_PASSWORD'})
84 {
85 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
86 }
87 }
88
89 if ($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 }
95
96 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
97 @http_header = `$wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
98 $ENV{'http_proxy'} = '';
99
100 foreach (@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 }
106
107 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
108 unlink "$repository/download/$vendorid/$updatefile";
109 $_ = system("$wget $login $dlrate --user-agent=\"$useragent\" -q -nc -P $repository/download/$vendorid $sourceurl");
110 $ENV{'http_proxy'} = '';
111
112 if ($_ == 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
150 sub 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 # -------------------------------------------------------------------
179
180 sub writelog
181 {
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
193 sub setcachestatus
194 {
195 open (FILE,">$_[0]");
196 print FILE "$_[1]\n";
197 close FILE;
198 }
199
200 # -------------------------------------------------------------------