#!/usr/bin/perl -w
# ------------------------------------------------------------
# script:  ldapdumpusers.pl
# Author:  Brad Marshall (bmarshal@pisoftware.com)
# License: GPL
# Date:    20000203
#
# Purpose: Dumps the users into an /etc/passwd type file
#
# Copyright (c) 2000 Plugged In Software Pty Ltd.  All rights reserved.

use strict;
use Net::LDAP;

my($ldap) = Net::LDAP->new('ldap.staff.plugged.com.au') 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;

my($entry,$attr);

my(%results);
foreach $entry ($mesg->entries) {

# student::509:bmarshal
# gid:userpassword:uidnumber:users

	my($tmpcn) = $entry->get('cn');
	my($tmpnum) = $entry->get('gidnumber');
	my($tmppassword) = $entry->get('userpassword');
	if ($tmppassword) {
		$tmppassword =~ s/^{crypt}//;
	}
	if (! $tmppassword) {
		$tmppassword = "x";
	}
	my(@users) = $entry->get('memberuid');
	my($users) = join(",", @users);

	my($tmpstr) = "$tmppassword:$tmpnum:$users";
	$results{$tmpcn} = $tmpstr;
}

sub bygroup {
	(split /:/,$results{$a})[1] <=> (split /:/,$results{$b})[1]
}

my($foo);
foreach $foo (sort bygroup keys %results) {
	#printf "%3d\t%s\n",$results{$foo}, $foo;
	print "$foo:$results{$foo}\n";
}

$ldap->unbind;


