2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2003-10-01 22:57:11 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
milter
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This plugin allows you to attach to milter filters (yes, those written for
|
|
|
|
sendmail) as though they were qpsmtpd plugins.
|
|
|
|
|
|
|
|
In order to do this you need the C<Net::Milter> module from CPAN.
|
|
|
|
|
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
It takes two required parameters - a milter name (for logging) and the port
|
|
|
|
to connect to on the localhost. This can also contain a hostname if
|
|
|
|
the filter is on another machine:
|
|
|
|
|
2003-11-10 09:06:00 +01:00
|
|
|
milter Brightmail 5513
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2003-11-10 09:06:00 +01:00
|
|
|
milter Brightmail bmcluster:5513
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
This plugin has so far only been tested with Brightmail's milter module.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
use Net::Milter;
|
2012-04-08 02:11:16 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2003-10-01 22:57:11 +02:00
|
|
|
no warnings;
|
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ($self, $qp, @args) = @_;
|
|
|
|
|
|
|
|
die "Invalid milter setup args: '@args'" unless @args > 1;
|
|
|
|
my ($name, $port) = @args;
|
|
|
|
my $host = '127.0.0.1';
|
|
|
|
if ($port =~ s/^(.*)://) {
|
|
|
|
$host = $1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{name} = $name;
|
|
|
|
$self->{host} = $host;
|
|
|
|
$self->{port} = $port;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_disconnect {
|
2003-10-01 22:57:11 +02:00
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $milter = $self->qp->connection->notes('milter') || return DECLINED;
|
|
|
|
$milter->send_quit();
|
|
|
|
|
|
|
|
$self->qp->connection->notes('spam', undef);
|
|
|
|
$self->qp->connection->notes('milter', undef);
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check_results {
|
|
|
|
my ($self, $transaction, $where, @results) = @_;
|
|
|
|
foreach my $result (@results) {
|
|
|
|
next if $result->{action} eq 'continue';
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGINFO, "milter $self->{name} result action: $result->{action}");
|
2003-10-01 22:57:11 +02:00
|
|
|
if ($result->{action} eq 'reject') {
|
|
|
|
die("Rejected at $where by $self->{name} milter ($result->{explanation})");
|
|
|
|
}
|
|
|
|
elsif ($result->{action} eq 'add') {
|
|
|
|
if ($result->{header} eq 'body') {
|
|
|
|
$transaction->body_write($result->{value});
|
|
|
|
}
|
|
|
|
else {
|
2004-09-16 12:44:47 +02:00
|
|
|
push @{$transaction->notes('milter_header_changes')->{add}},
|
|
|
|
[$result->{header}, $result->{value}];
|
2003-10-01 22:57:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($result->{action} eq 'delete') {
|
2004-09-16 12:44:47 +02:00
|
|
|
push @{$transaction->notes('milter_header_changes')->{delete}},
|
|
|
|
$result->{header};
|
2003-10-01 22:57:11 +02:00
|
|
|
}
|
|
|
|
elsif ($result->{action} eq 'accept') {
|
|
|
|
# TODO - figure out what this is used for
|
|
|
|
}
|
|
|
|
elsif ($result->{action} eq 'replace') {
|
2004-09-16 12:44:47 +02:00
|
|
|
push @{$transaction->notes('milter_header_changes')->{replace}},
|
|
|
|
[$result->{header}, $result->{value}];
|
2003-10-01 22:57:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_connect {
|
2003-10-01 22:57:11 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} opening connection to milter backend");
|
2003-10-01 22:57:11 +02:00
|
|
|
my $milter = Net::Milter->new();
|
|
|
|
$milter->open($self->{host}, $self->{port}, 'tcp');
|
|
|
|
$milter->protocol_negotiation();
|
|
|
|
|
|
|
|
$self->qp->connection->notes(milter => $milter);
|
2004-09-16 12:44:47 +02:00
|
|
|
|
|
|
|
$self->qp->connection->notes(
|
|
|
|
milter_header_changes => { add => [], delete => [], replace => [], }
|
|
|
|
);
|
2003-10-01 22:57:11 +02:00
|
|
|
my $remote_ip = $self->qp->connection->remote_ip;
|
|
|
|
my $remote_host = $self->qp->connection->remote_host;
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} checking connect from $remote_host\[$remote_ip\]");
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
eval {
|
|
|
|
$self->check_results($transaction, "connection",
|
|
|
|
$milter->send_connect($remote_host, 'tcp4', 0, $remote_ip));
|
|
|
|
};
|
|
|
|
$self->qp->connection->notes('spam', $@) if $@;
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_helo {
|
2003-10-01 22:57:11 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
if (my $txt = $self->qp->connection->notes('spam')) {
|
|
|
|
return DENY, $txt;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $milter = $self->qp->connection->notes('milter');
|
|
|
|
|
|
|
|
my $helo = $self->qp->connection->hello;
|
|
|
|
my $host = $self->qp->connection->hello_host;
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} checking HELO $host");
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
eval { $self->check_results($transaction, "HELO",
|
|
|
|
$milter->send_helo($host)) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_mail {
|
2006-04-07 20:58:02 +02:00
|
|
|
my ($self, $transaction, $address, %param) = @_;
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
my $milter = $self->qp->connection->notes('milter');
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} checking MAIL FROM " . $address->format);
|
2003-10-01 22:57:11 +02:00
|
|
|
eval { $self->check_results($transaction, "MAIL FROM",
|
|
|
|
$milter->send_mail_from($address->format)) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_rcpt {
|
2006-04-07 20:58:02 +02:00
|
|
|
my ($self, $transaction, $address, %param) = @_;
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
my $milter = $self->qp->connection->notes('milter');
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} checking RCPT TO " . $address->format);
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
eval { $self->check_results($transaction, "RCPT TO",
|
|
|
|
$milter->send_rcpt_to($address->format)) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
2005-07-18 13:13:17 +02:00
|
|
|
sub hook_data_post {
|
2003-10-01 22:57:11 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
my $milter = $self->qp->connection->notes('milter');
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} checking headers");
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
my $headers = $transaction->header(); # Mail::Header object
|
|
|
|
foreach my $h ($headers->tags) {
|
|
|
|
# munge these headers because milters prefer them this way
|
|
|
|
$h =~ s/\b(\w)/\U$1/g;
|
|
|
|
$h =~ s/\bid\b/ID/g;
|
|
|
|
foreach my $val ($headers->get($h)) {
|
2004-03-05 13:46:24 +01:00
|
|
|
# $self->log(LOGDEBUG, "milter $self->{name} checking header: $h: $val");
|
2003-10-01 22:57:11 +02:00
|
|
|
eval { $self->check_results($transaction, "header $h",
|
|
|
|
$milter->send_header($h, $val)) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eval { $self->check_results($transaction, "end headers",
|
|
|
|
$milter->send_end_headers()) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
|
|
|
|
$transaction->body_resetpos;
|
|
|
|
|
|
|
|
# skip past headers
|
|
|
|
while (my $line = $transaction->body_getline) {
|
|
|
|
$line =~ s/\r?\n//;
|
|
|
|
$line =~ s/\s*$//;
|
|
|
|
last unless length($line);
|
|
|
|
}
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGDEBUG, "milter $self->{name} checking body");
|
2003-10-01 22:57:11 +02:00
|
|
|
|
|
|
|
my $data = '';
|
|
|
|
while (my $line = $transaction->body_getline) {
|
|
|
|
$data .= $line;
|
|
|
|
if (length($data) > 60000) {
|
|
|
|
eval { $self->check_results($transaction, "body",
|
|
|
|
$milter->send_body($data)) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
$data = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length($data)) {
|
|
|
|
eval { $self->check_results($transaction, "body",
|
|
|
|
$milter->send_body($data)) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
$data = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
eval { $self->check_results($transaction, "end of DATA",
|
|
|
|
$milter->send_end_body()) };
|
|
|
|
return(DENY, $@) if $@;
|
|
|
|
|
2004-09-16 12:44:47 +02:00
|
|
|
my $milter_header_changes = $transaction->notes('milter_header_changes');
|
|
|
|
|
|
|
|
foreach my $add (@{$milter_header_changes->{add}}) {
|
|
|
|
$headers->add($add->[0], $add->[1]);
|
|
|
|
}
|
|
|
|
foreach my $del (@{$milter_header_changes->{'delete'}}) {
|
|
|
|
$headers->delete($del);
|
|
|
|
}
|
|
|
|
foreach my $repl (@{$milter_header_changes->{replace}}) {
|
|
|
|
$headers->replace($repl->[0], $repl->[1]);
|
|
|
|
}
|
|
|
|
|
2003-10-01 22:57:11 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|