#!/usr/bin/perl -wT

# F*EX document output
#
# is a subprogram of fexsrv! do not run it directly!
#
# Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
#

use File::Basename;
use Fcntl 	qw(:flock :seek :mode);
use POSIX	qw(strftime locale_h);
use Cwd 	qw(getcwd abs_path);
# use utf8;
# use CGI::Carp	qw(fatalsToBrowser);

# import from fex.pp
our ($bs,$tmpdir,@doc_dirs,$RA);

&redirect_stderr;

my $log = 'dop.log';

# POSIX time format needed for HTTP header
setlocale(LC_TIME,'POSIX');

sub dop {
  my $doc = shift;
  my $source = shift;
  my $seek = 0;
  my $stop = 0;
  my ($link,$host,$path,$range);
  local @includes = (); # html files

  our $error = 'F*EX document output ERROR';

  security_check($doc);

  # reget?
  if ($range = $ENV{HTTP_RANGE}) {
    $seek = $1 if $range =~ /^bytes=(\d+)-/i;
    $stop = $1 if $range =~ /^bytes=\d*-(\d+)/i;
  }

  # redirect on relative symlinks without "../"
  if ($link = readlink($doc) and
      $link !~ m:^/: and $link !~ m:\.\./: and $link !~ /^:.+:$/) {
    $path = $ENV{REQUEST_URI};
    $path =~ s:[^/]*$::;
    $doc = "$path/$link";
    $doc =~ s:/+:/:g;
    $doc =~ s:^/::;
    nvt_print(
      "HTTP/1.1 302 Found",
      "Location: /$doc",
      "Content-Length: 0",
      ""
    );
    &reexec;
  }

  # watchdog documents
  if (@wdd and $wdd and grep { $doc =~ /$_/ } @wdd) { &$wdd($doc) }

  my $dir = untaint(getcwd());
  chdir(dirname($doc));
  http_output($doc,$seek,$stop);
  chdir($dir);
}

sub http_output {
  my ($file,$seek,$stop) = @_;
  my ($filename,$files,$streamfile,$size,$total_size);
  my ($data,$type);
  my ($var,$env,$con);
  my @files; # used for streaming
  my $htmldoc = '';
  my $textdoc = '';
  my $htauth;
  my @s;
  my $s = 0;
  my $b = 0;
  my $mtime =0;
  my $http_client = $ENV{HTTP_USER_AGENT} || '';
  local $_;

  # extra security check: document must not be in lib or spool directory
  if (path_match($file,$FEXLIB) or path_match($file,$spooldir)) {
    http_error(403);
  }

  security_check($file);
  $_=check_htauth($file);
# http_die("[$_]");

  if (-f $file) {
    # normal file
    open $file,'<',$file or http_error(400);
    security_check($file);
  } elsif ($file =~ /(.+)\.gz$/ and -f $1) {
    @files = ($1);
    open $file,'-|',qw'gzip -c',@files or http_error(503);
  } elsif ($file =~ /(.+)\.tgz$/ and -f "$1.tar") {
    @files = ("$1.tar");
    open $file,'-|',qw'gzip -c',@files or http_error(503);
  } elsif ($file =~ /(.+)\.(tar|tgz|zip)$/ and
           @s = lstat($streamfile = "$1.stream") and
           ($s[4] == $< or $s[4] == 0))
  {
    # streaming file
    chdir dirname($file);
    security_check($file);
    if (-l $streamfile and readlink($streamfile) =~ /^:(.+):$/) {
      # special symlink pointer file for streaming
      @files = split(/:/,$1);
    } elsif (open $streamfile,$streamfile) {
      # special streaming file
      while (<$streamfile>) {
        next if /^#/;
        chomp;
        s/\s+$//;
        if (/^(\/.*):/) {
          chdir $1;
          security_check($1);
        } else {
          push @files,$_;
          $mtime = mtime($_) if mtime($_) > $mtime;
        }
      }
    } else {
      http_error(503);
    }
    close $streamfile;
    if (isodate($mtime) =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d)/) {
      $mtime = "$1$2$3_$4$5";
    } else {
      $mtime = 0
    }
    my $filename = basename($file);
    $filename =~ s/\.(\w+)$/-$mtime.$1/;
    # wget --content-disposition ...
    unshift @extra_header,"Content-Disposition: attachment; filename=$filename";
    foreach (@files) {
      if (/^\// or /\.\.\//) {
        # absolute path or relative path with parent directory is not allowed
        errorlog("$streamfile: $_ is not allowed for streaming");
        http_error(403);
      }
      unless (-e $_) {
        errorlog("$streamfile: $_ does not exist");
        http_error(404);
      }
      if (@s = stat($_) and not($s[2] & S_IRGRP) or not -r $_) {
        # file must be readable by user and group
        errorlog("$streamfile: $_ is not readable by user and group");
        http_error(403);
      }
    }
    http_error(416) if $ENV{HTTP_RANGE};
    close STDERR;
    if    ($file =~ /\.tar$/) { @a = qw'tar --exclude *~ --exclude .* -cf -' }
    elsif ($file =~ /\.tgz$/) { @a = qw'tar --exclude *~ --exclude .* -czf -' }
    elsif ($file =~ /\.zip$/) { @a = qw'zip -x *~ */.*/* @ -rq -' }
    else { http_error(400) }
    open $file,'-|',@a,@files or http_error(503);
  } else {
    http_error(404);
  }

  if ($binarymode) {
    # not in use any more!
    $type = 'application/x-unknown';
    unshift @extra_header,"Content-Disposition: attachment; filename=".
                          basename($file);
  } else {
    $type = 'application/octet-stream';
    if    ($file =~ /\.html?$/i) { $type = 'text/html' }
    elsif ($file =~ /\.css$/)	 { $type = 'text/css' }
    elsif ($file =~ /\.js$/)	 { $type = 'text/javascript' }
#   elsif ($file =~ /\.txt$/i)	 { $type = 'text/plain' }
    elsif ($file =~ /\.ps$/i)	 { $type = 'application/postscript' }
    elsif ($file =~ /\.pdf$/i)	 { $type = 'application/pdf' }
    elsif ($file =~ /\.mp3$/i)	 { $type = 'audio/mpeg' }
    elsif ($file =~ /\.mp4$/i)	 { $type = 'video/mp4' }
    elsif ($file =~ /\.avi$/i)	 { $type = 'video/x-msvideo' }
    elsif ($file =~ /\.jpe?g$/i) { $type = 'image/jpeg' }
    elsif ($file =~ /\.png$/i)	 { $type = 'image/png' }
    elsif ($file =~ /\.tiff?$/i) { $type = 'image/tiff' }
    elsif ($file =~ /\.gif$/i)	 { $type = 'image/gif' }
    elsif ($file =~ /\.bmp$/i)	 { $type = 'image/x-ms-bmp' }
    elsif ($file =~ /\.ico$/i)	 { $type = 'image/vnd.microsoft.icon' }
    elsif ($file !~ /\.(tar|tgz|zip|jar|rar|arj|7z|bz2?|gz)$/) {
      my $qfile = untaint(shellquote(abs_path($file)));
      $_ = `file $qfile`;
      if (0 and /HTML/) {
        $type = 'text/html';
      } elsif (/text/i and not -x $file) {
        $type = 'text/plain';
        if    (/\sASCII\s/)    { $type .= "; charset=us-ascii" }
        elsif (/(ISO-[\w-]+)/) { $type .= "; charset=".lc($1) }
        else                   { $type .= "; charset=utf-8" }
      }
    }

    # show sourcecode if URL ends with '!'
    # to avoid this for a HTML file, simple do a: chmod o-r file
    if ($type eq 'text/html') {
      if ($htmlsource) {
        if (@s = stat($file) and $s[2] & S_IROTH) {
          $type = 'text/plain';
        } else {
          http_error(403);
        }
      }
    } elsif ($ENV{'QUERY_STRING'} eq '!') {
      $type = 'text/plain';
    }

    if ($type eq 'text/html') {
      $seek = $stop = 0;
      local $^W = 0;
      local $/;
      $htmldoc = <$file>;
      $htmldoc =~ s:(</html>).*:$1\n:is;
      $htmldoc = html_parse($htmldoc);
      $total_size = $size = $s = length($htmldoc);
    } elsif ($type =~ m:^text/:) {
      while (<$file>) {
        if (%censor) {
          foreach my $rx (keys %censor) {
            s/$rx/$censor{$rx}/g;
          }
        }
        $textdoc .= $_;
      }
      $total_size = $size = $s = length($textdoc);
    } else {
      if (@files) {
        $size = 0;
      } else {
        $total_size = -s $file || 0;
        $size = $total_size - $seek - ($stop ? $total_size-$stop-1 : 0);
      }
    }
  
  }

  if ($size < 0) {
    http_header('416 Requested Range Not Satisfiable');
    exit;
  }

  alarm($timeout*10);

  if ($ENV{PROTO} =~ /^http/) {
    if ($seek or $stop) {
      my $range;
      if ($stop) {
        $range = sprintf("bytes %s-%s/%s",$seek,$stop,$total_size);
      } else {
        $range = sprintf("bytes %s-%s/%s",$seek,$total_size-1,$total_size);
      }
      nvt_print(
        'HTTP/1.1 206 Partial Content',
        'Server: fexsrv',
        "Content-Length: $size",
        "Content-Range: $range",
        "Content-Type: $type",
      );
    } else {
      # streaming?
      if (@files) {
        nvt_print(
          'HTTP/1.1 200 OK',
          'Server: fexsrv',
          "Expires: 0",
          "Content-Type: $type",
        );
      } else {
        # Java (clients) needs Last-Modified header!
        # if there are locale versions, use actual time for Last-Modified
        # to enforce reload of page
        $file =~ m{/htdocs/(.+)};
        my @lfiles = glob("$FEXHOME/locale/*/htdocs/$1");
        my $date = ($dynamic or @lfiles > 1) ?
          strftime("%a, %d %b %Y %T GMT",gmtime(time)) :
          http_date($file);
        nvt_print(
          'HTTP/1.1 200 OK',
          'Server: fexsrv',
          "Last-Modified: $date",
          "Expires: 0",
          "Content-Length: $size",
          "Content-Type: $type",
        );
        # nvt_print("Set-Cookie: locale=$locale") if $use_cookies and $locale;
      }
    }
    nvt_print($_) foreach(@extra_header);
    nvt_print('');
  }

  if ($ENV{REQUEST_METHOD} eq 'GET') {
    if ($type eq 'text/html') {
      alarm($timeout*10);
      print $htmldoc;
      $s = $size;
    } elsif ($type =~ /^text/) {
      alarm($timeout*10);
      print $textdoc;
      $s = $size;
    } else {
      # binary data # can be stream!
      seek $file,$seek,0 if $seek;
      while ($b = read($file,$data,$bs)) {
        if ($stop and $s+$b > $size) {
          $b = $size-$s;
          $data = substr($data,0,$b)
        }
        $s += $b;
        alarm($timeout*10);
        print $data or last;
      }
    }
    fdlog($log,$file,$s,$size) if $s;
  }

  alarm(0);
  close $file;
  exit if @files; # streaming end
  return $s;
}


sub html_parse {
  my $htmldoc = shift;
  my $mark = randstring(16);
  local $_;

  # remove ##comments
  while ($htmldoc =~ s/^##.*?\n/\n/m) {}

  # evaluate #script ... #endscript blocks
  while ($htmldoc =~ s/\n#script\s*\n(.+?)\n#endscript\s*\n/\n$mark/s) {
    # eval code with output substitution
    local $pc = $1;
    local $__ = '';
    local $^W = 0;
    tie *STDOUT => "Buffer",\$__;
    eval('package DOP;' . $pc); # ignore return value!
    untie *STDOUT;
    $htmldoc =~ s/$mark/$__/;
  }

  # evaluate #if ... #else ... #elseif ... #endif blocks
  while ($htmldoc =~ s/\n(#if\s+(.+?)\n.+?\n)#endif\s*\n/\n$mark\n/s) {
    $_ = $1;
    # if block
    if (eval $2) {
      s/#if.*\n//;
      s/\n#else.*//s;
      $htmldoc =~ s/$mark/$_/;
    } else {
      # elseif blocks
      while (s/.*?\n#elseif\s+(.+?)\n//s) {
        if (eval $1) {
          s/\n#else.*//s;
          $htmldoc =~ s/$mark/$_/;
        }
      }
      # else block left?
      if ($htmldoc =~ /$mark/) {
        s/.*\n#else\s*\n//s or $_ = '';
        $htmldoc =~ s/$mark/$_/;
      }
    }
  }

  # evaluate #include "file"
  my @includes = ();
  while ($htmldoc =~ s/\n#include "(.+?)"/\n$mark/s) {
    my $file = abs_path($1);
    my $include = '';

#   security_check($file,"internal access error in $doc");
    security_check($file,"internal access error");

    if (grep { $_ eq $file } @::includes) {
      $dynamic = $htmldoc =~ s/$mark//;
    } else {
      if (open $file,$file) {
        $include = <$file>;
        close $file;
        $include = html_parse($include);
        push @includes,$file;
      }
      $dynamic = $htmldoc =~ s/$mark/$include/;
    }
  }
  push @::includes,@includes if @includes;

  # evaluate <<perl-code>> or <<<perl-code>>>
  {
    local $timeout = '';
    local $SIG{ALRM} = sub { $timeout = '<h3>TIMEOUT!</h3>' };
    alarm(10);
    while ($htmldoc =~ /<<(.+?>?)>>/s) {
      local $pc = $1;
      if ($pc =~ s/^<(.+)>$/$1/) {
        # <<<eval code>>> without output substitution
        eval('package DOP;' . $pc);
        last if $timeout;
        $dynamic = $htmldoc =~ s/<<<(.+?)>>>//s;
      } else {
        # eval code with output substitution
        local $__ = '';
        local $^W = 0;
        tie *STDOUT => "Buffer",\$__;
        my $r .= eval('package DOP;' . $pc);
        $__ .= $r if $pc !~ /;\s*$/; # append return value if no trailing ;
        untie *STDOUT;
        last if $timeout;
        $dynamic = $htmldoc =~ s/<<(.+?)>>/$__/s;
      }
    }
    alarm(0);
    $dynamic = $htmldoc =~ s/<<(.+?>?)>>/$timeout/sg if $timeout;
  }

  # substitute $variable$ with value from environment (if present)
  while ($htmldoc =~ /\$([\w_]+)\$/g) {
    $var = $1;
    if (defined($env = $ENV{$var})) {
      $htmldoc =~ s/\$$var\$/$env/g;
    }
  }

  # purity censorship
#  if ($::hostname =~ /uni-stuttgart/) {
#    while ($htmldoc =~ s/\n.*sex.*\n/\n/i) {}
#  }

  if (%censor) {
    foreach my $rx (keys %censor) {
      $htmldoc =~ s/$rx/$censor{$rx}/g;
    }
  }

  return $htmldoc;
}


# show directory index
sub showindex {
  my $dir = shift;
  my ($htmldoc,$size);
  my @links = ();
  my @dirs = ();
  my @files = ();
  my $uri = $ENV{REQUEST_URI};
  my $allowed;
  my ($htindex,$htauth);
  local $_;

  $uri =~ s:/+$::;
  $dir =~ s:/+$::;

  security_check($dir);

  $htindex = "$dir/.htindex";
  $htauth  = "$dir/.htauth";

  open $htindex,$htindex or http_error(403);
  check_htauth($dir);

  # .htindex may contain listing regexp
  chomp ($allowed = <$htindex>||'.');
  close $htindex;

  opendir $dir,$dir or http_error(503);
  while (defined(my $a = readdir $dir)) {
    next if $a =~ /^[.#]|~$/;
    if (my @censor = keys %censor) {
      next if grep {$a =~ /$_/} @censor;
    }
    if (@s = lstat "$dir/$a" and ($s[2] & (S_IRGRP|S_IROTH))) {
      if    (-l _) { push @links,$a }
      elsif (-d _) { push @dirs,$a }
      elsif (-f _) { push @files,$a }
    }
  }
  closedir $dir;

  # parent directory listable?
  if ($uri =~ m:(/.+)/.+: and -f "$dir/../.htindex") {
    unshift @dirs,$1;
  }

  # first the (sub)directories
  $htmldoc = "<HTML>\n<h1>$uri/</h1>\n";
  foreach my $d (sort @dirs) {
    my $du = urlencode($d);
    my $dh = htmlencode($d);
    if ($d =~ m:^/: and -f "$d/.htindex") {
      $htmldoc .= "<b><a href=\"$du/\">$dh/</a></b><br>\n";
    } elsif (-f "$dir/$d/.htindex") {
      $htmldoc .= "<b><a href=\"$uri/$du/\">$uri/$dh/</a></b><br>\n";
    }
  }

#  # then the symlinks
#  $htmldoc .= "\n<pre>\n";
#  my $link;
#  foreach my $l (sort @links) {
#    if ($l =~ /$allowed/ and $link = readlink "$dir/$l" and $link =~ /^[^.\/]/) {
#      $htmldoc .= "$l -> <a href=\"$link\">$dir/$link</a>\n";
#    }
#  }

  # then the files
  $htmldoc .= "\n<pre>\n";
  foreach my $f (sort @files) {
    if ($f =~ /$allowed/) {
      $htmldoc .= sprintf "%20s %20s <a href=\"%s/%s\">%s</a>\n",
                          isodate(mtime("$dir/$f")),
                          d3(-s "$dir/$f"||0),
                          $uri,urlencode($f),htmlencode($f);
    }
  }
  $htmldoc .= "</pre>\n</HTML>\n";

  $size = length($htmldoc);
  nvt_print(
    'HTTP/1.1 200 OK',
    'Server: fexsrv',
    "Content-Length: $size",
    "Content-Type: text/html",
    '',
  );
  print $htmldoc;
  fdlog($log,"$dir/",$size,$size);
}


sub d3 {
  local $_ = shift;
  while (s/(\d)(\d\d\d\b)/$1,$2/) {};
  return $_;
}


sub http_date {
  my $file = shift;
  my @stat;

  if (@stat = stat($file)) {
    return strftime("%a, %d %b %Y %T GMT",gmtime($stat[9]));
  } else {
    return 0;
  }
}


sub path_match {
  my $p1 = abs_path(shift);
  my $p2 = abs_path(shift);

  if (defined $p1 and defined $p2) {
    return 1 if $p1          =~ /^\Q$p2/;
    return 2 if dirname($p1) =~ /^\Q$p2/;
  }
  return 0;
}


# return real file name (from symlink)
sub realfilename {
  my $file = shift;

  return '' unless -e $file;

  if (-l $file) {
    return realfilename(readlink($file));
  } else {
    return $file;
  }
}


sub security_check {
  my $file = shift; # can be directory, too!
  my $msg = shift||'';
  my $afile = abs_path($file)||$file;
  my @s;

  # client ip allowed?
  access_check($file);

  # documents with leading . are not allowed, but RFC5785 is accepted
  if ($afile =~ /\/\./ and $afile !~ m:/.well-known/acme-challenge/:) {
    errorlog("$afile with leading . in path");
    http_error(403);
  }

  if (-f $file) {

    # document filename must not contain @
    if (realfilename($file) =~ /@/ or $afile =~ /@/) {
      errorlog("$file contains @");
      http_error(403);
    }

    # document filename must not end with ~
    if (realfilename($file) =~ /~$/) {
      errorlog("$file ends with ~");
      http_error(403);
    }

    # file must be group or world readable
    if (@s = stat($file) and not($s[2] & (S_IRGRP|S_IROTH))) {
      errorlog("$file not group or world readable");
      http_error(403);
    }

    # symlink to regular file and symlink owned by root or fex? ==> ok!
    if (-l $file and path_match(dirname($file),$docdir)) {
      @s = lstat($file);
      return if $s[4] == 0 or $s[4] == $<;
    }

  }

  # file in allowed directory? ==> ok!
  foreach my $dir (@doc_dirs) {
    return if path_match($file,$dir);
  }

  errorlog("$file not in \@doc_dirs");
  http_error(403,$msg);
}

# security check: client ip allowed?
sub access_check {
  my $file = abs_path(shift);
  my $dir = $file;
  my $af;
  local $_;

  $dir .= '/x' if -d $dir;

  while ($dir = dirname($dir) and $dir ne '/') {
    $af = "$dir/.htaccessfrom";
    if (open $af,$af) {
      while (<$af>) {
        s/\s//g;
        if (ipin($ra,$_)) {
          close $af;
          return;
        }
      }
      errorlog("no access to $file by $af");
      http_error(403);
    }
  }

}

# HTTP Basic authentication
sub check_htauth {
  my $doc = shift;
  my ($htauth,$realm,$dir);
  my $akey = '';
  my $http_auth = '';
  my $uri = $ENV{REQUEST_URI} || '/';
  my $HA = $ENV{HTTP_AUTHORIZATION}||'';
  my $HC = $ENV{HTTP_COOKIE}||'';

  $uri =~ s/\/index\.html$//;
  $uri =~ s/\/$//;

  $akey = $1 if $HC =~ /\bakey=(\w+)/ and -f "$akeydir/$1/@";
  $http_auth = decode_b64($1) if $HA =~ /Basic\s+(.+)/;
  $dir = -d $doc ? $doc : dirname($doc);
  
  if (-d $doc or $doc =~ /\/index\.html$/) {
    $realm = $uri;
  } else {
    $realm = dirname($uri);
  }

  for (;;) {
    $htauth = "$dir/.htauth";
    if (-f $htauth) {
      
      # authorize via user + auth-ID? (.htauth must contain @ as single line)
      if (open $htauth,$htauth) {
        while (<$htauth>) {
          chomp;
          return $_ if $_ eq '*'; # all allowed
          if ($_ eq '@') {
            return $akey if $akey;
            if ($http_auth =~ /.:./) {
              my ($user,$aid) = split(':',$http_auth);
              $user = checkchars('user',$user);
              return $http_auth if (readline1("$spooldir/$user/@")||'') eq $aid;
            }
          }
        }
        close $htauth;
      }
    
      if ($http_auth and open $htauth,$htauth) {
        while (<$htauth>) {
          chomp;
          return $http_auth if /.:./ and $_ eq $http_auth;
        }
        close $htauth;
      }
      last;
    }
      
    $dir = dirname($dir);
    return $dir if $dir eq '/';
  }
    
  http_header(
    '401 Authorization Required',
    "WWW-Authenticate: Basic realm=\"$realm\"",
    'Content-Length: 0',
  );
  # control back to fexsrv for further HTTP handling
  &reexec;
}


# function for <<perl-code>> inside HTML documents
sub out {
  $__ .= join('',@_);
  return '';
}

# tie STDOUT to buffer variable (redefining print and printf)
package Buffer;

sub TIEHANDLE {
  my ($class,$buffer) = @_;
  bless $buffer,$class;
}

sub PRINT {
  my $buffer = shift;
  $$buffer .= $_ foreach @_;
}

sub PRINTF {
  my $buffer = shift;
  my $fmt = shift @_;
  $$buffer .= sprintf($fmt,@_);
}

1;
