| 1 |
#!/usr/bin/perl -wT |
| 2 |
|
| 3 |
# F*EX CGI for download |
| 4 |
# |
| 5 |
# Author: Ulli Horlacher <[email protected]> |
| 6 |
# |
| 7 |
|
| 8 |
BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 } |
| 9 |
|
| 10 |
# use utf8; |
| 11 |
use Fcntl qw':DEFAULT :flock :seek'; |
| 12 |
use Cwd qw'abs_path'; |
| 13 |
use File::Basename; |
| 14 |
use IO::Handle; |
| 15 |
use Encode; |
| 16 |
|
| 17 |
# add fex lib |
| 18 |
($FEXLIB) = $ENV{FEXLIB} =~ /(.+)/; |
| 19 |
die "$0: no $FEXLIB\n" unless -d $FEXLIB; |
| 20 |
|
| 21 |
our $error = 'F*EX download ERROR'; |
| 22 |
our $head = "$ENV{SERVER_NAME} F*EX download"; |
| 23 |
# import from fex.pp |
| 24 |
our ($spooldir,$tmpdir,@logdir,$skeydir,$dkeydir,$akeydir,$durl); |
| 25 |
our ($bs,$fop_auth,$timeout,$keep_default,$nowarning); |
| 26 |
our ($limited_download,$admin,$akey,$adlm,$amdl,$vp,$arx,$archive_sharing); |
| 27 |
our (@file_link_dirs); |
| 28 |
our ($MB,$RA); |
| 29 |
|
| 30 |
local $from = ''; |
| 31 |
local $to = ''; |
| 32 |
local $file = ''; |
| 33 |
|
| 34 |
# load common code, local config : $HOME/lib/fex.ph |
| 35 |
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n"; |
| 36 |
|
| 37 |
my $RM = $ENV{REQUEST_METHOD}; |
| 38 |
|
| 39 |
my $maccess = ''; # modify access right |
| 40 |
|
| 41 |
if (@download_hosts and not ipin($RA,@download_hosts)) { |
| 42 |
http_die( |
| 43 |
"Downloads from your host ($RA) are not allowed.", |
| 44 |
"Contact $ENV{SERVER_ADMIN} for details." |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
&check_maint; |
| 49 |
|
| 50 |
# call localized fop if available |
| 51 |
if ($0 !~ m{/locale/.*/fop} and my $lang = $ENV{HTTP_ACCEPT_LANGUAGE}) { |
| 52 |
if ($lang =~ /^de/ and $0 =~ m{(.*)/cgi-bin/fop}) { |
| 53 |
my $fop = "$1/locale/deutsch/cgi-bin/fop"; |
| 54 |
exec $fop if -x $fop; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
my $log = 'fop.log'; |
| 59 |
|
| 60 |
my $http_client = $ENV{HTTP_USER_AGENT} || ''; |
| 61 |
|
| 62 |
$file = $ENV{PATH_INFO} || ''; |
| 63 |
http_die('no file name') unless $file; |
| 64 |
$file =~ s:%3F:/?/:g; # escape '?' for URL-decoding |
| 65 |
$file =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge; |
| 66 |
$file =~ s:/\?/:%3F:g; # deescape '?' |
| 67 |
$file =~ s:/\.\.:/__:g; |
| 68 |
$file =~ s:^/+::; |
| 69 |
$file = untaint($file); |
| 70 |
|
| 71 |
# secure mode with HTTP authorization? |
| 72 |
if ($fop_auth) { |
| 73 |
@http_auth = (); |
| 74 |
if ($ENV{HTTP_AUTHORIZATION} and $ENV{HTTP_AUTHORIZATION} =~ /Basic\s+(.+)/) { |
| 75 |
@http_auth = split(':',decode_b64($1)); |
| 76 |
} |
| 77 |
if (@http_auth != 2) { |
| 78 |
&require_auth; |
| 79 |
} |
| 80 |
&check_auth($file,@http_auth); |
| 81 |
} |
| 82 |
|
| 83 |
# download-URI-scheme for share archive request (with version) |
| 84 |
if ($file =~ m:(.+?)/(.+?)/(.+?)/(\w+)/((.+)_($vp\.($arx)))$:) { |
| 85 |
my $owner = $1; |
| 86 |
my $share = $2; |
| 87 |
my $suser = $3; |
| 88 |
my $key = $4; |
| 89 |
my $file = $5; |
| 90 |
my $archive = $6; |
| 91 |
my $avt = $7; |
| 92 |
unless ($owner =~ /\w/ and $share =~ /\w/ and $suser =~ /\w/) { |
| 93 |
http_die("illegal request"); |
| 94 |
} |
| 95 |
$owner .= '@'.$mdomain if $mdomain and $owner !~ /@/; |
| 96 |
$suser .= '@'.$mdomain if $mdomain and $suser !~ /@/; |
| 97 |
my $sdir = "$owner/SHARE/$share"; |
| 98 |
my $filed = "$sdir/archives/$archive/$avt"; |
| 99 |
if ($owner eq $suser) { |
| 100 |
my $id = readline1("$owner/@") or |
| 101 |
http_die("unknown owner user $owner"); |
| 102 |
if ($key ne md5_hex("$file:$id") and |
| 103 |
$key ne md5_hex("$file:".md5_hex($id))) |
| 104 |
{ http_die("wrong share access key") } |
| 105 |
my $sharing = readlink("$owner/\@ARCHIVE_SHARING")||''; |
| 106 |
if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) |
| 107 |
{ http_die("archive sharing is not allowed") } |
| 108 |
$maccess = 'owner'; |
| 109 |
} else { |
| 110 |
my $pkey = readline1("$sdir/users/$suser/pkey") or |
| 111 |
http_die("unknown share user $suser"); |
| 112 |
if ($key ne md5_hex("$file:$pkey")) { |
| 113 |
http_die("wrong share access key"); |
| 114 |
} |
| 115 |
$maccess = readlink_("$sdir/users/$suser/access"); |
| 116 |
} |
| 117 |
|
| 118 |
if ($RM eq 'HEAD') { |
| 119 |
my $size = -s "$filed/upload" || -s "$filed/data" || 0; |
| 120 |
my $fileid = readlink("$filed/id"); |
| 121 |
my $dkey = readlink("$filed/dkey"); |
| 122 |
nvt_print( |
| 123 |
'HTTP/1.1 200 OK', |
| 124 |
"Content-Length: $size", |
| 125 |
"X-Share-Archive: $filed", |
| 126 |
); |
| 127 |
if ($fileid) { |
| 128 |
nvt_print("X-File-ID: $fileid"); |
| 129 |
} |
| 130 |
if ($dkey and -l "$dkeydir/$dkey") { |
| 131 |
nvt_print("X-Location: $durl/$dkey/$file"); |
| 132 |
} |
| 133 |
nvt_print(''); |
| 134 |
&reexec; |
| 135 |
} |
| 136 |
|
| 137 |
$::owner = $owner; |
| 138 |
$::to = $owner; |
| 139 |
$::from = $owner; |
| 140 |
$::suser = $suser; |
| 141 |
$::share = $share; |
| 142 |
$::archive = "$archive:$avt"; |
| 143 |
$::file = "$sdir/archives/$archive/$avt"; |
| 144 |
} |
| 145 |
|
| 146 |
# download-URI-scheme for share archive request (generic) |
| 147 |
elsif ($file =~ m:(.+?)/(.+?)/(.+?)/(\w+)/(.+):) { |
| 148 |
my $owner = $1; |
| 149 |
my $share = $2; |
| 150 |
my $suser = $3; |
| 151 |
my $key = $4; |
| 152 |
my $archive = $5; |
| 153 |
unless ($owner =~ /\w/ and $share =~ /\w/ and $suser =~ /\w/) { |
| 154 |
http_die("illegal request"); |
| 155 |
} |
| 156 |
$owner .= '@'.$mdomain if $mdomain and $owner !~ /@/; |
| 157 |
$suser .= '@'.$mdomain if $mdomain and $suser !~ /@/; |
| 158 |
my $sdir = "$owner/SHARE/$share"; |
| 159 |
my @archives; |
| 160 |
foreach (glob "$sdir/archives/*/*/data") { |
| 161 |
push @archives,$1 if m:(.*/\Q$archive\E/$vp\.($arx))/:; |
| 162 |
} |
| 163 |
unless (@archives) { |
| 164 |
http_header('404 File not found'); |
| 165 |
print html_header($head), |
| 166 |
"<h3>archive $archive not found</h3>\n", |
| 167 |
"</body></html>\n"; |
| 168 |
exit; |
| 169 |
} |
| 170 |
|
| 171 |
if ($owner eq $suser) { |
| 172 |
my $id = readline1("$owner/@") or |
| 173 |
http_die("unknown owner user $owner"); |
| 174 |
if ($key ne md5_hex("$archive:$id") and |
| 175 |
$key ne md5_hex("$archive:".md5_hex($id))) |
| 176 |
{ http_die("wrong share access key") } |
| 177 |
my $sharing = readlink("$owner/\@ARCHIVE_SHARING")||''; |
| 178 |
if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) |
| 179 |
{ http_die("archive sharing is not allowed") } |
| 180 |
$maccess = 'owner'; |
| 181 |
} else { |
| 182 |
my $pkey = readline1("$sdir/users/$suser/pkey") or |
| 183 |
http_die("unknown share user $suser"); |
| 184 |
if ($key ne md5_hex("$archive:$pkey")) { |
| 185 |
http_die("wrong share access key"); |
| 186 |
} |
| 187 |
$maccess = readlink_("$sdir/users/$suser/access"); |
| 188 |
} |
| 189 |
|
| 190 |
$::owner = $owner; |
| 191 |
$::to = $owner; |
| 192 |
$::from = $owner; |
| 193 |
$::suser = $suser; |
| 194 |
$::share = $share; |
| 195 |
$::file = $archives[-1]; |
| 196 |
$::archive = $archive.':'.basename($::file); |
| 197 |
$qs = ''; |
| 198 |
} |
| 199 |
|
| 200 |
# download-URI-scheme /$dkey/$file ? |
| 201 |
elsif ($file =~ m:^(\w+)/[^/]+$:) { |
| 202 |
$dkey = $1; |
| 203 |
if ($link = readlink("$dkeydir/$dkey")) { |
| 204 |
if ($link eq '/dev/null') { |
| 205 |
http_die("/$file has been withdrawn"); |
| 206 |
} |
| 207 |
$link =~ s:^\.\./:: or http_die("internal error on dkey for $link"); |
| 208 |
$file = untaint($link); |
| 209 |
if ($file =~ m:(.+?)/(.+?)/:) { |
| 210 |
$to = $1; |
| 211 |
$from = $2; |
| 212 |
} |
| 213 |
} else { |
| 214 |
http_die("no such file /$file"); |
| 215 |
} |
| 216 |
} |
| 217 |
# download-URI-scheme /$to/$from/$file |
| 218 |
elsif ($file =~ m:.+/.+/.+:) { |
| 219 |
$file =~ s/\?.*//; |
| 220 |
|
| 221 |
# add domain to addresses if necessary |
| 222 |
if ($file =~ s:(.+?)/(.+?)/(.+):$3:) { |
| 223 |
$to = lc $1; |
| 224 |
$from = lc $2; |
| 225 |
if ($to eq '+') { |
| 226 |
$prefetch = $to = $from; |
| 227 |
} elsif ($to eq '.') { |
| 228 |
$to = $from; |
| 229 |
} else { |
| 230 |
$to =~ s/[:,].*//; |
| 231 |
} |
| 232 |
$to .= '@'.$hostname if $to eq 'anonymous'; |
| 233 |
$from .= '@'.$hostname if $from eq 'anonymous'; |
| 234 |
$to .= '@'.$mdomain if $mdomain and -d "$to\@$mdomain"; |
| 235 |
$from .= '@'.$mdomain if $mdomain and -d "$from\@$mdomain"; |
| 236 |
$file = "$to/$from/$file"; |
| 237 |
} |
| 238 |
|
| 239 |
if ($RM eq 'GET' and -s "$from/\@ALLOWED_RECIPIENTS") { |
| 240 |
http_die("$from is a restricted user"); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if ($file and $to and $from) { |
| 245 |
# afex! |
| 246 |
if ($from =~ s/^(anonymous).*/$1/) { |
| 247 |
if (@anonymous_upload and ipin($RA,@anonymous_upload) or $dkey) { |
| 248 |
$anonymous = $from; |
| 249 |
} else { |
| 250 |
http_header('403 Forbidden'); |
| 251 |
print html_header($head), |
| 252 |
"Your ip $RA has no permission to request the URI $ENV{REQUEST_URI}\n", |
| 253 |
"</body></html>\n"; |
| 254 |
exit; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
$filed = $file; |
| 260 |
$data = "$filed/data"; |
| 261 |
|
| 262 |
# open $file,$file; print Digest::MD5->new->addfile($file)->hexdigest; |
| 263 |
|
| 264 |
unless ($to) { |
| 265 |
http_die("internal error: unknown recipient"); |
| 266 |
} |
| 267 |
|
| 268 |
unless ($from) { |
| 269 |
http_die("internal error: unknown sender"); |
| 270 |
} |
| 271 |
|
| 272 |
&check_status($from); |
| 273 |
|
| 274 |
# server based ip restrictions |
| 275 |
if (@download_hosts and not ipin($RA,@download_hosts)) { |
| 276 |
http_die( |
| 277 |
"Downloads from your host ($RA) are not allowed.", |
| 278 |
"Contact $ENV{SERVER_ADMIN} for details." |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
# user based ip restrictions |
| 283 |
unless (check_rhosts("$to/\@DOWNLOAD_HOSTS")) { |
| 284 |
http_die("You are not allowed to download from IP $RA"); |
| 285 |
} |
| 286 |
|
| 287 |
# file based ip restrictions |
| 288 |
unless (check_rhosts("$file/restrictions")) { |
| 289 |
http_die("Download of files from external user $from is restricted " |
| 290 |
."to internal hosts. Your IP $RA is not allowed."); |
| 291 |
} |
| 292 |
|
| 293 |
# request with ?query-parameter ? |
| 294 |
if ($qs = $ENV{QUERY_STRING}||'') { |
| 295 |
|
| 296 |
http_die("\"$1\" is not allowed in URL") if $qs =~ /([<>\'\"])/; |
| 297 |
|
| 298 |
$qs =~ s/^share=.*//; |
| 299 |
|
| 300 |
# subuser with skey? |
| 301 |
if ($qs =~ s/&*SKEY=([\w:]+)//i) { |
| 302 |
$skey = $1; |
| 303 |
# encrypted skey? |
| 304 |
if ($skey =~ s/^MD5H:(.+)/$1/) { |
| 305 |
# lookup real skey |
| 306 |
foreach my $s (glob "$skeydir/*") { |
| 307 |
$s =~ s:.*/::; |
| 308 |
if ($skey eq md5_hex($s.$ENV{SID})) { |
| 309 |
$skey = $s; |
| 310 |
last; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
if (open $skey,'<',"$skeydir/$skey") { |
| 315 |
$from = $to = ''; |
| 316 |
while (<$skey>) { |
| 317 |
$from = lc($1) if /^from=(.+)/; |
| 318 |
$to = lc($1) if /^to=(.+)/; |
| 319 |
} |
| 320 |
close $skey; |
| 321 |
if ($from and $to) { |
| 322 |
$file =~ s:.*/:$to/$from/:; |
| 323 |
} else { |
| 324 |
http_die("INTERNAL ERROR: missing data in $skeydir/$skey"); |
| 325 |
} |
| 326 |
} else { |
| 327 |
debuglog("SKEY=$skey"); |
| 328 |
http_die("wrong SKEY authentification"); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
# group member with gkey? |
| 333 |
if ($qs =~ s/&*GKEY=([\w:]+)//i) { |
| 334 |
$gkey = $1; |
| 335 |
# encrypted gkey? |
| 336 |
if ($gkey =~ s/^MD5H:(.+)/$1/) { |
| 337 |
# lookup real gkey |
| 338 |
foreach my $g (glob "$gkeydir/*") { |
| 339 |
$g =~ s:.*/::; |
| 340 |
if ($gkey eq md5_hex($g.$ENV{SID})) { |
| 341 |
$gkey = $g; |
| 342 |
last; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
if (open $gkey,'<',"$gkeydir/$gkey") { |
| 347 |
$from = $to = ''; |
| 348 |
while (<$gkey>) { |
| 349 |
$from = lc($1) if /^from=(.+)/; |
| 350 |
$group = lc($1) if /^to=\@(.+)/; |
| 351 |
} |
| 352 |
close $gkey; |
| 353 |
if ($from and $group and open $group,'<',"$from/\@GROUP/$group") { |
| 354 |
while (<$group>) { |
| 355 |
s/#.*//; |
| 356 |
s/\s//g; |
| 357 |
if (/(.+):/) { |
| 358 |
my $to = $1; |
| 359 |
$file =~ s:.*/:$to/$from/:; |
| 360 |
last; |
| 361 |
} |
| 362 |
} |
| 363 |
close $group; |
| 364 |
} else { |
| 365 |
http_die("INTERNAL ERROR: missing data in $gkeydir/$gkey"); |
| 366 |
} |
| 367 |
} else { |
| 368 |
debuglog("GKEY=$gkey"); |
| 369 |
http_die("wrong GKEY authentification"); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
# onetime user with okey? |
| 374 |
if ($qs =~ s/OKEY=(\w+)//i) { |
| 375 |
$okey = $1; |
| 376 |
if ($from = readlink("$to/\@OKEY/$okey")) { |
| 377 |
$from = untaint($from); |
| 378 |
$file =~ s:.*/:$to/$from/:; |
| 379 |
} else { |
| 380 |
debuglog("OKEY=$okey"); |
| 381 |
http_die("unknown OKEY"); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
# check for ID in query |
| 386 |
elsif ($qs =~ s/\&*\bID=([^&]+)//i) { |
| 387 |
$id = $1; |
| 388 |
$fop_auth = 0; |
| 389 |
|
| 390 |
if ($id eq 'PUBLIC') { |
| 391 |
http_header('403 Forbidden'); |
| 392 |
exit; |
| 393 |
} |
| 394 |
|
| 395 |
if ($file =~ m:^(.+)/(.+)/(.+):) { |
| 396 |
$to = $1; |
| 397 |
$from = $2; |
| 398 |
$from =~ s/\s//g; |
| 399 |
if ($to eq '.') { |
| 400 |
$to = $from; |
| 401 |
} else { |
| 402 |
$to =~ s/,+/,/g; |
| 403 |
$to =~ s/\s//g; |
| 404 |
} |
| 405 |
if ($mdomain and $from ne 'anonymous') { |
| 406 |
$to .= '@'.$mdomain if $to !~ /@/; |
| 407 |
$from .= '@'.$mdomain if $from !~ /@/; |
| 408 |
} |
| 409 |
$to = lc $to; |
| 410 |
$from = lc $from; |
| 411 |
} else { |
| 412 |
http_die("unknown file query format"); |
| 413 |
} |
| 414 |
|
| 415 |
# public or anonymous recipient? (needs no auth-ID for sender) |
| 416 |
if ($anonymous or $id eq 'PUBLIC' and |
| 417 |
@public_recipients and grep /^\Q$to\E$/i,@public_recipients) |
| 418 |
{ |
| 419 |
$rid = $id; |
| 420 |
} else { |
| 421 |
open my $idf,'<',"$from/@" or http_die("unknown user $from"); |
| 422 |
$rid = getline($idf); |
| 423 |
close $idf; |
| 424 |
$rid = sidhash($rid,$id); |
| 425 |
} |
| 426 |
|
| 427 |
unless ($id eq $rid) { |
| 428 |
debuglog("real id=$rid, id sent by user=$id"); |
| 429 |
http_die("wrong auth-ID"); |
| 430 |
} |
| 431 |
|
| 432 |
# set akey link for HTTP sessions |
| 433 |
# (need original id for consistant non-moving akey) |
| 434 |
if (-f "$from/@") { |
| 435 |
$akey = genakey($from); |
| 436 |
} |
| 437 |
|
| 438 |
my %to; |
| 439 |
COLLECTTO: foreach my $to (split(',',$to)) { |
| 440 |
if ($to !~ /.@./ and open my $AB,'<',"$from/\@ADDRESS_BOOK") { |
| 441 |
while (<$AB>) { |
| 442 |
s/\s*#.*//; |
| 443 |
s/^\s+//; |
| 444 |
next unless $_; |
| 445 |
if (/^\s*([\S]+)\s+([\S]+)/) { |
| 446 |
my ($alias,$address) = ($1,$2); |
| 447 |
if ($to =~ /^\Q$alias\E$/i) { |
| 448 |
foreach my $to (split(",",$address)) { |
| 449 |
$to .= '@'.$mdomain if $mdomain and $to !~ /@/; |
| 450 |
$to{$to} = lc $to; # ignore dupes |
| 451 |
} |
| 452 |
next COLLECTTO; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} elsif ($to =~ /^\@(.+)/) { |
| 457 |
my $group = "$from/\@GROUP/$1"; |
| 458 |
if (not -l $group and open $group) { |
| 459 |
while (<$group>) { |
| 460 |
s/#.*//; |
| 461 |
s/\s//g; |
| 462 |
if (/(.+\@[w.-]+):.+/) { |
| 463 |
$to{$1} = lc $1; # ignore dupes |
| 464 |
} |
| 465 |
} |
| 466 |
close $group; |
| 467 |
} |
| 468 |
} else { |
| 469 |
$to .= '@'.$mdomain if $mdomain and $to !~ /.@./; |
| 470 |
$to{$to} = lc $to; # ignore dupes |
| 471 |
} |
| 472 |
} |
| 473 |
foreach $to (keys %to) { |
| 474 |
# if (-e "$to/\@CAPTIVE") { http_die("$to is CAPTIVE") } |
| 475 |
unless (-d $to or checkaddress($to)) { |
| 476 |
http_die("$to is not a legal email address"); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
if ($qs =~ /\&?KEEP=(\d+)/i) { |
| 483 |
$keep = $1; |
| 484 |
$filename = filename($file); |
| 485 |
check_captive($file); |
| 486 |
if (-f $data) { |
| 487 |
unlink "$file/keep"; |
| 488 |
if (symlink $keep,"$file/keep") { |
| 489 |
http_header('200 OK'); |
| 490 |
print html_header($head), |
| 491 |
"<h3>set keep=$keep for $filename</h3>\n", |
| 492 |
"</body></html>\n"; |
| 493 |
} else { |
| 494 |
http_header('599 internal error'); |
| 495 |
print html_header($head), |
| 496 |
"<h3>$filename - $!</h3>\n", |
| 497 |
"</body></html>\n"; |
| 498 |
} |
| 499 |
} else { |
| 500 |
http_header('404 File not found'); |
| 501 |
print html_header($head), |
| 502 |
"<h3>$filename not found</h3>\n", |
| 503 |
"</body></html>\n"; |
| 504 |
} |
| 505 |
exit; |
| 506 |
} elsif ($qs =~ s/\&?KEEP//i) { |
| 507 |
check_captive($file); |
| 508 |
$autodelete = 'NO'; |
| 509 |
} |
| 510 |
|
| 511 |
if ($qs =~ s/\&?FILEID=(\w+)//i) { $fileid = $1 } |
| 512 |
|
| 513 |
if ($qs =~ s/\&?IGNOREWARNING//i) { $ignorewarning = 1 } |
| 514 |
|
| 515 |
if ($qs eq 'LIST') { |
| 516 |
http_header('200 OK','Content-Type: text/plain'); |
| 517 |
print "$file :\n"; |
| 518 |
chdir $file and exec 'l'; |
| 519 |
exit; |
| 520 |
} |
| 521 |
|
| 522 |
# copy file to yourself |
| 523 |
if ($qs eq 'COPY') { |
| 524 |
unless (-f "$file/data") { |
| 525 |
http_die("File not found."); |
| 526 |
} |
| 527 |
($to,$from,$file) = split('/',$file); |
| 528 |
unless ("$to/@") { |
| 529 |
# http_header('403 Forbidden'); |
| 530 |
# print html_header($head), |
| 531 |
# "You have no permission to copy a file.\n", |
| 532 |
# "</body></html>\n"; |
| 533 |
http_die("You have no permission to copy a file."); |
| 534 |
} |
| 535 |
if (-s "$to/\@ALLOWED_RECIPIENTS") { |
| 536 |
http_die("You are a restricted user."); |
| 537 |
} |
| 538 |
if (-e "$to/$to/$file/data") { |
| 539 |
# http_header('409 File Exists'); |
| 540 |
# print html_header($head), |
| 541 |
# "File $file already exists in your outgoing spool.\n", |
| 542 |
# "</body></html>\n"; |
| 543 |
http_die("File $file already exists in your outgoing spool."); |
| 544 |
} |
| 545 |
mkdirp("$to/$to/$file"); |
| 546 |
link "$to/$from/$file/data","$to/$to/$file/data" |
| 547 |
or http_die("cannot link to $to/$to/$file/data - $!\n"); |
| 548 |
my $fkey = copy("$to/$from/$file/filename","$to/$to/$file/filename"); |
| 549 |
open my $notify,'>',"$to/$to/$file/notify"; |
| 550 |
close $notify; |
| 551 |
my $dkey = randstring(8); |
| 552 |
unlink "$to/$to/$file/dkey","$dkeydir/$dkey"; |
| 553 |
symlink "../$to/$to/$file","$dkeydir/$dkey"; |
| 554 |
symlink $dkey,"$to/$to/$file/dkey"; |
| 555 |
http_header('200 OK',"Location: $durl/$dkey/$fkey"); |
| 556 |
print html_header($head), |
| 557 |
"File $file copied to yourself.\n", |
| 558 |
"</body></html>\n"; |
| 559 |
exit; |
| 560 |
} |
| 561 |
|
| 562 |
# ex and hopp? |
| 563 |
if ($qs =~ s/(^|&)DELETE//i) { |
| 564 |
if ($from eq $to and not($id and $rid and $id eq $rid or $share)) { |
| 565 |
http_die("need auth-ID"); |
| 566 |
} |
| 567 |
if (unlink $data) { |
| 568 |
$filename = filename($file); |
| 569 |
if (open my $log,'>',"$file/error") { |
| 570 |
printf {$log} "%s has been deleted by %s at %s\n", |
| 571 |
$filename,$RA,isodate(time); |
| 572 |
close $log; |
| 573 |
} |
| 574 |
foreach my $logdir (@logdir) { |
| 575 |
my $msg = sprintf "%s [%s_%s] %s %s deleted\n", |
| 576 |
isodate(time),$$,$ENV{REQUESTCOUNT},$RA,encode_Q($file); |
| 577 |
if (open $log,'>>',"$logdir/$log") { |
| 578 |
print {$log} $msg; |
| 579 |
close $log; |
| 580 |
} |
| 581 |
} |
| 582 |
if ($share and $archive) { |
| 583 |
rmrf($file); |
| 584 |
rmdir(dirname($file)); |
| 585 |
faslog("$owner/SHARE/$share","$suser deleted $archive"); |
| 586 |
} |
| 587 |
http_header('200 OK',"X-File: $file"); |
| 588 |
print html_header($head), |
| 589 |
"<h3>$filename deleted</h3>\n", |
| 590 |
"</body></html>\n"; |
| 591 |
exit; |
| 592 |
} else { |
| 593 |
http_die("no such file"); |
| 594 |
} |
| 595 |
exit; |
| 596 |
} |
| 597 |
|
| 598 |
# wipe out!? (for anonymous upload) |
| 599 |
if ($qs =~ s/(^|&)PURGE//i) { |
| 600 |
$filename = filename($file); |
| 601 |
if (@anonymous_upload and ipin($RA,@anonymous_upload)) { |
| 602 |
unlink "$dkeydir/$dkey" if $dkey; |
| 603 |
if (rmrf($file)) { |
| 604 |
foreach my $logdir (@logdir) { |
| 605 |
my $msg = sprintf "%s [%s_%s] %s %s purged\n", |
| 606 |
isodate(time),$$,$ENV{REQUESTCOUNT},$RA,encode_Q($file); |
| 607 |
if (open $log,'>>',"$logdir/$log") { |
| 608 |
print {$log} $msg; |
| 609 |
close $log; |
| 610 |
} |
| 611 |
} |
| 612 |
http_header('200 OK',"X-File: $file"); |
| 613 |
print html_header($head), |
| 614 |
"<h3>$filename purged</h3>\n", |
| 615 |
"</body></html>\n"; |
| 616 |
} else { |
| 617 |
http_die("no such file"); |
| 618 |
} |
| 619 |
} else { |
| 620 |
http_die("you are not allowed to purge $filename"); |
| 621 |
} |
| 622 |
exit; |
| 623 |
} |
| 624 |
|
| 625 |
# request for file size? |
| 626 |
if ($qs eq '?') { |
| 627 |
sendsize($file); |
| 628 |
# control back to fexsrv for further HTTP handling |
| 629 |
&reexec; |
| 630 |
} |
| 631 |
|
| 632 |
# reject unknown URL parameters |
| 633 |
if ($qs and $qs !~ /^(DOWNLOAD|EXTRACT|LISTARCHIVE|!)/) { |
| 634 |
http_die("unknown query format $qs"); |
| 635 |
} |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
unless ($id and $rid and $id eq $rid or $dkey or $anonymous or $share) { |
| 640 |
http_die("wrong parameter $file"); |
| 641 |
} |
| 642 |
|
| 643 |
if ($from and $file eq "$from/$from/ADDRESS_BOOK") { |
| 644 |
if (open my $AB,'<',"$from/\@ADDRESS_BOOK") { |
| 645 |
my $ab = ''; |
| 646 |
while (<$AB>) { |
| 647 |
s/^\s+//; |
| 648 |
s/\s+$//; |
| 649 |
s/[\r\n]//g; |
| 650 |
$ab .= $_."\r\n"; |
| 651 |
} |
| 652 |
close $AB; |
| 653 |
nvt_print( |
| 654 |
'HTTP/1.1 200 OK', |
| 655 |
'Content-Length: ' . length($ab), |
| 656 |
'Content-Type: text/plain', |
| 657 |
'' |
| 658 |
); |
| 659 |
print $ab; |
| 660 |
} else { |
| 661 |
nvt_print( |
| 662 |
'HTTP/1.1 404 No address book found', |
| 663 |
'Content-Length: 0', |
| 664 |
'' |
| 665 |
); |
| 666 |
} |
| 667 |
# control back to fexsrv for further HTTP handling |
| 668 |
&reexec; |
| 669 |
} |
| 670 |
|
| 671 |
if (-f $data) { |
| 672 |
# already downloaded? |
| 673 |
if ($limited_download and $limited_download !~ /^n/i |
| 674 |
and $from ne $to # fex to yourself is ok! |
| 675 |
and $from !~ /^_?fexmail/ # fexmail is ok! |
| 676 |
and $to !~ /^_?fexmail/ # fexmail is ok! |
| 677 |
and $to !~ /^anonymous/ # anonymous fex is ok! |
| 678 |
and $to !~ /$amdl/ # allowed multi download recipients |
| 679 |
and $http_client !~ /$adlm/ # allowed download managers |
| 680 |
and $file !~ /\/STDFEX$/ # xx is ok! |
| 681 |
and $file !~ /\/SHARE\// # sharing is ok! |
| 682 |
and (slurp("$file/comment")||'') !~ /^!\*!/ # multi download allow flag |
| 683 |
and not($dkey and ($ENV{HTTP_COOKIE}||'') =~ /dkey=$dkey/) |
| 684 |
and open $file,'<',"$file/download") |
| 685 |
{ |
| 686 |
my $d1 = <$file> || ''; # first download |
| 687 |
chomp $d1; |
| 688 |
close $file; |
| 689 |
if ($RA) { |
| 690 |
# allow downloads from same ip |
| 691 |
$d1 = '' if $d1 =~ /\Q$RA/; |
| 692 |
# allow downloads from sender ip |
| 693 |
$d1 = '' if (readlink("$file/ip")||'') eq $RA; |
| 694 |
# allow downloads from same hostname (with different ips) |
| 695 |
if ($d1 =~ / ([\w.:]+)$/) { |
| 696 |
my $da = $1; |
| 697 |
my ($dh,$rh); |
| 698 |
$dh = $1 if `host $da 2>/dev/null` =~ / pointer (.+)/; |
| 699 |
$rh = $1 if `host $RA 2>/dev/null` =~ / pointer (.+)/; |
| 700 |
$d1 = '' if $dh and $rh and $dh eq $rh; |
| 701 |
} |
| 702 |
} |
| 703 |
if ($d1 and $d1 =~ s/(.+) ([\w.:]+)$/$2 at $1/) { |
| 704 |
$file = filename($file); |
| 705 |
http_die("$file has already been downloaded by $d1"); |
| 706 |
} |
| 707 |
} |
| 708 |
} elsif (-l $data) { |
| 709 |
# $file =~ s:.*/::; |
| 710 |
http_die("<code>$file</code> has been withdrawn"); |
| 711 |
} elsif (my $err = readline1("$file/error")) { |
| 712 |
fdlog($log,$file,0,0); |
| 713 |
http_die($err); |
| 714 |
} elsif (not -f "$file/data" and $_ = readline1("$file/download")) { |
| 715 |
# fallback because of bug in old fex_cleanup |
| 716 |
fdlog($log,$file,0,0); |
| 717 |
my $err = $_; |
| 718 |
if (/^([\d-]+ [\d:]+) (\S+)/) { |
| 719 |
$err = sprintf "%s has been autodeleted after download from %s at %s\n", |
| 720 |
filename($file),$2,$1; |
| 721 |
} |
| 722 |
http_die($err); |
| 723 |
} else { |
| 724 |
fdlog($log,$file,0,0); |
| 725 |
if ($file =~ /^anonymous.*afex_\d+\.tar$/) { |
| 726 |
# should be extra handled... |
| 727 |
} |
| 728 |
http_die("no such file $file"); |
| 729 |
} |
| 730 |
|
| 731 |
$alist = "$filed/alist"; |
| 732 |
|
| 733 |
if ($filed =~ /\.(zip|7z)$/ and -s $alist and -s $data and |
| 734 |
$qs =~ /EXTRACT=(\d+):(.+)/) |
| 735 |
{ |
| 736 |
my $size = $1; |
| 737 |
my $file = $2; |
| 738 |
my $data = "$filed/data"; |
| 739 |
my $efile = "$filed:$file"; |
| 740 |
$file =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge; |
| 741 |
close STDERR; |
| 742 |
if ($filed =~ /\.zip$/) { |
| 743 |
open $data,'-|',qw'unzip -p',$data,$file |
| 744 |
or http_die("cannot unzip $filed - $!\n"); |
| 745 |
} |
| 746 |
if ($filed =~ /\.7z$/) { |
| 747 |
open $data,'-|',qw'7z e -so',$data,$file |
| 748 |
or http_die("cannot unzip $filed - $!\n"); |
| 749 |
} |
| 750 |
my $x; |
| 751 |
if (read($data,$x,$bs)) { |
| 752 |
nvt_print('HTTP/1.1 200 OK'); |
| 753 |
nvt_print("Content-Length: $size"); |
| 754 |
if ($file =~ /\.(png|jpg|gif|tif|pbm)$/i) { |
| 755 |
nvt_print("Content-Type: image/$1"); |
| 756 |
} elsif ($file =~ /\.(pdf)$/i) { |
| 757 |
nvt_print("Content-Type: application/$1"); |
| 758 |
} elsif ($file =~ /\.txt$/i) { |
| 759 |
nvt_print("Content-Type: text/plain"); |
| 760 |
} else { |
| 761 |
nvt_print("Content-Type: application/binary"); |
| 762 |
nvt_print("Content-Disposition: attachment; filename=\"$file\""); |
| 763 |
} |
| 764 |
nvt_print("Connection: close",''); |
| 765 |
print $x; |
| 766 |
print $x while read($data,$x,$bs); |
| 767 |
} else { |
| 768 |
http_die("$file not found in $filed"); |
| 769 |
} |
| 770 |
my $download = "$filed/download"; |
| 771 |
if (open $download,'>>',$download) { |
| 772 |
printf {$download} "%s %s\n",isodate(time),$RA; |
| 773 |
close $download; |
| 774 |
} |
| 775 |
fdlog($log,$efile,$size,-s $data); |
| 776 |
exit; |
| 777 |
} |
| 778 |
|
| 779 |
if (-s $alist and -s $data and $qs !~ /DOWNLOAD/) { |
| 780 |
my $webbrowser = |
| 781 |
'Mozilla|Firefox|AppleWebKit|Gecko|Chrome|Safari|Opera|MSIE|w3m|Lynx'; |
| 782 |
my $url = $ENV{REQUEST_URL}; |
| 783 |
my $rdkey = ' '; |
| 784 |
my $listarchive = $qs eq 'LISTARCHIVE'; |
| 785 |
$rdkey = $1 if ($ENV{HTTP_HEADER}||'') =~ m{\nReferer:.*/fop/(.+)/}; |
| 786 |
|
| 787 |
if (($http_client =~ /$webbrowser/ and |
| 788 |
$http_client !~ /Thunderbird/ and |
| 789 |
$url !~ /$rdkey/ |
| 790 |
or $qs eq '!' or $listarchive) |
| 791 |
and open $alist,$alist) |
| 792 |
{ |
| 793 |
my $size = int((-s $data||0)/$MB+0.5)||'<1'; |
| 794 |
# my $date = isodate((stat($file))[9]); |
| 795 |
my $comment = htmlquote(slurp("$filed/comment")||''); |
| 796 |
$comment =~ s/^!.+?!\s*//; |
| 797 |
$comment =~ s/^NOMAIL//; |
| 798 |
$comment =~ s/\n/ /g; |
| 799 |
$comment =~ s/\s+$//g; |
| 800 |
$url =~ s/\?.*//; |
| 801 |
if ($listarchive) { |
| 802 |
http_header('200 OK'); |
| 803 |
print "<html><body>\n"; |
| 804 |
} elsif ($filed =~ m:(.+?)/SHARE/(.+?)/archives/(.+)/(.+):) { |
| 805 |
my $owner = $1; |
| 806 |
my $share = $2; |
| 807 |
my $archive = $3; |
| 808 |
my $vt = $4; |
| 809 |
my $file = $3.'_'.$4; |
| 810 |
my $uploader = readlink("$filed/uploader")||$owner; |
| 811 |
my $fas= "/fas?owner=$owner?share=$share"; |
| 812 |
my $referer = $ENV{HTTP_REFERER}||''; |
| 813 |
my $mr = ''; |
| 814 |
if ($referer =~ /(user=.*pkey=\w+)/) { |
| 815 |
$fas .= "?$1" |
| 816 |
} elsif ($referer =~ m:/fas/.+/(.+@.+)/(\w+)$:) { |
| 817 |
$fas .= "?user=$1?pkey=$2" |
| 818 |
} |
| 819 |
if (0 and $maccess =~ /owner|manage/) { |
| 820 |
my $formparam = |
| 821 |
'method="post" accept-charset="UTF-8" enctype="multipart/form-data"'; |
| 822 |
$comment = qqq(qq( |
| 823 |
'<form action="$fas?modify=comment:$archive:$vt" $formparam>' |
| 824 |
'<input type="text" name="comment" value="$comment" size="80">' |
| 825 |
'<input type="submit" value="save">' |
| 826 |
'</form>' |
| 827 |
)); |
| 828 |
} |
| 829 |
if ($comment =~ /\S/) { |
| 830 |
$comment = "<tr><td><b>comment:</b><td><b>$comment</b></tr>"; |
| 831 |
} else { |
| 832 |
$comment = ''; |
| 833 |
} |
| 834 |
my @archives = glob(dirname($filed).'/*_*/data'); |
| 835 |
if (@archives and $archives[-1] eq "$filed/data") { |
| 836 |
$mr = ' (most recent)'; |
| 837 |
} |
| 838 |
http_header('200 OK'); |
| 839 |
print html_header("<a href=\"$url?DOWNLOAD=$file\">Download: $file</a>"); |
| 840 |
pq(qq( |
| 841 |
'<table>' |
| 842 |
'<tr><td><b>archive:</b><td><b><a href="$url">$file</a></b>$mr</tr>' |
| 843 |
'<tr><td><b>size:</b><td><b>$size MB</b></tr>' |
| 844 |
'<tr><td><b>share:</b><td><b>$share</b></tr>' |
| 845 |
'<tr><td><b>owner:</b><td><b>$owner</b></tr>' |
| 846 |
'<tr><td><b>uploader:</b><td><b>$uploader</b></tr>' |
| 847 |
'$comment' |
| 848 |
'</table>' |
| 849 |
'<p>' |
| 850 |
)); |
| 851 |
if ($akey and readlink_("$akeydir/$akey") =~ m:/\Q$owner\E$: ) { |
| 852 |
pq(qq( |
| 853 |
'[<a href="$fas?show=archive:$share:$archive">show all versions</a>]' |
| 854 |
'[<a href="$fas?show=share:$share:archives">show share</a>]' |
| 855 |
'<p>' |
| 856 |
)); |
| 857 |
} |
| 858 |
} elsif ($filed =~ m:(.+?)/(.+?)/(.+):) { |
| 859 |
my $to = $1; |
| 860 |
my $from = $2; |
| 861 |
my $file = $3; |
| 862 |
http_header('200 OK'); |
| 863 |
print html_header("<a href=\"$url?DOWNLOAD=$file\">Download: $file</a>"); |
| 864 |
pq(qq( |
| 865 |
'<table>' |
| 866 |
'<!-- file: $file -->' |
| 867 |
'<tr><td><b>File:</b><td><b><a href=\"$url\">$file</a></b></tr>' |
| 868 |
'<!-- size: $size MB -->' |
| 869 |
'<tr><td><b>Size:</b><td><b>$size MB</b></tr>' |
| 870 |
'<!-- sender: $from -->' |
| 871 |
'<tr><td><b>Sender:</b><td><b>$from</b></tr>' |
| 872 |
'<!-- recipient: $to -->' |
| 873 |
'<tr><td><b>Recipient:</b><td><b>$to</b></tr>' |
| 874 |
)); |
| 875 |
if ($comment =~ /\S/) { |
| 876 |
pq(qq( |
| 877 |
'<!-- comment: $comment -->' |
| 878 |
'<tr><td><b>comment:</b><td><b>$comment</b></tr>' |
| 879 |
)); |
| 880 |
} |
| 881 |
print "</table>\n"; |
| 882 |
} else { |
| 883 |
http_die("INTERNAL ERROR: file=$filed ?!"); |
| 884 |
} |
| 885 |
pq(qq( |
| 886 |
'<h3>Archive content:</h3>' |
| 887 |
'<p>' |
| 888 |
'<pre>' |
| 889 |
)); |
| 890 |
while (<$alist>) { |
| 891 |
s/&/&/g; |
| 892 |
s/</</g; |
| 893 |
unless ($listarchive or m:/$:) { |
| 894 |
if ($filed =~ /\.zip$/) { |
| 895 |
s/ (\d\d)-(\d\d)-(\d\d\d\d) / $3-$1-$2 /; |
| 896 |
s/^(\s+(\d+)\s+[\d-]+ \d\d:\d\d )(.+)/$1<a href="$url?EXTRACT=$2:$3">$3<\/a>/; |
| 897 |
} |
| 898 |
if ($filed =~ /\.7z$/) { |
| 899 |
s/^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d +(\d+) )(.+)/$1<a href="$url?EXTRACT=$2:$3">$3<\/a>/; |
| 900 |
} |
| 901 |
# rename file so that a webbrowser can save its name |
| 902 |
if (/\?EXTRACT=(.+?)\"/) { |
| 903 |
my $x = $1; |
| 904 |
$x =~ s|.*[:/]||; |
| 905 |
if ($x =~ /\.(png|jpg|gif|tif|pbm|txt|pdf)$/i) { |
| 906 |
s/[^\/]+\?/$x?/; |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
print; |
| 911 |
} |
| 912 |
print "</pre>\n"; |
| 913 |
print "</body></html>\n"; |
| 914 |
exit; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
# set time mark for this access |
| 919 |
if ($file =~ m:(.+?)/:) { |
| 920 |
my $user = $1; |
| 921 |
my $time = untaint(time); |
| 922 |
utime $time,$time,$user; |
| 923 |
} |
| 924 |
|
| 925 |
# reget or range? |
| 926 |
if ($range = $ENV{HTTP_RANGE}) { |
| 927 |
$seek = $1 if $range =~ /^bytes=(\d+)-/i; |
| 928 |
$stop = $1 if $range =~ /^bytes=\d*-(\d+)/i; |
| 929 |
} else { |
| 930 |
$seek = 0; |
| 931 |
$stop = 0; |
| 932 |
} |
| 933 |
|
| 934 |
if (not $autodelete or $autodelete ne 'NO') { |
| 935 |
$autodelete = readlink "$file/autodelete" || 'YES'; |
| 936 |
} |
| 937 |
$autodelete = 'NO' if $file =~ m:/SHARE/: and not $dkey; |
| 938 |
|
| 939 |
$sb = sendfile($file,$seek,$stop); |
| 940 |
shutdown(STDOUT,2); |
| 941 |
|
| 942 |
debuglog(sprintf("%s %s %d %d %d", |
| 943 |
isodate(time),$file,$sb||0,$seek,(-s $data)||0)); |
| 944 |
|
| 945 |
if ($sb+$seek == (-s $data)||0) { |
| 946 |
|
| 947 |
# note successfull download |
| 948 |
$download = "$file/download"; |
| 949 |
if (open $download,'>>',$download) { |
| 950 |
printf {$download} "%s %s\n",isodate(time),$RA; |
| 951 |
close $download; |
| 952 |
} |
| 953 |
if ($share and $archive) { |
| 954 |
faslog("$owner/SHARE/$share","$suser downloaded $archive"); |
| 955 |
} |
| 956 |
|
| 957 |
# delete file after grace period |
| 958 |
if (uc($autodelete) eq 'YES') { |
| 959 |
$grace_time = 60 unless defined $grace_time; |
| 960 |
for (;;) { |
| 961 |
my $utime = (stat $data)[8] || 0; |
| 962 |
my $dtime = (stat $download)[8] || 0; |
| 963 |
exit if $utime > $dtime; |
| 964 |
last if time > $dtime+$grace_time; |
| 965 |
sleep 10; |
| 966 |
} |
| 967 |
unlink $data; |
| 968 |
my $error = "$file/error"; |
| 969 |
if (open $error,'>',$error) { |
| 970 |
printf {$error} "%s has been autodeleted after download from %s at %s\n", |
| 971 |
filename($file),$RA,isodate(time); |
| 972 |
close $error; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
} |
| 977 |
|
| 978 |
exit; |
| 979 |
|
| 980 |
|
| 981 |
sub sendfile { |
| 982 |
my ($file,$seek,$stop) = @_; |
| 983 |
my ($filename,$size,$total_size,$fileid,$filetype,$upload); |
| 984 |
my ($data,$download,$header,$buf,$range,$s,$b,$t0); |
| 985 |
my $type = ''; |
| 986 |
|
| 987 |
# swap to and from for special senders, see fup storage swap! |
| 988 |
$file =~ s:^(_?anonymous_.*)/(anonymous.*)/:$2/$1/:; |
| 989 |
$file =~ s:^(_?fexmail_.*)/(fexmail.*)/:$2/$1/:; |
| 990 |
|
| 991 |
$data = $file.'/data'; |
| 992 |
$download = $file.'/download'; |
| 993 |
$header = $file.'/header'; |
| 994 |
|
| 995 |
# fallback defaults, should be set later with better values |
| 996 |
$filename = filename($file); |
| 997 |
$total_size = -s $data || 0; |
| 998 |
|
| 999 |
# file link? |
| 1000 |
if (-l $data) { |
| 1001 |
# prefetch? (fexsync, etc) |
| 1002 |
if (readlink($data) eq 'upload') { |
| 1003 |
if (open $data,$data and flock($data,LOCK_EX|LOCK_NB)) { |
| 1004 |
$file =~ m:(.+)/.+/(.+):; |
| 1005 |
http_die("$1 has stopped uploading $2"); |
| 1006 |
} |
| 1007 |
close $data; |
| 1008 |
$total_size = readlink "$file/size"; |
| 1009 |
$upload = "$file/upload"; |
| 1010 |
} else { |
| 1011 |
unless (-f $data and -r $data) { |
| 1012 |
http_die("<code>$file</code> has been withdrawn"); |
| 1013 |
} |
| 1014 |
$data = abs_path($data); |
| 1015 |
my $fok; |
| 1016 |
foreach (@file_link_dirs) { |
| 1017 |
my $dir = abs_path($_); |
| 1018 |
$fok = $data if $data =~ /^\Q$dir\//; |
| 1019 |
} |
| 1020 |
unless ($fok) { |
| 1021 |
http_die("no permission to download <code>$file</code>"); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} else { |
| 1025 |
unless (-f $data and -r $data) { |
| 1026 |
http_die("<code>$file</code> has gone"); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
if ($RM eq 'GET') { |
| 1031 |
debuglog("Exp: FROM=\"$from\"","Exp: TO=\"$to\""); |
| 1032 |
open $data,$data or http_die("cannot read <code>$file</code>"); |
| 1033 |
flock($data,LOCK_EX|LOCK_NB) unless $upload; |
| 1034 |
|
| 1035 |
# security check: must be regular file after abs_path() |
| 1036 |
if (-l $data and not $upload) { |
| 1037 |
http_die("no permission to download <code>$file</code>"); |
| 1038 |
} |
| 1039 |
# HTTP Range download suckers are already rejected by fexsrv |
| 1040 |
unless ($range = $ENV{HTTP_RANGE}) { |
| 1041 |
# download lock |
| 1042 |
open $download,'>>',$download or die "$download - $!\n"; |
| 1043 |
if ($file =~ m:(.+?)/(.+?)/: and $1 ne $2) { |
| 1044 |
# only one concurrent download is allowed if sender <> recipient |
| 1045 |
flock($download,LOCK_EX|LOCK_NB) or |
| 1046 |
http_die("$file locked: a download is already in progress"); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
$size = $total_size - $seek - ($stop ? $total_size-$stop-1 : 0); |
| 1050 |
} elsif ($RM eq 'HEAD') { |
| 1051 |
$size = -s $data || 0; |
| 1052 |
} else { |
| 1053 |
http_die("unknown HTTP request method $RM"); |
| 1054 |
} |
| 1055 |
|
| 1056 |
# read MIME entity header (what the client said) |
| 1057 |
if (open $header,'<',$header) { |
| 1058 |
while (<$header>) { |
| 1059 |
if (/^Content-Type: (.+)/i) { |
| 1060 |
$type = $1; |
| 1061 |
last; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
close $header; |
| 1065 |
$type =~ s/\s//g; |
| 1066 |
} |
| 1067 |
|
| 1068 |
$fileid = readlink "$file/id" || ''; |
| 1069 |
|
| 1070 |
# determine own MIME entity header for download |
| 1071 |
my $mime = $file; |
| 1072 |
$mime =~ s:/.*:/\@MIME:; |
| 1073 |
my $mt = $ENV{FEXHOME}.'/etc/mime.types'; |
| 1074 |
if (($type =~ /x-mime/i or -e $mime) and open $mt,'<',$mt) { |
| 1075 |
$type = 'application/octet-stream'; |
| 1076 |
MIMETYPES: while (<$mt>) { |
| 1077 |
chomp; |
| 1078 |
s/#.*//; |
| 1079 |
s/^\s+//; |
| 1080 |
my ($mt,@ft) = split; |
| 1081 |
foreach my $ft (@ft) { |
| 1082 |
if ($filename =~ /\.\Q$ft\E$/i) { |
| 1083 |
$type = $mt; |
| 1084 |
last MIMETYPES; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
} |
| 1088 |
close $mt; |
| 1089 |
} |
| 1090 |
# reset to default MIME type |
| 1091 |
else { $type = 'application/octet-stream' } |
| 1092 |
|
| 1093 |
# HTML is not allowed for security reasons! (embedded javascript, etc) |
| 1094 |
$type =~ s/html/plain/i; |
| 1095 |
|
| 1096 |
debuglog("download with $http_client"); |
| 1097 |
|
| 1098 |
if ($seek or $stop) { |
| 1099 |
if ($size < 0) { |
| 1100 |
http_header('416 Requested Range Not Satisfiable'); |
| 1101 |
exit; |
| 1102 |
} |
| 1103 |
if ($stop) { |
| 1104 |
$range = sprintf("bytes %s-%s/%s",$seek,$stop,$total_size); |
| 1105 |
} else { |
| 1106 |
$range = sprintf("bytes %s-%s/%s",$seek,$total_size-1,$total_size); |
| 1107 |
} |
| 1108 |
# RFC 7233 "Responses to a Range Request" |
| 1109 |
nvt_print( |
| 1110 |
'HTTP/1.1 206 Partial Content', |
| 1111 |
"Content-Length: $size", |
| 1112 |
"Content-Range: $range", |
| 1113 |
"Content-Type: $type", |
| 1114 |
); |
| 1115 |
if ($http_client !~ /MSIE/) { |
| 1116 |
nvt_print("Cache-Control: no-cache"); |
| 1117 |
if ($type eq 'application/octet-stream') { |
| 1118 |
nvt_print("Content-Disposition: attachment; filename=\"$filename\""); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
nvt_print(''); |
| 1122 |
} else { |
| 1123 |
# another stupid IE bug-workaround |
| 1124 |
# http://drupal.org/node/163445 |
| 1125 |
# http://support.microsoft.com/kb/323308 |
| 1126 |
if ($http_client =~ /MSIE/ and not $nowarning) { |
| 1127 |
# $type = 'application/x-msdownload'; |
| 1128 |
if ($ignorewarning) { |
| 1129 |
$type .= "; filename=$filename"; |
| 1130 |
nvt_print( |
| 1131 |
'HTTP/1.1 200 OK', |
| 1132 |
"Content-Length: $size", |
| 1133 |
"Content-Type: $type", |
| 1134 |
# "Pragma: no-cache", |
| 1135 |
# "Cache-Control: no-store", |
| 1136 |
"Content-Disposition: attachment; filename=\"$filename\"", |
| 1137 |
"Connection: close", |
| 1138 |
); |
| 1139 |
# nvt_print('','HTTP/1.1 200 OK',"Content-Length: $size","Content-Type: $type"); exit; |
| 1140 |
nvt_print($_) foreach(@extra_header); |
| 1141 |
} else { |
| 1142 |
http_header('200 OK'); |
| 1143 |
print html_header($head); |
| 1144 |
pq(qq( |
| 1145 |
'<h2>Internet Explorer warning</h2>' |
| 1146 |
'Using Microsoft Internet Explorer for download will probably' |
| 1147 |
'lead to problems, because it is not Internet compatible (RFC 2616).' |
| 1148 |
'<p>' |
| 1149 |
'We recommend <a href="http://firefox.com">Firefox</a>' |
| 1150 |
'<p>' |
| 1151 |
'If you really want to continue with Internet Explorer, then' |
| 1152 |
'<a href="$ENV{REQUEST_URL}?IGNOREWARNING">' |
| 1153 |
'click here with your right mouse button and select "save as"' |
| 1154 |
'</a>' |
| 1155 |
'<p>' |
| 1156 |
'See also <a href="/FAQ/user.html">F*EX user FAQ</a>.' |
| 1157 |
'</body></html>' |
| 1158 |
)); |
| 1159 |
&reexec; |
| 1160 |
} |
| 1161 |
} else { |
| 1162 |
nvt_print( |
| 1163 |
'HTTP/1.1 200 OK', |
| 1164 |
"Content-Length: $size", |
| 1165 |
"Content-Type: $type", |
| 1166 |
"Cache-Control: no-cache", |
| 1167 |
"Connection: close", |
| 1168 |
); |
| 1169 |
if ($type eq 'application/octet-stream') { |
| 1170 |
nvt_print(qq'Content-Disposition: attachment; filename="$filename"'); |
| 1171 |
} |
| 1172 |
nvt_print($_) foreach(@extra_header); |
| 1173 |
} |
| 1174 |
|
| 1175 |
nvt_print("X-Size: $total_size"); |
| 1176 |
nvt_print("X-File-ID: $fileid") if $fileid; |
| 1177 |
# if ((`file "$file/data" 2>/dev/null` || '') =~ m{.*/data:\s(.+)}) { |
| 1178 |
# nvt_print("X-File-Type: $1"); |
| 1179 |
# } |
| 1180 |
if ($dkey = $dkey||readlink "$file/dkey") { |
| 1181 |
my $ma = (readlink "$file/keep"||$keep_default)*60*60*24; |
| 1182 |
my $uri = $ENV{REQUEST_URI}; |
| 1183 |
$uri =~ s/\?.*//; |
| 1184 |
nvt_print("Set-Cookie: dkey=$dkey; Max-Age=$ma; Path=$uri"); |
| 1185 |
} |
| 1186 |
# nvt_print("X-RM: $RM"); |
| 1187 |
nvt_print(''); |
| 1188 |
} |
| 1189 |
|
| 1190 |
if ($RM eq 'HEAD') { |
| 1191 |
# control back to fexsrv for further HTTP handling |
| 1192 |
&reexec; |
| 1193 |
} |
| 1194 |
|
| 1195 |
if ($RM eq 'GET') { |
| 1196 |
|
| 1197 |
if (@throttle) { |
| 1198 |
my $to = $file; |
| 1199 |
$to =~ s:/.*::; |
| 1200 |
foreach (@throttle) { |
| 1201 |
if (/(.+):(\d+)$/) { |
| 1202 |
my $throttle = $1; |
| 1203 |
my $limit = $2; |
| 1204 |
# throttle ip address? |
| 1205 |
if ($throttle =~ /^[\d.-]+$/) { |
| 1206 |
if (ipin($RA,$throttle)) { |
| 1207 |
$bwl = $limit; |
| 1208 |
last; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
# throttle email address? |
| 1212 |
else { |
| 1213 |
# allow wildcard *, but not regexps |
| 1214 |
$throttle =~ quotemeta $throttle; |
| 1215 |
$throttle =~ s/\*/.*/g; |
| 1216 |
if ($to =~ /$throttle$/) { |
| 1217 |
$bwl = $limit; |
| 1218 |
last; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
foreach my $sig (keys %SIG) { local $SIG{$sig} = \&sigexit } |
| 1226 |
local $SIG{ALRM} = sub { |
| 1227 |
fdlog($log,$file,$s,$size); |
| 1228 |
die "TIMEOUT\n"; |
| 1229 |
}; |
| 1230 |
|
| 1231 |
if (-l $data) { |
| 1232 |
# my $wait = "$file/wait"; |
| 1233 |
# open $wait,'>',$wait and close $wait; |
| 1234 |
} |
| 1235 |
|
| 1236 |
seek $data,$seek,0 if $seek; |
| 1237 |
|
| 1238 |
$t0 = time; |
| 1239 |
$s = $b = 0; |
| 1240 |
|
| 1241 |
# sysread/syswrite because of speed |
| 1242 |
while ($s < $size) { |
| 1243 |
if ($b = sysread($data,$buf,$bs)) { |
| 1244 |
# last chunk for HTTP Range? |
| 1245 |
if ($stop and $s+$b > $size) { |
| 1246 |
$b = $size-$s; |
| 1247 |
$buf = substr($buf,0,$b) |
| 1248 |
} |
| 1249 |
$s += $b; |
| 1250 |
alarm($timeout*10); |
| 1251 |
syswrite STDOUT,$buf or last; # client still alive? |
| 1252 |
if ($bwl) { |
| 1253 |
alarm(0); |
| 1254 |
sleep 1 while $s/(time-$t0||1)/1024 > $bwl; |
| 1255 |
} |
| 1256 |
} else { |
| 1257 |
if ($upload) { |
| 1258 |
# uploader no more at work? |
| 1259 |
if (flock($data,LOCK_EX|LOCK_NB)) { |
| 1260 |
last; |
| 1261 |
} else { |
| 1262 |
sleep 1; |
| 1263 |
} |
| 1264 |
} else { |
| 1265 |
last; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
close $data; |
| 1271 |
alarm(0); |
| 1272 |
|
| 1273 |
fdlog($log,$file,$s,$size); |
| 1274 |
} |
| 1275 |
close $download; |
| 1276 |
|
| 1277 |
return $s; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
sub sendsize { |
| 1282 |
my ($path) = @_; |
| 1283 |
my ($file,$upload,$to,$from,$dkey); |
| 1284 |
my $size = 0; |
| 1285 |
local $_; |
| 1286 |
|
| 1287 |
$path =~ s:^/::; |
| 1288 |
($to,$from,$file) = split('/',$path); |
| 1289 |
$to =~ s/,.*//; |
| 1290 |
$to = lc $to; |
| 1291 |
$from = lc $from; |
| 1292 |
|
| 1293 |
# swap to and from for special senders, see fup storage swap! |
| 1294 |
($from,$to) = ($to,$from) if $from =~ /^(fexmail|anonymous)/; |
| 1295 |
|
| 1296 |
$to .= '@'.$hostname if $to eq 'anonymous'; |
| 1297 |
$from .= '@'.$hostname if $from eq 'anonymous'; |
| 1298 |
|
| 1299 |
$to .= '@'.$mdomain if -d "$to\@$mdomain"; |
| 1300 |
$from .= '@'.$mdomain if -d "$from\@$mdomain"; |
| 1301 |
|
| 1302 |
$file =~ s/%([A-F0-9]{2})/chr(hex($1))/ge; |
| 1303 |
$file = urlencode($file); |
| 1304 |
|
| 1305 |
if ($to eq '*' and $fileid) { |
| 1306 |
foreach my $fd (glob "*/$from/$file") { |
| 1307 |
if (-f "$fd/data" |
| 1308 |
and -l "$fd/id" and readlink "$fd/id" eq $fileid |
| 1309 |
and $dkey = readlink "$fd/dkey") { |
| 1310 |
$to = $fd; |
| 1311 |
$to =~ s:/.*::; |
| 1312 |
last; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
} elsif ($to !~ /@/ and open my $AB,'<',"$from/\@ADDRESS_BOOK") { |
| 1316 |
while (<$AB>) { |
| 1317 |
s/\s*#.*//; |
| 1318 |
$_ = lc $_; |
| 1319 |
my ($alias,$address) = split; |
| 1320 |
if ($address) { |
| 1321 |
$address =~ s/,.*//; |
| 1322 |
$address .= '@'.$mdomain if $mdomain and $address !~ /@/; |
| 1323 |
if ($to eq $alias) { |
| 1324 |
$to = $address; |
| 1325 |
last; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
} |
| 1329 |
close $AB; |
| 1330 |
} |
| 1331 |
|
| 1332 |
if (-f "$to/$from/$file/data") { |
| 1333 |
$dkey = readlink "$to/$from/$file/dkey"; |
| 1334 |
$fkey = slurp("$to/$from/$file/filename")||$file; |
| 1335 |
} elsif ($prefetch) { |
| 1336 |
mkdirp("$to/$from/$file"); |
| 1337 |
$dkey = randstring(8); |
| 1338 |
symlink "../$to/$from/$file","$dkeydir/$dkey"; |
| 1339 |
symlink $dkey,"$to/$from/$file/dkey"; |
| 1340 |
$fkey = $file; |
| 1341 |
} |
| 1342 |
|
| 1343 |
$upload = -s "$to/$from/$file/upload" || -s "$to/$from/$file/data" || 0; |
| 1344 |
$size = readlink "$to/$from/$file/size" || 0; |
| 1345 |
$fileid = readlink "$to/$from/$file/id" || ''; |
| 1346 |
|
| 1347 |
# if ($size and not $prefetch and not -f "$to/$from/$file/data") { |
| 1348 |
# http_die("<code>$file</code> has gone"); |
| 1349 |
# } |
| 1350 |
|
| 1351 |
nvt_print('HTTP/1.1 200 OK'); |
| 1352 |
nvt_print("Server: fexsrv"); |
| 1353 |
nvt_print("Content-Length: $upload"); |
| 1354 |
nvt_print("X-Original-Recipient: $to"); |
| 1355 |
if ($dkey and not -s "$from/\@ALLOWED_RECIPIENTS") { |
| 1356 |
nvt_print("X-DKEY: $dkey"); |
| 1357 |
if ($fkey) { |
| 1358 |
foreach my $durl (@durl) { |
| 1359 |
if ($durl =~ /^$ENV{PROTO}/i) { |
| 1360 |
nvt_print("X-Location: $durl/$dkey/$fkey"); |
| 1361 |
} |
| 1362 |
} |
| 1363 |
} |
| 1364 |
} |
| 1365 |
nvt_print("X-Size: $size"); |
| 1366 |
nvt_print("X-File-ID: $fileid") if $fileid; |
| 1367 |
nvt_print("X-Features: $ENV{FEATURES}"); |
| 1368 |
nvt_print(''); |
| 1369 |
} |
| 1370 |
|
| 1371 |
|
| 1372 |
sub check_rhosts { |
| 1373 |
my $ipr = shift; |
| 1374 |
my @hosts; |
| 1375 |
local $_; |
| 1376 |
|
| 1377 |
if (open $ipr,$ipr) { |
| 1378 |
while (<$ipr>) { |
| 1379 |
chomp; |
| 1380 |
s/#.*//; |
| 1381 |
s/\s//g; |
| 1382 |
if ($_ eq '@LOCAL_RHOSTS') { |
| 1383 |
push @hosts,@local_rhosts if @local_rhosts; |
| 1384 |
} elsif (/\w/) { |
| 1385 |
push @hosts,$_; |
| 1386 |
} |
| 1387 |
} |
| 1388 |
close $ipr; |
| 1389 |
if (@hosts and not ipin($RA,@hosts)) { |
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
return 1; |
| 1394 |
} |
| 1395 |
|
| 1396 |
|
| 1397 |
sub require_auth { |
| 1398 |
http_header( |
| 1399 |
'401 Authorization Required', |
| 1400 |
'WWW-Authenticate: Basic realm="'.$ENV{SERVER_NAME}.' F*EX download"', |
| 1401 |
'Content-Length: 0', |
| 1402 |
); |
| 1403 |
# control back to fexsrv for further HTTP handling |
| 1404 |
&reexec; |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
sub check_auth { |
| 1409 |
my ($path,$user,$auth) = @_; |
| 1410 |
my ($to,$from,$file,$dkey); |
| 1411 |
my ($id,$idf); |
| 1412 |
my ($subuser,$subid); |
| 1413 |
my $auth_ok = 0; |
| 1414 |
local $_; |
| 1415 |
|
| 1416 |
if ($path =~ m:(.+)/(.+)/(.+):) { |
| 1417 |
($to,$from,$file) = ($1,$2,$3); |
| 1418 |
} elsif ($path =~ m:(.+)/(.+):) { |
| 1419 |
($dkey,$file) = ($1,$2); |
| 1420 |
$path = readlink "$dkeydir/$dkey" or http_die('no such file'); |
| 1421 |
(undef,$to,$from,$file) = split('/',$path); |
| 1422 |
} else { |
| 1423 |
http_die("wrong URL format for download"); |
| 1424 |
} |
| 1425 |
|
| 1426 |
$to .= '@'.$mdomain if $mdomain and $to !~ /@/; |
| 1427 |
$from .= '@'.$mdomain if $mdomain and $from !~ /@/; |
| 1428 |
|
| 1429 |
$to = lc $to; |
| 1430 |
$from = lc $from; |
| 1431 |
|
| 1432 |
# auth user match to in download URL? |
| 1433 |
if ($to ne $user and "$to\@$mdomain" ne $user and $to ne "$user@$mdomain") { |
| 1434 |
debuglog("mismatch: to=$to, auth user=$user"); |
| 1435 |
&require_auth; |
| 1436 |
} |
| 1437 |
|
| 1438 |
# check for real user |
| 1439 |
if (open $idf,'<',"$to/@") { |
| 1440 |
$id = getline($idf); |
| 1441 |
close $idf; |
| 1442 |
unless ($id and $id eq $auth) { |
| 1443 |
debuglog("$user mismatch: id=$id, auth=$auth"); |
| 1444 |
&require_auth; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
# check for sub user |
| 1448 |
elsif (open $idf,'<',"$from/\@SUBUSER") { |
| 1449 |
while (<$idf>) { |
| 1450 |
chomp; |
| 1451 |
s/#.*//; |
| 1452 |
($subuser,$subid) = split ':'; |
| 1453 |
if ($subid and $subid eq $auth |
| 1454 |
and ($user eq $subuser |
| 1455 |
or $subuser eq '*@*' |
| 1456 |
or $subuser =~ /^\*\@(.+)/ and $user =~ /\@\Q$1\E$/i |
| 1457 |
or $subuser =~ /(.+)\@\*$/ and $user =~ /^\Q$1\E\@/i)) { |
| 1458 |
$auth_ok = 1; |
| 1459 |
last; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
close $idf; |
| 1463 |
unless ($auth_ok) { |
| 1464 |
debuglog("no matching $user in $from/\@SUBUSER"); |
| 1465 |
&require_auth; |
| 1466 |
} |
| 1467 |
} else { |
| 1468 |
debuglog("no $to/@ and no $from/@"); |
| 1469 |
&require_auth; |
| 1470 |
} |
| 1471 |
|
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
sub check_captive { |
| 1476 |
my $to = shift; |
| 1477 |
$to =~ s:/.*::; |
| 1478 |
$to .= '@'.$mdomain if $mdomain and -d "$to\@$mdomain"; |
| 1479 |
if (-e "$to/\@CAPTIVE") { |
| 1480 |
http_die("$to is CAPTIVE - no URL parameters allowed"); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
sub sigexit { |
| 1486 |
my ($sig) = @_; |
| 1487 |
my $msg; |
| 1488 |
|
| 1489 |
$msg = @_ ? "@_" : '???'; |
| 1490 |
$msg =~ s/\n/ /g; |
| 1491 |
$msg =~ s/\s+$//; |
| 1492 |
|
| 1493 |
errorlog("$file caught SIGNAL $msg"); |
| 1494 |
|
| 1495 |
# sigpipe means: client has terminated |
| 1496 |
# this event will be handled further by sendfile(), do not terminate here |
| 1497 |
if ($sig eq 'PIPE') { |
| 1498 |
debuglog("SIGNAL $msg"); |
| 1499 |
} else { |
| 1500 |
$SIG{__DIE__} = ''; |
| 1501 |
if ($sig eq 'DIE') { |
| 1502 |
shift; |
| 1503 |
die "$msg\n"; |
| 1504 |
} else { |
| 1505 |
die "SIGNAL $msg\n"; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
} |