1 |
joko |
1.1 |
package Data::Storage::Handler::NetLDAP; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
use base ("Data::Storage::Handler"); |
7 |
|
|
use base ("Data::Storage::Handler::Abstract"); |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
use Data::Dumper; |
11 |
|
|
use Net::LDAP; |
12 |
|
|
use Net::LDAP::Entry; |
13 |
|
|
#use Net::LDAP::Search; |
14 |
|
|
|
15 |
|
|
# get logger instance |
16 |
|
|
my $logger = Log::Dispatch::Config->instance; |
17 |
|
|
|
18 |
|
|
|
19 |
|
|
# the (package) global ldap-connection-handler |
20 |
|
|
my $ldap; |
21 |
|
|
|
22 |
|
|
# TODO: implement this! |
23 |
|
|
my $TRACELEVEL = 0; |
24 |
|
|
my $ldap_errors_file = '../log/ldap_errors.log'; |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
sub getMetaInfo { |
28 |
|
|
my $self = shift; |
29 |
|
|
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
30 |
|
|
return { |
31 |
|
|
'disconnectMethod' => 'disconnect', |
32 |
|
|
}; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
sub connect { |
36 |
|
|
|
37 |
|
|
my $self = shift; |
38 |
|
|
|
39 |
|
|
my $dsn = $self->{locator}->{dsn}; |
40 |
|
|
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
41 |
|
|
|
42 |
|
|
$self->{locator}->{connInfo} = $self->_getConnectionInfo($dsn); |
43 |
|
|
|
44 |
|
|
if (!$self->{locator}->{connInfo}->{host}) { |
45 |
|
|
$logger->critical( __PACKAGE__ . '->_getConnectionInfo: dsn-error: no host' ); |
46 |
|
|
return; |
47 |
|
|
} |
48 |
|
|
$self->{locator}->{connInfo}->{port} ||= '389'; |
49 |
|
|
|
50 |
|
|
#print Dumper($self); |
51 |
|
|
|
52 |
|
|
$ldap = Net::LDAP->new( |
53 |
|
|
$self->{locator}->{connInfo}->{host}, |
54 |
|
|
port => $self->{locator}->{connInfo}->{port}, |
55 |
|
|
#timeout => 120, |
56 |
|
|
debug => 0, |
57 |
|
|
#async => 1, |
58 |
|
|
#onerror => 'warn', |
59 |
|
|
onerror => \&_ldapError, |
60 |
|
|
#version => 3, |
61 |
|
|
) |
62 |
|
|
or do { |
63 |
|
|
my $message = { |
64 |
|
|
error => 'could not connect to server at ' . $self->{locator}->{connInfo}->{host} . ':' . $self->{locator}->{connInfo}->{port}, |
65 |
|
|
code => 'none', |
66 |
|
|
}; |
67 |
|
|
#$logger->error(__PACKAGE__ . "->connect: LDAP-ERROR: "); |
68 |
|
|
#$self->log("could not connect to ldap-server!"); |
69 |
|
|
_ldapError($message); |
70 |
|
|
return; |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
#$self->{_COREHANDLE} = |
74 |
|
|
$ldap->bind( |
75 |
|
|
$self->{locator}->{connInfo}->{binddn}, |
76 |
|
|
password => $self->{locator}->{connInfo}->{pass} |
77 |
|
|
) or die "$@"; |
78 |
|
|
|
79 |
|
|
$self->{locator}->{status}->{connected} = 1; |
80 |
|
|
|
81 |
|
|
return 1; |
82 |
|
|
|
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
sub _getConnectionInfo { |
86 |
|
|
my $self = shift; |
87 |
|
|
my $dsn = shift; |
88 |
|
|
$dsn =~ m/host=(.+?);binddn=(.+?);pass=(.+?)$/; |
89 |
|
|
my $args = { |
90 |
|
|
host => $1, |
91 |
|
|
binddn => $2, |
92 |
|
|
pass => $3, |
93 |
|
|
}; |
94 |
|
|
$args->{binddn} =~ s/'//g if $args->{binddn}; |
95 |
|
|
return $args; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
sub existsChildNode { |
99 |
|
|
my $self = shift; |
100 |
|
|
my $nodename = shift; |
101 |
|
|
|
102 |
|
|
#print Dumper($self->{locator}); |
103 |
|
|
|
104 |
|
|
my $dn = "ou=" . $nodename . ", " . $self->{locator}->{basedn}; |
105 |
|
|
|
106 |
|
|
$logger->debug( __PACKAGE__ . "->existsChildNode( dn '$dn' )" ); |
107 |
|
|
|
108 |
|
|
my $filter = "(objectClass=*)"; |
109 |
|
|
|
110 |
|
|
#{ |
111 |
|
|
my $mesg = $ldap->search( |
112 |
|
|
base => $dn, |
113 |
|
|
filter => $filter, |
114 |
|
|
); |
115 |
|
|
#} |
116 |
|
|
|
117 |
|
|
#return; |
118 |
|
|
|
119 |
|
|
#print "search-result-code: ", $mesg->code, "\n"; |
120 |
|
|
#print "search-result-error: ", $mesg->error, "\n"; |
121 |
|
|
# print "LDAP-error: ", $mesg->{errorMessage}, "\n" if $mesg->{errorMessage}; |
122 |
|
|
|
123 |
|
|
#return; |
124 |
|
|
#print Dumper($mesg); |
125 |
|
|
#exit; |
126 |
|
|
|
127 |
|
|
#$mesg->code && die $mesg->error; |
128 |
|
|
|
129 |
|
|
return 1 if exists $mesg->{entries}; |
130 |
|
|
return 0; |
131 |
|
|
|
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
sub disconnect { |
135 |
|
|
my $self = shift; |
136 |
|
|
#$self->{_COREHANDLE}->unbind; # take down session |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
sub _ldapError { |
140 |
|
|
my $message = shift; |
141 |
|
|
#print Dumper($message); |
142 |
|
|
my $textMessage = "LDAP-ERROR: " . $message->error . " - code: " . $message->code; |
143 |
|
|
$textMessage .= " - dn: " . $message->{matchedDN} if $message->{matchedDN}; |
144 |
|
|
#print "msg: '$textMessage'", "\n"; |
145 |
|
|
#a2f($ldap_errors_file, $textMessage) if !$TRACELEVEL; |
146 |
|
|
$logger->error($textMessage); |
147 |
|
|
return $message; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
1; |