File: dop

package info (click to toggle)
fex 20160919-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,944 kB
  • sloc: perl: 17,699; sh: 346; makefile: 67
file content (633 lines) | stat: -rwxr-xr-x 15,613 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#!/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);

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);

  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;
  my $htmldoc = '';
  my $htauth;
  my @s;
  my $s = 0;
  my $b = 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);
  $htauth = dirname($file).'/.htauth';
  require_auth($htauth,$file) if -f $htauth;

  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>) {
        chomp;
        if (/^(\/.*):/) {
          chdir $1;
          security_check($1);
        } else {
          push @files,$_;
        }
      }
    } else {
      http_error(503);
    }
    close $streamfile;
    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(403);
      }
      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);
  }

  $type = 'application/octet-stream';
  if    ($file =~ /\.html$/)	{ $type = 'text/html' }
  # elsif ($file =~ /\.txt$/)	{ $type = 'text/plain' }
  elsif ($file =~ /\.css$/)	{ $type = 'text/css' }
  elsif ($file =~ /\.js$/)	{ $type = 'text/javascript' }
  elsif ($file =~ /\.ps$/)	{ $type = 'application/postscript' }
  elsif ($file =~ /\.pdf$/)	{ $type = 'application/pdf' }
  elsif ($file =~ /\.jpg$/)	{ $type = 'image/jpeg' }
  elsif ($file =~ /\.png$/)	{ $type = 'image/png' }
  elsif ($file =~ /\.gif$/)	{ $type = 'image/gif' }
  elsif ($file !~ /\.(tar|tgz|zip|jar|rar|arj|7z|bz2?|gz)$/) {
    my $qfile = untaint(abs_path($file));
    $qfile =~ s/([^\/\.\+\w!=,_-])/\\$1/g;
    $_ = `file $qfile`;
    if (/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>;
    while ($htmldoc =~ s/\n##.*?\n/\n/) {};
    # evaluate #if ... #else ... #elseif ... #endif blocks
    my $mark = randstring(16);
    while ($htmldoc =~ s/\n(#if\s+(.+?)\n.+?\n)#endif/\n$mark/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
    while ($htmldoc =~ s/\n#include "(.*?)"/\n$mark/s) {
      my $file = $1;
      my $include = '';
      if (open $file,$file) {
        $include = <$file>;
        close $file;
      }
      $dynamic = $htmldoc =~ s/$mark/$include/;
    }
    # 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*$/;
          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;
      }
    };
    $total_size = $size = $s = length($htmldoc);
  } 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 ($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;
    } 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;
}


# 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);
  require_auth($htauth,$dir) if -f $htauth;

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

  opendir $dir,$dir or http_error(503);
  while (defined($_ = readdir $dir)) {
    next if /^[.#]/ or /~$/;
    if (@s = lstat "$dir/$_" and ($s[2] & (S_IRGRP|S_IROTH))) {
      if    (-l _) { push @links,$_ }
      elsif (-d _) { push @dirs,$_ }
      elsif (-f _) { push @files,$_ }
    }
  }
  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) {
    if ($d =~ m:^/: and -f "$d/.htindex") {
      $htmldoc .= "<h3><a href=\"$d/\">$d/</a></h3>\n";
    } elsif (-f "$dir/$d/.htindex") {
      $htmldoc .= "<h3><a href=\"$uri/$d/\">$uri/$d/</a></h3>\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),$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 @s;

  # client ip allowed?
  access_check($file);

  # documents with leading . are not allowed
  if (abs_path($file) =~ /\/\./) {
    errorlog("$file with leading .");
    http_error(403);
  }

  if (-f $file) {

    # document filename must not contain @
    if (realfilename($file) =~ /@/ or abs_path($file) =~ /@/) {
      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);
}

# 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 require_auth {
  my $htauth = shift;
  my $doc = shift;
  my ($realm,$auth);
  my @http_auth;
  my $uri = $ENV{REQUEST_URI} || '/';

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

  if (-d $doc or $doc =~ /\/index\.html$/) {
    $realm = $uri;
  } else {
    $realm = dirname($uri);
  }

  $auth = slurp($htauth);
  unless ($auth and $realm) {
    http_header("200 OK");
    print html_header("$ENV{SERVER_NAME} no authentication");
    pq(qq(
      '<h3><code>$htauth</code> missing</h3>'
      '</body></html>'
    ));
    exit;
  }
  chomp $auth;

  if ($ENV{HTTP_AUTHORIZATION} and $ENV{HTTP_AUTHORIZATION} =~ /Basic\s+(.+)/)
  { @http_auth = split(':',decode_b64($1)) }
  if (@http_auth != 2 or $http_auth[1] ne $auth) {
    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;