| 1 | package Apache::Session::Browseable::LDAP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Apache::Session; | ||
| 6 | use Apache::Session::Lock::Null; | ||
| 7 | use Apache::Session::Browseable::Store::LDAP; | ||
| 8 | use Apache::Session::Generate::SHA256; | ||
| 9 | use Apache::Session::Serialize::JSON; | ||
| 10 | use Apache::Session::Browseable::_common; | ||
| 11 | use Net::LDAP::Util qw(escape_filter_value); | ||
| 12 | |||
| 13 | our $VERSION = '1.3.6'; | ||
| 14 | our @ISA = qw(Apache::Session Apache::Session::Browseable::_common); | ||
| 15 | |||
| 16 | sub populate { | ||
| 17 | my $self = shift; | ||
| 18 | |||
| 19 | $self->{object_store} = new Apache::Session::Browseable::Store::LDAP $self; | ||
| 20 | $self->{lock_manager} = new Apache::Session::Lock::Null $self; | ||
| 21 | $self->{generate} = \&Apache::Session::Generate::SHA256::generate; | ||
| 22 | $self->{validate} = \&Apache::Session::Generate::SHA256::validate; | ||
| 23 | $self->{serialize} = \&Apache::Session::Serialize::JSON::serialize; | ||
| 24 | $self->{unserialize} = \&Apache::Session::Serialize::JSON::unserialize; | ||
| 25 | |||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub unserialize { | ||
| 30 | my $session = shift; | ||
| 31 | my $tmp = { serialized => $session }; | ||
| 32 | Apache::Session::Serialize::JSON::unserialize($tmp); | ||
| 33 | return $tmp->{data}; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub searchOn { | ||
| 37 | my ( $class, $args, $selectField, $value, @fields ) = @_; | ||
| 38 | |||
| 39 | my $index = | ||
| 40 | ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ]; | ||
| 41 | if ( grep { $_ eq $selectField } @$index ) { | ||
| 42 | ( $selectField, $value ) = escape_filter_value( $selectField, $value ); | ||
| 43 | return $class->_query( $args, $selectField, $value, @fields ); | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | return $class->SUPER::searchOn( $args, $selectField, $value, @fields ); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub searchOnExpr { | ||
| 51 | my ( $class, $args, $selectField, $value, @fields ) = @_; | ||
| 52 | |||
| 53 | my $index = | ||
| 54 | ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ]; | ||
| 55 | if ( grep { $_ eq $selectField } @$index ) { | ||
| 56 | ( $selectField, $value ) = escape_filter_value( $selectField, $value ); | ||
| 57 | $value =~ s/\\2a/\*/gi; | ||
| 58 | return $class->_query( $args, $selectField, $value, @fields ); | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | return $class->SUPER::searchOn( $args, $selectField, $value, @fields ); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _query { | ||
| 66 | my ( $class, $args, $selectField, $value, @fields ) = @_; | ||
| 67 | $args->{ldapObjectClass} ||= 'applicationProcess'; | ||
| 68 | $args->{ldapAttributeId} ||= 'cn'; | ||
| 69 | $args->{ldapAttributeContent} ||= 'description'; | ||
| 70 | $args->{ldapAttributeIndex} ||= 'ou'; | ||
| 71 | |||
| 72 | my %res = (); | ||
| 73 | my $ldap = | ||
| 74 | Apache::Session::Browseable::Store::LDAP::ldap( { args => $args } ); | ||
| 75 | my $msg = $ldap->search( | ||
| 76 | base => $args->{ldapConfBase}, | ||
| 77 | filter => "(&(objectClass=" | ||
| 78 | . $args->{ldapObjectClass} . ")(" | ||
| 79 | . $args->{ldapAttributeIndex} | ||
| 80 | . "=${selectField}_$value))", | ||
| 81 | |||
| 82 | #scope => 'base', | ||
| 83 | attrs => [ $args->{ldapAttributeContent}, $args->{ldapAttributeId} ], | ||
| 84 | ); | ||
| 85 | |||
| 86 | $ldap->unbind(); | ||
| 87 | $ldap->disconnect(); | ||
| 88 | |||
| 89 | if ( $msg->code ) { | ||
| 90 | Apache::Session::Browseable::Store::LDAP->logError($msg); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | foreach my $entry ( $msg->entries ) { | ||
| 94 | my $id = $entry->get_value( $args->{ldapAttributeId} ) or die; | ||
| 95 | my $tmp = $entry->get_value( $args->{ldapAttributeContent} ); | ||
| 96 | next unless ($tmp); | ||
| 97 | eval { $tmp = unserialize($tmp); }; | ||
| 98 | next if ($@); | ||
| 99 | if (@fields) { | ||
| 100 | $res{$id}->{$_} = $tmp->{$_} foreach (@fields); | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | $res{$id} = $tmp; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | return \%res; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub get_key_from_all_sessions { | ||
| 111 | my $class = shift; | ||
| 112 | my $args = shift; | ||
| 113 | my $data = shift; | ||
| 114 | $args->{ldapObjectClass} ||= 'applicationProcess'; | ||
| 115 | $args->{ldapAttributeId} ||= 'cn'; | ||
| 116 | $args->{ldapAttributeContent} ||= 'description'; | ||
| 117 | $args->{ldapAttributeIndex} ||= 'ou'; | ||
| 118 | |||
| 119 | my %res; | ||
| 120 | |||
| 121 | my $ldap = | ||
| 122 | Apache::Session::Browseable::Store::LDAP::ldap( { args => $args } ); | ||
| 123 | my $msg = $ldap->search( | ||
| 124 | base => $args->{ldapConfBase}, | ||
| 125 | |||
| 126 | # VERY STRANGE BUG ! With this filter, description isn't base64 encoded !!! | ||
| 127 | #filter => '(objectClass=applicationProcess)', | ||
| 128 | |||
| 129 | filter => '(&(objectClass=' | ||
| 130 | . $args->{ldapObjectClass} . ')(' | ||
| 131 | . $args->{ldapAttributeIndex} . '=*))', | ||
| 132 | attrs => [ $args->{ldapAttributeId}, $args->{ldapAttributeContent} ], | ||
| 133 | ); | ||
| 134 | |||
| 135 | $ldap->unbind(); | ||
| 136 | |||
| 137 | if ( $msg->code ) { | ||
| 138 | Apache::Session::Browseable::Store::LDAP->logError($msg); | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | foreach my $entry ( $msg->entries ) { | ||
| 142 | my $id = $entry->get_value( $args->{ldapAttributeId} ) or die; | ||
| 143 | my $tmp = $entry->get_value( $args->{ldapAttributeContent} ); | ||
| 144 | next unless ($tmp); | ||
| 145 | eval { $tmp = unserialize($tmp); }; | ||
| 146 | next if ($@); | ||
| 147 | if ( ref($data) eq 'CODE' ) { | ||
| 148 | $res{$id} = &$data( $tmp, $id ); | ||
| 149 | } | ||
| 150 | elsif ($data) { | ||
| 151 | $data = [$data] unless ( ref($data) ); | ||
| 152 | $res{$id}->{$_} = $tmp->{$_} foreach (@$data); | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | $res{$id} = $tmp; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | return \%res; | ||
| 161 | } | ||
| 162 | |||
| 163 | 1; | ||
| 164 | |||
| 165 | =pod | ||
| 166 | |||
| 167 | =head1 NAME | ||
| 168 | |||
| 169 | Apache::Session::Browseable::LDAP - An implementation of Apache::Session::LDAP | ||
| 170 | |||
| 171 | =head1 SYNOPSIS | ||
| 172 | |||
| 173 | use Apache::Session::Browseable::LDAP; | ||
| 174 | tie %hash, 'Apache::Session::Browseable::LDAP', $id, { | ||
| 175 | ldapServer => 'ldap://localhost:389', | ||
| 176 | ldapConfBase => 'dmdName=applications,dc=example,dc=com', | ||
| 177 | ldapBindDN => 'cn=admin,dc=example,dc=com', | ||
| 178 | ldapBindPassword => 'pass', | ||
| 179 | Index => 'uid ipAddr', | ||
| 180 | ldapObjectClass => 'applicationProcess', | ||
| 181 | ldapAttributeId => 'cn', | ||
| 182 | ldapAttributeContent => 'description', | ||
| 183 | ldapAttributeIndex => 'ou', | ||
| 184 | ldapVerify => 'require', | ||
| 185 | ldapCAFile => '/etc/ssl/certs/ca-certificates.crt', | ||
| 186 | ldapTimeout => 10, | ||
| 187 | }; | ||
| 188 | |||
| 189 | =head1 DESCRIPTION | ||
| 190 | |||
| 191 | This module is an implementation of Apache::Session. It uses an LDAP directory | ||
| 192 | to store datas. | ||
| 193 | |||
| 194 | =head1 COPYRIGHT AND LICENSE | ||
| 195 | |||
| 196 | =encoding utf8 | ||
| 197 | |||
| 198 | =over | ||
| 199 | |||
| 200 | =item 2009-2025 by Xavier Guimard | ||
| 201 | |||
| 202 | =item 2013-2025 by Clément Oudot | ||
| 203 | |||
| 204 | =item 2019-2025 by Maxime Besson | ||
| 205 | |||
| 206 | =item 2013-2025 by Worteks | ||
| 207 | |||
| 208 | =item 2023-2025 by Linagora | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | This library is free software; you can redistribute it and/or modify | ||
| 213 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 214 | at your option, any later version of Perl 5 you may have available. | ||
| 215 | |||
| 216 | =head1 SEE ALSO | ||
| 217 | |||
| 218 | L<Apache::Session> | ||
| 219 | |||
| 220 | =cut |