# Global Lib ...
# 
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

# Read Config File -
# since 1.3 serveral files at /etc/webmin/stta/dataconf/*.cfg 
# this is more easy to manage and to split vars for each theme
# ... it needs more cpu as it has to open several files ...
sub load_dataconf() {
   local %listfcfgs=();
   local @orderfcfgs=();
   local $d="";
   local ($listfcfgs) = $_[0] || "ordercfgs";

   if (! (-d "$config_directory/$module_name/dataconf" )) { $config{tta_dir}="" };
   &read_file("$config_directory/$module_name/dataconf/$listfcfgs", \%listfcfgs, \@orderfcfgs);
   foreach $d (@orderfcfgs) {
         &read_file("$config_directory/$module_name/dataconf/$d", \%config);
   }
}

sub load_minfo
{
   local $pathModInfo = $_[0] ||  '';
   $pathModInfo .= "module.info";

   local %minfo;
   &read_file( "$pathModInfo",\%minfo) ;
   return %minfo;
}

sub set_datetime_str
{
    local ($wkstr) = $_[0];
    local ($outStr) = "";
    local ($fday, $fmon, $fyea, $fhou, $fmin, $fsec, $fwday);

    # format should be: `date "+%d-%m-%Y %H:%M:%S %w"`;

    $fday=substr($wkstr,0,2); 
    $fmon=substr($wkstr,3,2); 
    $fyea=substr($wkstr,6,4); 
    $fhou=substr($wkstr,11,2); 
    $fmin=substr($wkstr,14,2); 
    $fsec=substr($wkstr,17,2); 
    $fwday=substr($wkstr,20,1); 

    # format for order criteria

    $outStr=$fyea.$fmon.$fday.$fhou.$fmin.$fsec.$wday ;
  return $outStr;
}

sub get_datetime_str
{
    local ($wkstr) = $_[0];
    local ($outStr) = "";
    local ($fday, $fmon, $fyea, $fhou, $fmin, $fsec, $fwday);

    # format should be: `date "+%d-%m-%Y %H:%M:%S $w"`;

    $fday=substr($wkstr,6,2); 
    $fmon=substr($wkstr,4,2); 
    $fyea=substr($wkstr,0,4); 
    $fhou=substr($wkstr,8,2); 
    $fmin=substr($wkstr,10,2); 
    $fsec=substr($wkstr,12,2); 
    $fsec=substr($wkstr,14,1); 

    # format for order criteria

    $outStr=$fday."-".$fmon."-".$fyea." ".$fhou.":".$fmin.":".$fsec." ".$fwday ;

  return $outStr;
}
sub set_now
{
    local ($now);
    if ( !$_[0] ) { 
         $now = time();
    } else {
         $now=$_[0];
    }     
    local @tm = localtime($now);
    local ($t_day, $t_mon, $t_yea, $t_hou, $t_min, $t_sec, $t_wday);
    
     # format should be: `date "+%d-%m-%Y %H:%M:%S $w"`;
     $t_sec=$tm[0];
     if ($t_sec < 10 ) { $t_sec="0".$t_sec;}
     $t_min=$tm[1];
     if ($t_min < 10 ) { $t_min="0".$t_min;}
     $t_hou=$tm[2];
     if ($t_hou < 10 ) { $t_hou="0".$t_hou;}
     $t_day=$tm[3];
     if ($t_day < 10 ) { $t_day="0".$t_day;}
     $t_mon=($tm[4]+1);
     if ($t_mon < 10 ) { $t_mon="0".$t_mon;}
     $t_yea=($tm[5]+1900);
     $t_wday=($tm[6]);
     $t_yead=$tm[7];
     if ($t_yead < 100 ) { $t_yead="0".$t_yead;}
     if ($t_yead < 10 ) { $t_yead="0".$t_yead;}
     $t_isdst=$tm[8];
        

    return "$t_day-$t_mon-$t_yea $t_hou:$t_min:$t_sec $t_wday $t_yead";

}

# cipher( text, length(text), key, length(key),result)
sub cipher
{
	local $i,$pos;
        local @buffer;
        local @string_arr;

        &seed_random();
	for ( $pos = 0; $pos < $_[3]; $pos++ ) {
                $i=int(rand($_[3]));
                $string_arr[$pos]=${$_[2]}[$i];
	}
	if (($_[1] + 2) >= $_[3]) {
		$pos = 1;
	} else {
		$pos = int(rand($_[3] - ($_[1] + 2)));
	}
        if ( $pos < 1 ) { $pos=1; }
	$string_arr[$pos - 1] = '\\';
        for ( $i=0 ; $i < ($_[1]+1) ; $i++ ) {
        	$string_arr[$pos+$i]=${$_[0]}[$i];
        }
	$string_arr[$pos + $_[1]+1] = '\\';

	for ( $i = 0; $i < $_[3]; $i++) {
		$buffer[$i]= $string_arr[$i] ^ ${$_[2]}[$i];
                ${$_[4]}.=$buffer[$i];
        }
       return; 
}

# decipher( text, key, length(key),result)
sub decipher
{
	local $i,$tobuff;
        local @buffer;

	for ( $i = 0; $i < $_[2]; $i++) {
		$buffer[$i] = ${$_[0]}[$i] ^ ${$_[1]}[$i];
        }

        $tobuff="0";
	for ( $i = 0; $i < $_[2]; $i++)  {
		if ($buffer[$i] eq '\\' )  {
                    if ( $tobuff eq "1" ) { 
                         $tobuff="0";
                    } elsif ( $tobuff eq "0" ) {
                         $tobuff="1";
                         next;
                    }
		}
                if ( $tobuff eq "1" ) { ${$_[3]}.=$buffer[$i]; }
        }
	return;
}

# cipher_text ( $task (code|decode), $text, &vartoreturn)
sub cipher_text
{
    local $longclave=12;
    local @clave=("0","1","2","3","4","5","6","7","0","1","2","3","4","5","6","7","0","1","2","3","4");
    local @texto =();
    local $lon=length($_[1]); 
    local $i;

    if ( $_[0] eq "code" ) {
         for ( $i = 0; $i < $lon;$i++) { $texto[$i]=substr($_[1],$i,1); }
	 &cipher(\@texto, $#texto, \@clave, $longclave,\${$_[2]});
    } elsif ( $_[0] eq "decode" ) { 
        $lon=length($_[1]); 
	for ( $i = 0; $i < $lon;$i++) { $texto[$i]=substr($_[1],$i,1); }
	&decipher(\@texto, \@clave, $lon,\${$_[2]});
    }
    if ( $debug eq "4" ) { &webmin_log("cipher_text",$_[0], "$_[1] -- ${$_[2]}"); }
}


# show_hash( %has-to-scan,$father-brach-if-hash-tree, $separator, $dev-to-output,&vartoout
sub show_hash
{
 local ($item,$v,$r);
  if ( $_[3] ) { select($_[3]);}
  return if (!%{$_[0]});
  local %h_lst=%{$_[0]};
  local @data;
  local $lnum=0;
  local $lchar=62;
  local $sep;

  if ( "$_[1]" eq "" && !$_[4] ) {
           print "___________________________________________________________\n"; 
  }
  foreach $item (keys (%h_lst)) {
#         if ( ! $h_lst{$item} ) { next ;}
         local $v=$h_lst{$item};
         $r=ref($v);
         if ( $r eq 'HASH') { 
               if ( $_[4] ) {
#                  ${$_[4]}.=$item."\n";
                   &show_hash(\%{$h_lst{$item}},$item,$_[2],$_[3],\${$_[4]});
               } else {
#                  print "$item\n";
                   &show_hash(\%{$h_lst{$item}},$item,$_[2],$_[3]);
               }
        } elsif ( $r eq 'ARRAY') {
               local @data=@{$v};
               for($i=1; $i <= $#data; $i++) {
                     if ( $_[4] ) {
                        ${$_[4]}.=$data[$i]."\n";
                     } else {
                        print $data[$i]."\n";
                     }
               }
        } else {
              if ( $_[2] ) { 
                     if ( $_[4] ) {
                          ${$_[4]}.="$item$_[2]=$_[2]$v\n" ;
                     } else {
                        print $item.$_[2]."=".$_[2].$h_lst{$item}."\n" ;
                     }
               } else  { 
                     if ( $_[4] ) {
                        ${$_[4]}.=$item."=".$h_lst{$item}."\n" ;
                     } else {
                        print $item."=".$h_lst{$item}."\n" ;
                     }
               }
         }
         $lnum++;
  }
  if ( "$_[1]" ne "" ) {
     $item="_($lnum)_$_[1]________________________________________________________";
     if ( $_[4] ) {
          ${$_[4]}.="_($lnum)__$_[1]_____\n";
     } else {
          print substr($item,0,$lchar)."\n";
     }
  }
  if ( $_[3] ) { $| =1; select(STDOUT);}
#  if ( $_[4] ) { print "4** $_[4]";}
  
}

# lookup in hash %{$_[0]} for key $_[1]  and return value
sub search_in_hash
{
  local ($k,$v,$,);
  return if (!%{$_[0]});
  local %h_lst=%{$_[0]};
  local $lnum=1;
  local $key_value;

  foreach $item (keys (%h_lst)) {
         local $v=$h_lst{$item};
         $r=ref($v);
         if ( $r eq 'HASH') { 
               local %h_cp=%{$v};          
              $key_value=&search_in_hash(\%h_cp,$_[1]);
              if ( $key_value.length gt 0 ) { return $key_value; }
        } else { 
              if ($item eq $_[1] ) { return $h_lst{$item}; }
        }
        $lnum++;
  }
}

# To show each item from Rec in a list (hash-list: key + hash ) 
sub showrec_list
{
  &show_hash(\%{$_[0]},"","\t");
}

# check_socket(host, port, handle, [&error])
sub check_socket
{
local($addr, $h); $h = $_[2];
        if (!socket($h, PF_INET, SOCK_STREAM, getprotobyname("tcp"))) {
	     if ($_[3]) { ${$_[3]} = &text(sckerr_no, $!); return false; }
	     else { &error("&text(sckerr_no, $!)"); }
	}
        if (!($addr = inet_aton($_[0]))) {
	     if ($_[3]) { ${$_[3]} = &text(sckerr_noip, $_[0]); return false; }
	     else { &error("&text(sckerr_noip, $_[0])"); }
	}
        if (!connect($h, sockaddr_in($_[1], $addr))) {
	     if ($_[3]) { ${$_[3]} = &text(sckerr_noconn, $_[0],$_[1],$!); return false; }
	     else { &error("&text(sckerr_noconn, $_[0],$_[1],$!)"); }
	}
        # select($h); $| =1; select(STDOUT);
        shutdown($h,2);
        if ($_[3]) { ${$_[3]} = &text('responding_on',$_[1]); }  
        return true;
}

# Loading Hash with file and properties %ttaprops (with @data in)  and @ttahosts
# get_ttaprops(has_toset,$dir_path,$extension,$names_toexclude) 
sub get_ttaprops 
{
     local @list_names=glob($_[0]."*".$_[1]);
     local $list_exclude=$_[2];
     local $nhost=0;
     local $target=0;
     local $k,$kname,$v;
     local $ttaproperties=$text{tta_properties};

     foreach $f_name (@list_names) {
         $FileName=$f_name;
         $FileName=~s/$dir_path//g;  
         if ( $FileName )  { $FileName=~s/$_[1]//g; }
         if ( $list_exclude =~ /$FileName/ ) { next; }
         local %configtta=();
         &read_file($f_name, \%configtta);
         if ( $ttaproperties =~ /$FileName/ ) {
                      $target="array";
         } else {
                  $target="host";
                  $ttahosts[$nhost] = { 'name' => $FileName  };
         }
         $ttaprops{$FileName} = { 
                           'name' => $FileName,
                           'target' => $target,
                           'path' => $f_name,
                           'config' => \%configtta
                          };
        if ( $target eq "host" ) { $nhost++; }
    }
}


sub load_ttaprops
{
     local $dir_path=$config{tta_dir}.$text{tta_global_dir};
     if ( $_[0] ) { $dir_path=$_[0] };
     local @data;
     local %configtta;
     local %h_cp;
     local ($array_port, $array_extport, $array_secureport, $cdm_enabled);
     local ($cdm_extlnbport, $billingservice, $searchldapLA, $ldap_url, $host_master);
     local $lnum=0,$masthost;
     local ($h_secure, $h_dns_ext, $h_dns_peer, $h_acceptconn,$item);

     &get_ttaprops($dir_path,$text{tta_ext_cfgfiles},"");

    $array_port	=
      &search_in_hash(\%{$ttaprops{bootstrap}{config}},"tarantella.config.array.port");
    $array_extport=
      &search_in_hash(\%{$ttaprops{bootstrap}{config}},"tarantella.config.array.externalport");
    $array_secureport=
      &search_in_hash(\%{$ttaprops{bootstrap}{config}},"tarantella.config.array.secureport");
    $cdm_enabled=
      &search_in_hash(\%{$ttaprops{cdm}{config}},"tarantella.config.cdm.enabled");
    $cdm_extlnbport=
      &search_in_hash(\%{$ttaprops{cdm}{config}},"tarantella.config.cdm.externalnbtport");
    $billingservice=
      &search_in_hash(\%{$ttaprops{components}{config}},"tarantella.config.components.billingservice");
    $searchldapLA=
      &search_in_hash(\%{$ttaprops{components}{config}},"tarantella.config.components.searchldaploginauthority");
    $ldap_url=
      &search_in_hash(\%{$ttaprops{ldap}{config}},"tarantella.config.ldap.url");
    $host_master=
    &search_in_hash(\%ttaprops,"tarantella.config.host.master");

    $dir_path=$config{tta_dir}.$text{tta_local_dir}.$text{tta_bootstrap};
    &read_file($dir_path,\%configtta);

    $lnum=0;
    @data=@ttahosts;
    foreach $item (@data) {
            %h_cp=%{$ttaprops{$item->{name}}};
            $h_secure= &search_in_hash(\%h_cp,"tarantella.config.host.security.active");
            $h_dns_ext= &search_in_hash(\%h_cp,"tarantella.config.host.dns.external");
            $h_dns_peer= &search_in_hash(\%h_cp,"tarantella.config.host.dns.peer");
            $h_acceptconn= &search_in_hash(\%h_cp,"tarantella.config.host.acceptnewconnections");
            $ttahosts[$lnum]={'name' => $item->{name},
                              'secure' => $h_secure,
                              'dns_ext' => $h_dns_ext,
                              'dns_peer' => $h_dns_peer,
                              'acceptconn' => $h_acceptconn,
  			      'ttaarray_status' => '',
  			      'ttastd_status' => '',
  			      'ttassl_status' => '',
  			      'ttacdm_status' => '',
  			      'http_status' => '',
  			      'https_status' => '',
                              'ipalive' => "" };
            if ( "$item->{name}" eq "$configtta{'tarantella.config.server.dns.peer'}" ){
                 $masthost=$lnum;
            }
            $lnum++;
    }
 
    $sttaparams{'tta'}= { 'ttalocal_dns_ext' => 
                                       $configtta{'tarantella.config.server.dns.external'},
                          'ttalocal_dns_peer' => 
                                       $configtta{'tarantella.config.server.dns.peer'},
                          'nhost'	 => $lnum,
                          'masthost'	 => $masthost,
                          'array_port'	 => $array_port,
                          'array_extport' => $array_extport,
                          'array_secureport' => $array_secureport,
                          'cdm_enabled' => $cdm_enabled,
                          'cdm_extlnbport' => $cdm_extlnbport,
                          'billingservice' => $billingservice,
                          'searchldapLA' => $searchldapLA,
                          'ldap_url' => $ldap_url,
                          'host_master' => $host_master };
    $sttaparams{'ldap'}= { 'ldap_status' => "",
  			  'ldap2_status' => "",
  			  'ldapcache_status' => "",
  			  'ldapcache2_status' => "" };
}

# Args can be "local"  (localhost and array services) or "remote" ( no local services)
# any other even nothing will be the whole array services !
sub scanSTTA_services
{
  local $title="";
  local $host,$port,$masthost;
  local $rv = { 'fh' => time().$$ };
  local %h_prop;
  local @data;
  local $is_ttamaster=false;
  local %h_hosts=&list_hosts(5);   

  local $thishost_name;
  $masthost=$sttaparams{'tta'}{'masthost'};

        # to look-up for master ...
        #  if ( $sttaparams{tta}{host_master} eq $sttaparams{tta}{ttalocal_dns_peer}){
        # Can be equal in Primary Array Server
        # if ( $sttaparams{'tta'}{'host_master'} eq $ttahosts[$masthost]->{'dns_peer'}){
 $lnum=0;
 foreach $item (@ttahosts) {
         $thishost_name="";
         %h_prop=%{$ttaprops{$item->{name}}};
         if ( $sttaparams{'tta'}{'host_master'} eq $item->{'dns_peer'} ) {
              $is_ttamaster=true;
              $thishost_name="TTA MASTER->";
         }
         next if  ( $_[0] eq "local" && 
              $item->{'dns_peer'} ne $sttaparams{tta}{ttalocal_dns_peer} &&
              $is_ttamaster ne true  );
         next if  ( $_[0] eq "remote" && 
              $item->{'dns_peer'} eq $sttaparams{tta}{ttalocal_dns_peer} );
       
       # Is TTA Alive on this host ??
         $host=$item->{'dns_peer'};
         $thishost_name.=$item->{'name'};

         if ( $item->{'name'} ne $host ) { $thishost_name.=$item->{'name'}." ($host)"; }

       # Try Array 5427
         $title="$text{srvTTA_Array} $thishost_name:";
         $port=$sttaparams{tta}{array_port};
         if ( $port) { 
              &check_socket($host, $port, $rv->{'fh'}, \$item->{'ttaarray_status'});
              if ( $debug eq 4 ) {
                   &additional_log('tta array socket', "socket", "$title $port");
                   &webmin_log("tta array socket","socket",$item->{'ttaarray_status'});
              }
         }
       # Try STD   
         $title="$text{srvTTA_STD} $thishost_name:";
         $port=$sttaparams{tta}{array_extport};
         if ( $port && $item->{acceptconn} eq "enabled" ) { 
              &check_socket($host, $port, $rv->{'fh'}, \$item->{'ttastd_status'});
              if ( $debug eq 4 ) {
                   &additional_log('ttaSTD socket', "socket", "$title $port");
                   &webmin_log("ttaSTD socket","socket",$item->{'ttastd_status'});
              }
         }; 
       # Try SSL
         $title="$text{srvTTA_SSL} $thishost_name:";
         $port=$sttaparams{tta}{array_secureport};
         if ( $port && $ttahosts[$masthost]->{acceptconn} eq "enabled" &&
              $item->{secure}  =~ /ssl/ ) { 
              &check_socket($host, $port, $rv->{'fh'}, \$item->{'ttassl_status'});
              if ( $debug eq 4 ) {
                   &additional_log('ttaSSL socket', "socket", "$title $port");
                   &webmin_log("ttaSSL socket","socket",$item->{'ttassl_status'});
              }
         };
       # Test CDM
         if ( $sttaparams{tta}{cdm_enabled} eq "1"  && 
             $item->{acceptconn} eq "enabled" ) {
             $title="$text{srvTTA_CDM} $thishost_name:";
             $port=$sttaparams{tta}{cdm_extlnbport};
             &check_socket($host, $port, $rv->{'fh'}, \$item->{'ttacdm_status'});
             if ( $debug eq 4 ) {
                  &additional_log('ttaCDM socket', "socket", "$title $port");
                  &webmin_log("ttaCDM socket","socket",$item->{'ttacdm_status'});
             }
         };
       # Assuming same ports in all the array ....
       # IS HTTP
         $port=$config{http_port};
         if ( $port ) {
             $title="$text{srvHTTP} $thishost_name:";
             &check_socket($host, $port, $rv->{'fh'}, \$item->{'http_status'}); 
             if ( $debug eq 4 ) {
                  &additional_log('http socket', "socket", "$title $port");
                  &webmin_log("http socket","socket",$item->{'http_status'});
             }
         };
       # IS HTTPS
         $port=$config{https_port};
#         if ( $port && $ttahosts[$masthost]->{secure} eq "1" ) { 
         if ( $port ) { 
             $title="$text{srvHTTPS} $thishost_name:";
             &check_socket($host, $port, $rv->{'fh'}, \$item->{'https_status'});
             if ( $debug eq 4 ) {
                  &additional_log('https socket', "socket", "$title $port");
                  &webmin_log("https socket","socket",$item->{'https_status'});
             }
         };
         # Alive Port
         $port="";
         if  ( $h_hosts{$host}->{'ipname'} eq $host ) {
            $port=$h_hosts{$host}->{'portalive'};
            $thishost_name=$h_hosts{$host}->{'name'};
            $host=$h_hosts{$host}->{'ipname'};
         }
         $title="$text{f_portalive} $thishost_name:";
         if ( $port ) {
             &check_socket($host, $port, $rv->{'fh'}, \$item->{'ipalive'});
             if ( $debug eq 4 ) {
                  &additional_log('ipalive socket', "socket", "$title $port");
                  &webmin_log("ipalive socket","socket",$item->{'ipalive'});
             }
         }
         $lnum++;
 }
   # IS LDAP up
         $host=$config{ldap_server};
         if ( $host ) {
              $port=$config {ldap_port};
              $thishost_name="$config{ldap_servertitle} $host";
              $title="$text{srvLDAP} $thishost_name:";
              &check_socket($host, $port, $rv->{'fh'}, \$sttaparams{'ldap'}{'ldap_status'});
              if ( $debug eq 4 ) {
                   &additional_log('ldap socket', "socket", "$title $port");
                   &webmin_log("ldap socket","socket",$sttaparams{'ldap'}{'ldap_status'});
              }
         };

  # IS LDAP2 up
         $host=$config{ldap2_server};
         if ( $host ) {
              $port=$config {ldap2_port};
              $thishost_name="$config{ldap2_servertitle} $host";
              $title="$text{srvLDAP} $thishost_name:";
              &check_socket($host, $port, $rv->{'fh'},\$sttaparams{'ldap'}{'ldap2_status'});
              if ( $debug eq 4 ) {
                   &additional_log('ldap2 socket', "socket", "$title $port");
                   &webmin_log("ldap2 socket","socket",$sttaparams{'ldap'}{'ldap2_status'});
              }
         };

  if ( $config{'is_ldapcache'} eq "true" ) {
         # IS LDAP CACHE up
         $host=$config{ldapcache_server};
         if ( $host ) {
              $port=$config {ldapcache_port};
              $thishost_name="$config{ldapcache_servertitle} $host";
              $title="$text{srvLDAPCACHE} $thishost_name:";
              &check_socket($host, $port, $rv->{'fh'}, \$sttaparams{'ldap'}{'ldapcache_status'});
              if ( $debug eq 4 ) {
                   &additional_log('ldapcache socket', "socket", "$title $port");
                   &webmin_log("ldapcache socket","socket",$sttaparams{'ldap'}{'ldapcache_status'});
              }
         };
     
         # IS LDAP2 CACHE up
         $host=$config{ldapcache2_server};
         if ( $host ) {
              $port=$config {ldapcache2_port};
              $thishost_name="$config{ldapcache2_servertitle} $host";
              $title="$text{srvLDAPCACHE} $thishost_name:";
              &check_socket($host, $port, $rv->{'fh'}, \$sttaparams{'ldap'}{'ldapcache2_status'});
              if ( $debug eq 4 ) {
                   &additional_log('ldapcache2 socket', "socket", "$title $port");
                   &webmin_log("ldapcache2 socket","socket",$sttaparams{'ldap'}{'ldapcache2_status'});
              }
         }; 
  } 
  $ldap_ok=true;
  $ldapcache_ok=true;
  $tta_ok=true;
  if ($sttaparams{'ldap'}{'ldapcache_status'} ne &text('responding_on',$config{ldapcache_port}) ) {
         $ldapcache_ok=false;
  }
  if ($sttaparams{'ldap'}{'ldap_status'} ne &text('responding_on',$config{ldap_port})) {
         $ldap_ok=false;
  }
  if  ($ttahosts[$mhost]->{ttaarray_status} ne &text('responding_on',$sttaparams{tta}{array_port})) {
         $tta_ok=false;
  }
}
  
# checkSTTA_services($which,$how_handle,&var_out)
# $which can be "local"  (localhost and array services) or "remote" ( no local services)
# any other even nothing will be the whole array services !
# $how_handle means $config{iferror_services} options: stop, warn, browse
# ${$_[2]} is var to keep
sub checkSTTA_services
{
 local $title="";
 local $lnum=0;
 local %h_prop;
 local %h_hosts=&list_hosts(5);
 foreach $item (@ttahosts) {
         next if  ( $_[0] eq "local" && 
              $item->{'dns_peer'} ne $sttaparams{tta}{ttalocal_dns_peer} &&
              $is_ttamaster ne true  );
         next if  ( $_[0] eq "remote" && 
              $item->{'dns_peer'} eq $sttaparams{tta}{ttalocal_dns_peer} );
         $thishost_name="";
         %h_prop=%{$ttaprops{$item->{name}}};
         if ( $sttaparams{'tta'}{'host_master'} eq $item->{'dns_peer'} ) {
              $is_ttamaster=true;
              $thishost_name="TTA MASTER->";
         }
       # Is TTA Alive on this host ??
         $host=$item->{'dns_peer'};
         $thishost_name.=$item->{'name'};

         if ( $item->{'name'} ne $host ) { $thishost_name.=$item-{'name'}." ($host)"; }

       # Try Array 5427
         $port=$sttaparams{tta}{array_port};
         $title="$text{srvTTA_Array} $thishost_name";
         if ( $port) { 
              &chkout_service($_[1],$title,$item->{'ttaarray_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         }
       # Try STD   
         $port=$sttaparams{tta}{array_extport};
         $title="$text{srvTTA_STD} $thishost_name:";
         if ( $port && $item->{acceptconn} eq "enabled" ) { 
              &chkout_service($_[1],$title,$item->{'ttastd_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         }; 
       # Try SSL
         $port=$sttaparams{tta}{array_secureport};
         $title="$text{srvTTA_SSL} $thishost_name:";
         if ( $port && $ttahosts[$masthost]->{acceptconn} eq "enabled" &&
              $item->{secure}  =~ /ssl/ ) { 
               &chkout_service($_[0],$title,$item->{'ttassl_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         };
       # Test CDM
         $title="$text{srvTTA_CDM} $thishost_name:";
         if ( $sttaparams{tta}{cdm_enabled} eq "1"  && 
             $item->{acceptconn} eq "enabled" ) {
             $port=$sttaparams{tta}{cdm_extlnbport};
              &chkout_service($_[1],$title,$item->{'ttacdm_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         };
       # Assuming same ports in all the array ....
       # IS HTTP
         $port=$config {http_port};
         $title="$text{srvHTTP} $thishost_name:";
         if ( $port ) {
              &chkout_service($_[1],$title,$item->{'http_status'},\${$_[2]}); 
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         };
       # IS HTTPS
         $port=$config {https_port};
         $title="$text{srvHTTPS} $thishost_name:";
#         if ( $port && $ttahosts[$masthost]->{secure} eq "1" ) { 
         if ( $port ) { 
              &chkout_service($_[1],$title,$item->{'https_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         }
       # Alive Port
         $host=$item->{'dns_peer'};
         $port="";
         if  ( $h_hosts{$host}->{'ipname'} eq $host ) {
            $port=$h_hosts{$host}->{'portalive'};
            $thishost_name=$h_hosts{$host}->{'name'};
            $host=$h_hosts{$host}->{'ipname'};
         }
         $title="$text{f_portalive} $thishost_name:";
         if ( $port ) { 
              &chkout_service($_[1],$title,$item->{'ipalive'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         }
       $lnum++;
 }
  
   # IS LDAP up
         $host=$config{ldap_server};
         $thishost_name=$config{ldap_servertitle};
         $title="$text{srvLDAP} $thishost_name:";
         if ( $host ) {
              $port=$config {ldap_port};
               &chkout_service($_[1],$title,$sttaparams{'ldap'}{'ldap_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         };

  # IS LDAP2 up
         $host=$config{ldap2_server};
         $thishost_name=$config{ldap2_servertitle};
         $title="$text{srvLDAP} $thishost_name:";
         if ( $host ) {
              $port=$config {ldap2_port};
              &chkout_service($_[1],$title,$sttaparams{'ldap'}{'ldap2_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         };

  if ( $config{'is_ldapcache'} eq "true" ) {
         # IS LDAP CACHE up
         $host=$config{ldapcache_server};
         $thishost_name=$config{ldapcache_servertitle};
         $title="$text{srvLDAP} $thishost_name:";
         if ( $host ) {
              $port=$config {ldapcache_port};
              &chkout_service($_[1],$title,$sttaparams{'ldap'}{'ldapcache_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         };
     
         # IS LDAP2 CACHE up
         $host=$config{ldapcache2_server};
         $thishost_name=$config{ldapcache2_servertitle};
         $title="$text{srvLDAPCACHE} $thishost_name:";
         if ( $host ) {
              $port=$config {ldapcache2_port};
              &chkout_service($_[1],$title,$sttaparams{'ldap'}{'ldapcache2_status'},\${$_[2]});
         } else {
              &chkout_service($_[1],$title,"unknown",\${$_[2]});
         }; 
  }
}

sub printSTTA_config
{
 local $item;

 local @datacfg = ( 'is_masterHost', 'masterHost', 'masterHostURL',
		'ldap_type',
		'is_ldapcache', 'ldapcache_type',
		'cache_chkdefaults', 'cache_chkttalogin',
		'cache_scandebug', 'cache_domain',
		'check_maxsess', 'default_maxsess', 'check_ticket',
		'default_cat', 'order_cat', 'interface',
		'has_monitor',
		'has_cronsess',
		'sessout_timeout',
		'output_cgis',
		'debug_code',
		'check_services',
		'iferror_services',
                         );
    foreach $item (@datacfg) {
           $str_val=$config{$item}; 
           if ( $config{$item} eq "true" ) { $str_val=$text{yes}; }
           if ( $config{$item} eq "false" ) { $str_val=$text{no}; }
           if ( $config{$item} eq "none" ) { $str_val=$text{none}; }
           if ( $item eq "sessout_timeout") {
                if ( $config{$item} eq "8" ) { $str_val="1 $text{edit_hours}"; }
                if ( $config{$item} eq "12" ) { $str_val="1 $text{edit_minutes}"; }
                if ( $config{$item} eq "11" ) { $str_val="10 $text{edit_minutes}"; }
                if ( $config{$item} eq "13" ) { $str_val="10 $text{edit_seconds}"; }
                if ( $config{$item} eq "14" ) { $str_val="1 $text{edit_seconds}"; }
           } 
           if ( $_[0]  ) {
               if ( $_[1]  ) {
                    ${$_[0]}.= "STTA: ".$text{'cfg_'.$item}."=".$_[1].$str_val."\n";
               } else {
                    ${$_[0]}.= "STTA: ".$text{'cfg_'.$item}."=".$str_val."\n";
               }
           } else {
               if ( $_[1]  ) {
                   print "STTA: ".$text{'cfg_'.$item}.$_[1]."=".$_[1].$str_val."\n";
                } else {
                   print "STTA: ".$text{'cfg_'.$item}."=".$str_val."\n";
                }
           }
    }
}

# chkout_service($how_handle, $title, $message,&varout,$sep)
# $how_handle means $config{iferror_services} options: stop, warn, browse, status
# ${$_[3]} is var to keep
sub chkout_service
{
    local $respo=$text{responding_on};
    $respo=~ s/\$1//;

       if ( "$_[0]" eq "stop" ) {
             if  ( "$_[2]" =~ /$respo/ || "$_[2]" eq "unknown"){ 
                   return; 
             } else {
                    $err_msg="$_[1]  $_[2]";
                   # &SrvError(); 
                    &additional_log("STTA $_[0]", "services", "$_[2]");
                    &webmin_log("STTA $text{$_[0]}","services","$_[1]",@_[2]);
                    error("STTA $text{$_[0]}: $_[1] \n\t\"$_[2]\" \n");
             }
       } elsif ( "$_[0]" eq "warn" ) {
             if ( "$_[2]" ne "unknown" ) {
                   &webmin_log("STTA $text{$_[0]}","services","$_[1] - $_[2]");
             } else {
                   &webmin_log("STTA $text{$_[0]}","services","$_[1] - $text{unknown}");
             }
       } elsif ( "$_[0]" eq "browse" ) {
             if ( $config{iferror_services} eq "stop" ) {
                if  ( "$_[2]" =~ /$respo/ || "$_[2]" eq "unknown"){ 
                    #
                } else {
                    $err_msg="$_[1]  $_[2]";
                    &additional_log("STTA $_[0]", "services", "$_[2]");
                    &webmin_log("STTA $text{$_[0]}","services","$_[1]");
                    &SrvError(); 
                }
             }
             if ( "$_[2]" ne "unknown" ) {
                   &webmin_log("STTA $text{$_[0]}","services","$_[1] - $_[2]");
             } else {
                   &webmin_log("STTA $text{$_[0]}","services","$_[1] - $text{unknown}");
             }
       } elsif ( "$_[0]" eq "status" && $_[3] ) {
             if ( $config{iferror_services} eq "stop" ) {
                if  ( "$_[2]" =~ /$respo/ || "$_[2]" eq "unknown"){ 
                    #
                } else {
                    $err_msg="$_[1]  $_[2]";
                    &additional_log("STTA $_[0]", "services", "$_[2]");
                    &webmin_log("STTA $text{$_[0]}","services","$_[1]");
                    &SrvError(); 
                }
             }
             if ( "$_[2]" ne "unknown"  ) {
                   ${$_[3]}.= "STTA: $_[1] == $_[2] \n";
             } else {
                   ${$_[3]}.= "STTA: $_[1] == $text{unknown} \n";
             }
       } else {
             if  ( "$_[2]" =~ /$respo/ || "$_[2]" eq "unknown"){ 
                   print "STTA:  $_[1] $text{OK} \n";
             } else {
                  if ( "$_[2]" ne "unknown" ) {
                      &webmin_log("STTA $text{$_[0]}","services","$_[1] - $_[2]");
                  } else {
                      &webmin_log("STTA $text{$_[0]}","services","$_[1] - $text{unknown}");
                  }
             }
       }
}

# To check if host is alive in a defined port ....
# $_[0] is the host alias
# $_[1] if is true print title
sub check_host {

  local $aliasHost=$_[0];
  local $grpalias;
  local $grpname;
  local $title;
  local $port;
  local $hosts_status;

  if ( !%h_hosts ) { %h_hosts=&list_hosts(); };
  if (defined($h_hosts{$_[0]}) ) { 
      if ( !%h_grphosts ) { %h_grphosts=&list_grphosts(); };
  } else {
      return "";
  }
  $grpalias=$h_hosts{$_[0]}->{'grpalias'};
  $grpname=$h_grphosts{$grpalias}->{'name'};
  $title="$h_hosts{$_[0]}->{name} [$grpname] ($h_hosts{$_[0]}->{ipname})";
  if ( $_[1] eq "true" ) { print "<tr><td>$title</td></tr>\n"; }
  if ( $DebugFile ) { print DebugFile "$title \n"; }
  $hosts_status=&text('responding_on',$h_hosts{$_[0]}->{portalive});
  &check_socket($h_hosts{$_[0]}->{ipname}, $h_hosts{$_[0]}->{portalive}, $rv->{'fh'}, \$hosts_status);
  &webmin_log("$h_hosts{$_[0]}->{ipname} socket","ipalive","title=>$title||status=>$hosts_status||port=>$h_hosts{$_[0]}->{portalive}",$h_hosts{$_[0]});
  if ( $DebugFile ) { print DebugFile "$hosts_status \n"; }
  return "$hosts_status";
}

sub test_minfo
{
   local $task=$_[0];
   local $where=$_[1];
   local $outFormat=$_[2];

}
1;  # Return true  
