X-Git-Url: http://git.alex.org.uk diff --git a/download.pl b/download.pl index 859d5d8..3f4c2ad 100755 --- a/download.pl +++ b/download.pl @@ -1,5 +1,6 @@ #!/usr/bin/perl +use File::stat; use strict; use warnings; @@ -11,6 +12,7 @@ use File::stat; use Digest::SHA qw(sha256_hex sha1); use MIME::Base64; use File::Spec; +use File::Spec::Functions qw(rel2abs); use CGI; use HTML::Entities; use IO::Handle; @@ -20,7 +22,7 @@ my $log; my $transaction="unknown"; my $logfile = "/var/log/download.log"; -my $datadir = "/var/www/server.example.com/public_html/download/"; +my $datadir = dirname(rel2abs($0))."/"; my $secretfile="/etc/apache2/download.secret"; my $secret; my $sentheader = 0; @@ -102,6 +104,14 @@ sub gethash return sha256_hex(shift @_); } +sub getfile +{ + my $fn = $datadir.(shift @_); + $fn = File::Spec->rel2abs( readlink($fn) ) if (-l $fn); + qdie ("File not found") unless ( -f $fn); + return $fn; +} + sub decodeparams { my $query = CGI::url(-absolute=>1); @@ -119,14 +129,40 @@ sub decodeparams my $hash = gethash($clienttime.":".$clientid.":".$clientfile.":".$secret); qdie ("Bad hash") unless ($hash eq $clienthash); - my $fn = $datadir.$clientfile; - $fn = File::Spec->rel2abs( readlink($fn) ) if (-l $fn); - qdie ("File not found") unless ( -f $fn); + my $fn = getfile($clientfile); $clientfile = basename ($fn); $transaction=$hash." ".$clientfile." ".$clientid; return $fn; } +sub doinfo +{ + my $clientfile = shift @_; + my $fn = getfile($clientfile); + $clientfile = basename ($fn); + my $size = "unknown"; + my $sb = stat($fn); + $size = $sb->size if (defined($sb) && defined($sb->size)); + my $md5sum = "unknown"; + my $md5fn = $fn.".md5sum"; + if ( -r $md5fn ) + { + my $md5; + open $md5, "<", $md5fn || qdie ("Can't read md5sum"); + while (<$md5>) + { + chomp; + $md5sum = $1 if (/^([a-f0-9]+)\b/); + } + close $md5; + } + $sentheader = 1; + print CGI::header( + -type => 'text/plain' ); + + print "$clientfile $size $md5sum\n"; +} + open (my $sfh, "<", $secretfile) || qdie("Can't open secret file $secretfile: $!"); chomp($secret=join("",<$sfh>)); close ($sfh); @@ -140,6 +176,13 @@ if (!defined($ENV{DOCUMENT_ROOT}) && !defined($ENV{SERVER_NAME})) } else { + my $info = CGI::url_param('info'); + if (defined($info)) + { + doinfo($info); + exit 0; + } + openlog; my $file = decodeparams; my $sb = stat($file);