ExtMail服务器社区's Archiver

fengyong 发表于 2010-5-23 18:56

slock WEB后台,MySQL存储灰名单/黑白名单

灰名单代码【进行中】[code]
#!perl
# vim: set cindent expandtab ts=4 sw=4:

# greylist      greylist plugin
#
# description: this plugin derive from postfix demo greylisting
#              policy perl script, fix some locking bug.
use strict;
use DBI;

sub init {
    my $self = shift;
    my $config = $self->config;

    $self->{plugin_name} = 'Greylist';

    $self->{delay} = $config->{greylist_delay} || 60;
}

sub pre_hook {
    my $self = shift;
    my $ar = $self->ar;
        my($time_stamp, $now);
    $self->init_db;

    # Lookup the time stamp for this client/sender/recipient.
        $time_stamp = $self->lookup($ar) || 0;
    $now = time();

    # If this is a new request add this client/sender/recipient to db
    if ($time_stamp == 0) {
        $time_stamp = $now;
        $self->update($ar, $time_stamp);
    }

    if ($now - $time_stamp > $self->{delay}) {
        return "DUNNO";
    } else {
        return "defer_if_permit Try again, see http://bl.extmail.org/cgi/why?greylist";
    }
}

sub init_db {
    my $self = shift;
        my $config=$self->config;
        my $dsn="DBI:mysql:database=$config->{dbname};host=$config->{host}";
        $self->{dbh} = DBI->connect($dsn,$config->{dbuser},$config->{dbpw}) or die $DBI::errstr;
}
sub lookup {
        my $self=shift;
        my $ar=shift;
        my $SQL = "SELECT time FROM greylist WHERE
                                        ip='$ar->{client_address}'AND
                                        sender='$ar->{sender}'AND
                                        recipient='$ar->{recipient}'";
        my $sth = $self->{dbh}->prepare($SQL);
        $sth->execute();
        my $arr={};
        $arr=$sth->fetchrow_hashref();
        return $arr->{'time'};
}
sub update {
        my $self=shift;
        my $ar=shift;
        my $time_stamp=shift;
        #my $SQL="INSERT INTO greylist (ip,sender,recipient,time) VALUE (?,?,?,?)";
        my $SQL="INSERT INTO greylist SET ip=?,sender=?,recipient=?,time=?";
        my $sth=$self->{dbh}->prepare($SQL);
        $sth->execute($ar->{client_address},$ar->{sender},$ar->{recipient},$time_stamp);
}

1;
[/code]

[[i] 本帖最后由 fengyong 于 2010-5-23 18:58 编辑 [/i]]

fengyong 发表于 2010-5-23 19:20

测试
[root@bogon tools]# ./policy_sig -h localhost -p 10030 --ip 123.65.246.15 --cli mail.wr.com --helo aaa.com --from [email]fy@aaa.com[/email] --to [email]root@bogon.com[/email]
action=defer_if_permit Try again, see [url]http://bl.extmail.org/cgi/why?greylist[/url]

看看数据库变化
Database changed
mysql> select * from greylist;
+----+----------------+------------------+----------------+------------+
| id | ip             | sender           | recipient      | time       |
+----+----------------+------------------+----------------+------------+
|  3 | 123.65.246.15 | [email]fy@aaa.com[/email] | [email]root@bogon.com[/email] | 1274623824 |
+----+----------------+------------------+----------------+------------+

N分钟后

[root@bogon tools]# ./policy_sig -h localhost -p 10030 --ip 123.65.246.15 --cli mail.wr.com --helo aaa.com --from [email]fy@aaa.com[/email] --to [email]root@bogon.com[/email]
action=DUNNO

[[i] 本帖最后由 fengyong 于 2010-5-23 19:27 编辑 [/i]]

fengyong 发表于 2010-5-23 22:54

继灰名单更改成功后,黑白名单也成功改为mysql
先看看数据库
mysql> select * from white;
+----+-------------+---------+
| id | white       | comment |
+----+-------------+---------+
|  1 | 192.168.8.1 |         |
+----+-------------+---------+
1 row in set (0.00 sec)

mysql> select * from black;
+----+-------------+---------+
| id | black       | comment |
+----+-------------+---------+
|  1 | 192.168.8.2 |         |
+----+-------------+---------+
1 row in set (0.00 sec)

测试结果

[root@bogon tools]# ./policy_sig -h localhost -p 10030 --ip 192.168.8.1 --cli mail.wr.com --helo aaa.com --from [email]fy@aaa.com[/email] --to [email]root@bogon.com[/email]
action=OK
[root@bogon tools]# ./policy_sig -h localhost -p 10030 --ip 192.168.8.2 --cli mail.wr.com --helo aaa.com --from [email]fy@aaa.com[/email] --to [email]root@bogon.com[/email]
action=504 blocked by localctrl, see [url]http://bl.extmail.org/cgi/why?localctrl[/url]

liushaobo 发表于 2010-5-23 23:24

:lol  顶你个肺

yg0898 发表于 2010-5-24 09:18

哈哈~!!会不会集成到下个EMOS版本呢?

wendyzh 发表于 2010-5-26 13:08

顶顶顶。。。。。。。。。。。。。。。。。。。

nhbbq 发表于 2010-7-5 14:35

mysql服务器会不会很多进程在sleep呢?我试过会出现sleep的pid不断增加的情况,怎样解决?

nhbbq 发表于 2010-12-2 09:49

黑白名单要怎么处理?

aken 发表于 2010-12-2 09:52

黑白名单也成功改为mysql
需要怎样做?

nhbbq 发表于 2011-1-7 09:10

基于楼主方式,自己东拼西拼,集合了greylist,__blacklist,__whitelist,_recip_blacklist,_recip_whitelist,_senders_blacklist,_senders_whitelist 7个表,因为对perl认知有限,希望有人能之更优化,献丑了

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.