| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
qmail-queue |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This is the most common plugin used to queue incoming mails. A |
| 10 |
variation of this plugin would maybe forward the mail via smtp. |
| 11 |
|
| 12 |
=head1 CONFIG |
| 13 |
|
| 14 |
It takes one optional parameter, the location of qmail-queue. This |
| 15 |
makes it easy to use a qmail-queue replacement. |
| 16 |
|
| 17 |
queue/qmail-queue /var/qmail/bin/another-qmail-queue |
| 18 |
|
| 19 |
If set the environment variable QMAILQUEUE overrides this setting. |
| 20 |
|
| 21 |
=cut |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
|
| 26 |
use Qpsmtpd::Constants; |
| 27 |
use POSIX (); |
| 28 |
|
| 29 |
sub register { |
| 30 |
my ($self, $qp, @args) = @_; |
| 31 |
|
| 32 |
if (@args > 0) { |
| 33 |
$self->{_queue_exec} = $args[0]; |
| 34 |
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.") |
| 35 |
if @args > 1; |
| 36 |
} |
| 37 |
|
| 38 |
$self->{_queue_exec} ||= ($ENV{QMAIL} || '/var/qmail') . "/bin/qmail-queue"; |
| 39 |
$self->{_queue_exec} = $ENV{QMAILQUEUE} if $ENV{QMAILQUEUE}; |
| 40 |
} |
| 41 |
|
| 42 |
sub hook_queue { |
| 43 |
my ($self, $transaction) = @_; |
| 44 |
|
| 45 |
# these bits inspired by Peter Samuels "qmail-queue wrapper" |
| 46 |
pipe(MESSAGE_READER, MESSAGE_WRITER) or die "Could not create message pipe"; |
| 47 |
pipe(ENVELOPE_READER, ENVELOPE_WRITER) |
| 48 |
or die "Could not create envelope pipe"; |
| 49 |
|
| 50 |
local $SIG{PIPE} = sub { die 'SIGPIPE' }; |
| 51 |
my $child = fork(); |
| 52 |
|
| 53 |
!defined $child and die "Could not fork"; |
| 54 |
|
| 55 |
if ($child) { |
| 56 |
|
| 57 |
# Parent |
| 58 |
my $oldfh = select MESSAGE_WRITER; |
| 59 |
$| = 1; |
| 60 |
select ENVELOPE_WRITER; |
| 61 |
$| = 1; |
| 62 |
select $oldfh; |
| 63 |
|
| 64 |
close MESSAGE_READER or die "close msg reader fault"; |
| 65 |
close ENVELOPE_READER or die "close envelope reader fault"; |
| 66 |
|
| 67 |
$transaction->header->print(\*MESSAGE_WRITER); |
| 68 |
$transaction->body_resetpos; |
| 69 |
while (my $line = $transaction->body_getline) { |
| 70 |
print MESSAGE_WRITER $line; |
| 71 |
} |
| 72 |
close MESSAGE_WRITER; |
| 73 |
|
| 74 |
my @rcpt = map { "T" . $_->address } $transaction->recipients; |
| 75 |
my $from = "F" . ($transaction->sender->address || ""); |
| 76 |
print ENVELOPE_WRITER "$from\0", join("\0", @rcpt), "\0\0" |
| 77 |
or return (DECLINED, "Could not print addresses to queue"); |
| 78 |
|
| 79 |
close ENVELOPE_WRITER; |
| 80 |
waitpid($child, 0); |
| 81 |
my $exit_code = $? >> 8; |
| 82 |
$exit_code |
| 83 |
and return (DECLINED, "Unable to queue message ($exit_code)"); |
| 84 |
|
| 85 |
my $msg_id = $transaction->header->get('Message-Id') || ''; |
| 86 |
$msg_id =~ s/[\r\n].*//s; # don't allow newlines in the Message-Id here |
| 87 |
$msg_id = "<$msg_id>" unless $msg_id =~ /^<.*>$/; # surround in <>'s |
| 88 |
return (OK, "Queued! " . time . " qp $child $msg_id"); |
| 89 |
} |
| 90 |
elsif (defined $child) { |
| 91 |
|
| 92 |
# Child |
| 93 |
close MESSAGE_WRITER or exit 1; |
| 94 |
close ENVELOPE_WRITER or exit 2; |
| 95 |
|
| 96 |
# Untaint $self->{_queue_exec} |
| 97 |
my $queue_exec = $self->{_queue_exec}; |
| 98 |
if ($queue_exec =~ /^(\/[\/\-\_\.a-z0-9A-Z]*)$/) { |
| 99 |
$queue_exec = $1; |
| 100 |
} |
| 101 |
else { |
| 102 |
$self->log(LOGERROR, |
| 103 |
"FATAL ERROR: Unexpected characters in qmail-queue plugin argument" |
| 104 |
); |
| 105 |
|
| 106 |
# This exit is ok as we're exiting a forked child process. |
| 107 |
exit 3; |
| 108 |
} |
| 109 |
|
| 110 |
# save the original STDIN and STDOUT in case exec() fails below |
| 111 |
open(SAVE_STDIN, "<&STDIN"); |
| 112 |
open(SAVE_STDOUT, ">&STDOUT"); |
| 113 |
|
| 114 |
POSIX::dup2(fileno(MESSAGE_READER), 0) |
| 115 |
or die "Unable to dup MESSAGE_READER: $!"; |
| 116 |
POSIX::dup2(fileno(ENVELOPE_READER), 1) |
| 117 |
or die "Unable to dup ENVELOPE_READER: $!"; |
| 118 |
|
| 119 |
my $ppid = getppid(); |
| 120 |
$self->log(LOGNOTICE, "(for $ppid) Queuing to $queue_exec"); |
| 121 |
|
| 122 |
my $rc = exec $queue_exec; |
| 123 |
|
| 124 |
# close the pipe |
| 125 |
close(MESSAGE_READER); |
| 126 |
close(MESSAGE_WRITER); |
| 127 |
|
| 128 |
exit 6; # we'll only get here if the exec fails |
| 129 |
} |
| 130 |
} |