# STTA Rev. 1.3 Copyright (c) Jesús Pérez Lorenzo  --- license GNU GPL 
# Vers 1.3 Moving to perl-LDAP  NET:LDAP available in CPAN  giving up Mozilla::LDAP 
# LDAP Library  + PerLDAP stuff

$conn = "";
$conncache = "";
%ldapObjHash=();
@ldapObjArr=();

use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_name ldap_error_text);

sub dieLdap {
    if ( $debug gt 0 ) { $err_msg=$_; }
    &ldap_logerr($text{ldap_connerr}); 
    if (  "$ldap_server" eq "1" ) {  # try the second one 
          $ldap_server=2; 
          &connect();
    } else {
          $err_msg="Fatal ERROR no LDAP Server Available";
    }
}

sub connect
{
   if ( "$ldap_server" eq "1" ) {
         $line_conn=$config{ldap_server}.":".$config {ldap_port};
         $ldapObj = Net::LDAP->new($config{ldap_server},
                                   port => $config{ldap_port}) or dieLdap "$@";
         $conn = $ldapObj->bind("$config{ldap_user}",
                 password => "$config{ldap_passwd}",
                 version => $config{ldap_version} );    
         $config{LDAP_type}=$config{ldap_type};
   } elsif ( "$ldap_server" eq "2" ) {
         $line_conn=$config{ldap2_server}.":".$config {ldap2_port};
         $ldapObj = Net::LDAP->new($config{ldap2_server},
                                   port => $config{ldap2_port}) or dieLdap "$@";
         $conn = $ldapObj->bind("$config{ldap2_user}",
                 password => "$config{ldap2_passwd}",
                 version => $config{ldap2_version} );    
         $config{LDAP_type}=$config{ldap2_type};
   } else {
        print "LDAP: $ldap_server \n";
        &footHere; exit  
   }
   if ( $conn->code ) {
        $errstr = $conn->code;
        $err=$text{ldap_conerr}.&ldap_error_text($errstr);
        &ldap_logerr("connect: $errstr\n $err"); 
        &footHere; exit  
   }
}

sub dieLdapcache {
    if ( $debug gt 0 ) { $err_msg=$_; }
    &ldapcache_logerr($text{ldap_connerr}); 
    if (  "$ldapcache_server" eq "1" ) {  # try the second one 
          $ldapcache_server=2; 
          &connect();
    } else {
          $err_msg="Fatal ERROR no LDAP CACHE Server Available";
    }
}

sub connect_cache
{
   if ( "$ldapcache_server" eq "1" ) {
         $line_conn=$config{ldapcache_server}.":".$config {ldapcache_port};
         $ldapcacheObj = Net::LDAP->new($config{ldapcache_server},
                                   port => $config{ldapcache_port}) or dieLdapcache "$@";
         $conncache = $ldapcacheObj->bind("$config{ldapcache_user}",
                 password => "$config{ldapcache_passwd}",
                 version => $config{ldapcache_version} );    
         $config{LDAP_type}=$config{ldapcache_type};
   } elsif ( "$ldapcache_server" eq "2" ) {
         $line_conn=$config{ldapcache2_server}.":".$config {ldapcache2_port};
         $ldapcacheObj = Net::LDAP->new($config{ldapcache2_server},
                                   port => $config{ldapcache2_port}) or dieLdapcache "$@";
         $conncache = $ldapcacheObj->bind("$config{ldapcache2_user}",
                 password => "$config{ldapcache2_passwd}",
                 version => $config{ldapcache2_version} );    
         $config{LDAP_type}=$config{ldapcache2_type};
   } else {
        print "LDAP: $ldap_server \n";
        &footHere; exit  
   }
   if ( $conncache->code ) {
        $errstr = $conncache->code;
        $err=$text{ldap_conerr}.&ldap_error_text($errstr);
        &ldapcache_logerr("connect: $errstr\n $err"); 
        &footCacheHere; exit  
   }
}

sub get_cache_obj
{
    # This is to return a hash &cacheObj with all values of the $dn entry 
    my ( $dn, $cur_entry) = @_;
    local %cacheObj=();
    %ldapObjHash=();
    @ldapObjArr=();

    if ( !defined($cur_entry) || $cur_entry->dn eq $dn ) {     # just to be sure 
         local @attrs = $cur_entry->attributes;
         foreach my $var (@attrs) {
             $attr = $cur_entry->get_value( $var, asref => 1 );
             if ( defined($attr) ) {
                  foreach my $value (@$attr) { 
                         $cacheObj{$var} = $value; }
             }
         }
    }
    %ldapObjHash=%entryObj;
    &ldapcache_logerr("browse $dn");
    return %cacheObj;
}

sub sel_cache_obj
{
    my ($dn,$filter) = @_;
    %ldapObjHash=();
    @ldapObjArr=();
    $conncache = $ldapcacheObj->search ( base => $dn, 
                                         scope => "sub", 
                                         filter => "($filter)");
    if ( $err = $conncache->code ||  $conncache->count eq 0 ) { 
         &ldapcache_logerr("browse $dn");
         return undef
    };
    return $conncache->entry(0);
}

sub get_ldap_obj
{
    # This is to return a hash &Obj with all values of the $dn entry 
    my ( $dn, $cur_entry) = @_;
    local %Obj=();
    %ldapObjHash=();
    @ldapObjArr=();

    if ( !defined($cur_entry)  || $cur_entry->dn eq $dn ) {     # just to be sure 
         local @attrs = $cur_entry->attributes;
         foreach my $var (@attrs) {
             $attr = $cur_entry->get_value( $var, asref => 1 );
             if ( defined($attr) ) {
                  foreach my $value (@$attr) { 
                         $Obj{$var} = $value; }
             }
         }
    }
    %ldapObjHash=%entryObj;
    &ldap_logerr("browse $dn");
    return %Obj;
}

sub sel_ldap_obj
{
    my ($dn, $filter) = @_;
    %ldapObjHash=();
    @ldapObjArr=();
 
    $conn = $ldapObj->search ( base => $dn, 
                                         scope => "sub", 
                                         filter => "($filter)");
    if ( $err = $conn->code ||  $conn->count eq 0 ) {
         &ldap_logerr("browse $dn");
         return undef
    };
    return $conn->entry(0);
}

sub ldapcache_logerr
{
    my ($msgstr) = @_;
    my ($msg_str)="";
    my ($msg_err)="";
    my ($line_conn);
    $err_msg="";

  $msg_str="$text{task}=>'$msgstr'"; 
  if ( $search_cmd ) { $msg_str.="||$search_cmd"; };
  if ( "$ldapcache_server" eq "1" ) {
         $line_conn=$config{LDAP_type}.":".$config{ldapcache_server}.":".$config {ldapcache_port};
  } elsif ( "$ldapcache_server" eq "2" ) {
         $line_conn=$config{LDAP_type}.":".$config{ldapcache2_server}.":".$config {ldapcache2_port};
  }
  if ( $err ) {
       $msg_err.="error=>'".$text{'ldapcache_admerr'}."' $text{f_error}: '";
       if ( $conncache->code ) { 
         $msg_err.="$err'";
       } else {
         $msg_err.=$text{'ldapcache_connerr'}."'";
       }
       if ( $debug gt 0 ) {
            if ( $DebugFile ) {
                  print DebugFile "$line_conn\n";
                  print DebugFile "$msg_str\n";
                  print DebugFile "$msg_err\n";
            }
       }
       $msg_err=~s/\\n/\|\|/g; 
       $msg_str.="||$msg_err";
       $line_conn.=" err=$err";
       if ( $debug gt 0 ) { $err_msg=$line_conn; }
  }
  &webmin_log($line_conn,"ldapcache","$err||ldap=>'$line_conn'||$msg_str||dn=>'$dn_cache'",\%ldapObjHash);
}

sub ldap_logerr
{
    my ($msgstr) = @_;
    my ($msg_str)="";
    my ($msg_err)="";
    my ($line_conn);
    $err_msg="";

  $msg_str="$text{task}=>'$msgstr'"; 
  if ( $search_cmd ) { $msg_str.="||$search_cmd"; };
  if ( "$ldap_server" eq "1" ) {
         $line_conn=$config{LDAP_type}.":".$config{ldap_server}.":".$config {ldap_port};
  } elsif ( "$ldap_server" eq "2" ) {
         $line_conn=$config{LDAP_type}.":".$config{ldap2_server}.":".$config {ldap2_port};
  }
  $line_conn.=" $msgstr";
  if ( $err ) {
       $msg_err.="error=>'".$text{'ldap_admerr'}."' $text{f_error}: '";
       if ( $conn->code ) { 
         $msg_err.="$err'";
       } else {
         $msg_err.=$text{'ldap_connerr'}."'";
       }
       if ( $debug gt 0 ) {
            if ( $DebugFile ) { 
                  print DebugFile "$line_conn\n";
                  print DebugFile "$msg_str\n";
                  print DebugFile "$msg_err\n";
            }
       }
       $msg_err=~s/\\n/\|\|/g; 
       $msg_str.="||$msg_err";
       $line_conn.=" err=$err";
       if ( $debug gt 0 ) { $err_msg=$line_conn; }
  }
  &webmin_log($line_conn,"ldap","$err||ldap=>'$line_conn'||$msg_str||dn=>'$dn'",\%ldapObjHash);
}
1; # Return true
