http://www.perlmonks.org?node_id=168128
Category: Win32 Stuff
Author/Contact Info Zin Mostacho
Description: With a user of windows NT, determine data information the server LDAP.
#!/usr/local/bin/perl
######################################################################
+###########
# Lista                                                               
+          #
# version 2.1                                                         
+          #
######################################################################
+###########
# Objetivo     : Enlace de usuarios NT con LDAP para envio de mail    
+          #
# Comunicacion : LDAP                                                 
+          #
# Fuente       : Perl [ Win32 ]                                       
+          #
# Compilacion  : Win32. executable.                                   
+          #
# Fecha        : 16-Ago-2001 9:36                                     
+           #
######################################################################
+###########
#
# Formato de entrada de la lista :
#
#    UserNT
#
######################################################################
+###########

use Mozilla::LDAP::Conn;
use Mozilla::LDAP::Entry;
use Mozilla::LDAP::Utils;
use win32::lanman;

# inicializa variables
#
$host = "host.com";
$port="389";
$searchbase = "o=Test,c=AR";
$maxentries = "10000";      # entradas a recuperar 


@atfind = ( mail, uid, cn, departmentnumber, employeenumber, telephone
+number );
print "\nVersion 1.0 <-> 16-08-2001\n";

#
# EVALUA PARAMETROS
#
if ( eval(@ARGV) < 2 ) {
   PrintHelp();
   exit 1;
}

if ( (uc($ARGV[0]) eq "U") || (uc($ARGV[0]) eq "-U")) {
    $OPTION = 1;

}
elsif ((uc($ARGV[0]) eq "L") || (uc($ARGV[0]) eq "-L")) {
    $OPTION = 2;


}
else {
    PrintHelp();
    exit 1;
}

open FILESI, ">OK.txt";
open FILENO, ">NOK.txt";
open DEBUG, ">DEBUG.txt";    
print FILESI "UidNT,*,Mail,UidNs, ApeyNom, CC, leg, te \n";

# hace la conexion 
$conn = new Mozilla::LDAP::Conn( $host, $port );
die "No se puede conectar a LDAP \n" unless $conn;


# Lectura del TXT 
#

if ($OPTION eq 2) {
    @datos = `type $ARGV[1]`;
}elsif ($OPTION eq 1) {
    if ($ARGV[1] eq "") {
        PrintHelp();
            exit 2;
    }else{    
        @datos=$ARGV[1];
     
    }
}
$server=$ARGV[2];
chomp($server);

# Proceso de busqueda
#
$i=0;
foreach $usuarioNT (@datos) {
    chomp($usuarioNT);
    $i=$i+1;
    ($linea)=&searchNT($usuarioNT,$server);     
    ($campo1, $campo2, $campo3, $campo4) = split(/,/, $linea);
    print "Procesando $i.- ($campo1, $campo2, $campo3, $campo4)\n";
    if ($campo1 =~ /[0-9]/) {
    $filter="(employeenumber=$campo1)";
    print "\nProcesando . \r";
    ($ok)= &salida($searchbase, $filter, @atfind);
    print DEBUG "1,$ok,$filter\n";
    if ($ok==0){
        $campo=$campo1;
        $campo =~ tr /A//;
        $campo =~ s/A//eg;
        $filter="(employeenumber=$campo)";
        print "Procesando .. \r";
        ($ok)= &salida($searchbase, $filter, @atfind);
        print DEBUG "2,$ok,$filter\n";
        if ($ok==0){
            $campo=$campo1;
            $campo =~ tr /B//;
            $campo =~ s/B//eg;
            $filter="(employeenumber=$campo)";
            print "Procesando ... \r";
            ($ok)= &salida($searchbase, $filter, @atfind);
            print DEBUG "3,$ok,$filter\n";
                if ($ok==0){
                    $campo=$campo3." ".$campo2; 
                    $filter="(cn=$campo)";
                    print "Procesando .... \r";
                    ($ok)= &salida($searchbase, $filter, @atfind);
                    print DEBUG "4,$ok,$filter\n";
                    if ($ok==0){
                        $campo=$campo3;
                        $campo=~ tr / //;
                        $campo=~ s/ //eg;
                        $campos= substr($campo, 0, 1);
                        $campo=$campos.$campo2;
                        $filter="uid=$campo";
                        print "Procesando ..... \r";
                        ($ok)= &salida($searchbase,$filter,@atfind);
                        print DEBUG "5,$ok,$filter\n";
                        if ($ok==0){
                            print FILENO ("$campo1,$campo2,$campo3 \n"
+);
                            
                        }    
                    }
                }
        }
    }
    }else{     
        $filter="uid=$campo1";
        ($ok)= &salida($searchbase, $filter, @atfind);
    if ($ok==0){
        $campo=$campo3." ".$campo2; 
        $filter="(cn=$campo)";
        print "Procesando ...... \r";
        ($ok)= &salida($searchbase, $filter, @atfind);
        print DEBUG "6,$ok,$filter\n";
        if ($ok==0){
            $campo=$campo3;
            $campo=~ tr / //;
            $campo=~ s/ //eg;
            $campos= substr($campo, 0, 1);
            $campo=$campos.$campo2;
            $filter="uid=$campo";
            print "Procesando ....... \r";
            ($ok)= &salida($searchbase,$filter,@atfind);
            print DEBUG "7,$ok,$filter\n";
            if ($ok==0){
                print FILENO ("$campo1,$campo2,$campo3 \n");
            }
        }
    }
    }     
    
# Proceso de salida
#
sub salida {
    ($searchbase,$filter,@atfind) = @_;     
    $entry = $conn->search( $searchbase, "sub", $filter, 0, @atfind );
+     
    if ($entry) {
    # si hay una entrada
         $cn = $entry->{cn}[0];
         $uid = $entry->{uid}[0];
         $cc = $entry->{departmentnumber}[0];
     $leg = $entry->{employeenumber}[0];    
     $te = $entry->{telephonenumber}[0];
     $mail = $entry->{mail}[0]; 
     print FILESI "$campo1,*,$mail,$uid,$cn,$cc,$leg,$te\n";
     print "Procesado          \r";
     if ($OPTION eq 1) {    
             print "\nmail: $mail \nuid: $uid \nN&A: $cn \nCC: $cc \nL
+eg: $leg \nTE: $te \n";
     }
     $ok =1;    
    }else {
    # no encuentro nada
     $ok =0;
    }    
  }
}
print "\nFin de la busqueda ... \n";
print "\nBuscar datos en ok.txt y en nok.txt, y ver errores en debug.t
+xt ... \n";
# Cierra conexion
#
$conn->close if $conn;
close(archivos);
close(archivon);
close(debug);
exit;

################
#
#
#
################
sub PrintHelp {

    print "Uso: nt2ldap.pl -l <Lista> <PDC-NT> / -u <userNT> <PDC-NT>\
+n\n";
    print "Formato de la lista entrada: userNT\n\n";
    print "Formato de salida : \n";
    print "Archivo OK.txt : UidNT,*,Mail, UidLdap, ApeyNom, CC, leg, t
+e \n";
    print "Formato de salida :\n";
    print "Archivo NOK.txt : idem del de entrada \n";

}

################
#
#
#
################
sub searchNT {
    ($user,$server)=@_;
    $server="\\\\".$server;
    if(!Win32::Lanman::NetUserGetInfo($server,$user,\%info)){
                # get the error code
                $error=Win32::Lanman::GetLastError();
        print "Algun error a ocurrido; error: $error ";
                $datosNT="$user,,,";
        }else{
                $fn = $info{full_name};
            ($campo1, $campo2) = split(/,/, $fn);
            $fn="$campo1,$campo2";
            $lg = $info{comment};
            ($trash, $campo2) = split(/\(/, $lg);
            ($lg, $trash) = split(/\)/, $campo2);
            $datosNT = "$user,$fn,$lg";
    }
           return($datosNT);
}