hi,<br><br>so this is a working version of the script (tested on my test ipa environment).<br><br>You save it as executable and run it as:<br><br>$./script ipausername<br><br>and you will get the groupnames separated by an empty space a user is member of.<br>
<br>modify the obvious bits, like kdc.domain.tld, user and password, and base. You also need the perl-LDAP rpm package.<br><br>The user that binds to the ldap server needs privileges (do not know exactly which ones, but as a normal user I cannot see the group memberships). I have run it as admin and it works. Probably overkill, if the user you use is member of the role 'user administrators' it should work as well. Not tested.<br>
<br><br>#!/usr/bin/perl<br><br>use strict;<br>use warnings;<br>use Net::LDAP;<br><br># Script requires user UID as the only parameter<br>if ( $ARGV[0] eq '' ) {<br>    print "<a href="http://ldap-query.pl">ldap-query.pl</a> requires one argument, user's uid\n";<br>
    exit 1;<br>}<br><br>my $user = $ARGV[0];<br><br># Create communication structure for LDAP connection<br>my $ldap = Net::LDAP->new( 'kdc.domain.tld' ) or die "$@";<br><br># Bind to LDAP with proper user<br>
my $msg = $ldap->bind(<br>         "uid=admin,cn=users,cn=accounts,dc=domain,dc=tld",<br>          password => "pwd",<br>);<br><br># search objects filtering in uid, get memberOf attribute only<br>
$msg = $ldap->search(<br>            base => "cn=users,cn=accounts,dc=domain,dc=tld",<br>            scope => "sub",<br>            filter => "(uid=$user)",<br>            attr => ['memberOf'],<br>
);<br><br># get the group membership of $user and print it in a line<br>for my $entry ( $msg->entries ) {<br>    my @memberof = $entry->get_value( 'memberOf') ;<br><br>    # the memberof attr is a full dn but we only want the cn, so we<br>
    # use the map function to strip everything else<br>    @memberof = map { s/^cn=(.*),cn=groups.*/$1/g; $_ } @memberof;<br><br>    # admin users or users with delegated privileges are members of groups<br>    # names containing spaces, we skip those. If this is not what you want,<br>
    # you need to adapt the for loop<br>    for ( @memberof ) {<br>        next if /(replication |add |host|uniqueid|unlock |manage |trust )/ ;<br>        print "$_" . " " ;<br>    }<br>    print "\n";<br>
}<br><br>have fun!<br><br>-- <br>groet,<br>natxo<br>