3 ## Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers.
4 ## All rights reserved.
6 ## $Id: qtool.pl,v 8.31 2010/11/10 19:11:54 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();
72 my $fcntl_struct = 's H60';
73 my $fcntl_structlockp = pack($fcntl_struct, Fcntl::F_WRLCK,
74 "000000000000000000000000000000000000000000000000000000000000");
75 my $fcntl_structunlockp = pack($fcntl_struct, Fcntl::F_UNLCK,
76 "000000000000000000000000000000000000000000000000000000000000");
79 Getopt::Std::getopts('bC:de:Qs:', \%opts);
84 my $destination = shift;
86 $result = $destination->add($source);
97 return $source->delete();
104 return $source->bounce();
107 $action = \&move_action;
108 if (defined $opts{d})
110 $action = \&delete_action;
112 elsif (defined $opts{b})
114 $action = \&bounce_action;
117 if (defined $opts{s})
119 $new_condition = new OlderThan($opts{s});
120 $conditions->add($new_condition);
123 if (defined $opts{e})
125 $new_condition = new Eval($opts{e});
126 $conditions->add($new_condition);
129 if (defined $opts{Q})
138 if ($action == \&move_action)
140 $dst_name = shift(@ARGV);
143 print("The destination '$dst_name' must be an existing " .
148 $destination = new Queue($dst_name);
151 # determine queue_root by reading config file
154 my $config_file = "/etc/mail/sendmail.cf";
155 if (defined $opts{C})
157 $config_file = $opts{C};
161 open(CONFIG_FILE, $config_file) or die "$config_file: $!";
163 ## Notice: we can only break out of this loop (using last)
164 ## when both entries (queue directory and group group)
166 while ($line = <CONFIG_FILE>)
169 if ($line =~ m/^O QueueDirectory=(.*)/)
172 if ($queue_root =~ m/(.*)\/[^\/]+\*$/)
176 # found also queue groups?
182 if ($line =~ m/^Q.*/)
185 if ($action == \&move_action)
187 print("WARNING: moving queue files around " .
188 "when queue groups are used may\n" .
189 "result in undelivered mail!\n");
191 # found also queue directory?
192 if (defined $queue_root)
199 if (!defined $queue_root)
201 die "QueueDirectory option not defined in $config_file";
207 $source_name = shift(@ARGV);
208 $result = add_source(\%sources, $source_name);
216 if (keys(%sources) == 0)
221 while (($source_name, $source) = each(%sources))
223 $result = $conditions->check_move($source, $destination);
226 $result = &{$action}($source, $destination);
236 print("Usage:\t$0 [options] directory source ...\n");
237 print("\t$0 [-Q][-d|-b] source ...\n");
239 print("\t-b\t\tBounce the messages specified by source.\n");
240 print("\t-C configfile\tSpecify sendmail config file.\n");
241 print("\t-d\t\tDelete the messages specified by source.\n");
242 print("\t-e [perl expression]\n");
243 print("\t\t\tMove only messages for which perl expression\n");
244 print("\t\t\treturns true.\n");
245 print("\t-Q\t\tOperate on quarantined files.\n");
246 print("\t-s [seconds]\tMove only messages whose queue file is older\n");
247 print("\t\t\tthan seconds.\n");
251 ## ADD_SOURCE -- Adds a source to the source hash.
253 ## Determines whether source is a file, directory, or id. Then it
254 ## creates a QueuedMessage or Queue for that source and adds it to the
258 ## sources -- A hash that contains all of the sources.
259 ## source_name -- The name of the source to add
262 ## error_string -- Undef if ok. Error string otherwise.
265 ## If a new source comes in with the same ID as a previous
266 ## source, the previous source gets overwritten in the sources
267 ## hash. This lets the user specify things like * and it still
274 my $source_name = shift;
275 my $source_base_name;
284 ($source_base_name, $source_dir_name) = File::Basename::fileparse($source_name);
285 $data_dir_name = $source_dir_name;
287 $source_prefix = substr($source_base_name, 0, 2);
288 if (!-d $source_name && $source_prefix ne $qprefix &&
289 $source_prefix ne 'df')
291 $source_base_name = "$qprefix$source_base_name";
292 $source_name = File::Spec->catfile("$source_dir_name",
293 "$source_base_name");
295 $source_id = substr($source_base_name, 2);
297 if (!-e $source_name)
299 $source_name = File::Spec->catfile("$source_dir_name", "qf",
300 "$qprefix$source_id");
301 if (!-e $source_name)
303 return "'$source_name' does not exist";
305 $data_dir_name = File::Spec->catfile("$source_dir_name", "df");
306 if (!-d $data_dir_name)
308 $data_dir_name = $source_dir_name;
310 $source_dir_name = File::Spec->catfile("$source_dir_name",
316 $queued_message = new QueuedMessage($source_dir_name,
319 $sources->{$source_id} = $queued_message;
323 if (!-d $source_name)
325 return "'$source_name' is not a plain file or a directory";
328 $queue = new Queue($source_name);
329 $result = $queue->read();
335 while (($source_id, $queued_message) = each(%{$queue->{files}}))
337 $sources->{$source_id} = $queued_message;
344 ## LOCK_FILE -- Opens and then locks a file.
346 ## Opens a file for read/write and uses flock to obtain a lock on the
347 ## file. The flock is Perl's flock which defaults to flock on systems
348 ## that support it. On systems without flock it falls back to fcntl
349 ## locking. This script will also call fcntl explicitly if flock
350 ## uses BSD semantics (i.e. if both flock() and fcntl() can successfully
351 ## lock the file at the same time)
354 ## file_name -- The name of the file to open and lock.
357 ## (file_handle, error_string) -- If everything works then
358 ## file_handle is a reference to a file handle and
359 ## error_string is undef. If there is a problem then
360 ## file_handle is undef and error_string is a string
361 ## explaining the problem.
366 my $file_name = shift;
369 if ($lock_both == -1)
371 if (open(DEVNULL, '>/dev/null'))
373 my $flock_status = flock(DEVNULL, Fcntl::LOCK_EX | Fcntl::LOCK_NB);
374 my $fcntl_status = fcntl (DEVNULL, Fcntl::F_SETLK, $fcntl_structlockp);
377 $lock_both = ($flock_status && $fcntl_status);
381 # Couldn't open /dev/null. Windows system?
387 $result = sysopen(FILE_TO_LOCK, $file_name, Fcntl::O_RDWR);
390 return (undef, "Unable to open '$file_name': $!");
393 $result = flock(FILE_TO_LOCK, Fcntl::LOCK_EX | Fcntl::LOCK_NB);
396 return (undef, "Could not obtain lock on '$file_name': $!");
401 my $result2 = fcntl (FILE_TO_LOCK, Fcntl::F_SETLK, $fcntl_structlockp);
404 return (undef, "Could not obtain fcntl lock on '$file_name': $!");
408 return (\*FILE_TO_LOCK, undef);
412 ## UNLOCK_FILE -- Unlocks a file.
414 ## Unlocks a file using Perl's flock.
417 ## file -- A file handle.
420 ## error_string -- If undef then no problem. Otherwise it is a
421 ## string that explains problem.
429 $result = flock($file, Fcntl::LOCK_UN);
432 return "Unlock failed on '$result': $!";
436 my $result2 = fcntl ($file, Fcntl::F_SETLK, $fcntl_structunlockp);
439 return (undef, "Fcntl unlock failed on '$result': $!");
447 ## MOVE_FILE -- Moves a file.
452 ## src_name -- The name of the file to be move.
453 ## dst_name -- The name of the place to move it to.
456 ## error_string -- If undef then no problem. Otherwise it is a
457 ## string that explains problem.
462 my $src_name = shift;
463 my $dst_name = shift;
466 $result = File::Copy::move($src_name, $dst_name);
469 return "File move from '$src_name' to '$dst_name' failed: $!";
477 ## CONTROL_FILE - Represents a sendmail queue control file.
479 ## This object represents represents a sendmail queue control file.
480 ## It can parse and lock its file.
489 my $class = ref($this) || $this;
492 $self->initialize(@_);
499 my $queue_dir = shift;
502 $self->{file_name} = $queue_dir . '/' . $qprefix . $self->{id};
503 $self->{headers} = {};
507 ## PARSE - Parses the control file.
509 ## Parses the control file. It just sticks each entry into a hash.
510 ## If a key has more than one entry, then it points to a list of
525 'C' => 'controlling_user',
526 'D' => 'data_file_name',
527 'd' => 'data_file_directory',
528 'E' => 'error_recipient',
530 'H' => 'parse_header',
531 'I' => 'inode_number',
532 'K' => 'next_delivery_time',
533 'L' => 'content-length',
535 'N' => 'num_delivery_attempts',
537 'Q' => 'original_recipient',
539 'q' => 'quarantine_reason',
540 'r' => 'final_recipient',
542 'T' => 'creation_time',
544 'Y' => 'current_delay',
556 open(CONTROL_FILE, "$self->{file_name}");
557 while ($line = <CONTROL_FILE>)
559 $line_type = substr($line, 0, 1);
560 if ($line_type eq "\t" && $last_type eq 'H')
567 $line_value = substr($line, 1);
569 $member_name = $parse_table{$line_type};
570 $last_type = $line_type;
573 $member_name = 'unknown';
575 if ($self->can($member_name))
577 $self->$member_name($line_value);
579 $member = $self->{$member_name};
582 $self->{$member_name} = $line_value;
585 if (ref($member) eq 'ARRAY')
587 push(@{$member}, $line_value);
590 $self->{$member_name} = [$member, $line_value];
601 my $headers = $self->{headers};
602 my $last_header = $self->{last_header};
607 $first_char = substr($line, 0, 1);
608 if ($first_char eq "?")
610 $line = (split(/\?/, $line,3))[2];
612 elsif ($first_char eq "\t")
614 if (ref($headers->{$last_header}) eq 'ARRAY')
616 $headers->{$last_header}[-1] =
617 $headers->{$last_header}[-1] . $line;
621 $headers->{$last_header} = $headers->{$last_header} .
626 ($header_name, $header_value) = split(/:/, $line, 2);
627 $self->{last_header} = $header_name;
628 if (exists $headers->{$header_name})
630 $headers->{$header_name} = [$headers->{$header_name},
635 $headers->{$header_name} = $header_value;
643 return (defined $self->{lock_handle});
652 if ($self->is_locked())
658 ($lock_handle, $result) = ::lock_file($self->{file_name});
664 $self->{lock_handle} = $lock_handle;
674 if (!$self->is_locked())
680 $result = ::unlock_file($self->{lock_handle});
682 $self->{lock_handle} = undef;
693 $result = open(QUEUE_FILE, $self->{file_name});
696 return "Unable to open '$self->{file_name}': $!";
698 @result = stat(QUEUE_FILE);
701 return "Unable to stat '$self->{file_name}': $!";
703 $self->{control_size} = $result[7];
704 $self->{control_last_mod_time} = $result[9];
719 $result = unlink($self->{file_name});
722 return "Unable to delete $self->{file_name}: $!";
729 ## DATA_FILE - Represents a sendmail queue data file.
731 ## This object represents represents a sendmail queue data file.
732 ## It is really just a place-holder.
740 my $class = ref($this) || $this;
743 $self->initialize(@_);
750 my $data_dir = shift;
752 my $control_file = shift;
754 $self->{file_name} = $data_dir . '/df' . $self->{id};
755 return if -e $self->{file_name};
756 $control_file->parse();
757 return if !defined $control_file->{data_file_directory};
758 $data_dir = $queue_root . '/' . $control_file->{data_file_directory};
760 if (-d ($data_dir . '/df'))
764 $self->{file_name} = $data_dir . '/df' . $self->{id};
773 $result = open(QUEUE_FILE, $self->{file_name});
776 return "Unable to open '$self->{file_name}': $!";
778 @result = stat(QUEUE_FILE);
781 return "Unable to stat '$self->{file_name}': $!";
783 $self->{body_size} = $result[7];
784 $self->{body_last_mod_time} = $result[9];
792 $result = unlink($self->{file_name});
795 return "Unable to delete $self->{file_name}: $!";
802 ## QUEUED_MESSAGE - Represents a queued sendmail message.
804 ## This keeps track of the files that make up a queued sendmail
806 ## Currently it has 'control_file' and 'data_file' as members.
808 ## You can tie it to a fetch only hash using tie. You need to
809 ## pass a reference to a QueuedMessage as the third argument
813 package QueuedMessage;
818 my $class = ref($this) || $this;
821 $self->initialize(@_);
828 my $queue_dir = shift;
830 my $data_dir = shift;
833 $self->{control_file} = new ControlFile($queue_dir, $id);
836 $data_dir = $queue_dir;
838 $self->{data_file} = new DataFile($data_dir, $id, $self->{control_file});
841 sub last_modified_time
845 @result = stat($self->{data_file}->{file_name});
852 my $class = ref($this) || $this;
862 if (exists $self->{control_file}->{$key})
864 return $self->{control_file}->{$key};
866 if (exists $self->{data_file}->{$key})
868 return $self->{data_file}->{$key};
878 return $self->{control_file}->lock();
885 return $self->{control_file}->unlock();
891 my $destination = shift;
896 $result = $self->lock();
902 $qf_dest = File::Spec->catfile($destination, "qf");
905 $df_dest = File::Spec->catfile($destination, "df");
908 $df_dest = $destination;
913 $qf_dest = $destination;
914 $df_dest = $destination;
917 if (-e File::Spec->catfile($qf_dest, "$qprefix$self->{id}"))
919 $result = "There is already a queued message with id '$self->{id}' in '$destination'";
924 $result = ::move_file($self->{data_file}->{file_name},
930 $result = ::move_file($self->{control_file}->{file_name},
943 return $self->{control_file}->parse();
950 $self->{control_file}->do_stat();
951 $self->{data_file}->do_stat();
967 $result = $self->{control_file}->delete();
972 $result = $self->{data_file}->delete();
986 $command = "sendmail -qI$self->{id} -O Timeout.queuereturn=now";
987 # print("$command\n");
992 ## QUEUE - Represents a queued sendmail queue.
994 ## This manages all of the messages in a queue.
1002 my $class = ref($this) || $this;
1004 bless $self, $class;
1005 $self->initialize(@_);
1013 $self->{queue_dir} = shift;
1014 $self->{files} = {};
1018 ## READ - Loads the queue with all of the objects that reside in it.
1020 ## This reads the queue's directory and creates QueuedMessage objects
1021 ## for every file in the queue that starts with 'qf' or 'hf'
1022 ## (depending on the -Q option).
1036 $control_dir = File::Spec->catfile($self->{queue_dir}, 'qf');
1038 if (-e $control_dir)
1040 $data_dir = File::Spec->catfile($self->{queue_dir}, 'df');
1043 $data_dir = $self->{queue_dir};
1048 $data_dir = $self->{queue_dir};
1049 $control_dir = $self->{queue_dir};
1052 $result = opendir(QUEUE_DIR, $control_dir);
1055 return "Unable to open directory '$control_dir'";
1058 @control_files = grep { /^$qprefix.*/ && -f "$control_dir/$_" } readdir(QUEUE_DIR);
1059 closedir(QUEUE_DIR);
1060 foreach $file_name (@control_files)
1062 $id = substr($file_name, 2);
1063 $queued_message = new QueuedMessage($control_dir, $id,
1065 $self->{files}->{$id} = $queued_message;
1073 ## ADD_QUEUED_MESSAGE - Adds a QueuedMessage to this Queue.
1075 ## Adds the QueuedMessage object to the hash and moves the files
1076 ## associated with the QueuedMessage to this Queue's directory.
1079 sub add_queued_message
1082 my $queued_message = shift;
1085 $result = $queued_message->move($self->{queue_dir});
1091 $self->{files}->{$queued_message->{id}} = $queued_message;
1097 ## ADD_QUEUE - Adds another Queue's QueuedMessages to this Queue.
1099 ## Adds all of the QueuedMessage objects in the passed in queue
1111 while (($id, $queued_message) = each %{$queue->{files}})
1113 $result = $self->add_queued_message($queued_message);
1116 print("$result.\n");
1122 ## ADD - Adds an item to this queue.
1124 ## Adds either a Queue or a QueuedMessage to this Queue.
1134 $type_name = ref($source);
1136 if ($type_name eq "QueuedMessage")
1138 return $self->add_queued_message($source);
1141 if ($type_name eq "Queue")
1143 return $self->add_queue($source);
1146 return "Queue does not know how to add a '$type_name'"
1155 while (($id, $queued_message) = each %{$self->{files}})
1157 $result = $queued_message->delete();
1160 print("$result.\n");
1171 while (($id, $queued_message) = each %{$self->{files}})
1173 $result = $queued_message->bounce();
1176 print("$result.\n");
1184 ## This next section is for any class that has an interface called
1185 ## check_move(source, dest). Each class represents some condition to
1186 ## check for to determine whether we should move the file from
1194 ## This Condition Class checks the modification time of the
1195 ## source file and returns true if the file's modification time is
1196 ## older than the number of seconds the class was initialized with.
1204 my $class = ref($this) || $this;
1206 bless $self, $class;
1207 $self->initialize(@_);
1215 $self->{age_in_seconds} = shift;
1223 if ((time() - $source->last_modified_time()) > $self->{age_in_seconds})
1234 ## Takes a list of Move Condition Classes. Check_move returns true
1235 ## if every Condition Class in the list's check_move function returns
1244 my $class = ref($this) || $this;
1246 bless $self, $class;
1247 $self->initialize(@_);
1255 $self->{condition_list} = [];
1261 my $new_condition = shift;
1263 push(@{$self->{condition_list}}, $new_condition);
1274 foreach $condition (@{$self->{condition_list}})
1276 if (!$condition->check_move($source, $dest))
1288 ## Takes a perl expression and evaluates it. The ControlFile object
1289 ## for the source QueuedMessage is available through the name '$msg'.
1297 my $class = ref($this) || $this;
1299 bless $self, $class;
1300 $self->initialize(@_);
1308 $self->{expression} = shift;
1319 $source->setup_vars();
1320 tie(%msg, 'QueuedMessage', $source);
1321 $result = eval($self->{expression});