source: trunk/dnsbl/dnsbl.cgi@ 40

Last change on this file since 40 was 40, checked in by Kris Deugau, 12 years ago

/trunk/dnsbl

Minor cleanups to prepare for semirelease
GPL-tag executables and Perl module from Makefile MANIFEST

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