source: trunk/dnsbl/export-dnsbl@ 37

Last change on this file since 37 was 37, checked in by Kris Deugau, 13 years ago

/trunk/dnsbl

Forgot to update the actual data-export lines when I added the
extra layers of bitmasking. rbldnsd does not like eg
"127.0.0.1050" as an IP to return...

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 3.8 KB
Line 
1#!/usr/bin/perl
2# Export DNSBL data
3# 2009/05/26 kdeugau@deepnet.cx
4# need to stub it out so it can use a module to actually write zone
5# data for different NS software
6# completed 2008/08/14
7
8use strict;
9use warnings;
10use DBI;
11
12use DNSBL;
13
14my $dnsbl = new DNSBL;
15
16# default DB info - all other settings should be loaded from the DB.
17my $dbhost = "localhost";
18my $dbname = "dnsbl";
19my $dbuser = "dnsbl";
20my $dbpass = "spambgone";
21
22die "Need config argument\n" if !$ARGV[0];
23my $cfgname = shift @ARGV;
24
25# Load a config ref containing DB host, name, user, and pass info based on
26# from the server name + full script web path. This allows us to host
27# multiple instances without having to duplicate the code.
28# This file is a Perl fragment to be processed inline.
29if (-e "/etc/dnsbl/$cfgname.conf") {
30 my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
31 ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
32 eval $cfg;
33}
34
35my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
36
37my %config;
38my $sth = $dbh->prepare("SELECT key,value FROM misc");
39$sth->execute;
40while (my ($key,$value) = $sth->fetchrow_array) {
41 $config{$key} = $value;
42}
43
44my %iplist;
45my $ipref = \%iplist;
46
47my $mode = $ARGV[0] || 'tiny';
48
49$dnsbl->initexport;
50#$dnsbl->export($ipref,$mode,1,'50.22.0.0/15');
51$dnsbl->export($ipref,$mode);
52
53##fixme - mode should pick actual output, not just export mode
54if ($mode eq 'cidr') {
55 # SOA, NS records. Maybe dnscache needs them?
56 print "\$SOA 900 ".($config{blzone} ? $config{blzone} : 'company').".dnsbl systems.company.com 0 1200 600 600 900\n".
57 "\$NS 3600 127.0.0.1\n".
58 "\$TTL 900\n";
59
60 # more or less raw CIDR block-and-IP info. rbldnsd format for convenience.
61 foreach (sort ipcmp keys %iplist) {
62 my $entry;
63 if ($iplist{$_} > 256) {
64 if ($iplist{$_} > 65536) {
65 $entry .= int($iplist{$_}/65536).".";
66 $iplist{$_} = $iplist{$_} % 65536;
67 } else {
68 $entry .= "0.";
69 }
70 $entry .= int($iplist{$_}/256).".";
71 $iplist{$_} = $iplist{$_} % 256;
72 } else {
73 $entry .= "0.0.";
74 }
75 $entry .= $iplist{$_};
76 my $out = "$_:127.$entry:".
77 ($iplist{$_} & 2 ?
78 ($config{iplisted} ? $config{iplisted} : '$ relayed a reported spam') :
79 ($config{blocklisted} ? $config{blocklisted} : 'Netblock listed on one or more criteria')
80 )."\n";
81 $out =~ s/:ENTITY:/$_/;
82 print $out;
83 }
84} else {
85 foreach (sort ipcmp keys %iplist) {
86 my $entry;
87 if ($iplist{$_} > 256) {
88 if ($iplist{$_} > 65536) {
89 $entry .= int($iplist{$_}/65536).".";
90 $iplist{$_} = $iplist{$_} % 65536;
91 } else {
92 $entry .= "0.";
93 }
94 $entry .= int($iplist{$_}/256).".";
95 $iplist{$_} = $iplist{$_} % 256;
96 } else {
97 $entry .= "0.0.";
98 }
99 $entry .= $iplist{$_};
100 my ($o1,$o2,$o3,$o4) = (/^(\d+)\.([\d*]+)(?:\.([\d*]+)(?:\.([\d*]+))?)?$/);
101 print "+".(defined($o4) ? "$o4." : '').(defined($o3) ? "$o3." : '').(defined($o2) ? "$o2." : '').
102 "$o1.spamhosts.company.com:127.0.0.$entry:900:::\n";
103 }
104}
105
106exit 0;
107
108# IP address comparison sub
109sub ipcmp {
110 my ($a1,$a2,$a3,$a4,$a5) = ($a =~ /^(\d+)\.([\d*]+)(?:\.([\d*]+)(?:\.([\d*]+))?)?(?:\/(\d+))?$/);
111 my ($b1,$b2,$b3,$b4,$b5) = ($b =~ /^(\d+)\.([\d*]+)(?:\.([\d*]+)(?:\.([\d*]+))?)?(?:\/(\d+))?$/);
112# le sigh. knew it wasn't going to be simple...
113 $b2 = -1 if $b2 && $b2 eq '*';
114 $b3 = -1 if $b3 && $b3 eq '*';
115 $b4 = -1 if $b4 && $b4 eq '*';
116 $b5 = 128 if !defined($b5);
117 $a2 = -1 if $a2 && $a2 eq '*';
118 $a3 = -1 if $a3 && $a3 eq '*';
119 $a4 = -1 if $a4 && $a4 eq '*';
120 $a5 = 128 if !defined($a5);
121 return 1 if $a1 > $b1;
122 return -1 if $a1 < $b1;
123 return 1 if $a2 > $b2;
124 return -1 if $a2 < $b2;
125 return 1 if $a3 > $b3;
126 return -1 if $a3 < $b3;
127 return 1 if $a4 > $b4;
128 return -1 if $a4 < $b4;
129 return 1 if $a5 > $b5;
130 return -1 if $a5 < $b5;
131 return 0;
132}
Note: See TracBrowser for help on using the repository browser.