#!/usr/bin/perl
#################################################################
# perl ouSearch.pl > ouSearch.txt
#################################################################

use Mozilla::LDAP::Conn;

$adserver = 'ms-ads.vzla.gob.ve';
$adport = '389';
$baseDN = 'DC=vzla,DC=gob,DC=ve';
my $aduser = 'CN=Administrator,CN=Users,DC=vzla,DC=gob,DC=ve';
my $adpass = '<PASS>';

$conn = new Mozilla::LDAP::Conn($adserver,$adport,$aduser,$adpass);
die "No puede conectarse al servidor $adserver" unless ($conn);

&traverse_org_unit($baseDN);
exit(0);

sub traverse_org_unit()
{
  my ($start_dn) = @_;
  my ($entry, $dn, $scope, $filter, $dn) = "";
  my (@DNs, @attrs) = ();
  
  
  @attrs = ( "ou" );
  $scope = "one";
  $filter = "(ou=*)";
  $entry = $conn->search($start_dn, $scope, $filter, 0, @attrs);
  
  if (! $entry)
  {
    # print "No se encontraron objetos OU";
  }
  else
  {
    while ($entry)
    {
      $dn = $entry->getDN();
      push (@DNs,$dn);
      print "$dn\n";
      $entry = $conn->nextEntry();
    }
  }

  foreach $dn (@DNs)
  {
    &traverse_org_unit($dn)
  }
}
