| 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::LDAPConf |
| 29 |
|
| 30 |
Kamailio::LDAPUtils::LDAPConf - Read openldap config from standard config files. |
| 31 |
|
| 32 |
use Kamailio::LDAPUtils::LDAPConf; |
| 33 |
my $conf = new Kamailio::LDAPUtils::LDAPConf(); |
| 34 |
|
| 35 |
This module may be used to retrieve the global LDAP configuration as |
| 36 |
used by other LDAP software, such as C<nsswitch.ldap> and C<pam-ldap>. |
| 37 |
The configuration is usually stored in C</etc/openldap/ldap.conf> |
| 38 |
|
| 39 |
When used from an account with sufficient privilegs (e.g. root), the |
| 40 |
ldap manager password is also retrieved. |
| 41 |
|
| 42 |
=cut |
| 43 |
package Kamailio::LDAPUtils::LDAPConf; |
| 44 |
|
| 45 |
my $def_ldap_path = "/etc/openldap" ; |
| 46 |
my $def_conf = "ldap.conf"; |
| 47 |
my $def_secret = "ldap.secret"; |
| 48 |
|
| 49 |
=head2 Constructor new() |
| 50 |
|
| 51 |
Returns a new, initialized C<Kamailio::LDAPUtils::LDAPConf> object. |
| 52 |
|
| 53 |
=cut |
| 54 |
sub new { |
| 55 |
my $class = shift; |
| 56 |
|
| 57 |
my $self = _init( $def_ldap_path . "/" . $def_conf , |
| 58 |
$def_ldap_path . "/" . $def_secret); |
| 59 |
|
| 60 |
if( ! $self ) { return undef; } ## can happen during customizing |
| 61 |
|
| 62 |
bless $self , $class; |
| 63 |
|
| 64 |
return $self; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
sub _init { |
| 69 |
my $conf_file = shift; |
| 70 |
my $secret_file = shift; |
| 71 |
|
| 72 |
my $result = {}; |
| 73 |
|
| 74 |
if( open(LDAPCONF,"<$conf_file") ) { |
| 75 |
while(<LDAPCONF>) { |
| 76 |
chomp; |
| 77 |
s/#.*$//; |
| 78 |
if( m/^\s*$/ ) { next; } |
| 79 |
my ($var,$val) = split(" ",$_,2); |
| 80 |
|
| 81 |
$result->{lc($var)} = $val; |
| 82 |
|
| 83 |
} |
| 84 |
close(LDAPCONF); |
| 85 |
} else { |
| 86 |
return undef; |
| 87 |
} |
| 88 |
|
| 89 |
if( -r $secret_file ) { |
| 90 |
if( open(SECRET,"<$secret_file") ) { |
| 91 |
my $secret = <SECRET>; |
| 92 |
chomp $secret; |
| 93 |
$result->{'rootbindpw'} = $secret; |
| 94 |
close(SECRET); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $result; |
| 99 |
} |
| 100 |
|
| 101 |
=head2 Method base() |
| 102 |
|
| 103 |
Returns the servers base-dn to use when doing queries. |
| 104 |
|
| 105 |
=cut |
| 106 |
sub base { return $_[0]->{'base'}; } |
| 107 |
|
| 108 |
=head2 Method host() |
| 109 |
|
| 110 |
Returns the ldap host to contact. |
| 111 |
|
| 112 |
=cut |
| 113 |
sub host { return $_[0]->{'host'}; } |
| 114 |
|
| 115 |
|
| 116 |
=head2 Method port() |
| 117 |
|
| 118 |
Returns the ldap servers port. |
| 119 |
|
| 120 |
=cut |
| 121 |
sub port { return $_[0]->{'port'}; } |
| 122 |
|
| 123 |
=head2 Method uri() |
| 124 |
|
| 125 |
Returns an uri to contact the ldap server. When there is no ldap_uri in |
| 126 |
the configuration file, an C<ldap:> uri is constucted from host and port. |
| 127 |
|
| 128 |
=cut |
| 129 |
sub uri { |
| 130 |
my $self = shift; |
| 131 |
|
| 132 |
if( $self->{'ldap_uri'} ) { return $self->{'ldap_uri'}; } |
| 133 |
|
| 134 |
return "ldap://" . $self->host . ":" . $self->port ; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
=head2 Method rootbindpw() |
| 139 |
|
| 140 |
Returns the ldap "root" password. |
| 141 |
|
| 142 |
Note that the C<rootbindpw> is only available when the current account has |
| 143 |
sufficient privilegs to access C</etc/openldap/ldap.secret>. |
| 144 |
|
| 145 |
=cut |
| 146 |
sub rootbindpw { return $_[0]->{'rootbindpw'}; } |
| 147 |
|
| 148 |
=head2 Method rootbinddn() |
| 149 |
|
| 150 |
Returns the DN to use for "root"-access to the ldap server. |
| 151 |
|
| 152 |
=cut |
| 153 |
sub rootbinddn { return $_[0]->{'rootbinddn'}; } |
| 154 |
|
| 155 |
=head2 Method binddn() |
| 156 |
|
| 157 |
Returns the DN to use for authentication to the ldap server. When no |
| 158 |
bind dn has been specified in the configuration file, returns the |
| 159 |
C<rootbinddn>. |
| 160 |
|
| 161 |
=cut |
| 162 |
sub binddn { |
| 163 |
my $self = shift; |
| 164 |
|
| 165 |
if( $self->{'binddn'} ) { return $self->{'binddn'}; } |
| 166 |
return $self->rootbinddn; |
| 167 |
} |
| 168 |
|
| 169 |
=head2 Method bindpw() |
| 170 |
|
| 171 |
Returns the password to use for authentication to the ldap server. When no |
| 172 |
bind password has been specified, returns the C<rootbindpw> if any. |
| 173 |
|
| 174 |
=cut |
| 175 |
sub bindpw { |
| 176 |
my $self = shift; |
| 177 |
|
| 178 |
if( $self->{'bindpw'} ) { return $self->{'bindpw'}; } |
| 179 |
return $self->rootbindpw; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
1; |
| 186 |
|