在sendmail 邮件服务器中限定用户邮箱的大小

来源: 作者:otto
  在sendmail 邮件服务器中限定用户邮箱的大小

---- 随着国际互联网的不断普及,电子邮件(E_mail)服务已成为应用最为广泛的服务之一。目前,在LINUX平台上,sendmail邮件服务器应用的最普遍。但是因为sendmail本身并不能限定用户邮箱的大小,所以,如果某些用户长时间不取信的话,他(她)的信件将在服务器上不断堆积,以致其邮箱越来越大,占用大量的硬盘空间。因此,邮件服务器的管理员应采取必要的措施,限定用户邮箱的大小,避免这种情况的发生。这里介绍一种简单实用的方法。

---- 一. 基本思路:

---- 先设定用户邮箱的最大值,然后定期检查邮箱文件的大小,将所有超过这一上限的邮箱文件名写进某一特定的文件中;再利用sendmail 的 blacklist_recipients 功能,读取该文件,从而使邮件服务器拒收该用户的新邮件。

---- 二. 运行环境

---- 1. Redhat5.1 或以上

---- 2. Sendmail.8.9.3以上

---- 若无该版本的sendmail 运行软件,可到freesoft.cei..gov.cn站点下载,按README的提示进行编译、安装。

---- 3. Perl5

---- 三.具体方法:

---- 1.重新配置 sendmail.cf文件

---- (如果原有的 sendmail.cf文件已经包含了 blacklist_recipinets 功能,可免此步骤)

---- 假定 sendmail-8.93 的软件包在/home/sendmail-8.9.3目录下展开、编译,并进行缺省安装。

# cd /home/sendmail-8.9.3/cf/domain
# vi linux.m4
在文件的末尾增加:
FEATURE(`access_db'',`hash -o /etc/mail/access'')dnl
FEATURE(`blacklist_recipients'')dnl
# cd ../cf
# m4 ../m4/cf.m4 ./linux.mc > ./sendmail.cf
# cp ./sendmail.cf /etc/

2.生成 access.db 文件
# cd /etc/mail
# makemap hash access < /dev/null

3. 重启sendmail
# /etc/rc.d/init/sendmail restart

4. 定时执行本文提供的PERL代码
# limit_mailbox.pl -s

---- 附录.limit_mailbox.pl 源代码


#!/usr/bin/perl
#
# limit_mailbox.pl
# Usage:
# limit_mailbox.pl-s
# searching the mailboxes larger than
maxsize such as 5M, then setting
# them full to reject new mails
# limit_mailbox.pl -a
# appending the records into
access.db from the STDIN
# limit_mailbox.pl -r
# removing the records in access.db
according the users'' name input from
# the STDIN
# limit_mailbox.pl -c
# cleaning access.db
# limit_mailbox.pl -p
# printing all the records in access.db to the STDOUT

# settings
$access_dbm="/etc/mail/access.db";
$maildrop="/var/spool/mail";
$maxsize=5*1024*1024; # the maximum size of mailbox
$dbtype="DB_HASH";

# Modules
use Getopt::Std;
use Fcntl;
use DB_File;
use POSIX;

# Variables
undef %db;

# Subroutines

sub opendb_read {
tie(%db,"DB_File",$access_dbm,
O_RDONLY,0,$$dbtype) || die "Can''t open $access_dbm\n";
}
sub opendb_trunc {
tie(%db,"DB_File",$access_dbm,
O_TRUNC,0,$$dbtype) || die "Can''t open $access_dbm\n";
}
sub opendb_write {
tie(%db,"DB_File",$access_dbm,O_RDWR||O
_CREAT,0640,$$dbtype) || die "Can''t open $access_dbm\n";
}
sub adddb {
my $rec=$_[0];
my @list;

($key,@list)=split(/\s+/,$rec);
if ( defined( $db{$key} ) ) {
print "Warning: $rec duplicated, adding failed! \n";
} else {
$db{$key}=join('' '',@list) || die "can''t add record into $access_dbm\n";
}
}
sub removedb {
my $user=$_[0];
if ( defined($db{$user}) ) {
delete $db{$user} && die "Can''t delete
$user in $access_dbm \n";
} else {
print " $user is not existed in $access_dbm \n";
}
}
sub closedb {
untie %db;
}

sub search_box {
chdir($maildrop) || die "Can''t change into $maildrop!\n";
while (< ./* >) {
s?.*/??;
( /^[:|.]/ ) && next;
$mailuser=$_;
$boxsize=(POSIX::stat("./".$mailuser))[7];
( $boxsize >= $maxsize ) && adddb("$mailuser 550 ERROR: this user\''s Mailbox is full");
}
}

# Main Program

getopts(''sarpc'') || die "Usage: limit_mailbox.pl [-s] [-a] [-r] [-p] [-c] \n";

if ($opt_a) {
# append the records from stdin into /etc/mail/access.db
print "Please input the records, ended by cntl-D\n";
opendb_write;
foreach $rec (< STDIN >) {
chop($rec);
adddb $rec;
}
closedb;

} elsif ($opt_r) {
# remove the records in /etc/mail/access.
db according to the STDOUT
print "Please input the removed users , ended by cntl-D\n";
opendb_write;
foreach $rec (< STDIN >) {
chop($rec);
@users=split(/\s+/,$rec);
foreach $user (@users) {
removedb($user);
}
}
closedb;

} elsif ($opt_p) {
# print all the records in /etc/mail/access.db
opendb_read;
while ( ($key,$value)=each(%db) ) {
print "$key\t\t$value\n";
}

} elsif ($opt_c) {
# clean the /etc/mail/access db
opendb_trunc;
closedb;

} elsif ($opt_s) {

# search all the mailbox which are over
Maxsize, then set those boxes full
opendb_write;
# clean db
( defined %emptydb ) && undef %emptydb;
%emptydb;
%db=%emptydb;
# search larger boxes
search_box;
closedb;

} else {
# No options given
die "Usage: limit_mailbox.pl [-s] [-a] [-r] [-p] [-c] \n";
}

exit 0;

时间:2001-03-21 01:40 来源: 作者:otto 原文链接

好文,顶一下
(0)
0%
文章真差,踩一下
(0)
0%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量