[2] | 1 | #!/usr/bin/perl
|
---|
[40] | 2 | # Main add-IP-to-list CGI
|
---|
| 3 | ##
|
---|
| 4 | # $Id: dnsbl.cgi 63 2017-12-29 16:40:31Z kdeugau $
|
---|
[51] | 5 | # Copyright 2009-2011,2014 Kris Deugau <kdeugau@deepnet.cx>
|
---|
[40] | 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 | ##
|
---|
[2] | 20 |
|
---|
| 21 | use strict;
|
---|
| 22 | use warnings;
|
---|
[17] | 23 | no warnings qw(uninitialized);
|
---|
[2] | 24 | use CGI::Carp qw (fatalsToBrowser);
|
---|
| 25 | use CGI::Simple;
|
---|
| 26 | use HTML::Template;
|
---|
[45] | 27 |
|
---|
[2] | 28 | use DNSBL;
|
---|
[51] | 29 | use DNSBLweb;
|
---|
[2] | 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;
|
---|
[54] | 43 | # here be drag'ns, should theoretically be $DNSBL::maxlvl, but we
|
---|
| 44 | # only have up to level 4 in the report HTML/template
|
---|
[55] | 45 | my $maxlvl = 4;
|
---|
[2] | 46 |
|
---|
[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');
|
---|
[24] | 50 |
|
---|
[25] | 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";
|
---|
[2] | 56 |
|
---|
[25] | 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.
|
---|
[26] | 61 | my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
|
---|
[25] | 62 | $cfgname =~ s|[./-]|_|g;
|
---|
[26] | 63 | $cfgname =~ s|_dnsbl_cgi.+||;
|
---|
| 64 | $cfgname =~ s|_$||;
|
---|
[25] | 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 |
|
---|
[2] | 73 | my $page;
|
---|
[25] | 74 | my $templatedir = $ENV{SCRIPT_FILENAME};
|
---|
[63] | 75 | $templatedir =~ s/\w+\.cgi//;
|
---|
[25] | 76 | $templatedir .= "templates";
|
---|
| 77 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
|
---|
[2] | 78 |
|
---|
[25] | 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 |
|
---|
[2] | 86 | # decide which page to spit out...
|
---|
| 87 | if (!$webvar{page}) {
|
---|
[25] | 88 | $page = HTML::Template->new(filename => "index.tmpl");
|
---|
[2] | 89 | } else {
|
---|
[25] | 90 | $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
|
---|
[2] | 91 | }
|
---|
| 92 |
|
---|
[25] | 93 | $page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
|
---|
| 94 | $page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
|
---|
| 95 |
|
---|
[2] | 96 | if ($webvar{page} eq 'report') {
|
---|
[26] | 97 | my $dnsblsiteroot = $ENV{REQUEST_URI};
|
---|
| 98 | $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
|
---|
| 99 | $page->param(dnsblsiteroot => $dnsblsiteroot);
|
---|
| 100 |
|
---|
[60] | 101 | $webvar{ip} =~ s/^\s*//;
|
---|
| 102 | $webvar{ip} =~ s/\s*$//;
|
---|
[2] | 103 | $page->param(ip => $webvar{ip});
|
---|
[5] | 104 | my $count = $dnsbl->ipexists($webvar{ip});
|
---|
| 105 | $page->param(nreports => $count) if $count;
|
---|
[51] | 106 | $page->param(browsebits =>
|
---|
| 107 | DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
|
---|
[54] | 108 | for (my $i=0; $i <= $maxlvl; $i++) {
|
---|
[2] | 109 | my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
|
---|
| 110 | if ($block) {
|
---|
[29] | 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);
|
---|
[2] | 116 | $page->param("block$i" => $block);
|
---|
| 117 | $page->param("org$i" => $org);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | } elsif ($webvar{page} eq 'dbreport') {
|
---|
[26] | 121 | my $dnsblsiteroot = $ENV{REQUEST_URI};
|
---|
| 122 | $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
|
---|
| 123 | $page->param(dnsblsiteroot => $dnsblsiteroot);
|
---|
| 124 |
|
---|
[11] | 125 | my $err = '';
|
---|
[54] | 126 |
|
---|
[60] | 127 | $webvar{ip} =~ s/^\s*//;
|
---|
| 128 | $webvar{ip} =~ s/\s*$//;
|
---|
| 129 |
|
---|
[54] | 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";
|
---|
[60] | 139 | $webvar{$orgn} =~ s/^\s*//;
|
---|
| 140 | $webvar{$orgn} =~ s/\s*$//;
|
---|
| 141 | $webvar{$blockn} =~ s/^\s*//;
|
---|
| 142 | $webvar{$blockn} =~ s/\s*$//;
|
---|
[54] | 143 | my $orgid = $dnsbl->orgexists($webvar{$orgn});
|
---|
| 144 | if (!$orgid) {
|
---|
| 145 | $orgid = $dnsbl->addorg($webvar{$orgn});
|
---|
| 146 | $page->param($orgn => $webvar{$orgn});
|
---|
[2] | 147 | }
|
---|
[54] | 148 | if ($webvar{$blockn} =~ /-/) {
|
---|
[59] | 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 | }
|
---|
[2] | 158 | }
|
---|
[54] | 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});
|
---|
[2] | 163 | }
|
---|
[54] | 164 | last unless $webvar{"block".($i+1)};
|
---|
[2] | 165 | }
|
---|
[54] | 166 |
|
---|
[32] | 167 | my $count = $dnsbl->report($webvar{ip});
|
---|
[2] | 168 |
|
---|
| 169 | $page->param(ip => $webvar{ip});
|
---|
[11] | 170 | $page->param(err => $err);
|
---|
[22] | 171 |
|
---|
[51] | 172 | $page->param(browsebits =>
|
---|
| 173 | DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
|
---|
[2] | 174 | }
|
---|
[25] | 175 |
|
---|
[2] | 176 | print $page->output;
|
---|