| 1 |
# @file |
| 2 |
# Add get_key_from_all_sessions() function to Apache::Session modules. |
| 3 |
# This file is used by Lemonldap::NG::Manager::Status and by the |
| 4 |
# purgeCentralCache script. |
| 5 |
# |
| 6 |
# Warning, this works only with SQL databases, simple or Berkeley files (not |
| 7 |
# for Apache::Session::Memcached for example) |
| 8 |
package Lemonldap::NG::Common::Apache::Session; |
| 9 |
|
| 10 |
use strict; |
| 11 |
use AutoLoader 'AUTOLOAD'; |
| 12 |
use Apache::Session; |
| 13 |
use base qw(Apache::Session); |
| 14 |
use Lemonldap::NG::Common::Apache::Session::Serialize::JSON; |
| 15 |
use Lemonldap::NG::Common::Apache::Session::Store; |
| 16 |
use Lemonldap::NG::Common::Apache::Session::Lock; |
| 17 |
|
| 18 |
our $VERSION = '2.19.0'; |
| 19 |
|
| 20 |
sub _load { |
| 21 |
my ( $backend, $func ) = @_; |
| 22 |
unless ( $backend->can('populate') ) { |
| 23 |
eval "require $backend"; |
| 24 |
die $@ if ($@); |
| 25 |
} |
| 26 |
return $func ? $backend->can($func) : 0; |
| 27 |
} |
| 28 |
|
| 29 |
sub populate { |
| 30 |
my $self = shift; |
| 31 |
my $backend = $self->{args}->{backend}; |
| 32 |
_load($backend); |
| 33 |
$backend .= "::populate"; |
| 34 |
{ |
| 35 |
no strict 'refs'; |
| 36 |
$self = $self->$backend(@_); |
| 37 |
} |
| 38 |
if ( $backend =~ |
| 39 |
/^Apache::Session::(?:(?:Postgre|Redi)s|S(?:QLite3|ybase)|(?:My|No)SQL|F(?:ile|lex)|Cassandra|Oracle|LDAP)/ |
| 40 |
and !$self->{args}->{useStorable} ) |
| 41 |
{ |
| 42 |
$self->{serialize} = |
| 43 |
\&Lemonldap::NG::Common::Apache::Session::Serialize::JSON::serialize; |
| 44 |
$self->{unserialize} = |
| 45 |
\&Lemonldap::NG::Common::Apache::Session::Serialize::JSON::unserialize; |
| 46 |
if ( $backend =~ /^Apache::Session::LDAP/ ) { |
| 47 |
$self->{unserialize} = |
| 48 |
\&Lemonldap::NG::Common::Apache::Session::Serialize::JSON::unserializeBase64; |
| 49 |
} |
| 50 |
} |
| 51 |
if ( $self->{args}->{generateModule} ) { |
| 52 |
my $generate = $self->{args}->{generateModule}; |
| 53 |
eval "require $generate"; |
| 54 |
die $@ if ($@); |
| 55 |
$self->{generate} = \&{ $generate . "::generate" }; |
| 56 |
$self->{validate} = \&{ $generate . "::validate" }; |
| 57 |
} |
| 58 |
if ( $self->{args}->{setId} ) { |
| 59 |
$self->{generate} = \&setId; |
| 60 |
$self->{validate} = sub { 1 }; |
| 61 |
} |
| 62 |
|
| 63 |
my $allow_cache_for_root = ( |
| 64 |
$self->{args}->{localStorageOptions} |
| 65 |
? $self->{args}->{localStorageOptions}->{allow_cache_for_root} |
| 66 |
: 0 |
| 67 |
); |
| 68 |
|
| 69 |
# If cache is configured, use our specific object store module |
| 70 |
if ( ( $> or $allow_cache_for_root ) |
| 71 |
and $self->{args}->{localStorage} ) |
| 72 |
{ |
| 73 |
$self->{args}->{object_store} = $self->{object_store}; |
| 74 |
$self->{object_store} = |
| 75 |
Lemonldap::NG::Common::Apache::Session::Store->new($self); |
| 76 |
$self->{args}->{lock_manager} = $self->{lock_manager}; |
| 77 |
$self->{lock_manager} = |
| 78 |
Lemonldap::NG::Common::Apache::Session::Lock->new($self); |
| 79 |
} |
| 80 |
return $self; |
| 81 |
} |
| 82 |
|
| 83 |
__END__ |
| 84 |
|
| 85 |
sub setId { |
| 86 |
my $session = shift; |
| 87 |
$session->{data}->{_session_id} = $session->{args}->{setId}; |
| 88 |
} |
| 89 |
|
| 90 |
sub searchOn { |
| 91 |
my ( $class, $args, $selectField, $value, @fields ) = splice @_; |
| 92 |
unless ( $args->{backend} ) { |
| 93 |
die "SearchOn called without backend. " . join( ', ', caller(0) ); |
| 94 |
} |
| 95 |
|
| 96 |
return $args->{backend}->searchOn( $args, $selectField, $value, @fields ) |
| 97 |
if ( _load( $args->{backend}, 'searchOn' ) ); |
| 98 |
my %res = (); |
| 99 |
$class->get_key_from_all_sessions( |
| 100 |
$args, |
| 101 |
sub { |
| 102 |
my $entry = shift; |
| 103 |
my $id = shift; |
| 104 |
return undef |
| 105 |
unless ( $entry->{$selectField} |
| 106 |
and $entry->{$selectField} eq $value ); |
| 107 |
if (@fields) { |
| 108 |
$res{$id}->{$_} = $entry->{$_} foreach (@fields); |
| 109 |
} |
| 110 |
else { |
| 111 |
$res{$id} = $entry; |
| 112 |
} |
| 113 |
undef; |
| 114 |
} |
| 115 |
); |
| 116 |
return \%res; |
| 117 |
} |
| 118 |
|
| 119 |
sub searchOnExpr { |
| 120 |
my ( $class, $args, $selectField, $value, @fields ) = splice @_; |
| 121 |
return $args->{backend} |
| 122 |
->searchOnExpr( $args, $selectField, $value, @fields ) |
| 123 |
if ( _load( $args->{backend}, 'searchOnExpr' ) ); |
| 124 |
$value = quotemeta($value); |
| 125 |
$value =~ s/\\\*/\.\*/g; |
| 126 |
$value = qr/^$value$/; |
| 127 |
my %res = (); |
| 128 |
$class->get_key_from_all_sessions( |
| 129 |
$args, |
| 130 |
sub { |
| 131 |
my $entry = shift; |
| 132 |
my $id = shift; |
| 133 |
return undef unless ( $entry->{$selectField} =~ $value ); |
| 134 |
if (@fields) { |
| 135 |
$res{$id}->{$_} = $entry->{$_} foreach (@fields); |
| 136 |
} |
| 137 |
else { |
| 138 |
$res{$id} = $entry; |
| 139 |
} |
| 140 |
undef; |
| 141 |
} |
| 142 |
); |
| 143 |
return \%res; |
| 144 |
} |
| 145 |
|
| 146 |
sub searchGt { |
| 147 |
my ( $class, $args, $selectField, $value, @fields ) = splice @_; |
| 148 |
return $args->{backend}->searchGt( $args, $selectField, $value, @fields ) |
| 149 |
if ( _load( $args->{backend}, 'searchGt' ) ); |
| 150 |
my %res = (); |
| 151 |
$class->get_key_from_all_sessions( |
| 152 |
$args, |
| 153 |
sub { |
| 154 |
my $entry = shift; |
| 155 |
my $id = shift; |
| 156 |
return undef unless ( $entry->{$selectField} > $value ); |
| 157 |
if (@fields) { |
| 158 |
$res{$id}->{$_} = $entry->{$_} foreach (@fields); |
| 159 |
} |
| 160 |
else { |
| 161 |
$res{$id} = $entry; |
| 162 |
} |
| 163 |
undef; |
| 164 |
} |
| 165 |
); |
| 166 |
return \%res; |
| 167 |
} |
| 168 |
|
| 169 |
sub searchLt { |
| 170 |
my ( $class, $args, $selectField, $value, @fields ) = splice @_; |
| 171 |
return $args->{backend}->searchLt( $args, $selectField, $value, @fields ) |
| 172 |
if ( _load( $args->{backend}, 'searchLt' ) ); |
| 173 |
my %res = (); |
| 174 |
$class->get_key_from_all_sessions( |
| 175 |
$args, |
| 176 |
sub { |
| 177 |
my $entry = shift; |
| 178 |
my $id = shift; |
| 179 |
return undef unless ( $entry->{$selectField} < $value ); |
| 180 |
if (@fields) { |
| 181 |
$res{$id}->{$_} = $entry->{$_} foreach (@fields); |
| 182 |
} |
| 183 |
else { |
| 184 |
$res{$id} = $entry; |
| 185 |
} |
| 186 |
undef; |
| 187 |
} |
| 188 |
); |
| 189 |
return \%res; |
| 190 |
} |
| 191 |
|
| 192 |
sub get_key_from_all_sessions { |
| 193 |
my $class = shift; |
| 194 |
|
| 195 |
my ( $args, $data ) = @_; |
| 196 |
my $backend = $args->{backend}; |
| 197 |
if ( _load( $backend, 'get_key_from_all_sessions' ) ) { |
| 198 |
return $backend->get_key_from_all_sessions( $args, $data ); |
| 199 |
} |
| 200 |
if ( $args->{useStorable} ) { |
| 201 |
require Storable; |
| 202 |
$args->{unserialize} = \&Storable::thaw; |
| 203 |
} |
| 204 |
else { |
| 205 |
$args->{unserialize} = |
| 206 |
\&Lemonldap::NG::Common::Apache::Session::Serialize::JSON::_unserialize; |
| 207 |
} |
| 208 |
|
| 209 |
# For now, Apache::Session::MariaDB doesn't exists. |
| 210 |
# Apache::Session::Browseable::MariaDB has its own get_key_from_all_sessions |
| 211 |
if ( $backend =~ |
| 212 |
/^Apache::Session::(SQLite\d?|MySQL|MySQL::NoLock|Postgres|Oracle|Sybase|Informix)$/ |
| 213 |
) |
| 214 |
{ |
| 215 |
return $class->_dbiGKFAS( $1, @_ ); |
| 216 |
} |
| 217 |
elsif ( $backend =~ /^Apache::Session::(File|PHP|DBFile|LDAP)$/ ) { |
| 218 |
no strict 'refs'; |
| 219 |
my $tmp = "_${1}GKFAS"; |
| 220 |
return $class->$tmp(@_); |
| 221 |
} |
| 222 |
else { |
| 223 |
die "$backend can not provide session exploration"; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
sub decodeThaw64 { |
| 228 |
require MIME::Base64; |
| 229 |
my $s = shift; |
| 230 |
return Storable::thaw( MIME::Base64::decode_base64($s) ); |
| 231 |
} |
| 232 |
|
| 233 |
sub _dbiGKFAS { |
| 234 |
my ( $class, $type, $args, $data ) = @_; |
| 235 |
my $next; |
| 236 |
if ( $type !~ /(?:MySQL)/ ) { |
| 237 |
$next = \&decodeThaw64; |
| 238 |
if ( $args->{useStorable} ) { |
| 239 |
$args->{unserialize} = $next; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
my $dbh = |
| 244 |
DBI->connect( $args->{DataSource}, $args->{UserName}, $args->{Password} ) |
| 245 |
or die("$!$@"); |
| 246 |
my $sth = $dbh->prepare( |
| 247 |
'SELECT id,a_session from ' . ( $args->{TableName} || 'sessions' ) ); |
| 248 |
$sth->execute; |
| 249 |
my %res; |
| 250 |
while ( my @row = $sth->fetchrow_array ) { |
| 251 |
eval { |
| 252 |
if ( ref($data) eq 'CODE' ) { |
| 253 |
my $tmp = |
| 254 |
&$data( $args->{unserialize}->( $row[1], $next ), $row[0] ); |
| 255 |
$res{ $row[0] } = $tmp if ( defined($tmp) ); |
| 256 |
} |
| 257 |
elsif ($data) { |
| 258 |
$data = [$data] unless ( ref($data) ); |
| 259 |
my $tmp = $args->{unserialize}->( $row[1], $next ); |
| 260 |
$res{ $row[0] }->{$_} = $tmp->{$_} foreach (@$data); |
| 261 |
} |
| 262 |
else { |
| 263 |
$res{ $row[0] } = $args->{unserialize}->( $row[1], $next ); |
| 264 |
} |
| 265 |
}; |
| 266 |
if ($@) { |
| 267 |
print STDERR "Error in session $row[0]\n"; |
| 268 |
delete $res{ $row[0] }; |
| 269 |
} |
| 270 |
} |
| 271 |
return \%res; |
| 272 |
} |
| 273 |
|
| 274 |
sub _FileGKFAS { |
| 275 |
my ( $class, $args, $data ) = @_; |
| 276 |
$args->{Directory} ||= '/tmp'; |
| 277 |
|
| 278 |
unless ( opendir DIR, $args->{Directory} ) { |
| 279 |
die "Cannot open directory $args->{Directory}\n"; |
| 280 |
} |
| 281 |
my @t = |
| 282 |
grep { -f "$args->{Directory}/$_" and /^[A-Za-z0-9@\-]+$/ } readdir(DIR); |
| 283 |
closedir DIR; |
| 284 |
my %res; |
| 285 |
for my $f (@t) { |
| 286 |
open F, '<', "$args->{Directory}/$f"; |
| 287 |
eval { |
| 288 |
my $row = join '', <F>; |
| 289 |
if ( ref($data) eq 'CODE' ) { |
| 290 |
eval { $res{$f} = &$data( $args->{unserialize}->($row), $f ); }; |
| 291 |
if ($@) { |
| 292 |
$res{$f} = &$data( undef, $f ); |
| 293 |
} |
| 294 |
} |
| 295 |
elsif ($data) { |
| 296 |
$data = [$data] unless ( ref($data) ); |
| 297 |
my $tmp; |
| 298 |
eval { $tmp = $args->{unserialize}->($row); }; |
| 299 |
if ($@) { |
| 300 |
$res{$f}->{$_} = undef foreach (@$data); |
| 301 |
} |
| 302 |
else { |
| 303 |
$res{$f}->{$_} = $tmp->{$_} foreach (@$data); |
| 304 |
} |
| 305 |
} |
| 306 |
else { |
| 307 |
eval { $res{$f} = $args->{unserialize}->($row); }; |
| 308 |
} |
| 309 |
}; |
| 310 |
if ($@) { |
| 311 |
print STDERR "Error in session $f\n"; |
| 312 |
delete $res{$f}; |
| 313 |
} |
| 314 |
} |
| 315 |
return \%res; |
| 316 |
} |
| 317 |
|
| 318 |
sub _PHPGKFAS { |
| 319 |
require Apache::Session::Serialize::PHP; |
| 320 |
my ( $class, $args, $data ) = @_; |
| 321 |
|
| 322 |
my $directory = $args->{SavePath} || '/tmp'; |
| 323 |
unless ( opendir DIR, $args->{SavePath} ) { |
| 324 |
die "Cannot open directory $args->{SavePath}\n"; |
| 325 |
} |
| 326 |
my @t = |
| 327 |
grep { -f "$args->{SavePath}/$_" and /^sess_[A-Za-z0-9@\-]+$/ } |
| 328 |
readdir(DIR); |
| 329 |
closedir DIR; |
| 330 |
my %res; |
| 331 |
for my $f (@t) { |
| 332 |
open F, '<', "$args->{SavePath}/$f"; |
| 333 |
my $row = join '', <F>; |
| 334 |
if ( ref($data) eq 'CODE' ) { |
| 335 |
$res{$f} = |
| 336 |
&$data( Apache::Session::Serialize::PHP::unserialize($row), $f ); |
| 337 |
} |
| 338 |
elsif ($data) { |
| 339 |
$data = [$data] unless ( ref($data) ); |
| 340 |
my $tmp = Apache::Session::Serialize::PHP::unserialize($row); |
| 341 |
$res{$f}->{$_} = $tmp->{$_} foreach (@$data); |
| 342 |
} |
| 343 |
else { |
| 344 |
$res{$f} = Apache::Session::Serialize::PHP::unserialize($row); |
| 345 |
} |
| 346 |
} |
| 347 |
return \%res; |
| 348 |
} |
| 349 |
|
| 350 |
sub _DBFileGKFAS { |
| 351 |
my ( $class, $args, $data ) = @_; |
| 352 |
|
| 353 |
if ( !tied %{ $class->{dbm} } ) { |
| 354 |
my $rv = tie %{ $class->{dbm} }, 'DB_File', $args->{FileName}; |
| 355 |
if ( !$rv ) { |
| 356 |
die "Could not open dbm file " . $args->{FileName} . ": $!"; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
my %res; |
| 361 |
foreach my $k ( keys %{ $class->{dbm} } ) { |
| 362 |
eval { |
| 363 |
if ( ref($data) eq 'CODE' ) { |
| 364 |
$res{$k} = |
| 365 |
&$data( $args->{unserialize}->( $class->{dbm}->{$k} ), $k ); |
| 366 |
} |
| 367 |
elsif ($data) { |
| 368 |
$data = [$data] unless ( ref($data) ); |
| 369 |
my $tmp = $args->{unserialize}->( $class->{dbm}->{$k} ); |
| 370 |
$res{$k}->{$_} = $tmp->{$_} foreach (@$data); |
| 371 |
} |
| 372 |
else { |
| 373 |
$res{$k} = $args->{unserialize}->( $class->{dbm}->{$k} ); |
| 374 |
} |
| 375 |
}; |
| 376 |
if ($@) { |
| 377 |
print STDERR "Error in session $k\n"; |
| 378 |
delete $res{$k}; |
| 379 |
} |
| 380 |
} |
| 381 |
return \%res; |
| 382 |
} |
| 383 |
|
| 384 |
sub _LDAPGKFAS { |
| 385 |
my ( $class, $args, $data ) = @_; |
| 386 |
$args->{ldapObjectClass} ||= 'applicationProcess'; |
| 387 |
$args->{ldapAttributeId} ||= 'cn'; |
| 388 |
$args->{ldapAttributeContent} ||= 'description'; |
| 389 |
|
| 390 |
my $ldap = Apache::Session::Store::LDAP::ldap( { args => $args } ); |
| 391 |
my $msg = $ldap->search( |
| 392 |
base => $args->{ldapConfBase}, |
| 393 |
filter => '(objectClass=' . $args->{ldapObjectClass} . ')', |
| 394 |
attrs => [ $args->{ldapAttributeId}, $args->{ldapAttributeContent} ], |
| 395 |
); |
| 396 |
|
| 397 |
$ldap->unbind(); |
| 398 |
$ldap->disconnect(); |
| 399 |
|
| 400 |
Apache::Session::Store::LDAP->logError($msg) if ( $msg->code ); |
| 401 |
|
| 402 |
my %res; |
| 403 |
|
| 404 |
foreach my $entry ( $msg->entries ) { |
| 405 |
my ( $k, $v ) = ( |
| 406 |
$entry->get_value( $args->{ldapAttributeId} ), |
| 407 |
$entry->get_value( $args->{ldapAttributeContent} ) |
| 408 |
); |
| 409 |
eval { $v = $args->{unserialize}->( $v, \&decodeThaw64 ); }; |
| 410 |
next if ($@); |
| 411 |
if ( ref($data) eq 'CODE' ) { |
| 412 |
$res{$k} = &$data( $v, $k ); |
| 413 |
} |
| 414 |
elsif ($data) { |
| 415 |
$data = [$data] unless ( ref($data) ); |
| 416 |
my $tmp = $v; |
| 417 |
$res{$k}->{$_} = $tmp->{$_} foreach (@$data); |
| 418 |
} |
| 419 |
else { |
| 420 |
$res{$k} = $v; |
| 421 |
} |
| 422 |
} |
| 423 |
return \%res; |
| 424 |
} |
| 425 |
|
| 426 |
1; |