7 use POSIX qw(strftime);
9 use File::Copy qw( copy );
12 use Digest::SHA qw(sha256_hex sha1);
15 use File::Spec::Functions qw(rel2abs);
19 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
22 my $transaction="unknown";
24 my $logfile = "/var/log/download.log";
25 my $datadir = dirname(rel2abs($0))."/";
26 my $secretfile="/etc/apache2/download.secret";
35 my $now = strftime "%a, %d %b %Y %T %z", localtime;
36 print $log "$now: [$$][$transaction]: ";
43 close $log if defined($log);
50 open ($log, '>>', $logfile) || die ("Cannot open logfile $logfile: $!");
57 lprintf "ERROR: $err\n";
60 my $error = encode_entities( $err );
61 print "Status: 404 Not Found\n";
62 print "Content-type: text/html\n\n";
63 print "<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>$error</p><hr><address>Download Server</address>";
64 print "</body></html>\n";
73 qdie ("Received SIG$signame");
78 my $name = basename($file);
80 open my $fh, '<:raw', $file
81 or qdie "Cannot open '$file': $!";
85 -type => 'application/octet-stream',
89 binmode STDOUT, ':raw';
91 unless (copy $fh => \*STDOUT, 8192)
93 qdie "Cannot write to STDOUT";
97 or qdie "Cannot close '$file': $!";
104 return sha256_hex(shift @_);
109 my $fn = $datadir.(shift @_);
110 $fn = File::Spec->rel2abs( readlink($fn) ) if (-l $fn);
111 qdie ("File not found") unless ( -f $fn);
117 my $query = CGI::url(-absolute=>1);
118 my $clienttime = CGI::url_param('time');
119 my $clientid = CGI::url_param('id');
120 my $clienthash = CGI::url_param('hash');
121 my $clientfile = CGI::url_param('file');
122 $clientfile = "default" unless(defined($clientfile));
123 qdie ("Bad parameters") unless (defined($clienttime) && defined($clientid) && defined($clienthash) && ($clienttime=~/^[0-9]+$/));
124 my $drift = time()-$clienttime;
125 qdie ("Client time has drifted - we have ".time()) if (($drift < -$maxdrift) || ($drift > $maxdrift));
126 qdie ("Bad ID") unless ($clientid=~/^[-+._\@a-zA-Z0-9]+$/);
127 qdie ("Bad filename") unless ($clientfile=~/^[-+._a-zA-Z0-9]+$/);
128 qdie ("Bad filename") if ($clientfile=~/^\./);
130 my $hash = gethash($clienttime.":".$clientid.":".$clientfile.":".$secret);
131 qdie ("Bad hash") unless ($hash eq $clienthash);
132 my $fn = getfile($clientfile);
133 $clientfile = basename ($fn);
134 $transaction=$hash." ".$clientfile." ".$clientid;
140 my $clientfile = shift @_;
141 my $fn = getfile($clientfile);
142 $clientfile = basename ($fn);
143 my $size = "unknown";
145 $size = $sb->size if (defined($sb) && defined($sb->size));
146 my $md5sum = "unknown";
147 my $md5fn = $fn.".md5sum";
151 open $md5, "<", $md5fn || qdie ("Can't read md5sum");
155 $md5sum = $1 if (/^([a-f0-9]+)\b/);
161 -type => 'text/plain' );
163 print "$clientfile $size $md5sum\n";
166 open (my $sfh, "<", $secretfile) || qdie("Can't open secret file $secretfile: $!");
167 chomp($secret=join("",<$sfh>));
170 if (!defined($ENV{DOCUMENT_ROOT}) && !defined($ENV{SERVER_NAME}))
172 die ("Bad parameters") unless ($#ARGV == 1);
174 printf "id=%s&file=%s&time=%s&hash=%s",uri_escape($ARGV[0]),uri_escape($ARGV[1]),$t,gethash($t.":".$ARGV[0].":".$ARGV[1].":".$secret)."\n";
179 my $info = CGI::url_param('info');
187 my $file = decodeparams;
188 my $sb = stat($file);
189 my $size = $sb->size;
190 my $t0 = [gettimeofday];
191 lprintf("STARTING\n");
192 $SIG{INT} = \&caughtsignal;
193 $SIG{QUIT} = \&caughtsignal;
194 $SIG{PIPE} = \&caughtsignal;
195 $SIG{HUP} = \&caughtsignal;
196 $SIG{KILL} = \&caughtsignal;
197 $SIG{TERM} = \&caughtsignal;
199 my $elapsed = tv_interval ( $t0, [gettimeofday]);
200 lprintf("SUCCESS %d bytes %.3f MB/s\n", $size, $size/(1000000.0*(($elapsed<0.001)?0.001:$elapsed)));