[2] | 1 | #!/usr/bin/perl
|
---|
| 2 |
|
---|
| 3 | use strict;
|
---|
| 4 | use warnings;
|
---|
[17] | 5 | no warnings qw(uninitialized);
|
---|
[2] | 6 | use CGI::Carp qw (fatalsToBrowser);
|
---|
| 7 | use CGI::Simple;
|
---|
| 8 | use HTML::Template;
|
---|
| 9 | use DNSBL;
|
---|
| 10 |
|
---|
| 11 | # Set up the CGI object...
|
---|
| 12 | my $q = new CGI::Simple;
|
---|
| 13 | # ... and get query-string params as well as POST params if necessary
|
---|
| 14 | $q->parse_query_string;
|
---|
| 15 |
|
---|
| 16 | my %webvar;
|
---|
| 17 | # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
|
---|
| 18 | foreach ($q->param()) {
|
---|
| 19 | $webvar{$_} = $q->param($_);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | my $dnsbl = new DNSBL;
|
---|
| 23 |
|
---|
[24] | 24 | print "Content-type: text/html\n\n";
|
---|
| 25 |
|
---|
[25] | 26 | # default DB info - all other settings should be loaded from the DB.
|
---|
| 27 | my $dbhost = "localhost";
|
---|
| 28 | my $dbname = "dnsbl";
|
---|
| 29 | my $dbuser = "dnsbl";
|
---|
| 30 | my $dbpass = "spambgone";
|
---|
[2] | 31 |
|
---|
[25] | 32 | # Load a config ref containing DB host, name, user, and pass info based on
|
---|
| 33 | # from the server name + full script web path. This allows us to host
|
---|
| 34 | # multiple instances without having to duplicate the code.
|
---|
| 35 | # This file is a Perl fragment to be processed inline.
|
---|
[26] | 36 | my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
|
---|
[25] | 37 | $cfgname =~ s|[./-]|_|g;
|
---|
[26] | 38 | $cfgname =~ s|_dnsbl_cgi.+||;
|
---|
| 39 | $cfgname =~ s|_$||;
|
---|
[25] | 40 | if (-e "/etc/dnsbl/$cfgname.conf") {
|
---|
| 41 | my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
|
---|
| 42 | ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
|
---|
| 43 | eval $cfg;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
|
---|
| 47 |
|
---|
[2] | 48 | my $page;
|
---|
[25] | 49 | my $templatedir = $ENV{SCRIPT_FILENAME};
|
---|
| 50 | $templatedir =~ s/dnsbl\.cgi//;
|
---|
| 51 | $templatedir .= "templates";
|
---|
| 52 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
|
---|
[2] | 53 |
|
---|
[25] | 54 | my %config;
|
---|
| 55 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
|
---|
| 56 | $sth->execute;
|
---|
| 57 | while (my ($key,$value) = $sth->fetchrow_array) {
|
---|
| 58 | $config{$key} = $value;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[2] | 61 | # decide which page to spit out...
|
---|
| 62 | if (!$webvar{page}) {
|
---|
[25] | 63 | $page = HTML::Template->new(filename => "index.tmpl");
|
---|
[2] | 64 | } else {
|
---|
[25] | 65 | $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
|
---|
[2] | 66 | }
|
---|
| 67 |
|
---|
[25] | 68 | $page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
|
---|
| 69 | $page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
|
---|
| 70 |
|
---|
[2] | 71 | if ($webvar{page} eq 'report') {
|
---|
[26] | 72 | my $dnsblsiteroot = $ENV{REQUEST_URI};
|
---|
| 73 | $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
|
---|
| 74 | $page->param(dnsblsiteroot => $dnsblsiteroot);
|
---|
| 75 |
|
---|
[2] | 76 | $page->param(ip => $webvar{ip});
|
---|
[5] | 77 | my $count = $dnsbl->ipexists($webvar{ip});
|
---|
| 78 | $page->param(nreports => $count) if $count;
|
---|
[7] | 79 | $page->param(browsebits => browse($dbh,$webvar{ip}));
|
---|
[2] | 80 | for (my $i=0; $i<3; $i++) {
|
---|
| 81 | my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
|
---|
| 82 | if ($block) {
|
---|
[29] | 83 | my ($bcl,$bal) = $dnsbl->islisted($block);
|
---|
| 84 | $page->param("autob$i" => $bcl);
|
---|
| 85 | $page->param("listb$i" => $bal);
|
---|
| 86 | my ($ol) = $dnsbl->islisted($org);
|
---|
| 87 | $page->param("listorg$i" => $ol);
|
---|
[2] | 88 | $page->param("block$i" => $block);
|
---|
| 89 | $page->param("org$i" => $org);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | } elsif ($webvar{page} eq 'dbreport') {
|
---|
[26] | 93 | my $dnsblsiteroot = $ENV{REQUEST_URI};
|
---|
| 94 | $dnsblsiteroot =~ s|/dnsbl\.cgi\?.+|/|;
|
---|
| 95 | $page->param(dnsblsiteroot => $dnsblsiteroot);
|
---|
| 96 |
|
---|
[11] | 97 | my $err = '';
|
---|
[2] | 98 | my $count = $dnsbl->report($webvar{ip});
|
---|
| 99 | my $org0id = $dnsbl->orgexists($webvar{org0});
|
---|
| 100 | if (!$org0id) {
|
---|
| 101 | $org0id = $dnsbl->addorg($webvar{org0});
|
---|
| 102 | $page->param(org0 => $webvar{org0});
|
---|
| 103 | }
|
---|
| 104 | if (!$dnsbl->blockexists($webvar{block0})) {
|
---|
[11] | 105 | my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
|
---|
| 106 | $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
|
---|
[2] | 107 | $page->param(block0 => $webvar{block0});
|
---|
| 108 | }
|
---|
| 109 | # yes, this is grotty. PTHBTT!
|
---|
| 110 | if ($webvar{block1}) {
|
---|
| 111 | my $org1id = $dnsbl->orgexists($webvar{org1});
|
---|
| 112 | if (!$org1id) {
|
---|
| 113 | $org1id = $dnsbl->addorg($webvar{org1});
|
---|
| 114 | $page->param(org1 => $webvar{org1});
|
---|
| 115 | }
|
---|
| 116 | if (!$dnsbl->blockexists($webvar{block1})) {
|
---|
[11] | 117 | my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
|
---|
| 118 | $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
|
---|
[2] | 119 | $page->param(block1 => $webvar{block1});
|
---|
| 120 | }
|
---|
| 121 | if ($webvar{block2}) {
|
---|
| 122 | my $org2id = $dnsbl->orgexists($webvar{org2});
|
---|
| 123 | if (!$org2id) {
|
---|
| 124 | $org2id = $dnsbl->addorg($webvar{org2});
|
---|
| 125 | $page->param(org2 => $webvar{org2});
|
---|
| 126 | }
|
---|
| 127 | if (!$dnsbl->blockexists($webvar{block2})) {
|
---|
[11] | 128 | my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
|
---|
| 129 | $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
|
---|
[2] | 130 | $page->param(block2 => $webvar{block2});
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | $page->param(ip => $webvar{ip});
|
---|
[11] | 136 | $page->param(err => $err);
|
---|
[22] | 137 |
|
---|
| 138 | $page->param(browsebits => browse($dbh,$webvar{ip}));
|
---|
[2] | 139 | }
|
---|
[25] | 140 |
|
---|
[2] | 141 | print $page->output;
|
---|
[7] | 142 |
|
---|
| 143 | exit 0;
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | ## extra subs. should probably put this in a module somehow to share with browse.cgi
|
---|
| 148 |
|
---|
| 149 | sub browse {
|
---|
| 150 | my $dbh = shift;
|
---|
| 151 | my $ip = shift;
|
---|
[22] | 152 | my $ipcidr = new NetAddr::IP $ip;
|
---|
[7] | 153 |
|
---|
| 154 | my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
|
---|
| 155 | "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
|
---|
| 156 | "WHERE b.block ";
|
---|
| 157 |
|
---|
| 158 | my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
|
---|
| 159 | my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
|
---|
| 160 | my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
|
---|
| 161 | my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
|
---|
| 162 |
|
---|
| 163 | my %ipseen;
|
---|
| 164 | my $out = '';
|
---|
| 165 |
|
---|
[19] | 166 | my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
| 167 |
|
---|
[7] | 168 | $sth0->execute($ip);
|
---|
| 169 | while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
|
---|
[22] | 170 | my $block0cidr = new NetAddr::IP $block0;
|
---|
| 171 | $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : '').
|
---|
| 172 | ( $ipcidr->within($block0cidr) ? ' inhere' : ''));
|
---|
[19] | 173 | $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
|
---|
| 174 | $tmpl0->param(net => $block0);
|
---|
| 175 | $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
|
---|
| 176 | $tmpl0->param(org => $org0);
|
---|
| 177 | $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
|
---|
| 178 | $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
|
---|
[7] | 179 | $sth1->execute($block0);
|
---|
[19] | 180 | my $lvl1out = '';
|
---|
[7] | 181 | if ($sth1->rows > 0) {
|
---|
| 182 | while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
|
---|
[22] | 183 | my $block1cidr = new NetAddr::IP $block1;
|
---|
[19] | 184 | my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
[22] | 185 | $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : '').
|
---|
| 186 | ( $ipcidr->within($block1cidr) ? ' inhere' : ''));
|
---|
[19] | 187 | $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
|
---|
| 188 | $tmpl1->param(net => $block1);
|
---|
| 189 | $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
|
---|
| 190 | $tmpl1->param(org => $org1);
|
---|
| 191 | $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
|
---|
| 192 | $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
|
---|
| 193 | $tmpl1->param(indent => ' ');
|
---|
| 194 | my $lvl2out = '';
|
---|
[7] | 195 | $sth2->execute($block1);
|
---|
| 196 | if ($sth2->rows > 0) {
|
---|
| 197 | while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
|
---|
[22] | 198 | my $block2cidr = new NetAddr::IP $block2;
|
---|
[19] | 199 | my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
[22] | 200 | $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : '').
|
---|
| 201 | ( $ipcidr->within($block2cidr) ? ' inhere' : ''));
|
---|
[19] | 202 | $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
|
---|
| 203 | $tmpl2->param(net => $block2);
|
---|
| 204 | $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
|
---|
| 205 | $tmpl2->param(org => $org2);
|
---|
| 206 | $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
|
---|
| 207 | $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
|
---|
| 208 | $tmpl2->param(indent => ' ');
|
---|
[7] | 209 | $sthiplist->execute($block2);
|
---|
[19] | 210 | my @iprows;
|
---|
[7] | 211 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
[19] | 212 | my %iprow;
|
---|
| 213 | $iprow{ip} = $data4[0];
|
---|
| 214 | $iprow{ipcount} = $data4[1];
|
---|
| 215 | $iprow{indent} = ' ';
|
---|
[22] | 216 | $iprow{repeater} = 1 if $ip eq $data4[0];
|
---|
[14] | 217 | # ip | count | s4list | added
|
---|
[19] | 218 | push @iprows, \%iprow;
|
---|
[7] | 219 | $ipseen{$data4[0]} = 1;
|
---|
| 220 | }
|
---|
[19] | 221 | $tmpl2->param(iplist => \@iprows);
|
---|
| 222 | $lvl2out .= $tmpl2->output;
|
---|
[7] | 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[19] | 226 | $sthiplist->execute($block1);
|
---|
| 227 | my @iprows;
|
---|
| 228 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
| 229 | next if $ipseen{$data4[0]};
|
---|
| 230 | my %iprow;
|
---|
| 231 | $iprow{ip} = $data4[0];
|
---|
| 232 | $iprow{ipcount} = $data4[1];
|
---|
| 233 | $iprow{indent} = ' ';
|
---|
[22] | 234 | $iprow{repeater} = 1 if $ip eq $data4[0];
|
---|
[19] | 235 | # ip | count | s4list | added
|
---|
| 236 | push @iprows, \%iprow;
|
---|
[7] | 237 | $ipseen{$data4[0]} = 1;
|
---|
| 238 | }
|
---|
[19] | 239 | $tmpl1->param(iplist => \@iprows);
|
---|
| 240 | $tmpl1->param(subs => $lvl2out);
|
---|
| 241 | $lvl1out .= $tmpl1->output;
|
---|
| 242 |
|
---|
[7] | 243 | }
|
---|
[19] | 244 | } # sth1->rows
|
---|
| 245 | $sthiplist->execute($block0);
|
---|
| 246 | my @iprows;
|
---|
| 247 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
| 248 | next if $ipseen{$data4[0]};
|
---|
| 249 | my %iprow;
|
---|
| 250 | $iprow{ip} = $data4[0];
|
---|
| 251 | $iprow{ipcount} = $data4[1];
|
---|
| 252 | $iprow{indent} = '';
|
---|
[22] | 253 | $iprow{repeater} = 1 if $ip eq $data4[0];
|
---|
[14] | 254 | # ip | count | s4list | added
|
---|
[19] | 255 | push @iprows, \%iprow;
|
---|
| 256 | $ipseen{$data4[0]} = 1;
|
---|
[7] | 257 | }
|
---|
[19] | 258 | $tmpl0->param(iplist => \@iprows);
|
---|
| 259 | $tmpl0->param(subs => $lvl1out);
|
---|
[7] | 260 | }
|
---|
| 261 |
|
---|
[19] | 262 | return $tmpl0->output;
|
---|
[7] | 263 | } # end browse()
|
---|