#!/usr/bin/perl
# updatecache.cgi
# Update cache...  Servicios de Tarantella.

require './tarantella-lib.pl';
&foreign_require("proc", "proc-lib.pl");
do "ldap-lib.pl"
&header($text{'index_updatecache'}, "", "");
print "<hr><p>\n"; 
&ReadParse();

$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 }
 
 # Getting Profile details 
        foreach $list_profcache (&list_profcache) {
           if ( $list_profcache->{'alias'} eq $profilename) {
               $prof_alias=$list_profcache->{'alias'};
               $prof_name=$list_profcache->{'name'};
               $prof_hostusername=$list_profcache->{'hostusername'};
               $prof_hostname=$list_profcache->{'hostname'};
               $prof_grphost=$list_profcache->{'grphost'};
               $prof_username=$list_profcache->{'username'};
               $prof_userou=$list_profcache->{'ou'};
               last;
            }
        }
 if ( !$prof_hostusername ) { &footHere; exit }
 # Getting Password for HOST or HostGroup
         foreach $hostsuser (&list_hostsuser) {
            if ( $hostsuser->{'username'} eq $prof_hostusername) {
                 $hostspassword=$hostsuser->{'passwd'};
                 last;
            }
         }
 if ( !$hostspassword ) { &footHere; exit }

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

       if ($prof_grphost) {
           foreach $grphosts (&list_grphosts()) {
              if ( $grphosts->{'alias'} eq $prof_grphost) {
                   $ens_grphosts=$grphosts->{'ens'};
                   $dn_grphosts=$grphosts->{'obj'};
                   $grp_name=$grphosts->{'name'};
                   last;
              }
           }
           if ( !$ens_grphosts ) { &footHere; exit }
                 foreach $host (&list_hosts()) {
                    if ( $host->{'grpalias'} eq $prof_grphost) {
                          push(@listhosts, { 'name' => $host->{'name'},
                                             'ens' => $host->{'ens'},
                                             'num' => scalar(@listhosts) });
                    }
                    $numhosts++;
                 }
        }
        if ($prof_hostname) {
                 foreach $host (&list_hosts()) {
                    if ( $host->{'alias'} eq $prof_hostname) {
                          push(@listhosts, { 'name' => $host->{'name'},
                                             'ens' => $host->{'ens'},
                                             'num' => scalar(@listhosts) });
                          last;
                    }
                    $numhosts++;
                 }
        }


       # If OU Group ... Load Users

       $numusrs=0;
       @listusers="";

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

           &connect ();
           $ldap_base="$dn_ou,$config{ldap_dnusers}";
           $filter = "(cn=*)";
           $entry = $conn->search ($ldap_base, "subtree", $filter );
           while ($entry) {
               my (%users);
               $users{dn} = $entry->getDN ();
               $users{cn} = $entry->{cn}[0];
               $users{displayname} = $entry->{displayname}[0];
               $users[$numusrs++] = \%users;
               if ( "$config{ldap_type}" eq "OpenLDAP" ) {
                     $users{department} = $entry->{description}[0];
               } else {
                     $users{department} = $entry->{departmentnumber}[0];
               }
               $entry = $conn->nextEntry ();
           }
        }
        if ($prof_username) {
           &connect ();
           $ldap_base="$config{ldap_dnusers}";
           $filter = "(cn=$prof_username)";
           $entry = $conn->search ($ldap_base, "subtree", $filter );
           while ($entry) {
               my (%users);
               $users{dn} = $entry->getDN ();
               $users{cn} = $entry->{cn}[0];
               $users{displayname} = $entry->{displayname}[0];
               if ( "$config{ldap_type}" eq "OpenLDAP" ) {
                     $users{department} = $entry->{description}[0];
               } else {
                     $users{department} = $entry->{departmentnumber}[0];
               }
               $users[$numusrs++] = \%users;
               $entry = $conn->nextEntry ();
           }
        }
        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";
        }
	unlink($temp_f);
        print "<tr></tr>\n";
 &footHere;
 exit;

