#!/usr/local/bin/perl
#
# $Id: LDAPDump.pl,v 1.13 2004/06/23 00:56:08 rdilley Exp $
#
# author: ron dilley
#
# desc: this perl script dumps info from an LDAP server
#
# Copyright (C) 2003  Ron A. Dilley
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ron.dilley@uberadmin.com
#
############################################################################

#
# modules
#
use Getopt::Std;
use Net::LDAP;

#
# pragmas
#
use strict;

#
# set environment
#
$ENV{PATH} = "/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb";

#
# turn on autoflush
#
select STDERR; $| = 1;
select STDOUT; $| = 1;

#
# defines
#
$::TRUE = 1;
$::FALSE = 0;
$::FAILED = -1;

$::VERSION = '$Id: LDAPDump.pl,v 1.13 2004/06/23 00:56:08 rdilley Exp $';
$::PROGNAME = "LDAPDump.pl";

%::Config = ();
$::Config{'debug'} = $::FALSE;
$::Config{'base'} = "";

#
# main routine
#
if ( &main() != $::TRUE ) {
  exit( 1 );
}

exit( 0 );

############################################################################
#
# sub-routines
#

#
# main routine
#
sub main {
  my $arg;

  #
  # display script banner
  #
  &show_banner();

  #
  # parse command-line
  #
  &parse_command_line();

  # process args that are left
  while( $arg = shift( @::ARGV ) ) {
    # do something with the spare args
  }
  &search_ldap( $::Config{'target'}, $::Config{'base'} );

  # done
  return $::TRUE;
}

#
# display banner info
#
sub show_banner {
  print STDERR "$::PROGNAME v$::VERSION\n";
  print STDERR "By: Ron Dilley\n";
  print STDERR "\n";
  print STDERR "$::PROGNAME comes with ABSOLUTELY NO WARRANTY.\n";
  print STDERR "This is free software, and you are welcome\n";
  print STDERR "to redistribute it under certain conditions;\n";
  print STDERR "See the GNU General Public License for details.\n";
  print STDERR "\n";

  return $::TRUE;
}

#
# display help info
#
sub show_help {
  print STDERR "Syntax:\n";
  print STDERR "\n";
  print STDERR "$::PROGNAME [options]\n";
  print STDERR "\n";
  print STDERR "-d {0-9}     Display debug information during program run\n";
  print STDERR "-b {base dn} (o=Microsoft,c=US)\n";
  print STDERR "-t {target}  Name or address of target ldap server\n";

  return $::TRUE;
}

#
# parse command-line arguments
#
sub parse_command_line {
  no strict 'vars';

  if ( getopts( 'd:b:t:' ) == $::FALSE ) {
    &show_help();
    return $::FAILED;
  }
  if ( defined $opt_d ) {
    if ( $opt_d > 0 ) {
      # set debug mode
      $::Config{'debug'} = $opt_d;
    }
  }
  if ( defined $opt_b ) {
    # set base dn
    $::Config{'base'} = $opt_b;
  }
  if ( defined $opt_t ) {
    # set ldap server to connect to
    $::Config{'target'} = $opt_t;
  }
  return $::TRUE;
}

#
# search the ldap
#
sub search_ldap {
  my ( $target, $base ) = @_;
  my $ldap;
  my $mesg;
  my $entry;
  my $uid;
  my $mail;
  my $fullname;
  my $dept;
  my $dept_num;
  my $status;
  my $domain;
  my $lname;
  my $fname;
  my $manager;
  my $lang;

  if ( $::Config{'debug'} >= 3 ) {
    print STDERR "DEBUG - Connecting to [$target]\n";
  }
  if ( ! defined ( $ldap = Net::LDAP->new( $target ) ) ) {
    print STDERR "ERROR - Unable to connect to [$target]\n";
    return $::FAILED;
  }
  # anonymous bind
  $ldap->bind( anonymous => 1 );
  $mesg = $ldap->search(
			base => $base,
			scope => 'one',
			filter => '(uid=*)',
			attrs => ['uid','mail','CN','ou','departmentNumber','organizationalStatus','ntUserDomainId','sn','givenName','SigAuth','LocationCode']
		       );

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

  # header (required)
  print "Status,LOGINID,FIRSTNAME,LASTNAME,EMAIL,DEPARTMENT,CUSTOMFIELDS\n";

  #
  # loop through all dumped data
  #
  foreach $entry ($mesg->all_entries) {
    # get the attributes that we are interested in
    $uid = $entry->get_value('uid');
    $mail = $entry->get_value('mail');
    $fullname = $entry->get_value('CN');
    $fullname =~ tr/,/ /;
    $status = $entry->get_value('organizationalStatus');
    $domain = $entry->get_value('ntUserDomainId');
    $lname = $entry->get_value('sn');
    $lname =~ tr/,/ /;
    $fname = $entry->get_value('givenName');
    $fname =~ tr/,/ /;
    $dept = $entry->get_value('ou');
    $dept_num = $entry->get_value('departmentNumber');
    if ( $entry->get_value('SigAuth') > 0 ) {
      $manager = "Yes";
    } else {
      $manager = "No";
    }
    if ( $entry->get_value('LocationCode' ) eq "PRJU" ) {
      $lang = "spanish";
    } else {
      $lang = "english";
    }
    if ( $domain eq "" ) {
      print STDERR "INFO - $uid has a blank NT domain\n";
      $domain = 'amgen';
    }
    if( $dept_num eq "" ) {
      $dept_num = "Unknown";
    }
    print "$status,$domain\\$uid,$fname,$lname,$mail,$dept_num,Language=$lang~Manager=$manager\n";
  }
  $mesg = $ldap->unbind;

  return $::TRUE;
}
