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

# where is config file for parameters .... (same as Webmin)
$config_directory="ETCWEBMINDIR";
$debug=0; # if 0 it will try to get config file value
$stta_toolsdir="STTATOOLSDIR";

do "$stta_toolsdir/lib/global-lib.pl";
do "$stta_toolsdir/lib/base-lib.pl";
do "$stta_toolsdir/lib/lists-lib.pl";
require "$stta_toolsdir/lib/ttacache-lib.pl";

$tta_logfile=$ENV{'TTA_LOGFILE'};

$tool_name="scanttacache";
&read_file("$config_directory/stta/dataconf/scancache", \%config);
if ( $debug eq 0 ) { $debug=$config{cache_scandebug}; };
&initialize();
&start_debug();
 
$rule_limit=$config{cache_rulelimit};
$default_obj="cn=$config{ldapcache_defaultobj}";
$default_dnobj="$config{ldapcache_dndefaultobj}";
$check_default=$config{cache_chkdefaults};
$check_ttalogin=$config{cache_chkttalogin};
$check_maxsess=$config{check_maxsess};
$check_ticket=$config{check_ticket};
$has_cronsess=$config{has_cronsess};
$countsessout_file=$config{countsessout_file};
$msg_noticket=$config{msg_noticket};
$msg_nosess=$config{msg_nosess};

if ( $config{has_ldapusers} eq "true" ){ do "$stta_toolsdir/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'}//;
$UserName=~s/$config{'tta_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 
if ( $config{has_ldapusers} eq "true" ){
 $ldapcache_server=1;   
 &connect_cache ();
 if (! $conncache ) { exit } ;
  $TargetHost=$NameHost;
  if ( "$NameApp" ne "" ) { $NameApp.=","; }
  if ( "$NameAppGrp" ne "" ) { $NameAppGrp.=","; }
  if ( "$NameHost" ne "" ) { $NameHost.=","; }
  if ( "$NameHostGrp" ne "" ) { $NameHostGrp.=","; }
  if ( "$NameDomain" ne "" ) { $NameDomain.=","; }
  if ( "$OuName" ne "" ) { $OuName.=","; }
  if ( "$NameUser" ne "" ) { $NameUser.=","; }
 
 for ( $i=1 ; $i< $rule_limit+1; $i++ ) {
    $obj=eval("\$config{cacherule_$i}");
    if ( "$obj" eq "users" ) { $dn="ou=$NameUser$OuName$config{ldapcache_base}"; }
    if ( "$obj" eq "ous" ) { $dn="$OuName$config{ldapcache_base}"; }
    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;
    if ( "$obj" ne "users" && "$obj" ne "ous" ) { 
         # Trying  Username 
           $target="user";
           $dn_obj="cn=$NameUser$OuName$dn";
           if ( $debug eq 2 ) { print CacheDebugLog "($i) $obj -- $dn_obj \n" };
           $cur_entry=&sel_cache_obj($dn_obj,"cn=*");
           if ( defined($cur_entry) && $cur_entry->dn eq $dn_obj) { 
                %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                &outData ; last;
           };
         # Trying  default_obj for UserOU 
           $target="ou";
           if ( "$check_default" eq "true" ) {
                $dn_obj="$default_obj,$OuName$dn";
                if ( $debug eq 2 ) { print CacheDebugLog "($i) $obj -- $dn_obj \n" };
                $cur_entry=&sel_cache_obj($dn_obj,"cn=*");
                if ( defined($cur_entry) && $cur_entry->dn eq $dn_obj) { 
                     %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                     &outData ; last;
                };
           }
    }
    # Trying  $dn (OBJECT)  default_obj 
      $target="obj";
      if ( "$check_default" eq "true" ) {
           $dn_obj="$default_obj,$dn";
           if ( $debug eq 2 ) { print CacheDebugLog "($i) $obj -- $dn_obj \n" };
           $cur_entry=&sel_cache_obj($dn_obj,"cn=*");
           if ( defined($cur_entry) && $cur_entry->dn eq $dn_obj) { 
                %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                &outData ; last;
           };
      }
 }
}
# Last Chance ...
 if ( !defined($cur_entry) )  {
      if ( "$check_ttalogin" eq "true" ) {
           if ( $debug eq 2 ) { print CacheDebugLog "(ttalogin) -- $tta_username \n" };
                $use_ttalogin_values=true;
                &out_data_cmd();
      } elsif ( "$check_default" eq "true"  && $config{has_ldapusers} eq "true" ) {
           $dn_obj="$default_dnobj,$config{ldapcache_base}";
           if ( $debug eq 2 ) { print CacheDebugLog "(last) -- $dn_obj \n" };
           $cur_entry=&sel_cache_obj($dn_obj,"cn=*");
           if ( defined($cur_entry) && $cur_entry->dn ne $dn_obj) { 
                %entryObj=&get_cache_obj($dn_obj,$cur_entry);
                &outData ; 
           };
      }
}

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