3 ## Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers.
4 ## All rights reserved.
6 ## $Id: qtool.pl,v 8.28 2002/06/27 23:06:16 gshapiro 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 'I' => 'inode_number',
489 'K' => 'next_delivery_time',
490 'L' => 'content-length',
492 'N' => 'num_delivery_attempts',
494 'Q' => 'original_recipient',
496 'q' => 'quarantine_reason',
497 'r' => 'final_recipient',
499 'T' => 'creation_time',
501 'Y' => 'current_delay',
513 open(CONTROL_FILE, "$self->{file_name}");
514 while ($line = <CONTROL_FILE>)
516 $line_type = substr($line, 0, 1);
517 if ($line_type eq "\t" && $last_type eq 'H')
524 $line_value = substr($line, 1);
526 $member_name = $parse_table{$line_type};
527 $last_type = $line_type;
530 $member_name = 'unknown';
532 if ($self->can($member_name))
534 $self->$member_name($line_value);
536 $member = $self->{$member_name};
539 $self->{$member_name} = $line_value;
542 if (ref($member) eq 'ARRAY')
544 push(@{$member}, $line_value);
547 $self->{$member_name} = [$member, $line_value];
558 my $headers = $self->{headers};
559 my $last_header = $self->{last_header};
564 $first_char = substr($line, 0, 1);
565 if ($first_char eq "?")
567 $line = substr($line, 3);
569 elsif ($first_char eq "\t")
571 if (ref($headers->{$last_header}) eq 'ARRAY')
573 $headers->{$last_header}[-1] =
574 $headers->{$last_header}[-1] . $line;
578 $headers->{$last_header} = $headers->{$last_header} .
583 ($header_name, $header_value) = split(/:/, $line, 2);
584 $self->{last_header} = $header_name;
585 if (exists $headers->{$header_name})
587 $headers->{$header_name} = [$headers->{$header_name},
592 $headers->{$header_name} = $header_value;
600 return (defined $self->{lock_handle});
609 if ($self->is_locked())
615 ($lock_handle, $result) = ::lock_file($self->{file_name});
621 $self->{lock_handle} = $lock_handle;
631 if (!$self->is_locked())
637 $result = ::unlock_file($self->{lock_handle});
639 $self->{lock_handle} = undef;
650 $result = open(QUEUE_FILE, $self->{file_name});
653 return "Unable to open '$self->{file_name}': $!";
655 @result = stat(QUEUE_FILE);
658 return "Unable to stat '$self->{file_name}': $!";
660 $self->{control_size} = $result[7];
661 $self->{control_last_mod_time} = $result[9];
676 $result = unlink($self->{file_name});
679 return "Unable to delete $self->{file_name}: $!";
686 ## DATA_FILE - Represents a sendmail queue data file.
688 ## This object represents represents a sendmail queue data file.
689 ## It is really just a place-holder.
697 my $class = ref($this) || $this;
700 $self->initialize(@_);
707 my $data_dir = shift;
709 my $control_file = shift;
711 $self->{file_name} = $data_dir . '/df' . $self->{id};
712 return if -e $self->{file_name};
713 $control_file->parse();
714 return if !defined $control_file->{data_file_directory};
715 $data_dir = $queue_root . '/' . $control_file->{data_file_directory};
717 if (-d ($data_dir . '/df'))
721 $self->{file_name} = $data_dir . '/df' . $self->{id};
730 $result = open(QUEUE_FILE, $self->{file_name});
733 return "Unable to open '$self->{file_name}': $!";
735 @result = stat(QUEUE_FILE);
738 return "Unable to stat '$self->{file_name}': $!";
740 $self->{body_size} = $result[7];
741 $self->{body_last_mod_time} = $result[9];
749 $result = unlink($self->{file_name});
752 return "Unable to delete $self->{file_name}: $!";
759 ## QUEUED_MESSAGE - Represents a queued sendmail message.
761 ## This keeps track of the files that make up a queued sendmail
763 ## Currently it has 'control_file' and 'data_file' as members.
765 ## You can tie it to a fetch only hash using tie. You need to
766 ## pass a reference to a QueuedMessage as the third argument
770 package QueuedMessage;
775 my $class = ref($this) || $this;
778 $self->initialize(@_);
785 my $queue_dir = shift;
787 my $data_dir = shift;
790 $self->{control_file} = new ControlFile($queue_dir, $id);
793 $data_dir = $queue_dir;
795 $self->{data_file} = new DataFile($data_dir, $id, $self->{control_file});
798 sub last_modified_time
802 @result = stat($self->{data_file}->{file_name});
809 my $class = ref($this) || $this;
819 if (exists $self->{control_file}->{$key})
821 return $self->{control_file}->{$key};
823 if (exists $self->{data_file}->{$key})
825 return $self->{data_file}->{$key};
835 return $self->{control_file}->lock();
842 return $self->{control_file}->unlock();
848 my $destination = shift;
853 $result = $self->lock();
859 $qf_dest = File::Spec->catfile($destination, "qf");
862 $df_dest = File::Spec->catfile($destination, "df");
865 $df_dest = $destination;
870 $qf_dest = $destination;
871 $df_dest = $destination;
874 if (-e File::Spec->catfile($qf_dest, "$qprefix$self->{id}"))
876 $result = "There is already a queued message with id '$self->{id}' in '$destination'";
881 $result = ::move_file($self->{data_file}->{file_name},
887 $result = ::move_file($self->{control_file}->{file_name},
900 return $self->{control_file}->parse();
907 $self->{control_file}->do_stat();
908 $self->{data_file}->do_stat();
924 $result = $self->{control_file}->delete();
929 $result = $self->{data_file}->delete();
943 $command = "sendmail -qI$self->{id} -O Timeout.queuereturn=now";
944 # print("$command\n");
949 ## QUEUE - Represents a queued sendmail queue.
951 ## This manages all of the messages in a queue.
959 my $class = ref($this) || $this;
962 $self->initialize(@_);
970 $self->{queue_dir} = shift;
975 ## READ - Loads the queue with all of the objects that reside in it.
977 ## This reads the queue's directory and creates QueuedMessage objects
978 ## for every file in the queue that starts with 'qf' or 'hf'
979 ## (depending on the -Q option).
993 $control_dir = File::Spec->catfile($self->{queue_dir}, 'qf');
997 $data_dir = File::Spec->catfile($self->{queue_dir}, 'df');
1000 $data_dir = $self->{queue_dir};
1005 $data_dir = $self->{queue_dir};
1006 $control_dir = $self->{queue_dir};
1009 $result = opendir(QUEUE_DIR, $control_dir);
1012 return "Unable to open directory '$control_dir'";
1015 @control_files = grep { /^$qprefix.*/ && -f "$control_dir/$_" } readdir(QUEUE_DIR);
1016 closedir(QUEUE_DIR);
1017 foreach $file_name (@control_files)
1019 $id = substr($file_name, 2);
1020 $queued_message = new QueuedMessage($control_dir, $id,
1022 $self->{files}->{$id} = $queued_message;
1030 ## ADD_QUEUED_MESSAGE - Adds a QueuedMessage to this Queue.
1032 ## Adds the QueuedMessage object to the hash and moves the files
1033 ## associated with the QueuedMessage to this Queue's directory.
1036 sub add_queued_message
1039 my $queued_message = shift;
1042 $result = $queued_message->move($self->{queue_dir});
1048 $self->{files}->{$queued_message->{id}} = $queued_message;
1054 ## ADD_QUEUE - Adds another Queue's QueuedMessages to this Queue.
1056 ## Adds all of the QueuedMessage objects in the passed in queue
1068 while (($id, $queued_message) = each %{$queue->{files}})
1070 $result = $self->add_queued_message($queued_message);
1073 print("$result.\n");
1079 ## ADD - Adds an item to this queue.
1081 ## Adds either a Queue or a QueuedMessage to this Queue.
1091 $type_name = ref($source);
1093 if ($type_name eq "QueuedMessage")
1095 return $self->add_queued_message($source);
1098 if ($type_name eq "Queue")
1100 return $self->add_queue($source);
1103 return "Queue does not know how to add a '$type_name'"
1112 while (($id, $queued_message) = each %{$self->{files}})
1114 $result = $queued_message->delete();
1117 print("$result.\n");
1128 while (($id, $queued_message) = each %{$self->{files}})
1130 $result = $queued_message->bounce();
1133 print("$result.\n");
1141 ## This next section is for any class that has an interface called
1142 ## check_move(source, dest). Each class represents some condition to
1143 ## check for to determine whether we should move the file from
1151 ## This Condition Class checks the modification time of the
1152 ## source file and returns true if the file's modification time is
1153 ## older than the number of seconds the class was initialzed with.
1161 my $class = ref($this) || $this;
1163 bless $self, $class;
1164 $self->initialize(@_);
1172 $self->{age_in_seconds} = shift;
1180 if ((time() - $source->last_modified_time()) > $self->{age_in_seconds})
1191 ## Takes a list of Move Condition Classes. Check_move returns true
1192 ## if every Condition Class in the list's check_move function returns
1201 my $class = ref($this) || $this;
1203 bless $self, $class;
1204 $self->initialize(@_);
1212 $self->{condition_list} = [];
1218 my $new_condition = shift;
1220 push(@{$self->{condition_list}}, $new_condition);
1231 foreach $condition (@{$self->{condition_list}})
1233 if (!$condition->check_move($source, $dest))
1245 ## Takes a perl expression and evaluates it. The ControlFile object
1246 ## for the source QueuedMessage is avaliable through the name '$msg'.
1254 my $class = ref($this) || $this;
1256 bless $self, $class;
1257 $self->initialize(@_);
1265 $self->{expression} = shift;
1276 $source->setup_vars();
1277 tie(%msg, 'QueuedMessage', $source);
1278 $result = eval($self->{expression});