| 1 |
##@file |
| 2 |
# Base package for Lemonldap::NG configuration system |
| 3 |
|
| 4 |
##@class |
| 5 |
# Implements Lemonldap::NG shared configuration system. |
| 6 |
# In case of error or warning, the message is stored in the global variable |
| 7 |
# $Lemonldap::NG::Common::Conf::msg |
| 8 |
package Lemonldap::NG::Common::Conf; |
| 9 |
|
| 10 |
use strict; |
| 11 |
use utf8; |
| 12 |
no strict 'refs'; |
| 13 |
use Lemonldap::NG::Common::Conf::Constants; #inherits |
| 14 |
|
| 15 |
# Import compacter |
| 16 |
use Lemonldap::NG::Common::Conf::Compact; |
| 17 |
*compactConf = \&Lemonldap::NG::Common::Conf::Compact::compactConf; |
| 18 |
|
| 19 |
# TODO: don't import this big file, use a proxy |
| 20 |
use Lemonldap::NG::Common::Conf::DefaultValues; #inherits |
| 21 |
use Lemonldap::NG::Common::Crypto |
| 22 |
; #link protected cipher Object "cypher" in configuration hash |
| 23 |
use Config::IniFiles; |
| 24 |
|
| 25 |
#inherits Lemonldap::NG::Common::Conf::Backends::File |
| 26 |
#inherits Lemonldap::NG::Common::Conf::Backends::DBI |
| 27 |
#inherits Lemonldap::NG::Common::Conf::Backends::SOAP |
| 28 |
#inherits Lemonldap::NG::Common::Conf::Backends::LDAP |
| 29 |
|
| 30 |
our $VERSION = '2.23.0'; |
| 31 |
our $msg = ''; |
| 32 |
our $iniObj; |
| 33 |
|
| 34 |
BEGIN { |
| 35 |
eval { |
| 36 |
require threads::shared; |
| 37 |
threads::shared::share($iniObj); |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
## @cmethod Lemonldap::NG::Common::Conf new(hashRef arg) |
| 42 |
# Constructor. |
| 43 |
# Succeed if it has found a way to access to Lemonldap::NG configuration with |
| 44 |
# $arg (or default file). It can be : |
| 45 |
# - Nothing: default configuration file is tested, |
| 46 |
# - { confFile => "/path/to/storage.conf" }, |
| 47 |
# - { Type => "File", dirName => "/path/to/conf/dir/" }, |
| 48 |
# - { Type => "DBI", dbiChain => "DBI:MariaDB:database=lemonldap-ng;host=1.2.3.4", |
| 49 |
# dbiUser => "user", dbiPassword => "password" }, |
| 50 |
# - { Type => "SOAP", proxy => "https://auth.example.com/config" }, |
| 51 |
# - { Type => "LDAP", ldapServer => "ldap://localhost", ldapConfBranch => "ou=conf,ou=applications,dc=example,dc=com", |
| 52 |
# ldapBindDN => "cn=manager,dc=example,dc=com", ldapBindPassword => "secret"}, |
| 53 |
# |
| 54 |
# $self->{type} contains the type of configuration access system and the |
| 55 |
# corresponding package is loaded. |
| 56 |
# @param $arg hash reference or hash table |
| 57 |
# @return New Lemonldap::NG::Common::Conf object |
| 58 |
sub new { |
| 59 |
my $class = shift; |
| 60 |
my $self = bless {}, $class; |
| 61 |
if ( ref( $_[0] ) ) { |
| 62 |
%$self = %{ $_[0] }; |
| 63 |
} |
| 64 |
else { |
| 65 |
if ( (@_) && $#_ % 2 == 1 ) { |
| 66 |
%$self = @_; |
| 67 |
} |
| 68 |
} |
| 69 |
unless ( $self->{mdone} ) { |
| 70 |
unless ( $self->{type} ) { |
| 71 |
|
| 72 |
# Use local conf to get configStorage and localStorage |
| 73 |
my $localConfAccessPrms = |
| 74 |
$self->getLocalConf( CONFSECTION, $self->{confFile}, 0 ); |
| 75 |
|
| 76 |
if ( defined $localConfAccessPrms ) { |
| 77 |
%$self = ( %$self, %$localConfAccessPrms ); |
| 78 |
$self->replacePlaceholders($localConfAccessPrms); |
| 79 |
|
| 80 |
# localConfAccessPrms might have changed so we need to |
| 81 |
# update $self |
| 82 |
%$self = ( %$self, %$localConfAccessPrms ); |
| 83 |
} |
| 84 |
} |
| 85 |
unless ( $self->{type} ) { |
| 86 |
$msg .= "Error: configStorage: type is not defined.\n"; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
unless ( $self->{type} =~ /^[\w:]+$/ ) { |
| 90 |
$msg .= "Error: configStorage: type is not well formed.\n"; |
| 91 |
} |
| 92 |
$self->{type} = "Lemonldap::NG::Common::Conf::Backends::$self->{type}" |
| 93 |
unless $self->{type} =~ /::/; |
| 94 |
eval "require $self->{type}"; |
| 95 |
if ($@) { |
| 96 |
$msg .= "Error: failed to load $self->{type}: \n $@"; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
return 0 unless $self->prereq; |
| 100 |
$self->{mdone}++; |
| 101 |
$msg = "$self->{type} loaded.\n"; |
| 102 |
} |
| 103 |
if ( $self->{localStorage} and not defined( $self->{refLocalStorage} ) ) { |
| 104 |
eval "use $self->{localStorage};"; |
| 105 |
if ($@) { |
| 106 |
$msg .= "Error: Unable to load $self->{localStorage}: $@.\n"; |
| 107 |
} |
| 108 |
|
| 109 |
# TODO: defer that until $> > 0 (to avoid creating local cache with |
| 110 |
# root privileges |
| 111 |
else { |
| 112 |
$self->{refLocalStorage} = |
| 113 |
$self->{localStorage}->new( $self->{localStorageOptions} ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $self; |
| 118 |
} |
| 119 |
|
| 120 |
## @method int saveConf(hashRef conf, hash args) |
| 121 |
# Serialize $conf and call store(). |
| 122 |
# @param $conf Lemonldap::NG configuration hashRef |
| 123 |
# @param %args Parameters |
| 124 |
# @return Number of the saved configuration, <=0 in case of error. |
| 125 |
sub saveConf { |
| 126 |
my ( $self, $conf, %args ) = @_; |
| 127 |
|
| 128 |
# noCache => 1 disables the localStorage fallback in lastCfg, so a stale |
| 129 |
# cached cfgNum cannot let us overwrite a newer central configuration. |
| 130 |
my $last = $self->lastCfg( { noCache => 1 } ); |
| 131 |
|
| 132 |
# If configuration was modified, return an error |
| 133 |
if ( not $args{force} ) { |
| 134 |
if ( $conf->{cfgNum} ne $last |
| 135 |
|| $args{cfgDate} && $args{cfgDate} ne $args{currentCfgDate} ) |
| 136 |
{ |
| 137 |
|
| 138 |
if ( $conf->{cfgNum} ne $last ) { |
| 139 |
$msg .= |
| 140 |
"Configuration $last was changed since $conf->{cfgNum}.\n"; |
| 141 |
} |
| 142 |
if ( $args{cfgDate} ) { |
| 143 |
$msg .= |
| 144 |
"Configuration date $args{currentCfgDate}" |
| 145 |
. " was changed since $args{cfgDate}.\n"; |
| 146 |
} |
| 147 |
|
| 148 |
return CONFIG_WAS_CHANGED; |
| 149 |
} |
| 150 |
if ( $self->isLocked() or not $self->lock() ) { |
| 151 |
$msg .= "Configuration is locked.\n"; |
| 152 |
return DATABASE_LOCKED; |
| 153 |
} |
| 154 |
} |
| 155 |
$conf->{cfgNum} = $last + 1 unless ( $args{cfgNumFixed} ); |
| 156 |
delete $conf->{cipher}; |
| 157 |
|
| 158 |
# Try to store configuration |
| 159 |
my $tmp = $self->store($conf); |
| 160 |
|
| 161 |
unless ( $tmp > 0 ) { |
| 162 |
$msg .= "Error: Configuration $conf->{cfgNum} not stored.\n"; |
| 163 |
$self->unlock(); |
| 164 |
return ( $tmp ? $tmp : UNKNOWN_ERROR ); |
| 165 |
} |
| 166 |
|
| 167 |
$msg .= "Configuration $conf->{cfgNum} stored.\n"; |
| 168 |
$self->setDefault($conf); |
| 169 |
$self->setValuesFromEnv($conf); |
| 170 |
$self->compactConf($conf); |
| 171 |
|
| 172 |
if ( Lemonldap::NG::Handler::Main->can('tsv') |
| 173 |
and Lemonldap::NG::Handler::Main->tsv->{msgBrokerWriter} ) |
| 174 |
{ |
| 175 |
Lemonldap::NG::Handler::Main->tsv->{msgBrokerWriter} |
| 176 |
->publish( $conf->{eventQueueName}, { action => 'newConf' } ); |
| 177 |
} |
| 178 |
else { |
| 179 |
eval { Lemonldap::NG::Handler::Main->reload() }; |
| 180 |
} |
| 181 |
|
| 182 |
$self->unlock(); |
| 183 |
return $tmp; |
| 184 |
} |
| 185 |
|
| 186 |
## @method hashRef getConf(hashRef args) |
| 187 |
# Get configuration from remote configuration storage system or from local |
| 188 |
# cache if configuration has not been changed. If $args->{local} is set and if |
| 189 |
# a local configuration is available, remote configuration is not tested. |
| 190 |
# |
| 191 |
# Uses lastCfg to test and getDBConf() to get the remote configuration |
| 192 |
# @param $args Optional, contains {local=>1} or nothing |
| 193 |
# @return Lemonldap::NG configuration |
| 194 |
sub getConf { |
| 195 |
my ( $self, $args ) = @_; |
| 196 |
my $res; |
| 197 |
my $local = $args->{local}; |
| 198 |
my $cfgNum = $args->{cfgNum}; |
| 199 |
my $localPrm = $args->{localPrm}; |
| 200 |
my $noCache = $args->{noCache}; |
| 201 |
my $raw = $args->{raw}; |
| 202 |
my $allow_cache_for_root = ( |
| 203 |
$self->{localStorageOptions} |
| 204 |
? $self->{localStorageOptions}->{allow_cache_for_root} |
| 205 |
: 0 |
| 206 |
); |
| 207 |
|
| 208 |
# If running as UID=0, disabled the cache unless explicitely allowed |
| 209 |
if ( $> == 0 and !$allow_cache_for_root ) { |
| 210 |
$noCache = 1; |
| 211 |
} |
| 212 |
|
| 213 |
# Use only cache to get conf if $local is set |
| 214 |
if ( !$noCache |
| 215 |
and $local |
| 216 |
and ref( $self->{refLocalStorage} ) |
| 217 |
and $res = $self->{refLocalStorage}->get('conf') ) |
| 218 |
{ |
| 219 |
$msg .= "Get configuration from cache without verification.\n"; |
| 220 |
} |
| 221 |
|
| 222 |
# Check cfgNum in conf backend |
| 223 |
# Get conf in backend only if a newer configuration is available |
| 224 |
else { |
| 225 |
$cfgNum ||= $self->lastCfg( { noCache => $noCache } ); |
| 226 |
unless ($cfgNum) { |
| 227 |
$msg .= "Error: No configuration available in backend.\n"; |
| 228 |
} |
| 229 |
my $r; |
| 230 |
unless ( ref( $self->{refLocalStorage} ) ) { |
| 231 |
$msg .= "Get remote configuration (localStorage unavailable).\n"; |
| 232 |
$r = $self->getDBConf( { cfgNum => $cfgNum } ); |
| 233 |
return undef unless ( $r->{cfgNum} ); |
| 234 |
$self->setDefault( $r, $localPrm ); |
| 235 |
$self->setValuesFromEnv($r); |
| 236 |
$self->compactConf($r); |
| 237 |
} |
| 238 |
else { |
| 239 |
eval { $r = $self->{refLocalStorage}->get('conf') } |
| 240 |
if ( !$noCache ); |
| 241 |
$msg .= "Warn: $@" if ($@); |
| 242 |
|
| 243 |
if ( ref($r) |
| 244 |
and $r->{cfgNum} |
| 245 |
and $cfgNum |
| 246 |
and $r->{cfgNum} == $cfgNum ) |
| 247 |
{ |
| 248 |
$msg .= |
| 249 |
"Configuration unchanged, get configuration from cache.\n"; |
| 250 |
} |
| 251 |
else { |
| 252 |
my $r2 = $self->getDBConf( { cfgNum => $cfgNum } ); |
| 253 |
unless ( $r2->{cfgNum} ) { |
| 254 |
$r = $self->{refLocalStorage}->get('conf') unless ($r); |
| 255 |
$msg .= |
| 256 |
$r |
| 257 |
? "Error: Using previous cached configuration\n" |
| 258 |
: "Error: No configuration found in local cache\n"; |
| 259 |
return undef unless ($r); |
| 260 |
} |
| 261 |
else { |
| 262 |
$r = $r2; |
| 263 |
} |
| 264 |
|
| 265 |
# Store configuration in cache |
| 266 |
$self->setLocalConf($r) |
| 267 |
if ( $self->{refLocalStorage} |
| 268 |
and ( !$noCache or $noCache != 1 ) |
| 269 |
and !$raw ); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
# Return configuration hash |
| 274 |
$res = $r; |
| 275 |
} |
| 276 |
|
| 277 |
$self->setDefault( $res, $localPrm ); |
| 278 |
$self->setValuesFromEnv($res); |
| 279 |
$self->compactConf($res); |
| 280 |
|
| 281 |
# Create cipher object and replace variable placeholder |
| 282 |
unless ($raw) { |
| 283 |
|
| 284 |
$self->replacePlaceholders($res); |
| 285 |
eval { |
| 286 |
$res->{cipher} = Lemonldap::NG::Common::Crypto->new( $res->{key} ); |
| 287 |
}; |
| 288 |
if ($@) { |
| 289 |
$msg .= "Bad key: $@. \n"; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return $res; |
| 294 |
} |
| 295 |
|
| 296 |
## @method hashRef setDefault(hashRef conf, hashRef localPrm) |
| 297 |
# Set default params |
| 298 |
# @param $conf Lemonldap::NG configuration hashRef |
| 299 |
# @param $localPrm Local parameters |
| 300 |
# @return conf |
| 301 |
sub setDefault { |
| 302 |
my ( $self, $conf, $localPrm ) = @_; |
| 303 |
if ( defined $localPrm ) { |
| 304 |
$self->{localPrm} = $localPrm; |
| 305 |
} |
| 306 |
else { |
| 307 |
$localPrm = $self->{localPrm}; |
| 308 |
} |
| 309 |
my $defaultValues = |
| 310 |
Lemonldap::NG::Common::Conf::DefaultValues->defaultValues(); |
| 311 |
if ( $localPrm and %$localPrm ) { |
| 312 |
foreach my $k ( keys %$localPrm ) { |
| 313 |
$conf->{$k} = $localPrm->{$k}; |
| 314 |
} |
| 315 |
} |
| 316 |
foreach my $k ( keys %$defaultValues ) { |
| 317 |
$conf->{$k} //= $defaultValues->{$k}; |
| 318 |
} |
| 319 |
|
| 320 |
# Some parameters expect key name (example), not variable ($example) |
| 321 |
if ( defined $conf->{whatToTrace} ) { |
| 322 |
$conf->{whatToTrace} =~ s/^\$//; |
| 323 |
} |
| 324 |
|
| 325 |
return $conf; |
| 326 |
} |
| 327 |
|
| 328 |
sub setValuesFromEnv { |
| 329 |
my ( $self, $conf ) = @_; |
| 330 |
$conf->{hashedSessionStore} = $ENV{LLNG_HASHED_SESSION_STORE} |
| 331 |
if $ENV{LLNG_HASHED_SESSION_STORE}; |
| 332 |
return $conf; |
| 333 |
} |
| 334 |
|
| 335 |
## @method hashRef getLocalConf(string section, string file, int loaddefault) |
| 336 |
# Get configuration from local file |
| 337 |
# |
| 338 |
# @param $section Optional section name (default DEFAULTSECTION) |
| 339 |
# @param $file Optional file name (default DEFAULTCONFFILE) |
| 340 |
# @param $loaddefault Optional load default section parameters (default 1) |
| 341 |
# @return Lemonldap::NG configuration |
| 342 |
sub getLocalConf { |
| 343 |
my ( $self, $section, $file, $loaddefault ) = @_; |
| 344 |
my $r = {}; |
| 345 |
|
| 346 |
$section ||= DEFAULTSECTION; |
| 347 |
$file ||= |
| 348 |
$self->{confFile} |
| 349 |
|| $ENV{LLNG_DEFAULTCONFFILE} |
| 350 |
|| DEFAULTCONFFILE; |
| 351 |
$loaddefault = 1 unless ( defined $loaddefault ); |
| 352 |
my $cfg; |
| 353 |
|
| 354 |
# First, search if this file has been parsed |
| 355 |
unless ( $cfg = $iniObj->{$file} ) { |
| 356 |
|
| 357 |
# If default configuration cannot be read |
| 358 |
# - Error if configuration section is requested |
| 359 |
# - Silent exit for other section requests |
| 360 |
if ( open( my $fh, '<', $file ) ) { |
| 361 |
|
| 362 |
# Parse ini file |
| 363 |
$cfg = Config::IniFiles->new( -file => $fh, -allowcontinue => 1 ); |
| 364 |
close($fh); |
| 365 |
} |
| 366 |
else { |
| 367 |
if ( $section eq CONFSECTION ) { |
| 368 |
$msg .= |
| 369 |
"Cannot read $file to get configuration access parameters: $!\n"; |
| 370 |
return $r; |
| 371 |
} |
| 372 |
return $r; |
| 373 |
} |
| 374 |
|
| 375 |
unless ( defined $cfg ) { |
| 376 |
$msg .= "Local config error: " |
| 377 |
. ( join "\n", @Config::IniFiles::errors ) . "\n"; |
| 378 |
return $r; |
| 379 |
} |
| 380 |
|
| 381 |
# Check if default section exists |
| 382 |
unless ( $cfg->SectionExists(DEFAULTSECTION) ) { |
| 383 |
$msg .= "Default section (" . DEFAULTSECTION . ") is missing. \n"; |
| 384 |
return $r; |
| 385 |
} |
| 386 |
|
| 387 |
# Check if configuration section exists |
| 388 |
if ( $section eq CONFSECTION and !$cfg->SectionExists(CONFSECTION) ) { |
| 389 |
$msg .= "Configuration section (" . CONFSECTION . ") is missing.\n"; |
| 390 |
return $r; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
# First load all default section parameters |
| 395 |
if ($loaddefault) { |
| 396 |
foreach ( $cfg->Parameters(DEFAULTSECTION) ) { |
| 397 |
$r->{$_} = $cfg->val( DEFAULTSECTION, $_ ); |
| 398 |
if ( $_ eq "require" ) { |
| 399 |
|
| 400 |
foreach my $require ( split /[,\s]+/, $r->{$_} ) { |
| 401 |
eval { require $require }; |
| 402 |
$msg .= "Error: $@" if ($@); |
| 403 |
} |
| 404 |
} |
| 405 |
if ( $r->{$_} =~ /^[{\[].*[}\]]$/ |
| 406 |
|| $r->{$_} =~ /^sub\s*{.*}$/ ) |
| 407 |
{ |
| 408 |
eval "\$r->{$_} = $r->{$_}"; |
| 409 |
if ($@) { |
| 410 |
$msg .= "Warn: error in file $file: $@.\n"; |
| 411 |
return $r; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
# If we have an additional section to load |
| 418 |
if ( $section ne DEFAULTSECTION and $cfg->SectionExists($section) ) { |
| 419 |
|
| 420 |
# Check if requested section exists |
| 421 |
# Load section parameters |
| 422 |
foreach ( $cfg->Parameters($section) ) { |
| 423 |
$r->{$_} = $cfg->val( $section, $_ ); |
| 424 |
|
| 425 |
# Remove spaces before and after value (#1488) |
| 426 |
$r->{$_} =~ s/^\s*(.+?)\s*/$1/; |
| 427 |
if ( $r->{$_} =~ /^[{\[].*[}\]]$/ || $r->{$_} =~ /^sub\s*{.*}$/ ) { |
| 428 |
eval "\$r->{$_} = $r->{$_}"; |
| 429 |
if ($@) { |
| 430 |
$msg .= "Warn: error in file $file: $@.\n"; |
| 431 |
return $r; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
$self->replacePlaceholders($r); |
| 438 |
return $r; |
| 439 |
} |
| 440 |
|
| 441 |
## @method void setLocalConf(hashRef conf) |
| 442 |
# Store $conf in the local cache. |
| 443 |
# @param $conf Lemonldap::NG configuration hashRef |
| 444 |
sub setLocalConf { |
| 445 |
my ( $self, $conf ) = @_; |
| 446 |
eval { $self->{refLocalStorage}->set( "conf", $conf ) }; |
| 447 |
$msg .= "Warn: $@\n" if ($@); |
| 448 |
} |
| 449 |
|
| 450 |
## @method hashRef getDBConf(hashRef args) |
| 451 |
# Get configuration from remote storage system. |
| 452 |
# @param $args hashRef that must contains a key "cfgNum" (number of the wanted |
| 453 |
# configuration) |
| 454 |
# @return Lemonldap::NG configuration hashRef |
| 455 |
sub getDBConf { |
| 456 |
my ( $self, $args ) = @_; |
| 457 |
my $cfgNum = $args->{cfgNum}; |
| 458 |
return undef unless $cfgNum; |
| 459 |
|
| 460 |
if ( $cfgNum < 0 ) { |
| 461 |
my @a = $self->available(); |
| 462 |
$cfgNum = |
| 463 |
( @a + $cfgNum > 0 ) |
| 464 |
? ( $a[ $#a + $cfgNum ] ) |
| 465 |
: $a[0]; |
| 466 |
} |
| 467 |
my $conf = $self->load($cfgNum); |
| 468 |
return undef if $conf == "-1"; |
| 469 |
$msg .= "Get configuration $conf->{cfgNum}.\n" |
| 470 |
if ( defined $conf->{cfgNum} ); |
| 471 |
return $conf; |
| 472 |
} |
| 473 |
|
| 474 |
sub _launch { |
| 475 |
my $self = shift; |
| 476 |
my $sub = shift; |
| 477 |
my @res; |
| 478 |
eval { |
| 479 |
local $SIG{ALRM} = sub { die "TIMEOUT\n" }; |
| 480 |
eval { |
| 481 |
alarm( $self->{confTimeout} || 10 ); |
| 482 |
@res = &{ $self->{type} . "::$sub" }( $self, @_ ); |
| 483 |
}; |
| 484 |
alarm 0; |
| 485 |
die $@ if $@; |
| 486 |
}; |
| 487 |
if ($@) { |
| 488 |
$msg .= $@; |
| 489 |
print STDERR "MSG $msg\n"; |
| 490 |
return undef; |
| 491 |
} |
| 492 |
return wantarray ? (@res) : $res[0]; |
| 493 |
} |
| 494 |
|
| 495 |
## @method boolean prereq() |
| 496 |
# Call prereq() from the $self->{type} package. |
| 497 |
# @return True if succeed |
| 498 |
sub prereq { |
| 499 |
return shift->_launch( 'prereq', @_ ); |
| 500 |
} |
| 501 |
|
| 502 |
## @method @ available() |
| 503 |
# Call available() from the $self->{type} package. |
| 504 |
# @return list of available configuration numbers |
| 505 |
sub available { |
| 506 |
return shift->_launch( 'available', @_ ); |
| 507 |
} |
| 508 |
|
| 509 |
## @method int lastCfg(hashRef args) |
| 510 |
# Call lastCfg() from the $self->{type} package. |
| 511 |
# |
| 512 |
# When the backend call fails (returns undef/0) and a populated localStorage |
| 513 |
# is available, returns the cfgNum of the cached configuration except if |
| 514 |
# noCache is set. |
| 515 |
# |
| 516 |
# @param $args Optional hashRef, recognized keys: noCache |
| 517 |
# @return Number of the last configuration available, 0 on failure |
| 518 |
sub lastCfg { |
| 519 |
my ( $self, $args ) = @_; |
| 520 |
$args ||= {}; |
| 521 |
my $r = $self->_launch('lastCfg'); |
| 522 |
return $r if $r; |
| 523 |
|
| 524 |
if ( !$args->{noCache} and ref( $self->{refLocalStorage} ) ) { |
| 525 |
my $cached; |
| 526 |
eval { $cached = $self->{refLocalStorage}->get('conf') }; |
| 527 |
$msg .= "Warn: $@\n" if $@; |
| 528 |
if ( ref($cached) and $cached->{cfgNum} ) { |
| 529 |
$msg .= |
| 530 |
"lastCfg failed, falling back to cached configuration " |
| 531 |
. "(cfgNum=$cached->{cfgNum}).\n"; |
| 532 |
return $cached->{cfgNum}; |
| 533 |
} |
| 534 |
} |
| 535 |
return 0; |
| 536 |
} |
| 537 |
|
| 538 |
## @method boolean lock() |
| 539 |
# Call lock() from the $self->{type} package. |
| 540 |
# @return True if succeed |
| 541 |
sub lock { |
| 542 |
return shift->_launch( 'lock', @_ ); |
| 543 |
} |
| 544 |
|
| 545 |
## @method boolean isLocked() |
| 546 |
# Call isLocked() from the $self->{type} package. |
| 547 |
# @return True if database is locked |
| 548 |
sub isLocked { |
| 549 |
return shift->_launch( 'isLocked', @_ ); |
| 550 |
} |
| 551 |
|
| 552 |
## @method boolean unlock() |
| 553 |
# Call unlock() from the $self->{type} package. |
| 554 |
# @return True if succeed |
| 555 |
sub unlock { |
| 556 |
return shift->_launch( 'unlock', @_ ); |
| 557 |
} |
| 558 |
|
| 559 |
## @method int store(hashRef conf) |
| 560 |
# Call store() from the $self->{type} package. |
| 561 |
# @param $conf Lemondlap configuration serialized |
| 562 |
# @return Number of new configuration stored if succeed, 0 else. |
| 563 |
sub store { |
| 564 |
return shift->_launch( 'store', @_ ); |
| 565 |
} |
| 566 |
|
| 567 |
## @method load(int cfgNum, arrayRef fields) |
| 568 |
# Call load() from the $self->{type} package. |
| 569 |
# @return Lemonldap::NG Configuration hashRef if succeed, 0 else. |
| 570 |
sub load { |
| 571 |
return shift->_launch( 'load', @_ ); |
| 572 |
} |
| 573 |
|
| 574 |
## @method boolean delete(int cfgNum) |
| 575 |
# Call delete() from the $self->{type} package. |
| 576 |
# @param $cfgNum Number of configuration to delete |
| 577 |
# @return True if succeed |
| 578 |
sub delete { |
| 579 |
my ( $self, $c ) = @_; |
| 580 |
my @a = $self->available(); |
| 581 |
if ( grep( /^$c$/, @a ) ) { |
| 582 |
return $self->_launch( 'delete', $c ); |
| 583 |
} |
| 584 |
else { |
| 585 |
return 0; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
sub logError { |
| 590 |
return shift->_launch( 'logError', @_ ); |
| 591 |
} |
| 592 |
|
| 593 |
sub _substPlaceHolders { |
| 594 |
my ( $self, $string, $placeholder_config ) = @_; |
| 595 |
return $string unless $string; |
| 596 |
|
| 597 |
while ( my ( $key, $value ) = each %$placeholder_config ) { |
| 598 |
$string =~ s/%\Q$key\E:(.*?)%/$value->($1)/ge; |
| 599 |
} |
| 600 |
return $string; |
| 601 |
} |
| 602 |
|
| 603 |
## @method void replacePlaceholders(res: LLNG_Conf) |
| 604 |
# |
| 605 |
# Recursively replace %SERVERENV:VariableName% by $ENV{VariableName} value |
| 606 |
sub replacePlaceholders { |
| 607 |
my ( $self, $conf ) = @_; |
| 608 |
|
| 609 |
my $placeholder_config = {}; |
| 610 |
if ( $self->{useServerEnv} ) { |
| 611 |
$placeholder_config->{SERVERENV} = sub { $ENV{ $_[0] } }; |
| 612 |
} |
| 613 |
if ( my $moduleList = $self->{configPlaceHolderModules} ) { |
| 614 |
for my $moduledef ( split( /[,\s]+/, $moduleList ) ) { |
| 615 |
if ( my ( $prefix, $module ) = |
| 616 |
$moduledef =~ /(\w+)\s*=\s*([\w:]+)/ ) |
| 617 |
{ |
| 618 |
eval "require $module"; |
| 619 |
$placeholder_config->{$prefix} = |
| 620 |
sub { $module->replace( $self, $_[0] ) }; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
return $self->_replacePlaceholder_recurse( $conf, $placeholder_config ); |
| 626 |
} |
| 627 |
|
| 628 |
sub _replacePlaceholder_recurse { |
| 629 |
my ( $self, $conf, $placeholder_config ) = @_; |
| 630 |
if ( ref $conf eq 'HASH' ) { |
| 631 |
foreach my $oldkey ( keys %$conf ) { |
| 632 |
my $key = $self->_substPlaceHolders( $oldkey, $placeholder_config ); |
| 633 |
if ( $key ne $oldkey ) { |
| 634 |
$conf->{$key} = delete $conf->{$oldkey}; |
| 635 |
} |
| 636 |
next unless ( $conf->{$key} ); |
| 637 |
if ( ref $conf->{$key} ) { |
| 638 |
$self->_replacePlaceholder_recurse( $conf->{$key}, |
| 639 |
$placeholder_config ); |
| 640 |
} |
| 641 |
else { |
| 642 |
$conf->{$key} = $self->_substPlaceHolders( $conf->{$key}, |
| 643 |
$placeholder_config ); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
elsif ( ref $conf eq 'ARRAY' ) { |
| 648 |
for ( my $i = 0 ; $i < @$conf ; $i++ ) { |
| 649 |
if ( ref $conf->[$i] ) { |
| 650 |
$self->_replacePlaceholder_recurse( $conf->[$i], |
| 651 |
$placeholder_config ); |
| 652 |
} |
| 653 |
else { |
| 654 |
$conf->[$i] = |
| 655 |
$self->_substPlaceHolders( $conf->[$i], $placeholder_config ); |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
1; |
| 662 |
__END__ |
| 663 |
|
| 664 |
=head1 NAME |
| 665 |
|
| 666 |
=encoding utf8 |
| 667 |
|
| 668 |
Lemonldap::NG::Common::Conf - Perl extension written to manage Lemonldap::NG |
| 669 |
Web-SSO configuration. |
| 670 |
|
| 671 |
=head1 SYNOPSIS |
| 672 |
|
| 673 |
use Lemonldap::NG::Common::Conf; |
| 674 |
# Lemonldap::NG::Common::Conf reads loacl configuration from lemonldap-ng.ini. |
| 675 |
# Parameters can be overridden in a hash: |
| 676 |
my $confAccess = new Lemonldap::NG::Common::Conf( |
| 677 |
{ |
| 678 |
type=>'File', |
| 679 |
dirName=>"/tmp/", |
| 680 |
|
| 681 |
# To use local cache, set : |
| 682 |
localStorage => "Cache::FileCache", |
| 683 |
localStorageOptions = { |
| 684 |
'namespace' => 'lemonldap-ng-config', |
| 685 |
'default_expires_in' => 600, |
| 686 |
'directory_umask' => '007', |
| 687 |
'cache_root' => '/tmp', |
| 688 |
'cache_depth' => 5, |
| 689 |
}, |
| 690 |
}, |
| 691 |
) or die "Unable to build Lemonldap::NG::Common::Conf, see Apache logs"; |
| 692 |
# Next, get global configuration. Note that local parameters override global |
| 693 |
# ones |
| 694 |
my $config = $confAccess->getConf(); |
| 695 |
|
| 696 |
=head1 DESCRIPTION |
| 697 |
|
| 698 |
Lemonldap::NG::Common::Conf is used by all Lemonldap::NG packages to access to |
| 699 |
local/global configuration. |
| 700 |
|
| 701 |
=head2 SUBROUTINES |
| 702 |
|
| 703 |
=over |
| 704 |
|
| 705 |
=item * B<new> (constructor) |
| 706 |
|
| 707 |
It can takes any Lemonldap::NG parameter to override configuration. The |
| 708 |
'confFile' parameter can be used to override lemonldap-ng.ini path. |
| 709 |
Examples: |
| 710 |
|
| 711 |
=over |
| 712 |
|
| 713 |
=item * B<Set another lemonldap-ng.ini file> |
| 714 |
$confAccess = new Lemonldap::NG::Common::Conf( |
| 715 |
{ confFile => '/opt/lemonldap-ng.ini' } ); |
| 716 |
=item * B<Override global storage>: |
| 717 |
$confAccess = new Lemonldap::NG::Common::Conf( |
| 718 |
{ |
| 719 |
type => 'File', |
| 720 |
dirName => '/var/lib/lemonldap-ng/conf', |
| 721 |
}); |
| 722 |
|
| 723 |
=back |
| 724 |
|
| 725 |
=item * B<getConf>: returns a hash reference to the configuration. it takes |
| 726 |
a hash reference as first argument containing 2 optional parameters: |
| 727 |
|
| 728 |
=over |
| 729 |
|
| 730 |
=item * C<cfgNum => $number>: the number of the configuration wanted. If this |
| 731 |
argument is omitted, the last configuration is returned. |
| 732 |
|
| 733 |
=back |
| 734 |
|
| 735 |
=item * B<saveConf>: stores the Lemonldap::NG configuration passed in argument |
| 736 |
(hash reference). it returns the number of the new configuration. |
| 737 |
|
| 738 |
=back |
| 739 |
|
| 740 |
=head1 SEE ALSO |
| 741 |
|
| 742 |
L<http://lemonldap-ng.org/> |
| 743 |
|
| 744 |
=head1 AUTHORS |
| 745 |
|
| 746 |
=over |
| 747 |
|
| 748 |
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team> |
| 749 |
|
| 750 |
=back |
| 751 |
|
| 752 |
=head1 BUG REPORT |
| 753 |
|
| 754 |
Use OW2 system to report bug or ask for features: |
| 755 |
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues> |
| 756 |
|
| 757 |
=head1 DOWNLOAD |
| 758 |
|
| 759 |
Lemonldap::NG is available at |
| 760 |
L<https://lemonldap-ng.org/download> |
| 761 |
|
| 762 |
=head1 COPYRIGHT AND LICENSE |
| 763 |
|
| 764 |
See COPYING file for details. |
| 765 |
|
| 766 |
This library is free software; you can redistribute it and/or modify |
| 767 |
it under the terms of the GNU General Public License as published by |
| 768 |
the Free Software Foundation; either version 2, or (at your option) |
| 769 |
any later version. |
| 770 |
|
| 771 |
This program is distributed in the hope that it will be useful, |
| 772 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 773 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 774 |
GNU General Public License for more details. |
| 775 |
|
| 776 |
You should have received a copy of the GNU General Public License |
| 777 |
along with this program. If not, see L<http://www.gnu.org/licenses/>. |
| 778 |
|
| 779 |
=cut |