qmail-queue: a few tweaks and a lot of whitespace

This commit is contained in:
Matt Simerson 2012-06-23 00:57:04 -04:00
parent 6afcc54f4b
commit 8c3377f026

View File

@ -20,6 +20,11 @@ If set the environment variable QMAILQUEUE overrides this setting.
=cut =cut
use strict;
use warnings;
use Qpsmtpd::Constants;
use POSIX (); use POSIX ();
sub register { sub register {
@ -27,35 +32,33 @@ sub register {
if (@args > 0) { if (@args > 0) {
$self->{_queue_exec} = $args[0]; $self->{_queue_exec} = $args[0];
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.") if (@args > 1); $self->log(LOGWARN, "WARNING: Ignoring additional arguments.") if @args > 1;
}
else {
$self->{_queue_exec} = ($ENV{QMAIL} || '/var/qmail') . "/bin/qmail-queue";
} }
$self->{_queue_exec} ||= ($ENV{QMAIL} || '/var/qmail') . "/bin/qmail-queue";
$self->{_queue_exec} = $ENV{QMAILQUEUE} if $ENV{QMAILQUEUE}; $self->{_queue_exec} = $ENV{QMAILQUEUE} if $ENV{QMAILQUEUE};
} }
sub hook_queue { sub hook_queue {
my ($self, $transaction) = @_; my ($self, $transaction) = @_;
# these bits inspired by Peter Samuels "qmail-queue wrapper" # these bits inspired by Peter Samuels "qmail-queue wrapper"
pipe(MESSAGE_READER, MESSAGE_WRITER) or die("Could not create message pipe"); pipe(MESSAGE_READER, MESSAGE_WRITER) or die "Could not create message pipe";
pipe(ENVELOPE_READER, ENVELOPE_WRITER) or die("Could not create envelope pipe"); pipe(ENVELOPE_READER, ENVELOPE_WRITER) or die "Could not create envelope pipe";
local $SIG{PIPE} = sub { die "SIGPIPE" }; local $SIG{PIPE} = sub { die 'SIGPIPE' };
my $child = fork(); my $child = fork();
not defined $child and die("Could not fork"); ! defined $child and die "Could not fork";
if ($child) { if ($child) {
# Parent # Parent
my $oldfh = select(MESSAGE_WRITER); $| = 1; my $oldfh = select MESSAGE_WRITER; $| = 1;
select(ENVELOPE_WRITER); $| = 1; select ENVELOPE_WRITER; $| = 1;
select($oldfh); select $oldfh;
close MESSAGE_READER or die("close msg reader fault"); close MESSAGE_READER or die "close msg reader fault";
close ENVELOPE_READER or die("close envelope reader fault"); close ENVELOPE_READER or die "close envelope reader fault";
$transaction->header->print(\*MESSAGE_WRITER); $transaction->header->print(\*MESSAGE_WRITER);
$transaction->body_resetpos; $transaction->body_resetpos;
@ -80,21 +83,21 @@ sub hook_queue {
return (OK, "Queued! " . time . " qp $child $msg_id"); return (OK, "Queued! " . time . " qp $child $msg_id");
} }
elsif (defined $child) { elsif (defined $child) {
# Child # Child
close MESSAGE_WRITER or exit 1; close MESSAGE_WRITER or exit 1;
close ENVELOPE_WRITER or exit 2; close ENVELOPE_WRITER or exit 2;
# Untaint $self->{_queue_exec} # Untaint $self->{_queue_exec}
my $queue_exec = $self->{_queue_exec}; my $queue_exec = $self->{_queue_exec};
if ($queue_exec =~ /^(\/[\/\-\_\.a-z0-9A-Z]*)$/) { if ($queue_exec =~ /^(\/[\/\-\_\.a-z0-9A-Z]*)$/) {
$queue_exec = $1; $queue_exec = $1;
} else { } else {
$self->log(LOGERROR, "FATAL ERROR: Unexpected characters in qmail-queue plugin argument"); $self->log(LOGERROR, "FATAL ERROR: Unexpected characters in qmail-queue plugin argument");
# This exit is ok as we're exiting a forked child process. # This exit is ok as we're exiting a forked child process.
exit 3; exit 3;
} }
# save the original STDIN and STDOUT in case exec() fails below # save the original STDIN and STDOUT in case exec() fails below
open(SAVE_STDIN, "<&STDIN"); open(SAVE_STDIN, "<&STDIN");
open(SAVE_STDOUT, ">&STDOUT"); open(SAVE_STDOUT, ">&STDOUT");
@ -102,11 +105,11 @@ sub hook_queue {
POSIX::dup2(fileno(ENVELOPE_READER), 1) or die "Unable to dup ENVELOPE_READER: $!"; POSIX::dup2(fileno(ENVELOPE_READER), 1) or die "Unable to dup ENVELOPE_READER: $!";
my $ppid = getppid(); my $ppid = getppid();
$self->log(LOGNOTICE, "(for $ppid ) Queuing qp $$ to $queue_exec"); $self->log(LOGNOTICE, "(for $ppid) Queuing to $queue_exec");
my $rc = exec $queue_exec; my $rc = exec $queue_exec;
# close the pipe # close the pipe
close(MESSAGE_READER); close(MESSAGE_READER);
close(MESSAGE_WRITER); close(MESSAGE_WRITER);