source: trunk/dnsbl/dnsbl.cgi@ 54

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

/trunk/dnsbl

Extend the number of layers/depth from 3 to 7 internally. Note that only

5 are exposed in the "add" UI.

Add support to extract the CIDR range when a WHOIS lookup gives a non-CIDR

range.

Fix tracking of "seen" IPs creating the browse display.
Add the new DNSBLweb.pm to the Makefile MANIFEST, and bump the version in

the Makefile

  • 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 54 2014-12-11 22:22:28Z 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# here be drag'ns, should theoretically be $DNSBL::maxlvl, but we
44# only have up to level 4 in the report HTML/template
45my $maxlvl = 4
46
47# try to be friendly to non-US-ASCII characters. Still need to find what
48# difference from RH<->Debian is still at fault.
49print $q->header(-charset=>'utf8');
50
51# default DB info - all other settings should be loaded from the DB.
52my $dbhost = "localhost";
53my $dbname = "dnsbl";
54my $dbuser = "dnsbl";
55my $dbpass = "spambgone";
56
57# Load a config ref containing DB host, name, user, and pass info based on
58# from the server name + full script web path. This allows us to host
59# multiple instances without having to duplicate the code.
60# This file is a Perl fragment to be processed inline.
61my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
62$cfgname =~ s|[./-]|_|g;
63$cfgname =~ s|_dnsbl_cgi.+||;
64$cfgname =~ s|_$||;
65if (-e "/etc/dnsbl/$cfgname.conf") {
66 my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
67 ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
68 eval $cfg;
69}
70
71my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
72
73my $page;
74my $templatedir = $ENV{SCRIPT_FILENAME};
75$templatedir =~ s/dnsbl\.cgi//;
76$templatedir .= "templates";
77$ENV{HTML_TEMPLATE_ROOT} = $templatedir;
78
79my %config;
80my $sth = $dbh->prepare("SELECT key,value FROM misc");
81$sth->execute;
82while (my ($key,$value) = $sth->fetchrow_array) {
83 $config{$key} = $value;
84}
85
86# decide which page to spit out...
87if (!$webvar{page}) {
88 $page = HTML::Template->new(filename => "index.tmpl");
89} else {
90 $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
91}
92
93$page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
94$page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
95
96if ($webvar{page} eq 'report') {
97 my $dnsblsiteroot = $ENV{REQUEST_URI};
98 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
99 $page->param(dnsblsiteroot => $dnsblsiteroot);
100
101 $page->param(ip => $webvar{ip});
102 my $count = $dnsbl->ipexists($webvar{ip});
103 $page->param(nreports => $count) if $count;
104 $page->param(browsebits =>
105 DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
106 for (my $i=0; $i <= $maxlvl; $i++) {
107 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
108 if ($block) {
109 my ($bcl,$bal) = $dnsbl->islisted($block);
110 $page->param("autob$i" => $bcl);
111 $page->param("listb$i" => $bal);
112 my ($ol) = $dnsbl->islisted($org);
113 $page->param("listorg$i" => $ol);
114 $page->param("block$i" => $block);
115 $page->param("org$i" => $org);
116 }
117 }
118} elsif ($webvar{page} eq 'dbreport') {
119 my $dnsblsiteroot = $ENV{REQUEST_URI};
120 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
121 $page->param(dnsblsiteroot => $dnsblsiteroot);
122
123 my $err = '';
124
125 # basic algo: for each listing level, add the org and block if not already present.
126 # escape the loop if we check a level with no block entered.
127
128 # there are still error checks that should probably be done. changes in block
129 # level/parenting should also Just Work(TM), rather than requiring setparents.pl
130 # or setparents-full.pl
131 for (my $i = 0; $i <= $maxlvl; $i++) {
132 my $orgn = "org$i";
133 my $blockn = "block$i";
134 my $orgid = $dnsbl->orgexists($webvar{$orgn});
135 if (!$orgid) {
136 $orgid = $dnsbl->addorg($webvar{$orgn});
137 $page->param($orgn => $webvar{$orgn});
138 }
139 if ($webvar{$blockn} =~ /-/) {
140 $err .= "Autofinding CIDR block containing $webvar{ip} for range '$webvar{$blockn}': ";
141 my ($s,$f) = split /[\s-]+/, $webvar{$blockn};
142 my $cidr = $dnsbl->range2cidr($s, $f, $webvar{ip});
143 $err .= "$cidr<br>\n";
144 $webvar{$blockn} = $cidr;
145 }
146 if (!$dnsbl->blockexists($webvar{$blockn})) {
147 my $ret = $dnsbl->addblock($webvar{$blockn}, $orgid, $i);
148 $err .= "error adding $webvar{$blockn}: $ret<br>\n" if $ret;
149 $page->param($blockn => $webvar{$blockn});
150 }
151 last unless $webvar{"block".($i+1)};
152 }
153
154 my $count = $dnsbl->report($webvar{ip});
155
156 $page->param(ip => $webvar{ip});
157 $page->param(err => $err);
158
159 $page->param(browsebits =>
160 DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
161}
162
163print $page->output;
Note: See TracBrowser for help on using the repository browser.