ad7837825cf97092a95f0f23ac6102459e71d9b8
[licom.git] / lib / LiCoM / Connection.pm
1 package LiCoM::Connection;
2
3 use strict;
4 use warnings;
5
6 use Exporter;
7 use Net::LDAP;
8 use Net::LDAP::Filter;
9
10 =head1 NAME
11
12 LiCoM::Connection - Represents the connection to an LDAP-server.
13
14 =cut
15
16 our %Config = get_config ();
17 our $Ldap;
18
19 @LiCoM::Connection::EXPORT_OK = (qw($Ldap));
20 @LiCoM::Connection::ISA = ('Exporter');
21
22 return (1);
23
24 =head1 METHODS
25
26 =over 4
27
28 =item LiCoM::Connection-E<gt>B<connect> (I<$server>, I<$bind_dn>, I<$password>, I<$base_dn>, [I<$port>])
29
30 Connects to the LDAP-Server given.
31
32 =cut
33
34 sub connect
35 {
36         my $pkg = shift;
37         my %opts = @_;
38
39         my $bind_dn = $opts{'bind_dn'};
40         my $base_dn = $opts{'base_dn'};
41         my $uri     = $opts{'uri'};
42         my $passwd  = $opts{'password'};
43
44         my $msg;
45
46         die unless ($bind_dn and $base_dn and $uri and defined ($passwd));
47
48         $Ldap = Net::LDAP->new ($uri);
49
50         $msg = $Ldap->bind ($bind_dn, password => $passwd);
51         if ($msg->is_error ())
52         {
53                 warn ('LDAP bind failed: ' . $msg->error_text ());
54                 return (0);
55         }
56
57         $Config{'base_dn'} = $base_dn;
58
59         return (1);
60 }
61
62 =item LiCoM::Connection-E<gt>B<disconnect> ()
63
64 Disconnect from the LDAP-Server.
65
66 =cut
67
68 sub disconnect
69 {
70         $Ldap->unbind ();
71         $Ldap = undef;
72 }
73
74 =back
75
76 =head1 AUTHOR
77
78 Florian octo Forster E<lt>octo at verplant.orgE<gt>
79
80 =cut