Lemonldap-NG-Common
view release on metacpan or search on metacpan
lib/Lemonldap/NG/Common/Notifications/LDAP.pm view on Meta::CPAN
}
return $value;
}
## @method object private decodeLdapValue()
# decode ldap value from utf8 to latin1
# @param value value to decode
# @return value decoded in latin1
sub decodeLdapValue {
my $value = shift;
Encode::from_to( $value, "utf8", "iso-8859-1", Encode::FB_CROAK );
return $value;
}
## @method object private _ldap()
# Return the ldap object (build it if needed).
# @return ldap handle object
sub _ldap {
my $self = shift;
return $self->{ldap} if ( $self->{ldap} );
# Parse servers configuration
my $useTls = 0;
my $tlsParam;
my @servers = ();
foreach my $server ( split /[\s,]+/, $self->ldapServer ) {
if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
$useTls = 1;
$server = $1;
$tlsParam = $2 || "";
}
else {
$useTls = 0;
}
push @servers, $server;
}
# Connect
my $ldap = Net::LDAP->new(
\@servers,
onerror => undef,
keepalive => 1,
( $self->ldapPort ? ( port => $self->ldapPort ) : () ),
( $self->ldapVerify ? ( verify => $self->ldapVerify ) : () ),
( $self->ldapCAFile ? ( cafile => $self->ldapCAFile ) : () ),
( $self->ldapCAPath ? ( capath => $self->ldapCAPath ) : () ),
);
unless ($ldap) {
use Data::Dumper;
die 'connexion failed: ' . $@;
}
elsif ( $Net::LDAP::VERSION < '0.64' ) {
# CentOS7 has a bug in which IO::Socket::SSL will return a broken
# socket when certificate validation fails. Net::LDAP does not catch
# it, and the process ends up crashing.
# As a precaution, make sure the underlying socket is doing fine:
if ( $ldap->socket->isa('IO::Socket::SSL')
and $ldap->socket->errstr < 0 )
{
die "SSL connection error: " . $ldap->socket->errstr;
}
}
# Start TLS if needed
if ($useTls) {
my %h = split( /[&=]/, $tlsParam );
$h{cafile} ||= $self->ldapCAFile if ( $self->ldapCAFile );
$h{capath} ||= $self->ldapCAPath if ( $self->ldapCAPath );
$h{verify} ||= $self->ldapVerify if ( $self->ldapVerify );
my $start_tls = $ldap->start_tls(%h);
if ( $start_tls->code ) {
die 'tls failed: ' . $start_tls->error;
}
}
# Bind with credentials
my $bind =
$ldap->bind( $self->ldapBindDN, password => $self->ldapBindPassword );
if ( $bind->code ) {
die 'bind failed: ' . $bind->error;
}
$self->{ldap} = $ldap;
return $ldap;
}
## @method string getIdentifier(string uid, string ref, string date)
# Get notification identifier
# @param $uid uid
# @param $ref ref
# @param $date date
# @return the notification identifier
sub getIdentifier {
my ( $self, $uid, $ref, $date ) = @_;
return $date . "#" . $uid . "#" . $ref;
}
1;
( run in 1.747 second using v1.01-cache-2.11-cpan-39bf76dae61 )