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 |
|
---|
21 | use strict;
|
---|
22 | use warnings;
|
---|
23 | no warnings qw(uninitialized);
|
---|
24 | use CGI::Carp qw (fatalsToBrowser);
|
---|
25 | use CGI::Simple;
|
---|
26 | use HTML::Template;
|
---|
27 |
|
---|
28 | use DNSBL;
|
---|
29 | use DNSBLweb;
|
---|
30 |
|
---|
31 | # Set up the CGI object...
|
---|
32 | my $q = new CGI::Simple;
|
---|
33 | # ... and get query-string params as well as POST params if necessary
|
---|
34 | $q->parse_query_string;
|
---|
35 |
|
---|
36 | my %webvar;
|
---|
37 | # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
|
---|
38 | foreach ($q->param()) {
|
---|
39 | $webvar{$_} = $q->param($_);
|
---|
40 | }
|
---|
41 |
|
---|
42 | my $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
|
---|
45 | my $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.
|
---|
49 | print $q->header(-charset=>'utf8');
|
---|
50 |
|
---|
51 | # default DB info - all other settings should be loaded from the DB.
|
---|
52 | my $dbhost = "localhost";
|
---|
53 | my $dbname = "dnsbl";
|
---|
54 | my $dbuser = "dnsbl";
|
---|
55 | my $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.
|
---|
61 | my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
|
---|
62 | $cfgname =~ s|[./-]|_|g;
|
---|
63 | $cfgname =~ s|_dnsbl_cgi.+||;
|
---|
64 | $cfgname =~ s|_$||;
|
---|
65 | if (-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 |
|
---|
71 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
|
---|
72 |
|
---|
73 | my $page;
|
---|
74 | my $templatedir = $ENV{SCRIPT_FILENAME};
|
---|
75 | $templatedir =~ s/dnsbl\.cgi//;
|
---|
76 | $templatedir .= "templates";
|
---|
77 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
|
---|
78 |
|
---|
79 | my %config;
|
---|
80 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
|
---|
81 | $sth->execute;
|
---|
82 | while (my ($key,$value) = $sth->fetchrow_array) {
|
---|
83 | $config{$key} = $value;
|
---|
84 | }
|
---|
85 |
|
---|
86 | # decide which page to spit out...
|
---|
87 | if (!$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 |
|
---|
96 | if ($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 |
|
---|
176 | print $page->output;
|
---|