#!/usr/bin/perl
# STTA Rev. 1.2
# Fix TTA host ENV from list defs
# Checking maxsess and tickets 

# where is config file for parameters .... (same as Webmin)
$config_directory="/etc/webmin/tarantella";
$debug=0; # if 0 it will try to get config file value

$msg_noticket="Ticket not Valid !!!";
$msg_nosess="Not Sessions Availables" !!!;

$msg_noticket="ˇˇˇ Ticket no Válido !!!!";
$msg_nosess="ˇˇˇ No hay Sesiones Disponibles !!!";

do './base-lib.pl';
require './ttacache-lib.pl';
do './lists-lib.pl';

$tta_logfile=$ENV{'TTA_LOGFILE'};

&initialize();
 
$rule_limit=$config{cache_rulelimit};
$default_obj="cn=$config{ldapcache_defaultobj}";
$default_dnobj="$config{ldapcache_dndefaultobj}";
$chek_default=$config{cache_chkdefaults};
$chek_ttalogin=$config{cache_chkttalogin};
$chek_maxsess=$config{check_maxsess};
$chek_ticket=$config{check_ticket};
$has_cronsess=$config{has_cronsess};
$cron_sessout_file=$config{cron_sessout_file};


do "./ldap-lib.pl";

$UserName=$ENV{'TTA_UserName'};
$HostName=$ENV{'TTA_HostName'};
$AppName=$ENV{'TTA_ApplicationName'};
$NameDomain=$ENV{'TTA_DomainName'};
if ( "$NameDomain" eq "" ) { $NameDomain=$config{cache_domain} };
$tta_username=$ENV{'tta_username'};
$tta_password=$ENV{'tta_password'};

$WinAppServer=$ENV{'TTA_WindowsApplicationServer'};
$ConnectionService=$ENV{'TTA_CONNECTIONSERVICE'};
$tta_host=$ENV{'tta_host'};

$ens_base=$config{'ens_base'}.$config{'tta_base'};
$UserName=~s/$config{'tta_ldapUser'}//;
$UserName=~s/,$config{'ldap_base'}//;
$UserName=~s/$config{'ens_base'}//;
$AppName=~s/$ens_base\///;
$AppName=~s/$config{'ens_base'}//;
$HostName=~s/$ens_base\///;

$userType="LDAP";  
$NameUser="";
$OuName="";
$NameApp="";
$NameAppGrp="";
$NameHost=$HostName;
$use_ttalogin_values=false;   
$numKeyLimit=6;

$nowTime=`date +%H%M%S`;
$nowDate=`date +%d%m%y`;

&scan_env();

if ( $debug gt 0 ) { &print_results(); };

 &out_empty_data;  # just in case 
 &connect_cache ();
 if (! $conncache ) { exit } ;
 
 for ( $i=1 ; $i< $rule_limit+1; $i++ ) {
    $obj=eval("\$config{cacherule_$i}");
    if ( "$obj" eq "apps" ) { $dn="ou=$NameApp,$NameAppGrp,$config{ldapcache_base}"; }
    if ( "$obj" eq "grpapps" ) { $dn="$NameAppGrp,$config{ldapcache_base}"; }
    if ( "$obj" eq "hosts" ) { 
         if ("$NameAppGrp" eq "$NameHostGrp" ) {
             $dn="$NameHostGrp,$config{ldapcache_base}";
         } else {
             $dn="ou=$NameHost,$NameHostGrp,$config{ldapcache_base}";
         }
    }
    if ( "$obj" eq "grphosts" ) { $dn="$NameHostGrp,$config{ldapcache_base}"; }
    if ( "$obj" eq "domains" ) { $dn="ou=$NameDomain,$config{ldapcache_base}"; }
    $dn=~s/cn=/ou=/g;
    # Trying  Username 
      $target="user";
      $dn_obj="cn=$NameUser,$OuName,$dn";
      if ( $debug eq 2 ) { print DebugLog "($i) $obj -- $dn_obj \n" };
      $entry = $conncache->browse ($dn_obj);
      if ( $entry )  { &outData ; $i=$rule_limit+1}
    # Trying  default_obj for UserOU 
      $target="ou";
      if ( "$chek_default" eq "true" ) {
           $dn_obj="$default_obj,$OuName,$dn";
           if ( $debug eq 2 ) { print DebugLog "($i) $obj -- $dn_obj \n" };
           $entry = $conncache->browse ($dn_obj);
           if ( $entry )  { &outData ; $i=$rule_limit+1}
      }
    # Trying  $dn (OBJECT)  default_obj 
      $target="obj";
      if ( "$chek_default" eq "true" ) {
           $dn_obj="$default_obj,$dn";
           if ( $debug eq 2 ) { print DebugLog "($i) $obj -- $dn_obj \n" };
           $entry = $conncache->browse ($dn_obj);
           if ( $entry )  { &outData ; $i=$rule_limit+1}
      }
 }
# Last Chance ...
 if ( !$entry )  {
      if ( "$chek_ttalogin" eq "true" ) {
           if ( $debug eq 2 ) { print DebugLog "(ttalogin) -- $tta_username \n" };
                $use_ttalogin_values=true;
                if ( $debug gt 0 ) { &print_data(); };
      } elsif ( "$chek_default" eq "true" ) {
           $dn_obj="$default_dnobj,$config{ldapcache_base}";
           if ( $debug eq 2 ) { print DebugLog "(last) -- $dn_obj \n" };
           $entry = $conncache->browse ($dn_obj);
           if ( $entry )  { &outData;}
      }
}

if ( $debug gt 0 ) { &endclose_debug(); }
exit;
