remove user script

Download, Add a comment, Back to main page
#!/usr/bin/perl
#remove_users.pl
#Joe Horstmann - joe_horstmann@comcast.net
#Created 10/01/04
#This script was created to restrict user accounts on a 1.4 jabber server to a predefined list of users.
#Allowing server registration to remain active, so users can change thier own passwords, v-card, etc.
#Copy this file to your /var/spool/jabber/ directory, and set the $hostname to match your system.
#Create a userlist.txt file. The userlist.txt file should contain the username of each user on a single line. 
#The script will then compare the usernames in the .txt file with the actual .xml files. 
#Files not on the list are logged to remove_users.log and deleted from the spool directory.

&read_dir;
&read_list;
&compare_rm;

##########
sub read_dir {

$hostname="your host name";

chdir("/var/spool/jabber/$hostname") || die("Unable to set directory");
$dirtoget=".";
opendir(DIR, $dirtoget) || die("Cannot open directory");
@thefiles= readdir(DIR);
closedir(DIR);

   foreach $file (@thefiles) {
        if ($file=~/\.xml/) {
        #$file =~ s/\.xml//;
        push (@xmlfile ,$file); 
        } 
   }
  
@xmlfile = sort (@xmlfile); 

#print "$#xmlfile \n";
#print "@xmlfile \n";

}

##########
sub read_list {

chdir("/var/spool/jabber/") || die("Unable to set directory");
open INPUT, "userlist.txt";
chomp (@userlist = <INPUT>);  
close INPUT;
@userlist = sort (@userlist);

#print "$#userlist \n";
#print "@userlist \n";

}

##########
sub compare_rm {

chdir("/var/spool/jabber/$hostname") || die("Unable to set directory");
$ext=".xml";
open LOG, ">>../remove_user.log" or die "Cannot open remove_user.log for write :$!";
print LOG "\n";
print LOG "--- Removed Users --- \n";

	   foreach $xmlfile (@xmlfile) {
          
              LBL1: for ($y=0; $y <= $#userlist; $y++) {
 
                       if ($xmlfile eq $userlist[$y].$ext) {
                 
                       last LBL1  
                       } elsif ($y == $#userlist) { 

                         print LOG "$xmlfile \n"; 
                         system("rm $xmlfile");

                       } 
                        
                    }
           }
close LOG;
}