#!/usr/bin/perl
# tta_delusr.cgi
# Delete Users to Services with Tarantella
# 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';
&foreign_require("proc", "proc-lib.pl");
if ( $config{'has_ldapusers'} eq "true" || $config{'is_ldapcache'} eq "true") {
     do "ldap-lib.pl";
}
&ReadParse();

 $dn = $in{dn};
 if (!$dn) { &redirect("tta_selusr.cgi?form=delusr"); }
 $username=$in{username};
 $sysuser=$in{sysuser};
 if ( $sysuser ne ""  && $dn eq "-" ) { 
      $username=$sysuser;
      $dn="";
 }
 $userou=$in{userou};
 &headerstta($text{'index_delusr'}, "", "delusr",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
 print "<hr><p>\n"; 
 print "<table width=100% border>\n";
 %h_ou=&list_ous();
 print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_delusr'} $username (".$h_ou{$userou}->{name}.") </b></td> </tr>\n";
 if ( $h_ou{$userou}->{'alias'} ne $userou || $userou eq "" ) {
      $err_msg=$text{'list_errno'};
      &footHere;
      exit;
 }
 $dn_ou=$h_ou{$userou}->{'obj'};
 $organizationname=$h_ou{$userou}->{'name'};
 $ens_ou=$h_ou{$userou}->{'ens'};
 print "<tr $cb> <td><table width=100%>\n";
 if ( $config{'has_ldapusers'} eq "true" && $dn ) {
    $ldap_server=1;
    &connect ();
    if ( &del_usr() eq 0 ) { 
        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &del_usr() ne 0 ) { &err_replication() }
        }
        if ( $config{'ldap_log_name'} ne "" ) {
             $lin_user="del::".$username."::::::".$dn; 
             &lock_file($config{'ldap_log_name'});
             open(FILELDAPLOG, ">>$config{'ldap_log_name'}");
                  print FILELDAPLOG "$lin_user\n";
             close (FILELDAPLOG);
             &unlock_file($config{'ldap_log_name'});
             &remote_file("write",$config{ldap_log_name},"ldap_log_name");
        }
        $str=$text{'user_delete'}.$username." ->  ".$dn;
        &additional_log('delusr.cgi', "usr", $str);
        &webmin_log($config{'ldap_log_name'}, "usr", "delete");
        print "<tr></tr><tr></tr>\n";
        print "<tr $cb><td><b>".$text{'user_delete'}."... LDAP ... </b></td></tr>\n";
    } else { &html_errshow("ldap")};
  }
 if ( $config{'has_systemusers'} ne "true") { $sysuser="" }
 print "<tr $cb><td><pre>\n";
 if ( $username ) {
      if ( $ens_ou eq "" ) {
            $ensobj = "cn=$username";
      } else {
            $ensobj = "$ens_ou/cn=$username";
      }
      $ens_obj = "--name \"".$config{tta_base}."/$ensobj\"";
      $type_cmd="delete";
      if (&run_tta_cmd("delete",$ens_obj)) { 
          print "</pre><tr $cb><td><b>$text{'user_delete'}... Tarantella </td></tr>\n";
      } else { &html_errshow("tta")};
 }
 if ( $sysuser ne "" ) {
            %h_sysusers=&list_sysusers();
            $lin_sysuser=$sysuser."::".$userou."::".$ensobj."::".$maxsess."::".$in{email}."::".$where."::";
            &out_objslist( "delete", "delusr.cgi", "sysusers", "sysusers_list", $sysuser, $lin_sysuser, \%h_sysusers );
            # Try to delete in remote OS ...
            if ( $in{syncuser} eq "true" ) {  &make_remote_user("delete",$sysuser); }
            # Try to delete in local OS ...
            &foreign_require("useradmin", "user-lib.pl");
            @ulist=&foreign_call("useradmin", "list_users");
            %h_ulist=();
            foreach $item (@ulist) { $h_ulist{$item->{user}} = $item  }
            &foreign_call("useradmin", "delete_user",$h_ulist{$sysuser});
            &webmin_log("$sysuser", "local_user_del","$sysuser",$h_ulist{$sysuser});
            if ( $debug gt 1 ) { print "<tr><td>$text{user_delete} ($sysuser)"; }
 }
 if ( $config{'is_ldapcache'} eq "true" ) { 
       if ( $sysuser ne "" ) {
                  $dn_org = "ou=$sysuser";
       } else {
                  $dn_org = "ou=$username";
       }
       if ( $dn_ou ne "" ) { $dn_org .= ",$dn_ou"; }
       $ldapcache_server=1;
       &connect_cache ();
       $dn = "$dn_org,".$config{ldapcache_base};
       if ( &del_ldap_obj($dn) eq 0 ) {
            print "<tr><b>".$text{'user_delete'}."... LDAP CACHE ... </b></tr>\n";
       } else { &html_errshow("ldap")};
 }
 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;
