Can remove the sender and/or the recipient from the key of the greylister.
[apps/pfixtools.git] / tools / postgrey2postlicyd
1 #!/usr/bin/env python
2 ##############################################################################
3 #          pfixtools: a collection of postfix related tools                  #
4 #          ~~~~~~~~~                                                         #
5 #  ________________________________________________________________________  #
6 #                                                                            #
7 #  Redistribution and use in source and binary forms, with or without        #
8 #  modification, are permitted provided that the following conditions        #
9 #  are met:                                                                  #
10 #                                                                            #
11 #  1. Redistributions of source code must retain the above copyright         #
12 #     notice, this list of conditions and the following disclaimer.          #
13 #  2. Redistributions in binary form must reproduce the above copyright      #
14 #     notice, this list of conditions and the following disclaimer in the    #
15 #     documentation and/or other materials provided with the distribution.   #
16 #  3. The names of its contributors may not be used to endorse or promote    #
17 #     products derived from this software without specific prior written     #
18 #     permission.                                                            #
19 #                                                                            #
20 #  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   #
21 #  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     #
22 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        #
23 #  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    #
24 #  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    #
25 #  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      #
26 #  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  #
27 #  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   #
28 #  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   #
29 #  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    #
30 #  THE POSSIBILITY OF SUCH DAMAGE.                                           #
31 ##############################################################################
32
33 # Copyright (c) 2008 Aymeric Augustin
34
35
36 # Convert the postgrey_whitelist_clients file to a format
37 # suitable for use with the postlicyd Postfix policy daemon
38
39
40 import os, re, sys
41
42 def process(infile, outfile):
43
44     # Write headers
45     outfile.write("# Do not edit, file autogenerated by %s\n" % sys.argv[0])
46     if len(sys.argv) > 1 and sys.argv[1] != '-':
47         outfile.write("# This file has been generated from %s\n" % sys.argv[1])
48
49     re_domain_name = re.compile(r'[a-z0-9.\-]+\.[a-z]+')
50     re_ip_address = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
51
52     # Store each entry to avoid duplicates
53     entries = []
54
55     for line in infile:
56         # Comments: keep them
57         if line == '\n' or line[0] == '#':
58             outfile.write(line)
59         # IP addresses: keep as is
60         elif re_ip_address.match(line):
61             outfile.write(line)
62         # Regexps: extract final constant part
63         elif line[0] == '/':
64             line = line.rstrip(r'$/').replace(r'\.', r'.')
65             host = re_domain_name.findall(line)[-1]
66             result = host + '\n'
67             if result not in entries:
68                 entries.append(result)
69                 outfile.write(result)
70         # Domain names: prepend a dot if the domain name contains only one dot
71         elif re_domain_name.match(line):
72             if line.count('.') < 2:
73                 result = '.' + line
74             else:
75                 result = line
76             if result not in entries:
77                 entries.append(result)
78                 outfile.write(result)
79         # Unrecognized: report on stderr and comment in output
80         else:
81             outfile.write('# IGNORED: ' + line)
82             sys.stderr.write("Couldn't process line: %s" % line)
83
84
85 if __name__ == '__main__':
86
87     # Check number of arguments
88     if len(sys.argv) > 3:
89         print "Usage: %s [input] [output]" % sys.argv[0]
90         print "If input/output is omitted or -, stdin/stdout is used."
91         sys.exit(1)
92
93     # Parse first argument
94     if len(sys.argv) > 1 and sys.argv[1] != '-':
95         infile = open(sys.argv[1], 'r')
96     else:
97         infile = sys.stdin
98
99     # Parse second argument
100     if len(sys.argv) > 2 and sys.argv[2] != '-':
101         if sys.argv[1] == sys.argv[2]:
102             print "Source file and destination file are identical, aborting"
103             sys.exit(1)
104         if os.path.exists(sys.argv[2]):
105             print "Destination file %s already exists, aborting" % sys.argv[2]
106             sys.exit(1)
107         outfile = open(sys.argv[2], 'w')
108     else:
109         outfile = sys.stdout
110
111     # Do the processing
112     process(infile, outfile)