#!/usr/bin/perl
# addobj.cgi
# ...  Servicios de Tarantella.

require './tarantella-lib.pl';
do "ldap-lib.pl";

&header($text{'index_addldapcache'}, "", "");
print "<hr><p>\n";
if ( $config{'is_ldapcache'} ne "true" ) { &footCacheHere; exit }
&foreign_require("proc", "proc-lib.pl");
$ldapcache_server=1;
&connect_cache();
&ReadParse();

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

 $username=$in{username};
 $user_ou=$in{user_ou};
 $userou=$in{userou};
 if (!$username && !$user_ou )  { &footHere; exit }  ;
  # Getting ORIGIN .....
     if ( $userou eq "" ) {
       # .... taking ou as target 
       $oualias=$user_ou;
     } else {
       $oualias=$userou;
     }
     $organizationname="";
     $dn_ou="";
     $ens_ou="";
     $oudescrip="";
     foreach $ou (&list_ous()) {
          if ( $ou->{'alias'} eq $oualias ) {
               $dn_ou=$ou->{obj};
               $ens_ou=$ou->{ens};
               $organizationname=$ou->{name};
               $oudescrip=$ou->{descrip};
               last;
          }
     }
     if ( "$dn_ou" eq "" ) {
               print "<hr>\n";
               print "<b> error: ($oualias) </b>\n";
               print "<hr>\n";
               &footHere; exit;
     }
     if ( $userou eq "" ) {
       # .... taking ou as target  do not check it !!!
       $dn = "$dn_ou";
       $dn_origin="cn=default_obj";
       $objname=$organizationname;
       $obj_descrip="Default - $oudescrip";
     } else {
       $objname=$username;
       # ... checking that Username exists !!!!
       $dn = "cn=$username,$dn_ou";
       $dn_origin="cn=$username";
       $dnfull = "$dn,".$config{ldap_base};
       &connect ;
       $entry = $conn->browse($dnfull);
       if ($err = $conn->getErrorCode ()) {
               print "<hr>\n";
               print "<b> ($dnfull): $err:".$conn->getErrorString ()."</b>\n";
               print "<hr>\n";
               &footHere; exit;
       }
       if ( "$config{ldap_type}" eq "OpenLDAP" ) {
              $dn_dept = $entry->{description}[0];
        } else {
              $dn_dept = $entry->{departmentnumber}[0];
        }
       $obj_descrip=$entry->{sn}[0]."- ($dn_dept)";
     }
  #  Getting TARGET ..... expecting only one !!!
     $objType="";
     if ( $in{appname} ne "none" ) {
          $objType="app";
          $target_alias=$in{appname};
          @listobj =&list_apps();
     }
     if ( $in{grpappsname} ne "none" ) {
          $objType="grpapps";
          $target_alias=$in{grpappsname};
          @listobj =&list_grpapps();
     }
     if ( $in{hostname} ne "none" ) {
          $objType="host";
          $target_alias=$in{hostname};
          @listobj =&list_hosts();
     }
     if ( $in{usergrphosts} ne "none" ) {
          $objType="grphosts";
          $target_alias=$in{grpappsname};
          @listobj =&list_grphosts();
     }
     if ( $in{domain} ne "none" ) {
          $objType="domain";
          $target_alias=$in{domain};
          @listobj =&list_domains();
     }

     $dn_target="";
     foreach $elem (@listobj) {
        if ( $elem->{'alias'} eq $target_alias ) {
             $descrip_target=$elem->{descrip};
             $dn_target=$elem->{obj};
             $ens_target=$elem->{ens};
             $name_target=$elem->{name};
             break;
         }
     }
     if ( $dn_target eq "" ) {
               print "<hr>\n";
               print "<b> error:  ($objType :$target_alias) </b>\n";
               print "<tr><p><b>$obj_descrip</b></tr>\n";
               print "<hr>\n";
               &footHere; exit;
     }
     $dn_target=~s/cn=/ou=/g;  
     $ldapcache_server=1;
     &connect_cache ();
     @tta3tier=&set_tta3tier($dn_origin,$dn_target,$dn_ou,"add",$objType);
     print "<tr><p>.</tr>\n";
     print "<tr><p><b>$objname ->  $name_target</b></tr>\n";
     print "<tr><p><b>$obj_descrip</b></tr>\n";
     if ( &cache_obj(\%tta3tier) ne 0 ) { &footHere; exit }  ;
     if ( $config{ldapcache2_server}.length gt 0 ) {
             $ldapcache_server=2;
             &connect_cache ();
             if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
     }
     print "<tr><p><b>".$text{'user_made'}."</b></tr>\n";
     print "<tr></tr>\n";
     &footHere;
     exit;

