source: trunk/dnsbl/dnsbl.cgi@ 60

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

/trunk/dnsbl

Strip leading and trailing whitespace on IP, netblock, and org fields

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 5.8 KB
Line 
1#!/usr/bin/perl
2# Main add-IP-to-list CGI
3##
4# $Id: dnsbl.cgi 60 2015-03-10 16:26:24Z 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 $webvar{ip} =~ s/^\s*//;
102 $webvar{ip} =~ s/\s*$//;
103 $page->param(ip => $webvar{ip});
104 my $count = $dnsbl->ipexists($webvar{ip});
105 $page->param(nreports => $count) if $count;
106 $page->param(browsebits =>
107 DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
108 for (my $i=0; $i <= $maxlvl; $i++) {
109 my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
110 if ($block) {
111 my ($bcl,$bal) = $dnsbl->islisted($block);
112 $page->param("autob$i" => $bcl);
113 $page->param("listb$i" => $bal);
114 my ($ol) = $dnsbl->islisted($org);
115 $page->param("listorg$i" => $ol);
116 $page->param("block$i" => $block);
117 $page->param("org$i" => $org);
118 }
119 }
120} elsif ($webvar{page} eq 'dbreport') {
121 my $dnsblsiteroot = $ENV{REQUEST_URI};
122 $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
123 $page->param(dnsblsiteroot => $dnsblsiteroot);
124
125 my $err = '';
126
127 $webvar{ip} =~ s/^\s*//;
128 $webvar{ip} =~ s/\s*$//;
129
130 # basic algo: for each listing level, add the org and block if not already present.
131 # escape the loop if we check a level with no block entered.
132
133 # there are still error checks that should probably be done. changes in block
134 # level/parenting should also Just Work(TM), rather than requiring setparents.pl
135 # or setparents-full.pl
136 for (my $i = 0; $i <= $maxlvl; $i++) {
137 my $orgn = "org$i";
138 my $blockn = "block$i";
139 $webvar{$orgn} =~ s/^\s*//;
140 $webvar{$orgn} =~ s/\s*$//;
141 $webvar{$blockn} =~ s/^\s*//;
142 $webvar{$blockn} =~ s/\s*$//;
143 my $orgid = $dnsbl->orgexists($webvar{$orgn});
144 if (!$orgid) {
145 $orgid = $dnsbl->addorg($webvar{$orgn});
146 $page->param($orgn => $webvar{$orgn});
147 }
148 if ($webvar{$blockn} =~ /-/) {
149 my $tmp = new NetAddr::IP $webvar{$blockn};
150 if (!$tmp) {
151 # Don't need to autofind ranges that are already CIDR-matched
152 $err .= "Autofinding CIDR block containing $webvar{ip} for range '$webvar{$blockn}': ";
153 my ($s,$f) = split /[\s-]+/, $webvar{$blockn};
154 my $cidr = $dnsbl->range2cidr($s, $f, $webvar{ip});
155 $err .= "$cidr<br>\n";
156 $webvar{$blockn} = $cidr;
157 }
158 }
159 if (!$dnsbl->blockexists($webvar{$blockn})) {
160 my $ret = $dnsbl->addblock($webvar{$blockn}, $orgid, $i);
161 $err .= "error adding $webvar{$blockn}: $ret<br>\n" if $ret;
162 $page->param($blockn => $webvar{$blockn});
163 }
164 last unless $webvar{"block".($i+1)};
165 }
166
167 my $count = $dnsbl->report($webvar{ip});
168
169 $page->param(ip => $webvar{ip});
170 $page->param(err => $err);
171
172 $page->param(browsebits =>
173 DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
174}
175
176print $page->output;
Note: See TracBrowser for help on using the repository browser.