| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
exim-bsmtp |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This plugin enqueues mail from qpsmtpd into Exim via BSMTP |
| 10 |
|
| 11 |
=head1 INSTALLATION |
| 12 |
|
| 13 |
The qpsmtpd user B<must> be configured in the I<trusted_users> setting |
| 14 |
in your Exim configuration. If it is not, queueing will still work, |
| 15 |
but sender addresses will not be honored by exim, which will make all |
| 16 |
mail appear to originate from the smtpd user itself. |
| 17 |
|
| 18 |
=head1 CONFIGURATION |
| 19 |
|
| 20 |
The plugin accepts configuration settings in space-delimited name/value |
| 21 |
pairs. For example: |
| 22 |
|
| 23 |
queue/exim-bsmtp exim_path /usr/sbin/exim4 |
| 24 |
|
| 25 |
=over 4 |
| 26 |
|
| 27 |
=item exim_path I<path> |
| 28 |
|
| 29 |
The path to use to execute the Exim BSMTP receiver; by default this is |
| 30 |
I</usr/sbin/rsmtp>. The commandline switch '-bS' will be added (this is |
| 31 |
actually redundant with rsmtp, but harmless). |
| 32 |
|
| 33 |
=back |
| 34 |
|
| 35 |
=head1 LICENSE |
| 36 |
|
| 37 |
Copyright (c) 2004 by Devin Carraway <[email protected]> |
| 38 |
|
| 39 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 40 |
of this software and associated documentation files (the "Software"), to deal |
| 41 |
in the Software without restriction, including without limitation the rights |
| 42 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 43 |
copies of the Software, and to permit persons to whom the Software is |
| 44 |
furnished to do so, subject to the following conditions: |
| 45 |
|
| 46 |
The above copyright notice and this permission notice shall be included in all |
| 47 |
copies or substantial portions of the Software. |
| 48 |
|
| 49 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 50 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 51 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 52 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 53 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 54 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 55 |
SOFTWARE. |
| 56 |
|
| 57 |
=cut |
| 58 |
|
| 59 |
use strict; |
| 60 |
use warnings; |
| 61 |
|
| 62 |
use IO::File; |
| 63 |
use Sys::Hostname qw(hostname); |
| 64 |
use File::Temp qw(tempfile); |
| 65 |
use Qpsmtpd::Constants; |
| 66 |
|
| 67 |
sub register { |
| 68 |
my ($self, $qp, %args) = @_; |
| 69 |
|
| 70 |
$self->{_exim_path} = $args{exim_path} || '/usr/sbin/rsmtp'; |
| 71 |
$self->{_exim_path} = $1 if $self->{_exim_path} =~ /(.*)/; |
| 72 |
unless (-x $self->{_exim_path}) { |
| 73 |
$self->log(LOGERROR, |
| 74 |
"Could not find exim at $self->{_exim_path};" |
| 75 |
. " please set exim_path in config/plugins" |
| 76 |
); |
| 77 |
return undef; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
sub hook_queue { |
| 82 |
my ($self, $transaction) = @_; |
| 83 |
|
| 84 |
unless ($transaction->header) { |
| 85 |
$self->log(LOGERROR, "No header parsed for transaction; can't enqueue"); |
| 86 |
return (DENY, 'Mail unqueuable'); |
| 87 |
} |
| 88 |
my $tmp_dir = $self->qp->config('spool_dir') || '/tmp'; |
| 89 |
$tmp_dir = $1 if ($tmp_dir =~ /(.*)/); |
| 90 |
my ($tmp, $tmpfn) = tempfile("exim-bsmtp.$$.XXXXXX", DIR => $tmp_dir); |
| 91 |
unless ($tmp && $tmpfn) { |
| 92 |
$self->log(LOGERROR, "Couldn't create tempfile: $!"); |
| 93 |
return (DECLINED, 'Internal error enqueueing mail'); |
| 94 |
} |
| 95 |
|
| 96 |
print $tmp "HELO ", hostname(), "\n", |
| 97 |
"MAIL FROM:<", ($transaction->sender->address || ''), ">\n"; |
| 98 |
print $tmp "RCPT TO:<", ($_->address || ''), ">\n" |
| 99 |
for $transaction->recipients; |
| 100 |
print $tmp "DATA\n", $transaction->header->as_string; |
| 101 |
$transaction->body_resetpos; |
| 102 |
while (my $line = $transaction->body_getline) { |
| 103 |
$line =~ s/^\./../; |
| 104 |
print $tmp $line; |
| 105 |
} |
| 106 |
print $tmp ".\nQUIT\n"; |
| 107 |
close $tmp; |
| 108 |
|
| 109 |
my $cmd = "$self->{_exim_path} -bS < $tmpfn"; |
| 110 |
$self->log(LOGDEBUG, "executing cmd $cmd"); |
| 111 |
my $exim = new IO::File "$cmd|"; |
| 112 |
unless ($exim) { |
| 113 |
$self->log(LOGERROR, "Could not execute $self->{_exim_path}: $!"); |
| 114 |
unlink $tmpfn or $self->log(LOGERROR, "unlink: $tmpfn: $!"); |
| 115 |
return (DECLINED, "Internal error enqueuing mail"); |
| 116 |
} |
| 117 |
|
| 118 |
# Normally exim produces no output in BSMTP mode; anything that |
| 119 |
# does come out is an error worth logging. |
| 120 |
my $start = time; |
| 121 |
my ($bsmtp_error, $bsmtp_msg); |
| 122 |
while (<$exim>) { |
| 123 |
chomp; |
| 124 |
$self->log(LOGERROR, "exim: $_"); |
| 125 |
if (/(\d\d\d)\s(\S.*)/) { |
| 126 |
($bsmtp_error, $bsmtp_msg) = ($1, $2); |
| 127 |
} |
| 128 |
} |
| 129 |
$self->log(LOGDEBUG, "BSMTP finished (" . (time - $start) . " sec)"); |
| 130 |
$exim->close; |
| 131 |
my $exit = $?; |
| 132 |
unlink $tmpfn or $self->log(LOGERROR, "unlink: $tmpfn: $!"); |
| 133 |
|
| 134 |
$self->log(LOGDEBUG, "Exitcode from exim: $exit"); |
| 135 |
if ($bsmtp_error && $bsmtp_error >= 400 && $bsmtp_error < 600) { |
| 136 |
$self->log(LOGERROR, |
| 137 |
"BSMTP enqueue failed; response $bsmtp_error" . " ($bsmtp_msg)"); |
| 138 |
return ($bsmtp_error < 400 ? DECLINED : DENY, $bsmtp_msg); |
| 139 |
} |
| 140 |
elsif (($exit >> 8) != 0 || $bsmtp_error) { |
| 141 |
$self->log(LOGERROR, |
| 142 |
'BSMTP enqueue failed; exitcode ' |
| 143 |
. ($exit >> 8) |
| 144 |
. " from $self->{_exim_path} -bS" |
| 145 |
); |
| 146 |
return (DECLINED, 'Internal error enqueuing mail'); |
| 147 |
} |
| 148 |
|
| 149 |
$self->log(LOGINFO, "Enqueued to exim via BSMTP"); |
| 150 |
return (OK, "Queued!"); |
| 151 |
} |
| 152 |
|