| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | # Import a tinydns-formatted URI blacklist
 | 
|---|
| 3 | ##
 | 
|---|
| 4 | # $Id: import-cur.pl 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 | ##
 | 
|---|
| 20 | 
 | 
|---|
| 21 | use strict;
 | 
|---|
| 22 | use warnings;
 | 
|---|
| 23 | use DBI;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | use URIdb;
 | 
|---|
| 26 | 
 | 
|---|
| 27 | my $uridb = new URIdb;
 | 
|---|
| 28 | 
 | 
|---|
| 29 | # default DB info - all other settings should be loaded from the DB.
 | 
|---|
| 30 | my $dbhost = "localhost";
 | 
|---|
| 31 | my $dbname = "uridb";
 | 
|---|
| 32 | my $dbuser = "uridb";
 | 
|---|
| 33 | my $dbpass = "spambgone";
 | 
|---|
| 34 | 
 | 
|---|
| 35 | # Load a config ref containing DB host, name, user, and pass info based on
 | 
|---|
| 36 | # from the server name + full script web path.  This allows us to host
 | 
|---|
| 37 | # multiple instances without having to duplicate the code.
 | 
|---|
| 38 | # This file is a Perl fragment to be processed inline.
 | 
|---|
| 39 | if (-e "/etc/uridb/uridb.conf") {
 | 
|---|
| 40 |   my $cfg = `cat /etc/uridb/uridb.conf`;
 | 
|---|
| 41 |   ($cfg) = ($cfg =~ /^(.+)$/s);         # avoid warnings, failures, and general nastiness with taint mode
 | 
|---|
| 42 |   eval $cfg;
 | 
|---|
| 43 | }
 | 
|---|
| 44 | 
 | 
|---|
| 45 | my $dbh = $uridb->connect($dbhost, $dbname, $dbuser, $dbpass);
 | 
|---|
| 46 | 
 | 
|---|
| 47 | my $datesth = $dbh->prepare("UPDATE urilist SET added=? WHERE uri=?");
 | 
|---|
| 48 | 
 | 
|---|
| 49 | my %config;
 | 
|---|
| 50 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
 | 
|---|
| 51 | $sth->execute;
 | 
|---|
| 52 | while (my ($key,$value) = $sth->fetchrow_array) {
 | 
|---|
| 53 |   $config{$key} = $value;
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | my @months = ('null','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
 | 
|---|
| 57 | 
 | 
|---|
| 58 | my $uridate = 'Tue Nov  9 16:26:29 EST 2010';
 | 
|---|
| 59 | my $uricomment;
 | 
|---|
| 60 | 
 | 
|---|
| 61 | #my $count = 0;
 | 
|---|
| 62 | while (<>) {
 | 
|---|
| 63 |   next if /^\s*$/;
 | 
|---|
| 64 |   if (/^#/) {
 | 
|---|
| 65 |     $uricomment = '';
 | 
|---|
| 66 |     if (/^# (\w\w\w \w\w\w  ?\d\d?  ?\d\d?:\d\d:\d\d \w\w\w \d{4})\s*$/) {
 | 
|---|
| 67 |       $uridate = $1;
 | 
|---|
| 68 |       print "setting date on following URIs to $uridate\n";
 | 
|---|
| 69 |     } elsif (m|^# (\d\d\d\d)/(\d\d)/(\d\d?) ?(\d\d:\d\d:?\d?\d?)?$|) {
 | 
|---|
| 70 |       $uridate = "$months[$2] $3 $1";
 | 
|---|
| 71 |       $uridate .= ($4 ? " $4" : " 10:30:42");
 | 
|---|
| 72 |       print "setting date on following URIs to $uridate\n";
 | 
|---|
| 73 |     } else {
 | 
|---|
| 74 |       chomp;
 | 
|---|
| 75 |       s/^#\s+//;
 | 
|---|
| 76 |       $uricomment = $_;
 | 
|---|
| 77 |       print "setting comment on next URI to $uricomment\n";
 | 
|---|
| 78 |     }
 | 
|---|
| 79 |     next;
 | 
|---|
| 80 |   }
 | 
|---|
| 81 |   chomp;
 | 
|---|
| 82 |   s/^\+//;
 | 
|---|
| 83 |   my ($domain,$list,$rest) = split /:/;
 | 
|---|
| 84 |   ($list) = ($list =~ /\.(\d+)$/);
 | 
|---|
| 85 |   $domain =~ s/\.uribl\.company\.com$//;
 | 
|---|
| 86 |   if (!$uridb->exists($domain)) {
 | 
|---|
| 87 |     print "adding $domain, $list\n";
 | 
|---|
| 88 |     $uridb->report($domain, $list, $uricomment);
 | 
|---|
| 89 |     $datesth->execute($uridate, $domain) or die "bad date-foo: ".$dbh->errstr."\n";
 | 
|---|
| 90 |     $dbh->commit;
 | 
|---|
| 91 |   } else {
 | 
|---|
| 92 |     print "$domain exists, not adding\n";
 | 
|---|
| 93 |   }
 | 
|---|
| 94 |   $uricomment = '';
 | 
|---|
| 95 | #  last if $count++ > 1000;
 | 
|---|
| 96 | }
 | 
|---|