#!/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 # 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 "\n"; print CMD "\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 "
\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 " $user failERROR: $@ "; next; } if ($expire - $now <=0) { if ($u->{active}) { print CMD " $user expired updated"; disable_user($u); # send ref } else { print CMD " $user expired -"; } } else { print CMD " $user alive -"; } print CMD "\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"; } }