2016-03-17 23:19:44 +01:00
|
|
|
package Lemonldap::NG::Handler::Main::Init;
|
|
|
|
|
2022-07-28 12:42:15 +02:00
|
|
|
our $VERSION = '2.0.15';
|
2017-02-08 23:18:52 +01:00
|
|
|
|
|
|
|
package Lemonldap::NG::Handler::Main;
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
use strict;
|
|
|
|
use Lemonldap::NG::Common::Conf;
|
|
|
|
|
2018-06-11 22:11:18 +02:00
|
|
|
our $statusInit = 1;
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
## @imethod void init(hashRef args)
|
|
|
|
# Read parameters and build the Lemonldap::NG::Common::Conf object.
|
|
|
|
# @param $args hash containing parameters
|
|
|
|
sub init($$) {
|
|
|
|
my ( $class, $args ) = @_;
|
|
|
|
|
|
|
|
# According to doc, localStorage can be declared in $args root,
|
|
|
|
# but it must be in $args->{configStorage}
|
|
|
|
foreach (qw(localStorage localStorageOptions)) {
|
|
|
|
$args->{configStorage}->{$_} ||= $args->{$_};
|
|
|
|
}
|
|
|
|
|
|
|
|
my $tmp = Lemonldap::NG::Common::Conf->new( $args->{configStorage} );
|
2016-03-24 07:23:37 +01:00
|
|
|
unless ( $class->confAcc($tmp) ) {
|
2016-03-17 23:19:44 +01:00
|
|
|
die( "$class : unable to build configuration: "
|
|
|
|
. "$Lemonldap::NG::Common::Conf::msg" );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Merge local configuration parameters so that params defined in
|
|
|
|
# startup parameters have precedence over lemonldap-ng.ini params
|
|
|
|
$class->localConfig(
|
2016-03-24 07:23:37 +01:00
|
|
|
{ %{ $class->confAcc->getLocalConf('handler') }, %{$args} } );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
$class->checkTime( $class->localConfig->{checkTime} || $class->checkTime );
|
|
|
|
|
|
|
|
# Few actions that must be done at server startup:
|
|
|
|
# * set log level for Lemonldap::NG logs
|
2017-02-12 21:19:39 +01:00
|
|
|
$class->logLevelInit();
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# * set server signature
|
|
|
|
$class->serverSignatureInit unless ( $class->localConfig->{hideSignature} );
|
|
|
|
|
|
|
|
# * launch status process
|
2018-06-12 21:00:10 +02:00
|
|
|
$class->statusInit();
|
2016-03-17 23:19:44 +01:00
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# @method void logLevelInit
|
|
|
|
# Set log level for Lemonldap::NG logs
|
|
|
|
sub logLevelInit {
|
2017-02-12 21:19:39 +01:00
|
|
|
my ($class) = @_;
|
2022-07-28 12:11:16 +02:00
|
|
|
my $logger = $class->localConfig->{logger} ||=
|
|
|
|
$ENV{LLNG_DEFAULTLOGGER} || $class->defaultLogger;
|
2017-02-12 21:19:39 +01:00
|
|
|
eval "require $logger";
|
|
|
|
die $@ if ($@);
|
2022-02-13 14:10:30 +01:00
|
|
|
my $err;
|
|
|
|
unless ( $class->localConfig->{logLevel} =~
|
|
|
|
/^(?:debug|info|notice|warn|error)$/ )
|
2019-04-16 17:01:00 +02:00
|
|
|
{
|
2022-02-13 14:10:30 +01:00
|
|
|
$err =
|
|
|
|
'Bad logLevel value \''
|
2019-04-16 17:01:00 +02:00
|
|
|
. $class->localConfig->{logLevel}
|
|
|
|
. "', switching to 'info'\n";
|
|
|
|
$class->localConfig->{logLevel} = 'info';
|
|
|
|
}
|
2017-02-13 12:58:39 +01:00
|
|
|
$class->logger( $logger->new( $class->localConfig ) );
|
2022-02-13 14:10:30 +01:00
|
|
|
$class->logger->error($err) if $err;
|
2017-02-21 07:41:50 +01:00
|
|
|
$class->logger->debug("Logger $logger loaded");
|
2017-02-13 12:58:39 +01:00
|
|
|
$logger = $class->localConfig->{userLogger} || $logger;
|
|
|
|
eval "require $logger";
|
|
|
|
die $@ if ($@);
|
2019-07-04 21:21:49 +02:00
|
|
|
require Lemonldap::NG::Common::Logger::_Duplicate;
|
|
|
|
$class->userLogger(
|
|
|
|
Lemonldap::NG::Common::Logger::_Duplicate->new(
|
|
|
|
$class->localConfig,
|
|
|
|
user => 1,
|
|
|
|
logger => $logger,
|
|
|
|
dup => $class->logger
|
|
|
|
)
|
|
|
|
);
|
2017-02-21 07:41:50 +01:00
|
|
|
$class->logger->debug("User logger $logger loaded");
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# @method void serverSignatureInit
|
|
|
|
# adapt server signature
|
|
|
|
sub serverSignatureInit {
|
|
|
|
my $class = shift;
|
2019-02-01 11:21:25 +01:00
|
|
|
require Lemonldap::NG::Handler;
|
2019-01-16 12:31:42 +01:00
|
|
|
my $version = $Lemonldap::NG::Handler::VERSION;
|
|
|
|
$class->setServerSignature("Lemonldap::NG/$version");
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## @ifn protected void statusInit()
|
|
|
|
# Launch the status process
|
|
|
|
sub statusInit {
|
|
|
|
my ($class) = @_;
|
2018-06-12 21:00:10 +02:00
|
|
|
return unless ( $class->localConfig->{status} and $statusInit );
|
|
|
|
$statusInit = 0;
|
|
|
|
return if ( $class->tsv->{statusPipe} );
|
|
|
|
if ( $ENV{LLNGSTATUSHOST} ) {
|
|
|
|
require IO::Socket::INET;
|
|
|
|
$class->tsv->{statusPipe} = IO::Socket::INET->new(
|
|
|
|
Proto => 'udp',
|
|
|
|
PeerAddr => $ENV{LLNGSTATUSHOST}
|
|
|
|
);
|
|
|
|
$class->tsv->{statusOut} = undef;
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-06-12 21:00:10 +02:00
|
|
|
require IO::Pipe;
|
|
|
|
my $statusPipe = IO::Pipe->new;
|
|
|
|
my $statusOut = IO::Pipe->new;
|
|
|
|
if ( my $pid = fork() ) {
|
|
|
|
$class->logger->debug("Status collector launched ($pid)");
|
|
|
|
$statusPipe->writer();
|
|
|
|
$statusOut->reader();
|
|
|
|
$statusPipe->autoflush(1);
|
|
|
|
( $class->tsv->{statusPipe}, $class->tsv->{statusOut} ) =
|
|
|
|
( $statusPipe, $statusOut );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$statusPipe->reader();
|
|
|
|
$statusOut->writer();
|
|
|
|
my $fdin = $statusPipe->fileno;
|
|
|
|
my $fdout = $statusOut->fileno;
|
|
|
|
open STDIN, "<&$fdin";
|
|
|
|
open STDOUT, ">&$fdout";
|
|
|
|
my $perl_exec = ( $^X =~ /perl/ ) ? $^X : 'perl';
|
|
|
|
exec $perl_exec, '-MLemonldap::NG::Handler::Lib::Status',
|
|
|
|
|
|
|
|
# Insert @INC in Perl path
|
2019-02-05 23:12:17 +01:00
|
|
|
map( { "-I$_" } @INC ),
|
2018-06-12 21:00:10 +02:00
|
|
|
|
|
|
|
# Command to launch
|
|
|
|
'-e', '&Lemonldap::NG::Handler::Lib::Status::run()',
|
|
|
|
|
|
|
|
# Optional arg: UDP socket to listen to
|
|
|
|
(
|
|
|
|
$ENV{LLNGSTATUSLISTEN}
|
|
|
|
? ( '--', '--udp', $ENV{LLNGSTATUSLISTEN} )
|
|
|
|
: ()
|
|
|
|
);
|
|
|
|
}
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|