source: trunk/uribl/URIdb.pm@ 84

Last change on this file since 84 was 81, checked in by Kris Deugau, 5 days ago

/trunk/uribl

Corral some escaping syntax

  • Property svn:keywords set to Date Rev Author Id
File size: 5.7 KB
Line 
1# URIdb
2# Functions for interacting with the URI database
3##
4# $Id: URIdb.pm 81 2025-09-11 20:30:47Z kdeugau $
5# Copyright 2010,2025 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
21package URIdb;
22
23use strict;
24use warnings;
25use Exporter;
26use DBI;
27use NetAddr::IP;
28
29use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30
31$VERSION = 2.0;
32@ISA = qw(Exporter);
33@EXPORT_OK = qw( $dbh );
34
35@EXPORT = qw( $dbh );
36
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)
47our %status = (
48 0 => "Don't list",
49 2 => "Black",
50 4 => "Grey",
51 8 => "Abused URL shortener/redirector"
52);
53
54# variables
55our $dbh;
56
57our $err;
58our $errstr;
59
60# basic object subs
61sub new {
62 my $this = shift;
63 my $class = ref($this) || $this;
64 my %args = @_;
65
66 # Prepopulate a basic config. Note some of these *will* cause errors if left unset.
67 my %defconfig = (
68 dbhost => "localhost",
69 dbname => "dnsbl",
70 dbuser => "dnsbl",
71 dbpass => "spambgone",
72 );
73
74 my %siteconfig;
75 my $dbhost;
76 my $dbname;
77 my $dbuser;
78 my $dbpass;
79 if (defined($args{configfile})) {
80 if (-e $args{configfile} && -f $args{configfile}) {
81 my $ret = eval `cat $args{configfile}`;
82 unless ($ret) {
83 if ($@) { $errstr = "couldn't parse $args{configfile}: $@\n"; return; }
84 if (!defined($ret)) { $errstr = "couldn't load $args{configfile}: $!\n"; return; }
85 if (!$ret) { $errstr = "couldn't load $args{configfile}\n"; return; }
86 }
87 # crossload legacy variables, but prefer new %siteconfig values
88 $siteconfig{dbhost} = $dbhost if !$siteconfig{dbhost} && $dbhost;
89 $siteconfig{dbname} = $dbname if !$siteconfig{dbname} && $dbname;
90 $siteconfig{dbuser} = $dbuser if !$siteconfig{dbuser} && $dbuser;
91 $siteconfig{dbpass} = $dbpass if !$siteconfig{dbpass} && $dbpass;
92 }
93 }
94
95 # Assemble the object. Apply configuration hashes in order of precedence.
96 my $self = {
97 # Hardcoded defaults
98 %defconfig,
99 # Default config file OR caller-specified one, loaded above
100 %siteconfig,
101 # Caller-specified arguments
102 %args
103 };
104 bless $self, $class;
105
106 return $self;
107}
108
109sub DESTROY {
110 my $self = shift;
111 $self->dbclose() if $dbh;
112}
113
114# JIC someone wants to close the db but not finish the script
115sub dbclose {
116 $dbh->rollback;
117 $dbh->disconnect;
118}
119
120## specific object subs:
121
122sub connect {
123 my $self = shift;
124 ## want to NOT autocommit everything, it's unlikely we'll step on our own toes but...
125 $dbh = DBI->connect("DBI:Pg:host=$self->{dbhost};dbname=$self->{dbname}", $self->{dbuser}, $self->{dbpass}, {
126 AutoCommit => 0,
127 PrintError => 1
128 })
129 or die "database inaccessible: ".$DBI::errstr;
130# my $sth = $dbh->prepare("SELECT masklen,ipcount FROM autolist");
131# $sth->execute;
132# while (my ($masklen,$ipcount) = $sth->fetchrow_array) {
133# $autolist{$masklen} = $ipcount;
134# }
135 return $dbh;
136}
137
138
139# report a URI to the db
140# increments a hit counter iff the reported URI exists, otherwise it adds it
141# requires the uri.
142# can accept the listing level and a comment
143sub report {
144 my $self = shift;
145 my $uri = shift;
146 my $list = shift || 2;
147 my $comment = shift || '';
148
149 my $sth;
150 my $rows = 0;
151 $sth = $dbh->prepare("SELECT count FROM urilist WHERE uri=?");
152 $sth->execute($uri) or die "eep? ".$dbh->errstr."\n";
153 $rows = $sth->rows;
154 if ($rows == 0) {
155 $sth = $dbh->prepare("INSERT INTO urilist (uri,list,comment) VALUES (?,?,?)");
156 $sth->execute($uri,$list,$comment) or die "couldn't add $uri: ".$dbh->errstr."\n";
157 } elsif ($rows == 1) {
158 $sth = $dbh->prepare("UPDATE urilist SET count=count+1 WHERE uri=?");
159 $sth->execute($uri) or die "couldn't update listing for $uri: ".$dbh->errstr."\n";
160 } else {
161 die "db corrupt: found $rows matches on $uri\n";
162 }
163 $dbh->commit;
164 return $rows;
165} # end report()
166
167
168sub exists {
169 my $self = shift;
170 my $checkme = shift;
171
172 my $sth = $dbh->prepare("SELECT count(*) FROM urilist WHERE uri=?");
173 $sth->execute($checkme);
174 my ($count) = $sth->fetchrow_array;
175 return $count;
176}
177
178
179# Write the data straight to a DNS server's files.
180# Takes a server type and open filehandle.
181sub export {
182 my $self = shift;
183 my $target = shift;
184 my $blfh = shift;
185
186 my $sth;
187
188 if ($target eq 'rbldnsd') {
189 print $blfh ":127.0.0.2:Domain found in reported missed-spam, no legitimate root or www content\n";
190 $sth = $dbh->prepare("SELECT uri FROM urilist WHERE list=2");
191 $sth->execute;
192 while (my ($uri) = $sth->fetchrow_array) {
193 print $blfh ".$uri\n";
194 }
195 print $blfh ":127.0.0.4:Domain seen repeatedly in reported missed-spam, legitimate root or www content\n";
196 $sth = $dbh->prepare("SELECT uri FROM urilist WHERE list=4");
197 $sth->execute;
198 while (my ($uri) = $sth->fetchrow_array) {
199 print $blfh ".$uri\n";
200 }
201 print $blfh ":127.0.0.8:Abused, not well-known URL-shortener or redirector domain\n";
202 $sth = $dbh->prepare("SELECT uri FROM urilist WHERE list=8");
203 $sth->execute;
204 while (my ($uri) = $sth->fetchrow_array) {
205 print $blfh ".$uri\n";
206 }
207 } elsif ($target eq 'tinydns') {
208 } elsif ($target eq 'bind') {
209 } else {
210 }
211 return;
212} # end export()
213
214
215# make Perl happy
2161;
Note: See TracBrowser for help on using the repository browser.