OpenInteract/LDAP.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 165 CC 10 SCAR 28.1
CC
10
Cov
0%
connect
42.0
bind
20.0
_w
6.0
CRAP 16.5
1 package OpenInteract::LDAP;
2  
3 # $Id: LDAP.pm,v 1.6 2002/01/02 02:43:53 lachoy Exp $
4  
5 use strict;
6 use Data::Dumper qw( Dumper );
7 use Net::LDAP qw();
8  
9 @OpenInteract::LDAP::ISA = ();
10 $OpenInteract::LDAP::VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/);
11  
12 use constant DEBUG => 0;
13 use constant LDAP_PORT => 389;
14 use constant LDAP_DEBUG => 0;
15 use constant LDAP_TIMEOUT => 120;
16 use constant LDAP_VERSION => 2;
17  
18 sub connect {
19 my ( $class, $ldap_info, $p ) = @_;
20 $p ||= {};
21  
22 # Allow callback to modify the connection info; note that we
23 # dereference $ldap_info for a reason here -- so we don't mess up the
24 # original information, say, in the config hashref
25  
26 if ( ref $p->{pre_connect} eq 'CODE' ) {
27 DEBUG && _w( 1, "Before pre_connect code: ", Dumper( $ldap_info ) );
28 my $new_ldap_info = $p->{pre_connect}->( \%{ $ldap_info } );
29 $ldap_info = $new_ldap_info if ( $new_ldap_info->{host} );
30 }
31  
32 # Set defaults
33  
34 $ldap_info->{port} ||= LDAP_PORT;
35 $ldap_info->{debug} ||= LDAP_DEBUG;
36 $ldap_info->{timeout} ||= LDAP_TIMEOUT;
37 $ldap_info->{version} ||= LDAP_VERSION;
38  
39 unless ( $ldap_info->{host} ) {
40 die "Key 'host' must be defined in first hashref of parameters.\n";
41 }
42  
43 # Make the actual connection
44  
45 DEBUG && _w( 1, "Trying to connect to LDAP with information:\n",
46 Dumper( $ldap_info ) );
47 my $ldap = Net::LDAP->new( $ldap_info->{host},
48 timeout => $ldap_info->{timeout},
49 port => $ldap_info->{port},
50 debug => $ldap_info->{debug},
51 version => $ldap_info->{version} );
52 unless ( $ldap ) {
53 die "Connect failed: cannot create connection to LDAP directory.\n";
54 }
55 DEBUG && _w( 1, "New LDAP handle created ok." );
56  
57 # Allow callback to do something with the database handle along with
58 # the parameters used to connect to it.
59  
60 if ( ref $p->{post_connect} eq 'CODE' ) {
61 DEBUG && _w( 1, "Calling post_connect code with handle and info" );
62 $p->{post_connect}->( \%{ $ldap_info }, $ldap );
63 }
64 return $ldap;
65 }
66  
67 sub bind {
68 my ( $class, $ldap, $ldap_info ) = @_;
69  
70 my %bind_params = ();
71 if ( $ldap_info->{sasl} and $ldap_info->{bind_dn} ) {
72 require Authen::SASL;
73 $bind_params{sasl} = Authen::SASL->new( 'CRAM-MD5',
74 password => $ldap_info->{bind_password} );
75 }
76 elsif ( $ldap_info->{bind_dn} ) {
77 $bind_params{password} = $ldap_info->{bind_password};
78 }
79  
80 DEBUG && _w( 1, "Calling bind() with DN ($ldap_info->{bind_dn}) and params:\n",
81 Dumper( \%bind_params ) );
82 my $bind_msg = $ldap->bind( $ldap_info->{bind_dn}, %bind_params );
83 if ( my $bind_code = $bind_msg->code ) {
84 die "Bind failed: ", $bind_msg->error, " (Code: $bind_code)\n";
85 }
86 DEBUG && _w( 1, "Bind executed ok." );
87 return $ldap;
88 }
89  
90  
91 sub connect_and_bind {
92 my ( $class, $ldap_info, @params ) = @_;
93 my $ldap = $class->connect( $ldap_info, @params );
94 return $class->bind( $ldap, $ldap_info );
95 }
96  
97  
98 sub _w {
99 return unless ( DEBUG >= shift );
100 my ( $pkg, $file, $line ) = caller;
101 my @ci = caller(1);
102 warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n";
103 }
104  
105  
106 1;
107  
108 __END__