3 ## Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers.
4 ## All rights reserved.
6 ## $Id: qtool.pl,v 8.27 2002/01/29 21:55:49 ca Exp $
12 use Fcntl qw(:flock :DEFAULT);
17 ## This program is for moving files between sendmail queues. It is
18 ## pretty similar to just moving the files manually, but it locks the files
19 ## the same way sendmail does to prevent problems.
21 ## NOTICE: Do not use this program to move queue files around
22 ## if you use sendmail 8.12 and multiple queue groups. It may interfere
23 ## with sendmail's internal queue group selection strategy and can cause
24 ## mail to be not delivered.
26 ## The syntax is the reverse of mv (ie. the target argument comes
27 ## first). This lets you pick the files you want to move using find and
30 ## Since you cannot delete queues while sendmail is running, QTOOL
31 ## assumes that when you specify a directory as a source, you mean that you
32 ## want all of the queue files within that directory moved, not the
35 ## There is a mechanism for adding conditionals for moving the files.
36 ## Just create an Object with a check_move(source, dest) method and add it
37 ## to the $conditions object. See the handling of the '-s' option for an
45 ## The -e option takes any valid perl expression and evaluates it
46 ## using the eval() function. Inside the expression the variable
47 ## '$msg' is bound to the ControlFile object for the current source
48 ## queue message. This lets you check for any value in the message
49 ## headers or the control file. Here's an example:
51 ## ./qtool.pl -e '$msg{num_delivery_attempts} >= 2' /q1 /q2
53 ## This would move any queue files whose number of delivery attempts
54 ## is greater than or equal to 2 from the queue 'q2' to the queue 'q1'.
56 ## See the function ControlFile::parse for a list of available
71 my $conditions = new Compound();
73 Getopt::Std::getopts('bC:de:Qs:', \%opts);
78 my $destination = shift;
80 $result = $destination->add($source);
91 return $source->delete();
98 return $source->bounce();
101 $action = \&move_action;
102 if (defined $opts{d})
104 $action = \&delete_action;
106 elsif (defined $opts{b})
108 $action = \&bounce_action;
111 if (defined $opts{s})
113 $new_condition = new OlderThan($opts{s});
114 $conditions->add($new_condition);
117 if (defined $opts{e})
119 $new_condition = new Eval($opts{e});
120 $conditions->add($new_condition);
123 if (defined $opts{Q})
132 if ($action == \&move_action)
134 $dst_name = shift(@ARGV);
137 print("The destination '$dst_name' must be an existing " .
142 $destination = new Queue($dst_name);
145 # determine queue_root by reading config file
148 my $config_file = "/etc/mail/sendmail.cf";
149 if (defined $opts{C})
151 $config_file = $opts{C};
155 open(CONFIG_FILE, $config_file) or die "$config_file: $!";
157 ## Notice: we can only break out of this loop (using last)
158 ## when both entries (queue directory and group group)
160 while ($line = <CONFIG_FILE>)
163 if ($line =~ m/^O QueueDirectory=(.*)/)
166 if ($queue_root =~ m/(.*)\/[^\/]+\*$/)
170 # found also queue groups?
176 if ($line =~ m/^Q.*/)
179 if ($action == \&move_action)
181 print("WARNING: moving queue files around " .
182 "when queue groups are used may\n" .
183 "result in undelivered mail!\n");
185 # found also queue directory?
186 if (defined $queue_root)
193 if (!defined $queue_root)
195 die "QueueDirectory option not defined in $config_file";
201 $source_name = shift(@ARGV);
202 $result = add_source(\%sources, $source_name);
210 if (keys(%sources) == 0)
215 while (($source_name, $source) = each(%sources))
217 $result = $conditions->check_move($source, $destination);
220 $result = &{$action}($source, $destination);
230 print("Usage:\t$0 [options] directory source ...\n");
231 print("\t$0 [-Q][-d|-b] source ...\n");
233 print("\t-b\t\tBounce the messages specified by source.\n");
234 print("\t-C configfile\tSpecify sendmail config file.\n");
235 print("\t-d\t\tDelete the messages specified by source.\n");
236 print("\t-e [perl expression]\n");
237 print("\t\t\tMove only messages for which perl expression\n");
238 print("\t\t\treturns true.\n");
239 print("\t-Q\t\tOperate on quarantined files.\n");
240 print("\t-s [seconds]\tMove only messages whose queue file is older\n");
241 print("\t\t\tthan seconds.\n");
245 ## ADD_SOURCE -- Adds a source to the source hash.
247 ## Determines whether source is a file, directory, or id. Then it
248 ## creates a QueuedMessage or Queue for that source and adds it to the
252 ## sources -- A hash that contains all of the sources.
253 ## source_name -- The name of the source to add
256 ## error_string -- Undef if ok. Error string otherwise.
259 ## If a new source comes in with the same ID as a previous
260 ## source, the previous source gets overwritten in the sources
261 ## hash. This lets the user specify things like * and it still
268 my $source_name = shift;
269 my $source_base_name;
278 ($source_base_name, $source_dir_name) = File::Basename::fileparse($source_name);
279 $data_dir_name = $source_dir_name;
281 $source_prefix = substr($source_base_name, 0, 2);
282 if (!-d $source_name && $source_prefix ne $qprefix &&
283 $source_prefix ne 'df')
285 $source_base_name = "$qprefix$source_base_name";
286 $source_name = File::Spec->catfile("$source_dir_name",
287 "$source_base_name");
289 $source_id = substr($source_base_name, 2);
291 if (!-e $source_name)
293 $source_name = File::Spec->catfile("$source_dir_name", "qf",
294 "$qprefix$source_id");
295 if (!-e $source_name)
297 return "'$source_name' does not exist";
299 $data_dir_name = File::Spec->catfile("$source_dir_name", "df");
300 if (!-d $data_dir_name)
302 $data_dir_name = $source_dir_name;
304 $source_dir_name = File::Spec->catfile("$source_dir_name",
310 $queued_message = new QueuedMessage($source_dir_name,
313 $sources->{$source_id} = $queued_message;
317 if (!-d $source_name)
319 return "'$source_name' is not a plain file or a directory";
322 $queue = new Queue($source_name);
323 $result = $queue->read();
329 while (($source_id, $queued_message) = each(%{$queue->{files}}))
331 $sources->{$source_id} = $queued_message;
338 ## LOCK_FILE -- Opens and then locks a file.
340 ## Opens a file for read/write and uses flock to obtain a lock on the
341 ## file. The flock is Perl's flock which defaults to flock on systems
342 ## that support it. On systems without flock it falls back to fcntl
346 ## file_name -- The name of the file to open and lock.
349 ## (file_handle, error_string) -- If everything works then
350 ## file_handle is a reference to a file handle and
351 ## error_string is undef. If there is a problem then
352 ## file_handle is undef and error_string is a string
353 ## explaining the problem.
358 my $file_name = shift;
361 $result = sysopen(FILE_TO_LOCK, $file_name, Fcntl::O_RDWR);
364 return (undef, "Unable to open '$file_name': $!");
367 $result = flock(FILE_TO_LOCK, Fcntl::LOCK_EX | Fcntl::LOCK_NB);
370 return (undef, "Could not obtain lock on '$file_name': $!");
373 return (\*FILE_TO_LOCK, undef);
377 ## UNLOCK_FILE -- Unlocks a file.
379 ## Unlocks a file using Perl's flock.
382 ## file -- A file handle.
385 ## error_string -- If undef then no problem. Otherwise it is a
386 ## string that explains problem.
394 $result = flock($file, Fcntl::LOCK_UN);
397 return "Unlock failed on '$result': $!";
404 ## MOVE_FILE -- Moves a file.
409 ## src_name -- The name of the file to be move.
410 ## dst_nome -- The name of the place to move it to.
413 ## error_string -- If undef then no problem. Otherwise it is a
414 ## string that explains problem.
419 my $src_name = shift;
420 my $dst_name = shift;
423 $result = File::Copy::move($src_name, $dst_name);
426 return "File move from '$src_name' to '$dst_name' failed: $!";
434 ## CONTROL_FILE - Represents a sendmail queue control file.
436 ## This object represents represents a sendmail queue control file.
437 ## It can parse and lock its file.
446 my $class = ref($this) || $this;
449 $self->initialize(@_);
456 my $queue_dir = shift;
459 $self->{file_name} = $queue_dir . '/' . $qprefix . $self->{id};
460 $self->{headers} = {};
464 ## PARSE - Parses the control file.
466 ## Parses the control file. It just sticks each entry into a hash.
467 ## If a key has more than one entry, then it points to a list of
482 'C' => 'controlling_user',
483 'D' => 'data_file_name',
484 'd' => 'data_file_directory',
485 'E' => 'error_recipient',
487 'H' => 'parse_header',
488 'G' => 'queue_delay',
489 'I' => 'inode_number',
490 'K' => 'next_delivery_time',
491 'L' => 'content-length',
493 'N' => 'num_delivery_attempts',
495 'Q' => 'original_recipient',
497 'q' => 'quarantine_reason',
498 'r' => 'final_recipient',
500 'T' => 'creation_time',
502 'Y' => 'current_delay',
514 open(CONTROL_FILE, "$self->{file_name}");
515 while ($line = <CONTROL_FILE>)
517 $line_type = substr($line, 0, 1);
518 if ($line_type eq "\t" && $last_type eq 'H')
525 $line_value = substr($line, 1);
527 $member_name = $parse_table{$line_type};
528 $last_type = $line_type;
531 $member_name = 'unknown';
533 if ($self->can($member_name))
535 $self->$member_name($line_value);
537 $member = $self->{$member_name};
540 $self->{$member_name} = $line_value;
543 if (ref($member) eq 'ARRAY')
545 push(@{$member}, $line_value);
548 $self->{$member_name} = [$member, $line_value];
559 my $headers = $self->{headers};
560 my $last_header = $self->{last_header};
565 $first_char = substr($line, 0, 1);
566 if ($first_char eq "?")
568 $line = substr($line, 3);
570 elsif ($first_char eq "\t")
572 if (ref($headers->{$last_header}) eq 'ARRAY')
574 $headers->{$last_header}[-1] =
575 $headers->{$last_header}[-1] . $line;
579 $headers->{$last_header} = $headers->{$last_header} .
584 ($header_name, $header_value) = split(/:/, $line, 2);
585 $self->{last_header} = $header_name;
586 if (exists $headers->{$header_name})
588 $headers->{$header_name} = [$headers->{$header_name},
593 $headers->{$header_name} = $header_value;
601 return (defined $self->{lock_handle});
610 if ($self->is_locked())
616 ($lock_handle, $result) = ::lock_file($self->{file_name});
622 $self->{lock_handle} = $lock_handle;
632 if (!$self->is_locked())
638 $result = ::unlock_file($self->{lock_handle});
640 $self->{lock_handle} = undef;
651 $result = open(QUEUE_FILE, $self->{file_name});
654 return "Unable to open '$self->{file_name}': $!";
656 @result = stat(QUEUE_FILE);
659 return "Unable to stat '$self->{file_name}': $!";
661 $self->{control_size} = $result[7];
662 $self->{control_last_mod_time} = $result[9];
677 $result = unlink($self->{file_name});
680 return "Unable to delete $self->{file_name}: $!";
687 ## DATA_FILE - Represents a sendmail queue data file.
689 ## This object represents represents a sendmail queue data file.
690 ## It is really just a place-holder.
698 my $class = ref($this) || $this;
701 $self->initialize(@_);
708 my $data_dir = shift;
710 my $control_file = shift;
712 $self->{file_name} = $data_dir . '/df' . $self->{id};
713 return if -e $self->{file_name};
714 $control_file->parse();
715 return if !defined $control_file->{data_file_directory};
716 $data_dir = $queue_root . '/' . $control_file->{data_file_directory};
718 if (-d ($data_dir . '/df'))
722 $self->{file_name} = $data_dir . '/df' . $self->{id};
731 $result = open(QUEUE_FILE, $self->{file_name});
734 return "Unable to open '$self->{file_name}': $!";
736 @result = stat(QUEUE_FILE);
739 return "Unable to stat '$self->{file_name}': $!";
741 $self->{body_size} = $result[7];
742 $self->{body_last_mod_time} = $result[9];
750 $result = unlink($self->{file_name});
753 return "Unable to delete $self->{file_name}: $!";
760 ## QUEUED_MESSAGE - Represents a queued sendmail message.
762 ## This keeps track of the files that make up a queued sendmail
764 ## Currently it has 'control_file' and 'data_file' as members.
766 ## You can tie it to a fetch only hash using tie. You need to
767 ## pass a reference to a QueuedMessage as the third argument
771 package QueuedMessage;
776 my $class = ref($this) || $this;
779 $self->initialize(@_);
786 my $queue_dir = shift;
788 my $data_dir = shift;
791 $self->{control_file} = new ControlFile($queue_dir, $id);
794 $data_dir = $queue_dir;
796 $self->{data_file} = new DataFile($data_dir, $id, $self->{control_file});
799 sub last_modified_time
803 @result = stat($self->{data_file}->{file_name});
810 my $class = ref($this) || $this;
820 if (exists $self->{control_file}->{$key})
822 return $self->{control_file}->{$key};
824 if (exists $self->{data_file}->{$key})
826 return $self->{data_file}->{$key};
836 return $self->{control_file}->lock();
843 return $self->{control_file}->unlock();
849 my $destination = shift;
854 $result = $self->lock();
860 $qf_dest = File::Spec->catfile($destination, "qf");
863 $df_dest = File::Spec->catfile($destination, "df");
866 $df_dest = $destination;
871 $qf_dest = $destination;
872 $df_dest = $destination;
875 if (-e File::Spec->catfile($qf_dest, "$qprefix$self->{id}"))
877 $result = "There is already a queued message with id '$self->{id}' in '$destination'";
882 $result = ::move_file($self->{data_file}->{file_name},
888 $result = ::move_file($self->{control_file}->{file_name},
901 return $self->{control_file}->parse();
908 $self->{control_file}->do_stat();
909 $self->{data_file}->do_stat();
925 $result = $self->{control_file}->delete();
930 $result = $self->{data_file}->delete();
944 $command = "sendmail -qI$self->{id} -O Timeout.queuereturn=now";
945 # print("$command\n");
950 ## QUEUE - Represents a queued sendmail queue.
952 ## This manages all of the messages in a queue.
960 my $class = ref($this) || $this;
963 $self->initialize(@_);
971 $self->{queue_dir} = shift;
976 ## READ - Loads the queue with all of the objects that reside in it.
978 ## This reads the queue's directory and creates QueuedMessage objects
979 ## for every file in the queue that starts with 'qf' or 'hf'
980 ## (depending on the -Q option).
994 $control_dir = File::Spec->catfile($self->{queue_dir}, 'qf');
998 $data_dir = File::Spec->catfile($self->{queue_dir}, 'df');
1001 $data_dir = $self->{queue_dir};
1006 $data_dir = $self->{queue_dir};
1007 $control_dir = $self->{queue_dir};
1010 $result = opendir(QUEUE_DIR, $control_dir);
1013 return "Unable to open directory '$control_dir'";
1016 @control_files = grep { /^$qprefix.*/ && -f "$control_dir/$_" } readdir(QUEUE_DIR);
1017 closedir(QUEUE_DIR);
1018 foreach $file_name (@control_files)
1020 $id = substr($file_name, 2);
1021 $queued_message = new QueuedMessage($control_dir, $id,
1023 $self->{files}->{$id} = $queued_message;
1031 ## ADD_QUEUED_MESSAGE - Adds a QueuedMessage to this Queue.
1033 ## Adds the QueuedMessage object to the hash and moves the files
1034 ## associated with the QueuedMessage to this Queue's directory.
1037 sub add_queued_message
1040 my $queued_message = shift;
1043 $result = $queued_message->move($self->{queue_dir});
1049 $self->{files}->{$queued_message->{id}} = $queued_message;
1055 ## ADD_QUEUE - Adds another Queue's QueuedMessages to this Queue.
1057 ## Adds all of the QueuedMessage objects in the passed in queue
1069 while (($id, $queued_message) = each %{$queue->{files}})
1071 $result = $self->add_queued_message($queued_message);
1074 print("$result.\n");
1080 ## ADD - Adds an item to this queue.
1082 ## Adds either a Queue or a QueuedMessage to this Queue.
1092 $type_name = ref($source);
1094 if ($type_name eq "QueuedMessage")
1096 return $self->add_queued_message($source);
1099 if ($type_name eq "Queue")
1101 return $self->add_queue($source);
1104 return "Queue does not know how to add a '$type_name'"
1113 while (($id, $queued_message) = each %{$self->{files}})
1115 $result = $queued_message->delete();
1118 print("$result.\n");
1129 while (($id, $queued_message) = each %{$self->{files}})
1131 $result = $queued_message->bounce();
1134 print("$result.\n");
1142 ## This next section is for any class that has an interface called
1143 ## check_move(source, dest). Each class represents some condition to
1144 ## check for to determine whether we should move the file from
1152 ## This Condition Class checks the modification time of the
1153 ## source file and returns true if the file's modification time is
1154 ## older than the number of seconds the class was initialzed with.
1162 my $class = ref($this) || $this;
1164 bless $self, $class;
1165 $self->initialize(@_);
1173 $self->{age_in_seconds} = shift;
1181 if ((time() - $source->last_modified_time()) > $self->{age_in_seconds})
1192 ## Takes a list of Move Condition Classes. Check_move returns true
1193 ## if every Condition Class in the list's check_move function returns
1202 my $class = ref($this) || $this;
1204 bless $self, $class;
1205 $self->initialize(@_);
1213 $self->{condition_list} = [];
1219 my $new_condition = shift;
1221 push(@{$self->{condition_list}}, $new_condition);
1232 foreach $condition (@{$self->{condition_list}})
1234 if (!$condition->check_move($source, $dest))
1246 ## Takes a perl expression and evaluates it. The ControlFile object
1247 ## for the source QueuedMessage is avaliable through the name '$msg'.
1255 my $class = ref($this) || $this;
1257 bless $self, $class;
1258 $self->initialize(@_);
1266 $self->{expression} = shift;
1277 $source->setup_vars();
1278 tie(%msg, 'QueuedMessage', $source);
1279 $result = eval($self->{expression});