|
|
@ -18,8 +18,8 @@ my($log_header); |
|
|
|
my ($log_all_body,$log_deny_body,$body_table); |
|
|
|
my ($log_all_body,$log_deny_body,$body_table); |
|
|
|
my ($dbh,$sth); |
|
|
|
my ($dbh,$sth); |
|
|
|
|
|
|
|
|
|
|
|
sub register |
|
|
|
sub register { |
|
|
|
{my ($self,$qp) = (shift,shift); |
|
|
|
my ($self,$qp) = (shift,shift); |
|
|
|
$self->log(LOGERROR,"Bad count of parameters in log2sql plugin.") if @_ % 2; |
|
|
|
$self->log(LOGERROR,"Bad count of parameters in log2sql plugin.") if @_ % 2; |
|
|
|
my (%args) = @_; |
|
|
|
my (%args) = @_; |
|
|
|
|
|
|
|
|
|
|
@ -28,9 +28,7 @@ $self->register_hook("mail", "mail_handler"); |
|
|
|
$self->register_hook("rcpt", "rcpt_handler"); |
|
|
|
$self->register_hook("rcpt", "rcpt_handler"); |
|
|
|
$self->register_hook("data_post", "data_post_handler"); |
|
|
|
$self->register_hook("data_post", "data_post_handler"); |
|
|
|
$self->register_hook("queue", "queue_handler"); |
|
|
|
$self->register_hook("queue", "queue_handler"); |
|
|
|
|
|
|
|
|
|
|
|
$self->register_hook("deny", "deny_handler") if $args{D}; |
|
|
|
$self->register_hook("deny", "deny_handler") if $args{D}; |
|
|
|
|
|
|
|
|
|
|
|
$self->register_hook("disconnect", "disconnect_handler"); |
|
|
|
$self->register_hook("disconnect", "disconnect_handler"); |
|
|
|
|
|
|
|
|
|
|
|
$dsn = 'DBI'; |
|
|
|
$dsn = 'DBI'; |
|
|
@ -51,14 +49,13 @@ $log_header = $args{H} || undef; |
|
|
|
$log_all_body = $args{B} || undef; |
|
|
|
$log_all_body = $args{B} || undef; |
|
|
|
$log_deny_body = $args{DB} || undef; |
|
|
|
$log_deny_body = $args{DB} || undef; |
|
|
|
$body_table = $args{b} || 'message_body'; |
|
|
|
$body_table = $args{b} || 'message_body'; |
|
|
|
}#end register |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub connect_handler |
|
|
|
sub connect_handler { |
|
|
|
{my ($self,$transaction) = @_; |
|
|
|
my ($self,$transaction) = @_; |
|
|
|
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG,"DSN:$dsn"); |
|
|
|
$self->log(LOGDEBUG,"DSN:$dsn"); |
|
|
|
$dbh = DBI->connect($dsn,$user,$passwd) |
|
|
|
$dbh = DBI->connect($dsn,$user,$passwd) || $self->log(LOGERROR,DBI::errstr); |
|
|
|
|| $self->log(LOGERROR,DBI::errstr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# generate mail id |
|
|
|
# generate mail id |
|
|
|
$mail_id = $$.'.'.time.'.'.int(rand(10000)); |
|
|
|
$mail_id = $$.'.'.time.'.'.int(rand(10000)); |
|
|
@ -85,16 +82,15 @@ my($statement) = "INSERT INTO ".$mail_table." (mail_id,". |
|
|
|
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG,"connect_handler statement:".$statement); |
|
|
|
$self->log(LOGDEBUG,"connect_handler statement:".$statement); |
|
|
|
|
|
|
|
|
|
|
|
$dbh->do($statement) |
|
|
|
$dbh->do($statement) || $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return(DECLINED); |
|
|
|
return(DECLINED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub mail_handler |
|
|
|
sub mail_handler { |
|
|
|
{my ($self, $transaction, $sender) =@_; |
|
|
|
my ($self, $transaction, $sender) = @_; |
|
|
|
|
|
|
|
|
|
|
|
my ($statement) = "UPDATE ".$mail_table." SET sender=". |
|
|
|
my ($statement) = "UPDATE ".$mail_table." SET sender=". |
|
|
|
$dbh->quote($sender->user.'@'.$sender->host). |
|
|
|
$dbh->quote($sender->user.'@'.$sender->host). |
|
|
@ -109,8 +105,8 @@ $dbh->do($statement) |
|
|
|
return(DECLINED); |
|
|
|
return(DECLINED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub rcpt_handler |
|
|
|
sub rcpt_handler { |
|
|
|
{my($self,$transaction,$recipient) = @_; |
|
|
|
my ($self,$transaction,$recipient) = @_; |
|
|
|
|
|
|
|
|
|
|
|
my ($statement) = "INSERT INTO ".$rcpt_table." (mail_id,". |
|
|
|
my ($statement) = "INSERT INTO ".$rcpt_table." (mail_id,". |
|
|
|
"recipient) ". |
|
|
|
"recipient) ". |
|
|
@ -120,15 +116,14 @@ my($statement) = "INSERT INTO ".$rcpt_table." (mail_id,". |
|
|
|
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG,"rcpt_handler statement:".$statement); |
|
|
|
$self->log(LOGDEBUG,"rcpt_handler statement:".$statement); |
|
|
|
|
|
|
|
|
|
|
|
$dbh->do($statement) |
|
|
|
$dbh->do($statement) || $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return(DECLINED); |
|
|
|
return(DECLINED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub data_post_handler |
|
|
|
sub data_post_handler{ |
|
|
|
{my($self,$transaction)=@_; |
|
|
|
my ($self,$transaction) = @_; |
|
|
|
my ($header) = $transaction->header(); |
|
|
|
my ($header) = $transaction->header(); |
|
|
|
# In data_post handler the header is not actual, |
|
|
|
# In data_post handler the header is not actual, |
|
|
|
# but in queue handler somethimes there are problems with |
|
|
|
# but in queue handler somethimes there are problems with |
|
|
@ -154,24 +149,24 @@ $dbh->do($statement) |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
#if $args{H} then log the message header in the main 'messages' table |
|
|
|
#if $args{H} then log the message header in the main 'messages' table |
|
|
|
if ($log_header) |
|
|
|
if ($log_header){ |
|
|
|
{$statement = "UPDATE ".$mail_table." SET ". |
|
|
|
$statement = "UPDATE ".$mail_table." SET ". |
|
|
|
"header=".$dbh->quote($header->as_string()). |
|
|
|
"header=".$dbh->quote($header->as_string()). |
|
|
|
"WHERE mail_id=".$mail_id; |
|
|
|
"WHERE mail_id=".$mail_id; |
|
|
|
$self->log(LOGDEBUG,"data_post_handler log header statement:".$statement); |
|
|
|
$self->log(LOGDEBUG,"data_post_handler log header statement:".$statement); |
|
|
|
|
|
|
|
|
|
|
|
$dbh->do($statement) |
|
|
|
$dbh->do($statement) |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
}#end log geader |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if args{B} then log message body in a separated table |
|
|
|
#if args{B} then log message body in a separated table |
|
|
|
if ($log_all_body) |
|
|
|
if ($log_all_body) { |
|
|
|
{my(@body) = ($transaction->header()->as_string()); |
|
|
|
my (@body) = ($transaction->header()->as_string()); |
|
|
|
|
|
|
|
|
|
|
|
$transaction->body_resetpos(); |
|
|
|
$transaction->body_resetpos(); |
|
|
|
|
|
|
|
|
|
|
|
while(my $line = $transaction->body_getline()) |
|
|
|
while(my $line = $transaction->body_getline()) { |
|
|
|
{push(@body,$line); |
|
|
|
push(@body,$line); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$statement = "INSERT INTO ".$body_table." (mail_id,body)". |
|
|
|
$statement = "INSERT INTO ".$body_table." (mail_id,body)". |
|
|
@ -181,14 +176,14 @@ if ($log_all_body) |
|
|
|
$dbh->do($statement) |
|
|
|
$dbh->do($statement) |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
}#end body log |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return(DECLINED); |
|
|
|
return(DECLINED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub queue_handler |
|
|
|
sub queue_handler { |
|
|
|
{my($self,$transaction) = @_; |
|
|
|
my ($self,$transaction) = @_; |
|
|
|
my ($status) = $transaction->header->get('X-Spam-Status') or return (DECLINED); |
|
|
|
my ($status) = $transaction->header->get('X-Spam-Status') or return (DECLINED); |
|
|
|
my ($score) = ($status =~ m/hits=(\d+\.\d+)/)[0] || "0" ; |
|
|
|
my ($score) = ($status =~ m/hits=(\d+\.\d+)/)[0] || "0" ; |
|
|
|
|
|
|
|
|
|
|
@ -204,8 +199,8 @@ $dbh->do($statement) |
|
|
|
return(DECLINED); |
|
|
|
return(DECLINED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub deny_handler |
|
|
|
sub deny_handler { |
|
|
|
{my($self,$transaction,$plugin,$code,$msg) = @_; |
|
|
|
my ($self,$transaction,$plugin,$code,$msg) = @_; |
|
|
|
|
|
|
|
|
|
|
|
my ($statement) = "UPDATE ".$mail_table." SET ". |
|
|
|
my ($statement) = "UPDATE ".$mail_table." SET ". |
|
|
|
"deny='YES'". |
|
|
|
"deny='YES'". |
|
|
@ -216,34 +211,32 @@ my($statement) = "UPDATE ".$mail_table." SET ". |
|
|
|
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG,"deny_handler statement:".$statement); |
|
|
|
$self->log(LOGDEBUG,"deny_handler statement:".$statement); |
|
|
|
|
|
|
|
|
|
|
|
$dbh->do($statement) |
|
|
|
$dbh->do($statement) || $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if configured to log the body of the denied messages |
|
|
|
#if configured to log the body of the denied messages |
|
|
|
if ($log_deny_body && ! $log_all_body) |
|
|
|
if ($log_deny_body && ! $log_all_body) { |
|
|
|
{my(@body) = ($transaction->header()->as_string()); |
|
|
|
my (@body) = ($transaction->header()->as_string()); |
|
|
|
|
|
|
|
|
|
|
|
$transaction->body_resetpos(); |
|
|
|
$transaction->body_resetpos(); |
|
|
|
|
|
|
|
|
|
|
|
while(my $line = $transaction->body_getline()) |
|
|
|
while (my $line = $transaction->body_getline()) { |
|
|
|
{push(@body,$line); |
|
|
|
push(@body,$line); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$statement = "INSERT INTO ".$body_table." (mail_id,body)". |
|
|
|
$statement = "INSERT INTO ".$body_table." (mail_id,body)". |
|
|
|
" VALUES(".$mail_id.",".$dbh->quote(join('',@body)).")"; |
|
|
|
" VALUES(".$mail_id.",".$dbh->quote(join('',@body)).")"; |
|
|
|
$self->log(LOGDEBUG,"data_post body log statement:".$statement); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$dbh->do($statement) |
|
|
|
$self->log(LOGDEBUG,"data_post body log statement:".$statement); |
|
|
|
|| $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}#end body log |
|
|
|
$dbh->do($statement) || $self->log(LOGERROR,$dbh->errstr()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return(DECLINED); |
|
|
|
return(DECLINED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub disconnect_handler |
|
|
|
sub disconnect_handler { |
|
|
|
{my($self,$transaction) = @_; |
|
|
|
my ($self,$transaction) = @_; |
|
|
|
|
|
|
|
|
|
|
|
$dbh->disconnect(); |
|
|
|
$dbh->disconnect(); |
|
|
|
|
|
|
|
|
|
|
@ -293,12 +286,13 @@ Here are the CREATE statements for the tables. Assuming the 'messages' table is |
|
|
|
|
|
|
|
|
|
|
|
CREATE TABLE `messages` ( |
|
|
|
CREATE TABLE `messages` ( |
|
|
|
`mail_id` varchar(255) NOT NULL default '', |
|
|
|
`mail_id` varchar(255) NOT NULL default '', |
|
|
|
`time` int(11) default '0', |
|
|
|
`date_day` date, |
|
|
|
|
|
|
|
`date_time` time, |
|
|
|
`remote_ip` varchar(255) default NULL, |
|
|
|
`remote_ip` varchar(255) default NULL, |
|
|
|
`remote_host` varchar(255) default NULL, |
|
|
|
`remote_host` varchar(255) default NULL, |
|
|
|
`remote_info` varchar(255) default NULL, |
|
|
|
`remote_info` varchar(255) default NULL, |
|
|
|
`sender` varchar(255) default NULL, |
|
|
|
`sender` varchar(255) default NULL, |
|
|
|
`subject` text, |
|
|
|
`subject` varchar(255) default NULL, |
|
|
|
`header_size` int(11) default NULL, |
|
|
|
`header_size` int(11) default NULL, |
|
|
|
`body_size` int(11) default NULL, |
|
|
|
`body_size` int(11) default NULL, |
|
|
|
`spam_status` float default NULL, |
|
|
|
`spam_status` float default NULL, |
|
|
|