1 | #!/usr/bin/perl
|
---|
2 | # quickndirty browse-the-damned-by-web
|
---|
3 | ##
|
---|
4 | # $Id: browse.cgi 40 2012-03-04 20:02:13Z kdeugau $
|
---|
5 | # Copyright 2009-2011 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 | use DBI;
|
---|
24 | use CGI::Carp qw(fatalsToBrowser);
|
---|
25 | use HTML::Template;
|
---|
26 |
|
---|
27 | use DNSBL;
|
---|
28 |
|
---|
29 | my $dnsbl = new DNSBL;
|
---|
30 |
|
---|
31 | # default DB info - all other settings should be loaded from the DB.
|
---|
32 | my $dbhost = "localhost";
|
---|
33 | my $dbname = "dnsbl";
|
---|
34 | my $dbuser = "dnsbl";
|
---|
35 | my $dbpass = "spambgone";
|
---|
36 |
|
---|
37 | # Load a config ref containing DB host, name, user, and pass info based on
|
---|
38 | # from the server name + full script web path. This allows us to host
|
---|
39 | # multiple instances without having to duplicate the code.
|
---|
40 | # This file is a Perl fragment to be processed inline.
|
---|
41 | my $cfgname = $ENV{SERVER_NAME}.$ENV{SCRIPT_NAME};
|
---|
42 | $cfgname =~ s|[./-]|_|g;
|
---|
43 | $cfgname =~ s|_browse_cgi||;
|
---|
44 | if (-e "/etc/dnsbl/$cfgname.conf") {
|
---|
45 | my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
|
---|
46 | ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
|
---|
47 | eval $cfg;
|
---|
48 | }
|
---|
49 |
|
---|
50 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
|
---|
51 |
|
---|
52 | print "Content-Type: text/html\n\n";
|
---|
53 |
|
---|
54 | my $templatedir = $ENV{SCRIPT_FILENAME};
|
---|
55 | $templatedir =~ s/browse\.cgi//;
|
---|
56 | $templatedir .= "templates";
|
---|
57 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
|
---|
58 |
|
---|
59 | my %config;
|
---|
60 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
|
---|
61 | $sth->execute;
|
---|
62 | while (my ($key,$value) = $sth->fetchrow_array) {
|
---|
63 | $config{$key} = $value;
|
---|
64 | }
|
---|
65 |
|
---|
66 | my $template = HTML::Template->new(filename => "browse.tmpl");
|
---|
67 |
|
---|
68 | $template->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
|
---|
69 | $template->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
|
---|
70 |
|
---|
71 | my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
|
---|
72 | "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
|
---|
73 | "WHERE b.parent = ";
|
---|
74 | my $sth0 = $dbh->prepare($basesql."'0/0' AND b.level=0 ORDER BY block");
|
---|
75 | #my $sth0 = $dbh->prepare($basesql."'64/8' AND b.level=0 ORDER BY block");
|
---|
76 | my $sth1 = $dbh->prepare($basesql."? AND b.level=1 ORDER BY block");
|
---|
77 | my $sth2 = $dbh->prepare($basesql."? AND b.level=2 ORDER BY block");
|
---|
78 | my $sthiplist = $dbh->prepare("select * from iplist where parent = ? order by ip");
|
---|
79 |
|
---|
80 | my %ipseen;
|
---|
81 |
|
---|
82 | my $out = '';
|
---|
83 |
|
---|
84 | $sth0->execute;
|
---|
85 | while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
|
---|
86 | my $tmpl0 = new HTML::Template(filename => "browse-block.tmpl");
|
---|
87 | $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : ''));
|
---|
88 | $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
|
---|
89 | $tmpl0->param(net => $block0);
|
---|
90 | $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
|
---|
91 | $tmpl0->param(org => $org0);
|
---|
92 | $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
|
---|
93 | $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
|
---|
94 | $sth1->execute($block0);
|
---|
95 | my $lvl1out = '';
|
---|
96 | if ($sth1->rows > 0) {
|
---|
97 | while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
|
---|
98 | my $tmpl1 = new HTML::Template(filename => "browse-block.tmpl");
|
---|
99 | $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : ''));
|
---|
100 | $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
|
---|
101 | $tmpl1->param(net => $block1);
|
---|
102 | $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
|
---|
103 | $tmpl1->param(org => $org1);
|
---|
104 | $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
|
---|
105 | $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
|
---|
106 | $tmpl1->param(indent => ' ');
|
---|
107 | my $lvl2out = '';
|
---|
108 | $sth2->execute($block1);
|
---|
109 | if ($sth2->rows > 0) {
|
---|
110 | while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
|
---|
111 | my $tmpl2 = new HTML::Template(filename => "browse-block.tmpl");
|
---|
112 | $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : ''));
|
---|
113 | $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
|
---|
114 | $tmpl2->param(net => $block2);
|
---|
115 | $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
|
---|
116 | $tmpl2->param(org => $org2);
|
---|
117 | $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
|
---|
118 | $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
|
---|
119 | $tmpl2->param(indent => ' ');
|
---|
120 | $sthiplist->execute($block2);
|
---|
121 | my @iprows;
|
---|
122 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
123 | my %iprow;
|
---|
124 | $iprow{ip} = $data4[0];
|
---|
125 | $iprow{ipcount} = $data4[1];
|
---|
126 | $iprow{indent} = ' ';
|
---|
127 | # ip | count | s4list | added
|
---|
128 | push @iprows, \%iprow;
|
---|
129 | $ipseen{$data4[0]} = 1;
|
---|
130 | }
|
---|
131 | $tmpl2->param(iplist => \@iprows);
|
---|
132 | $lvl2out .= $tmpl2->output;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | $sthiplist->execute($block1);
|
---|
137 | my @iprows;
|
---|
138 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
139 | next if $ipseen{$data4[0]};
|
---|
140 | my %iprow;
|
---|
141 | $iprow{ip} = $data4[0];
|
---|
142 | $iprow{ipcount} = $data4[1];
|
---|
143 | $iprow{indent} = ' ';
|
---|
144 | # ip | count | s4list | added
|
---|
145 | push @iprows, \%iprow;
|
---|
146 | $ipseen{$data4[0]} = 1;
|
---|
147 | }
|
---|
148 | $tmpl1->param(iplist => \@iprows);
|
---|
149 | $tmpl1->param(subs => $lvl2out);
|
---|
150 | $lvl1out .= $tmpl1->output;
|
---|
151 | }
|
---|
152 | } # sth1->rows
|
---|
153 | $sthiplist->execute($block0);
|
---|
154 | my @iprows;
|
---|
155 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
156 | next if $ipseen{$data4[0]};
|
---|
157 | my %iprow;
|
---|
158 | $iprow{ip} = $data4[0];
|
---|
159 | $iprow{ipcount} = $data4[1];
|
---|
160 | $iprow{indent} = '';
|
---|
161 | # ip | count | s4list | added
|
---|
162 | push @iprows, \%iprow;
|
---|
163 | $ipseen{$data4[0]} = 1;
|
---|
164 | }
|
---|
165 | $tmpl0->param(iplist => \@iprows);
|
---|
166 | $tmpl0->param(subs => $lvl1out);
|
---|
167 | $out .= $tmpl0->output;
|
---|
168 | }
|
---|
169 |
|
---|
170 | # probably more efficient ways to do this somewhere...
|
---|
171 | $template->param(enchilada => $out);
|
---|
172 | print $template->output;
|
---|