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

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

&header($text{'index_addgrphosts'}, "", "");
print "<hr><p>\n"; 
&foreign_require("proc", "proc-lib.pl");
&ReadParse();

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

 $grphostsname=$in{grphostsname};
 $usergrphosts=$in{usergrphosts};
 $aliasgrphosts=$in{aliasgrphosts};
 $descrip=$in{descrip};

 if ($grphostsname)  {
    $grp_name="";
    $dn_grphosts="";
    $ens_grphosts="";
    foreach $grphosts (&list_grphosts()) {
         if ( $grphosts->{'alias'} eq $usergrphosts ) {
            $dn_grphosts=$grphosts->{obj};
            $ens_grphosts=$grphosts->{ens};
            $grp_name=$grphosts->{name};
            last;
         }
    }
    if ( $dn_grphosts eq "" ) {
         $dn = "cn=$grphostsname";
         $ens = "cn=$grphostsname";
         $ens_obj = "--name \"".$config{tta_base}."/cn=$grphostsname\"";
    } else {
         $dn = "cn=$grphostsname,$dn_grphosts";
         $ens = "$ens_grphosts/cn=$grphostsname";
         $ens_obj = "--name \"".$config{tta_base}."/$ens_grphosts/cn=$grphostsname\"";
    }
        $dn_org=$dn;
        $| = 1;
        $temp = &tempname();
        open(TEMP, ">$temp");
        print TEMP "#!/bin/sh\n";
        print TEMP $env;
        print TEMP "export $export\n" if ($export);
        print TEMP "$str\n";
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "$ens_obj\n";
        close(TEMPF);
	# Default object para LDAP cache ...
        if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) {
            $obj_descrip=$in{descrip};
            $dn_org=$dn;
            $dn=~s/cn=/ou=/;
            $dn_cache=$dn;
            $dnldap = "default";
            $ldapcache_server=1;
            &connect_cache ();
            $default_obj="cn=$config{ldapcache_defaultobj}";  
            @tta3tier=&set_tta3tier($default_obj,$dn_cache,$dnldap,"add","grphost");
            &cache_obj(\%tta3tier);
            if ( $config{ldapcache2_server}.length gt 0 ) {
                 $ldapcache_server=2;
                 &connect_cache ();
                 if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
            }
        }

        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
           $lin_grphosts=$aliasgrphosts."::".$grphostsname."::"."$dn_org"."::"."$ens"."::".$descrip;
           &lock_file($config{'grphosts_list'});
	   open(FILEGRPHOSTS, ">>$config{'grphosts_list'}");
           print FILEGRPHOSTS "$lin_grphosts\n";
           close (FILEGRPHOSTS);
           $str=$text{'user_made'}.$grphostname." ->  ".$in{usergrphosts};
           &additional_log('addgrphosts.cgi', undef, $str);
           &webmin_log($config{'grphosts_list'}, undef, "write");
           &unlock_file($config{'grphosts_list'});
           print "<tr><p><b>".$text{'user_made'}.$grphostsname." ->  ".$in{usergrphosts}."</b></tr>\n";
           print "<tr></tr>\n";
#           print "<tr><form METHOD=POST name=addgrphosts ACTION=\"tta_modgrphosts.cgi?grphostsname=$grphostsname&usergrphosts=$usergrphosts\" >\n"; 
#           print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"addgrphosts\"\n";
#           print "  <input TYPE=\"hidden\" NAME=\"grphostsname\" VALUE=\"",$grphostsname,"\"\n";
#           print "  <input TYPE=\"hidden\" NAME=\"usergrphosts\" VALUE=\"",$usergrphosts,"\">\n";
#           print "  <input TYPE=\"hidden\" NAME=\"aliasgrphosts\" VALUE=\"",$aliasgrphosts,"\">\n";
#           print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\n";
#           print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_modatt'},"\" VALUE=\"",$text{'f_modatt'},"\">\n";
#           print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
#           print "	</form>\n"; 
        }
	unlink($temp_f);
 }
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;

