[2] | 1 | #!/usr/bin/perl
|
---|
[40] | 2 | # Main add-IP-to-list CGI
|
---|
| 3 | ##
|
---|
| 4 | # $Id: dnsbl.cgi 51 2014-12-09 22:11:39Z 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;
|
---|
| 43 |
|
---|
[46] | 44 | # try to be friendly to non-US-ASCII characters. Still need to find what
|
---|
| 45 | # difference from RH<->Debian is still at fault.
|
---|
| 46 | print $q->header(-charset=>'utf8');
|
---|
[24] | 47 |
|
---|
[25] | 48 | # default DB info - all other settings should be loaded from the DB.
|
---|
| 49 | my $dbhost = "localhost";
|
---|
| 50 | my $dbname = "dnsbl";
|
---|
| 51 | my $dbuser = "dnsbl";
|
---|
| 52 | my $dbpass = "spambgone";
|
---|
[2] | 53 |
|
---|
[25] | 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.
|
---|
[26] | 58 | my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
|
---|
[25] | 59 | $cfgname =~ s|[./-]|_|g;
|
---|
[26] | 60 | $cfgname =~ s|_dnsbl_cgi.+||;
|
---|
| 61 | $cfgname =~ s|_$||;
|
---|
[25] | 62 | if (-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 |
|
---|
| 68 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
|
---|
| 69 |
|
---|
[2] | 70 | my $page;
|
---|
[25] | 71 | my $templatedir = $ENV{SCRIPT_FILENAME};
|
---|
| 72 | $templatedir =~ s/dnsbl\.cgi//;
|
---|
| 73 | $templatedir .= "templates";
|
---|
| 74 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
|
---|
[2] | 75 |
|
---|
[25] | 76 | my %config;
|
---|
| 77 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
|
---|
| 78 | $sth->execute;
|
---|
| 79 | while (my ($key,$value) = $sth->fetchrow_array) {
|
---|
| 80 | $config{$key} = $value;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[2] | 83 | # decide which page to spit out...
|
---|
| 84 | if (!$webvar{page}) {
|
---|
[25] | 85 | $page = HTML::Template->new(filename => "index.tmpl");
|
---|
[2] | 86 | } else {
|
---|
[25] | 87 | $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
|
---|
[2] | 88 | }
|
---|
| 89 |
|
---|
[25] | 90 | $page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
|
---|
| 91 | $page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
|
---|
| 92 |
|
---|
[2] | 93 | if ($webvar{page} eq 'report') {
|
---|
[26] | 94 | my $dnsblsiteroot = $ENV{REQUEST_URI};
|
---|
| 95 | $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
|
---|
| 96 | $page->param(dnsblsiteroot => $dnsblsiteroot);
|
---|
| 97 |
|
---|
[2] | 98 | $page->param(ip => $webvar{ip});
|
---|
[5] | 99 | my $count = $dnsbl->ipexists($webvar{ip});
|
---|
| 100 | $page->param(nreports => $count) if $count;
|
---|
[51] | 101 | $page->param(browsebits =>
|
---|
| 102 | DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
|
---|
[2] | 103 | for (my $i=0; $i<3; $i++) {
|
---|
| 104 | my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
|
---|
| 105 | if ($block) {
|
---|
[29] | 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);
|
---|
[2] | 111 | $page->param("block$i" => $block);
|
---|
| 112 | $page->param("org$i" => $org);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | } elsif ($webvar{page} eq 'dbreport') {
|
---|
[26] | 116 | my $dnsblsiteroot = $ENV{REQUEST_URI};
|
---|
| 117 | $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
|
---|
| 118 | $page->param(dnsblsiteroot => $dnsblsiteroot);
|
---|
| 119 |
|
---|
[11] | 120 | my $err = '';
|
---|
[2] | 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})) {
|
---|
[11] | 127 | my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
|
---|
| 128 | $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
|
---|
[2] | 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})) {
|
---|
[11] | 139 | my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
|
---|
| 140 | $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
|
---|
[2] | 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})) {
|
---|
[11] | 150 | my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
|
---|
| 151 | $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
|
---|
[2] | 152 | $page->param(block2 => $webvar{block2});
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[32] | 156 | my $count = $dnsbl->report($webvar{ip});
|
---|
[2] | 157 |
|
---|
| 158 | $page->param(ip => $webvar{ip});
|
---|
[11] | 159 | $page->param(err => $err);
|
---|
[22] | 160 |
|
---|
[51] | 161 | $page->param(browsebits =>
|
---|
| 162 | DNSBLweb::retlvl($dbh, $dnsbl, 0, ip => $webvar{ip}, block => $dnsbl->getcontainer($webvar{ip},0) ));
|
---|
[2] | 163 | }
|
---|
[25] | 164 |
|
---|
[2] | 165 | print $page->output;
|
---|