sub get_config
{
- my $file = @_ ? shift : '/etc/licom/licom.conf';
+ my @files = ('/etc/licom/licom.conf');
+
+ if (@_)
+ {
+ @files = @_;
+ }
+ elsif (defined ($ENV{'HOME'}) and (-d $ENV{'HOME'}))
+ {
+ push (@files, $ENV{'HOME'} . '/.licomrc');
+ }
+
+ for (@files)
+ {
+ my $file = $_;
+ next unless (-r $file);
+
+ read_file ($file, $config);
+ }
+
+ return ($config);
+}
+
+sub read_file
+{
+ my $file = @_ ? shift : '/etc/licom/licom.conf';
+ my $config = @_ ? shift : {};
my $fh;
- my $config = {};
open ($fh, "< $file") or die ("open ($file): $!");
for (<$fh>)
--- /dev/null
+package LiCoM::Connection;
+
+use strict;
+use warnings;
+
+use Exporter;
+use Net::LDAP;
+use Net::LDAP::Filter;
+
+=head1 NAME
+
+LiCoM::Connection - Represents the connection to an LDAP-server.
+
+=cut
+
+our %Config = get_config ();
+our $Ldap;
+
+@LiCoM::Connection::EXPORT_OK = (qw($Ldap));
+@LiCoM::Connection::ISA = ('Exporter');
+
+return (1);
+
+=head1 METHODS
+
+=over 4
+
+=item LiCoM::Connection-E<gt>B<connect> (I<$server>, I<$bind_dn>, I<$password>, I<$base_dn>, [I<$port>])
+
+Connects to the LDAP-Server given.
+
+=cut
+
+sub connect
+{
+ my $pkg = shift;
+ my %opts = @_;
+
+ my $bind_dn = $opts{'bind_dn'};
+ my $base_dn = $opts{'base_dn'};
+ my $uri = $opts{'uri'};
+ my $passwd = $opts{'password'};
+
+ my $msg;
+
+ die unless ($bind_dn and $base_dn and $uri and defined ($passwd));
+
+ $Ldap = Net::LDAP->new ($uri);
+
+ $msg = $Ldap->bind ($bind_dn, password => $passwd);
+ if ($msg->is_error ())
+ {
+ warn ('LDAP bind failed: ' . $msg->error_text ());
+ return (0);
+ }
+
+ $Config{'base_dn'} = $base_dn;
+
+ return (1);
+}
+
+=item LiCoM::Connection-E<gt>B<disconnect> ()
+
+Disconnect from the LDAP-Server.
+
+=cut
+
+sub disconnect
+{
+ $Ldap->unbind ();
+ $Ldap = undef;
+}
+
+=back
+
+=head1 AUTHOR
+
+Florian octo Forster E<lt>octo at verplant.orgE<gt>
+
+=cut
return ($cn, $id);
}
+=item LiCoM::Person-E<gt>B<all_groups> ()
+
+Returns all defined groups. In scalar context returns a hash-ref with the
+group-names as keys and the number of group members as values. In list context
+returns a sorted list of group names.
+
+=cut
+
+sub all_groups
+{
+ my $pkg = shift;
+ my %retval = ();
+
+ my $mesg = $Ldap->search
+ (
+ base => $Config{'base_dn'},
+ filter => "(objectClass=groupOfNames)"
+ );
+
+ if ($mesg->is_error ())
+ {
+ warn ("Error while querying LDAP server: " . $mesg->error_text ());
+ return (qw());
+ }
+
+ for ($mesg->entries ())
+ {
+ my $entry = $_;
+ my ($name) = $entry->get_value ('cn', asref => 0);
+ my @members = $entry->get_value ('member', asref => 0);
+
+ $retval{$name} = scalar (@members);
+ }
+
+ if (wantarray ())
+ {
+ return (sort (keys %retval));
+ }
+ else
+ {
+ return (\%retval);
+ }
+}
+
=back
=head1 AUTHOR