source: trunk/dnsbl/dnsbl.cgi@ 11

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

/trunk/dnsbl

Report errors adding a netblock (commonly a range in WHOIS that's not

a single CIDR block)

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 6.3 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use CGI::Carp qw (fatalsToBrowser);
6use CGI::Simple;
7use HTML::Template;
8use DNSBL;
9
10# Set up the CGI object...
11my $q = new CGI::Simple;
12# ... and get query-string params as well as POST params if necessary
13$q->parse_query_string;
14
15my %webvar;
16# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
17foreach ($q->param()) {
18 $webvar{$_} = $q->param($_);
19}
20
21my $dnsbl = new DNSBL;
22
23my $dbh = $dnsbl->connect;
24
25print "Content-type: text/html\n\n";
26
27my $page;
28my $templatedir = "templates";
29
30# decide which page to spit out...
31if (!$webvar{page}) {
32 $page = HTML::Template->new(filename => "$templatedir/index.tmpl");
33} else {
34 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
35}
36
37if ($webvar{page} eq 'report') {
38 $page->param(ip => $webvar{ip});
39 my $count = $dnsbl->ipexists($webvar{ip});
40 $page->param(nreports => $count) if $count;
41 $page->param(browsebits => browse($dbh,$webvar{ip}));
42 for (my $i=0; $i<3; $i++) {
43 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
44 if ($block) {
45 $page->param("block$i" => $block);
46 $page->param("org$i" => $org);
47 }
48 }
49} elsif ($webvar{page} eq 'dbreport') {
50 my $err = '';
51 my $count = $dnsbl->report($webvar{ip});
52 my $org0id = $dnsbl->orgexists($webvar{org0});
53 if (!$org0id) {
54 $org0id = $dnsbl->addorg($webvar{org0});
55 $page->param(org0 => $webvar{org0});
56 }
57 if (!$dnsbl->blockexists($webvar{block0})) {
58 my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
59 $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
60 $page->param(block0 => $webvar{block0});
61 }
62# yes, this is grotty. PTHBTT!
63 if ($webvar{block1}) {
64 my $org1id = $dnsbl->orgexists($webvar{org1});
65 if (!$org1id) {
66 $org1id = $dnsbl->addorg($webvar{org1});
67 $page->param(org1 => $webvar{org1});
68 }
69 if (!$dnsbl->blockexists($webvar{block1})) {
70 my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
71 $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
72 $page->param(block1 => $webvar{block1});
73 }
74 if ($webvar{block2}) {
75 my $org2id = $dnsbl->orgexists($webvar{org2});
76 if (!$org2id) {
77 $org2id = $dnsbl->addorg($webvar{org2});
78 $page->param(org2 => $webvar{org2});
79 }
80 if (!$dnsbl->blockexists($webvar{block2})) {
81 my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
82 $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
83 $page->param(block2 => $webvar{block2});
84 }
85 }
86 }
87
88 $page->param(ip => $webvar{ip});
89 $page->param(err => $err);
90}
91print $page->output;
92
93exit 0;
94
95
96
97## extra subs. should probably put this in a module somehow to share with browse.cgi
98
99sub browse {
100 my $dbh = shift;
101 my $ip = shift;
102
103 my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
104 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
105 "WHERE b.block ";
106
107 my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
108 my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
109 my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
110 my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
111
112 my %ipseen;
113 my $out = '';
114
115 $sth0->execute($ip);
116 while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
117 $out .= "<div class=\"lvl0".($dnsbl->autolist_block($block0) ? ' auto0"' : '"').
118 "><span".($listmeb0 ? ' class=b0list' : '').">$block0".
119 ($listmeb0 ? " ($bcomments0)" : '')."</span> ".
120 "<span".($listmeo0 ? ' class=b0org' : '').">$org0".
121 ($listmeo0 ? " ($ocomments0)" : '')."</span>\n";
122 $sth1->execute($block0);
123 if ($sth1->rows > 0) {
124 while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
125# lvl 1 div open
126 $out .= " <div class=\"lvl1".($dnsbl->autolist_block($block1) ? ' auto1"' : '"').
127 "><span".($listmeb1 ? ' class=b1list' : '').">$block1".
128 ($listmeb1 ? " ($bcomments1)" : '')."</span> ".
129 "<span".($listmeo1 ? ' class=b1org' : '').">$org1".
130 ($listmeo1 ? " ($ocomments1)" : '')."</span>\n";
131 $sth2->execute($block1);
132 if ($sth2->rows > 0) {
133 while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
134# lvl 2 div open
135 $out .= " <div class=\"lvl2".($dnsbl->autolist_block($block2) ? ' auto2"' : '"').
136 "><span".($listmeb2 ? ' class=b2list' : '').">$block2".
137 ($listmeb2 ? " ($bcomments2)" : '')."</span> ".
138 "<span".($listmeo2 ? ' class=b2org' : '').">$org2".
139 ($listmeo2 ? " ($ocomments2)" : '')."</span>\n";
140 $sthiplist->execute($block2);
141 $out .= " <div class=iplist>\n";
142 while (my @data4 = $sthiplist->fetchrow_array) {
143 $out .= " $data4[0]<br>\n";
144 $ipseen{$data4[0]} = 1;
145 }
146 $out .= " </div>\n";
147# lvl2 div close
148 $out .= " </div>\n";
149 }
150 } else {
151 $sthiplist->execute($block1);
152 $out .= " <div class=iplist>\n";
153 while (my @data4 = $sthiplist->fetchrow_array) {
154 $out .= " $data4[0]<br>\n";
155 $ipseen{$data4[0]} = 1;
156 }
157 $out .= " </div>\n";
158 }
159
160 my $sqlalt = "select ip from iplist where ip << ?";
161 my $sthalt = $dbh->prepare($sqlalt);
162 $sthalt->execute($block1);
163 my @newips;
164 while (my @data4 = $sthalt->fetchrow_array) {
165 push @newips, $data4[0] if !$ipseen{$data4[0]};
166 $ipseen{$data4[0]} = 1;
167 }
168 if ($#newips > -1) {
169 $out .= " <div class=iplist>\n";
170 foreach (@newips) {
171 $out .= " $_<br>\n";
172 }
173 $out .= " </div>\n";
174 }
175# lvl 1 div close
176 $out .= " </div>\n";
177 }
178 } else {
179 $sthiplist->execute($block0);
180 $out .= " <div class=iplist>\n";
181 while (my @data4 = $sthiplist->fetchrow_array) {
182 $out .= " $data4[0]<br>\n";
183 $ipseen{$data4[0]} = 1;
184 }
185 $out .= " </div>\n";
186 }
187
188 my $sqlalt = "select ip from iplist where ip << ?";
189 my $sthalt = $dbh->prepare($sqlalt);
190 $sthalt->execute($block0);
191 my @newips;
192 while (my @data4 = $sthalt->fetchrow_array) {
193 push @newips, $data4[0] if (!$ipseen{$data4[0]});
194 }
195 if ($#newips > -1) {
196 $out .= " <div class=iplist>\n";
197 foreach (@newips) {
198 $out .= " $_<br>\n";
199 }
200 $out .= " </div>\n";
201 }
202
203 $out .= "</div>\n";
204 }
205
206 return $out;
207} # end browse()
Note: See TracBrowser for help on using the repository browser.