source: trunk/uribl/URIdb.pm@ 27

Last change on this file since 27 was 27, checked in by Kris Deugau, 14 years ago

/trunk/uribl

Add URI blacklist database interface code

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