X-Git-Url: https://git.treefish.org/fex.git/blobdiff_plain/7fa382617fbaccc0ce522b2b3adbbee9db5ad227..e5c93609849bda051fff54b5d5265af5608c6c69:/cgi-bin/sex diff --git a/cgi-bin/sex b/cgi-bin/sex index d483ebe..ab7abc8 100755 --- a/cgi-bin/sex +++ b/cgi-bin/sex @@ -15,14 +15,14 @@ die "$0: no $FEXLIB\n" unless -d $FEXLIB; $| = 1; # import from fex.pp -our ($tmpdir,$logdir,$timeout,$fra,$bs); +our ($tmpdir,@logdir,$timeout,$fra,$bs); # load common code, local config: $HOME/lib/fex.ph require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n"; chdir $spooldir or error(500,"$spooldir - $!"); -my $debuglog = "$tmpdir/sex.log"; +# my $debuglog = "$tmpdir/sex.log"; my $ra = $ENV{REMOTE_ADDR}||0; $fra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR}; $timeout *= 10; @@ -72,24 +72,24 @@ if ($mode eq 'PUSH') { my $lock = "$stream/lock"; open $lock,'>>',$lock or error(503,"Cannot open $lock : $!"); flock $lock,LOCK_EX|LOCK_NB or error(409,"$stream already in use"); - + chmod 0600,$fifo; unlink "$stream/mode"; unlink "$stream/type"; symlink $pmode,"$stream/mode" if $pmode; symlink $type, "$stream/type" if $type; - $SIG{PIPE} = sub { - sleep 1; - rmrf($stream); - exit; + $SIG{PIPE} = sub { + sleep 1; + rmrf($stream); + exit; }; - $SIG{ALRM} = sub { - syswrite STDOUT,"."; - exit if $!; - $ALARM = 1; + $SIG{ALRM} = sub { + syswrite STDOUT,"."; + exit if $!; + $ALARM = 1; }; - syswrite STDOUT,"HTTP/1.9 199 Hold on"; + syswrite STDOUT,"HTTP/1.9 199 Hold on"; for (my $i=0;$i<$timeout;$i++) { alarm(1); $ALARM = 0; @@ -98,13 +98,13 @@ if ($mode eq 'PUSH') { unless ($ALARM) { error(503,"Cannot open $fifo : $!") } } alarm(0); - syswrite STDOUT,"\r\n"; - - unless (fileno $fifo) { + syswrite STDOUT,"\r\n"; + + unless (fileno $fifo) { rmrf($stream); error(504,"Timeout"); } - + header('200 OK'); $B = 0; @@ -120,7 +120,7 @@ if ($mode eq 'PUSH') { } elsif ($mode eq 'POP') { $stream =~ s:/STDSTR:/PUBLIC: if $id eq 'public'; - unless ($id eq 'public' and (readlink "$stream/mode"||'') eq 'PUBLIC' + unless ($id eq 'public' and (readlink "$stream/mode"||'') eq 'PUBLIC' or $user =~ /^anonymous/) { &authentificate; } @@ -135,13 +135,13 @@ elsif ($mode eq 'POP') { alarm(0); header('200 OK',$type); sexlog($mode); - + while (sysread($fifo,$_,$bs)) { syswrite STDOUT,$_ or die $!; } exit; - -} + +} else { error(405,"Unknown Request"); } @@ -151,27 +151,35 @@ exit; sub setparam { my ($v,$vv) = @_; - + $v = uc(despace($v)); $vv = untaint(normalize($vv)); # $param{$v} = $vv; - if ($v eq 'USER') { $user = lc(despace($vv)) } - elsif ($v eq 'ID') { $id = despace($vv) } - elsif ($v eq 'MODE') { $pmode = uc(despace($vv)) } - elsif ($v eq 'TYPE') { $type = uc(despace($vv)) } + if ($v eq 'USER') { $user = lc(despace($vv)) } + elsif ($v eq 'ID') { $id = despace($vv) } + elsif ($v eq 'MODE') { $pmode = uc(despace($vv)) } + elsif ($v eq 'TYPE') { $type = uc(despace($vv)) } elsif ($v eq 'STREAM') { $stream = normalize_filename($vv) } - elsif ($v eq 'BS' and $vv =~ /(\d+)/) { $bs = $1 } + elsif ($v eq 'BS' and $vv =~ /(\d+)/) { $bs = $1 } elsif ($v eq 'TIMEOUT' and $vv =~ /(\d+)/) { $timeout = $1 } elsif ($v eq 'ANONYMOUS') { $id = $user ='anonymous'; $stream = $vv; } } sub sexlog { - if (open my $log,'>>',"$logdir/sex.log") { - flock $log,LOCK_EX; - seek $log,0,SEEK_END; - printf {$log} "%s [%s_%s] %s (%s) %s\n", - isodate(time),$$,$ENV{REQUESTCOUNT},$user,$fra,"@_"; - close $log; + my $msg = "@_"; + + $msg =~ s/\n/ /g; + $msg =~ s/\s+$//; + $msg = sprintf "%s [%s_%s] %s (%s) %s\n", + isodate(time),$$,$ENV{REQUESTCOUNT},$user,$fra,$msg; + + foreach my $log (@logdir) { + if (open $log,'>>',"$log/sex.log") { + flock $log,LOCK_EX; + seek $log,0,SEEK_END; + printf {$log} $msg; + close $log; + } } } @@ -183,10 +191,20 @@ sub sigdie { sub sigexit { my ($sig) = @_; - if (open my $log,'>>',"$logdir/sex.log") { - printf {$log} "%s %s (%s) caught SIGNAL %s\n", - isodate(time),$user||'-',$fra||'-',"@_"; - close $log; + my $msg = "@_"; + + $msg =~ s/\n/ /g; + $msg =~ s/\s+$//; + $msg = sprintf "%s %s (%s) caught SIGNAL %s\n", + isodate(time),$user||'-',$fra||'-',$msg; + + foreach my $log (@logdir) { + if (open $log,'>>',"$log/sex.log") { + flock $log,LOCK_EX; + seek $log,0,SEEK_END; + printf {$log} $msg; + close $log; + } } if ($sig eq 'DIE') { shift;