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

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

&header($text{'index_modhosts'}, "", "");
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_adhosts'}</b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $hostname=$in{hostname};
 $location=$in{location};
 $descrip=$in{descrip};
 $ntdomain=$in{ntdomain};
 $ipaddress=$in{ipaddress};
 $maxsess=$in{maxsess};
 $aliashosts=$in{aliashosts};
 $usergrphosts=$in{usergrphosts};

 if ($hostname)  {
    $grp_name="";
    $dn_hosts="";
    $ens_hosts="";
    foreach $grphosts (&list_grphosts()) {
         if ( $grphosts->{'alias'} eq $usergrphosts ) {
            $dn_hosts=$grphosts->{obj};
            $ens_hosts=$grphosts->{ens};
            $grp_name=$grphosts->{name};
            last;
         }
    }
    if ( $dn_hosts eq "" ) {
       $dn = "ou=$hostname";
       $ens = "cn=$hostname";
       $ens_obj = "--name \"".$config{tta_base}."/cn=$hostname\"";
    } else {
       $dn = "ou=$hostname,$dn_hosts";
       $ens = "$ens_hosts/cn=$hostname";
       $ens_obj = "--name \"".$config{tta_base}."/$ens_hosts/cn=$hostname\"";
    }
        $| = 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";
                if ( $ntdomain ne "" ) {print TEMPF "  --ntdomain \"$ntdomain\"\\\n"; }
                if ( $location ne "" ) {print TEMPF "  --location \"$location\"\\\n"; }
                if ( $descrip ne "" ) {print TEMPF "  --description \"$descrip\"\\\n"; }
                print TEMPF "  --address \"$ipaddress\"\n"; 
        close(TEMPF);
        # Default object para LDAP cache ...
        if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
 	    $obj_descrip=$in{descrip};
            $dnldap = "default";
            $dn_cache=$dn;
            $ldapcache_server=1;
            &connect_cache ();
            $default_obj="cn=$config{ldapcache_defaultobj}";
            @tta3tier=&set_tta3tier($default_obj,$dn_cache,$dnldap,"mod","host");
            &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)) { 
           @listhosts=&list_hosts();
           @list__hosts = sort {$a->{'name'} cmp $b->{'name'}} @listhosts;
           &lock_file($config{'hosts_list'});
           open(FILEHOSTS, ">$config{'hosts_list'}");
              foreach $hosts (@list__hosts) {
                   if ( $hosts->{'alias'} ne $hostname) {
                        $lin_hosts=$hosts->{'alias'}."::".$hosts->{'name'}."::".$hosts->{'grpalias'}."::".$hosts->{'obj'}."::".$hosts->{'ens'}."::".$hosts->{'ipname'}."::".$hosts->{'maxsess'}."::".$hosts->{'descrip'}."::\n";
                        print FILEHOSTS "$lin_hosts";
                   }
              }
              $lin_hosts=$aliashosts."::".$hostname."::".$usergrphosts."::"."$dn"."::"."$ens"."::".$ipaddress."::".$maxsess."::".$descrip."::";
              print FILEHOSTS "$lin_hosts\n";
           close (FILEHOSTS);
           $str=$text{'user_update'}.$hostname." ->  ".$in{userhosts;
           &additional_log('modhosts.cgi', undef, $str);
           &webmin_log($config{'hosts_list'}, undef, "write");
           &unlock_file($config{'hosts_list'});
           print "<tr><p><b>".$text{'user_update'}.$hostname." ->  ".$in{userhosts}."</b></tr>\n";
           print "<tr></tr>\n";
#           print "<tr><form METHOD=POST name=modhosts ACTION=\"tta_modhosts.cgi?hostname=$hostname&userhosts=$userhosts\" >\n"; 
#           print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"modhosts\"\n";
#           print "  <input TYPE=\"hidden\" NAME=\"hostname\" VALUE=\"",$hostname,"\"\n";
#           print "  <input TYPE=\"hidden\" NAME=\"ipaddress\" VALUE=\"",$ipaddress,"\"\n";
#           print "  <input TYPE=\"hidden\" NAME=\"maxsess\" VALUE=\"",$maxsess,"\"\n";
#           print "  <input TYPE=\"hidden\" NAME=\"descrip\" VALUE=\"",$descrip,"\">\n";
#           print "  <input TYPE=\"hidden\" NAME=\"location\" VALUE=\"",$location,"\">\n";
#           print "  <input TYPE=\"hidden\" NAME=\"ntdomain\" VALUE=\"",$ntdomain,"\">\n";
#           print "  <input TYPE=\"hidden\" NAME=\"aliashosts\" VALUE=\"",$aliashosts,"\">\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;

