#!/usr/bin/perl
# ldap_loglist.cgi
# Select 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';
$postform="ldap_loglist";
if ( $config{'has_ldapusers'} ne "true" || $config{'ldap_log_name'} eq "" ) {
     &headerstta($text{'index_ldaploglist'}, "", $postform,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","" );
     $err_msg=$text{'ldap_log_err'};
     &footHere;
     exit;
}
do "ldap-lib.pl";
$has_ldapusers="";  
if ( $ldap_ok ne true && $config{has_ldapusers} eq "true" ){ 
     $has_ldapusers=$config{'has_ldapusers'}; 
     $config{'has_ldapusers'}="false"; 
}
&ReadParse();
if ($in{form}) { $postform = $in{form}; }
if ( !$in{filter} ) { &redirect("tta_filter.cgi?form=$postform"); }
if ( !$in{outscreen} && !$in{file} ) { &redirect("tta_filter.cgi?form=$postform"); }
$toScreen=$in{outscreen} || "";
$fileName=$in{file} || "" ;
$now = time();
$filter=$in{filter};
#    $filter = "(cn=*)";

$ldap_server=$in{ldapserver};
$sysuser=$in{sysuser};
$syncuser=$in{syncuser};

$base=$in{base};
if ( $base ) { 
   $base="$base,$config{ldap_dnusers}";
} else {
   $base=$config{ldap_dnusers};
};
$sort_on = ($in{sort_on}) ? $in{sort_on} : "sn";

&headerstta($text{'index_ldaploglist'}, "", $postform,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";
 print "<tr $tb> <td valig=top><font color='$revtext'><b> $text{'titidx_ldaploglist'}".&set_now($now)."</b></td></tr>";
print "<tr><td>";
$i=0;
@all_users=();

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};
#   @line=split(/,/, $dn_ou, -1);
#   $dn_ou=~s/$line[0],//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;
   $conn->pop_entry; 
}
if ( $config{'has_ldapusers'} eq "true" && $ldap_server ne "0" ) {
    &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";   }
    } else {
       print "<B>-- $text{'f_ldapserver'}  ";
       if ( "$ldap_server" eq "1" ) {
            print $config{ldap_servertitle}."  (".$config{ldap_server}.") ";
       } elsif ( "$ldap_server" eq "2" ) {
            print $config{ldap2_servertitle}."  (".$config{ldap2_server}.") ";
       }
    }
}
if ( $config{'has_systemusers'} eq "true" && $sysuser eq "true" ) {
   &foreign_require("useradmin", "user-lib.pl");
   @ulist=&foreign_call("useradmin", "list_users");
   %h_ulist=();
   foreach $item (@ulist) { $h_ulist{$item->{user}} = $item  }
   %h_sysusers=&list_sysusers();
   foreach $item (keys (%h_sysusers)) {
        if ( $filter eq "*" ||  $h_sysusers{$item}->{name} =~ /$filter/ ) {
             $all_users[$i]{dn} = "-";
             $all_users[$i]{cn} = $h_sysusers{$item}->{name};
             $all_users[$i]{sn} = $h_ulist{$item}{real};
             $all_users[$i]{grpalias} = $h_sysusers{$item}->{grpalias};
             $all_users[$i]{ens} = $h_sysusers{$item}->{ens};
             $all_users[$i]{mail} = $h_sysusers{$item}->{email};
             $all_users[$i]{description} = "uid=".$h_ulist{$item}{uid}." ".$h_sysusers{$item}->{where};
             $i++;
       }
   }
}
if ($sort_on ne "browse") { print "<B>-- $text{'l_orderby'}  "; }
if ($sort_on eq "browse") {
       @users = sort {$a->{ens} cmp $b->{ens}} @all_users;
       print $text{browse} . "\n";
    }
elsif ($sort_on eq "mail") {
       @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
       print $text{l_sn} . "\n";
    }
elsif ($sort_on eq "sn") {
	    @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
       print $text{l_uid} . "\n";
    }
elsif ($sort_on eq "cn") {
       @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
       print $text{l_cn} . "\n";
    }
else {
       @users = sort {($a->{description} . $a->{cn}) cmp
       ($b->{description} . $b->{cn})} @all_users;
       print $text{l_department} . "\n";
}
print "</tr><tr ><td><table width=100%>\n";
sub err_file_open
{
 $err_msg="$text{f_error} $fileName";
 &footHere;
 exit;
}

$ReportOut="";
if ( $toScreen ne 1 ) {
     $ReportOut=$fileName;
     open (REPORTOUT,">$ReportOut") || &err_file_open();
}
if ( $ReportOut ) { select(REPORTOUT)}
$i = 0;
foreach $user (@users) {
     $i_dn = $user->{dn};
     $i_sysuser= "";
     if ( $i_dn eq "-" ) {
             $i_sysuser=$i_cn;
     }
     print "list::".$user->{cn}."::".$user->{password}."::".$user->{sn}."::".$i_dn."\n";
     if ( $toScreen eq 1 ) { print "<br>"; }
     $i++;
}
 if ( $toScreen ne 1 ) { close(REPORTOUT); }
 if ( $ReportOut ) { $| =1; select(STDOUT);}
 print "<tr $cb> <td align=center>$text{user_made} $fileName </a></td></tr>\n";
 print "<tr><tr></table></table><p>\n";
 print "<p><p><p>\n";
 if ( $has_ldapusers ne "" ) { $config{'has_ldapusers'}= $has_ldapusers } 
 &footer($config{'back_return'},$text{'index'});
 do "footer.pl";
 exit;
