#!/usr/bin/perl use strict; use warnings; use Net::LDAP; use Text::CSV; use Net::LDAP::E#ntry; use File::Basename; use File::chmod; use Config::Tiny; use File::Copy; use Text::Trim; use Data::Dumper qw(Dumper); use Net::LDAP::Util qw(ldap_error_text); use Net::LDAP::Constant; my $config = Config::Tiny->read('config.ini'); #Variable Declaration section my ($bindhost,$port,$bindpwd,$binddn,$base_search,$ldap,$customerCode,$logDir,$entry,$result,$csv,$file,$line,$data,$cn,$dn,$entry2,$start_timestamp,$new,$u,$ct,$old,$logfile,$max,$stop_timestamp); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $start_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec); foreach my $section (keys %{$config}) { #LDAP Binding Connectivity variables declaration $bindhost = $config->{$section}->{'ldap_host'}; $port = $config->{$section}->{'ldap_port'}; $bindpwd = $config->{$section}->{'ldap_password'}; $binddn = $config->{$section}->{'ldap_user'}; $base_search = $config->{$section}->{'ldap_customers_ou_dn'}; $logDir = $config->{$section}->{'log_dir'}; # connect to the ldap server my $ldap = Net::LDAP->new($bindhost,port=>$port,timeout=>240) or die "Could not bind to ldap server: $! - $@\n"; $result = $ldap->bind ( dn => trim($binddn), password=>trim($bindpwd) ); #Open Script directory over here opendir(DIR, "."); my @files = grep(/\.csv$/,readdir(DIR)); closedir(DIR); $csv = Text::CSV->new({ sep_char => ',' }); #print "\n Script starts processing for the timings $start_timestamp"; #Visit each .csv file by checking its naming convention over here my $fileCt = 0; if($file=$ARGV[0]){ print "\n Script starts processing for the timings $start_timestamp"; $ct = 1; open($data, '<', $file) or die "Could not open given file \n"; open($logfile, '>>', 'logfile.txt'); print $logfile "Script started running for file $file at ".$start_timestamp."\n"; close $logfile; while ($line = <$data>){ if ($csv->parse($line)) { my @fields = $csv->fields(); $customerCode = $fields[0]; $result = $ldap->search( base => "$base_search", filter => "(&(customerCode=$customerCode))", ); die ldap_error_text($result->code) if $result->code; $max = $result->count; if($max == 0) { open($logfile, '>>', 'logfile.txt'); print $logfile "This customerCode $customerCode was not found in LDAP and was not reset\n"; close $logfile } else { open($logfile, '>>', 'logfile.txt'); print $logfile "This customerCode $customerCode was found in LDAP and is reset\n"; close $logfile } for (my $index = 0 ; $index < $max ; $index++) { my $entry = $result->entry($index); $u = ${$entry->get('uid')}[0]; $dn = "uid=$u,$base_search"; } my @all = (); @all = trim($result->entries); foreach $entry (@all){} $entry = Net::LDAP::Entry->new; $entry->dn($dn); $entry->replace( 'cn' => " ", 'userPassword'=> "", 'challengeQuestion'=> "", 'challengeAnswer'=> "", 'ctscPasswordCreationDate'=> "", 'ctscPasswordExpirationDate'=> "", 'ctscPasswordHistory'=> "", 'ctscPasswordResetAttempts'=> "", 'ctscPasswordLockoutEnable'=> "", 'ctscLastResetDate'=> "", 'ctscFailedLoginCount'=> "", ); $entry->update ($ldap); $old = ${$entry->get('cn')}[0]; $old = ${$entry->get('userPassword')}[0]; $old = ${$entry->get('challengeQuestion')}[0]; $old = ${$entry->get('challengeAnswer')}[0]; $old = ${$entry->get('ctscPasswordCreationDate')}[0]; $old = ${$entry->get('ctscPasswordExpirationDate')}[0]; $old = ${$entry->get('ctscPasswordHistory')}[0]; $old = ${$entry->get('ctscPasswordResetAttempts')}[0]; $old = ${$entry->get('ctscPasswordLockoutEnable')}[0]; $old = ${$entry->get('ctscLastResetDate')}[0]; $old = ${$entry->get('ctscFailedLoginCount')}[0]; $entry2 = $entry->clone; # copies entry $ldap->modify($dn, replace => {'cn' => "" }); $ldap->modify($dn, replace => {'userPassword' => "" }); $ldap->modify($dn, replace => {'challengeQuestion' => "" }); $ldap->modify($dn, replace => {'challengeAnswer' => "" }); $ldap->modify($dn, replace => {'ctscPasswordCreationDate' => "" }); $ldap->modify($dn, replace => {'ctscPasswordExpirationDate' => "" }); $ldap->modify($dn, replace => {'ctscPasswordHistory' => "" }); $ldap->modify($dn, replace => {'ctscPasswordResetAttempts' => "" }); $ldap->modify($dn, replace => {'ctscPasswordLockoutEnable' => "" }); $ldap->modify($dn, replace => {'ctscLastResetDate' => "" }); $ldap->modify($dn, replace => {'ctscFailedLoginCount' => "" }); } else { warn "Line could not be parsed: $line\n"; } $ct++; } #End while loop my ($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1)=localtime(time); $stop_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",$year1+1900,$mon1+1,$mday1,$hour1,$min1,$sec1); print "\n Script ends Here for the timings - $stop_timestamp "; open($logfile, '>>', 'logfile.txt'); print $logfile "Processing stopped at ".$stop_timestamp."\n"; close $logfile; close $data; } #if file pattern checking loop ends else { print "\n Please provide a .csv file as an input"; } }