#!/usr/bin/perl -w
# vim: set ci et ts=4 sw=4:
# expireusers.pl: find expired users and disable them completely
# this script is base on chifeng's expireuser.pl
# and rewritten completely
#
# Author: He zhiqiang <hzqbbc@hzqbbc.com>
# Last Update: Tue Nov 20 2007 20:59:03
# Version: 0.5
use vars qw($DIR);
BEGIN {
my $path = $0;
if ($path =~ s/tools\/[0-9a-zA-Z\-\_]+\.pl$//) {
if ($path !~ /^\//) {
$DIR = "./$path";
} else {
$DIR = $path;
}
} else {
$DIR = '../';
}
unshift @INC, $DIR .'libs';
};
use POSIX qw(strftime);
use Ext::Mgr;
use CmdTools;
use Ext::DateTime qw(time2epoch epoch2time);
die "Usage: $0 [domain|-all] [recipient]\n" unless $#ARGV == 1;
my $ctx = CmdTools->new( config => $DIR . '/webman.cf', directory => $DIR );
my $c = \%Ext::Cfg;
my $mgr = $ctx->ctx; # backend object
my $SENDMAIL = '/usr/sbin/sendmail -t -oi';
my $backend = $c->{SYS_BACKEND_TYPE};
my $domain = "$ARGV[0]";
my $recip = $ARGV[1];
my $now = strftime ("%Y-%m-%d %H:%M:%S", localtime);
$ctx->_lock;
open(CMD, "|$SENDMAIL") or die "sendmail command error: $!\n";
print CMD "Content-type: text/html; charset=UTF-8\n";
print CMD "To: $recip\n";
print CMD "Subject: [$now] Expiration report for $domain domain\n";
print CMD "\n";
$now = time2epoch ($now);
print CMD "<style>
.red td{ background: #ff0000; color: #fff }
.grey td{ background: #cccccc; }
</style>\n";
print CMD "<table border=1 width=100%>\n";
if ($domain eq '-all') {
# check all
my $all = $mgr->get_domains_list || [];
for my $dm (@$all) {
check_domain($dm->{domain});
}
} else {
check_domain($domain);
}
print CMD "</table>\n";
close CMD;
# terminate normally
$ctx->_unlock;
exit (0);
sub check_domain {
my $domain = shift;
my $ul = $mgr->get_users_list($domain);
for my $u (@$ul) {
my $user = $u->{mail};
my $expire;
eval { $expire = time2epoch($u->{expire}) };
if ($@) {
print CMD "<tr class=red>
<td>$user</td>
<td>fail</td.
<td>ERROR: $@</td>
</tr>";
next;
}
if ($expire - $now <=0) {
if ($u->{active}) {
print CMD "<tr class=red>
<td>$user</td>
<td>expired</td>
<td>updated</td>";
disable_user($u); # send ref
} else {
print CMD "<tr class=grey>
<td>$user</td>
<td>expired</td>
<td>-</td>";
}
} else {
print CMD "<tr class=normal>
<td>$user</td>
<td>alive</td>
<td>-</td>";
}
print CMD "</tr>\n";
}
}
sub disable_user {
my $r = shift;
my $user = $r->{mail};
$rc = $mgr->modify_user(
user => $user,
cn => $r->{cn},
uidnumber => $r->{uidnumber},
gidnumber => $r->{gidnumber},
expire => $r->{expire},
quota => $r->{quota},
netdiskquota => $r->{netdiskquota},
active => 0,
disablesmtpd => $r->{disablesmtpd},
disablesmtp => $r->{disablesmtp},
disablewebmail => $r->{disablewebmail},
disablenetdisk => $r->{disablenetdisk},
disablepop3 => $r->{disablepop3},
disableimap => $r->{disableimap},
);
if ($rc) {
print CMD "modify $user fail: $rc\n";
}
}
syntax highlighted by Code2HTML, v. 0.9.1