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

 require './stta-lib.pl';
&ReadParse();
if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) {
     do "ldap-lib.pl";
}

&headerstta($text{'index_modhosts'}, "", "modhosts",undef,1,1,$text{headpage_rigth} );
print "<hr><p>\n"; 
&foreign_require("proc", "proc-lib.pl");

 $type_cmd = $in{ttacmd};
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_modhosts'}</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};
 $portalive=$in{portalive};
 $aliashosts=$in{aliashosts};
 $usergrphosts=$in{usergrphosts};

 if ($hostname)  {
    $alias=$aliashosts;
    %h_hosts=&list_hosts();
    if ( $h_hosts{$aliashosts}->{'alias'} ne $aliashosts ) {
         $err_msg=$text{'list_errno'};
         &footHere;
         exit;
    }
    $grp_name="";
    $dn_host="";
    $ens_host="";
    $dn_host=$h_hosts{$aliashosts}->{'obj'};
    $ens_host=$h_hosts{$aliashosts}->{'ens'};
    %h_grphosts=&list_grphosts();
    $grp_name=$h_grphosts{$usergrphosts}->{'name'};
    if ( $dn_host eq "" ) {
       $dn_org = "ou=$hostname";
       $ens_org = "cn=$hostname";
    } else {
       $dn_org = $dn_host;
       $ens_org = $ens_host;
    }
        $ens_obj = "--name \"".$config{tta_base}."/$ens_org\"";
        $| = 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_org;
            $task="mod";
            $ldapcache_server=1;
            &connect_cache ();
            $default_obj="cn=$config{ldapcache_defaultobj}";
            $dn_wk=$default_obj.",".$dn_cache.",".$config{ldapcache_base};
            $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
            if ( !defined($cur_entry) || $cur_entry->dn ne $dn_wk) { $task="add"; };
            @tta3tier=&set_tta3tier($default_obj,$dn_cache,$dnldap,$task,"host");
            if ( &cache_obj(\%tta3tier) eq 0 ) {
                 if ( $config{ldapcache2_server}.length gt 0 ) {
                      $ldapcache_server=2;
                      &connect_cache ();
                      if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
                 }
            } else { &html_errshow("ldap")};
        } 
        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
           delete $h_hosts{$aliashosts};
           foreach $item (keys (%h_hosts)) { push (@listhosts, $h_hosts{$item}); }
           @list__hosts = sort {$a->{'name'} cmp $b->{'name'}} @listhosts;
           &lock_file($config{'hosts_list'});
           open(FILEHOSTS, ">$config{'hosts_list'}");
              foreach $hosts (@list__hosts) {
                      $lin_hosts=$hosts->{'alias'}."::".$hosts->{'name'}."::".
                                 $hosts->{'grpalias'}."::".$hosts->{'obj'}."::".
                                 $hosts->{'ens'}."::".$hosts->{'ipname'}."::".
                                 $hosts->{'maxsess'}."::".$hosts->{'portalive'}."::".
                                 $hosts->{'descrip'}."::\n";
                      print FILEHOSTS "$lin_hosts";
              }
              $lin_hosts=$aliashosts."::".$hostname."::".$usergrphosts."::"."$dn_org"."::"."$ens_org"."::".$ipaddress."::".$maxsess."::".$portalive."::".$descrip."::";
              print FILEHOSTS "$lin_hosts\n";
           close (FILEHOSTS);
           $str=$text{'user_update'}.$hostname." ->  ".$aliashosts;
           &additional_log('modhosts.cgi', "host", $str);
           &unlock_file($config{'hosts_list'});
           &webmin_log($config{'hosts_list'}, "host", "update",$lin_hosts);
           print "<tr><p><b>".$text{'user_update'}.$hostname." ->  ".$grp_name."</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"; 
        } else { &html_errshow("tta")};
	if ( $debug lt 2 )  { 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;

