2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2013-04-21 06:50:39 +02:00
|
|
|
|
2003-06-09 13:06:41 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
smtp-forward
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This plugin forwards the mail via SMTP to a specified server, rather than
|
|
|
|
delivering the email locally.
|
|
|
|
|
2014-02-13 21:53:33 +01:00
|
|
|
It also supports the Postfix XCLIENT extension.
|
|
|
|
|
2003-06-09 13:06:41 +02:00
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
It takes one required parameter, the IP address or hostname to forward to.
|
|
|
|
|
|
|
|
queue/smtp-forward 10.2.2.2
|
|
|
|
|
|
|
|
Optionally you can also add a port:
|
|
|
|
|
|
|
|
queue/smtp-forward 10.2.2.2 9025
|
|
|
|
|
2014-02-13 21:53:33 +01:00
|
|
|
And a flag:
|
|
|
|
|
|
|
|
queue/smtp-forward 10.2.2.2 9025 xclient
|
|
|
|
|
2003-06-09 13:06:41 +02:00
|
|
|
=cut
|
|
|
|
|
|
|
|
use Net::SMTP;
|
2014-02-13 21:53:33 +01:00
|
|
|
use Net::Cmd qw//;
|
2003-06-09 13:06:41 +02:00
|
|
|
|
2006-03-20 17:47:05 +01:00
|
|
|
sub init {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $qp, @args) = @_;
|
2003-06-09 13:06:41 +02:00
|
|
|
|
2014-02-13 21:53:33 +01:00
|
|
|
if (@args <= 0) {
|
|
|
|
die "No SMTP server specified in smtp-forward config";
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($args[0] =~ /^([\.\w_-]+)$/) {
|
|
|
|
$self->{_smtp_server} = $1;
|
2003-06-09 13:06:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-02-13 21:53:33 +01:00
|
|
|
die "Bad data in smtp server: $args[0]";
|
2003-06-09 13:06:41 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 21:53:33 +01:00
|
|
|
$self->{_smtp_port} = 25;
|
|
|
|
if (@args > 1 and $args[1] =~ /^(\d+)$/) {
|
|
|
|
$self->{_smtp_port} = $1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (my $i = 2; $i < @args; $i++) {
|
|
|
|
if ($args[$i] !~ /^(\w+)$/) {
|
|
|
|
$self->log(LOGWARN, "WARNING: Rejecting invalid flag");
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
my $flag = lc($1);
|
|
|
|
$self->log(LOGWARN, "WARNING: Unknown flag $flag") unless $flag eq 'xclient';
|
|
|
|
$self->{_flags}{$flag} = 1;
|
|
|
|
}
|
2003-06-09 13:06:41 +02:00
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_queue {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
$self->log(LOGINFO,
|
|
|
|
"forwarding to $self->{_smtp_server}:$self->{_smtp_port}");
|
|
|
|
my $smtp = Net::SMTP->new(
|
|
|
|
$self->{_smtp_server},
|
|
|
|
Port => $self->{_smtp_port},
|
|
|
|
Timeout => 60,
|
|
|
|
Hello => $self->qp->config("me"),
|
2014-02-13 21:53:33 +01:00
|
|
|
) || die $!;
|
|
|
|
|
|
|
|
|
|
|
|
my $xcret = $self->xclient($smtp);
|
|
|
|
return(DECLINED, $xcret) if defined $xcret;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$smtp->mail($transaction->sender->address || "")
|
2014-09-18 03:28:51 +02:00
|
|
|
or return DECLINED, "Unable to queue message ($!)";
|
2013-04-21 06:50:39 +02:00
|
|
|
for ($transaction->recipients) {
|
|
|
|
$smtp->to($_->address)
|
2014-09-18 03:28:51 +02:00
|
|
|
or return DECLINED, "Unable to queue message ($!)";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
$smtp->data() or return DECLINED, "Unable to queue message ($!)";
|
2013-04-21 06:50:39 +02:00
|
|
|
$smtp->datasend($transaction->header->as_string)
|
2014-09-18 03:28:51 +02:00
|
|
|
or return DECLINED, "Unable to queue message ($!)";
|
2013-04-21 06:50:39 +02:00
|
|
|
$transaction->body_resetpos;
|
|
|
|
while (my $line = $transaction->body_getline) {
|
|
|
|
$smtp->datasend($line)
|
2014-09-18 03:28:51 +02:00
|
|
|
or return DECLINED, "Unable to queue message ($!)";
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2014-09-18 03:28:51 +02:00
|
|
|
$smtp->dataend() or return DECLINED, "Unable to queue message ($!)";
|
2014-01-24 15:11:39 +01:00
|
|
|
my $qid = $smtp->message();
|
|
|
|
my @list = split(' ', $qid);
|
|
|
|
$qid = pop(@list);
|
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
$smtp->quit() or return DECLINED, "Unable to queue message ($!)";
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGINFO, "finished queueing");
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK, "queued as $qid";
|
2003-06-09 13:06:41 +02:00
|
|
|
}
|
2014-02-13 21:53:33 +01:00
|
|
|
|
|
|
|
sub xclient {
|
|
|
|
my ($self, $smtp) = @_;
|
|
|
|
|
|
|
|
return unless $self->{_flags}{xclient};
|
|
|
|
|
|
|
|
my $parts = $smtp->supports('XCLIENT');
|
|
|
|
if (!defined($parts)) { # what parts do they want?
|
|
|
|
return "Unable to queue message (Server does not advertise XCLIENT support)";
|
|
|
|
};
|
|
|
|
|
|
|
|
my %haveparts;
|
|
|
|
for my $part (split(/\s+/, $parts)) {
|
|
|
|
next unless $part =~ /^(\w+)$/;
|
|
|
|
$haveparts{uc($part)} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $conn = $self->qp->connection;
|
|
|
|
my @rparts;
|
|
|
|
|
|
|
|
if ($haveparts{NAME}) {
|
|
|
|
my $name = $conn->remote_host || '[UNAVAILABLE]';
|
|
|
|
$name = '[UNAVAILABLE]' if ($name eq 'Unknown');
|
|
|
|
push(@rparts, "NAME=$name");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($haveparts{ADDR}) {
|
|
|
|
my $ip = $conn->remote_ip;
|
|
|
|
push(@rparts, "ADDR=$ip");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($haveparts{PORT}) {
|
|
|
|
my $port = $conn->remote_port;
|
|
|
|
push(@rparts, "PORT=$port");
|
|
|
|
}
|
|
|
|
|
|
|
|
my $hello_name = $self->connection->hello_host;
|
|
|
|
$hello_name ||= '[UNAVAILABLE]';
|
|
|
|
if ($haveparts{HELO}) {
|
|
|
|
push(@rparts, "HELO=$hello_name");
|
|
|
|
}
|
|
|
|
|
|
|
|
my $hello = $conn->hello;
|
|
|
|
if ($haveparts{PROTO} && defined($hello)) {
|
|
|
|
my $proto = (uc($hello) eq 'EHLO') ? 'ESMTP' : 'SMTP';
|
|
|
|
push(@rparts, "PROTO=$proto");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (scalar(@rparts)) {
|
|
|
|
my @items;
|
|
|
|
my $cursz = 0;
|
|
|
|
while (defined(my $item = $rparts[0])) {
|
|
|
|
my $len = length($item);
|
|
|
|
last if ($cursz + $len > 500);
|
|
|
|
$cursz += $len;
|
|
|
|
push(@items, shift @rparts);
|
|
|
|
}
|
|
|
|
|
|
|
|
last unless @items;
|
|
|
|
if ($smtp->command('XCLIENT', @items)->response() != Net::Cmd::CMD_OK) {
|
|
|
|
return "Unable to queue message (XCLIENT failed)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$smtp->hello($hello_name) or return "Unable to queue message (HELLO after XCLIENT failed)";
|
|
|
|
return;
|
|
|
|
}
|