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


require './tarantella-lib.pl';
do "ldap-lib.pl";
 
&header($text{'index_moddou'}, "", "modou");
print "<hr><p>\n"; 
&foreign_require("proc", "proc-lib.pl");
$ldap_server=1;
&connect ();
&ReadParse();

$dn = $in{dn};
$type_cmd = $in{ttacmd};
$ouname=$in{ouname};
$maxsess=$in{maxsess};
$aliasou=$in{userorg};
if (!$dn) {
   print "<hr><p>\n";
   &footer($config{'back_return'},$text{'index'});
   print "<p><p><p>\n";
   do "footer.pl";
   exit;
}

 print "<p>\n";
 print "<table width=100% border>\n";
 if ($ouname && $dn)  {
   
    $organizationname="";
    $ens_ou="";
    $dn_ou="";
    foreach $ou (&list_ous()) {
         if ( $ou->{'alias'} eq $aliasou) {
            $ens_ou=$ou->{ens};
            $dn_ou=$ou->{ens};
            $organizationname=$ou->{name};
            last;
         }
    }
    print "<tr $tb> <td><b>".&text('titidx_modou',$ouname,$organizationname)."</b></td> </tr>\n";
    print "<tr $cb> <td><table width=100%>\n";
    print "<center>\n";

    if ( &mod_ou() eq 0 ) { 

        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &mod_ou() ne 0 ) { &err_replication() }
        }
      	$| = 1;
        print "<tr></tr><tr></tr>\n";
        print "<tr><p></p><p><b>".$text{'user_update'}."... LDAP ... </b></tr>\n";
        print "<tr><td><pre>\n";
        print "<tr><td>$ouname\n";
        print "<tr><td>$ens_obj\n";
        $ens_obj = "--name \"".$config{tta_base}."/$ens_ou\"";

        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "$ens_obj \\\n";
                print TEMPF "  --description \"$in{descrip}\"\\\n";
                foreach $w (&list_webtops()) {
                      if ( $w->{'alias'} eq $in{webtop}) {
                          print TEMPF "  --webtop  \"$w->{'obj'}\"\\\n";
                      }
                }
                if ( $in{inherit} ne "" ) {print TEMPF "  --inherit  \"$in{inherit}\"\\\n";}
                if ( $in{conntype} ne "" ) {
                       print TEMPF "  --conntype \"$in{conntype}\"\\\n";
                }
                print TEMPF "  --cdm  \"$in{cdm}\"  \n";

        close(TEMPF);
        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
            @listous=&list_ous();
            @list__ous = sort {$a->{'name'} cmp $b->{'name'}} @listous;
            &lock_file($config{'ou_list'});
            open(FILEOUS, ">$config{'ou_list'}");
                foreach $ou (@list__ous) {
                    if ( $ou->{'alias'} ne $userou) {
                         $lin_ou=$ou->{'alias'}."::".$ou->{'name'}."::".$ou->{'obj'}."::".$ou->{'ens'}."::".$ou->{'maxsess'}."::\n";
                         print FILEOUS "$lin_ou";
                    }
                }
                $lin_ou=$aliasou."::".$ouname."::"."ou=$ouname,$dn_ou"."::"."$ens_ou/ou=$ouname"."::".$maxsess."::";
                print FILEOUS "$lin_ou";
            close (FILEOUS);
            print "</pre><tr><p><b>$text{'user_update'}... Tarantella </b></tr>\n";
            $str=$text{'user_update'}.$ouname." ->  ".$in{userou};
            &additional_log('moddou.cgi', undef, $str);
            &webmin_log($config{'ou_list'}, undef, "modify");
            &unlock_file($config{'ou_list'});
        }
        print "</tr></td></pre>\n";
   }
 }
   print "<tr></tr>\n";
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
