Prevent to die if Custom Functions package is not found

This commit is contained in:
Christophe Maudoux 2020-03-20 22:24:13 +01:00
parent ff75e28efc
commit e67c81bdca
4 changed files with 34 additions and 25 deletions

View File

@ -18,6 +18,9 @@
; CUSTOM FUNCTION
; If you want to create customFunctions in rules, declare them here:
;require = Package
; Prevent Portal to crash if Perl module is not found
;requireDontDie = 1
;customFunctions = function1 function2
;customFunctions = Package::func1 Package::func2

View File

@ -27,7 +27,7 @@ use Config::IniFiles;
#inherits Lemonldap::NG::Common::Conf::Backends::SOAP
#inherits Lemonldap::NG::Common::Conf::Backends::LDAP
our $VERSION = '2.0.6';
our $VERSION = '2.0.8';
our $msg = '';
our $iniObj;
@ -304,7 +304,7 @@ sub getLocalConf {
$cfg = Config::IniFiles->new( -file => $file, -allowcontinue => 1 );
unless ( defined $cfg ) {
$msg .= "Local config error: " . @Config::IniFiles::errors . "\n";
$msg .= "Local config Error: " . @Config::IniFiles::errors . "\n";
return $r;
}
@ -334,7 +334,7 @@ sub getLocalConf {
{
eval "\$r->{$_} = $r->{$_}";
if ($@) {
$msg .= "Warning: error in file $file: $@.\n";
$msg .= "Warn: error in file $file: $@.\n";
return $r;
}
}
@ -356,7 +356,7 @@ sub getLocalConf {
if ( $r->{$_} =~ /^[{\[].*[}\]]$/ || $r->{$_} =~ /^sub\s*{.*}$/ ) {
eval "\$r->{$_} = $r->{$_}";
if ($@) {
$msg .= "Warning: error in file $file: $@.\n";
$msg .= "Warn: error in file $file: $@.\n";
return $r;
}
}

View File

@ -27,15 +27,16 @@ has multiValuesSeparator => ( is => 'rw', isa => 'Maybe[Str]' );
has jail => ( is => 'rw' );
has error => ( is => 'rw' );
our $VERSION = '2.0.6';
our $VERSION = '2.0.8';
our @builtCustomFunctions;
## @imethod protected build_jail()
# Build and return the security jail used to compile rules and headers.
# @return Safe object
sub build_jail {
my ( $self, $api, $require ) = @_;
my ( $self, $api, $require, $dontDie ) = @_;
my $build = 1;
return $self->jail
if ( $self->jail
and $self->jail->useSafeJail
@ -53,29 +54,34 @@ sub build_jail {
eval { require $f; };
}
if ($@) {
die "Unable to load '$f': $@";
$dontDie
? $api->logger->error($@)
: die "Unable to load '$f': $@";
undef $build;
}
}
}
@builtCustomFunctions =
$self->customFunctions ? split( /\s+/, $self->customFunctions ) : ();
foreach (@builtCustomFunctions) {
no warnings 'redefine';
$api->logger->debug("Custom function : $_");
my $sub = $_;
unless (/::/) {
$sub = "$self\::$_";
}
else {
s/^.*:://;
}
next if ( $self->can($_) );
eval "sub $_ {
if ($build) {
@builtCustomFunctions =
$self->customFunctions ? split( /\s+/, $self->customFunctions ) : ();
foreach (@builtCustomFunctions) {
no warnings 'redefine';
$api->logger->debug("Custom function : $_");
my $sub = $_;
unless (/::/) {
$sub = "$self\::$_";
}
else {
s/^.*:://;
}
next if ( $self->can($_) );
eval "sub $_ {
return $sub(\@_)
}";
$api->logger->error($@) if ($@);
$_ = "&$_";
$api->logger->error($@) if ($@);
$_ = "&$_";
}
}
if ( $self->useSafeJail ) {

View File

@ -185,7 +185,7 @@ sub jailInit {
multiValuesSeparator => $conf->{multiValuesSeparator},
}
);
$class->tsv->{jail}->build_jail( $class, $conf->{require} );
$class->tsv->{jail}->build_jail( $class, $conf->{require}, $conf->{requireDontDie} );
}
## @imethod protected void defaultValuesInit(hashRef args)