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

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


$dn = $in{dn};
$type_cmd = $in{ttacmd};
$username=$in{username};
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 ($username && $dn)  {
   
    $organizationname="";
    $ens_ou="";
    foreach $ou (&list_ous()) {
         if ( $ou->{'alias'} eq $in{userorg}) {
            $ens_ou=$ou->{ens};
            $organizationname=$ou->{name};
            last;
         }
    }
    print "<tr $tb> <td><b>".&text('titidx_modusr',$username,$organizationname)."</b></td> </tr>\n";
    print "<tr $cb> <td><table width=100%>\n";
    print "<center>\n";

    if ( &mod_usr() eq 0 ) { 

        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &mod_usr() ne 0 ) { &err_replication() }
        }

        $lin_user="mod::".$username."::".$password."::".$in{surname}."::".$dn;
        &lock_file($config{'ldap_log'});            
        open(FILELDAPLOG, ">>$config{'ldap_log'}");
        print FILELDAPLOG "$lin_user\n";
        close (FILELDAPLOG);
        $str=$text{'user_update'}.$username." ->  ".$dn;
        &additional_log('modusr.cgi', undef, $str);
        &webmin_log($config{'ldap_log'}, undef, "modify");
        &unlock_file($config{'ldap_log'});

        $| = 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";
        $ens_obj = "--name \"".$config{tta_base}."/$ens_ou/cn=$username\"";
 

        $temp_f = &tempname();
        $shared=$in{shared};
        if ( $shared ne "false" && $shared ne "true" ) { $shared="false" }
        open(TEMPF, ">$temp_f");
                print TEMPF "$ens_obj \\\n";
                print TEMPF "  --surname \"$in{surname}\" \\\n";
                print TEMPF "  --description \"$in{descrip}\"\\\n";
                print TEMPF "  --user  \"$in{user}\"\\\n";
                print TEMPF "  --email  \"$in{email}\"\\\n";
                if ( $in{ntdomain} ne ""  ) {
                    print TEMPF "  --ntdomain \"$in{ntdomain}\"\\\n";
                }
                foreach $w (&list_webtops()) {
                      if ( $w->{'alias'} eq $in{webtop}) {
                          print TEMPF "  --webtop  \"$w->{'obj'}\"\\\n";
                      }
                }
                print TEMPF "  --inherit  \"$in{inherit}\"\\\n";
                print TEMPF "  --shared  \"$shared\"\\\n";
                print TEMPF "  --enabled  \"$in{enabled}\"\\\n";
                if ( $conntype ne "" ) {
                       print TEMPF "  --conntype \"$in{conntype}\"\\\n";
                }
                if ( $in{bandwidth} gt 0 ) {
                      print TEMPF "  --bandwidth \"$in{bandwidth}\"\\\n";
                }
                print TEMPF "  --cdm  \"$in{cdm}\"  \n";

        close(TEMPF);

        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
            print "</pre><tr><p><b>$text{'user_update'}... Tarantella </b></tr>\n";
        }
        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;
