source: postfix-logtools/poststat/anonymizer.pl

Last change on this file was 627, checked in by ben, 9 years ago

cleaning deliver, see #28

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1#!/usr/bin/env perl
2
3# 2010-06-14 <vschmitt@bearstech.com>
4
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# Anonymizer for Postfix mail logs.
19# Usage example :
20#     zcat -f /var/log/mail/mail.log* | anonymizer.pl --backup-mx mx.backup.server1.com mx.backup.server2.org
21
22use strict;
23use warnings;
24use Getopt::Long;
25
26my @backupmx;
27my @lanservers;
28GetOptions(
29  "backup-mx=s{,}" => \@backupmx,
30  "lan-servers=s{,}" => \@lanservers
31);
32my $hostname_whitelist = join('|',@backupmx,'localhost',@lanservers);
33
34while (<>) {
35    # Anonymize mail server's hostname and move dates to a fixed offset.
36    next unless s/^[a-zA-Z]+ +\d+ ([\d:]{8}) \w+ /Apr 1 $1 host /;
37    # Only keep postfix/* milter-greylist deliver and amavis related lines
38    next unless m/^Apr 1 ([\d:]{8}) host (postfix\/|milter-greylist\:|.*deliver\(|amavis)/;
39    # Scramble ip addresses.
40    s| (([\d]{1,3}\.){3}[\d]{1,3}) | join('.', map {int($_/3)} split(/\./, $1)) |gex;
41    # Scramble email addresses and message ids.
42    s/<[^<]*@[^<]*(\.[a-zA-Z]{2,6})>/<name\@domain$1>/g;
43    s/(sasl_username=).*@.*(\.[a-zA-Z]{2,6})/$1name\@domain$2/g;
44    s/(message-id)=<.*@.*>/$1=<$1\@domain>/g;
45    # Scramble hostnames.
46    s/(from )[\w\-\.]*/$1hostname/g unless /from ($hostname_whitelist)/;
47    s/(hostname |connect to |client=|relay=)[\w\-\.]*/$1hostname/g 
48        unless /(hostname |client=|relay=)localhost/;
49    s/(helo=<).*>/$1hostname>/;
50    s/<[^<]*>:/<hostname_or_email_address>:/g;
51    print $_;
52}
53
Note: See TracBrowser for help on using the repository browser.