From 297ce7fe28302141e53fb85c12ccc1db65ff59d7 Mon Sep 17 00:00:00 2001 From: octo Date: Fri, 6 May 2005 10:25:49 +0000 Subject: [PATCH] More and more group management changes.. This is going to be messy.. --- lib/LiCoM/Config.pm | 28 +++++++++++++++-- lib/LiCoM/Connection.pm | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/LiCoM/Person.pm | 44 +++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 lib/LiCoM/Connection.pm diff --git a/lib/LiCoM/Config.pm b/lib/LiCoM/Config.pm index 1c561a2..c631a4b 100644 --- a/lib/LiCoM/Config.pm +++ b/lib/LiCoM/Config.pm @@ -12,9 +12,33 @@ return (1); 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>) diff --git a/lib/LiCoM/Connection.pm b/lib/LiCoM/Connection.pm new file mode 100644 index 0000000..ad78378 --- /dev/null +++ b/lib/LiCoM/Connection.pm @@ -0,0 +1,80 @@ +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-EB (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-EB () + +Disconnect from the LDAP-Server. + +=cut + +sub disconnect +{ + $Ldap->unbind (); + $Ldap = undef; +} + +=back + +=head1 AUTHOR + +Florian octo Forster Eocto at verplant.orgE + +=cut diff --git a/lib/LiCoM/Person.pm b/lib/LiCoM/Person.pm index d9453ff..e9b3063 100644 --- a/lib/LiCoM/Person.pm +++ b/lib/LiCoM/Person.pm @@ -578,6 +578,50 @@ sub get_user return ($cn, $id); } +=item LiCoM::Person-EB () + +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 -- 2.11.0