On Wednesday 26 March 2003 07:41, Matt wrote:
> I'm running qmail with ldap now... and am curious.. .when I delete an
> entry fromt he LDAP (or have my backend software do it).. the user's
> directory still remains there.   What have other people done to remove
> directories of "Defunct" users?

This seems to come up a lot.

Perl script attached. I run this in cron once a week. The script archives the 
mailbox and then deletes it. You would have to add a find in there somewhere 
that would delete the archives in X months or whatever. It prints a report of 
how many boxes it removes.

-- 
Brendon Colby
Systems Administrator
Midcontinent Communications
#!/usr/bin/perl -w
#
# Description: Checks LDAP for the user accounts. If not exists, archive 
#   user's mailbox then delete.

use strict;
use Net::LDAP;
use Archive::Tar;
use File::Find;
use Date::Manip;

# LDAP Declarations
my($ldap_server) = '';
my($ldap_binddn) = '';
my($ldap_base) = '';

my($server) = `hostname -f`;
chomp($server);

my($defunct_dir) = '/home/users/defunct';
my($users_dir) = '/home/users';
my($reg_dir);
my($user,$result);
my(@reg_list,@user_list);
my(%stats,%reg_stats);

cleanup_archives();

exit;

# Set to zero in case of no deletes
$stats{mbs}{deleted}{total} = 0;

# Connect to LDAP server
my($ldap) = Net::LDAP->new($ldap_server) or die "$@";

$ldap->bind or die "$@";

# Gather region list
opendir(USERSDIR, $users_dir) or die "Cannot open $users_dir: $!\n";
@reg_list = grep !/^\.\.?|defunct\z/, readdir USERSDIR;
close USERSDIR;

# For every region directory, gather a list of users underneath
foreach $reg_dir (@reg_list) {

    opendir(REGDIR, $users_dir."/".$reg_dir) 
      or die "Cannot open $users_dir\/$reg_dir: $!\n";

    @user_list = grep !/^\.\.?\z/, readdir REGDIR;

    close REGDIR;

    foreach $user (@user_list) {
	
	$stats{mbs}{total} += 1;
	
	$reg_stats{$reg_dir}{total} += 1;

	#print "Check [EMAIL PROTECTED]";

	# LOGIC: Check LDAP for existence of [EMAIL PROTECTED]
	# Delete mailbox if non-existent on ldap server
	$result = $ldap->search ( base   => "$ldap_base",
				  filter => "([EMAIL PROTECTED])"
				);

	if($result->code) {

	    warn "LDAP search for [EMAIL PROTECTED] failed: ", $result->error;

	}

	if(!$result->count) {
	    
	    $stats{mbs}{deleted}{total} += 1;
	    $stats{mbs}{deleted}{regions}{$reg_dir} += 1;
	    
	    # Create a new tar object
	    my $tar = Archive::Tar->new;

	    # Calls function to either add a file or recursively add a dir
	    add_to_tar($tar, $user, $users_dir."/".$reg_dir);

	    # Write the tar file to the defunct directory
	    $tar->write($defunct_dir."/".$user."\@".$reg_dir.".tar.gz", 9);

	    system('rm','-r','-f',$users_dir."/".$reg_dir."/".$user);

#	    print "[EMAIL PROTECTED] ARCHIVED AND DELETED.\n";

	} else {
	    
#	    print "FOUND\n";
	
	}

    }

}

print "Statistics for $server\n\n";
print "Total mailboxes: ".$stats{mbs}{total}."\n";
print "Total archived and deleted: ".$stats{mbs}{deleted}{total}."\n";

$ldap->unbind;

# Some code off of perlmonks.org
sub add_to_tar {
    my($tar, $object, $base) = @_;

    if (defined $base) {
	chdir $base or die "Can't chdir to $base: $!\n";
#	$object =~ s/^$base//;
    }

    # If object is a file just add it
    if (-f $object) {
	$tar->add_files($object);
    }
    # If object is a dir then recurse it and add all files
    elsif (-d $object) {

	my $code = sub {
	    return if ! -f $_;

	    local *FH;
	    open FH, $_ or die "Can't open $_: $!";
	    binmode FH;
	    my $c = do { local $/; <FH> };
	    close FH or die "Can't close $_: $!";

	    $tar->add_data($File::Find::name, $c);
	};
	find $code, $object;
    }
}

sub cleanup_archives {

  my($days_to_arch) = '90';

  my($test) = UnixDate(ParseDate("90 days ago"));

  my($def_dir) = '/home/users/defunct';
  my(@archives,@stat);
  my($archive);

  print $test."\n";

  exit;

  opendir(USERSDIR, $def_dir) or die "Cannot open $def_dir: $!\n";
  @archives = grep !/^\.\.?\z/, readdir USERSDIR;
  close USERSDIR;

  foreach $archive (@archives) { 

    @stat = stat($def_dir.'/'.$archive);

    print("Last access of $archive was: $stat[9]\n");

  }

}

Reply via email to