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

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

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

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

 $ouname=$in{ouname};
 $userou=$in{userou};
 $aliasou=$in{aliasou};

 if ($ouname)  {
    $organizationname="";
    $dn_ou="";
    $ens_ou="";
    foreach $ou (&list_ous()) {
         if ( $ou->{'alias'} eq $userou ) {
            $dn_ou=$ou->{obj};
            $ens_ou=$ou->{ens};
            $organizationname=$ou->{name};
            last;
         }
    }
    $dn = "ou=$ouname,$dn_ou,".$config{ldap_base};
    $ens = "ou=$ouname,$ens_ou,".$config{tta_base};

    if ( &add_ou() eq 0 ) { 

        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &add_ou() ne 0 ) { &err_replication() }
        }
        $| = 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";
        $ens_obj = "--name \"".$config{tta_base}."/$ens_ou/ou=$ouname\"";
        $temp_f=$config{'tta_defaults_ou'}; 
        foreach $i (&load_ttaobj()) {
            if ( $i->{'property'} eq "cdm" ) { $cdm=$i->{'value'} };
            if ( $i->{'property'} eq "inherit" ) { $inherit=$i->{'value'} };
            if ( $i->{'property'} eq "webtop" ) { $webtop=$i->{'value'} };
            if ( $i->{'property'} eq "conntype" ) { $conntype=$i->{'value'} };
        }
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "$ens_obj\\\n";
                if ( $cdm ne "" ) {print TEMPF "  --cdm \"$cdm\"\\\n"; }
                if ( $inherit ne "" ) {print TEMPF "  --inherit \"$inherit\"\\\n"; }
                if ( $conntype ne "" ) { print TEMPF "  --conntype \"$conntype\"\\\n"; }
                if ( $webtop ne "" ) {print TEMPF "  --webtop \"$webtop\"\\\n"; }
                print TEMPF "  --description \"$in{ouname}\" \n";
        close(TEMPF);

        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
           $lin_ou=$aliasou."::".$ouname."::"."ou=$ouname,$dn_ou"."::"."$ens_ou/ou=$ouname";
           &lock_file($config{'ou_list'});
	   open(FILEOUS, ">>$config{'ou_list'}");
           print FILEOUS "$lin_ou\n";
           close (FILEOUS);
           $str=$text{'user_made'}.$ouname." ->  ".$in{userou};
           &additional_log('addou.cgi', undef, $str);
           &webmin_log($config{'ou_list'}, undef, "write");
           &unlock_file($config{'ou_list'});
           print "<tr><p><b>".$text{'user_made'}.$ouname." ->  ".$in{userou}."</b></tr>\n";
           print "<tr></tr>\n";
           print "<tr><form METHOD=POST name=addou ACTION=\"tta_modou.cgi?ouname=$ouname&userou=$userou\" >\n"; 
           print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"addou\"\n";
           print "  <input TYPE=\"hidden\" NAME=\"ouname\" VALUE=\"",$ouname,"\"\n";
           print "  <input TYPE=\"hidden\" NAME=\"userou\" VALUE=\"",$userou,"\">\n";
           print "  <input TYPE=\"hidden\" NAME=\"aliasou\" VALUE=\"",$aliasou,"\">\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;

