#!/usr/bin/perl -w
# ------------------------------------------------------------
# script:  ldaplistgroups.pl
# Author:  Brad Marshall (bmarshal@pisoftware.com)
# License: GPL
# Date:    20000203
#
# Purpose: Lists which groups a user is in
#
# Copyright (c) 2000 Plugged In Software Pty Ltd.  All rights reserved.

use strict;
use Net::LDAP;

my($uid);
my(@groups);
my($entry);
my($host) = 'ldap.staff.plugged.com.au';

if (! $ARGV[0]) {
    print "$0: $0 username\n";
    exit 1;
}

if ($ARGV[0]) {
    $uid = $ARGV[0];
} else {
    die "Sorry, you must specify a login";
}

my($ldap) = Net::LDAP->new($host) or die "Can't bind to ldap: $!\n";

$ldap->bind;

my($mesg) = $ldap->search( base => "dc=pisoftware,dc=com",
					   filter => '(objectclass=posixGroup)');

$mesg->code && die $mesg->error;

foreach $entry ($mesg->entries) {
	my($tmpcn) = $entry->get('cn');
	my($tmpnum) = $entry->get('gidnumber');
	my(@members) = $entry->get('memberuid');

	#print "\@members = @members\n";
	#print "Checking $tmpcn...";

	my %t = ();
	@t{@members} = 1;
	if (exists $t{$uid}) {
		#print "yes.";
		# Push the cn onto an array
		push @groups, $tmpcn;
	}
	#print "\n";
}

$ldap->unbind;

my($groups) = join " ", @groups;
print "$uid is in $groups\n";


