OpenInteract2/Datasource/LDAP.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 182 CC 11 SCAR 31.2
CC
11
Cov
0%
connect
56.0
bind
30.0
connect_and_bind
2.0
CRAP 22.7
1 package OpenInteract2::Datasource::LDAP;
2  
3 # $Id: LDAP.pm,v 1.11 2005/03/17 14:58:01 sjn Exp $
4  
5 use strict;
6 use Log::Log4perl qw( get_logger );
7 use OpenInteract2::Constants qw( :log );
8 use OpenInteract2::Context qw( CTX );
9 use OpenInteract2::Exception qw( oi_error oi_datasource_error );
10  
11 $OpenInteract2::Datasource::LDAP::VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
12  
13 my ( $log );
14 my $REQUIRED = 0;
15  
16 use constant LDAP_PORT => 389;
17 use constant LDAP_DEBUG => 0;
18 use constant LDAP_TIMEOUT => 120;
19 use constant LDAP_VERSION => 2;
20  
21 sub connect {
22 my ( $class, $ds_name, $ds_info ) = @_;
23 unless ( $REQUIRED ) {
24 require Net::LDAP; $REQUIRED++;
25 }
26 $log ||= get_logger( LOG_DS );
27 unless ( ref $ds_info ) {
28 $log->error( "No data given to create LDAP [$ds_name] handle" );
29 oi_error "Cannot create connection without datasource info!";
30 }
31 unless ( $ds_name ) {
32 $log->warn( 'Correct usage of connect() is',
33 '$class->connect( $ds_name, \%ds_info ). ',
34 'Continuing...' );
35 }
36  
37 unless ( $ds_info->{host} ) {
38 $log->error( "Required configuration key undefined ",
39 "'datasource.$ds_name.host'" );
40 oi_error "Key 'host' must be defined in hashref of parameters.";
41 }
42  
43 # Set defaults
44  
45 $ds_info->{port} ||= LDAP_PORT;
46 $ds_info->{debug} ||= LDAP_DEBUG;
47 $ds_info->{timeout} ||= LDAP_TIMEOUT;
48 $ds_info->{version} ||= LDAP_VERSION;
49  
50 $log->is_debug &&
51 $log->debug( "LDAP connect info:\n", CTX->dump( $ds_info ) );
52  
53 my $ldap = Net::LDAP->new( $ds_info->{host},
54 timeout => $ds_info->{timeout},
55 port => $ds_info->{port},
56 debug => $ds_info->{debug},
57 version => $ds_info->{version} );
58  
59 unless ( $ldap ) {
60 oi_datasource_error
61 "Cannot create connection to LDAP directory",
62 { datasource_name => $ds_name,
63 datasource_type => 'LDAP',
64 connect_params => "$ds_info->{host} $ds_info->{port}" };
65 }
66  
67 $log->is_info &&
68 $log->info( "LDAP directory [$ds_name] connected ok." );
69  
70 if ( $ds_info->{perform_bind} ) {
71 $class->bind( $ldap, $ds_info );
72 }
73 return $ldap;
74 }
75  
76  
77 sub bind {
78 my ( $class, $ldap, $ds_info ) = @_;
79 $log ||= get_logger( LOG_DS );
80 my %bind_params = ();
81 if ( $ds_info->{sasl} and $ds_info->{bind_dn} ) {
82 eval { require Authen::SASL };
83 if ( $@ ) {
84 $log->error( "Authen::SASL not available, cannot do SASL auth" );
85 oi_error "You requested SASL authentication, but Authen::SASL ",
86 "could not be loaded: $@";
87 }
88 $bind_params{sasl} = Authen::SASL->new(
89 'CRAM-MD5',
90 password => $ds_info->{bind_password} );
91 }
92 elsif ( $ds_info->{bind_dn} ) {
93 $bind_params{password} = $ds_info->{bind_password};
94 }
95  
96 $log->is_debug &&
97 $log->debug( "Calling bind() with DN ($ds_info->{bind_dn}) ",
98 "and params:\n", CTX->dump( \%bind_params ) );
99 my $bind_msg = $ldap->bind( $ds_info->{bind_dn}, %bind_params );
100 if ( my $bind_code = $bind_msg->code ) {
101 my $error_msg = $bind_msg->error . " (Code: $bind_code)";
102 my $params = "DN: $ds_info->{bind_dn}; " .
103 join( '; ', map { "$_ = $bind_params{$_}" }
104 keys %bind_params );
105 OpenInteract2::Exception::Datasource->throw(
106 "Bind to LDAP directory failed: $error_msg",
107 { datasource_type => 'LDAP',
108 connect_params => $params } );
109 }
110 $log->is_info &&
111 $log->info( "Bound to [$ds_info->{bind_dn}] ok" );
112 }
113  
114  
115 sub connect_and_bind {
116 my ( $class, $ds_info, @params ) = @_;
117 my $ldap = $class->connect( $ds_info, @params );
118 $class->bind( $ldap, $ds_info );
119 return $ldap;
120 }
121  
122 # no-op...
123  
124 sub resolve_datasource_info {
125 my ( $self, $name, $ds_info ) = @_;
126 return { %{ $ds_info } };
127 }
128  
129 1;
130  
131 __END__