[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 |
|
---|
[7] | 24 | my $dbh = $dnsbl->connect;
|
---|
[2] | 25 |
|
---|
| 26 | print "Content-type: text/html\n\n";
|
---|
| 27 |
|
---|
| 28 | my $page;
|
---|
| 29 | my $templatedir = "templates";
|
---|
| 30 |
|
---|
| 31 | # decide which page to spit out...
|
---|
| 32 | if (!$webvar{page}) {
|
---|
| 33 | $page = HTML::Template->new(filename => "$templatedir/index.tmpl");
|
---|
| 34 | } else {
|
---|
| 35 | $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | if ($webvar{page} eq 'report') {
|
---|
| 39 | $page->param(ip => $webvar{ip});
|
---|
[5] | 40 | my $count = $dnsbl->ipexists($webvar{ip});
|
---|
| 41 | $page->param(nreports => $count) if $count;
|
---|
[7] | 42 | $page->param(browsebits => browse($dbh,$webvar{ip}));
|
---|
[2] | 43 | for (my $i=0; $i<3; $i++) {
|
---|
| 44 | my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
|
---|
| 45 | if ($block) {
|
---|
| 46 | $page->param("block$i" => $block);
|
---|
| 47 | $page->param("org$i" => $org);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | } elsif ($webvar{page} eq 'dbreport') {
|
---|
[11] | 51 | my $err = '';
|
---|
[2] | 52 | my $count = $dnsbl->report($webvar{ip});
|
---|
| 53 | my $org0id = $dnsbl->orgexists($webvar{org0});
|
---|
| 54 | if (!$org0id) {
|
---|
| 55 | $org0id = $dnsbl->addorg($webvar{org0});
|
---|
| 56 | $page->param(org0 => $webvar{org0});
|
---|
| 57 | }
|
---|
| 58 | if (!$dnsbl->blockexists($webvar{block0})) {
|
---|
[11] | 59 | my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
|
---|
| 60 | $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
|
---|
[2] | 61 | $page->param(block0 => $webvar{block0});
|
---|
| 62 | }
|
---|
| 63 | # yes, this is grotty. PTHBTT!
|
---|
| 64 | if ($webvar{block1}) {
|
---|
| 65 | my $org1id = $dnsbl->orgexists($webvar{org1});
|
---|
| 66 | if (!$org1id) {
|
---|
| 67 | $org1id = $dnsbl->addorg($webvar{org1});
|
---|
| 68 | $page->param(org1 => $webvar{org1});
|
---|
| 69 | }
|
---|
| 70 | if (!$dnsbl->blockexists($webvar{block1})) {
|
---|
[11] | 71 | my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
|
---|
| 72 | $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
|
---|
[2] | 73 | $page->param(block1 => $webvar{block1});
|
---|
| 74 | }
|
---|
| 75 | if ($webvar{block2}) {
|
---|
| 76 | my $org2id = $dnsbl->orgexists($webvar{org2});
|
---|
| 77 | if (!$org2id) {
|
---|
| 78 | $org2id = $dnsbl->addorg($webvar{org2});
|
---|
| 79 | $page->param(org2 => $webvar{org2});
|
---|
| 80 | }
|
---|
| 81 | if (!$dnsbl->blockexists($webvar{block2})) {
|
---|
[11] | 82 | my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
|
---|
| 83 | $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
|
---|
[2] | 84 | $page->param(block2 => $webvar{block2});
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | $page->param(ip => $webvar{ip});
|
---|
[11] | 90 | $page->param(err => $err);
|
---|
[2] | 91 | }
|
---|
| 92 | print $page->output;
|
---|
[7] | 93 |
|
---|
| 94 | exit 0;
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | ## extra subs. should probably put this in a module somehow to share with browse.cgi
|
---|
| 99 |
|
---|
| 100 | sub browse {
|
---|
| 101 | my $dbh = shift;
|
---|
| 102 | my $ip = shift;
|
---|
| 103 |
|
---|
| 104 | my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
|
---|
| 105 | "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
|
---|
| 106 | "WHERE b.block ";
|
---|
| 107 |
|
---|
| 108 | my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
|
---|
| 109 | my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
|
---|
| 110 | my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
|
---|
| 111 | my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
|
---|
| 112 |
|
---|
| 113 | my %ipseen;
|
---|
| 114 | my $out = '';
|
---|
| 115 |
|
---|
[19] | 116 | my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
| 117 |
|
---|
[7] | 118 | $sth0->execute($ip);
|
---|
| 119 | while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
|
---|
[19] | 120 | $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : ''));
|
---|
| 121 | $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
|
---|
| 122 | $tmpl0->param(net => $block0);
|
---|
| 123 | $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
|
---|
| 124 | $tmpl0->param(org => $org0);
|
---|
| 125 | $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
|
---|
| 126 | $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
|
---|
[7] | 127 | $sth1->execute($block0);
|
---|
[19] | 128 | my $lvl1out = '';
|
---|
[7] | 129 | if ($sth1->rows > 0) {
|
---|
| 130 | while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
|
---|
[19] | 131 | my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
| 132 | $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : ''));
|
---|
| 133 | $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
|
---|
| 134 | $tmpl1->param(net => $block1);
|
---|
| 135 | $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
|
---|
| 136 | $tmpl1->param(org => $org1);
|
---|
| 137 | $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
|
---|
| 138 | $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
|
---|
| 139 | $tmpl1->param(indent => ' ');
|
---|
| 140 | my $lvl2out = '';
|
---|
[7] | 141 | $sth2->execute($block1);
|
---|
| 142 | if ($sth2->rows > 0) {
|
---|
| 143 | while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
|
---|
[19] | 144 | my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
| 145 | $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : ''));
|
---|
| 146 | $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
|
---|
| 147 | $tmpl2->param(net => $block2);
|
---|
| 148 | $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
|
---|
| 149 | $tmpl2->param(org => $org2);
|
---|
| 150 | $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
|
---|
| 151 | $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
|
---|
| 152 | $tmpl2->param(indent => ' ');
|
---|
[7] | 153 | $sthiplist->execute($block2);
|
---|
[19] | 154 | my @iprows;
|
---|
[7] | 155 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
[19] | 156 | my %iprow;
|
---|
| 157 | $iprow{ip} = $data4[0];
|
---|
| 158 | $iprow{ipcount} = $data4[1];
|
---|
| 159 | $iprow{indent} = ' ';
|
---|
[14] | 160 | # ip | count | s4list | added
|
---|
[19] | 161 | push @iprows, \%iprow;
|
---|
[7] | 162 | $ipseen{$data4[0]} = 1;
|
---|
| 163 | }
|
---|
[19] | 164 | $tmpl2->param(iplist => \@iprows);
|
---|
| 165 | $lvl2out .= $tmpl2->output;
|
---|
[7] | 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[19] | 169 | $sthiplist->execute($block1);
|
---|
| 170 | my @iprows;
|
---|
| 171 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
| 172 | next if $ipseen{$data4[0]};
|
---|
| 173 | my %iprow;
|
---|
| 174 | $iprow{ip} = $data4[0];
|
---|
| 175 | $iprow{ipcount} = $data4[1];
|
---|
| 176 | $iprow{indent} = ' ';
|
---|
| 177 | # ip | count | s4list | added
|
---|
| 178 | push @iprows, \%iprow;
|
---|
[7] | 179 | $ipseen{$data4[0]} = 1;
|
---|
| 180 | }
|
---|
[19] | 181 | $tmpl1->param(iplist => \@iprows);
|
---|
| 182 | $tmpl1->param(subs => $lvl2out);
|
---|
| 183 | $lvl1out .= $tmpl1->output;
|
---|
| 184 |
|
---|
[7] | 185 | }
|
---|
[19] | 186 | } # sth1->rows
|
---|
| 187 | $sthiplist->execute($block0);
|
---|
| 188 | my @iprows;
|
---|
| 189 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
| 190 | next if $ipseen{$data4[0]};
|
---|
| 191 | my %iprow;
|
---|
| 192 | $iprow{ip} = $data4[0];
|
---|
| 193 | $iprow{ipcount} = $data4[1];
|
---|
| 194 | $iprow{indent} = '';
|
---|
[14] | 195 | # ip | count | s4list | added
|
---|
[19] | 196 | push @iprows, \%iprow;
|
---|
| 197 | $ipseen{$data4[0]} = 1;
|
---|
[7] | 198 | }
|
---|
[19] | 199 | $tmpl0->param(iplist => \@iprows);
|
---|
| 200 | $tmpl0->param(subs => $lvl1out);
|
---|
[7] | 201 | }
|
---|
| 202 |
|
---|
[19] | 203 | return $tmpl0->output;
|
---|
[7] | 204 | } # end browse()
|
---|