#!/usr/bin/perl
# updatecache.cgi
# Update cache...  Servicios de Tarantella.
# 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");
do "ldap-lib.pl"
&headerstta($text{'index_updatecache'}, "", "",undef,1,1,$text{headpage_rigth} );
print "<hr><p>\n"; 
&ReadParse();

sub callbackSearch 
{
   my ( $conn, $entry) = @_;   

   if ( !defined($entry) ) { return };
   $dn = $entry->dn;
   @attrs = $entry->attributes; # Obtain attributes for this entry.
   my (%users);
   $users{dn} = $dn;
   foreach my $var (@attrs) {
        $attr = $entry->get_value( $var, asref => 1 );
        if ( defined($attr) ) {          
             foreach my $value (@$attr) { $users{$var} = $value; } 
        }                                                                       
   }
   $users{department} = $users{description};
   $users[$numusrs++] = \%users;
   $conn->pop_entry; 
}

$ldap_server=1;

 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_updatecache'}</b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $profilename=$in{profilename};

 if (!$profilename)  { &footHere; exit }

 %h_ou=&list_ous();
 %h_grphost=&list_grphosts();
 %h_host=&list_hosts();
 %h_hostuser=&list_hostuser();
 %h_profcache=&list_profcache();
 
 # Getting Profile details 
 if ( $h_profcache{$profilename}->{'alias'} eq $profilename) {
               $prof_alias=$h_profcache{$profilename}->{'alias'};
               $prof_name=$h_profcache{$profilename}->{'name'};
               $prof_hostusername=$h_profcache{$profilename}->{'hostusername'};
               $prof_hostname=$h_profcache{$profilename}->{'hostname'};
               $prof_grphost=$h_profcache{$profilename}->{'grphost'};
               $prof_username=$h_profcache{$profilename}->{'username'};
               $prof_userou=$h_profcache{$profilename}->{'ou'};
    }
 if ( !$prof_hostusername ) { &footHere; exit }

 # Getting Password for HOST or HostGroup
 if ( $h_hostsuser{$prof_hostusername}->{'username'} eq $prof_hostusername) {
                 $hostspassword=$h_hostuser{$prof_hostusername}->{'passwd'};
 }
 if ( !$hostspassword ) { &footHere; exit }

       # If HostGroup ... Load Hosts ...
       $numhosts=0;
       @listhosts="";

       if ( $h_grphost{$prof_grphost}->{'alias'} eq $prof_grphost) {
                   $ens_grphosts=$h_grphost{$prof_grphost}->{'ens'};
                   $dn_grphosts=$h_grphost{$prof_grphost}->{'obj'};
                   $grp_name=$h_grphost{$prof_grphost}->{'name'};
           }
           if ( !$ens_grphosts ) { &footHere; exit }
                 foreach $item (keys (%h_host)) {
                      if ( $h_host{$item}->{'grpalias'} eq $prof_grphost) {
                          push(@listhosts, { 'name' => $h_host{$item}->{'name'},
                                             'ens' => $h_host{$item}->{'ens'},
                                             'num' => scalar(@listhosts) });
                          $numhosts++;
                      }
                 }
        }
        if ($prof_hostname) {
                 if ( $h_host{$prof_hostname}->{'alias'} eq $prof_hostname) {
                          push(@listhosts, { 'name' => $h_host{$prof_hostname}->{'name'},
                                             'ens' => $h_host{$prof_hostname}->{'ens'},
                                             'num' => scalar(@listhosts) });
                      $numhosts++;
                 }
        }

       # If OU Group ... Load Users

       $numusrs=0;
       @listusers="";

      if ($prof_userou) {
           if ( $h_ou{$prof_userou}->{'alias'} eq $prof_userou) {
                   $dn_ou=$h_ou{$prof_userou}->{'obj'};
                   $ouname=$h_ou{$prof_userou}->{'name'};
                   $ens_ou=$h_ou{$prof_userou}->{'ens'};
                   $organizationname=$h_ou{$prof_userou}->{'name'};
           }
           if ( !$ens_ou ) { &footHere; exit }

           &connect ();
           $ldap_base="$dn_ou,$config{ldap_dnusers}";
           $filter = "(cn=*)";

           &additional_log("search ", undef, "$ldap_base\nsubtree\n$filter");
           my $status_con = $ldapObj->search ( base => $ldap_base,
                                               scope => "sub",
                                               filter => "(cn=" . $filter . ")",
                                               callback => \&callbackSearch
                                              );
           &ldap_logerr("search $ldap_base");  
        }
        if ($prof_username) {
           &connect ();
           $ldap_base="$config{ldap_dnusers}";
           $filter = "(cn=$prof_username)";

           &additional_log("search ", undef, "$ldap_base\nsubtree\n$filter");
           my $status_con = $ldapObj->search ( base => $ldap_base,
                                               scope => "sub",
                                               filter => "(cn=" . $filter . ")",
                                               callback => \&callbackSearch
                                              );
           &ldap_logerr("search $ldap_base");  
        }
        if ($#users < 0) { &footHere; exit }

    # Ready to write TTA passcache file ...

        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");

        foreach $user (@users) {
          $dn = $user->{dn};
          if ($dn) {
              $cn = $user->{cn};
              $ou=$user->{department};
              $ens_usr = ".../_ens/".$config{tta_base}."/$ens_ou/$cn";
              $dn_usr = ".../_service/sco/tta/ldapcache/$dn";
              # Scan for host in the same HostGroup ...
              foreach $host (@listhosts) {
                $host_name=$host->{name};
                $enshost = $host->{ens};
                if ($enshost ) {
                    $ens_host = ".../_ens/".$config{tta_base}."/$enshost";
                    print TEMPF "--person \"$dn_usr\" --resource \"$ens_host\" --resuser \"$prof_hostusername\" --respass \"$hostspassword\"\n";
                   print "<tr><td>$profilename: $cn -- $host_name -- $prof_hostusername</tr></td>\n";
                }
              }
          }
        }
        close(TEMPF);

        $type_cmd="passcache";
        if (&run_tta_cmd($type_cmd,"",$temp_f)) { 
            print "<tr><td><b>".$text{'user_made'}.$prof_alias." ->  ".$prof_name."</b><td></tr>\n";
        }
	if ( $debug lt 2 } {  unlink($temp_f); }
        print "<tr></tr>\n";
 &footHere;
 exit;

