Debian Codegraph @

bin/fexsrv

raw
1 #!/usr/bin/perl -T
2
3 # fexsrv : web server for F*EX service
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7
8 use 5.008;
9 use Socket;
10 use IO::Handle;
11 use Fcntl qw':flock :seek';
12 use warnings;
13
14 BEGIN {
15 # stunnel workaround
16 $SIG{CHLD} = "DEFAULT";
17 $ENV{PERLINIT} = q{
18 $| = 1;
19 ## open STDERR,">>/tmp/fex.log";
20 $ENV{LC_ALL} = 'en_US.utf8';
21 unshift @INC,(getpwuid($<))[7].'/perl';
22 # web error handler
23 $SIG{__DIE__} = $SIG{__WARN__} = sub {
24 my $info = '';
25 my $url = $ENV{REQUEST_URL}||'';
26 my @d = localtime time;
27 my $time = sprintf('%d-%02d-%02d %02d:%02d:%02d',
28 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
29 if ($admin) {
30 my $mailto = "mailto:$admin?subject=fex%20bug";
31 $info = "<h3>send this error to <a href=\"$mailto\">$admin</a></h3>";
32 }
33 $_ = join("\n",@_);
34 debuglog($_) if exists &debuglog;
35 chomp;
36 s/&/&amp;/g;
37 s/</&lt;/g;
38 my $html = join("\n",
39 "<html><body>",
40 "<h1>INTERNAL ERROR in $0</h1>",
41 "<pre>\n$_\n</pre>\n<p>",
42 "$url\n<p>",
43 "$time\n<p>",
44 "$info\n<p>",
45 "</body></html>\n"
46 );
47 $length = length($html);
48 local $SIG{ALRM} = sub { exit 99 };
49 alarm(3);
50 unless ($HTTP_HEADER) {
51 print "HTTP/1.0 200 INTERNAL ERROR\r\n";
52 print "Content-Type: text/html\r\n";
53 print "Content-Length: $length\r\n";
54 print "\r\n";
55 }
56 print $html;
57 sleep 1;
58 close STDOUT;
59 exit 99;
60 }
61 };
62 eval $ENV{PERLINIT};
63 }
64
65 # use BSD::Resource;
66 # setrlimit(RLIMIT_CPU,999,999) or die "$0: $!\n";
67
68 # SSL remote address provided by stunnel
69 if (@ARGV and $ARGV[0] eq 'stunnel' and $ENV{REMOTE_HOST} =~ /(.+)/) {
70 $ssl_ra = $1;
71 # stunnel 5.40 bug workaround
72 $ssl_ra =~ s/.*:// if $ssl_ra =~ /\./;
73 }
74
75 # KEEP_ALIVE <== callback from CGI or fexsrv reexec
76 unless ($keep_alive = $ENV{KEEP_ALIVE}) {
77 %ENV = ( PERLINIT => $ENV{PERLINIT} ); # clean environment
78 }
79
80 $ENV{STUNNEL} = $ssl_ra if $ssl_ra;
81
82 $ENV{HOME} = (getpwuid($<))[7] or die "no HOME";
83
84 # fexsrv MUST be run with full path!
85 if ($0 =~ m:^(/.+)/bin/fexsrv:) {
86 $FEXHOME = $1;
87 $FEXHOME =~ s:/+:/:g;
88 $FEXHOME =~ s:/$::;
89 $ENV{FEXHOME} = $FEXHOME;
90 }
91
92 foreach my $lib (
93 $FEXHOME,
94 '/usr/local/fex',
95 '/usr/local/share/fex',
96 '/usr/share/fex',
97 ) {
98 $ENV{FEXLIB} = $FEXLIB = $lib and last if -f "$lib/fex.pp";
99 $ENV{FEXLIB} = $FEXLIB = "$lib/lib" and last if -f "$lib/lib/fex.pp";
100 }
101
102 # import from fex.pp
103 our ($hostname,$debug,$timeout,$max_error,$max_error_handler,$keep_archives);
104 our ($spooldir,@logdir,$docdir,$xkeydir,$akeydir,$lockdir,$encryption);
105 our ($force_https,$default_locale,$bs,$MB,$adlm,@forbidden_user_agents);
106 our (@locales);
107
108 # load common code (local config: $FEXHOME/lib/fex.ph)
109 require "$FEXLIB/fex.pp" or die "cannot load $FEXLIB/fex.pp - $!\n";
110
111 chdir $spooldir or http_die("$0: $spooldir - $!\n");
112
113 our $log = 'fexsrv.log';
114 our $error = 'F*EX ERROR';
115 our $htmlsource;
116 our $hid = ''; # header ID
117 our @log = ();
118 our $connect = '';
119 our $request = '';
120 my $sin = ''; # STDIN input buffer, filled by getaline()
121
122 $0 = untaint($0);
123
124 $ENV{GATEWAY_INTERFACE} = 'CGI/1.1f';
125 $ENV{SERVER_NAME} = $hostname;
126 $ENV{REQUEST_METHOD} = '';
127 $ENV{QUERY_STRING} = '';
128 $ENV{HTTP_COOKIE} = '';
129 $ENV{PATH_INFO} = '';
130 $ENV{RANDOM} = randstring(8);
131 $ENV{FEATURES} =
132 'SID,CHECKRECIPIENT,GROUPS,QUOTA,FILEID,MULTIPOST,XKEY,FILEQUERY,FILESTREAM,'.
133 'NOSTORE,AXEL,FEXMAIL,FILELINK,LISTARCHIVE,PREFETCH,CCCSYNC,FUPWATCH,MUP';
134
135 $port = 0;
136
137 # continue session?
138 if ($keep_alive) {
139 if ($ENV{HTTP_HOST} =~ /(.+):(.+)/) {
140 $hostname = $1;
141 $port = $2;
142 } else {
143 $hostname = $ENV{HTTP_HOST};
144 if ($ENV{PROTO} eq 'https') { $port = 443 }
145 else { $port = 80 }
146 }
147 $ra = $ENV{REMOTE_ADDR};
148 $rh = $ENV{REMOTE_HOST};
149 }
150
151 # new session
152 else {
153 my $iaddr;
154
155 # HTTPS connect
156 if ($ssl_ra) {
157 $ENV{PROTO} = 'https';
158 $ra = $ssl_ra;
159 if ($ssl_ra =~ /[\w:]:\w/) {
160 # ($rh) = `host $ssl_ra 2>/dev/null` =~ /name pointer (.+)\.$/;
161 $^W = 0; eval 'use Socket6'; $^W = 1;
162 http_error(503) if $@;
163 $iaddr = inet_pton(AF_INET6,$ssl_ra) and
164 $rh = gethostbyaddr($iaddr,AF_INET6);
165 } else {
166 $rh = gethostbyaddr(inet_aton($ra),AF_INET);
167 }
168 $port = 443;
169 # print {$log} "X-SSL-Remote-Host: $ssl_ra\n";
170 }
171
172 # HTTP connect
173 else {
174 $ENV{PROTO} = 'http';
175 my $sa = getpeername(STDIN) or die "no network stream on STDIN\n";
176 if (sockaddr_family($sa) == AF_INET) {
177 ($ENV{REMOTE_PORT},$iaddr) = sockaddr_in($sa);
178 $ra = inet_ntoa($iaddr);
179 $rh = gethostbyaddr($iaddr,AF_INET);
180 ($port) = sockaddr_in(getsockname(STDIN));
181 } elsif (sockaddr_family($sa) == AF_INET6) {
182 $^W = 0; eval 'use Socket6'; $^W = 1;
183 http_error(503) if $@;
184 ($ENV{REMOTE_PORT},$iaddr) = unpack_sockaddr_in6($sa);
185 $ra = inet_ntop(AF_INET6,$iaddr);
186 $rh = gethostbyaddr($iaddr,AF_INET6);
187 ($port) = unpack_sockaddr_in6(getsockname(STDIN));
188 } else {
189 die "unknown IP version\n";
190 }
191 $port ||= 80;
192 }
193
194 $rh ||= '-';
195 $ENV{REMOTE_ADDR} = $ra;
196 $ENV{REMOTE_HOST} = $rh;
197 $ENV{PORT} = $port;
198 $ENV{HTTP_HOST} = ($port == 80 or $port == 443)
199 ? $hostname : "$hostname:$port";
200
201 if (@allowed_domains) {
202 my $connect = sprintf "CONNECT:%s %s %s %s [%s_%s]",
203 $port,isodate(time),$rh,$ra,$$,0;
204 if ($rh eq '-') {
205 fexlog($connect,"no DNS");
206 while (<>) { last if /^\s*$/ }
207 http_error(403,"You have no DNS entry.");
208 }
209 if ("@allowed_domains" ne '*') {
210 my $allowed = 0;
211 foreach my $ad (@allowed_domains) {
212 $allowed = $ad if $rh =~ /(^|\.)\Q$ad\E$/i;
213 }
214 unless ($allowed) {
215 fexlog($connect,"$rh not in \@allowed_domains");
216 while (<>) { last if /^\s*$/ }
217 http_error(403,"You ($rh) are not in the allowed domains list.");
218 }
219 }
220 }
221 }
222
223 $ra = untaint($ra);
224 $rh = untaint($rh);
225 $port = untaint($port);
226
227 $ra =~ s/^::ffff:// if $ra =~ /\./;
228
229 if ($reverse_proxy_ip and $reverse_proxy_ip eq $ra) {
230 $ENV{FEATURES} =~ s/SID,//;
231 }
232
233 if (@anonymous_upload and ipin($ra,@anonymous_upload)) {
234 $ENV{FEATURES} .= ',ANONYMOUS';
235 }
236
237 $ENV{FEATURES} .= ',FAS' if -x "$FEXHOME/cgi-bin/fas";
238 $ENV{FEATURES} .= ',DOX' if -x "$FEXHOME/cgi-bin/dox";
239
240 $| = 1;
241
242 $SIG{CHLD} = "DEFAULT"; # stunnel workaround
243
244 $SIG{ALRM} = sub {
245 # printf {$log} "\nTIMEOUT %s %s\n",isodate(time),$connect;
246 $request ||= $sin;
247 exit if $keep_alive and not $request;
248 $request ||= '(NOTHING)';
249 @log = ($request) unless @log;
250 debuglog('TIMEOUT',isodate(time));
251 fexlog($connect,@log,"TIMEOUT");
252 &$header_hook($connect,$request,$ra) if $header_hook;
253 exit;
254 };
255
256 REQUEST:
257
258 $sin = $request = $HTTP_HEADER = '';
259 @log = ();
260
261 while (*STDIN) {
262
263 if (defined $ENV{REQUESTCOUNT}) { $ENV{REQUESTCOUNT}++ }
264 else { $ENV{REQUESTCOUNT} = 0 }
265
266 $connect = sprintf "%s:%s %s %s %s [%s_%s]",
267 $keep_alive ? 'CONTINUE' : 'CONNECT',
268 $port,
269 isodate(time),
270 $rh||'-',
271 $ra,
272 $$,$ENV{REQUESTCOUNT};
273 $hid = sprintf("%s %s\n",$rh||'-',$ra);
274
275 @header = @log = ();
276 $header = '';
277
278 # read complete HTTP header
279 while (defined ($_ = &getaline)) {
280 last if /^\s*$/;
281 $hl += length;
282 $header .= $_;
283 s/[\r\n]+$//;
284 # URL-encode non-printable chars
285 s/([\x00-\x08\x0E-\x1F\x7F-\x9F])/sprintf "%%%02X",ord($1)/ge;
286 s/%21/!/g;
287 if (@header and s/^\s+/ /) {
288 $header[-1] .= $_;
289 } else {
290 push @header,$_;
291 $header{$1} = $2 if /(.+)\s*:\s*(.+)/;
292 push @log,$_;
293 }
294 if ($hl > $MB) {
295 fexlog($connect,@log,"OVERRUN");
296 http_error(413);
297 }
298
299 if (/^(GET \/|\S*Forwarded|\S*Client-IP|\S*Coming-From|User-Agent)/i) {
300 $hid .= $_."\n";
301 }
302
303 # reverse-proxy?
304 # (only IPv4 support!)
305 if ($reverse_proxy_ip and $reverse_proxy_ip eq $ra and
306 /^\S*(Forwarded|Client-IP|Coming-From)\S*: ([\da-f:.]+)/i
307 ) {
308 $ENV{REMOTE_ADDR} = $ra = $2;
309 if ($ra =~ /:/) {
310 $rh = untaint(gethostbyaddr(inet_atop($ra),AF_INET6)) || '';
311 } else {
312 $rh = untaint(gethostbyaddr(inet_aton($ra),AF_INET)) || '';
313 }
314 $ENV{REMOTE_HOST} = $rh;
315 $ENV{HTTP_HOST} = $hostname;
316 if ($ENV{PROTO} eq 'https') { $port = 443 }
317 else { $port = 80 }
318 }
319 }
320
321 exit unless @header;
322 exit if $header =~ /^\s*$/;
323
324 $ENV{HTTP_HEADER} = $header;
325 debuglog($header);
326 # http_die("<pre>$header</pre>");
327
328 $ENV{'HTTP_HEADER_LENGTH'} = $hl;
329 $ENV{REQUEST_URI} = $uri = '';
330 $cgi = '';
331
332 # is it a HTTP-request at all?
333 $request = shift @header;
334 if ($request !~ /^(GET|HEAD|POST|OPTIONS).*HTTP\/\d\.\d$/i) {
335 fexlog($connect,$request,"DISCONNECT: no HTTP request");
336 badlog("no HTTP request: $request");
337 exit;
338 }
339
340 if ($force_https and $port != 443
341 and $request =~ /^(GET|HEAD|POST)\s+(.+)\s+(HTTP\/[\d\.]+$)/i) {
342 $request = $2;
343 nvt_print(
344 "HTTP/1.1 301 Moved Permanently",
345 "Location: https://$hostname$request",
346 "Content-Length: 0",
347 ""
348 );
349 fexlog($connect,@log);
350 exit;
351 }
352
353 $request =~ s{^(GET|HEAD|POST) https?://$hostname(:\d+)?}{$1 }i;
354
355 if ($request =~ m"^(GET|HEAD) /fop/\w+/") {
356 # no header inquisition on regular fop request
357 # $header_hook = '';
358 } else {
359 &$header_hook($connect,$request,$ra) if $header_hook;
360 }
361
362 unless ($keep_alive) {
363 if ($request =~ m:(HTTP/1.(\d)): and $2) {
364 $ENV{KEEP_ALIVE} = $keep_alive = $ra
365 } else {
366 $ENV{KEEP_ALIVE} = $keep_alive = '';
367 }
368 }
369
370 if ($request =~ /^OPTIONS \/?FEX HTTP\/[\d\.]+$/i) {
371 fexlog($connect,@log);
372 nvt_print(
373 "HTTP/1.1 201 OK",
374 "X-Features: $ENV{FEATURES}",
375 "X-Timeout: $timeout",
376 ''
377 );
378 # goto REQUEST if $keep_alive;
379 &reexec;
380 exit;
381 }
382
383 if ($request =~ m:^GET /?SID HTTP/[\d\.]+$:i) {
384 if (1 and $ENV{FEATURES} =~ /\bSID\b/) {
385 $ENV{SID} = randstring(8);
386 nvt_print(
387 "HTTP/1.1 201 ".$ENV{SID},
388 "Server: fexsrv",
389 "X-SID: ".$ENV{SID},
390 "X-Features: ".$ENV{FEATURES},
391 "X-Timeout: ".$timeout,
392 'Content-Length: 0',
393 ''
394 );
395 } else {
396 nvt_print(
397 "HTTP/1.1 501 Not Implemented",
398 "Server: fexsrv",
399 "X-Features: ".$ENV{FEATURES},
400 "X-Timeout: ".$timeout,
401 'Content-Length: 0',
402 ''
403 );
404 }
405 fexlog($connect,@log);
406 # goto REQUEST if $keep_alive;
407 &reexec;
408 exit;
409 }
410
411 if ($request =~ /^(GET|HEAD|POST)\s+(.+)\s+(HTTP\/[\d\.]+$)/i) {
412 $ENV{REQUEST} = $_;
413 $ENV{REQUEST_METHOD} = uc($1);
414 $ENV{REQUEST_URI} = $uri = $cgi = $2;
415 $ENV{HTTP_VERSION} = $protocol = $3;
416 $ENV{QUERY_STRING} = $1 if $cgi =~ s/\?(.*)//;
417 $ENV{PATH_INFO} = $1 if $cgi =~ m:/.+?(/.+?)(\?|$):;
418 $ENV{KEEP_ALIVE} = $keep_alive = '' if $protocol =~ /1\.0/;
419 $ENV{REQUEST_URL} = "$ENV{PROTO}://$ENV{HTTP_HOST}$ENV{REQUEST_URI}";
420 if ($request !~ m:^(GET|HEAD) /(fop|dox)/:) {
421 if ($uri =~ /<|%3c/i) { badchar("&lt;") }
422 if ($uri =~ />|%3e/i) { badchar(">") }
423 if ($uri =~ /\||%7c/i) { badchar("|") }
424 if ($uri =~ /\\|%5c/i) { badchar("\\") }
425 }
426 $uri =~ s:/+:/:g;
427 &$request_hook if $request_hook;
428 }
429
430 my $fua = join('|',@forbidden_user_agents);
431
432 while ($_ = shift @header) {
433
434 # header inquisition!
435 &$header_hook($connect,$_,$ra) if $header_hook;
436
437 # mega stupid "Download Manager" FlashGet
438 if ($uri =~ m{^/fop/} and m{^Referer: https?://.*\Q$uri$}) {
439 fexlog($connect,@log,"NULL: FlashGet");
440 debuglog("NULL: FlashGet");
441 exec qw'cat /dev/zero' or sleep 30;
442 exit;
443 }
444
445 if ($fua and /^User-Agent: ($fua)/) {
446 disconnect($1,"499 User Agent $1 Not Supported",30);
447 }
448
449 if (/^Range:.*,/) {
450 disconnect("Range a,b","416 Requested Range Not Satisfiable",30);
451 }
452 if (/^Range:.*(\d+)-(\d+)/) {
453 if ($1 > $2) {
454 disconnect("Range a>b","416 Requested Range Not Satisfiable",0);
455 }
456 if (($header{'User-Agent'}||'') !~ /$adlm/ ) {
457 disconnect("Range a-b","416 Requested Range Not Satisfiable",30);
458 }
459 }
460
461 if (/^Range:.*\d+-$/ and $hid) {
462 my $lock = untaint($lockdir.'/'.md5_hex($hid));
463 if (open $lock,'+>>',$lock) {
464 if (flock($lock,LOCK_EX|LOCK_NB)) {
465 seek $lock,0,0;
466 truncate $lock,0;
467 print {$lock} $hid;
468 } else {
469 disconnect(
470 "multiple Range request",
471 "400 Multiple Requests Not Allowed",
472 10,
473 );
474 }
475 }
476 }
477
478 # client signed int bug
479 if (/^Range:.*-\d+-/) {
480 disconnect("Range -a-","416 Requested Range Not Satisfiable",0);
481 }
482
483 # if (/^Range:/ and $protocol =~ /1\.0/) {
484 # &$header_hook($connect,$_,$ra) while ($header_hook and $_ = shift @header);
485 # fexlog($connect,@log,"DISCONNECT: Range + HTTP/1.0");
486 # debuglog("DISCONNECT: Range + HTTP/1.0");
487 # http_error(416);
488 # exit;
489 # }
490
491 if (/^Connection:\s*close/i) {
492 $ENV{KEEP_ALIVE} = $keep_alive = '';
493 }
494
495 # HTTP header ==> environment variables
496 if (/^([\w\-_]+):\s*(.+)/s) {
497 $http_var = $1;
498 $http_val = $2;
499 $http_var =~ s/-/_/g;
500 $http_var = uc($http_var);
501 $http_val =~ s/^\s+//;
502 $http_val =~ s/\s+$//;
503 if ($http_var =~ /^X_(FEX_\w+|CONTENT_LENGTH)$/) {
504 $http_var = $1;
505 } else {
506 $http_val =~ s/\s+/ /g;
507 if ($http_var =~ /^HTTP_(HOST|VERSION)$/) {
508 $http_var = 'HTTP_X_'.$1;
509 } elsif ($http_var =~ /^PROXY/) {
510 # http://cert.at/warnings/all/20160718.html
511 $http_var = 'HTTP_X_'.$http_var;
512 } elsif ($http_var !~ /^CONTENT_/) {
513 $http_var = 'HTTP_'.$http_var;
514 }
515 }
516 $http_val =~ s/[^\w.:-]/_/g if $http_var eq 'HTTP_HOST';
517 $ENV{$http_var} = $http_val;
518 }
519 }
520
521 # multiline header inquisition
522 &$header_hook($connect,$header,$ra) if $header_hook;
523
524 exit unless $cgi;
525
526 # extra download request? (request http://fexserver//xkey)
527 if ($cgi =~ m{^//([^/]+)$}) {
528 my $xkey = $1;
529 my $dkey;
530 if ($xkey =~ /^afex_\d/) {
531 $dkey = readlink "$xkeydir/$xkey" and $dkey =~ s/^\.\.\///;
532 } else {
533 $dkey = readlink "$xkeydir/$xkey/dkey" and $dkey .= "/$xkey";
534 }
535 if ($dkey) {
536 # xkey downloads are only one time possible - besides afex
537 if ($xkey !~ /^afex_\d/) {
538 unlink "$xkeydir/$xkey/xkey";
539 unlink "$xkeydir/$xkey";
540 }
541 nvt_print(
542 "HTTP/1.1 301 Moved Permanently",
543 "Location: $ENV{PROTO}://$ENV{HTTP_HOST}/fop/$dkey",
544 "Content-Length: 0",
545 ""
546 );
547 fexlog($connect,@log);
548 exit;
549 }
550 fexlog($connect,@log);
551 http_error(404);
552 exit;
553 }
554
555 # get locale
556 if (($ENV{QUERY_STRING} =~ /.*locale=([\w-]+)/ or
557 $ENV{HTTP_COOKIE} =~ /.*locale=([\w-]+)/)
558 and -d "$FEXHOME/locale/$1") {
559 $ENV{LOCALE} = $locale = $1;
560 } else {
561 $ENV{LOCALE} = $locale = $default_locale;
562 }
563
564 # for dynamic HTML documents
565 if ($ENV{HTTP_COOKIE} =~ /akey=(\w+)/) {
566 my $akey = $1;
567 my ($user,$id);
568 if ($user = readlink "$akeydir/$akey") {
569 $user =~ s:.*/::;
570 $user = untaint($user);
571 if ($id = slurp("$spooldir/$user/@")) {
572 chomp $id;
573 $ENV{AKEY} = $akey;
574 $ENV{USER} = $user;
575 $ENV{ID} = $id;
576 }
577 }
578 }
579
580 # check for name based virtual host
581 $vhost = vhost($ENV{'HTTP_HOST'});
582
583 if ($debug) {
584 debuglog("ENV:\n");
585 foreach $var (sort keys %ENV) {
586 if (defined($ENV{$var})) {
587 debuglog(sprintf " %s = >%s<\n",$var,$ENV{$var});
588 }
589 }
590 debuglog("\n");
591 }
592
593 # locale definitions in fex.ph?
594 if (@locales) {
595 if (@locales == 1) {
596 $locale = $locales[0];
597 } elsif (not grep /^$locale$/,@locales) {
598 $locale = $default_locale;
599 }
600 }
601
602 # prepare document file name
603 if ($ENV{REQUEST_METHOD} =~ /^GET|HEAD$/) {
604 if (%redirect) {
605 foreach my $r (keys %redirect) {
606 if ($uri =~ /^\Q$r/) {
607 redirect($uri,$r);
608 exit;
609 }
610 }
611 }
612 $doc = untaint($uri);
613 $doc =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge;
614 $doc =~ m:/\.\./: and http_error(403);
615 $doc =~ s:^/+::;
616 $doc =~ s/\?.*//;
617 if ($locale and $locale ne 'english' and -e "$docdir/locale/$locale/$doc") {
618 $doc = "$docdir/locale/$locale/$doc";
619 } else {
620 $doc = "$docdir/$doc";
621 }
622 }
623
624 # CGI or document request?
625 if ($cgi =~ s:^/+::) {
626 $cgi =~ s:/.*::;
627 unless ($cgi) {
628 my $login = "$FEXHOME/cgi-bin/login";
629 if (-x $login) {
630 $cgi = untaint(readlink($login) || $login);
631 $cgi =~ s:.*/::;
632 }
633 }
634
635 $ENV{SCRIPT_NAME} = $cgi;
636
637 # locale CGIs? (vhost comes already with own FEXLIB)
638 if ($locale and $locale ne 'english'
639 and -f "$FEXHOME/locale/$locale/cgi-bin/$cgi") {
640 $ENV{SCRIPT_FILENAME} = $cgi = "$FEXHOME/locale/$locale/cgi-bin/$cgi";
641 $ENV{FEXLIB} = $FEXLIB = "$FEXHOME/locale/$locale/lib" unless $vhost;
642 } else {
643 $ENV{SCRIPT_FILENAME} = $cgi = "$FEXHOME/cgi-bin/$cgi";
644 $ENV{FEXLIB} = $FEXLIB = "$FEXHOME/lib" unless $vhost;
645 }
646
647 $status = '';
648 if (-x $cgi and -f $cgi) {
649 if (@forbidden_hosts and ipin($ra,@forbidden_hosts)) {
650 fexlog($connect,@log,"FORBIDDEN");
651 http_error(403);
652 }
653 if ($header !~ /^Host: ./im) {
654 fexlog($connect,@log,"BADREQUEST: no Host header");
655 badlog("no Host header");
656 http_error(400);
657 }
658 unlink "$spooldir/.error/$ra";
659 # push @log,"DEBUG: locale=$locale locales=(@locales)";
660 fexlog($connect,@log,"EXEC $cgi");
661 eval { local $^W = 0; exec $cgi };
662 $status = "$! or bad interpreter";
663 fexlog($connect,@log,"FAILED to exec $cgi : $status");
664 http_error(555);
665 } elsif ($ENV{REQUEST_METHOD} =~ /^GET|HEAD$/) {
666 if (-f "$doc/.htindex") {
667 require "$FEXLIB/dop";
668 fexlog($connect,@log);
669
670 showindex($doc);
671 STDOUT->flush;
672 # goto REQUEST if $keep_alive;
673 &reexec;
674 exit;
675 }
676 if (-f "$doc/index.html") {
677 # force redirect if trailing / is missing
678 # this is mandatory for processing further HTTP request!
679 if ($doc !~ m{/$}) {
680 nvt_print(
681 "HTTP/1.1 301 Moved Permanently",
682 "Location: $ENV{REQUEST_URL}/",
683 "Content-Length: 0",
684 ""
685 );
686 fexlog($connect,@log);
687 # goto REQUEST if $keep_alive;
688 &reexec;
689 exit;
690 }
691 $doc .= '/index.html';
692 $doc =~ s:/+:/:g;
693 }
694 $doc =~ s/#.*//; # ignore HTML anchors (stupid msnbot)
695
696 # special request for F*EX UNIX clients
697 # if ($ENV{SCRIPT_NAME} eq 'xx.tar') {
698 # bintar(qw'fexget fexsend xx fexpush fexpull xxx');
699 # }
700 # if ($ENV{SCRIPT_NAME} eq 'sex.tar') {
701 # bintar(qw'sexsend sexget sexxx');
702 # }
703 # if ($ENV{SCRIPT_NAME} eq 'afex.tar') {
704 # bintar(qw'afex fexget fexsend');
705 # }
706 # if ($ENV{SCRIPT_NAME} eq 'afs.tar') {
707 # bintar(qw'fexget fexsend xx fexpush fexpull xxx',
708 # qw'afex asex sexsend sexget sexxx');
709 # }
710 # URL ends with ".html!" or ".html?!"
711 if ($doc =~ s/(\.html)!$/$1/ or
712 $doc =~ /\.html$/ and $ENV{'QUERY_STRING'} eq '!')
713 { $htmlsource = $doc } else { $htmlsource = '' }
714
715 if (-f $doc
716 or $doc =~ /(.+)\.(tar|tgz|zip)$/ and lstat("$1.stream")
717 or $doc =~ /(.+)\.tgz$/ and -f "$1.tar"
718 or $doc =~ /(.+)\.gz$/ and -f $1)
719 {
720 unlink "$spooldir/.error/$ra";
721 delete $ENV{SCRIPT_FILENAME};
722 $ENV{DOCUMENT_FILENAME} = $doc;
723 require "$FEXLIB/dop";
724 fexlog($connect,@log);
725 dop($doc);
726 STDOUT->flush;
727 # goto REQUEST if $keep_alive;
728 &reexec;
729 exit;
730 } elsif ($uri eq '/bunny') {
731 fexlog($connect,@log);
732 nvt_print(
733 'HTTP/1.1 200 OK',
734 'Server: fexsrv',
735 "Content-Type: text/plain",
736 '',
737 '=:3',
738 );
739 exit;
740 } elsif ($uri eq '/camel') {
741 fexlog($connect,@log);
742 nvt_print(
743 'HTTP/1.1 200 OK',
744 'Server: fexsrv',
745 "Content-Type: text/plain",
746 '',
747 );
748 local $/;
749 print unpack('u',<DATA>);
750 exit;
751 } elsif (-e $cgi) {
752 $status = 'not executable';
753 }
754
755 }
756
757 }
758
759 # neither document nor CGI ==> error
760
761 if ($status) {
762 fexlog($connect,@log,"FAILED to exec $cgi : $status");
763 http_error(666);
764 } else {
765 fexlog($connect,@log,"UNKNOWN URL");
766 badlog($request);
767 http_error(404);
768 }
769 exit;
770 }
771
772
773 # read one text line unbuffered from STDIN
774 sub getaline {
775 my $n = 0;
776 my $c;
777 $sin = ''; # input buffer, must be global for timeout()
778
779 alarm(10);
780
781 # must use sysread to avoid perl line buffering
782 # (later exec would destroy line buffer)
783 while (sysread STDIN,$c,1) {
784 $sin .= $c;
785 $n++;
786 last if $c eq "\n";
787 if ($n > $bs) {
788 fexlog($connect,@log,$sin,"OVERRUN");
789 http_error(413);
790 }
791 }
792
793 alarm(0);
794
795 return $sin;
796 }
797
798
799 sub fexlog {
800 my @log = @_;
801 local $SIG{ALRM} = sub { return };
802
803 foreach my $logdir (@logdir) {
804 if (open $log,'>>',"$logdir/$log") {
805 alarm(2);
806 flock $log,LOCK_EX;
807 alarm(0);
808 seek $log,0,SEEK_END;
809 print {$log} "\n",join("\n",@log),"\n";
810 close $log;
811 } else {
812 http_die("$0: cannot write to $logdir/$log - $!\n");
813 }
814 }
815 }
816
817
818 sub badchar {
819 my $bc = shift;
820
821 fexlog($connect,@log,"DISCONNECT: bad characters in URL ($bc)");
822 debuglog("DISCONNECT: bad characters in URL $uri ($bc)");
823 badlog($request);
824 http_die("\"$bc\" is not allowed in URL");
825 }
826
827
828 sub bintar {
829 my $tmpdir = "$FEXHOME/tmp";
830 my $fs = "$ENV{PROTO}://$ENV{HTTP_HOST}";
831
832 if (chdir "$FEXHOME/bin") {
833 fexlog($connect,@log);
834 chdir $fstb if $fstb;
835 mkdir $tmpdir;
836 foreach my $f (@_) {
837 copy($f,"$tmpdir/$f","fexserver = ''","fexserver = '$fs'");
838 chmod 0755,"$tmpdir/$f";
839 }
840 chdir $tmpdir or http_die("internal error: $tmpdir - $!");
841 my $tar = `tar cf - @_ 2>/dev/null`;
842 unlink @_;
843 nvt_print(
844 'HTTP/1.1 200 OK',
845 'Server: fexsrv',
846 "Content-Length: ".length($tar),
847 "Content-Type: application/x-tar",
848 '',
849 );
850 print $tar;
851 exit;
852 }
853 }
854
855
856 sub disconnect {
857 my $info = shift;
858 my $error = shift;
859 my $wait = shift||0;
860
861 # &$header_hook($connect,$_,$ra) while ($header_hook and $_ = shift @header);
862 fexlog($connect,@log,"DISCONNECT: $info");
863 debuglog("DISCONNECT: $info");
864 errorlog("$ENV{REQUEST_URI} ==> $error");
865 badlog("$ENV{REQUEST_URI} ==> $error ($info)");
866
867 sleep 5;
868 nvt_print("HTTP/1.0 $error");
869 exit;
870 }
871
872
873 sub redirect {
874 my $uri = shift;
875 my $r = shift;
876 my $rr = $redirect{$r};
877 my $newurl;
878
879 $uri =~ s/\Q$r//;
880
881 if ($rr =~ s/^!// or $header =~ /\nUser-Agent: (wget|curl)/i) {
882 $newurl = $rr.$uri;
883 nvt_print(
884 "HTTP/1.1 301 Moved Permanently",
885 "Location: $newurl",
886 "Content-Length: 0",
887 ""
888 );
889 } else {
890 if ($rr =~ /^http/) {
891 $newurl = $rr.$uri;
892 } else {
893 $newurl = "$ENV{PROTO}://$ENV{HTTP_HOST}$rr$uri";
894 }
895
896 http_header("200 OK");
897 print html_header("$hostname page has moved");
898 pq(qq(
899 '<h3>Please use new URL: <a href="$newurl">$newurl</a></h3>'
900 '</body></html>'
901 ));
902 }
903 fexlog($connect,@log,"REDIRECT $newurl");
904 if ($rr =~ /^http/) {
905 exit;
906 } else {
907 &reexec;
908 exit;
909 }
910 }
911
912
913 sub badlog {
914 my $request = shift;
915 my @n;
916 my $ed = "$spooldir/.error";
917 local $_;
918
919 if (@ignore_error) {
920 foreach (@ignore_error) {
921 return if $request =~ /$_/;
922 }
923 }
924
925 if ($ra and $max_error and $max_error_handler) {
926 mkdir($ed) unless -d $ed;
927
928 if (open $ra,"+>>$ed/$ra") {
929 flock($ra,LOCK_EX);
930 seek $ra,0,SEEK_SET;
931 @n = <$ra>;
932 printf {$ra} "%s %s\n",isodate(time),$request;
933 close $ra;
934 &$max_error_handler($ra,'bad request',@n) if scalar(@n) > $max_error;
935 }
936 }
937 }
938
939
940 __END__
941 M("`@("`@("`@("`@("`@("`@("`@("`@("`@("`@("`@("PM)R(G+5P*("`@
942 M("`@("`@("`@("`@("`@("`@("`@("]@8&`M+B`@(&!<("`@("Q=+B`@("`@
943 M("`@("`@("`@("`@("`@("`L+BY?"B`@("`@("`@("`@("`@("`@("`@("`@
944 M+&`@("`@(&`B+B`@72X@("`@(&`N("`@("`@("`@("`@("`@7U]?+BY>(&!?
945 M)V`B(B(*("`@("`@("`@("`@("`@("`@("`@("`I("`@("`@("`G+"TG+2T@
946 M+B`@("!@+B`@("`@(%]?7RPN+2<@("XN("T@("`G("TP("Y?"B`@("`@("`@
947 M("`@+"X@("`@("`@("`@?%]?+"Y?("!?("`N+5\@("!<("`\("Y@(B<G)V`L
948 M7R`@("`@("`@+2!?("TN("`@("`@("<M+BX*("`@("`@("`@('P@(&8M+2TM
949 M+2TM+2<O("`@("!@)RTM+BXL("`@("`M("`@("`@("`@("T@("`@("`@("`@
950 M("`@("`G("XL7U\N7%\N)PH@("`@("`@("`@8"TM)R<G)R<G8"TM7"`@("`@
951 M("`@("`@("!@)RTM+B`@("`@("`@("`@("`@("`@("`@7RPN)RTM)R<*("`@
952 M("`@("`@("`@("`@("`@("`@("\@("`@("`@("`@("`@("`@("!@8#T@+2`@
953 M+2`@("!?+2`@7RXM)PH@("`@("`@("`@("`@("`@("`@("!\("`@("`@("`@
954 M("`@("`@("`@("`@("!?)V`@("`G)U\M+2<*("`@("`@("`@("`@("`@("`@
955 M("`@?"`@("`@("`@("`@("`@("`@("`O("`@?"T]+BXM+2<*("`@("`@("`@
956 M("`@("`@(%\@("`@("<N("`@("`@("`N7R`@(%\N+2=@)R`@(&!?7PH@("`@
957 M("`@("`@("`@("PG("`N("PG("TL3%]?+WP@(%P@8"<G+&\N("`@+5\@("`@
958 M)V`M+2X*("`@("`@("`@("`@("`@+BPG(BT@("`O("`@("`@("`@?"`@8'8@
959 M+R`N("PG8"TM+BXG7R`G+@H@("`@("`@("`@("`@("`@("TN+E\L)R`@("`@
960 M('P@("!\("`@)RTM+E]@("!?+R`@('P@("!\"B`@("`@("`@("`@("`@("`@
961 M("`@("`@("`@("=@8"<@6R`@("`@("`@("=@)R`@("`L+RXN+PH@("`@("`@
962 M("`@("`@("`@("`@("`@("`@('P@("`M+B<@("`@("`@("`@("`@("Y;("`@
963 M+PH@("`@("`@("`@("`@("`@("`@(%]?7U]?7RX@("`L.E]?7U]?7U]?7U]?
964 M7U]?7RQ@("PG"B`@("TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2T@+2(M+2TM
965 M+2TM+2TM+2TM+2TM+2T]+0H@+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
966 6+2TM+2TM+2TM+2TM+2TM+2TM+2TM"@``