qpsmtpd/plugins/queue/exim-bsmtp

152 lines
4.9 KiB
Plaintext
Raw Permalink Normal View History

#!perl -w
=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).
=back
=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);
POD corrections, additional tests, plugin consistency on files in plugins dir: fixed a number of POD errors formatted some # comments into POD removed bare 1; (these are plugins, not perl modules) most instances of this were copy/pasted from a previous plugin that had it removed instances of # vim ts=N ... they weren't consistent, many didn't match .perltidyrc on modules that failed perl -c tests, added 'use Qpsmtpd::Constants;' Conflicts: plugins/async/check_earlytalker plugins/async/dns_whitelist_soft plugins/async/dnsbl plugins/async/queue/smtp-forward plugins/async/require_resolvable_fromhost plugins/async/rhsbl plugins/async/uribl plugins/auth/auth_checkpassword plugins/auth/auth_cvm_unix_local plugins/auth/auth_flat_file plugins/auth/auth_ldap_bind plugins/auth/auth_vpopmail plugins/auth/auth_vpopmail_sql plugins/auth/authdeny plugins/check_badmailfromto plugins/check_badrcptto_patterns plugins/check_bogus_bounce plugins/check_earlytalker plugins/check_norelay plugins/check_spamhelo plugins/connection_time plugins/dns_whitelist_soft plugins/dnsbl plugins/domainkeys plugins/greylisting plugins/hosts_allow plugins/http_config plugins/logging/adaptive plugins/logging/apache plugins/logging/connection_id plugins/logging/transaction_id plugins/logging/warn plugins/milter plugins/queue/exim-bsmtp plugins/queue/maildir plugins/queue/postfix-queue plugins/queue/smtp-forward plugins/quit_fortune plugins/random_error plugins/rcpt_map plugins/rcpt_regexp plugins/relay_only plugins/require_resolvable_fromhost plugins/rhsbl plugins/sender_permitted_from plugins/spamassassin plugins/tls plugins/tls_cert plugins/uribl plugins/virus/aveclient plugins/virus/bitdefender plugins/virus/clamav plugins/virus/clamdscan plugins/virus/hbedv plugins/virus/kavscanner plugins/virus/klez_filter plugins/virus/sophie plugins/virus/uvscan
2012-04-08 02:11:16 +02:00
use Qpsmtpd::Constants;
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 {
my ($self, $transaction) = @_;
unless ($transaction->header) {
$self->log(LOGERROR, "No header parsed for transaction; can't enqueue");
return DENY, 'Mail unqueuable';
}
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) {
$self->log(LOGERROR, "Couldn't create tempfile: $!");
return DECLINED, 'Internal error enqueueing mail';
}
print $tmp "HELO ", hostname(), "\n",
"MAIL FROM:<", ($transaction->sender->address || ''), ">\n";
print $tmp "RCPT TO:<", ($_->address || ''), ">\n"
for $transaction->recipients;
print $tmp "DATA\n", $transaction->header->as_string;
$transaction->body_resetpos;
while (my $line = $transaction->body_getline) {
$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: $!");
return DECLINED, "Internal error enqueuing mail";
}
# Normally exim produces no output in BSMTP mode; anything that
# does come out is an error worth logging.
my $start = time;
my ($bsmtp_error, $bsmtp_msg);
while (<$exim>) {
chomp;
$self->log(LOGERROR, "exim: $_");
if (/(\d\d\d)\s(\S.*)/) {
($bsmtp_error, $bsmtp_msg) = ($1, $2);
}
}
$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");
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) {
$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!";
}