source: trunk/dnsbl/dnsbl.cgi@ 22

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

/trunk/dnsbl

Extend browse listing in reporting to indicate containing blocks
and repeat-offender IPs, as well as show containing block on report
finalize page

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 7.1 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
24my $dbh = $dnsbl->connect;
25
26print "Content-type: text/html\n\n";
27
28my $page;
29my $templatedir = "templates";
30
31# decide which page to spit out...
32if (!$webvar{page}) {
33 $page = HTML::Template->new(filename => "$templatedir/index.tmpl");
34} else {
35 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
36}
37
38if ($webvar{page} eq 'report') {
39 $page->param(ip => $webvar{ip});
40 my $count = $dnsbl->ipexists($webvar{ip});
41 $page->param(nreports => $count) if $count;
42 $page->param(browsebits => browse($dbh,$webvar{ip}));
43 for (my $i=0; $i<3; $i++) {
44 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
45 if ($block) {
46 $page->param("block$i" => $block);
47 $page->param("org$i" => $org);
48 }
49 }
50} elsif ($webvar{page} eq 'dbreport') {
51 my $err = '';
52 my $count = $dnsbl->report($webvar{ip});
53 my $org0id = $dnsbl->orgexists($webvar{org0});
54 if (!$org0id) {
55 $org0id = $dnsbl->addorg($webvar{org0});
56 $page->param(org0 => $webvar{org0});
57 }
58 if (!$dnsbl->blockexists($webvar{block0})) {
59 my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
60 $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
61 $page->param(block0 => $webvar{block0});
62 }
63# yes, this is grotty. PTHBTT!
64 if ($webvar{block1}) {
65 my $org1id = $dnsbl->orgexists($webvar{org1});
66 if (!$org1id) {
67 $org1id = $dnsbl->addorg($webvar{org1});
68 $page->param(org1 => $webvar{org1});
69 }
70 if (!$dnsbl->blockexists($webvar{block1})) {
71 my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
72 $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
73 $page->param(block1 => $webvar{block1});
74 }
75 if ($webvar{block2}) {
76 my $org2id = $dnsbl->orgexists($webvar{org2});
77 if (!$org2id) {
78 $org2id = $dnsbl->addorg($webvar{org2});
79 $page->param(org2 => $webvar{org2});
80 }
81 if (!$dnsbl->blockexists($webvar{block2})) {
82 my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
83 $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
84 $page->param(block2 => $webvar{block2});
85 }
86 }
87 }
88
89 $page->param(ip => $webvar{ip});
90 $page->param(err => $err);
91
92 $page->param(browsebits => browse($dbh,$webvar{ip}));
93}
94print $page->output;
95
96exit 0;
97
98
99
100## extra subs. should probably put this in a module somehow to share with browse.cgi
101
102sub browse {
103 my $dbh = shift;
104 my $ip = shift;
105 my $ipcidr = new NetAddr::IP $ip;
106
107 my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
108 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
109 "WHERE b.block ";
110
111 my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
112 my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
113 my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
114 my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
115
116 my %ipseen;
117 my $out = '';
118
119 my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
120
121 $sth0->execute($ip);
122 while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
123 my $block0cidr = new NetAddr::IP $block0;
124 $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : '').
125 ( $ipcidr->within($block0cidr) ? ' inhere' : ''));
126 $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
127 $tmpl0->param(net => $block0);
128 $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
129 $tmpl0->param(org => $org0);
130 $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
131 $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
132 $sth1->execute($block0);
133 my $lvl1out = '';
134 if ($sth1->rows > 0) {
135 while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
136 my $block1cidr = new NetAddr::IP $block1;
137 my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
138 $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : '').
139 ( $ipcidr->within($block1cidr) ? ' inhere' : ''));
140 $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
141 $tmpl1->param(net => $block1);
142 $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
143 $tmpl1->param(org => $org1);
144 $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
145 $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
146 $tmpl1->param(indent => ' ');
147 my $lvl2out = '';
148 $sth2->execute($block1);
149 if ($sth2->rows > 0) {
150 while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
151 my $block2cidr = new NetAddr::IP $block2;
152 my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
153 $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : '').
154 ( $ipcidr->within($block2cidr) ? ' inhere' : ''));
155 $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
156 $tmpl2->param(net => $block2);
157 $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
158 $tmpl2->param(org => $org2);
159 $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
160 $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
161 $tmpl2->param(indent => ' ');
162 $sthiplist->execute($block2);
163 my @iprows;
164 while (my @data4 = $sthiplist->fetchrow_array) {
165 my %iprow;
166 $iprow{ip} = $data4[0];
167 $iprow{ipcount} = $data4[1];
168 $iprow{indent} = ' ';
169 $iprow{repeater} = 1 if $ip eq $data4[0];
170# ip | count | s4list | added
171 push @iprows, \%iprow;
172 $ipseen{$data4[0]} = 1;
173 }
174 $tmpl2->param(iplist => \@iprows);
175 $lvl2out .= $tmpl2->output;
176 }
177 }
178
179 $sthiplist->execute($block1);
180 my @iprows;
181 while (my @data4 = $sthiplist->fetchrow_array) {
182 next if $ipseen{$data4[0]};
183 my %iprow;
184 $iprow{ip} = $data4[0];
185 $iprow{ipcount} = $data4[1];
186 $iprow{indent} = ' ';
187 $iprow{repeater} = 1 if $ip eq $data4[0];
188# ip | count | s4list | added
189 push @iprows, \%iprow;
190 $ipseen{$data4[0]} = 1;
191 }
192 $tmpl1->param(iplist => \@iprows);
193 $tmpl1->param(subs => $lvl2out);
194 $lvl1out .= $tmpl1->output;
195
196 }
197 } # sth1->rows
198 $sthiplist->execute($block0);
199 my @iprows;
200 while (my @data4 = $sthiplist->fetchrow_array) {
201 next if $ipseen{$data4[0]};
202 my %iprow;
203 $iprow{ip} = $data4[0];
204 $iprow{ipcount} = $data4[1];
205 $iprow{indent} = '';
206 $iprow{repeater} = 1 if $ip eq $data4[0];
207# ip | count | s4list | added
208 push @iprows, \%iprow;
209 $ipseen{$data4[0]} = 1;
210 }
211 $tmpl0->param(iplist => \@iprows);
212 $tmpl0->param(subs => $lvl1out);
213 }
214
215 return $tmpl0->output;
216} # end browse()
Note: See TracBrowser for help on using the repository browser.