2012-04-29 05:41:31 +02:00
|
|
|
#!perl -Tw
|
2005-07-29 09:41:10 +02:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
exim-bsmtp
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This plugin enqueues mail from qpsmtpd into Exim via BSMTP
|
|
|
|
|
|
|
|
=head1 INSTALLATION
|
|
|
|
|
|
|
|
The qpsmtpd user B<must> be configured in the I<trusted_users> setting
|
|
|
|
in your Exim configuration. If it is not, queueing will still work,
|
|
|
|
but sender addresses will not be honored by exim, which will make all
|
|
|
|
mail appear to originate from the smtpd user itself.
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
|
|
|
The plugin accepts configuration settings in space-delimited name/value
|
|
|
|
pairs. For example:
|
|
|
|
|
|
|
|
queue/exim-bsmtp exim_path /usr/sbin/exim4
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item exim_path I<path>
|
|
|
|
|
|
|
|
The path to use to execute the Exim BSMTP receiver; by default this is
|
|
|
|
I</usr/sbin/rsmtp>. The commandline switch '-bS' will be added (this is
|
|
|
|
actually redundant with rsmtp, but harmless).
|
|
|
|
|
2007-09-03 17:47:08 +02:00
|
|
|
=back
|
|
|
|
|
2005-07-29 09:41:10 +02:00
|
|
|
=head1 LICENSE
|
|
|
|
|
|
|
|
Copyright (c) 2004 by Devin Carraway <qpsmtpd@devin.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use IO::File;
|
|
|
|
use Sys::Hostname qw(hostname);
|
|
|
|
use File::Temp qw(tempfile);
|
2012-04-08 02:11:16 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2005-07-29 09:41:10 +02:00
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ($self, $qp, %args) = @_;
|
|
|
|
|
|
|
|
$self->{_exim_path} = $args{exim_path} || '/usr/sbin/rsmtp';
|
|
|
|
$self->{_exim_path} = $1 if $self->{_exim_path} =~ /(.*)/;
|
|
|
|
unless (-x $self->{_exim_path}) {
|
|
|
|
$self->log(LOGERROR, "Could not find exim at $self->{_exim_path};".
|
|
|
|
" please set exim_path in config/plugins");
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_queue {
|
2008-05-05 19:05:38 +02:00
|
|
|
my ($self, $transaction) = @_;
|
2005-07-29 09:41:10 +02:00
|
|
|
|
2009-12-22 08:50:45 +01:00
|
|
|
unless ($transaction->header) {
|
|
|
|
$self->log(LOGERROR, "No header parsed for transaction; can't enqueue");
|
|
|
|
return (DENY, 'Mail unqueuable');
|
|
|
|
}
|
2005-07-29 09:41:10 +02:00
|
|
|
my $tmp_dir = $self->qp->config('spool_dir') || '/tmp';
|
|
|
|
$tmp_dir = $1 if ($tmp_dir =~ /(.*)/);
|
|
|
|
my ($tmp, $tmpfn) = tempfile("exim-bsmtp.$$.XXXXXX", DIR => $tmp_dir);
|
|
|
|
unless ($tmp && $tmpfn) {
|
2009-12-22 08:50:45 +01:00
|
|
|
$self->log(LOGERROR, "Couldn't create tempfile: $!");
|
|
|
|
return (DECLINED, 'Internal error enqueueing mail');
|
2005-07-29 09:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print $tmp "HELO ", hostname(), "\n",
|
2008-05-05 19:05:38 +02:00
|
|
|
"MAIL FROM:<", ($transaction->sender->address || ''), ">\n";
|
2005-07-29 09:41:10 +02:00
|
|
|
print $tmp "RCPT TO:<", ($_->address || ''), ">\n"
|
2008-05-05 19:05:38 +02:00
|
|
|
for $transaction->recipients;
|
|
|
|
print $tmp "DATA\n", $transaction->header->as_string;
|
|
|
|
$transaction->body_resetpos;
|
|
|
|
while (my $line = $transaction->body_getline) {
|
2005-07-29 09:41:10 +02:00
|
|
|
$line =~ s/^\./../;
|
|
|
|
print $tmp $line;
|
|
|
|
}
|
|
|
|
print $tmp ".\nQUIT\n";
|
|
|
|
close $tmp;
|
|
|
|
|
|
|
|
my $cmd = "$self->{_exim_path} -bS < $tmpfn";
|
|
|
|
$self->log(LOGDEBUG, "executing cmd $cmd");
|
|
|
|
my $exim = new IO::File "$cmd|";
|
|
|
|
unless ($exim) {
|
|
|
|
$self->log(LOGERROR, "Could not execute $self->{_exim_path}: $!");
|
|
|
|
unlink $tmpfn or $self->log(LOGERROR, "unlink: $tmpfn: $!");
|
2008-05-05 19:05:38 +02:00
|
|
|
return (DECLINED, "Internal error enqueuing mail");
|
2005-07-29 09:41:10 +02:00
|
|
|
}
|
|
|
|
# Normally exim produces no output in BSMTP mode; anything that
|
|
|
|
# does come out is an error worth logging.
|
|
|
|
my $start = time;
|
2009-12-22 08:50:45 +01:00
|
|
|
my ($bsmtp_error, $bsmtp_msg);
|
2005-07-29 09:41:10 +02:00
|
|
|
while (<$exim>) {
|
2009-12-22 08:50:45 +01:00
|
|
|
chomp;
|
|
|
|
$self->log(LOGERROR, "exim: $_");
|
|
|
|
if (/(\d\d\d)\s(\S.*)/) {
|
|
|
|
($bsmtp_error, $bsmtp_msg) = ($1, $2);
|
|
|
|
}
|
2005-07-29 09:41:10 +02:00
|
|
|
}
|
|
|
|
$self->log(LOGDEBUG, "BSMTP finished (".(time - $start)." sec)");
|
|
|
|
$exim->close;
|
|
|
|
my $exit = $?;
|
|
|
|
unlink $tmpfn or $self->log(LOGERROR, "unlink: $tmpfn: $!");
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "Exitcode from exim: $exit");
|
2009-12-22 08:50:45 +01:00
|
|
|
if ($bsmtp_error && $bsmtp_error >= 400 && $bsmtp_error < 600) {
|
|
|
|
$self->log(LOGERROR, "BSMTP enqueue failed; response $bsmtp_error".
|
|
|
|
" ($bsmtp_msg)");
|
|
|
|
return ($bsmtp_error < 400 ? DECLINED : DENY, $bsmtp_msg);
|
|
|
|
}
|
|
|
|
elsif (($exit >> 8) != 0 || $bsmtp_error) {
|
2005-07-29 09:41:10 +02:00
|
|
|
$self->log(LOGERROR, 'BSMTP enqueue failed; exitcode '.($exit >> 8).
|
|
|
|
" from $self->{_exim_path} -bS");
|
|
|
|
return (DECLINED, 'Internal error enqueuing mail');
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->log(LOGINFO, "Enqueued to exim via BSMTP");
|
|
|
|
return (OK, "Queued!");
|
|
|
|
}
|
|
|
|
|