#!/usr/bin/perl
# fix_lists.cgi
# Fix Tarantella Services Lists
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

require './stta-lib.pl';
#&foreign_require("proc", "proc-lib.pl");
&ReadParse();

$VERSION="1.2";
# FUNCTIONS 1.1 
# list_ous_11()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "ou"
sub list_ous_11
{
# read the OU list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading OU File List
	$lnum = 0;
	open(FILEOUS, $config{'ou_list'});
	while(<FILEOUS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEOUS);
return @rv;
}

# list_apps_11()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "app"
sub list_apps_11
{
# read the APP list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading APP File List
	$lnum = 0;
	open(FILEAPPS, $config{'app_list'});
	while(<FILEAPPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'grpalias' => $lin[2], 
				    'obj' => $lin[3], 'ens' => $lin[4], 
				    'descrip' => $lin[5], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEAPPS);
return @rv;
}

# list_grpapps_11()
# Returns an array of hashtable, each containing info about one grpapps. Each hash
# will always contain the keys "alias" "name" "grpapps"
sub list_grpapps_11
{
# read the GRPAPPS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading GRPAPPS File List
	$lnum = 0;
	open(FILEGRPAPPS, $config{'grpapps_list'});
	while(<FILEGRPAPPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'descrip' => $lin[4], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEGRPAPPS);
return @rv;
}

# list_domains_11()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "domain"
sub list_domains_11
{
# read the DOMAIN list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading DOMAIN File List
	$lnum = 0;
	open(FILEDOMAINS, $config{'domain_list'});
	while(<FILEDOMAINS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'descrip' => $lin[4], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEDOMAINS);
return @rv;
}

# list_grphosts_11()
# Returns an array of hashtable, each containing info about one grphosts. Each hash
# will always contain the keys "alias" "name" "grphosts"
sub list_grphosts_11
{
# read the GRPHOSTS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading GRPHOSTS File List
	$lnum = 0;
	open(FILEGRPHOSTS, $config{'grphosts_list'});
	while(<FILEGRPHOSTS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'descrip' => $lin[4], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEGRPHOSTS);
return @rv;
}

# list_hosts_11()
# Returns an array of hashtable, each containing info about one hosts. Each hash
# will always contain the keys "alias" "name" "hosts"
sub list_hosts_11
{
# read the HOSTS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading HOSTS File List
	$lnum = 0;
	open(FILEHOSTS, $config{'hosts_list'});
	while(<FILEHOSTS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'grpalias' => $lin[2], 
				    'obj' => $lin[3], 'ens' => $lin[4], 
				    'descrip' => $lin[5], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEHOSTS);
return @rv;
}


 &headerstta($text{'index_fixlists'}, "", "",undef,1,1,$text{headpage_rigth} );
 print "<hr><p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_fixlists'} $userou </b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";
 %minfo=&load_minfo();
 if ( $config{stta_defslist} ne "" ) {
      $config_dir=$config{stta_defslist};
 } else {
      $config_dir="$config_directory/$module_name";
 } 
 &read_file( "$config_dir/module.info",\%cinfo) ;

 if ( $minfo{version} eq $cinfo{version} ) {
       print "<tr><b>$minfo{name} Module $text{'user_update'}... $minfo{version} </b></tr>\n";
       &footHere;
       exit;
 }
    $maxsess="9999";
    $portalive="22";
    # OUS ...
    @listous=&list_ous_11(); 
             @list__ous = sort {$a->{'name'} cmp $b->{'name'}} @listous;
             &lock_file($config{'ou_list'});
             open(FILEOUS, ">$config{'ou_list'}");
             foreach $ou (@list__ous) {
                   $lin_ou=$ou->{'alias'}."::".$ou->{'name'}."::".$ou->{'obj'}."::".$ou->{'ens'}."::".$maxsess."::\n";
                       print FILEOUS "$lin_ou";
             }
             close (FILEOUS);
             &unlock_file($config{'ou_list'});
             $str=$text{'user_modify'}."-".$text{'index_ou'};
             print "<tr><p><b>$str</b></tr>\n";
             &additional_log('fix_lists.cgi', "ou", $str);
             &webmin_log($config{'ou_list'}, "ou", "fixed");


    # APPS ...
     @listapps=&list_apps_11();
             @list__apps = sort {$a->{'name'} cmp $b->{'name'}} @listapps;
             &lock_file($config{'app_list'});
             open(FILEAPPS, ">$config{'app_list'}");
                   foreach $app (@list__apps) {
                       if ( $app->{'descrip'} eq "" ) {
                           $descrip=$app->{'name'};
                       } else {
                           $descrip=$app->{'descrip'};
                       }
                       $linapp=$app->{'alias'}."::".$app->{'name'}."::".$app->{'grpalias'}."::".$app->{'obj'}."::".$app->{'ens'}."::".$maxsess."::".$descrip."::\n";
                       print FILEAPPS "$linapp";
                   }
             close (FILEAPPS);
             &unlock_file($config{'app_list'});
             $str=$text{'user_modify'}."-".$text{'index_apps'};
             print "<tr><p><b>$str</b></tr>\n";
             &additional_log('fix_lists.cgi', "app", $str);
             &webmin_log($config{'app_list'}, "app", "fixed");

    # GRPAPPS ...
      @listgrpapps=&list_grpapps_11();
             @list_grpapps = sort {$a->{'name'} cmp $b->{'name'}} @listgrpapps;
             &lock_file($config{'grpapps_list'});
             open(FILEGRPAPPS, ">$config{'grpapps_list'}");
                   foreach $grpapps (@list_grpapps) {
                       if ( $grpapps->{'descrip'} eq "" ) {
                           $descrip=$grpapps->{'name'};
                       } else {
                           $descrip=$grpapps->{'descrip'};
                       }
                       $lingrpapps=$grpapps->{'alias'}."::".$grpapps->{'name'}."::".$grpapps->{'obj'}."::".$grpapps->{'ens'}."::".$maxsess."::".$descrip."::\n";
                       print FILEGRPAPPS "$lingrpapps";
                   }
             close (FILEGRPAPPS);
             &unlock_file($config{'grpapps_list'});
             $str=$text{'user_modify'}."-".$text{'index_grpapps'};
             print "<tr><p><b>$str</b></tr>\n";
             &additional_log('fix_lists.cgi', "grpapps", $str);
             &webmin_log($config{'grpapps_list'}, "grpapps", "fixed");


    # HOSTS ...
     @listhosts=&list_hosts_11();
             @list__hosts = sort {$a->{'name'} cmp $b->{'name'}} @listhosts;
             &lock_file($config{'hosts_list'});
             open(FILEHOSTS, ">$config{'hosts_list'}");
                      foreach $hosts (@list__hosts) {
                         $ipname=$hosts->{'alias'};
                         if ( $hosts->{'descrip'} eq "" ) {
                              $descrip=$hosts->{'name'};
                         } else {
                              $descrip=$hosts->{'descrip'};
                         }
                         $grpalias=$hosts->{'grpalias'};
                         $dn_obj=$hosts->{'obj'};
                         $ens_obj=$hosts->{'ens'};
                         $wk_rima=true;
                         # For RIMA PLANTA ...
                         if ( $wk_rima eq true ) {
                               $grpalias="servidores";
                               $dn_obj=$hosts->{'grpalias'};
                               $ens_obj=$hosts->{'obj'};
                         }
                         $dn_obj=~s/cn=/ou=/g;
                         $lin_hosts=$hosts->{'alias'}."::".$hosts->{'name'}."::".$grpalias."::".$dn_obj."::".$ens_obj."::".$ipname."::".$maxsess."::".$portalive."::".$descrip."::\n";
                         print FILEHOSTS "$lin_hosts";
                      }
             close (FILEHOSTS);
             &unlock_file($config{'hosts_list'});
             $str=$text{'user_modify'}."-".$text{'index_hosts'};
             print "<tr><p><b>$str</b></tr>\n";
             &additional_log('fix_lists.cgi', "host", $str);
             &webmin_log($config{'hosts_list'}, "host", "fixed");

    # GRPHOSTS ...
      @listgrphosts=&list_grphosts_11();
             @list__grphosts = sort {$a->{'name'} cmp $b->{'name'}} @listgrphosts;
             &lock_file($config{'grphosts_list'});
             open(FILEGRPHOSTS, ">$config{'grphosts_list'}");
                      foreach $grphosts (@list__grphosts) {
                         if ( $grphosts->{'descrip'} eq "" ) {
                              $descrip=$grphosts->{'name'};
                         } else {
                              $descrip=$grphosts->{'descrip'};
                         }
                         $dn_obj=$grphosts->{'obj'};
                         $dn_obj=~s/cn=/ou=/g;
                         $lin_grphosts=$grphosts->{'alias'}."::".$grphosts->{'name'}."::".$dn_obj."::".$grphosts->{'ens'}."::".$maxsess."::".$descrip."::\n";
                         print FILEGRPHOSTS "$lin_grphosts";
 
                      }
             close (FILEGRPHOSTS);
             &unlock_file($config{'grphosts_list'});
             $str=$text{'user_modify'}."-".$text{'index_grphosts'};
             print "<tr><p><b>$str</b></tr>\n";
             &additional_log('fix_lists.cgi', "grphosts", $str);
             &webmin_log($config{'grphosts_list'}, "grphosts", "fixed");


    # DOMAINS ...
    @listdomains=&list_domains();
             @list__domains = sort {$a->{name} cmp $b->{name}} @listdomains;
             &lock_file($config{'domain_list'});
             open(FILEDOMAINS, ">$config{'domain_list'}");
                   foreach $domain (@list__domains) {
                       if ( $domain->{'descrip'} eq "") {
                            $descrip=$domain->{'name'};
                       } else {
                            $descrip=$domain->{'descrip'};
                       }
                       $lindomain=$domain->{'alias'}."::".$domain->{'name'}."::".$domain->{'obj'}."::".$domain->{'ens'}."::".$maxsess."::".$descrip."::\n";
                       print FILEDOMAINS "$lindomain";
                   }
             close (FILEDOMAINS);
             &unlock_file($config{'domain_list'});
             $str=$text{'user_modify'}."-".$text{'index_domains'};
             print "<tr><p><b>$str</b></tr>\n";
             &additional_log('fix_lists.cgi', "domain", $str);
             &webmin_log($config{'domain_list'}, "domain", "fixed");
 
 &lock_file( "$config_dir/module.info") ;
 $cinfo{version}=$VERSION;
 open (MODINFO, ">$config_dir/module.info");
 foreach $item (keys (%minfo)) {
    print MODINFO "$item=".$minfo{$item}."\n";
 }
 close (MODINFO);
 &unlock_file("$config_dir/module.info");
 $str=$text{'user_modify'}."-"."$config_dir/module.info";
 &additional_log('fix_lists.cgi', "modinfo", $str);
 &webmin_log("module.info", "modinfo", "updated");

 print "<tr><b>$minfo{name} Module $text{'user_update'}... $minfo{version} </b></tr>\n";
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
