From 7553d5b95c69f4f14e67e73ab88b0beff96f7f4b Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Tue, 20 Oct 2009 13:20:53 +0000 Subject: [PATCH] Serialization moved to conf modules (to be able to use another serialization) --- modules/lemonldap-ng-common/MANIFEST | 1 + .../lib/Lemonldap/NG/Common/Conf.pm | 71 ++--------------- .../lib/Lemonldap/NG/Common/Conf/DBI.pm | 6 +- .../lib/Lemonldap/NG/Common/Conf/File.pm | 4 +- .../Lemonldap/NG/Common/Conf/Serializer.pm | 78 +++++++++++++++++++ 5 files changed, 94 insertions(+), 66 deletions(-) create mode 100644 modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm diff --git a/modules/lemonldap-ng-common/MANIFEST b/modules/lemonldap-ng-common/MANIFEST index 06da8fa93..09c952b18 100644 --- a/modules/lemonldap-ng-common/MANIFEST +++ b/modules/lemonldap-ng-common/MANIFEST @@ -12,6 +12,7 @@ lib/Lemonldap/NG/Common/Conf/DBI.pm lib/Lemonldap/NG/Common/Conf/File.pm lib/Lemonldap/NG/Common/Conf/LDAP.pm lib/Lemonldap/NG/Common/Conf/SOAP.pm +lib/Lemonldap/NG/Common/Conf/Serializer.pm lib/Lemonldap/NG/Common/Crypto.pm lib/Lemonldap/NG/Common/Safelib.pm Makefile.PL diff --git a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf.pm b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf.pm index 4f99ac05b..c3ca0bedc 100644 --- a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf.pm +++ b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf.pm @@ -9,7 +9,6 @@ package Lemonldap::NG::Common::Conf; use strict; no strict 'refs'; -use Data::Dumper; use Lemonldap::NG::Common::Conf::Constants; #inherits use Lemonldap::NG::Common::Crypto; #link protected cipher Object "cypher" in configuration hash use Regexp::Assemble; @@ -55,9 +54,12 @@ sub new { unless ( $self->{mdone} ) { $self->_readConfFile( $self->{confFile} ) unless ( $self->{type} ); unless ( $self->{type} ) { - $msg .= "Error: configStorage: type is not defined\n"; + $msg .= 'Error: configStorage: type is not defined'; return 0; } + unless ( $self->{type} =~ /^[\w:]+$/ ) { + $msg .= "Error: configStorage: type is not well formed"; + } $self->{type} = "Lemonldap::NG::Common::Conf::$self->{type}" unless $self->{type} =~ /^Lemonldap::/; eval "require $self->{type}"; @@ -126,32 +128,9 @@ sub saveConf { return CONFIG_WAS_CHANGED if ( $conf->{cfgNum} != $self->lastCfg or $self->isLocked ); $self->lock or return DATABASE_LOCKED; - my $fields; - local $Data::Dumper::Indent = 0; - local $Data::Dumper::Varname = "data"; - while ( my ( $k, $v ) = each(%$conf) ) { - next if ( $k =~ /^(?:reVHosts|cipher)$/ ); - if ( ref($v) ) { - $fields->{$k} = Dumper($v); - $fields->{$k} =~ s/'/'/g; - $fields->{$k} = "'$fields->{$k}'"; - } - elsif ( $v =~ /^\d+$/ ) { - $fields->{$k} = "$v"; - } - else { - - # mono-line - $v =~ s/[\r\n]/ /gm; - - # trim - $v =~ s/^\s*(.*?)\s*$/$1/; - $fields->{$k} = "'$v'"; - } - } - $fields->{cfgNum} = $self->lastCfg + 1; - $msg = "Configuration $fields->{cfgNum} stored"; - return $self->store($fields); + $conf->{cfgNum}++; + $msg = "Configuration $conf->{cfgNum} stored"; + return $self->store($conf); } ## @method hashRef getConf(hashRef args) @@ -234,41 +213,7 @@ sub getDBConf { ? ( $a[ $#a + $args->{cfgNum} ] ) : $a[0]; } - my $fields = $self->load( $args->{cfgNum} ); - my $conf; - while ( my ( $k, $v ) = each(%$fields) ) { - $v =~ s/^'(.*)'$/$1/s; - if ( $k =~ -/^(?:exportedVars|locationRules|groups|exportedHeaders|macros|globalStorageOptions)$/ - and $v ||= {} - and not ref($v) ) - { - $conf->{$k} = {}; - if ( defined($v) and $v !~ /^\$/ ) { - print STDERR -"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n"; - eval { require Storable; require MIME::Base64; }; - if ($@) { - $msg = "Error : $@"; - return 0; - } - $conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) ); - } - else { - my $data; - $v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/; - $v =~ s/&#?39;/'/g; - eval $v; - print STDERR -"Lemonldap::NG : Error while reading configuration with $k key: $@\n" - if ($@); - $conf->{$k} = $data; - } - } - else { - $conf->{$k} = $v; - } - } + my $conf = $self->load( $args->{cfgNum} ); $msg = "Get configuration $conf->{cfgNum}"; my $re = Regexp::Assemble->new(); foreach ( keys %{ $conf->{locationRules} } ) { diff --git a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/DBI.pm b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/DBI.pm index 97b6ba6eb..3452b25e4 100644 --- a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/DBI.pm +++ b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/DBI.pm @@ -3,7 +3,8 @@ package Lemonldap::NG::Common::Conf::DBI; use strict; use DBI; use MIME::Base64; -use Lemonldap::NG::Common::Conf::Constants; +use Lemonldap::NG::Common::Conf::Constants; #inherits +use Lemonldap::NG::Common::Conf::Serializer; our $VERSION = 0.17; @@ -79,6 +80,7 @@ sub unlock { sub store { my ( $self, $fields ) = @_; + my $fields = $self->serialize($fields); my $tmp = $self->dbh->do( "insert into " . $self->{dbiTable} . " (" @@ -107,7 +109,7 @@ sub load { $self->logError; return 0; } - return $row; + return $self->unserialize($row); } sub delete { diff --git a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm index 75f35b08f..c8d74b69a 100644 --- a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm +++ b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/File.pm @@ -2,6 +2,7 @@ package Lemonldap::NG::Common::Conf::File; use strict; use Lemonldap::NG::Common::Conf::Constants; #inherits +use Lemonldap::NG::Common::Conf::Serializer; our $VERSION = 0.23; @@ -63,6 +64,7 @@ sub unlock { sub store { my ( $self, $fields ) = @_; + my $fields = $self->serialize($fields); my $mask = umask; umask( oct('0027') ); unless ( open FILE, @@ -98,7 +100,7 @@ sub load { } } close FILE; - return $f; + return $self->unserialize($f); } sub delete { diff --git a/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm new file mode 100644 index 000000000..10c215bfb --- /dev/null +++ b/modules/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm @@ -0,0 +1,78 @@ +package Lemonldap::NG::Common::Conf::Serializer; + +use Data::Dumper; + +BEGIN { + *Lemonldap::NG::Common::Conf::serialize = \&serialize; + *Lemonldap::NG::Common::Conf::unserialize = \&unserialize; +} + +sub serialize { + my ( $self, $conf ) = @_; + my $fields; + local $Data::Dumper::Indent = 0; + local $Data::Dumper::Varname = "data"; + while ( my ( $k, $v ) = each(%$conf) ) { + next if ( $k =~ /^(?:reVHosts|cipher)$/ ); + if ( ref($v) ) { + $fields->{$k} = Dumper($v); + $fields->{$k} =~ s/'/'/g; + $fields->{$k} = "'$fields->{$k}'"; + } + elsif ( $v =~ /^\d+$/ ) { + $fields->{$k} = "$v"; + } + else { + + # mono-line + $v =~ s/[\r\n]/ /gm; + + # trim + $v =~ s/^\s*(.*?)\s*$/$1/; + $fields->{$k} = "'$v'"; + } + } + return $fields; +} + +sub unserialize { + my ( $self, $fields ) = @_; + my $conf; + while ( my ( $k, $v ) = each(%$fields) ) { + $v =~ s/^'(.*)'$/$1/s; + if ( $k =~ +/^(?:exportedVars|locationRules|groups|exportedHeaders|macros|globalStorageOptions)$/ + and $v ||= {} + and not ref($v) ) + { + $conf->{$k} = {}; + if ( defined($v) and $v !~ /^\$/ ) { + print STDERR +"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n"; + eval { require Storable; require MIME::Base64; }; + if ($@) { + $msg = "Error : $@"; + return 0; + } + $conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) ); + } + else { + my $data; + $v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/; + $v =~ s/&#?39;/'/g; + eval $v; + print STDERR +"Lemonldap::NG : Error while reading configuration with $k key: $@\n" + if ($@); + $conf->{$k} = $data; + } + } + else { + $conf->{$k} = $v; + } + } + return $conf; +} + +1; +__END__