#!/usr/bin/perl
# addou.cgi
# Add Ou in STTA
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

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

&headerstta($text{'index_addou'}, "", "addou",undef,1,1,$text{headpage_rigth} ); 
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};
 $maxsess=$in{maxsess};

 if ($ouname)  {
    %h_ou=&list_ous();
    if ( $h_ou{$aliasou}->{'alias'} eq $aliasou || $h_ou{$userou}->{'alias'} ne $userou ) {
         $err_msg=$text{'list_err'};
         &footHere;
         exit;
    }
    $dn_ou=$h_ou{$userou}->{'obj'};
    $organizationname=$h_ou{$userou}->{'name'};
    $ens_ou=$h_ou{$userou}->{'ens'};
    
    if ( $dn_ou eq "" ) {
       $dnou = "ou=$ouname";
       $ens = "ou=$ouname";
    } else {
       $dnou = "ou=$ouname,$dn_ou";
       $ens = "$ens_ou/ou=$ouname";
    }
    $ens_obj = "--name \"".$config{tta_base}."/$ens\"";

    $dn = "$dnou,".$config{ldap_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";
        $temp_f=$config{'tta_defaults_ou'}; 
       # Assigning and collecting values to be used in the form ...
        %h_ttaobj=&load_ttaobj();
        $cdm     =$h_ttaobj{'cdm'}->{'value'};
        $inherit =$h_ttaobj{'inherit'}->{'value'};
        $webtop  =$h_ttaobj{'webtop'}->{'value'};
        $conntype=$h_ttaobj{'conntype'}->{'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."::"."$dnou"."::"."$ens"."::".$maxsess."::".$ouname;
           &lock_file($config{'ou_list'});
	   open(FILEOUS, ">>$config{'ou_list'}");
           print FILEOUS "$lin_ou\n";
           close (FILEOUS);
           &unlock_file($config{'ou_list'});
           $str=$text{'user_made'}.$ouname." ->  ".$in{userou};
           &additional_log('addou.cgi', "ou", $str);
           &webmin_log($config{'ou_list'}, "ou", "write",$lin_ou);
           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\" >\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=\"",$aliasou,"\">\n";
           print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dnou,"\">\n";
           print "  <input TYPE=\"hidden\" NAME=\"maxsess\" VALUE=\"",$maxsess,"\">\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); }
    } else { &html_errshow("ldap")};
 }
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;

