#!/usr/bin/perl
# addou.cgi
# Add Ou in STTA
# 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';
if ( $config{'has_ldapusers'} eq "true" || $config{'is_ldapcache'} eq "true") {
     do "ldap-lib.pl";
}

&headerstta($text{'index_addou'}, "", "addou",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();

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

 if ( !$ouname || $ouname eq "" )  {
         &footHere;
         exit;
 }
    %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\"";
 if ( $config{'has_ldapusers'} eq "true" ) {
    $ldap_server=1;
    &connect ();
    $dn = "$dnou,".$config{ldap_base};
    if ( &add_ou($dn) eq 0 ) { 
        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &add_ou($dn) ne 0 ) { &err_replication() }
        }
    } else { &html_errshow("ldap")};
  }
    $| = 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 ( $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")};
    $lin_ou=$aliasou."::".$ouname."::"."$dnou"."::"."$ens"."::".$maxsess."::".$ouname."::";
    &out_objslist( "add", "add.cgi", "ou", "ou_list", $aliasou, $lin_ou, undef );

    if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
             $dnldap = "default";
             $dn_org="ou=$ouname";
             if ( $dn_ou ne "" ) { $dn_org .=",$dn_ou"; }
             $default_obj="cn=$config{ldapcache_defaultobj}";
             $ldapcache_server=1;
             &connect_cache ();
             @tta3tier=&set_tta3tier($default_obj,$dn_org,$dnldap,"add","ou");
             if ( &cache_obj(\%tta3tier) ne 0 ) { 
                  &html_errshow("ldap");
                  &footHere; exit;
             };
             if ( $config{ldapcache2_server}.length gt 0 ) {
                      $ldapcache_server=2;
                      &connect_cache ();
                      if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
             }
        }
    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"; 
    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;
