##
## Copyright (c) 1998-2002 Proofpoint, Inc. and its suppliers.
## All rights reserved.
##
## $Id: qtool.pl,v 8.32 2013-11-22 20:51:18 ca Exp $
##
use strict;
##
## QTOOL
## This program is for moving files between sendmail queues. It is
## pretty similar to just moving the files manually, but it locks the files
## the same way sendmail does to prevent problems.
##
## NOTICE: Do not use this program to move queue files around
## if you use sendmail 8.12 and multiple queue groups. It may interfere
## with sendmail's internal queue group selection strategy and can cause
## mail to be not delivered.
##
## The syntax is the reverse of mv (ie. the target argument comes
## first). This lets you pick the files you want to move using find and
## xargs.
##
## Since you cannot delete queues while sendmail is running, QTOOL
## assumes that when you specify a directory as a source, you mean that you
## want all of the queue files within that directory moved, not the
## directory itself.
##
## There is a mechanism for adding conditionals for moving the files.
## Just create an Object with a check_move(source, dest) method and add it
## to the $conditions object. See the handling of the '-s' option for an
## example.
##
##
## OPTION NOTES
##
## The -e option:
## The -e option takes any valid perl expression and evaluates it
## using the eval() function. Inside the expression the variable
## '$msg' is bound to the ControlFile object for the current source
## queue message. This lets you check for any value in the message
## headers or the control file. Here's an example:
##
## ./qtool.pl -e '$msg{num_delivery_attempts} >= 2' /q1 /q2
##
## This would move any queue files whose number of delivery attempts
## is greater than or equal to 2 from the queue 'q2' to the queue 'q1'.
##
## See the function ControlFile::parse for a list of available
## variables.
##
my %opts;
my %sources;
my $dst_name;
my $destination;
my $source_name;
my $source;
my $result;
my $action;
my $new_condition;
my $qprefix;
my $queuegroups = 0;
my $conditions = new Compound();
my $fcntl_struct = 's H60';
"000000000000000000000000000000000000000000000000000000000000");
"000000000000000000000000000000000000000000000000000000000000");
my $lock_both = -1;
sub move_action
{
my $source = shift;
my $destination = shift;
if ($result)
{
print("$result.\n");
}
}
sub delete_action
{
my $source = shift;
return $source->delete();
}
sub bounce_action
{
my $source = shift;
}
$action = \&move_action;
if (defined $opts{d})
{
$action = \&delete_action;
}
elsif (defined $opts{b})
{
$action = \&bounce_action;
}
if (defined $opts{s})
{
}
if (defined $opts{e})
{
}
if (defined $opts{Q})
{
$qprefix = "hf";
}
else
{
$qprefix = "qf";
}
if ($action == \&move_action)
{
if (!-d $dst_name)
{
print("The destination '$dst_name' must be an existing " .
"directory.\n");
usage();
exit;
}
}
# determine queue_root by reading config file
my $queue_root;
{
my $config_file = "/etc/mail/sendmail.cf";
if (defined $opts{C})
{
$config_file = $opts{C};
}
my $line;
## Notice: we can only break out of this loop (using last)
## when both entries (queue directory and group group)
## have been found.
while ($line = <CONFIG_FILE>)
{
chomp $line;
if ($line =~ m/^O QueueDirectory=(.*)/)
{
$queue_root = $1;
if ($queue_root =~ m/(.*)\/[^\/]+\*$/)
{
$queue_root = $1;
}
# found also queue groups?
if ($queuegroups)
{
last;
}
}
if ($line =~ m/^Q.*/)
{
$queuegroups = 1;
if ($action == \&move_action)
{
print("WARNING: moving queue files around " .
"when queue groups are used may\n" .
"result in undelivered mail!\n");
}
# found also queue directory?
if (defined $queue_root)
{
last;
}
}
}
close(CONFIG_FILE);
if (!defined $queue_root)
{
die "QueueDirectory option not defined in $config_file";
}
}
while (@ARGV)
{
$source_name = shift(@ARGV);
if ($result)
{
print("$result.\n");
exit;
}
}
if (keys(%sources) == 0)
{
exit;
}
{
if ($result)
{
if ($result)
{
print("$result\n");
}
}
}
sub usage
{
print("Usage:\t$0 [options] directory source ...\n");
print("\t$0 [-Q][-d|-b] source ...\n");
print("Options:\n");
print("\t-b\t\tBounce the messages specified by source.\n");
print("\t-C configfile\tSpecify sendmail config file.\n");
print("\t-d\t\tDelete the messages specified by source.\n");
print("\t-e [perl expression]\n");
print("\t\t\tMove only messages for which perl expression\n");
print("\t\t\treturns true.\n");
print("\t-Q\t\tOperate on quarantined files.\n");
print("\t-s [seconds]\tMove only messages whose queue file is older\n");
print("\t\t\tthan seconds.\n");
}
##
## ADD_SOURCE -- Adds a source to the source hash.
##
## Determines whether source is a file, directory, or id. Then it
## creates a QueuedMessage or Queue for that source and adds it to the
## list.
##
## Parameters:
## sources -- A hash that contains all of the sources.
## source_name -- The name of the source to add
##
## Returns:
## error_string -- Undef if ok. Error string otherwise.
##
## Notes:
## If a new source comes in with the same ID as a previous
## source, the previous source gets overwritten in the sources
## hash. This lets the user specify things like * and it still
## works nicely.
##
sub add_source
{
my $sources = shift;
my $source_name = shift;
my $source_base_name;
my $source_dir_name;
my $data_dir_name;
my $source_id;
my $source_prefix;
my $queued_message;
my $queue;
my $result;
$source_prefix ne 'df')
{
$source_base_name = "$qprefix$source_base_name";
"$source_base_name");
}
if (!-e $source_name)
{
"$qprefix$source_id");
if (!-e $source_name)
{
return "'$source_name' does not exist";
}
if (!-d $data_dir_name)
{
}
"qf");
}
if (-f $source_name)
{
return undef;
}
if (!-d $source_name)
{
return "'$source_name' is not a plain file or a directory";
}
if ($result)
{
return $result;
}
{
}
return undef;
}
##
## LOCK_FILE -- Opens and then locks a file.
##
## file. The flock is Perl's flock which defaults to flock on systems
## that support it. On systems without flock it falls back to fcntl
## locking. This script will also call fcntl explicitly if flock
## uses BSD semantics (i.e. if both flock() and fcntl() can successfully
## lock the file at the same time)
##
## Parameters:
## file_name -- The name of the file to open and lock.
##
## Returns:
## (file_handle, error_string) -- If everything works then
## file_handle is a reference to a file handle and
## error_string is undef. If there is a problem then
## file_handle is undef and error_string is a string
## explaining the problem.
##
sub lock_file
{
my $file_name = shift;
my $result;
if ($lock_both == -1)
{
{
close(DEVNULL);
}
else
{
$lock_both = 0;
}
}
if (!$result)
{
return (undef, "Unable to open '$file_name': $!");
}
if (!$result)
{
return (undef, "Could not obtain lock on '$file_name': $!");
}
if ($lock_both)
{
if (!$result2)
{
return (undef, "Could not obtain fcntl lock on '$file_name': $!");
}
}
return (\*FILE_TO_LOCK, undef);
}
##
## UNLOCK_FILE -- Unlocks a file.
##
## Unlocks a file using Perl's flock.
##
## Parameters:
## file -- A file handle.
##
## Returns:
## error_string -- If undef then no problem. Otherwise it is a
## string that explains problem.
##
sub unlock_file
{
my $file = shift;
my $result;
if (!$result)
{
return "Unlock failed on '$result': $!";
}
if ($lock_both)
{
if (!$result2)
{
return (undef, "Fcntl unlock failed on '$result': $!");
}
}
return undef;
}
##
## MOVE_FILE -- Moves a file.
##
## Moves a file.
##
## Parameters:
## src_name -- The name of the file to be move.
## dst_name -- The name of the place to move it to.
##
## Returns:
## error_string -- If undef then no problem. Otherwise it is a
## string that explains problem.
##
sub move_file
{
my $src_name = shift;
my $dst_name = shift;
my $result;
if (!$result)
{
return "File move from '$src_name' to '$dst_name' failed: $!";
}
return undef;
}
##
## CONTROL_FILE - Represents a sendmail queue control file.
##
## This object represents represents a sendmail queue control file.
## It can parse and lock its file.
##
package ControlFile;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
my $queue_dir = shift;
}
##
## PARSE - Parses the control file.
##
## Parses the control file. It just sticks each entry into a hash.
## If a key has more than one entry, then it points to a list of
## entries.
##
sub parse
{
my $self = shift;
{
return;
}
my %parse_table =
(
'A' => 'auth',
'B' => 'body_type',
'C' => 'controlling_user',
'D' => 'data_file_name',
'd' => 'data_file_directory',
'E' => 'error_recipient',
'F' => 'flags',
'H' => 'parse_header',
'I' => 'inode_number',
'K' => 'next_delivery_time',
'L' => 'content-length',
'M' => 'message',
'N' => 'num_delivery_attempts',
'P' => 'priority',
'Q' => 'original_recipient',
'R' => 'recipient',
'q' => 'quarantine_reason',
'r' => 'final_recipient',
'S' => 'sender',
'T' => 'creation_time',
'V' => 'version',
'Y' => 'current_delay',
'Z' => 'envid',
'!' => 'deliver_by',
'$' => 'macro'
);
my $line;
my $line_type;
my $line_value;
my $member_name;
my $member;
my $last_type;
open(CONTROL_FILE, "$self->{file_name}");
while ($line = <CONTROL_FILE>)
{
{
$line_type = 'H';
$line_value = $line;
}
else
{
}
if (!$member_name)
{
$member_name = 'unknown';
}
{
}
if (!$member)
{
next;
}
if (ref($member) eq 'ARRAY')
{
push(@{$member}, $line_value);
next;
}
}
close(CONTROL_FILE);
}
sub parse_header
{
my $self = shift;
my $line = shift;
my $header_name;
my $header_value;
my $first_char;
if ($first_char eq "?")
{
}
elsif ($first_char eq "\t")
{
{
}
else
{
$line;
}
return;
}
if (exists $headers->{$header_name})
{
$header_value];
}
else
{
}
}
sub is_locked
{
my $self = shift;
return (defined $self->{lock_handle});
}
sub lock
{
my $self = shift;
my $lock_handle;
my $result;
{
# Already locked
return undef;
}
if (!$lock_handle)
{
return $result;
}
return undef;
}
sub unlock
{
my $self = shift;
my $result;
{
# Not locked
return undef;
}
$self->{lock_handle} = undef;
return $result;
}
sub do_stat
{
my $self = shift;
my $result;
my @result;
if (!$result)
{
return "Unable to open '$self->{file_name}': $!";
}
@result = stat(QUEUE_FILE);
if (!@result)
{
return "Unable to stat '$self->{file_name}': $!";
}
}
sub DESTROY
{
my $self = shift;
}
sub delete
{
my $self = shift;
my $result;
if (!$result)
{
return "Unable to delete $self->{file_name}: $!";
}
return undef;
}
##
## DATA_FILE - Represents a sendmail queue data file.
##
## This object represents represents a sendmail queue data file.
## It is really just a place-holder.
##
package DataFile;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
my $data_dir = shift;
my $control_file = shift;
$control_file->parse();
return if !defined $control_file->{data_file_directory};
chomp $data_dir;
if (-d ($data_dir . '/df'))
{
$data_dir .= '/df';
}
}
sub do_stat
{
my $self = shift;
my $result;
my @result;
if (!$result)
{
return "Unable to open '$self->{file_name}': $!";
}
@result = stat(QUEUE_FILE);
if (!@result)
{
return "Unable to stat '$self->{file_name}': $!";
}
}
sub delete
{
my $self = shift;
my $result;
if (!$result)
{
return "Unable to delete $self->{file_name}: $!";
}
return undef;
}
##
## QUEUED_MESSAGE - Represents a queued sendmail message.
##
## This keeps track of the files that make up a queued sendmail
## message.
## Currently it has 'control_file' and 'data_file' as members.
##
## You can tie it to a fetch only hash using tie. You need to
## pass a reference to a QueuedMessage as the third argument
## to tie.
##
package QueuedMessage;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
my $queue_dir = shift;
my $id = shift;
my $data_dir = shift;
if (!$data_dir)
{
}
}
{
my $self = shift;
my @result;
return $result[9];
}
sub TIEHASH
{
my $this = shift;
my $self = shift;
return $self;
}
sub FETCH
{
my $self = shift;
my $key = shift;
{
}
{
}
return undef;
}
sub lock
{
my $self = shift;
return $self->{control_file}->lock();
}
sub unlock
{
my $self = shift;
}
sub move
{
my $self = shift;
my $destination = shift;
my $df_dest;
my $qf_dest;
my $result;
if ($result)
{
return $result;
}
if (-d $qf_dest)
{
if (!-d $df_dest)
{
$df_dest = $destination;
}
}
else
{
$qf_dest = $destination;
$df_dest = $destination;
}
{
$result = "There is already a queued message with id '$self->{id}' in '$destination'";
}
if (!$result)
{
$df_dest);
}
if (!$result)
{
$qf_dest);
}
return $result;
}
sub parse
{
my $self = shift;
}
sub do_stat
{
my $self = shift;
}
sub setup_vars
{
my $self = shift;
}
sub delete
{
my $self = shift;
my $result;
if ($result)
{
return $result;
}
if ($result)
{
return $result;
}
return undef;
}
sub bounce
{
my $self = shift;
my $command;
$command = "sendmail -qI$self->{id} -O Timeout.queuereturn=now";
# print("$command\n");
system($command);
}
##
## QUEUE - Represents a queued sendmail queue.
##
## This manages all of the messages in a queue.
##
package Queue;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
}
##
## READ - Loads the queue with all of the objects that reside in it.
##
## This reads the queue's directory and creates QueuedMessage objects
## for every file in the queue that starts with 'qf' or 'hf'
## (depending on the -Q option).
##
sub read
{
my $self = shift;
my @control_files;
my $queued_message;
my $file_name;
my $id;
my $result;
my $control_dir;
my $data_dir;
if (-e $control_dir)
{
if (!-e $data_dir)
{
}
}
else
{
}
if (!$result)
{
return "Unable to open directory '$control_dir'";
}
closedir(QUEUE_DIR);
foreach $file_name (@control_files)
{
$data_dir);
}
return undef;
}
##
## ADD_QUEUED_MESSAGE - Adds a QueuedMessage to this Queue.
##
## Adds the QueuedMessage object to the hash and moves the files
## associated with the QueuedMessage to this Queue's directory.
##
{
my $self = shift;
my $queued_message = shift;
my $result;
if ($result)
{
return $result;
}
return $result;
}
##
## ADD_QUEUE - Adds another Queue's QueuedMessages to this Queue.
##
## Adds all of the QueuedMessage objects in the passed in queue
## to this queue.
##
sub add_queue
{
my $self = shift;
my $queue = shift;
my $id;
my $queued_message;
my $result;
{
if ($result)
{
print("$result.\n");
}
}
}
##
## ADD - Adds an item to this queue.
##
## Adds either a Queue or a QueuedMessage to this Queue.
##
sub add
{
my $self = shift;
my $source = shift;
my $type_name;
my $result;
if ($type_name eq "QueuedMessage")
{
}
if ($type_name eq "Queue")
{
}
return "Queue does not know how to add a '$type_name'"
}
sub delete
{
my $self = shift;
my $id;
my $queued_message;
{
$result = $queued_message->delete();
if ($result)
{
print("$result.\n");
}
}
}
sub bounce
{
my $self = shift;
my $id;
my $queued_message;
{
if ($result)
{
print("$result.\n");
}
}
}
##
## Condition Class
##
## This next section is for any class that has an interface called
## check_move(source, dest). Each class represents some condition to
## check for to determine whether we should move the file from
## source to dest.
##
##
## OlderThan
##
## This Condition Class checks the modification time of the
## source file and returns true if the file's modification time is
## older than the number of seconds the class was initialized with.
##
package OlderThan;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
$self->{age_in_seconds} = shift;
}
sub check_move
{
my $self = shift;
my $source = shift;
{
return 1;
}
return 0;
}
##
## Compound
##
## Takes a list of Move Condition Classes. Check_move returns true
## if every Condition Class in the list's check_move function returns
## true.
##
package Compound;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
$self->{condition_list} = [];
}
sub add
{
my $self = shift;
my $new_condition = shift;
}
sub check_move
{
my $self = shift;
my $source = shift;
my $dest = shift;
my $condition;
my $result;
{
{
return 0;
}
}
return 1;
}
##
## Eval
##
## Takes a perl expression and evaluates it. The ControlFile object
## for the source QueuedMessage is available through the name '$msg'.
##
package Eval;
sub new
{
my $this = shift;
my $self = {};
$self->initialize(@_);
return $self;
}
sub initialize
{
my $self = shift;
$self->{expression} = shift;
}
sub check_move
{
my $self = shift;
my $source = shift;
my $dest = shift;
my $result;
my %msg;
$source->setup_vars();
return $result;
}