1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | no warnings qw(uninitialized);
|
---|
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 | print "Content-type: text/html\n\n";
|
---|
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";
|
---|
31 |
|
---|
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.
|
---|
36 | my $cfgname = $ENV{SERVER_NAME}.$ENV{SCRIPT_NAME};
|
---|
37 | $cfgname =~ s|[./-]|_|g;
|
---|
38 | $cfgname =~ s|_dnsbl_cgi||;
|
---|
39 | if (-e "/etc/dnsbl/$cfgname.conf") {
|
---|
40 | my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
|
---|
41 | ($cfg) = ($cfg =~ /^(.+)$/s); # avoid warnings, failures, and general nastiness with taint mode
|
---|
42 | eval $cfg;
|
---|
43 | }
|
---|
44 |
|
---|
45 | my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);
|
---|
46 |
|
---|
47 | my $page;
|
---|
48 | my $templatedir = $ENV{SCRIPT_FILENAME};
|
---|
49 | $templatedir =~ s/dnsbl\.cgi//;
|
---|
50 | $templatedir .= "templates";
|
---|
51 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
|
---|
52 |
|
---|
53 | my %config;
|
---|
54 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
|
---|
55 | $sth->execute;
|
---|
56 | while (my ($key,$value) = $sth->fetchrow_array) {
|
---|
57 | $config{$key} = $value;
|
---|
58 | }
|
---|
59 |
|
---|
60 | # decide which page to spit out...
|
---|
61 | if (!$webvar{page}) {
|
---|
62 | $page = HTML::Template->new(filename => "index.tmpl");
|
---|
63 | } else {
|
---|
64 | $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
|
---|
65 | }
|
---|
66 |
|
---|
67 | $page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
|
---|
68 | $page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
|
---|
69 |
|
---|
70 | if ($webvar{page} eq 'report') {
|
---|
71 | $page->param(ip => $webvar{ip});
|
---|
72 | my $count = $dnsbl->ipexists($webvar{ip});
|
---|
73 | $page->param(nreports => $count) if $count;
|
---|
74 | $page->param(browsebits => browse($dbh,$webvar{ip}));
|
---|
75 | for (my $i=0; $i<3; $i++) {
|
---|
76 | my ($block,$org) = $dnsbl->getcontainer($webvar{ip},$i);
|
---|
77 | if ($block) {
|
---|
78 | $page->param("block$i" => $block);
|
---|
79 | $page->param("org$i" => $org);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | } elsif ($webvar{page} eq 'dbreport') {
|
---|
83 | my $err = '';
|
---|
84 | my $count = $dnsbl->report($webvar{ip});
|
---|
85 | my $org0id = $dnsbl->orgexists($webvar{org0});
|
---|
86 | if (!$org0id) {
|
---|
87 | $org0id = $dnsbl->addorg($webvar{org0});
|
---|
88 | $page->param(org0 => $webvar{org0});
|
---|
89 | }
|
---|
90 | if (!$dnsbl->blockexists($webvar{block0})) {
|
---|
91 | my $ret = $dnsbl->addblock($webvar{block0}, $org0id, 0);
|
---|
92 | $err .= "error adding $webvar{block0}: $ret<br>\n" if $ret;
|
---|
93 | $page->param(block0 => $webvar{block0});
|
---|
94 | }
|
---|
95 | # yes, this is grotty. PTHBTT!
|
---|
96 | if ($webvar{block1}) {
|
---|
97 | my $org1id = $dnsbl->orgexists($webvar{org1});
|
---|
98 | if (!$org1id) {
|
---|
99 | $org1id = $dnsbl->addorg($webvar{org1});
|
---|
100 | $page->param(org1 => $webvar{org1});
|
---|
101 | }
|
---|
102 | if (!$dnsbl->blockexists($webvar{block1})) {
|
---|
103 | my $ret = $dnsbl->addblock($webvar{block1}, $org1id, 1);
|
---|
104 | $err .= "error adding $webvar{block1}: $ret<br>\n" if $ret;
|
---|
105 | $page->param(block1 => $webvar{block1});
|
---|
106 | }
|
---|
107 | if ($webvar{block2}) {
|
---|
108 | my $org2id = $dnsbl->orgexists($webvar{org2});
|
---|
109 | if (!$org2id) {
|
---|
110 | $org2id = $dnsbl->addorg($webvar{org2});
|
---|
111 | $page->param(org2 => $webvar{org2});
|
---|
112 | }
|
---|
113 | if (!$dnsbl->blockexists($webvar{block2})) {
|
---|
114 | my $ret = $dnsbl->addblock($webvar{block2}, $org2id, 2);
|
---|
115 | $err .= "error adding $webvar{block2}: $ret<br>\n" if $ret;
|
---|
116 | $page->param(block2 => $webvar{block2});
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | $page->param(ip => $webvar{ip});
|
---|
122 | $page->param(err => $err);
|
---|
123 |
|
---|
124 | $page->param(browsebits => browse($dbh,$webvar{ip}));
|
---|
125 | }
|
---|
126 |
|
---|
127 | print $page->output;
|
---|
128 |
|
---|
129 | exit 0;
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 | ## extra subs. should probably put this in a module somehow to share with browse.cgi
|
---|
134 |
|
---|
135 | sub browse {
|
---|
136 | my $dbh = shift;
|
---|
137 | my $ip = shift;
|
---|
138 | my $ipcidr = new NetAddr::IP $ip;
|
---|
139 |
|
---|
140 | my $basesql = "SELECT b.block,o.orgname,b.listme,o.listme,b.comments,o.comments ".
|
---|
141 | "FROM blocks b INNER JOIN orgs o ON b.orgid=o.orgid ".
|
---|
142 | "WHERE b.block ";
|
---|
143 |
|
---|
144 | my $sth0 = $dbh->prepare($basesql." >> ? AND b.level=0 ORDER BY block");
|
---|
145 | my $sth1 = $dbh->prepare($basesql." <<= ? AND b.level=1 ORDER BY block");
|
---|
146 | my $sth2 = $dbh->prepare($basesql." <<= ? AND b.level=2 ORDER BY block");
|
---|
147 | my $sthiplist = $dbh->prepare("select * from iplist where ip <<= ? order by ip");
|
---|
148 |
|
---|
149 | my %ipseen;
|
---|
150 | my $out = '';
|
---|
151 |
|
---|
152 | my $tmpl0 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
153 |
|
---|
154 | $sth0->execute($ip);
|
---|
155 | while (my ($block0,$org0,$listmeb0,$listmeo0,$bcomments0,$ocomments0) = $sth0->fetchrow_array) {
|
---|
156 | my $block0cidr = new NetAddr::IP $block0;
|
---|
157 | $tmpl0->param(lvlclass => 'lvl0'.($dnsbl->autolist_block($block0) ? ' auto0' : '').
|
---|
158 | ( $ipcidr->within($block0cidr) ? ' inhere' : ''));
|
---|
159 | $tmpl0->param(netclass => ($listmeb0 ? 'b0list' : ''));
|
---|
160 | $tmpl0->param(net => $block0);
|
---|
161 | $tmpl0->param(orgclass => ($listmeo0 ? 'b0org' : ''));
|
---|
162 | $tmpl0->param(org => $org0);
|
---|
163 | $tmpl0->param(bcomment => $bcomments0) if $bcomments0;
|
---|
164 | $tmpl0->param(ocomment => $ocomments0) if $ocomments0;
|
---|
165 | $sth1->execute($block0);
|
---|
166 | my $lvl1out = '';
|
---|
167 | if ($sth1->rows > 0) {
|
---|
168 | while (my ($block1,$org1,$listmeb1,$listmeo1,$bcomments1,$ocomments1) = $sth1->fetchrow_array) {
|
---|
169 | my $block1cidr = new NetAddr::IP $block1;
|
---|
170 | my $tmpl1 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
171 | $tmpl1->param(lvlclass => 'lvl1'.($dnsbl->autolist_block($block1) ? ' auto1' : '').
|
---|
172 | ( $ipcidr->within($block1cidr) ? ' inhere' : ''));
|
---|
173 | $tmpl1->param(netclass => ($listmeb1 ? 'b1list' : ''));
|
---|
174 | $tmpl1->param(net => $block1);
|
---|
175 | $tmpl1->param(orgclass => ($listmeo1 ? 'b1org' : ''));
|
---|
176 | $tmpl1->param(org => $org1);
|
---|
177 | $tmpl1->param(bcomment => $bcomments1) if $bcomments1;
|
---|
178 | $tmpl1->param(ocomment => $ocomments1) if $ocomments1;
|
---|
179 | $tmpl1->param(indent => ' ');
|
---|
180 | my $lvl2out = '';
|
---|
181 | $sth2->execute($block1);
|
---|
182 | if ($sth2->rows > 0) {
|
---|
183 | while (my ($block2,$org2,$listmeb2,$listmeo2,$bcomments2,$ocomments2) = $sth2->fetchrow_array) {
|
---|
184 | my $block2cidr = new NetAddr::IP $block2;
|
---|
185 | my $tmpl2 = new HTML::Template(filename => 'templates/browse-block.tmpl');
|
---|
186 | $tmpl2->param(lvlclass => 'lvl2'.($dnsbl->autolist_block($block2) ? ' auto2' : '').
|
---|
187 | ( $ipcidr->within($block2cidr) ? ' inhere' : ''));
|
---|
188 | $tmpl2->param(netclass => ($listmeb2 ? 'b2list' : ''));
|
---|
189 | $tmpl2->param(net => $block2);
|
---|
190 | $tmpl2->param(orgclass => ($listmeo2 ? 'b2org' : ''));
|
---|
191 | $tmpl2->param(org => $org2);
|
---|
192 | $tmpl2->param(bcomment => $bcomments2) if $bcomments2;
|
---|
193 | $tmpl2->param(ocomment => $ocomments2) if $ocomments2;
|
---|
194 | $tmpl2->param(indent => ' ');
|
---|
195 | $sthiplist->execute($block2);
|
---|
196 | my @iprows;
|
---|
197 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
198 | my %iprow;
|
---|
199 | $iprow{ip} = $data4[0];
|
---|
200 | $iprow{ipcount} = $data4[1];
|
---|
201 | $iprow{indent} = ' ';
|
---|
202 | $iprow{repeater} = 1 if $ip eq $data4[0];
|
---|
203 | # ip | count | s4list | added
|
---|
204 | push @iprows, \%iprow;
|
---|
205 | $ipseen{$data4[0]} = 1;
|
---|
206 | }
|
---|
207 | $tmpl2->param(iplist => \@iprows);
|
---|
208 | $lvl2out .= $tmpl2->output;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | $sthiplist->execute($block1);
|
---|
213 | my @iprows;
|
---|
214 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
215 | next if $ipseen{$data4[0]};
|
---|
216 | my %iprow;
|
---|
217 | $iprow{ip} = $data4[0];
|
---|
218 | $iprow{ipcount} = $data4[1];
|
---|
219 | $iprow{indent} = ' ';
|
---|
220 | $iprow{repeater} = 1 if $ip eq $data4[0];
|
---|
221 | # ip | count | s4list | added
|
---|
222 | push @iprows, \%iprow;
|
---|
223 | $ipseen{$data4[0]} = 1;
|
---|
224 | }
|
---|
225 | $tmpl1->param(iplist => \@iprows);
|
---|
226 | $tmpl1->param(subs => $lvl2out);
|
---|
227 | $lvl1out .= $tmpl1->output;
|
---|
228 |
|
---|
229 | }
|
---|
230 | } # sth1->rows
|
---|
231 | $sthiplist->execute($block0);
|
---|
232 | my @iprows;
|
---|
233 | while (my @data4 = $sthiplist->fetchrow_array) {
|
---|
234 | next if $ipseen{$data4[0]};
|
---|
235 | my %iprow;
|
---|
236 | $iprow{ip} = $data4[0];
|
---|
237 | $iprow{ipcount} = $data4[1];
|
---|
238 | $iprow{indent} = '';
|
---|
239 | $iprow{repeater} = 1 if $ip eq $data4[0];
|
---|
240 | # ip | count | s4list | added
|
---|
241 | push @iprows, \%iprow;
|
---|
242 | $ipseen{$data4[0]} = 1;
|
---|
243 | }
|
---|
244 | $tmpl0->param(iplist => \@iprows);
|
---|
245 | $tmpl0->param(subs => $lvl1out);
|
---|
246 | }
|
---|
247 |
|
---|
248 | return $tmpl0->output;
|
---|
249 | } # end browse()
|
---|