Peter Blair

C Code to run postfix style pcre header checks

I had to write a proof of concept at work which never was introduced into any production software, so I thought that I’d share it up here.

The code is setup to operate like the postfix header_checks but doesn’t require all of the options.

The code accepts a config file of search patterns, actions and return messages:


/.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*/ WARN Domain $1 scored in the 80s
/.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*/ REJECT Rejecting mail: Domain scored at $1
/^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*/ REJECT Block Mr $1

Notice that the return code has “$1″ in it, which will be replaced by the first grouped pattern matched.

I’ve included a Perl script used for testing the application:

my @strings = qw( DNSBL:pete.com-127.0.0.99,X:blah, Y:Zing,DNSBL:richard.com-127.0.0.93,X:blarg Y:Zing,DNSBL:firefly-test.com-127.0.0.85,Foo:Bar );
 
for( @strings ){
        my @output = `./regex -f table.regex -s "$_"`;
        print for ( @output );
}
$ ./test.pl
Reading from: table.regex
Read: [.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*],[WARN],[Domain $1 scored in the 80s ]
Read: [.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*],[REJECT],[Rejecting mail: Domain scored at $1 ]
Read: [^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*],[REJECT],[Block Mr $1     ]
Matching [DNSBL:pete.com-127.0.0.99,X:blah,] against [.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*]
Matching [DNSBL:pete.com-127.0.0.99,X:blah,] against [.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*]
+Evaluated $1 = [99]
++replaced [Rejecting mail: Domain scored at 99 ]
Matching [DNSBL:pete.com-127.0.0.99,X:blah,] against [^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*]
Reading from: table.regex
Read: [.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*],[WARN],[Domain $1 scored in the 80s ]
Read: [.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*],[REJECT],[Rejecting mail: Domain scored at $1 ]
Read: [^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*],[REJECT],[Block Mr $1     ]
Matching [Y:Zing,DNSBL:richard.com-127.0.0.93,X:blarg] against [.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*]
Matching [Y:Zing,DNSBL:richard.com-127.0.0.93,X:blarg] against [.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*]
+Evaluated $1 = [93]
++replaced [Rejecting mail: Domain scored at 93 ]
Matching [Y:Zing,DNSBL:richard.com-127.0.0.93,X:blarg] against [^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*]
Reading from: table.regex
Read: [.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*],[WARN],[Domain $1 scored in the 80s ]
Read: [.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*],[REJECT],[Rejecting mail: Domain scored at $1 ]
Read: [^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*],[REJECT],[Block Mr $1     ]
Matching [Y:Zing,DNSBL:firefly-test.com-127.0.0.85,Foo:Bar] against [.*DNSBL:(\S+?)-127\.\d+\.\d+\.(8[0-9]),.*]
+Evaluated $1 = [firefly-test.com]
++replaced [Domain firefly-test.com scored in the 80s ]
Matching [Y:Zing,DNSBL:firefly-test.com-127.0.0.85,Foo:Bar] against [.*DNSBL:\S+?-127\.\d+\.\d+\.(9[0-9]),.*]
Matching [Y:Zing,DNSBL:firefly-test.com-127.0.0.85,Foo:Bar] against [^X-Spam-Summary:.*,DNSBL:(\S+?)-dnsbl7\.mailshell\.net-127\.1\.0\.1(?:9[0-9]),.*]

The source code can be found here


Categorised as: Programming


Comments are closed.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes