source: trunk/dnsbl/dnsbl.cgi@ 51

Last change on this file since 51 was 51, checked in by Kris Deugau, 9 years ago

/trunk/dnsbl

Update browse.cgi and dnsbl.cgi to display extended depth entries

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 5.4 KB
Line 
1#!/usr/bin/perl
2# Main add-IP-to-list CGI
3##
4# $Id: dnsbl.cgi 51 2014-12-09 22:11:39Z kdeugau $
5# Copyright 2009-2011,2014 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;
27
28use DNSBL;
29use DNSBLweb;
30
31# Set up the CGI object...
32my $q = new CGI::Simple;
33# ... and get query-string params as well as POST params if necessary
34$q->parse_query_string;
35
36my %webvar;
37# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
38foreach ($q->param()) {
39 $webvar{$_} = $q->param($_);
40}
41
42my $dnsbl = new DNSBL;
43
44# try to be friendly to non-US-ASCII characters. Still need to find what
45# difference from RH<->Debian is still at fault.
46print $q->header(-charset=>'utf8');
47
48# default DB info - all other settings should be loaded from the DB.
49my $dbhost = "localhost";
50my $dbname = "dnsbl";
51my $dbuser = "dnsbl";
52my $dbpass = "spambgone";
53
54# Load a config ref containing DB host, name, user, and pass info based on
55# from the server name + full script web path. This allows us to host
56# multiple instances without having to duplicate the code.
57# This file is a Perl fragment to be processed inline.
58my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
59$cfgname =~ s|[./-]|_|g;
60$cfgname =~ s|_dnsbl_cgi.+||;
61$cfgname =~ s|_$||;
62if (-e "/etc/dnsbl/$cfgname.conf") {
63 my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
64 ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
65 eval $cfg;
66}
67
68my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
69
70my $page;
71my $templatedir = $ENV{SCRIPT_FILENAME};
72$templatedir =~ s/dnsbl\.cgi//;
73$templatedir .= "templates";
74$ENV{HTML_TEMPLATE_ROOT} = $templatedir;
75
76my %config;
77my $sth = $dbh->prepare("SELECT key,value FROM misc");
78$sth->execute;
79while (my ($key,$value) = $sth->fetchrow_array) {
80 $config{$key} = $value;
81}
82
83# decide which page to spit out...
84if (!$webvar{page}) {
85 $page = HTML::Template->new(filename => "index.tmpl");
86} else {
87 $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
88}
89
90$page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
91$page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
92
93if ($webvar{page} eq 'report') {
94 my $dnsblsiteroot = $ENV{REQUEST_URI};
95 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
96 $page->param(dnsblsiteroot => $dnsblsiteroot);
97
98 $page->param(ip => $webvar{ip});
99 my $count = $dnsbl->ipexists($webvar{ip});
100 $page->param(nreports => $count) if $count;
101 $page->param(browsebits =>
102 DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
103 for (my $i=0; $i<3; $i++) {
104 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
105 if ($block) {
106 my ($bcl,$bal) = $dnsbl->islisted($block);
107 $page->param("autob$i" => $bcl);
108 $page->param("listb$i" => $bal);
109 my ($ol) = $dnsbl->islisted($org);
110 $page->param("listorg$i" => $ol);
111 $page->param("block$i" => $block);
112 $page->param("org$i" => $org);
113 }
114 }
115} elsif ($webvar{page} eq 'dbreport') {
116 my $dnsblsiteroot = $ENV{REQUEST_URI};
117 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
118 $page->param(dnsblsiteroot => $dnsblsiteroot);
119
120 my $err = '';
121 my $org0id = $dnsbl->orgexists($webvar{org0});
122 if (!$org0id) {
123 $org0id = $dnsbl->addorg($webvar{org0});
124 $page->param(org0 => $webvar{org0});
125 }
126 if (!$dnsbl->blockexists($webvar{block0})) {
127 my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
128 $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
129 $page->param(block0 => $webvar{block0});
130 }
131# yes, this is grotty. PTHBTT!
132 if ($webvar{block1}) {
133 my $org1id = $dnsbl->orgexists($webvar{org1});
134 if (!$org1id) {
135 $org1id = $dnsbl->addorg($webvar{org1});
136 $page->param(org1 => $webvar{org1});
137 }
138 if (!$dnsbl->blockexists($webvar{block1})) {
139 my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
140 $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
141 $page->param(block1 => $webvar{block1});
142 }
143 if ($webvar{block2}) {
144 my $org2id = $dnsbl->orgexists($webvar{org2});
145 if (!$org2id) {
146 $org2id = $dnsbl->addorg($webvar{org2});
147 $page->param(org2 => $webvar{org2});
148 }
149 if (!$dnsbl->blockexists($webvar{block2})) {
150 my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
151 $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
152 $page->param(block2 => $webvar{block2});
153 }
154 }
155 }
156 my $count = $dnsbl->report($webvar{ip});
157
158 $page->param(ip => $webvar{ip});
159 $page->param(err => $err);
160
161 $page->param(browsebits =>
162 DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
163}
164
165print $page->output;
Note: See TracBrowser for help on using the repository browser.