########################################################################## # package LDAPTools.pm # This module contains functions which asks hosts questions # V Who When What # --------------------------------------------------------------------------- # 1.0.3 skendric 2008-07-30 Handle down LDAP servers gracefully # 1.0.2 skendric 2008-06-16 Stylistic mods # 1.0.1 skendric 2007-03-12 Stylistic mods # 1.0.0 skendric 2005-07-25 First version # # # # Authors: Stuart Kendrick # # Source: http://www.skendric.com/device/soma # # This software is available under the GNU GENERAL PUBLIC LICENSE, see # http://www.fsf.org/licenses/gpl.html # package FHCRC::VDOPS::LDAPTools; #### Load modules #### use strict; use warnings; use threads; use threads::shared; use Carp qw(carp cluck croak confess); use Data::Dumper; use Exporter; use Net::LDAP::Express; use Perl6::Say; use lib '/home/soma/lib'; use FHCRC::VDOPS::SomaData; use FHCRC::VDOPS::Utilities; #### Set-up export stuff #### our @ISA = qw(Exporter); our @EXPORT = qw( connect_ldap ); # Declare package local variables ##### Only subroutines below here #### ######################################################################## # Given a reference to a list of LDAP servers and a reference to a list # of searchattrs, find the first server in the list which is functioning # and return a Net::LDAP::Express object refering to that server along # with the server's name. Return undef if none of the LDAP servers # respond ######################################################################## sub connect_ldap { my $bound; # The server to which we successfully bind my $ldap; # Net::LDAP::Express connection object my $servers = shift; # Reference to list of LDAP servers my $search = shift; # Reference to list of searchattrs # Debug trace trace_location('begin') if $debug > 2; # Sanity check confess 'Server list broken' unless (ref $servers eq 'ARRAY' and @$servers > 0); confess 'Attrs list broken' unless (ref $search eq 'ARRAY' and @$search > 0); # Find a working LDAP server LDAPServer: for my $server (@$servers) { my ($service, $uri); # Construct URI if ($ldapTLS) { $service = 'ldaps'; $ldapPort = 636 unless defined $ldapPort } else { $service = 'ldap'; $ldapPort = 389 unless defined $ldapPort; } $uri = $service . '://' . $server . $COLON. $ldapPort; # Debug info if ($debug > 2) { say "Connecting to LDAP server $server using these parameters:"; say " host = $uri"; say " base = $ldapBase"; say " bindDN = $ldapBindDN"; say " bindpw = $ldapBindPW"; say " searchattrs = @$search"; } eval { $ldap = Net::LDAP::Express->new ( host => $uri, base => $ldapBase, bindDN => $ldapBindDN, bindpw => $ldapBindPW, searchattrs => $search ) }; # Parse results if ($@) { say "Cannot connect to $server: $@" if $debug; } else { $bound = $server; say "Using LDAP server $bound" if $debug; last LDAPServer; } } # Debug trace trace_location('end') if $debug > 2; return $ldap, $bound; } 1;