Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve all Groups from LDAP with Perl

I have a Perl script wich binds to an LDAP server and retrieves all users. So far it works good but I want to filter that search in order to gather all groups. Once I have all groups the user can select one of these groups and I'll show him only users that are member of that group. How can I do those queries? I tryed this one:

my $mesg = $ldap->search(
    base => $base,
    filter => '(objectclass=user)',
    attrs => ['memberOf']
);

But then some groups are repeated and I will have to manually filter the result (and I'd like to avoid that). And what about the second query?

like image 911
raz3r Avatar asked Dec 20 '11 08:12

raz3r


2 Answers

cnThe filter to get all groups is "(objectclass=group)" you can retreive groups in only one organizationalUnit (scope => 'one') or in all suborganization (scope => 'sub')

$mesg = $ldap->search(  filter=>"(&(objectclass=group)(cn=the group choosen by the user)", 
                        base=>"ou=Monou,dc=societe,dc=fr"
                        scope=>"sub"
                        attrs=> ['cn', 'member']);
@entries = $mesg->entries;
foreach $entry (@entries)
{
    $entry->dump;
    @member = $entry->get_value("member");  # returns all members 
}

For more help see An Introduction to perl-ldap


Edited

So the filter you were looking for is :

(&(objectClass=user)(memberof=CN=Mongroupe,OU=MonOU,DC=societe,DC=fr))
like image 145
JPBlanc Avatar answered Sep 25 '22 14:09

JPBlanc


Use objectclass=* to get all.

my $msg = $ldap->search(base => $dn,
            scope => 'one',
            filter => "(objectclass=*)");
$msg->all_entries;          
like image 39
Bala Avatar answered Sep 22 '22 14:09

Bala