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

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

&headerstta($text{'index_addldapcache'}, "", "addobj",undef,1,1,$text{headpage_rigth} ); 
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="";
     %h_ou=&list_ous();
     if ( $h_ou{$oualias}->{'alias'} ne $oualias ) {
          $err_msg=$text{'list_err'}," ($oualias) ";
          &footHere; 
          exit;
     }
     $dn_ou=$h_ou{$oualias}->{'obj'};
     $organizationname=$h_ou{$oualias}->{'name'};
     $ens_ou=$h_ou{$oualias}->{'ens'}; 
     $ou_descrip=$h_ou{$oualias}->{'decrip'}; 
     if ( "$dn_ou" eq "" ) {
     }
     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};
       $ldap_server=1;
       &connect();
       $cur_entry=&sel_ldap_obj($dn,"ou=*");
       if ( !defined($cur_entry) || $cur_entry->dn ne $dn) {
            $err_msg=" Error: $username $organizationname\n";
            &html_errshow("ldap");
            &footHere; exit ;
       }
       %entryObj=&get_ldap_obj($dn,$cur_entry);
       $dn_dept = $entryObj{description};
       $obj_descrip=$entryObj{sn}."- ($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="";
     if ( $listobj{$target_alias}->{'alias'} eq $target_alias ) {
             $descrip_target=$listobj{$target_alias}->{descrip};
             $dn_target=$listobj{$target_alias}->{obj};
             $ens_target=$listobj{$target_alias}->{ens};
             $name_target=$listobj{$target_alias}->{name};
     }
     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;

