2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2012-04-08 02:11:16 +02:00
|
|
|
|
2003-01-20 12:01:32 +01:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
qmail-queue
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This is the most common plugin used to queue incoming mails. A
|
|
|
|
variation of this plugin would maybe forward the mail via smtp.
|
|
|
|
|
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
It takes one optional parameter, the location of qmail-queue. This
|
|
|
|
makes it easy to use a qmail-queue replacement.
|
|
|
|
|
|
|
|
queue/qmail-queue /var/qmail/bin/another-qmail-queue
|
|
|
|
|
|
|
|
If set the environment variable QMAILQUEUE overrides this setting.
|
|
|
|
|
|
|
|
=cut
|
2002-10-10 03:49:34 +02:00
|
|
|
|
2012-06-23 06:57:04 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Qpsmtpd::Constants;
|
2003-10-09 19:21:49 +02:00
|
|
|
use POSIX ();
|
|
|
|
|
2002-10-10 03:49:34 +02:00
|
|
|
sub register {
|
2012-06-23 06:57:04 +02:00
|
|
|
my ($self, $qp, @args) = @_;
|
2003-01-20 12:01:32 +01:00
|
|
|
|
2012-06-23 06:57:04 +02:00
|
|
|
if (@args > 0) {
|
|
|
|
$self->{_queue_exec} = $args[0];
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.")
|
|
|
|
if @args > 1;
|
2012-06-23 06:57:04 +02:00
|
|
|
}
|
2003-01-20 12:01:32 +01:00
|
|
|
|
2012-06-23 06:57:04 +02:00
|
|
|
$self->{_queue_exec} ||= ($ENV{QMAIL} || '/var/qmail') . "/bin/qmail-queue";
|
|
|
|
$self->{_queue_exec} = $ENV{QMAILQUEUE} if $ENV{QMAILQUEUE};
|
2002-10-10 03:49:34 +02:00
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_queue {
|
2012-06-23 06:57:04 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# these bits inspired by Peter Samuels "qmail-queue wrapper"
|
2012-06-23 06:57:04 +02:00
|
|
|
pipe(MESSAGE_READER, MESSAGE_WRITER) or die "Could not create message pipe";
|
2013-04-21 06:50:39 +02:00
|
|
|
pipe(ENVELOPE_READER, ENVELOPE_WRITER)
|
|
|
|
or die "Could not create envelope pipe";
|
2012-06-23 06:57:04 +02:00
|
|
|
|
|
|
|
local $SIG{PIPE} = sub { die 'SIGPIPE' };
|
|
|
|
my $child = fork();
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
!defined $child and die "Could not fork";
|
2012-06-23 06:57:04 +02:00
|
|
|
|
|
|
|
if ($child) {
|
2013-04-21 06:50:39 +02:00
|
|
|
|
|
|
|
# Parent
|
|
|
|
my $oldfh = select MESSAGE_WRITER;
|
|
|
|
$| = 1;
|
|
|
|
select ENVELOPE_WRITER;
|
|
|
|
$| = 1;
|
2012-06-23 06:57:04 +02:00
|
|
|
select $oldfh;
|
|
|
|
|
|
|
|
close MESSAGE_READER or die "close msg reader fault";
|
|
|
|
close ENVELOPE_READER or die "close envelope reader fault";
|
|
|
|
|
|
|
|
$transaction->header->print(\*MESSAGE_WRITER);
|
|
|
|
$transaction->body_resetpos;
|
|
|
|
while (my $line = $transaction->body_getline) {
|
|
|
|
print MESSAGE_WRITER $line;
|
|
|
|
}
|
|
|
|
close MESSAGE_WRITER;
|
|
|
|
|
|
|
|
my @rcpt = map { "T" . $_->address } $transaction->recipients;
|
2013-04-21 06:50:39 +02:00
|
|
|
my $from = "F" . ($transaction->sender->address || "");
|
|
|
|
print ENVELOPE_WRITER "$from\0", join("\0", @rcpt), "\0\0"
|
2014-09-18 03:28:51 +02:00
|
|
|
or return DECLINED, "Could not print addresses to queue";
|
2012-06-23 06:57:04 +02:00
|
|
|
|
|
|
|
close ENVELOPE_WRITER;
|
|
|
|
waitpid($child, 0);
|
|
|
|
my $exit_code = $? >> 8;
|
2013-04-21 06:50:39 +02:00
|
|
|
$exit_code
|
2014-09-18 03:28:51 +02:00
|
|
|
and return DECLINED, "Unable to queue message ($exit_code)";
|
2012-06-23 06:57:04 +02:00
|
|
|
|
|
|
|
my $msg_id = $transaction->header->get('Message-Id') || '';
|
|
|
|
$msg_id =~ s/[\r\n].*//s; # don't allow newlines in the Message-Id here
|
2013-04-21 06:50:39 +02:00
|
|
|
$msg_id = "<$msg_id>" unless $msg_id =~ /^<.*>$/; # surround in <>'s
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK, "Queued! " . time . " qp $child $msg_id";
|
2002-10-10 03:49:34 +02:00
|
|
|
}
|
2012-06-23 06:57:04 +02:00
|
|
|
elsif (defined $child) {
|
2013-04-21 06:50:39 +02:00
|
|
|
|
|
|
|
# Child
|
|
|
|
close MESSAGE_WRITER or exit 1;
|
2012-06-23 06:57:04 +02:00
|
|
|
close ENVELOPE_WRITER or exit 2;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# Untaint $self->{_queue_exec}
|
2012-06-23 06:57:04 +02:00
|
|
|
my $queue_exec = $self->{_queue_exec};
|
|
|
|
if ($queue_exec =~ /^(\/[\/\-\_\.a-z0-9A-Z]*)$/) {
|
|
|
|
$queue_exec = $1;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->log(LOGERROR,
|
|
|
|
"FATAL ERROR: Unexpected characters in qmail-queue plugin argument"
|
|
|
|
);
|
|
|
|
|
|
|
|
# This exit is ok as we're exiting a forked child process.
|
2012-06-23 06:57:04 +02:00
|
|
|
exit 3;
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# save the original STDIN and STDOUT in case exec() fails below
|
|
|
|
open(SAVE_STDIN, "<&STDIN");
|
2012-06-23 06:57:04 +02:00
|
|
|
open(SAVE_STDOUT, ">&STDOUT");
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
POSIX::dup2(fileno(MESSAGE_READER), 0)
|
|
|
|
or die "Unable to dup MESSAGE_READER: $!";
|
|
|
|
POSIX::dup2(fileno(ENVELOPE_READER), 1)
|
|
|
|
or die "Unable to dup ENVELOPE_READER: $!";
|
2012-06-23 06:57:04 +02:00
|
|
|
|
|
|
|
my $ppid = getppid();
|
|
|
|
$self->log(LOGNOTICE, "(for $ppid) Queuing to $queue_exec");
|
|
|
|
|
|
|
|
my $rc = exec $queue_exec;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# close the pipe
|
2012-06-23 06:57:04 +02:00
|
|
|
close(MESSAGE_READER);
|
|
|
|
close(MESSAGE_WRITER);
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
exit 6; # we'll only get here if the exec fails
|
2002-10-10 03:49:34 +02:00
|
|
|
}
|
|
|
|
}
|