#!/usr/bin/perl
# addhosts.cgi
# ...  Services with Tarantella.
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

 require './stta-lib.pl';

&headerstta($text{'index_addhosts'}, "", "addhosts",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
print "<hr><p>\n"; 
&foreign_require("proc", "proc-lib.pl");
&ReadParse();
if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
     do "ldap-lib.pl";
}

 $type_cmd = $in{ttacmd};
 $tta_norun = $in{ttanorun};
 print "<table width=100% border>\n";
 print "<tr $tb> <td><font color='$revtext'><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};
 $portalive=$in{portalive};
 $aliashosts=$in{aliashosts};
 $usergrphosts=$in{usergrphosts};

 if ( ! $hostname)  { &footHere; exit; }

    $alias=$hostname;
    %h_host=&list_hosts();
    if ( $h_host{$hostname}->{'alias'} eq $hostname ) {
         $err_msg="$hostname: ".$text{'list_err'};
         &footHere;
         exit;
    }
    $grp_name="";
    $dn_hosts="";
    $ens_hosts="";
    $grp_descrip="";
    %h_grphosts=&list_grphosts();
    if ( $h_grphosts{$usergrphosts}->{'alias'} eq $usergrphosts ) {
         $dn_hosts=$h_grphosts{$usergrphosts}->{obj};
         $ens_hosts=$h_grphosts{$usergrphosts}->{ens};
         $grp_name=$h_grphosts{$usergrphosts}->{name};
         $grp_descrip=$h_grphosts{$usergrphosts}->{descrip};
    } else {
         $err_msg="$usergrphosts :".$text{'list_errno'};
         &footHere;
         exit;
    }
    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\"";
    }
    $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";
    close(TEMP);
    $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);
    if ( $tta_norun eq "true" ) { $type_cmd = "no_run $type_cmd"; };
    if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
            print "</pre>\n";
    } else { &html_errshow("tta")};
    # 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,"add","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")};
    } 
    $lin_hosts=$aliashosts."::".$hostname."::".$usergrphosts."::"."$dn"."::"."$ens"."::".$ipaddress."::".$maxsess."::".$portalive."::".$descrip."::";
    &out_objslist( "add", "addhosts.cgi", "hosts", "hosts_list", $aliashosts, $lin_hosts, undef );
    print "<tr><p><b>".$text{'user_made'}.$hostname." ->  ".$usergrphosts."</b></tr>\n";
    print "<tr></tr>\n";
#   print "<tr><form METHOD=POST name=addhosts ACTION=\"tta_modhosts.cgi?hostname=$hostname&usergrphosts=$usergrphosts\" >\n"; 
#   print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"addhosts\"\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=\"portalive\" VALUE=\"",$portalive,"\"\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"; 
    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;

