[27] | 1 | # URIdb
|
---|
| 2 | # Functions for interacting with the URI database
|
---|
[41] | 3 | ##
|
---|
| 4 | # $Id: URIdb.pm 41 2012-03-04 20:52:10Z kdeugau $
|
---|
| 5 | # Copyright 2010 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 | ##
|
---|
[27] | 20 |
|
---|
| 21 | package URIdb;
|
---|
| 22 |
|
---|
| 23 | use strict;
|
---|
| 24 | use warnings;
|
---|
| 25 | use Exporter;
|
---|
| 26 | use DBI;
|
---|
| 27 | use NetAddr::IP;
|
---|
| 28 |
|
---|
| 29 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 30 |
|
---|
| 31 | $VERSION = 1.0;
|
---|
| 32 | @ISA = qw(Exporter);
|
---|
| 33 | @EXPORT_OK = qw(
|
---|
| 34 | );
|
---|
| 35 |
|
---|
| 36 | @EXPORT = (); # Export nothing by default.
|
---|
| 37 | %EXPORT_TAGS = ( ALL => [qw(
|
---|
| 38 | )]
|
---|
| 39 | );
|
---|
| 40 |
|
---|
| 41 | ## "constants"
|
---|
| 42 |
|
---|
| 43 | # 8 bits available
|
---|
| 44 | # 128 is per-IP shitlist
|
---|
| 45 | # 2 is IP hitlist
|
---|
| 46 | # 1 not available so we don't $self->shoot(foot)
|
---|
| 47 | our %status = (
|
---|
| 48 | 0 => "Don't list",
|
---|
| 49 | 2 => "Black",
|
---|
| 50 | 4 => "Grey",
|
---|
| 51 | 8 => "Abused URL shortener/redirector"
|
---|
| 52 | );
|
---|
| 53 |
|
---|
| 54 | # variables
|
---|
| 55 | our $dbh;
|
---|
| 56 |
|
---|
| 57 | our $err;
|
---|
| 58 | our $errstr;
|
---|
| 59 |
|
---|
| 60 | # basic object subs
|
---|
| 61 | sub new {
|
---|
| 62 | # iff we want to start taking arguments, or doing other things on instantiation
|
---|
| 63 | # my $self = {};
|
---|
| 64 | # bless $self, "DNSBL";
|
---|
| 65 | # return $self;
|
---|
| 66 | bless {};
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | sub DESTROY {
|
---|
| 70 | my $self = shift;
|
---|
| 71 | $self->dbclose() if $dbh;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | # JIC someone wants to close the db but not finish the script
|
---|
| 75 | sub dbclose {
|
---|
| 76 | $dbh->rollback;
|
---|
| 77 | $dbh->disconnect;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | ## specific object subs:
|
---|
| 81 |
|
---|
| 82 | sub connect {
|
---|
| 83 | my $self = shift;
|
---|
| 84 | my $dbhost = shift;
|
---|
| 85 | my $dbname = shift;
|
---|
| 86 | my $dbuser = shift;
|
---|
| 87 | my $dbpass = shift;
|
---|
| 88 | ## want to NOT autocommit everything, it's unlikely we'll step on our own toes but...
|
---|
| 89 | $dbh = DBI->connect("DBI:Pg:host=$dbhost;dbname=$dbname", $dbuser, $dbpass, {
|
---|
| 90 | AutoCommit => 0,
|
---|
| 91 | PrintError => 1
|
---|
| 92 | })
|
---|
| 93 | or die "database inaccessible: ".$DBI::errstr;
|
---|
| 94 | # my $sth = $dbh->prepare("SELECT masklen,ipcount FROM autolist");
|
---|
| 95 | # $sth->execute;
|
---|
| 96 | # while (my ($masklen,$ipcount) = $sth->fetchrow_array) {
|
---|
| 97 | # $autolist{$masklen} = $ipcount;
|
---|
| 98 | # }
|
---|
| 99 | return $dbh;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | # report a URI to the db
|
---|
| 104 | # increments a hit counter iff the reported URI exists, otherwise it adds it
|
---|
| 105 | # requires the uri.
|
---|
| 106 | # can accept the listing level and a comment
|
---|
| 107 | sub report {
|
---|
| 108 | my $self = shift;
|
---|
| 109 | my $uri = shift;
|
---|
| 110 | my $list = shift || 2;
|
---|
| 111 | my $comment = shift || '';
|
---|
| 112 |
|
---|
| 113 | my $sth;
|
---|
| 114 | my $rows = 0;
|
---|
| 115 | $sth = $dbh->prepare("SELECT count FROM urilist WHERE uri=?");
|
---|
| 116 | $sth->execute($uri) or die "eep? ".$dbh->errstr."\n";
|
---|
| 117 | $rows = $sth->rows;
|
---|
| 118 | if ($rows == 0) {
|
---|
| 119 | $sth = $dbh->prepare("INSERT INTO urilist (uri,list,comment) VALUES (?,?,?)");
|
---|
| 120 | $sth->execute($uri,$list,$comment) or die "couldn't add $uri: ".$dbh->errstr."\n";
|
---|
| 121 | } elsif ($rows == 1) {
|
---|
| 122 | $sth = $dbh->prepare("UPDATE urilist SET count=count+1 WHERE uri=?");
|
---|
| 123 | $sth->execute($uri) or die "couldn't update listing for $uri: ".$dbh->errstr."\n";
|
---|
| 124 | } else {
|
---|
| 125 | die "db corrupt: found $rows matches on $uri\n";
|
---|
| 126 | }
|
---|
| 127 | $dbh->commit;
|
---|
| 128 | return $rows;
|
---|
| 129 | } # end report()
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | sub exists {
|
---|
| 133 | my $self = shift;
|
---|
| 134 | my $checkme = shift;
|
---|
| 135 |
|
---|
| 136 | my $sth = $dbh->prepare("SELECT count(*) FROM urilist WHERE uri=?");
|
---|
| 137 | $sth->execute($checkme);
|
---|
| 138 | my ($count) = $sth->fetchrow_array;
|
---|
| 139 | return $count;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | # Write the data straight to a DNS server's files.
|
---|
| 144 | # Takes a server type and open filehandle.
|
---|
| 145 | sub export {
|
---|
| 146 | my $self = shift;
|
---|
| 147 | my $target = shift;
|
---|
| 148 | my $blfh = shift;
|
---|
| 149 |
|
---|
| 150 | my $sth;
|
---|
| 151 |
|
---|
| 152 | if ($target eq 'rbldnsd') {
|
---|
| 153 | print $blfh ":127.0.0.2:Domain found in reported missed-spam, no legitimate root or www content\n";
|
---|
| 154 | $sth = $dbh->prepare("SELECT uri FROM urilist WHERE list=2");
|
---|
| 155 | $sth->execute;
|
---|
| 156 | while (my ($uri) = $sth->fetchrow_array) {
|
---|
| 157 | print $blfh ".$uri\n";
|
---|
| 158 | }
|
---|
| 159 | print $blfh ":127.0.0.4:Domain seen repeatedly in reported missed-spam, legitimate root or www content\n";
|
---|
| 160 | $sth = $dbh->prepare("SELECT uri FROM urilist WHERE list=4");
|
---|
| 161 | $sth->execute;
|
---|
| 162 | while (my ($uri) = $sth->fetchrow_array) {
|
---|
| 163 | print $blfh ".$uri\n";
|
---|
| 164 | }
|
---|
| 165 | print $blfh ":127.0.0.8:Abused, not well-known URL-shortener or redirector domain\n";
|
---|
| 166 | $sth = $dbh->prepare("SELECT uri FROM urilist WHERE list=8");
|
---|
| 167 | $sth->execute;
|
---|
| 168 | while (my ($uri) = $sth->fetchrow_array) {
|
---|
| 169 | print $blfh ".$uri\n";
|
---|
| 170 | }
|
---|
| 171 | } elsif ($target eq 'tinydns') {
|
---|
| 172 | } elsif ($target eq 'bind') {
|
---|
| 173 | } else {
|
---|
| 174 | }
|
---|
| 175 | return;
|
---|
| 176 | } # end export()
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | # make Perl happy
|
---|
| 180 | 1;
|
---|