#!/usr/bin/perl
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 
# Fix TTA host ENV from list defs
# Checking maxsess and tickets 

# where is config file for parameters .... (same as Webmin)
$config_directory="/etc/webmin";
$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 './lib/global-lib.pl';
do './lib/base-lib.pl';
do './lib/lists-lib.pl';
require './lib/ttacache-lib.pl';

$tta_logfile=$ENV{'TTA_LOGFILE'};

&initialize();
&load_dataconf("scancache");
$DebugFile=$config{cache_debuglog};
if ( $debug eq 0 ) { $debug=$config{cache_scandebug}; };
if ( $debug gt 0 ) {
     @linlogfile = split(/\//, $tta_logfile);
     $logfile_ext=$linlogfile[$#linlogfile];
     $logfile_ext=~s/.log//g;
     $logfile_ext=~s/script//g;
     $DebugFile=$config{cache_debuglog}.$logfile_ext.".log";
}

&start_debug();
$tool_name="scanttacache";
 
$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};
$countsessout_file=$config{countsessout_file};


do "./lib/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 
 $ldapcache_server=1;   
 &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" ) { $dn="$NameHost,$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" };
      $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
      if ( defined($cur_entry) && $cur_entry->dn ne $dn_obj) { 
           %entryObj=&get_cache_obj($dn_obj,$cur_entry);
           &outData ; last;
      };
    # 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" };
           $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
           if ( defined($cur_entry) && $cur_entry->dn ne $dn_obj) { 
                %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                &outData ; last;
           };
      }
    # 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" };
           $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
           if ( defined($cur_entry) && $cur_entry->dn ne $dn_obj) { 
                %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                &outData ; last;
           };
      }
 }
# Last Chance ...
 if ( !defined($cur_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" };
           $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
           if ( defined($cur_entry) && $cur_entry->dn ne $dn_obj) { 
                %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                &outData ; last;
           };
      }
}

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