source: trunk/dnsbl/dnsbl.cgi@ 29

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

/trunk/dnsbl

Add islisted()
Add waslisted table in SQL def
Indicate block status on adding an IP to an existing block
Fix for scripts-not-at-webroot
Tweak for scrolling "blocks in this registrar block" list
Load more config from DB on export

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 8.7 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{REQUEST_URI};
37$cfgname =~ s|[./-]|_|g;
38$cfgname =~ s|_dnsbl_cgi.+||;
39$cfgname =~ s|_$||;
40if (-e "/etc/dnsbl/$cfgname.conf") {
41 my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
42 ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
43 eval $cfg;
44}
45
46my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
47
48my $page;
49my $templatedir = $ENV{SCRIPT_FILENAME};
50$templatedir =~ s/dnsbl\.cgi//;
51$templatedir .= "templates";
52$ENV{HTML_TEMPLATE_ROOT} = $templatedir;
53
54my %config;
55my $sth = $dbh->prepare("SELECT key,value FROM misc");
56$sth->execute;
57while (my ($key,$value) = $sth->fetchrow_array) {
58 $config{$key} = $value;
59}
60
61# decide which page to spit out...
62if (!$webvar{page}) {
63 $page = HTML::Template->new(filename => "index.tmpl");
64} else {
65 $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
66}
67
68$page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
69$page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
70
71if ($webvar{page} eq 'report') {
72 my $dnsblsiteroot = $ENV{REQUEST_URI};
73 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
74 $page->param(dnsblsiteroot => $dnsblsiteroot);
75
76 $page->param(ip => $webvar{ip});
77 my $count = $dnsbl->ipexists($webvar{ip});
78 $page->param(nreports => $count) if $count;
79 $page->param(browsebits => browse($dbh,$webvar{ip}));
80 for (my $i=0; $i<3; $i++) {
81 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
82 if ($block) {
83 my ($bcl,$bal) = $dnsbl->islisted($block);
84 $page->param("autob$i" => $bcl);
85 $page->param("listb$i" => $bal);
86 my ($ol) = $dnsbl->islisted($org);
87 $page->param("listorg$i" => $ol);
88 $page->param("block$i" => $block);
89 $page->param("org$i" => $org);
90 }
91 }
92} elsif ($webvar{page} eq 'dbreport') {
93 my $dnsblsiteroot = $ENV{REQUEST_URI};
94 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
95 $page->param(dnsblsiteroot => $dnsblsiteroot);
96
97 my $err = '';
98 my $count = $dnsbl->report($webvar{ip});
99 my $org0id = $dnsbl->orgexists($webvar{org0});
100 if (!$org0id) {
101 $org0id = $dnsbl->addorg($webvar{org0});
102 $page->param(org0 => $webvar{org0});
103 }
104 if (!$dnsbl->blockexists($webvar{block0})) {
105 my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
106 $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
107 $page->param(block0 => $webvar{block0});
108 }
109# yes, this is grotty. PTHBTT!
110 if ($webvar{block1}) {
111 my $org1id = $dnsbl->orgexists($webvar{org1});
112 if (!$org1id) {
113 $org1id = $dnsbl->addorg($webvar{org1});
114 $page->param(org1 => $webvar{org1});
115 }
116 if (!$dnsbl->blockexists($webvar{block1})) {
117 my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
118 $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
119 $page->param(block1 => $webvar{block1});
120 }
121 if ($webvar{block2}) {
122 my $org2id = $dnsbl->orgexists($webvar{org2});
123 if (!$org2id) {
124 $org2id = $dnsbl->addorg($webvar{org2});
125 $page->param(org2 => $webvar{org2});
126 }
127 if (!$dnsbl->blockexists($webvar{block2})) {
128 my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
129 $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
130 $page->param(block2 => $webvar{block2});
131 }
132 }
133 }
134
135 $page->param(ip => $webvar{ip});
136 $page->param(err => $err);
137
138 $page->param(browsebits => browse($dbh,$webvar{ip}));
139}
140
141print $page->output;
142
143exit 0;
144
145
146
147## extra subs. should probably put this in a module somehow to share with browse.cgi
148
149sub browse {
150 my $dbh = shift;
151 my $ip = shift;
152 my $ipcidr = new NetAddr::IP $ip;
153
154 my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
155 "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
156 "WHERE b.block ";
157
158 my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
159 my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
160 my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
161 my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
162
163 my %ipseen;
164 my $out = '';
165
166 my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
167
168 $sth0->execute($ip);
169 while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
170 my $block0cidr = new NetAddr::IP $block0;
171 $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : '').
172 ( $ipcidr->within($block0cidr) ? ' inhere' : ''));
173 $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
174 $tmpl0->param(net => $block0);
175 $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
176 $tmpl0->param(org => $org0);
177 $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
178 $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
179 $sth1->execute($block0);
180 my $lvl1out = '';
181 if ($sth1->rows > 0) {
182 while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
183 my $block1cidr = new NetAddr::IP $block1;
184 my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
185 $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : '').
186 ( $ipcidr->within($block1cidr) ? ' inhere' : ''));
187 $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
188 $tmpl1->param(net => $block1);
189 $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
190 $tmpl1->param(org => $org1);
191 $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
192 $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
193 $tmpl1->param(indent => ' ');
194 my $lvl2out = '';
195 $sth2->execute($block1);
196 if ($sth2->rows > 0) {
197 while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
198 my $block2cidr = new NetAddr::IP $block2;
199 my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
200 $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : '').
201 ( $ipcidr->within($block2cidr) ? ' inhere' : ''));
202 $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
203 $tmpl2->param(net => $block2);
204 $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
205 $tmpl2->param(org => $org2);
206 $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
207 $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
208 $tmpl2->param(indent => ' ');
209 $sthiplist->execute($block2);
210 my @iprows;
211 while (my @data4 = $sthiplist->fetchrow_array) {
212 my %iprow;
213 $iprow{ip} = $data4[0];
214 $iprow{ipcount} = $data4[1];
215 $iprow{indent} = ' ';
216 $iprow{repeater} = 1 if $ip eq $data4[0];
217# ip | count | s4list | added
218 push @iprows, \%iprow;
219 $ipseen{$data4[0]} = 1;
220 }
221 $tmpl2->param(iplist => \@iprows);
222 $lvl2out .= $tmpl2->output;
223 }
224 }
225
226 $sthiplist->execute($block1);
227 my @iprows;
228 while (my @data4 = $sthiplist->fetchrow_array) {
229 next if $ipseen{$data4[0]};
230 my %iprow;
231 $iprow{ip} = $data4[0];
232 $iprow{ipcount} = $data4[1];
233 $iprow{indent} = ' ';
234 $iprow{repeater} = 1 if $ip eq $data4[0];
235# ip | count | s4list | added
236 push @iprows, \%iprow;
237 $ipseen{$data4[0]} = 1;
238 }
239 $tmpl1->param(iplist => \@iprows);
240 $tmpl1->param(subs => $lvl2out);
241 $lvl1out .= $tmpl1->output;
242
243 }
244 } # sth1->rows
245 $sthiplist->execute($block0);
246 my @iprows;
247 while (my @data4 = $sthiplist->fetchrow_array) {
248 next if $ipseen{$data4[0]};
249 my %iprow;
250 $iprow{ip} = $data4[0];
251 $iprow{ipcount} = $data4[1];
252 $iprow{indent} = '';
253 $iprow{repeater} = 1 if $ip eq $data4[0];
254# ip | count | s4list | added
255 push @iprows, \%iprow;
256 $ipseen{$data4[0]} = 1;
257 }
258 $tmpl0->param(iplist => \@iprows);
259 $tmpl0->param(subs => $lvl1out);
260 }
261
262 return $tmpl0->output;
263} # end browse()
Note: See TracBrowser for help on using the repository browser.