2008-12-31 16:10:02 +01:00
|
|
|
##@file
|
|
|
|
# LDAP common functions
|
|
|
|
|
|
|
|
##@class
|
|
|
|
# LDAP common functions
|
2008-10-05 20:42:50 +02:00
|
|
|
package Lemonldap::NG::Portal::_LDAP;
|
|
|
|
|
2009-02-15 09:53:44 +01:00
|
|
|
require Net::LDAP;
|
|
|
|
use Exporter;
|
|
|
|
use base qw(Exporter Net::LDAP);
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @EXPORT = qw(ldap);
|
2008-10-05 20:42:50 +02:00
|
|
|
|
2009-02-10 18:28:27 +01:00
|
|
|
our $VERSION = '0.11';
|
2008-10-05 20:42:50 +02:00
|
|
|
|
2008-12-31 16:10:02 +01:00
|
|
|
## @cmethod Lemonldap::NG::Portal::_LDAP new(Lemonldap::NG::Portal::Simple portal)
|
|
|
|
# Build a Net::LDAP object using parameters issued from $portal
|
|
|
|
# @return Lemonldap::NG::Portal::_LDAP object
|
2008-10-05 20:42:50 +02:00
|
|
|
sub new {
|
2008-10-07 22:15:48 +02:00
|
|
|
my $class = shift;
|
2008-10-05 20:42:50 +02:00
|
|
|
my $portal = shift;
|
2008-10-07 22:15:48 +02:00
|
|
|
my $self;
|
|
|
|
unless ($portal) {
|
2008-11-21 08:27:08 +01:00
|
|
|
$class->abort("$class : portal argument required !");
|
2008-10-05 20:42:50 +02:00
|
|
|
}
|
|
|
|
my $useTls = 0;
|
|
|
|
my $tlsParam;
|
2008-10-07 22:15:48 +02:00
|
|
|
foreach my $server ( split /[\s,]+/, $portal->{ldapServer} ) {
|
2008-10-05 20:42:50 +02:00
|
|
|
if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
|
|
|
|
$useTls = 1;
|
|
|
|
$server = $1;
|
|
|
|
$tlsParam = $2 || "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$useTls = 0;
|
|
|
|
}
|
|
|
|
last
|
2008-10-07 22:15:48 +02:00
|
|
|
if $self = Net::LDAP->new(
|
2008-10-05 20:42:50 +02:00
|
|
|
$server,
|
2008-10-07 22:15:48 +02:00
|
|
|
port => $portal->{ldapPort},
|
2008-10-05 20:42:50 +02:00
|
|
|
onerror => undef,
|
|
|
|
);
|
|
|
|
}
|
2008-10-07 22:15:48 +02:00
|
|
|
unless ($self) {
|
2009-02-12 20:48:53 +01:00
|
|
|
$portal->lmLog( $@, 'error' );
|
2008-10-07 22:15:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-08 10:45:15 +02:00
|
|
|
bless $self, $class;
|
2008-10-05 20:42:50 +02:00
|
|
|
if ($useTls) {
|
|
|
|
my %h = split( /[&=]/, $tlsParam );
|
2008-10-07 22:15:48 +02:00
|
|
|
$h{cafile} = $portal->{caFile} if ( $portal->{caFile} );
|
|
|
|
$h{capath} = $portal->{caPath} if ( $portal->{caPath} );
|
2009-01-17 20:45:21 +01:00
|
|
|
my $mesg = $self->start_tls(%h);
|
2008-10-07 22:15:48 +02:00
|
|
|
if ( $mesg->code ) {
|
2009-02-12 20:48:53 +01:00
|
|
|
$portal->lmLog( 'StartTLS failed', 'error' );
|
2008-10-07 22:15:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-05 20:42:50 +02:00
|
|
|
}
|
2008-10-07 22:15:48 +02:00
|
|
|
$self->{portal} = $portal;
|
|
|
|
return $self;
|
2008-10-05 20:42:50 +02:00
|
|
|
}
|
|
|
|
|
2008-12-31 16:10:02 +01:00
|
|
|
## @method Net::LDAP::Message bind(string dn, %args)
|
|
|
|
# Reimplementation of Net::LDAP::bind(). Connection is done :
|
|
|
|
# - with $dn and $args->{password} as dn/password if defined,
|
|
|
|
# - or with Lemonldap::NG account,
|
|
|
|
# - or with an anonymous bind.
|
|
|
|
# @param $dn LDAP distinguish name
|
|
|
|
# @param %args See Net::LDAP(3) manpage for more
|
|
|
|
# @return Net::LDAP::Message
|
2008-10-05 20:42:50 +02:00
|
|
|
sub bind {
|
|
|
|
my $self = shift;
|
2008-10-07 22:15:48 +02:00
|
|
|
my $mesg;
|
|
|
|
my ( $dn, %args ) = @_;
|
2009-02-12 20:48:53 +01:00
|
|
|
unless ($dn) {
|
2008-12-31 16:10:02 +01:00
|
|
|
$dn = $self->{portal}->{managerDn};
|
|
|
|
$args{password} = $self->{portal}->{managerPassword};
|
|
|
|
}
|
2008-11-05 22:26:37 +01:00
|
|
|
if ( $dn && $args{password} ) {
|
|
|
|
$mesg = $self->SUPER::bind( $dn, %args );
|
2008-11-21 18:51:52 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$mesg = $self->SUPER::bind();
|
2008-11-05 22:26:37 +01:00
|
|
|
}
|
|
|
|
return $mesg;
|
2008-10-05 20:42:50 +02:00
|
|
|
}
|
|
|
|
|
2009-02-15 09:53:44 +01:00
|
|
|
## @method protected Lemonldap::NG::Portal::_LDAP ldap()
|
|
|
|
# @return Lemonldap::NG::Portal::_LDAP object
|
|
|
|
sub ldap {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{ldap} if ( ref( $self->{ldap} ) );
|
|
|
|
if ( $self->{ldap} = Lemonldap::NG::Portal::_LDAP->new($self)
|
|
|
|
and my $mesg = $self->{ldap}->bind )
|
|
|
|
{
|
|
|
|
return $self->{ldap} if ( $mesg->code == 0 );
|
|
|
|
$self->lmLog( "LDAP error : " . $mesg->error, 'error' );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->lmLog( "LDAP error : $@", 'error' );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-05 20:42:50 +02:00
|
|
|
1;
|