source: trunk/dnsbl/dnsbl.cgi@ 25

Last change on this file since 25 was 25, checked in by Kris Deugau, 14 years ago

/trunk/dnsbl

Changes across the board to support multi-instance without code changes.
DB config is now loaded from a fragment in /etc/dnsbl, and the DB in turn
contains the autolist thresholds and some visual sugar for the web
interface so you know which DB you're dealing with.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 8.2 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5no warnings qw(uninitialized);
6use CGI::Carp qw (fatalsToBrowser);
7use CGI::Simple;
8use HTML::Template;
9use DNSBL;
10
11# Set up the CGI object...
12my $q = new CGI::Simple;
13# ... and get query-string params as well as POST params if necessary
14$q->parse_query_string;
15
16my %webvar;
17# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
18foreach ($q->param()) {
19 $webvar{$_} = $q->param($_);
20}
21
22my $dnsbl = new DNSBL;
23
24print "Content-type: text/html\n\n";
25
26# default DB info - all other settings should be loaded from the DB.
27my $dbhost = "localhost";
28my $dbname = "dnsbl";
29my $dbuser = "dnsbl";
30my $dbpass = "spambgone";
31
32# Load a config ref containing DB host, name, user, and pass info based on
33# from the server name + full script web path. This allows us to host
34# multiple instances without having to duplicate the code.
35# This file is a Perl fragment to be processed inline.
36my $cfgname = $ENV{SERVER_NAME}.$ENV{SCRIPT_NAME};
37$cfgname =~ s|[./-]|_|g;
38$cfgname =~ s|_dnsbl_cgi||;
39if (-e "/etc/dnsbl/$cfgname.conf") {
40 my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
41 ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
42 eval $cfg;
43}
44
45my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
46
47my $page;
48my $templatedir = $ENV{SCRIPT_FILENAME};
49$templatedir =~ s/dnsbl\.cgi//;
50$templatedir .= "templates";
51$ENV{HTML_TEMPLATE_ROOT} = $templatedir;
52
53my %config;
54my $sth = $dbh->prepare("SELECT key,value FROM misc");
55$sth->execute;
56while (my ($key,$value) = $sth->fetchrow_array) {
57 $config{$key} = $value;
58}
59
60# decide which page to spit out...
61if (!$webvar{page}) {
62 $page = HTML::Template->new(filename => "index.tmpl");
63} else {
64 $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
65}
66
67$page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
68$page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
69
70if ($webvar{page} eq 'report') {
71 $page->param(ip => $webvar{ip});
72 my $count = $dnsbl->ipexists($webvar{ip});
73 $page->param(nreports => $count) if $count;
74 $page->param(browsebits => browse($dbh,$webvar{ip}));
75 for (my $i=0; $i<3; $i++) {
76 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
77 if ($block) {
78 $page->param("block$i" => $block);
79 $page->param("org$i" => $org);
80 }
81 }
82} elsif ($webvar{page} eq 'dbreport') {
83 my $err = '';
84 my $count = $dnsbl->report($webvar{ip});
85 my $org0id = $dnsbl->orgexists($webvar{org0});
86 if (!$org0id) {
87 $org0id = $dnsbl->addorg($webvar{org0});
88 $page->param(org0 => $webvar{org0});
89 }
90 if (!$dnsbl->blockexists($webvar{block0})) {
91 my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
92 $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
93 $page->param(block0 => $webvar{block0});
94 }
95# yes, this is grotty. PTHBTT!
96 if ($webvar{block1}) {
97 my $org1id = $dnsbl->orgexists($webvar{org1});
98 if (!$org1id) {
99 $org1id = $dnsbl->addorg($webvar{org1});
100 $page->param(org1 => $webvar{org1});
101 }
102 if (!$dnsbl->blockexists($webvar{block1})) {
103 my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
104 $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
105 $page->param(block1 => $webvar{block1});
106 }
107 if ($webvar{block2}) {
108 my $org2id = $dnsbl->orgexists($webvar{org2});
109 if (!$org2id) {
110 $org2id = $dnsbl->addorg($webvar{org2});
111 $page->param(org2 => $webvar{org2});
112 }
113 if (!$dnsbl->blockexists($webvar{block2})) {
114 my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
115 $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
116 $page->param(block2 => $webvar{block2});
117 }
118 }
119 }
120
121 $page->param(ip => $webvar{ip});
122 $page->param(err => $err);
123
124 $page->param(browsebits => browse($dbh,$webvar{ip}));
125}
126
127print $page->output;
128
129exit 0;
130
131
132
133## extra subs. should probably put this in a module somehow to share with browse.cgi
134
135sub browse {
136 my $dbh = shift;
137 my $ip = shift;
138 my $ipcidr = new NetAddr::IP $ip;
139
140 my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
141 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
142 "WHERE b.block ";
143
144 my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
145 my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
146 my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
147 my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
148
149 my %ipseen;
150 my $out = '';
151
152 my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
153
154 $sth0->execute($ip);
155 while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
156 my $block0cidr = new NetAddr::IP $block0;
157 $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : '').
158 ( $ipcidr->within($block0cidr) ? ' inhere' : ''));
159 $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
160 $tmpl0->param(net => $block0);
161 $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
162 $tmpl0->param(org => $org0);
163 $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
164 $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
165 $sth1->execute($block0);
166 my $lvl1out = '';
167 if ($sth1->rows > 0) {
168 while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
169 my $block1cidr = new NetAddr::IP $block1;
170 my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
171 $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : '').
172 ( $ipcidr->within($block1cidr) ? ' inhere' : ''));
173 $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
174 $tmpl1->param(net => $block1);
175 $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
176 $tmpl1->param(org => $org1);
177 $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
178 $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
179 $tmpl1->param(indent => ' ');
180 my $lvl2out = '';
181 $sth2->execute($block1);
182 if ($sth2->rows > 0) {
183 while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
184 my $block2cidr = new NetAddr::IP $block2;
185 my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
186 $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : '').
187 ( $ipcidr->within($block2cidr) ? ' inhere' : ''));
188 $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
189 $tmpl2->param(net => $block2);
190 $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
191 $tmpl2->param(org => $org2);
192 $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
193 $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
194 $tmpl2->param(indent => ' ');
195 $sthiplist->execute($block2);
196 my @iprows;
197 while (my @data4 = $sthiplist->fetchrow_array) {
198 my %iprow;
199 $iprow{ip} = $data4[0];
200 $iprow{ipcount} = $data4[1];
201 $iprow{indent} = ' ';
202 $iprow{repeater} = 1 if $ip eq $data4[0];
203# ip | count | s4list | added
204 push @iprows, \%iprow;
205 $ipseen{$data4[0]} = 1;
206 }
207 $tmpl2->param(iplist => \@iprows);
208 $lvl2out .= $tmpl2->output;
209 }
210 }
211
212 $sthiplist->execute($block1);
213 my @iprows;
214 while (my @data4 = $sthiplist->fetchrow_array) {
215 next if $ipseen{$data4[0]};
216 my %iprow;
217 $iprow{ip} = $data4[0];
218 $iprow{ipcount} = $data4[1];
219 $iprow{indent} = ' ';
220 $iprow{repeater} = 1 if $ip eq $data4[0];
221# ip | count | s4list | added
222 push @iprows, \%iprow;
223 $ipseen{$data4[0]} = 1;
224 }
225 $tmpl1->param(iplist => \@iprows);
226 $tmpl1->param(subs => $lvl2out);
227 $lvl1out .= $tmpl1->output;
228
229 }
230 } # sth1->rows
231 $sthiplist->execute($block0);
232 my @iprows;
233 while (my @data4 = $sthiplist->fetchrow_array) {
234 next if $ipseen{$data4[0]};
235 my %iprow;
236 $iprow{ip} = $data4[0];
237 $iprow{ipcount} = $data4[1];
238 $iprow{indent} = '';
239 $iprow{repeater} = 1 if $ip eq $data4[0];
240# ip | count | s4list | added
241 push @iprows, \%iprow;
242 $ipseen{$data4[0]} = 1;
243 }
244 $tmpl0->param(iplist => \@iprows);
245 $tmpl0->param(subs => $lvl1out);
246 }
247
248 return $tmpl0->output;
249} # end browse()
Note: See TracBrowser for help on using the repository browser.