Exim 有几种可用的灰名单备选实现。这里我们将介绍其中的几种。
这是一个由 我本人 开发的 Python 实现。(所以很自然地,这将是我将在后续的 最终 ACL 中包含的实现)。它作为一个独立的守护进程运行,因此不依赖于任何外部数据库。灰名单数据以简单的 32 位哈希值存储以提高效率。
你可以在 http://packages.debian.org/unstable/mail/greylistd 找到它。Debian 用户可以通过 APT 获取它
# apt-get install greylistd |
为了咨询greylistd,我们在之前声明的 acl_rcpt_to ACL 中插入两个语句,就在最后的accept语句
# Consult "greylistd" to obtain greylisting status for this particular # peer/sender/recipient triplet. # # We do not greylist messages with a NULL sender, because sender # callout verification would break (and we might not be able to # send mail to a host that performs callouts). # defer message = $sender_host_address is not yet authorized to deliver mail \ from <$sender_address> to <$local_part@$domain>. \ Please try later. log_message = greylisted. domains = +local_domains : +relay_to_domains !senders = : postmaster@* set acl_m9 = $sender_host_address $sender_address $local_part@$domain set acl_m9 = ${readsocket{/var/run/greylistd/socket}{$acl_m9}{5s}{}{}} condition = ${if eq {$acl_m9}{grey}{true}{false}} |
除非你整合 信封发件人签名 以阻止伪造的 投递状态通知,否则你可能需要在你的 acl_data 中添加类似的语句,以便也对具有 NULL 发件人的消息进行灰名单处理。
我们在此处用于灰名单目的的数据将与上面略有不同。除了$sender_address为空之外,$local_part也不$domain在此时未定义。相反,变量$recipients包含所有收件人地址的逗号分隔列表。对于合法的 DSN,应该只有一个地址。
# Perform greylisting on messages with no envelope sender here. # We did not subject these to greylisting after RCPT TO: because # that would interfere with remote hosts doing sender callouts. # defer message = $sender_host_address is not yet authorized to send \ delivery status reports to <$recipients>. \ Please try later. log_message = greylisted. senders = : postmaster@* set acl_m9 = $sender_host_address $recipients set acl_m9 = ${readsocket{/var/run/greylistd/socket}{$acl_m9}{5s}{}{}} condition = ${if eq {$acl_m9}{grey}{true}{false}} |
以下内联实现由 Johannes Berg 贡献<johannes (at) sipsolutions.net>,部分基于
Rick Stewart 的工作<rick.stewart (at) theinternetco.net>,发布在 http://theinternetco.net/projects/exim/greylist,反过来又基于
由 Tollef Fog Heen 创建的 Postgres 实现<tfheen (at) raw.no>,可在 http://raw.no/personal/blog/tech/Debian/2004-03-14-15-55_greylisting 找到。
它不需要任何外部程序 - 整个实现都基于这些配置片段以及 MySQL 数据库。
一个包含最新配置片段以及一个README文件的存档可在:http://johannes.sipsolutions.net/wiki/Projects/exim-greylist 找到。
需要在你的系统上安装 MySQL。在 MySQL 提示符下,创建一个exim4数据库,其中包含两个名为exim_greylist和exim_greylist_log的表,如下所示
CREATE DATABASE exim4; use exim4; CREATE TABLE exim_greylist ( id bigint(20) NOT NULL auto_increment, relay_ip varchar(80) default NULL, sender varchar(255) default NULL, recipient varchar(255) default NULL, block_expires datetime NOT NULL default '0000-00-00 00:00:00', record_expires datetime NOT NULL default '9999-12-31 23:59:59', create_time datetime NOT NULL default '0000-00-00 00:00:00', type enum('AUTO','MANUAL') NOT NULL default 'MANUAL', passcount bigint(20) NOT NULL default '0', blockcount bigint(20) NOT NULL default '0', PRIMARY KEY (id) ); CREATE TABLE exim_greylist_log ( id bigint(20) NOT NULL auto_increment, listid bigint(20) NOT NULL, timestamp datetime NOT NULL default '0000-00-00 00:00:00', kind enum('deferred', 'accepted') NOT NULL, PRIMARY KEY (id) ); |
在你的 Exim 配置文件的 main 部分,声明以下宏
# if you don't have another database defined, then define it here hide mysql_servers = localhost/exim4/user/password # options # these need to be valid as xxx in mysql's DATE_ADD(..,INTERVAL xxx) # not valid, for example, are plurals: "2 HOUR" instead of "2 HOURS" GREYLIST_INITIAL_DELAY = 1 HOUR GREYLIST_INITIAL_LIFETIME = 4 HOUR GREYLIST_WHITE_LIFETIME = 36 DAY GREYLIST_BOUNCE_LIFETIME = 0 HOUR # you can change the table names GREYLIST_TABLE=exim_greylist GREYLIST_LOG_TABLE=exim_greylist_log # comment out to the following line to disable greylisting (temporarily) GREYLIST_ENABLED= # uncomment the following to enable logging #GREYLIST_LOG_ENABLED= # below here, nothing should normally be edited .ifdef GREYLIST_ENABLED # database macros GREYLIST_TEST = SELECT CASE \ WHEN now() > block_expires THEN "accepted" \ ELSE "deferred" \ END AS result, id \ FROM GREYLIST_TABLE \ WHERE (now() < record_expires) \ AND (sender = '${quote_mysql:$sender_address}' \ OR (type='MANUAL' \ AND ( sender IS NULL \ OR sender = '${quote_mysql:@$sender_address_domain}' \ ) \ ) \ ) \ AND (recipient = '${quote_mysql:$local_part@$domain}' \ OR (type = 'MANUAL' \ AND ( recipient IS NULL \ OR recipient = '${quote_mysql:$local_part@}' \ OR recipient = '${quote_mysql:@$domain}' \ ) \ ) \ ) \ AND (relay_ip = '${quote_mysql:$sender_host_address}' \ OR (type='MANUAL' \ AND ( relay_ip IS NULL \ OR relay_ip = substring('${quote_mysql:$sender_host_address}',1,length(relay_ip)) \ ) \ ) \ ) \ ORDER BY result DESC LIMIT 1 GREYLIST_ADD = INSERT INTO GREYLIST_TABLE \ (relay_ip, sender, recipient, block_expires, \ record_expires, create_time, type) \ VALUES ( '${quote_mysql:$sender_host_address}', \ '${quote_mysql:$sender_address}', \ '${quote_mysql:$local_part@$domain}', \ DATE_ADD(now(), INTERVAL GREYLIST_INITIAL_DELAY), \ DATE_ADD(now(), INTERVAL GREYLIST_INITIAL_LIFETIME), \ now(), \ 'AUTO' \ ) GREYLIST_DEFER_HIT = UPDATE GREYLIST_TABLE \ SET blockcount=blockcount+1 \ WHERE id = $acl_m9 GREYLIST_OK_COUNT = UPDATE GREYLIST_TABLE \ SET passcount=passcount+1 \ WHERE id = $acl_m9 GREYLIST_OK_NEWTIME = UPDATE GREYLIST_TABLE \ SET record_expires = DATE_ADD(now(), INTERVAL GREYLIST_WHITE_LIFETIME) \ WHERE id = $acl_m9 AND type='AUTO' GREYLIST_OK_BOUNCE = UPDATE GREYLIST_TABLE \ SET record_expires = DATE_ADD(now(), INTERVAL GREYLIST_BOUNCE_LIFETIME) \ WHERE id = $acl_m9 AND type='AUTO' GREYLIST_LOG = INSERT INTO GREYLIST_LOG_TABLE \ (listid, timestamp, kind) \ VALUES ($acl_m9, now(), '$acl_m8') .endif |
现在,在 ACL 部分(在begin acl之后),声明一个新的 ACL,名为 "greylist_acl"
.ifdef GREYLIST_ENABLED # this acl returns either deny or accept # since we use it inside a defer with acl = greylist_acl, # accepting here makes the condition TRUE thus deferring, # denying here makes the condition FALSE thus not deferring greylist_acl: # For regular deliveries, check greylist. # check greylist tuple, returning "accepted", "deferred" or "unknown" # in acl_m8, and the record id in acl_m9 warn set acl_m8 = ${lookup mysql{GREYLIST_TEST}{$value}{result=unknown}} # here acl_m8 = "result=x id=y" set acl_m9 = ${extract{id}{$acl_m8}{$value}{-1}} # now acl_m9 contains the record id (or -1) set acl_m8 = ${extract{result}{$acl_m8}{$value}{unknown}} # now acl_m8 contains unknown/deferred/accepted # check if we know a certain triple, add and defer message if not accept # if above check returned unknown (no record yet) condition = ${if eq{$acl_m8}{unknown}{1}} # then also add a record condition = ${lookup mysql{GREYLIST_ADD}{yes}{no}} # now log, no matter what the result was # if the triple was unknown, we don't need a log entry # (and don't get one) because that is implicit through # the creation time above. .ifdef GREYLIST_LOG_ENABLED warn condition = ${lookup mysql{GREYLIST_LOG}} .endif # check if the triple is still blocked accept # if above check returned deferred then defer condition = ${if eq{$acl_m8}{deferred}{1}} # and note it down condition = ${lookup mysql{GREYLIST_DEFER_HIT}{yes}{yes}} # use a warn verb to count records that were hit warn condition = ${lookup mysql{GREYLIST_OK_COUNT}} # use a warn verb to set a new expire time on automatic records, # but only if the mail was not a bounce, otherwise set to now(). warn !senders = : postmaster@* condition = ${lookup mysql{GREYLIST_OK_NEWTIME}} warn senders = : postmaster@* condition = ${lookup mysql{GREYLIST_OK_BOUNCE}} deny .endif |
将此 ACL 合并到你的 acl_rcpt_to 中,以对发件人地址为非空的三元组进行灰名单处理。这是为了允许发件人回叫验证。
.ifdef GREYLIST_ENABLED defer !senders = : postmaster@* acl = greylist_acl message = greylisted - try again later .endif |
也将其合并到你的 acl_data 块中,但这次仅当发件人地址为空时才合并。这是为了防止垃圾邮件发送者通过将发件人地址设置为 NULL 来绕过灰名单。
.ifdef GREYLIST_ENABLED defer senders = : postmaster@* acl = greylist_acl message = greylisted - try again later .endif |