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
|
# Apache2 FastCGI client to query remote LL::NG FastCGI server
#
package Lemonldap::NG::SSOaaS::Apache::Client;
use strict;
use Apache2::Connection;
use Apache2::RequestUtil;
use Apache2::RequestRec;
use Apache2::Const -compile =>
qw(FORBIDDEN HTTP_UNAUTHORIZED REDIRECT OK DECLINED DONE SERVER_ERROR AUTH_REQUIRED HTTP_SERVICE_UNAVAILABLE);
use Apache2::Log;
use APR::Table;
use IO::Socket::INET;
use FCGI::Client;
use URI;
use URI::Escape qw(uri_unescape);
use constant FORBIDDEN => Apache2::Const::FORBIDDEN;
use constant HTTP_UNAUTHORIZED => Apache2::Const::HTTP_UNAUTHORIZED;
use constant REDIRECT => Apache2::Const::REDIRECT;
use constant DECLINED => Apache2::Const::DECLINED;
use constant SERVER_ERROR => Apache2::Const::SERVER_ERROR;
our $VERSION = '2.18.0';
sub handler {
my ( $class, $r ) = @_;
$r ||= $class;
my ( $uri, $args ) = ( $r->uri, $r->args );
my $uri_full = $uri . ( $args ? "?$args" : '' );
my $env = {
#%ENV,
HTTP_HOST => $r->hostname,
REMOTE_ADDR => (
$r->connection->can('remote_ip')
? $r->connection->remote_ip
: $r->connection->client_ip
),
QUERY_STRING => $args,
REQUEST_URI => $uri_full,
PATH_INFO => '',
SERVER_PORT => $r->get_server_port,
REQUEST_METHOD => $r->method,
};
foreach (qw(VHOSTTYPE RULES_URL HTTPS_REDIRECT PORT_REDIRECT)) {
if ( my $t = $r->dir_config($_) ) {
$env->{$_} = $t;
}
}
$r->headers_in->do(
sub {
my $h = shift;
my $k = uc($h);
if ( $k ne 'HOST' ) {
$k =~ s/-/_/g;
$env->{"HTTP_$k"} = $r->headers_in->{$h};
}
return 1;
}
);
$uri = URI->new( "http://" . $r->hostname . $r->unparsed_uri );
$env->{PATH_INFO} = uri_unescape( $uri->path );
my ( $host, $port ) = ( $r->dir_config('LLNG_SERVER') =~ /^(.*):(\d+)$/ );
unless ( $host and $port ) {
print STDERR 'Missing or bad LLNG_SERVER';
return SERVER_ERROR;
}
my $sock = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
) or die $!;
foreach ( keys %$env ) {
delete $env->{$_} unless ( length $env->{$_} );
}
my ( $stdout, $stderr, $status ) =
FCGI::Client::Connection->new( sock => $sock )->request($env);
my %hdrs =
map { m/(.*?):\s*(.*)/; } split /(?:\r?\n)+/, $stdout;
if ( $hdrs{Status} =~ /^(\d+)\s+(.*?)$/ ) {
$status = $1;
}
else {
print STDERR "Bad status line $hdrs{Status}\n";
return SERVER_ERROR;
}
if ( ( $status == 302 or $status == 401 ) and $hdrs{Location} ) {
$r->err_headers_out->set( Location => $hdrs{Location} );
return REDIRECT;
}
$r->user( $hdrs{'Lm-Remote-User'} ) if $hdrs{'Lm-Remote-User'};
$r->subprocess_env( REMOTE_CUSTOM => $hdrs{'Lm-Remote-Custom'} )
if $hdrs{'Lm-Remote-Custom'};
my $i = 1;
while ( $hdrs{"Headername$i"} ) {
$r->headers_in->set( $hdrs{"Headername$i"} => $hdrs{"Headervalue$i"} )
if $hdrs{"Headervalue$i"};
$i++;
}
$status = DECLINED if ( $status < 300 );
return $status;
}
1;
__END__
=pod
=encoding utf8
=head1 NAME
Lemonldap::NG::SSOaaS::Apache::Client - Apache client for Lemonldap::NG
FastCGI server.
=head1 SYNOPSIS
In apache2.conf:
<VirtualHost ...>
PerlHeaderParserHandler Lemonldap::NG::SSOaaS::Apache::Client
PerlSetVar LLNG_SERVER 127.0.0.1:9090
PerlSetVar VHOSTTYPE DevOps
# or PerlSetVar VHOSTTYPE DevOpsST
PerlSetVar HOST HTTP_HOST
PerlSetVar HTTPS_REDIRECT HTTPS
PerlSetVar PORT_REDIRECT SERVER_PORT
PerlSetVar RULES_URL http://app.tld/rules.json
...
</VirtualHost>
=head1 DESCRIPTION
Lemonldap::NG::SSOaaS::Apache::Client is an alternative to
L<Lemonldap::NG::Handler::ApacheMP2> that replace inside handler. It calls a
remote Lemonldap::NG FastCGI server to get authentication, authorization and
headers.
=head1 SEE ALSO
L<Lemonldap::NG::Handler::ApacheMP2>,
L<https://lemonldap-ng.org/documentation/latest/ssoaas>
=head1 AUTHORS
=over
=item LemonLDAP::NG team L<https://lemonldap-ng.org/team.html>
=back
=head1 BUG REPORT
Use OW2 system to report bug or ask for features:
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
=head1 DOWNLOAD
Lemonldap::NG is available at
L<https://lemonldap-ng.org/download.html>
=head1 COPYRIGHT AND LICENSE
See COPYING file for details.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see L<http://www.gnu.org/licenses/>.
=cut
|