blib/script/qqtool.pl untested

filter: Statementstmt 0.0% 0 / 194 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 342 SCAR 75.5
CC
43
Cov
0%
message_delete
132.0
messages_delete
56.0
messages_list
42.0
CRAP 1892.0
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4  
5 use vars qw/ $opt_a $opt_h $opt_q $opt_s $opt_v $remotes $locals /;
6  
7 use English;
8 use File::Basename;
9 use Getopt::Std;
10 use Pod::Usage;
11  
12 getopts('a:h:q:s:v');
13  
14 print " Qmail Queue Tool v6.0.0\n\n";
15 print "Only the root user has permission to read the queue.
16 You are not root, goodbye!\n"
17 and exit 0 if $UID != 0;
18  
19 pod2usage() if ! $opt_a;
20  
21 # Make sure the qmail queue directory is set correctly
22 my $qdir = '/var/qmail/queue';
23 exit 0 unless -d $qdir;
24  
25 # if a queue is specified, only check it.
26 print "$0, getting list of messages in delivery queues..." if $opt_v;
27 if ($opt_q) {
28 $opt_q eq "remote" ? $remotes = messages_get("remote")
29 : $opt_q eq "local" ? $locals = messages_get("local")
30 : die "invalid queue selected!";
31 }
32 else {
33 # otherwise, check both queues
34 $remotes = messages_get("remote");
35 $locals = messages_get("local");
36 print "\n";
37 }
38  
39 print "done.\n" if $opt_v;
40  
41 $opt_a eq "list" ? messages_list ( $remotes, $locals )
42 : $opt_a eq "delete" ? messages_delete( $remotes, $locals )
43 : $opt_a eq "expire" ? messages_expire( $remotes, $locals )
44 : pod2usage()
45 ;
46  
47 exit 0;
48  
49 # -----------------------------------------------------------------------------
50 # Subroutines. No user servicable parts below this line! #
51 # -----------------------------------------------------------------------------
52  
53 sub message_delete {
54 my ( $tree, $id ) = @_;
55  
56 print "Deleting message $id...";
57  
58 # for each message id, check each of the queues and remove it.
59 if ( -f "$qdir/local/$tree/$id" ) {
60 print "\t deleting file $qdir/local/$tree/$id\n" if ($opt_v);
61 unlink "$qdir/local/$tree/$id" or die "couldn't delete: $!";
62 }
63  
64 if ( -f "$qdir/remote/$tree/$id" ) {
65 print "\t deleting file $qdir/remote/$tree/$id\n" if ($opt_v);
66 unlink "$qdir/remote/$tree/$id" or die "couldn't delete: $!";
67 }
68  
69 if ( -f "$qdir/info/$tree/$id" ) {
70 print "\t deleting file $qdir/info/$tree/$id\n" if ($opt_v);
71 unlink "$qdir/info/$tree/$id" or die "couldn't delete: $!";
72 }
73  
74 if ( -f "$qdir/mess/$tree/$id" ) {
75 print "\t deleting file $qdir/mess/$tree/$id\n" if ($opt_v);
76 unlink "$qdir/mess/$tree/$id" or die "couldn't delete: $!";
77 }
78  
79 if ( -f "$qdir/bounce/$id" ) {
80 print "\t deleting file $qdir/bounce/$id\n" if ($opt_v);
81 unlink "$qdir/bounce/$id" or die "couldn't delete: $!";
82 }
83  
84 print "done.\n";
85 }
86  
87 sub messages_delete {
88  
89 my $svc_dir = '/var/qmail/control/qmail-smtpd';
90 if ( ! -d $svc_dir ) {
91 warn "The service directory does not exist: $svc_dir";
92 return;
93 }
94 warn "checking control dir $svc_dir, ok";
95  
96 my $r = send_signal('-d');
97 die "qmail-send wouldn't die!\n" if ($r);
98  
99 # we'll get passed an array of the local, remote, or both queues
100 foreach my $q (@_) {
101 foreach my $hash (@$q) {
102 my $header = headers_get( $hash->{'tree'}, $hash->{'num'} );
103  
104 unless ($opt_s) {
105 message_delete( $hash->{'tree'}, $hash->{'num'} );
106 next;
107 }
108  
109 if ($opt_h) {
110 if ( $header->{$opt_h} =~ /$opt_s/ ) {
111 message_delete( $hash->{'tree'}, $hash->{'num'} );
112 }
113 }
114 else {
115 foreach my $key ( keys %$header ) {
116 if ( $header->{$key} =~ /$opt_s/ ) {
117 message_delete( $hash->{'tree'}, $hash->{'num'} );
118 }
119 }
120 }
121 }
122 }
123  
124 send_signal('-u');
125 }
126  
127 sub message_expire {
128 my ($file) = @_;
129  
130 # set $ago to 8 days old.
131 my $ago = time - 8 * 24 * 60 * 60;
132  
133 # alter the timestamp of the file to 8 days ago.
134 utime $ago, $ago, $file;
135 print "Expired $file\n";
136 }
137  
138 sub messages_expire {
139  
140 foreach my $q (@_) {
141 foreach my $hash (@$q) {
142 my $header = headers_get( $hash->{'tree'}, $hash->{'num'} );
143 my $id = "$hash->{'tree'}/$hash->{'num'}";
144  
145 unless ($opt_s) {
146 message_expire("$qdir/info/$id");
147 next;
148 }
149  
150 if ($opt_h) {
151 if ( $header->{$opt_h} =~ /$opt_s/ ) {
152 message_expire("$qdir/info/$id");
153 }
154 }
155 else {
156 foreach my $key ( keys %$header ) {
157 if ( $header->{$key} =~ /$opt_s/ ) {
158 message_expire("$qdir/info/$id");
159 }
160 }
161 }
162 }
163 }
164  
165 system "qmailctl doqueue";
166  
167 print "NOTICE: Expiring the messages does not remove them from the queue.
168 It merely alters their expiration time. The messages will be removed from
169 the queue after qmail attempts to deliver them one more time.
170
171 I've already told qmail to start that process so be patient while qmail
172 is processing the queue. This might be a good time to check the value of
173 /var/qmail/control/concurrencyremote and verify its value is reasonable
174 for your site.\n\n";
175  
176 =head2 Message Expiration
177  
178 Expiring messages does not remove them from the queue. It merely alters their expiration time. The messages will be removed from the queue after qmail attempts to deliver them one last time.
179  
180 =cut
181  
182 }
183  
184 sub messages_list {
185  
186 QUEUE:
187 foreach my $queue (@_) {
188  
189 # skip to the next queue if it's empty
190 next QUEUE if ! $_[0];
191  
192 #print "message $queue starting\n";
193 foreach my $hash (@$queue) {
194  
195 #use Data::Dumper; print Dumper($hash);
196 my $header = headers_get( $hash->{'tree'}, $hash->{'num'} );
197 my $id = "$hash->{'tree'}/$hash->{'num'}";
198 print "id: $id\n";
199  
200 unless ($opt_s) {
201 message_print( $id, $header );
202 next;
203 }
204  
205 if ($opt_h) {
206 message_print( $id, $header )
207 if ( $header->{$opt_h} =~ /$opt_s/ );
208 }
209 else {
210 foreach my $key ( keys %$header ) {
211 if ( $header->{$key} =~ /$opt_s/ ) {
212 message_print( $id, $header );
213 exit;
214 }
215 }
216 }
217 }
218 }
219 }
220  
221 sub message_print {
222  
223 my ( $id, $header ) = @_;
224  
225 print "message # $id ";
226 print "To: $header->{'To'}\n";
227 print "From: $header->{'From'}\n";
228 print "Subject: $header->{'Subject'}\n";
229  
230 if ($opt_v) {
231 if ( $header->{'CC'} ) {
232 print "CC: $header->{'CC'}\n";
233 }
234 print "Date: $header->{'Date'}\n";
235 my $rp = `head -n1 "$qdir/info/$id"`;
236 print "Return Path: $rp\n";
237 }
238  
239 print "\n";
240 }
241  
242 sub headers_get {
243  
244 my ( $tree, $id ) = @_;
245 my %hash;
246  
247 my ($FILE, $header);
248  
249 # a better way to read in the headers
250 # from http://perl.plover.com/lp/Spam.html
251 if ( open $FILE, '<', "$qdir/mess/$tree/$id" )
252 {
253 local $/ = ""; # enable localized slurp mode
254 $header = <$FILE>; # read in the message headers
255 undef $/; # reset it back to normal
256 #$body = <STDIN>;
257 };
258  
259 foreach my $line ( split /\n/, $header ) {
260 #print "$line\n"; sleep 1;
261 if ( $line =~ /^([a-zA-Z\-]*):\s+(.*?)$/ ) {
262 print "header: $line\n" if $opt_v;
263 $hash{$1} = $2;
264 }
265 else {
266 print "body: $line\n" if $opt_v;
267 }
268 }
269 return \%hash;
270 }
271  
272 sub messages_get {
273  
274 my ($qsubdir) = @_;
275 my $queue = "$qdir/$qsubdir"; # /var/qmail/queue/[local|remote]
276  
277 my ( @messages, $up1dir, $id, $bucket, $queu );
278  
279 unless ( -e $queue ) {
280 print "ERROR: queue $queue does not exist!\n";
281 return 0;
282 }
283 unless ( -d $queue ) {
284 print "ERROR: queue $queue is not a directory!\n";
285 return 0;
286 }
287  
288 unless ( -r $queue ) {
289 print "ERROR: queue $queue is not readable by you!\n";
290 return 0;
291 }
292  
293 # eache queue has "buckets" within it that we need to iterate over
294 foreach my $queue_buckets ( get_dir_files( $queue ) ) {
295  
296 # within each bucket is files that contain the email address we
297 # are trying to deliver to.
298  
299 foreach my $file ( get_dir_files( $queue_buckets ) ) {
300  
301 # id is the message id
302 ($id, $up1dir) = fileparse($file); chop $up1dir;
303 ($bucket, $up1dir) = fileparse($up1dir); chop $up1dir;
304 ($queu, $up1dir) = fileparse($up1dir);
305  
306 print "messages_get: id: $id\n" if ($opt_v);
307  
308 my %message_details = (
309 num => $id,
310 file => $file,
311 tree => $bucket,
312 queu => $queu
313 );
314 push @messages, \%message_details;
315  
316 print "messages_get: file : $file\n" if ($opt_v);
317 }
318 }
319 my $count = @messages;
320 print "$qsubdir has $count messages\n";
321  
322 return \@messages;
323 }
324  
325 sub send_signal {
326 my $signal = shift;
327  
328 my $svc = '/usr/local/bin/svc';
329 my $svstat = '/usr/local/bin/svstat';
330  
331 my $qcontrol = '/var/qmail/supervise/qmail-send';
332  
333 if ( ! $qcontrol) {
334 warn "the service directory $qcontrol is missing! Giving up.";
335 return;
336 }
337  
338 if ($UID != 0) {
339 warn "Only root can control supervised daemons, and you aren't root!";
340 return;
341 };
342  
343 # send qmail-send a TERM signal
344 system "$svc $signal $qcontrol";
345  
346 # loop up to a hundred seconds waiting for qmail-send to exit
347 foreach my $i ( 1 .. 100 ) {
348 my $r = `$svstat $qcontrol`;
349 chomp $r;
350 if ( $r =~ /^.*:\sdown\s[0-9]*\sseconds/ ) {
351 print "Yay, we're down!\n";
352 return;
353 }
354 elsif ( $r =~ /supervise not running/ ) {
355 print "Yay, we're down!\n";
356 return;
357 }
358 else {
359  
360 # if more than 10 seconds passes, lets kill off the qmail-remote
361 # processes that are forcing us to wait.
362  
363 if ( $i > 10 ) {
364 system "killall qmail-remote qmail-send";
365 }
366 }
367 sleep 1;
368 }
369 return 1;
370 }
371  
372 sub get_dir_files {
373 my $dir = shift;
374 my @files;
375  
376 opendir D, $dir or do {
377 warn "couldn't open $dir: $!";
378 return;
379 };
380  
381 while ( defined( my $f = readdir(D) ) ) {
382 next if $f =~ /^\.\.?$/;
383 push @files, "$dir/$f";
384 }
385  
386 closedir(D);
387  
388 return @files;
389 }
390  
391 1;
392 __END__
393 sub {}
394  
395 =head1 NAME
396  
397 qqtool.pl - A tool for viewing and purging messages from a qmail queue
398  
399  
400 =head1 SYNOPSIS
401  
402 -a action (delete, expire, list)
403 -h header to match (From, To, Subject, Date)
404 -q queue to search (local/remote)
405 -s search (pattern to search for)
406 -v verbose
407
408 If no -h is specified, then the pattern is searched for in any header.
409 If no -q is specified, then both queues are searched.
410  
411 To list messages in queue from matt:
412 qqtool.pl -a list -s matt -h From
413  
414 To list messages in queue with string \"foo\" in the headers:
415 qqtool.pl -a list -s foo
416  
417  
418 =head1 DESCRIPTION
419  
420 Qmail Queue Tool (qqtool.pl)
421  
422 This program will allow you to search and view messages in your qmail queue. It will also allow you to remove them, via expiration or deletion. It was written by Matt Simerson for the toaster users on mail-toaster@simerson.net
423  
424 ChangeLog - http://www.tnpi.net/internet/mail/qqtool/changelog.shtml
425  
426  
427 =head1 INSTALL
428  
429 Download Mail::Toaster from http://mail-toaster.org/Mail-Toaster.tar.gz
430  
431 fetch Mail-Toaster.tar.gz
432 tar -xzf Mail-Toaster.tar.gz
433 cd Mail-Toaster-x.xx
434 perl Makefile.PL
435 make install
436 rehash
437  
438 Run the script without any parameters and it will show you a menu of options.
439  
440 qqtool.pl
441  
442 =head2 Sample Output
443  
444 # qqtool.pl
445 Qmail Queue Tool v 1.9
446  
447 -a action (delete, expire, list)
448 -h header to match (From, To, Subject, Date)
449 -q queue to search (local/remote)
450 -s search (pattern to search for)
451 -v verbose
452  
453 If no -h is specified, then the pattern is searched for in any header. If no -q is specified, then both queues are searched.
454  
455 To list messages in queue from matt:
456  
457 ./Mail-Toaster/qqtool.pl -a list -s matt -h From
458  
459 To list messages in queue with string "foo" in the headers:
460  
461 ./Mail-Toaster/qqtool.pl -a list -s foo
462  
463  
464 =head2 User Preferences
465  
466 There is one settings you can alter:
467  
468 $qdir is the path to your qmail queue
469  
470 If you aren't using the default (/var/qmail/queue), edit qqtool.pl and adjust it.
471  
472  
473  
474 =head1 AUTHOR
475  
476 Matt Simerson <matt@tnpi.net>
477  
478  
479 =head1 CREDITS
480  
481 Idea based on mailRemove.py by Dru Nelson <dru@redwoodsoft.com>, ideas borrowed from qmHandle by Michele Beltrame <mick@io.com>
482  
483 Community funding was contributed by the following mail-toaster@simerson.net mailing list subscribers:
484  
485 erik erik at microcontroller.nl (organizer)
486 Rick Romero rick at valeoinc.com
487 Chris Eaton Chris.Eaton at med.ge.com
488 Marius Kirschner marius at agoron.com
489 J. Vicente Carrasco carvay at teleline.es
490 Chris Odell chris at redstarnetworks.net
491 Pat Hayes pat at pathayes.net
492 Dixon Cole dixon at levee.net
493 Randy Meyer rjmeyer at humbleguys.net
494 kristian kristian at waveit.com
495 Michael Andreasen michael at subwire.dk (beer)
496 Nathan Nieblas nnieblas at microtosh.net
497 Randy Jordan ctech at pcwarp.com
498  
499  
500 =head1 BUGS
501  
502 Report to author. Patches welcome.
503  
504  
505 =head1 TODO
506  
507 In list mode, when showing messages in the queue, show which addresses delivery has failed for, so you know exactly why a message is still in the queue (useful for mailing lists with many recipients)
508  
509 Interactive mode - step through messages offering to delete/expire/skip each
510  
511 Clean mode - Leave qmail down after stopping it, useful for multiple invocations
512  
513 Write the messages into a "inactive" queue before deleting them.
514  
515 Ability to restore messages from "inactive" to the real queue.
516  
517  
518 =head1 SEE ALSO
519  
520 http://www.mail-toaster.org/
521  
522  
523 =head1 COPYRIGHT
524  
525 Copyright 2003-2013, The Network People, Inc. All Rights Reserved.
526  
527 =cut