#!/usr/bin/perl
# passwd.cgi
# 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" ) {
      do "ldap-lib.pl";
 }
&ReadParse();

$dn = $in{dn};
$filter = $in{filter};
$base = $in{base};
$username=$in{username};
$password=$in{password};
if ($username eq "" ) { &redirect("tta_selusr.cgi?form=passwd"); } 
if (!$dn) { &redirect("tta_selusr.cgi?form=passwd"); } 
if ( $dn ne "-" && !$password) { &redirect("tta_selusr.cgi?form=passwd"); } 
 &headerstta($text{'index_passwd'}, "", "passwd",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );
 print "<hr><p>\n"; 
 print "<p>\n";
 print "<table width=100% border>\n";
 if ( "$username" eq "*" ) {
     $dn="*";
     print "<tr $tb> <td><font color='$revtext'><b> $text{'titidx_passwd'} -- [ $base - $filter ] </b></td> </tr>\n";

 } else {
     %h_ou=&list_ous();
     $organizationname="";
     $ens_ou="";
     $aliasou=$in{userou};
     if ( $h_ou{$aliasou}->{'alias'} ne $aliasou ) {
          $err_msg=$text{'list_err'};
          &footHere;
          exit;
     }
     $dn_ou=$h_ou{$aliasou}->{'obj'};
     $organizationname=$h_ou{$aliasou}->{'name'};
     $ens_ou=$h_ou{$aliasou}->{'ens'};
     print "<tr $tb> <td><font color='$revtext'><b> $text{'titidx_passwd'} -- $username ($organizationname)</b></td> </tr>\n";
 }
 print "<tr $cb> <td><table width=100%>\n";
 if ($dn eq "-" ) {
      if ( $in{syncuser} eq "true" ) { &make_remote_user("modify",$username); }
      &footHere;
      exit;
 } 

$i=0;
@all_users=();
%h_ou=&list_ous(2);

sub callbackSearch 
{
   my ( $conn, $entry) = @_;   

   if ( !defined($entry) ) { return };

   $dn = $entry->dn;
 
   @attrs = $entry->attributes; # Obtain attributes for this entry.
   my (%all_users);
   $all_users{dn} = $dn;
   foreach my $var (@attrs) {
        $attr = $entry->get_value( $var, asref => 1 );
        if ( defined($attr) ) {          
             foreach my $value (@$attr) { $all_users{$var} = $value; } 
        }                                                                       
   }
   $dn_ou=$dn;
   $dn_ou=~s/,$config{ldap_base}//g;
   $i_cn = $all_users{cn};
   $dn_ou=~s/cn=$i_cn,//g;
   if ( $h_ou{$dn_ou}->{'obj'} eq $dn_ou ) {
          $all_users{'grpalias'}=$h_ou{$dn_ou}->{'alias'};
          $all_users{'ens'}=$h_ou{$dn_ou}->{'ens'}."/cn=".$i_cn;
   } else {
          $all_users{'grpalias'}=$dn_ou;
   }
   $all_users[$i++] = \%all_users;
   $conn->pop_entry; 
}
if ( $username eq "*" && $config{'has_ldapusers'} eq "true" && $ldap_server ne "0" ) {
    if ( $base ) { 
       $base="$base,$config{ldap_dnusers}";
    } else {
       $base=$config{ldap_dnusers};
    }
    $ldap_server=1;
    &connect ();
    %h_ou=&list_ous(2);
    my $status_con = $ldapObj->search ( base => $base,
                                        scope => "sub",
                                        filter => "(cn=" . $filter . ")",
                                        callback => \&callbackSearch
                                       );
    if ( $conn->code ) {
       if ($conn->count == 0) {  # if conncache is not defined we have nothing.
           print $text{msg_1} . "\n";   }
    }
}
if ( $username ne "*" ) {
   $all_users{dn} = $dn;
   $all_users{cn} = $username;
   $dn_ou=$dn;
   $i_cn=$username;
   $dn_ou=~s/,$config{ldap_base}//g;
   $dn_ou=~s/cn=$i_cn,//g;
   if ( $h_ou{$dn_ou}->{'obj'} eq $dn_ou ) {
          $all_users{'grpalias'}=$h_ou{$dn_ou}->{'alias'};
          $all_users{'ens'}=$h_ou{$dn_ou}->{'ens'}."/cn=".$i_cn;
   } else {
          $all_users{'grpalias'}=$dn_ou;
   }
   $all_users[$i++] = \%all_users;
}

%h_ou=&list_ous();
$i = 0;
$i_ou ="";
$lastgrp="";
foreach $user (@all_users) {
     $i_dn = $user->{dn};
     $i_cn = $user->{cn};
     $i_ou = $user->{grpalias};
     $surname = $user->{surname};
     $i_sysuser= "";
     if ( $i_dn eq "-" ) { $i_sysuser=$i_cn; }
     $user_line = "";
     $user_line .= " $user->{cn}"; 
     $user_line .= " $user->{sn}";
     $user_line .= " $user->{description}";
     $user_line .= "[".$h_ou{$i_ou}->{'name'}."] ";
#     $user_line .= "<b>DN:</b> $user->{dn}";
     $i++;
     $dn=$i_dn;
     $username=$i_cn;
     $ldap_server=1;
     &connect ();
     if ( &mod_usr_password() eq 0 ) {
             if ( $config{ldap2_server}.length gt 0 ) {
                  $ldap_server=2;
                  &connect ();
                  if ( &mod_usr_password() ne 0 ) { &err_replication() }
             }
             if ( $config{'ldap_log_name'} ne "" ) {
                  $lin_user="mod::".$username."::".$password."::".$surname."::".$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'});
             }
             $str=$text{'user_update'}.$username." ($surname) ->  ".$dn;
             &additional_log('passwd.cgi', "passwd", $str);
             &webmin_log($config{'ldap_log_name'}, "passwd", "modify",$lin_user);
             print "<tr><td><b>$text{'user_update'}... LDAP </b> --> $user_line </td></tr>\n";
      }
 }
 print "<tr></tr>\n";
 print "<p><tr><td><b> ".&text('l_total',$i)."</b></td><td><p>\n";
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
