diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session.pm index 926ac6d7c..b96989397 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session.pm @@ -248,7 +248,7 @@ sub _FileGKFAS { closedir DIR; my %res; for my $f (@t) { - open F, "$args->{Directory}/$f"; + open F, '<', "$args->{Directory}/$f"; my $row = join '', ; if ( ref($data) eq 'CODE' ) { eval { $res{$f} = &$data( $args->{unserialize}->($row), $f ); }; @@ -288,7 +288,7 @@ sub _PHPGKFAS { closedir DIR; my %res; for my $f (@t) { - open F, "$args->{SavePath}/$f"; + open F, '<', "$args->{SavePath}/$f"; my $row = join '', ; if ( ref($data) eq 'CODE' ) { $res{$f} = diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/File.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/File.pm index 0e3b8cb75..adb88fa3a 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/File.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/File.pm @@ -40,7 +40,7 @@ sub available { closedir D; @conf = sort { $a <=> $b } - map { /lmConf-(\d+)(?:\.js(?:on))?/ ? ( $1 + 0 ) : () } @conf; + map { /lmConf-(\d+)(?:\.js(?:on))?/ ? ( $1 + 0 ) : () } @conf; return @conf; } @@ -106,7 +106,7 @@ sub load { if ($filename) { local $/ = ''; my $ret; - unless ( open FILE, $filename ) { + unless ( open FILE, '<', $filename ) { $Lemonldap::NG::Common::Conf::msg .= "Read error: $!$@"; return undef; } @@ -124,9 +124,9 @@ sub load { # Old format elsif ( -e "$self->{dirName}/lmConf-$cfgNum" ) { - open FILE, "$self->{dirName}/lmConf-$cfgNum" or die "$!$@"; + open FILE, '<', "$self->{dirName}/lmConf-$cfgNum" or die "$!$@"; local $/ = ""; - unless ( open FILE, $self->{dirName} . "/lmConf-$cfgNum" ) { + unless ( open FILE, '<', $self->{dirName} . "/lmConf-$cfgNum" ) { $Lemonldap::NG::Common::Conf::msg .= "Open file failed: $! \n"; return undef; } diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/YAMLFile.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/YAMLFile.pm index 8b89ef4a9..af59d3e67 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/YAMLFile.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/YAMLFile.pm @@ -101,7 +101,7 @@ sub load { $filename = $self->_yamlFile($cfgNum); local $/ = ''; my $ret; - unless ( open FILE, $filename ) { + unless ( open FILE, '<', $filename ) { $Lemonldap::NG::Common::Conf::msg .= "Read error: $!$@"; return undef; } diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm index 23f5f98ec..e2541c6b9 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Notifications/File.pm @@ -47,7 +47,7 @@ sub get { my $files; foreach my $file (@notif) { - unless ( open F, $self->{dirName} . "/$file" ) { + unless ( open F, '<', $self->{dirName} . "/$file" ) { $self->logger->error( "Unable to read notification $self->{dirName}/$file"); next; @@ -116,7 +116,8 @@ sub newNotif { return ( 0, 'This notification still exists' ) if ( -e $filename ); my $old = ( $filename =~ /(.*?)(?:\.$ext)$/ )[0] . '.done'; return ( 0, 'This notification has been done' ) if ( -e $old ); - open my $F, ">$filename" or return ( 0, "Unable to create $filename ($!)" ); + open my $F, '>', $filename + or return ( 0, "Unable to create $filename ($!)" ); binmode($F); print $F $content; return ( 0, "Unable to close $filename ($!)" ) unless ( close $F ); diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm index bb6a31bb3..767b2efff 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm @@ -122,7 +122,7 @@ sub userError { sub sendJSONresponse { my ( $self, $req, $j, %args ) = @_; $args{code} ||= 200; - $args{headers} ||= $req->respHeaders || []; + $args{headers} ||= [ $req->spliceHdrs ]; my $type = 'application/json; charset=utf-8'; if ( ref $j ) { eval { $j = $_json->encode($j); }; @@ -152,9 +152,9 @@ sub sendError { return [ $code, [ - 'Content-Type' => 'application/xml; charset=utf-8', - @{ $req->respHeaders || [] }, - 'Content-Length' => length($s) + 'Content-Type' => 'application/xml; charset=utf-8', + 'Content-Length' => length($s), + $req->spliceHdrs, ], [$s] ]; @@ -189,8 +189,9 @@ body{background:#000;color:#fff;padding:10px 50px;font-family:sans-serif;}a{text return [ $code, [ - 'Content-Type' => 'text/html; charset=utf-8', - @{ $req->respHeaders || [] }, 'Content-Length' => length($s) + 'Content-Type' => 'text/html; charset=utf-8', + 'Content-Length' => length($s), + $req->spliceHdrs, ], [$s] ]; @@ -248,7 +249,7 @@ sub sendHtml { $sc = '.' unless ($sc); $sc =~ s#/*$#/#; $args{code} ||= 200; - $args{headers} ||= $req->respHeaders || []; + $args{headers} ||= [ $req->spliceHdrs ]; my $htpl; unless ( ref $template ) { diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Request.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Request.pm index b939fa604..9358f677d 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Request.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Request.pm @@ -58,6 +58,11 @@ sub respHeaders { return $self->{respHeaders}; } +sub spliceHdrs { + my ($self) = @_; + return splice @{ $self->{respHeaders} }; +} + sub accept { $_[0]->env->{HTTP_ACCEPT} } sub encodings { $_[0]->env->{HTTP_ACCEPT_ENCODING} } sub languages { $_[0]->env->{HTTP_ACCEPT_LANGUAGE} } @@ -192,6 +197,10 @@ Example: # Add header $req->respHeaders->{"X-Key"} = "Value"; +=head2 spliceHdrs + +Returns headers array and flush it. + =head2 set_param( $key, $value ) L param() method is read-only. This method can be used to diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/PSGI.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/PSGI.pm index 1b8965f75..43b12899e 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/PSGI.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/PSGI.pm @@ -69,7 +69,7 @@ sub _run { return sub { my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); my $res = $self->handler($req); - push @{ $res->[1] }, @{ $req->{respHeaders} }; + push @{ $res->[1] }, $req->spliceHdrs; return $res; }; } @@ -89,7 +89,7 @@ sub status { return sub { my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); $self->api->status($req); - return [ 200, [ @{ $req->{respHeaders} } ], [ $req->{respBody} ] ]; + return [ 200, [ $req->spliceHdrs ], [ $req->{respBody} ] ]; }; } @@ -107,7 +107,7 @@ sub reload { return sub { my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); $self->api->reload($req); - return [ 200, [ @{ $req->{respHeaders} } ], [ $req->{respBody} ] ]; + return [ 200, [ $req->spliceHdrs ], [ $req->{respBody} ] ]; }; } @@ -132,28 +132,19 @@ sub _authAndTrace { if ( $res < 300 ) { if ($noCall) { - return [ $res, $req->{respHeaders}, [] ]; + return [ $res, [ $req->spliceHdrs ], [] ]; } else { $self->logger->debug('User authenticated, calling handler()'); $res = $self->handler($req); - - # Insert respHeaders in response only if not already set - my %hdr1 = @{ $res->[1] }; - my %hdr2 = @{ $req->{respHeaders} }; - foreach ( keys %hdr2 ) { - unless ( $hdr1{$_} and $hdr2{$_} eq $hdr1{$_} ) { - push @{ $res->[1] }, ( $_ => $hdr2{$_} ); - } - } + push @{ $res->[1] }, $req->spliceHdrs; + return $res; } - return $res; } elsif ( $res < 400 ) { - return [ $res, $req->{respHeaders}, [] ]; + return [ $res, [ $req->spliceHdrs ], [] ]; } else { - my %h = $req->{respHeaders} ? @{ $req->{respHeaders} } : (); my $s = $type->tsv->{portal}->() . "/lmerror/$res"; $s = 'Redirection' @@ -161,9 +152,15 @@ sub _authAndTrace { . '

Please wait

' . qq{

An error occurs, you're going to be redirected to $s.

} . ''; - $h{'Content-Type'} = 'text/html'; - $h{'Content-Length'} = length $s; - return [ $res, [%h], [$s] ]; + return [ + $res, + [ + $req->spliceHdrs, + 'Content-Type' => 'text/html', + 'Content-Length' => length $s + ], + [$s] + ]; } } diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/SecureToken.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/SecureToken.pm index 731663ab1..8a046ab89 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/SecureToken.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/SecureToken.pm @@ -33,7 +33,7 @@ BEGIN { sub run { my $class = shift; my $r = $_[0]; - my $ret = $class->Lemonldap::NG::Handler::Main::run($r); + my ( $ret, $session ) = $class->Lemonldap::NG::Handler::Main::run($r); # Continue only if user is authorized return $ret unless ( $ret == $class->OK ); @@ -107,7 +107,7 @@ sub run { eval 'use Apache2::Filter' unless ( $INC{"Apache2/Filter.pm"} ); if ( $INC{"Apache2/Filter.pm"} ) { - $r->add_output_filter( + $r->{env}->{'psgi.r'}->add_output_filter( sub { my $f = shift; while ( $f->read( my $buffer, 1024 ) ) { @@ -158,7 +158,7 @@ sub _setToken { $secureTokenExpiration ); unless ($res) { - $class->( "Unable to store secure token $key", 'error' ); + $class->logger->error("Unable to store secure token $key"); return; } @@ -177,7 +177,7 @@ sub _deleteToken { my $res = $secureTokenMemcachedConnection->delete($key); unless ($res) { - $class->( "Unable to delete secure token $key", 'error' ); + $class->logger->error("Unable to delete secure token $key"); } else { $class->logger->info("Token $key deleted"); @@ -201,15 +201,14 @@ sub _isAlive { my $total_c = $stats->{'total'}->{'connection_structures'}; my $total_i = $stats->{'total'}->{'total_items'}; - $class->( -"Memcached connection is alive ($total_c connections / $total_i items)", - 'debug' + $class->logger->debug->( +"Memcached connection is alive ($total_c connections / $total_i items)" ); return 1; } - $class->( "Memcached connection is not alive", 'error' ); + $class->logger->error("Memcached connection is not alive"); return 0; } @@ -222,7 +221,7 @@ sub _returnError { my ( $class, $r, $secureTokenAllowOnError ) = @_; if ($secureTokenAllowOnError) { - $class->( "Allow request without secure token", 'debug' ); + $class->logger->debug("Allow request without secure token"); return $class->OK; } diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Reload.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Reload.pm index 0db62c7b4..c755fe97c 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Reload.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Reload.pm @@ -348,16 +348,18 @@ sub sessionStorageInit { if ( $conf->{status} ) { my $params = ""; if ( $class->tsv->{sessionCacheModule} ) { - require Data::Dumper; - $params = ' ' - . $class->tsv->{sessionCacheModule} . ',' - . Data::Dumper->new( [ $class->tsv->{sessionCacheOptions} ] ) - ->Terse(1)->Indent(0)->Dump; # To send params on one line + $params = ' ' . join( + ',', + $class->tsv->{sessionCacheModule} . map { + "$_ => " + . $class->tsv->{sessionCacheOptions}->{$_} + } keys %{ $class->tsv->{sessionCacheOptions} // {} } + ); } $class->tsv->{statusPipe}->print("RELOADCACHE $params\n"); } } - return 1; +return 1; } ## @imethod void headersInit(hashRef args) diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm index 261156618..1976606b6 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm @@ -147,7 +147,9 @@ sub run { # ACCOUNTING (1. Inform web server) $class->set_user( $req, $session->{ $class->tsv->{whatToTrace} } ); - $class->set_custom( $req, $session->{ $class->tsv->{customToTrace} } ); + $class->set_custom( $req, $session->{ $class->tsv->{customToTrace} } ) + if $class->tsv->{customToTrace} + and $session->{ $class->tsv->{customToTrace} }; # AUTHORIZATION return ( $class->forbidden( $req, $session ), $session ) @@ -409,7 +411,7 @@ sub fetchId { my $value = $lookForHttpCookie ? ( $t =~ /${cn}http=([^,; ]+)/o ? $1 : 0 ) - : ( $t =~ /$cn=([^,; ]+)/o ? $1 : 0 ); + : ( $t =~ /$cn=([^,; ]+)/o ? $1 : 0 ); if ( $value && $lookForHttpCookie && $class->tsv->{securedCookie} == 3 ) { $value = $class->tsv->{cipher}->decryptHex( $value, "http" ); diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Try.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Try.pm index 71c011f1e..a705561a4 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Try.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Try.pm @@ -47,7 +47,7 @@ sub addAuthRouteWithRedirect { sub _auth_and_redirect { my ( $self, $req ) = @_; $self->api->goToPortal( $req, $req->{env}->{REQUEST_URI} ); - return [ 302, $req->respHeaders, [] ]; + return [ 302, [$req->spliceHdrs], [] ]; } sub defaultAuthRoute { @@ -71,6 +71,7 @@ sub _run { if ( $res->[0] < 300 ) { $self->routes( $self->authRoutes ); $req->userData( $self->api->data ); + $req->respHeaders($res->[1]); } elsif ( $res->[0] != 403 and not $req->data->{noTry} ) { @@ -85,15 +86,7 @@ sub _run { return $res; } $res = $self->handler($req); - - # Insert respHeaders in response only if not already set - my %hdr1 = @{ $res->[1] }; - my %hdr2 = @{ $req->{respHeaders} }; - foreach ( keys %hdr2 ) { - unless ( $hdr1{$_} and $hdr2{$_} eq $hdr1{$_} ) { - push @{ $res->[1] }, ( $_ => $hdr2{$_} ); - } - } + push @{ $res->[1] }, $req->spliceHdrs; return $res; }; } diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server.pm index f40ad76a6..22df99df1 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server.pm @@ -26,7 +26,7 @@ sub _run { return sub { my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); my $res = $self->_authAndTrace($req); - push @{ $res->[1] }, @{ $req->respHeaders }, + push @{ $res->[1] }, $req->spliceHdrs, Cookie => ( $req->{Cookie} // '' ); return $res; }; diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server/Nginx.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server/Nginx.pm index ec109b479..bb95ad68c 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server/Nginx.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server/Nginx.pm @@ -65,13 +65,10 @@ sub _run { #@param $req Lemonldap::NG::Common::PSGI::Request sub handler { my ( $self, $req ) = @_; - my $hdrs = $req->{respHeaders}; - $req->{respHeaders} = []; my @convertedHdrs = ( 'Content-Length' => 0, Cookie => ( $req->env->{HTTP_COOKIE} // '' ) ); my $i = 0; - while ( my $k = shift @$hdrs ) { - my $v = shift @$hdrs; + while ( my ( $k, $v ) = splice( @{ $req->{respHeaders} }, 0, 2 ) ) { if ( $k =~ /^(?:Lm-Remote-(?:User|Custom)|Cookie)$/ ) { push @convertedHdrs, $k, $v; } diff --git a/lemonldap-ng-handler/t/65-Lemonldap-NG-Handler-PSGI-ServiceToken.t b/lemonldap-ng-handler/t/65-Lemonldap-NG-Handler-PSGI-ServiceToken.t index 46423d1f3..97fc3346e 100644 --- a/lemonldap-ng-handler/t/65-Lemonldap-NG-Handler-PSGI-ServiceToken.t +++ b/lemonldap-ng-handler/t/65-Lemonldap-NG-Handler-PSGI-ServiceToken.t @@ -53,9 +53,9 @@ count(2); my @headers = grep { /service|^XFromVH$/ } @{ $res->[1] }; my @values = grep { /\.example\.com|^$sessionId$/ } @{ $res->[1] }; -ok( @headers == 4, 'Found 4 service headers' ) +ok( @headers == 2, 'Found 2 service headers' ) or print STDERR Data::Dumper::Dumper( $res->[1] ); -ok( @values == 4, 'Found 4 service header values' ) +ok( @values == 2, 'Found 2 service header values' ) or print STDERR Data::Dumper::Dumper( $res->[1] ); count(2); @@ -75,9 +75,9 @@ count(2); @headers = grep { /service|^XFromVH$/ } @{ $res->[1] }; @values = grep { /\.example\.com|^$sessionId$/ } @{ $res->[1] }; -ok( @headers == 4, 'Found 4 service headers' ) +ok( @headers == 2, 'Found 2 service headers' ) or print STDERR Data::Dumper::Dumper( $res->[1] ); -ok( @values == 4, 'Found 4 service header values' ) +ok( @values == 2, 'Found 2 service header values' ) or print STDERR Data::Dumper::Dumper( $res->[1] ); count(2); @@ -116,9 +116,9 @@ count(2); @headers = grep { /service|^XFromVH$/ } @{ $res->[1] }; @values = grep { /\.example\.com|^$sessionId$/ } @{ $res->[1] }; -ok( @headers == 4, 'Found 4 service headers' ) +ok( @headers == 2, 'Found 2 service headers' ) or print STDERR Data::Dumper::Dumper( $res->[1] ); -ok( @values == 4, 'Found 4 service header values' ) +ok( @values == 2, 'Found 2 service header values' ) or print STDERR Data::Dumper::Dumper( $res->[1] ); count(2); diff --git a/lemonldap-ng-manager/site/templates/header.tpl b/lemonldap-ng-manager/site/templates/header.tpl index 4cb6e383f..37c14ddb5 100644 --- a/lemonldap-ng-manager/site/templates/header.tpl +++ b/lemonldap-ng-manager/site/templates/header.tpl @@ -4,6 +4,7 @@ + logos/favicon.ico" /> logos/favicon.ico" />