#Filezilla Inactive User Cleanup (FIUC)
#Copyright (c) 2011 Stergios Psathas stergios99@gmail.com
#This is free software, and you are welcome to redistribute it ,
#as long as copyright information remains intact.
#!/usr/bin/perl
#use warnings;
# use module
use XML::Simple;
use Data::Dumper;
use Time::Local;
use Date::Calc qw(Delta_Days);
$xml = new XML::Simple();
# read XML file
$data = $xml->XMLin("c:/program files/FileZilla Server/FileZilla Server.xml",
ForceArray => ['Permission','IpFilter','Allowed','Disallowed','IP'],
ForceContent => 0,KeepRoot =>1,
KeyAttr =>['Item'],
SuppressEmpty => 0 ); #<--------where to read the xml file
#print Dumper $data;
$i=0;
$count=0;
$countinactive=0;
foreach $user (@{$data->{'FileZillaServer'}->{'Users'}->{'User'}}){
if ($data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Option'}->[5]{'content'} == 1 ){
$username=$data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Name'};
$enabled=$data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Option'}->[5]{'content'};
if ( length($data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Name'}) >2){
@activeusers[$count]=$data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Name'};
$count++;}
}else{
$username=$data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Name'};
$enabled=$data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Option'}->[5]{'content'};
if ( length($data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Name'}) >2){
$countinactive++;
}}$i++;}
$totalcount=$count+$countinactive;
print "total active user count -> ". $count. " \n";
print "total inactive user count -> ". $countinactive. " \n";
undef $i;
my @tokeep=('user1','user2','...'); # <---------list with "safe" ftp users;
my $i=0;
my $j=0;
for (my $i=0;$i <=scalar(@activeusers);++$i){
for (my $j=0;$j <=scalar(@tokeep);++$j){
$keep=@tokeep[$j];
$list=@activeusers[$i];
if ($keep eq $list){
delete @activeusers[$i];
}
}
}
undef $i;
my $i=0;
for(my $i=0;$i <=scalar(@activeusers); ++$i){
#print $i . " \n";
if (defined(@activeusers[$i])){} else{
splice(@activeusers,$i,1);
#print $i."\n";
if (scalar(@activeusers)==$i){}else{$i--;}
}}
print "number of active users AFTER EXCEPTIONS: ".scalar(@activeusers)." \n";
$numofactiveusers=scalar(@activeusers);
@files = <c:/'program files'/'filezilla server'/logs/*>;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
@limitdate=localtime(time-1209600);#" to deactivate \n"; 14 days before. #useless. comparisson done in delta_days
@currentday=localtime(time);
#print Dumper @limitdate;
$year+=1900; $mon+=1; $mon=sprintf("%02d",$mon); $mday=sprintf("%02d",$mday);
foreach $file (@files) {
$fileyr=substr($file,43,4);
$filemo=substr($file,48,2);
$fileday=substr($file,51,2);
print $file. " diff-> ". Delta_Days($fileyr,$filemo,$fileday,$year,$mon,$mday)."\n";
$i++;
if ( Delta_Days($fileyr,$filemo,$fileday,$year,$mon,$mday) < 15){
open (AFILE,$file);# or die ("could not open file");
@raw_data=<AFILE>;
NEXTROW:
undef $i;
undef $p;
foreach $row (@raw_data){
#print Dumper @activeusers;
$p=0;
for (;$p <scalar(@activeusers);++$p){
if((rindex $row,@activeusers[$p])==-1 )
{} else{
print "found ".@activeusers[$p]." ".$p. " @ " . $fileday.$filemo.$fileyr."\n";
delete @activeusers[$p];
splice (@activeusers,$p,1);
#print $p;
#$p--;
} } }
close AFILE;
}
}
undef $i;
$i=0;
foreach $user2 (@{$data->{'FileZillaServer'}->{'Users'}->{'User'}}){
$k=0;
for (;$k<=scalar(@activeusers);$k++){
if ($data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Name'} eq @activeusers[$k]){
print "deactivating .. ->".@activeusers[$k]."\n";
$data->{'FileZillaServer'}->{'Users'}->{'User'}[$i]->{'Option'}->[5]{'content'}=0;
} else{
} }
$i++;}
#print Dumper $data;
$xml->XMLout($data,OutputFile => "c:/Program Files/FileZilla Server/FileZilla Server.xml",KeepRoot => 1,NoSort => 0,SuppressEmpty => 0,);
#exec `c:/xampp/FileZillaFTP/FileZillaServer.exe /start`;#<-start server
exec `c:/reloadconfigftp.cmd`;#<-on started server reload config with a batch file containing only one line this -> "c:\Program Files\FileZilla Server\FileZilla Server.exe" /reload-config
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)