| 1 |
# |
| 2 |
# $Id$ |
| 3 |
# |
| 4 |
# Perl module for Kamailio |
| 5 |
# |
| 6 |
# Copyright (C) 2006 Collax GmbH |
| 7 |
# (Bastian Friedrich <[email protected]>) |
| 8 |
# |
| 9 |
# This file is part of Kamailio, a free SIP server. |
| 10 |
# |
| 11 |
# Kamailio is free software; you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by |
| 13 |
# the Free Software Foundation; either version 2 of the License, or |
| 14 |
# (at your option) any later version |
| 15 |
# |
| 16 |
# Kamailio is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
# GNU General Public License for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU General Public License |
| 22 |
# along with this program; if not, write to the Free Software |
| 23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
# |
| 25 |
|
| 26 |
# This file was kindly donated by Collax GmbH |
| 27 |
|
| 28 |
=head1 Kamailio::LDAPUtils::LDAPConnection |
| 29 |
|
| 30 |
Kamailio::LDAPUtils::LDAPConnection - Perl module to perform simple LDAP queries. |
| 31 |
|
| 32 |
OO-Style interface: |
| 33 |
|
| 34 |
use Kamailio::LDAPUtils::LDAPConnection; |
| 35 |
my $ldap = new Kamailio::LDAPUtils::LDAPConnection; |
| 36 |
my @rows = $ldap-search("uid=andi","ou=people,ou=coreworks,ou=de"); |
| 37 |
|
| 38 |
Procedural interface: |
| 39 |
|
| 40 |
use Kamailio::LDAPUtils::LDAPConnection; |
| 41 |
my @rows = $ldap->search( |
| 42 |
new Kamailio::LDAPUtils::LDAPConfig(), "uid=andi","ou=people,ou=coreworks,ou=de"); |
| 43 |
|
| 44 |
This perl module offers a somewhat simplified interface to the C<Net::LDAP> |
| 45 |
functionality. It is intended for cases where just a few attributes should |
| 46 |
be retrieved without the overhead of the full featured C<Net::LDAP>. |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
package Kamailio::LDAPUtils::LDAPConnection; |
| 51 |
|
| 52 |
use Kamailio::LDAPUtils::LDAPConf; |
| 53 |
use Net::LDAP; |
| 54 |
use Authen::SASL; |
| 55 |
|
| 56 |
my $ldap_singleton = undef; |
| 57 |
|
| 58 |
|
| 59 |
=head2 Constructor new( [config, [authenticated]] ) |
| 60 |
|
| 61 |
Set up a new LDAP connection. |
| 62 |
|
| 63 |
The first argument, when given, should be a hash reference pointing |
| 64 |
to to the connection parameters, possibly an C<Kamailio::LDAPUtils::LDAPConfig> |
| 65 |
object. This argument may be C<undef> in which case a new (default) |
| 66 |
C<Kamailio::LDAPUtils::LDAPConfig> object is used. |
| 67 |
|
| 68 |
When the optional second argument is a true value, the connection |
| 69 |
will be authenticated. Otherwise an anonymous bind is done. |
| 70 |
|
| 71 |
On success, a new C<LDAPConnection> object is returned, otherwise |
| 72 |
the result is C<undef>. |
| 73 |
|
| 74 |
=cut |
| 75 |
sub new { |
| 76 |
my $class = shift; |
| 77 |
my $conf = shift; |
| 78 |
my $doauth = shift; |
| 79 |
|
| 80 |
if( ! defined( $conf ) ) { |
| 81 |
$conf = new Kamailio::LDAPUtils::LDAPConf(); |
| 82 |
} |
| 83 |
|
| 84 |
#print STDERR "new ldap checks\n"; |
| 85 |
my $self = { |
| 86 |
conf => $conf, |
| 87 |
}; |
| 88 |
|
| 89 |
bless $self, $class; |
| 90 |
|
| 91 |
# |
| 92 |
# connect to ldap server and do an anonymous bind |
| 93 |
# |
| 94 |
$self->{'ldap'} = Net::LDAP->new( $self->conf->uri() ); |
| 95 |
|
| 96 |
# return undef if ldap connection failed |
| 97 |
if( ! $self->ldap ) { |
| 98 |
print STDERR "Could not connect to LDAP server.\n"; |
| 99 |
return undef; |
| 100 |
} |
| 101 |
|
| 102 |
if( ! $doauth ) { |
| 103 |
$self->ldap->bind ; # an anonymous bind |
| 104 |
} else { |
| 105 |
|
| 106 |
if( $self->conf->uri && ( $self->conf->uri =~ m/^ldapi:/ ) ) { |
| 107 |
|
| 108 |
my $sasl = new Authen::SASL( mechanism => 'EXTERNAL ANONYMOUS'); |
| 109 |
$self->ldap->bind($self->conf->binddn, sasl => $sasl ); |
| 110 |
|
| 111 |
} else { |
| 112 |
# only simple binds for now. |
| 113 |
$self->ldap->bind($self->conf->binddn, |
| 114 |
password => $self->conf->bindpw); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
return $self; |
| 120 |
}; |
| 121 |
|
| 122 |
sub DESTROY { |
| 123 |
my $self = shift; |
| 124 |
if( $self->ldap ) { |
| 125 |
$self->ldap->unbind(); |
| 126 |
delete $self->{'ldap'}; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
sub conf { return $_[0]->{'conf'}; } |
| 131 |
sub ldap { return $_[0]->{'ldap'}; } |
| 132 |
|
| 133 |
=head2 Function/Method search( conf, filter, base, [requested_attributes ...]) |
| 134 |
|
| 135 |
perform an ldap search, return the dn of the first matching |
| 136 |
directory entry, unless a specific attribute has been requested, |
| 137 |
in which case the values(s) fot this attribute are returned. |
| 138 |
|
| 139 |
When the first argument (conf) is a C<Kamailio::LDAPUtils::LDAPConnection>, it |
| 140 |
will be used to perform the queries. You can pass the first argument |
| 141 |
implicitly by using the "method" syntax. |
| 142 |
|
| 143 |
Otherwise the C<conf> argument should be a reference to a hash |
| 144 |
containing the connection setup parameters as contained in a |
| 145 |
C<Kamailio::LDAPUtils::LDAPConf> object. In this mode, the |
| 146 |
C<Kamailio::LDAPUtils::LDAPConnection> from previous queries will be reused. |
| 147 |
|
| 148 |
=head3 Arguments: |
| 149 |
|
| 150 |
=over 12 |
| 151 |
|
| 152 |
=item conf |
| 153 |
|
| 154 |
configuration object, used to find host,port,suffix and use_ldap_checks |
| 155 |
|
| 156 |
=item filter |
| 157 |
|
| 158 |
ldap search filter, eg '(mail=some@domain)' |
| 159 |
|
| 160 |
=item base |
| 161 |
|
| 162 |
search base for this query. If undef use default suffix, |
| 163 |
concat base with default suffix if the last char is a ',' |
| 164 |
|
| 165 |
=item requested_attributes |
| 166 |
|
| 167 |
retrieve the given attributes instead of the dn from the ldap directory. |
| 168 |
|
| 169 |
=back |
| 170 |
|
| 171 |
=head3 Result: |
| 172 |
|
| 173 |
Without any specific C<requested_attributes>, return the dn of all matching |
| 174 |
entries in the LDAP directory. |
| 175 |
|
| 176 |
When some C<requested_attributes> are given, return an array with those |
| 177 |
attibutes. When multiple entries match the query, the attribute lists |
| 178 |
are concatenated. |
| 179 |
|
| 180 |
=cut |
| 181 |
sub search { |
| 182 |
|
| 183 |
my $conf = shift; |
| 184 |
|
| 185 |
my $ldap = undef; |
| 186 |
|
| 187 |
if( $conf->isa("Kamailio::LDAPUtils::LDAPConnection") ) { |
| 188 |
$ldap = $conf; |
| 189 |
} else { |
| 190 |
if( ! $ldap_singleton ) { |
| 191 |
$ldap_singleton = new Kamailio::LDAPUtils::LDAPConnection($conf); |
| 192 |
} |
| 193 |
return undef unless $ldap_singleton; |
| 194 |
$ldap = $ldap_singleton; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
my $filter = shift; |
| 199 |
my $base = shift; |
| 200 |
my @requested_attr = @_ ; |
| 201 |
|
| 202 |
if( ! $base ) { |
| 203 |
$base = $ldap->conf->base; |
| 204 |
} elsif ( substr($base,-1) eq ',' ) { |
| 205 |
$base .= $ldap->conf->base; |
| 206 |
} |
| 207 |
|
| 208 |
my @search_extra = (); |
| 209 |
if( @requested_attr ) { |
| 210 |
push @search_extra , 'attrs' => [ @requested_attr ]; |
| 211 |
} |
| 212 |
|
| 213 |
my $mesg = $ldap->ldap->search ( |
| 214 |
base => $base, |
| 215 |
filter => $filter, |
| 216 |
#sizelimit => 100 , |
| 217 |
@search_extra |
| 218 |
); |
| 219 |
|
| 220 |
|
| 221 |
if( $mesg->is_error ) { warn $mesg->error; return undef }; |
| 222 |
|
| 223 |
my @result=(); |
| 224 |
my $max_entries=$mesg->count; |
| 225 |
my $i; |
| 226 |
for($i = 0 ; $i < $max_entries ; $i++) { |
| 227 |
my $entry = $mesg->entry($i); |
| 228 |
foreach my $attr (@requested_attr) { |
| 229 |
push @result, $entry->get_value($attr); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return @result; |
| 234 |
} |
| 235 |
|
| 236 |
1; |
| 237 |
|