OpenSSL: fix bulid on older library versions
[exim.git] / src / src / exigrep.src
CommitLineData
8c4f17b3 1#! PERL_COMMAND
059ec3d9 2
8c4f17b3 3use warnings;
059ec3d9 4use strict;
4d3d955f 5BEGIN { pop @INC if $INC[-1] eq '.' };
059ec3d9 6
0a27a822 7use Pod::Usage;
82a996b1 8use Getopt::Long qw(:config no_ignore_case);
983da878 9use File::Basename;
0a27a822 10
9242a7e8 11# Copyright (c) 2007-2017 University of Cambridge.
059ec3d9
PH
12# See the file NOTICE for conditions of use and distribution.
13
14# Except when they appear in comments, the following placeholders in this
15# source are replaced when it is turned into a runnable script:
16#
17# PERL_COMMAND
18# ZCAT_COMMAND
19# COMPRESS_SUFFIX
20
21# PROCESSED_FLAG
22
23# This is a perl script which extracts from an Exim log all entries
24# for all messages that have an entry that matches a given pattern.
25# If *any* entry for a particular message matches the pattern, *all*
26# entries for that message are displayed.
27
28# We buffer up information on a per-message basis. It is done this way rather
29# than reading the input twice so that the input can be a pipe.
30
31# There must be one argument, which is the pattern. Subsequent arguments
32# are the files to scan; if none, the standard input is read. If any file
33# appears to be compressed, it is passed through zcat. We can't just do this
34# for all files, because zcat chokes on non-compressed files.
35
75b1493f
PH
36# Performance optimized in 02/02/2007 by Jori Hamalainen
37# Typical run time acceleration: 4 times
38
39
059ec3d9
PH
40use POSIX qw(mktime);
41
42
43# This subroutine converts a time/date string from an Exim log line into
44# the number of seconds since the epoch. It handles optional timezone
45# information.
46
47sub seconds {
48my($year,$month,$day,$hour,$min,$sec,$tzs,$tzh,$tzm) =
571b2715 49 $_[0] =~ /^(\d{4})-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)(?:.\d+)?(?>\s([+-])(\d\d)(\d\d))?/o;
059ec3d9
PH
50
51my $seconds = mktime $sec, $min, $hour, $day, $month - 1, $year - 1900;
52
53if (defined $tzs)
54 {
55 $seconds -= $tzh * 3600 + $tzm * 60 if $tzs eq "+";
56 $seconds += $tzh * 3600 + $tzm * 60 if $tzs eq "-";
57 }
58
59return $seconds;
60}
61
62
63# This subroutine processes a single line (in $_) from a log file. Program
64# defensively against short lines finding their way into the log.
65
0a27a822
HSHR
66my (%saved, %id_list, $pattern);
67
68my $queue_time = -1;
69my $insensitive = 1;
70my $invert = 0;
71my $related = 0;
72my $use_pager = 1;
73my $literal = 0;
74
059ec3d9 75
0eb51736 76# If using "related" option, have to track extra message IDs
0eb51736
TL
77my $related_re='';
78my @Mids = ();
79
059ec3d9 80sub do_line {
395ff96d
PH
81
82# Convert syslog lines to mainlog format, as in eximstats.
83
75b1493f 84if (!/^\d{4}-/o) { $_ =~ s/^.*? exim\b.*?: //o; }
395ff96d 85
059ec3d9 86return unless
571b2715 87 my($date,$id) = /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(?:\.\d+)? (?:[+-]\d{4} )?)(?:\[\d+\] )?(\w{6}\-\w{6}\-\w{2})?/o;
059ec3d9
PH
88
89# Handle the case when the log line belongs to a specific message. We save
90# lines for specific messages until the message is complete. Then either print
f3f065bb 91# or discard.
059ec3d9 92
75b1493f 93if (defined $id)
059ec3d9
PH
94 {
95 $saved{$id} = '' unless defined($saved{$id});
96
97 # Save up the data for this message in case it becomes interesting later.
98
99 $saved{$id} .= $_;
100
75b1493f 101 # Are we interested in this id ? Short circuit if we already were interested.
059ec3d9 102
b2d5182b
PH
103 if ($invert)
104 {
105 $id_list{$id} = 1 if (!defined($id_list{$id}));
106 $id_list{$id} = 0 if (($insensitive && /$pattern/io) || /$pattern/o);
107 }
108 else
109 {
0eb51736
TL
110 if (defined $id_list{$id} ||
111 ($insensitive && /$pattern/io) || /$pattern/o)
112 {
113 $id_list{$id} = 1;
114 get_related_ids($id) if $related;
115 }
116 elsif ($related && $related_re)
117 {
118 grep_for_related($_, $id);
119 }
b2d5182b 120 }
059ec3d9
PH
121
122 # See if this is a completion for some message. If it is interesting,
123 # print it, but in any event, throw away what was saved.
124
75b1493f 125 if (index($_, 'Completed') != -1 ||
3ce62588
PH
126 index($_, 'SMTP data timeout') != -1 ||
127 (index($_, 'rejected') != -1 &&
571b2715 128 /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(?:\.\d+)? (?:[+-]\d{4} )?)(?:\[\d+\] )?\w{6}\-\w{6}\-\w{2} rejected/o))
059ec3d9 129 {
75b1493f
PH
130 if ($queue_time != -1 &&
131 $saved{$id} =~ /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d ([+-]\d{4} )?)/o)
059ec3d9
PH
132 {
133 my $old_sec = &seconds($1);
134 my $sec = &seconds($date);
3ce62588 135 $id_list{$id} = 0 if $id_list{$id} && $sec - $old_sec <= $queue_time;
059ec3d9
PH
136 }
137
3ce62588 138 print "$saved{$id}\n" if ($id_list{$id});
b2d5182b 139 delete $id_list{$id};
059ec3d9
PH
140 delete $saved{$id};
141 }
142 }
143
144# Handle the case where the log line does not belong to a specific message.
145# Print it if it is interesting.
146
b2d5182b
PH
147elsif ( ($invert && (($insensitive && !/$pattern/io) || !/$pattern/o)) ||
148 (!$invert && (($insensitive && /$pattern/io) || /$pattern/o)) )
75b1493f 149 { print "$_\n"; }
059ec3d9
PH
150}
151
89b68021
TL
152# Rotated log files are frequently compressed and there are a variety of
153# formats it could be compressed with. Rather than use just one that is
154# detected and hardcoded at Exim compile time, detect and use what the
155# logfile is compressed with on the fly.
156#
157# List of known compression extensions and their associated commands:
158my $compressors = {
159 gz => { cmd => 'zcat', args => '' },
160 bz2 => { cmd => 'bzcat', args => '' },
161 xz => { cmd => 'xzcat', args => '' },
4f252517
AM
162 lzma => { cmd => 'lzma', args => '-dc' },
163 zst => { cmd => 'zstdcat', args => '' },
89b68021
TL
164};
165my $csearch = 0;
166
167sub detect_compressor_bin
168 {
169 my $ext = shift();
170 my $c = $compressors->{$ext}->{cmd};
171 $compressors->{$ext}->{bin} = `which $c 2>/dev/null`;
172 chomp($compressors->{$ext}->{bin});
173 }
174
175sub detect_compressor_capable
176 {
177 my $filename = shift();
178 map { &detect_compressor_bin($_) } keys %$compressors
179 if (!$csearch);
180 $csearch = 1;
181 return undef
182 unless (grep {$filename =~ /\.(?:$_)$/} keys %$compressors);
183 # Loop through them, figure out which one it detected,
184 # and build the commandline.
185 my $cmdline = undef;
186 foreach my $ext (keys %$compressors)
187 {
188 if ($filename =~ /\.(?:$ext)$/)
189 {
4c04137d 190 # Just die if compressor not found; if this occurs in the middle of
89b68021
TL
191 # two valid files with a lot of matches, error could easily be missed.
192 die("Didn't find $ext decompressor for $filename\n")
193 if ($compressors->{$ext}->{bin} eq '');
194 $cmdline = $compressors->{$ext}->{bin} ." ".
195 $compressors->{$ext}->{args};
196 last;
197 }
198 }
199 return $cmdline;
200 }
059ec3d9 201
0eb51736
TL
202sub grep_for_related {
203 my ($line,$id) = @_;
204 $id_list{$id} = 1 if $line =~ m/$related_re/;
205}
206
207sub get_related_ids {
208 my ($id) = @_;
209 push @Mids, $id unless grep /\b$id\b/, @Mids;
210 my $re = join '|', @Mids;
211 $related_re = qr/$re/;
212}
213
059ec3d9
PH
214# The main program. Extract the pattern and make sure any relevant characters
215# are quoted if the -l flag is given. The -t flag gives a time-on-queue value
0eb51736
TL
216# which is an additional condition. The -M flag will also display "related"
217# loglines (msgid from matched lines is searched in following lines).
059ec3d9 218
0a27a822
HSHR
219GetOptions(
220 'I|sensitive' => sub { $insensitive = 0 },
221 'l|literal' => \$literal,
222 'M|related' => \$related,
223 't|queue-time=i' => \$queue_time,
224 'pager!' => \$use_pager,
225 'v|invert' => \$invert,
226 'h|help' => sub { pod2usage(-exit => 0, -verbose => 1) },
227 'm|man' => sub {
228 pod2usage(
229 -exit => 0,
230 -verbose => 2,
231 -noperldoc => system('perldoc -V 2>/dev/null >&2')
232 );
233 },
983da878
HSHR
234 'version' => sub {
235 print basename($0) . ": $0\n",
236 "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
02721dcd 237 "perl(runtime): $]\n";
983da878
HSHR
238 exit 0;
239 },
0a27a822 240) and @ARGV or pod2usage;
059ec3d9
PH
241
242$pattern = shift @ARGV;
0a27a822 243$pattern = quotemeta $pattern if $literal;
059ec3d9 244
ab13201f 245# Start a pager if output goes to a terminal
0a27a822 246if (-t 1 and $use_pager)
ab13201f 247 {
b8d96756
HSHR
248 # for perl >= v5.10.x: foreach ($ENV{PAGER}//(), 'less', 'more')
249 foreach (defined $ENV{PAGER} ? $ENV{PAGER} : (), 'less', 'more')
ab13201f 250 {
503bf579 251 local $ENV{LESS} .= ' --no-init --quit-if-one-screen';
ab13201f
HSHR
252 open(my $pager, '|-', $_) or next;
253 select $pager;
254 last;
255 }
503bf579 256 }
059ec3d9
PH
257
258# If file arguments are given, open each one and process according as it is
259# is compressed or not.
260
261if (@ARGV)
262 {
263 foreach (@ARGV)
264 {
265 my $filename = $_;
fd4c285c 266 if (-x 'ZCAT_COMMAND' && $filename =~ /\.(?:COMPRESS_SUFFIX)$/o)
059ec3d9
PH
267 {
268 open(LOG, "ZCAT_COMMAND $filename |") ||
269 die "Unable to zcat $filename: $!\n";
270 }
89b68021
TL
271 elsif (my $cmdline = &detect_compressor_capable($filename))
272 {
273 open(LOG, "$cmdline $filename |") ||
274 die "Unable to decompress $filename: $!\n";
275 }
059ec3d9
PH
276 else
277 {
278 open(LOG, "<$filename") || die "Unable to open $filename: $!\n";
279 }
280 do_line() while (<LOG>);
281 close(LOG);
282 }
283 }
284
285# If no files are named, process STDIN only
286
287else { do_line() while (<STDIN>); }
288
3ce62588 289# At the end of processing all the input, print any uncompleted messages.
059ec3d9 290
79749a79
PH
291for (keys %id_list)
292 {
3ce62588 293 print "+++ $_ has not completed +++\n$saved{$_}\n";
79749a79 294 }
059ec3d9 295
e236f915
HSHR
296__END__
297
298=head1 NAME
299
300exigrep - search Exim's main log
301
302=head1 SYNOPSIS
303
304B<exigrep> [options] pattern [log] ...
305
306=head1 DESCRIPTION
307
308The B<exigrep> utility is a Perl script that searches one or more main log
309files for entries that match a given pattern. When it finds a match,
310it extracts all the log entries for the relevant message, not just
311those that match the pattern. Thus, B<exigrep> can extract complete log
312entries for a given message, or all mail for a given user, or for a
313given host, for example.
314
315If no file names are given on the command line, the standard input is read.
316
4f252517
AM
317For known file extensions indicating compression (F<.gz>, F<.bz2>, F<.xz>,
318F<.lzma>, and F<.zst>) a suitable de-compressor is used, if available.
e236f915 319
0a27a822
HSHR
320The output is sent through a pager if a terminal is connected to STDOUT. As
321pager are considered: C<$ENV{PAGER}>, C<less>, C<more>.
322
e236f915
HSHR
323=head1 OPTIONS
324
325=over
326
0a27a822 327=item B<-l>|B<--literal>
e236f915
HSHR
328
329This means 'literal', that is, treat all characters in the
330pattern as standing for themselves. Otherwise the pattern must be a
331Perl regular expression. The pattern match is case-insensitive.
332
0a27a822 333=item B<-t>|B<--queue-time> I<seconds>
e236f915
HSHR
334
335Limit the output to messages that spent at least I<seconds> in the
336queue.
337
0a27a822 338=item B<-I>|B<--sensitive>
e236f915
HSHR
339
340Do a case sensitive search.
341
0a27a822 342=item B<-v>|B<--invert>
e236f915
HSHR
343
344Invert the meaning of the search pattern. That is, print message log
345entries that are not related to that pattern.
346
0a27a822 347=item B<-M>|B<--related>
e236f915
HSHR
348
349Search for related messages too.
350
0a27a822
HSHR
351=item B<--no-pager>
352
353Do not use a pager, even if STDOUT is connected to a terminal.
354
355=item B<-h>|B<--help>
e236f915
HSHR
356
357Print a short reference help. For more detailed help try L<exigrep(8)>,
82a996b1 358or C<exigrep --man>.
e236f915 359
0a27a822 360=item B<-m>|B<--man>
e236f915
HSHR
361
362Print this manual page of B<exigrep>.
363
364=back
365
366=head1 SEE ALSO
367
368L<exim(8)>, L<perlre(1)>, L<Exim|http://exim.org/>
369
370=head1 AUTHOR
371
372This manual page was stitched together from spec.txt by Andreas Metzler L<ametzler at downhill.at.eu.org>
373and updated by Heiko Schlittermann L<hs@schlittermann.de>.
374
375=cut