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