| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | # Web UI for adding URIs to blacklist
 | 
|---|
| 3 | ##
 | 
|---|
| 4 | # $Id: uridb.cgi 80 2025-09-11 20:28:23Z 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 | 
 | 
|---|
| 21 | use strict;
 | 
|---|
| 22 | use warnings;
 | 
|---|
| 23 | no warnings qw(uninitialized);
 | 
|---|
| 24 | 
 | 
|---|
| 25 | use CGI::Carp qw (fatalsToBrowser);
 | 
|---|
| 26 | use CGI::Simple;
 | 
|---|
| 27 | use HTML::Template;
 | 
|---|
| 28 | use Encode;
 | 
|---|
| 29 | 
 | 
|---|
| 30 | # push "the directory the script is in" into @INC
 | 
|---|
| 31 | use FindBin;
 | 
|---|
| 32 | use lib "$FindBin::RealBin/";
 | 
|---|
| 33 | 
 | 
|---|
| 34 | use URIdb 2.0;
 | 
|---|
| 35 | 
 | 
|---|
| 36 | # Set up the CGI object...
 | 
|---|
| 37 | my $q = new CGI::Simple;
 | 
|---|
| 38 | # ... and get query-string params as well as POST params if necessary
 | 
|---|
| 39 | $q->parse_query_string;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | my %webvar;
 | 
|---|
| 42 | # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
 | 
|---|
| 43 | foreach ($q->param()) {
 | 
|---|
| 44 |   $webvar{$_} = $q->param($_);
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | print $q->header(-charset=>'utf8');
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #my %status = (0 => "Don't list",
 | 
|---|
| 50 | #       2 => "Black",
 | 
|---|
| 51 | #       4 => "Grey",
 | 
|---|
| 52 | #       8 => "Abused URL shortener/redirector"
 | 
|---|
| 53 | #       );
 | 
|---|
| 54 | 
 | 
|---|
| 55 | # Load a config ref containing DB host, name, user, and pass info based on
 | 
|---|
| 56 | # from the server name + full script web path.  This allows us to host
 | 
|---|
| 57 | # multiple instances without having to duplicate the code.
 | 
|---|
| 58 | # This file is a Perl fragment to be processed inline.
 | 
|---|
| 59 | my $cfgname = $ENV{SERVER_NAME}.$ENV{REQUEST_URI};
 | 
|---|
| 60 | $cfgname =~ s|[./-]|_|g;
 | 
|---|
| 61 | $cfgname =~ s|_uridb_cgi.*||;
 | 
|---|
| 62 | $cfgname =~ s|_$||;
 | 
|---|
| 63 | 
 | 
|---|
| 64 | my $uridb = new URIdb (configfile => "/etc/uridb/$cfgname.conf");
 | 
|---|
| 65 | 
 | 
|---|
| 66 | my $page;
 | 
|---|
| 67 | 
 | 
|---|
| 68 | my $cgiself = $ENV{SCRIPT_FILENAME};
 | 
|---|
| 69 | $cgiself =~ s|.+/([^/]+\.cgi)$|$1|;
 | 
|---|
| 70 | 
 | 
|---|
| 71 | my $templatedir = $ENV{SCRIPT_FILENAME};
 | 
|---|
| 72 | $templatedir =~ s/$cgiself//;
 | 
|---|
| 73 | $templatedir .= "templates";
 | 
|---|
| 74 | $ENV{HTML_TEMPLATE_ROOT} = $templatedir;
 | 
|---|
| 75 | 
 | 
|---|
| 76 | my %config;
 | 
|---|
| 77 | my $sth = $dbh->prepare("SELECT key,value FROM misc");
 | 
|---|
| 78 | $sth->execute;
 | 
|---|
| 79 | while (my ($key,$value) = $sth->fetchrow_array) {
 | 
|---|
| 80 |   $config{$key} = $value;
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | # decide which page to spit out...
 | 
|---|
| 84 | if (!$webvar{page}) {
 | 
|---|
| 85 |   $page = HTML::Template->new(filename => "index.tmpl");
 | 
|---|
| 86 | } else {
 | 
|---|
| 87 |   $page = HTML::Template->new(filename => "$webvar{page}.tmpl");
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | $page->param(pgtitle => $config{pgtitle}) if defined($config{pgtitle});
 | 
|---|
| 91 | $page->param(pgcomment => $config{pgcomment}) if defined($config{pgcomment});
 | 
|---|
| 92 | $page->param(cgiself => $cgiself);
 | 
|---|
| 93 | 
 | 
|---|
| 94 | my $uridbsiteroot = $ENV{REQUEST_URI};
 | 
|---|
| 95 | $uridbsiteroot =~ s|/$cgiself\?.+|/|;
 | 
|---|
| 96 | $page->param(uridbsiteroot => $uridbsiteroot);
 | 
|---|
| 97 | 
 | 
|---|
| 98 | if ($webvar{page} eq 'report') {
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   my @dombase = split /\n/m, $webvar{domlist};
 | 
|---|
| 101 |   my @domlist;
 | 
|---|
| 102 |   my $domcount = 0;
 | 
|---|
| 103 |   foreach my $domain (@dombase) {
 | 
|---|
| 104 |     my %row;
 | 
|---|
| 105 |     ($domain) = split /:/, $domain;
 | 
|---|
| 106 |     $domain =~ s/^\+//;
 | 
|---|
| 107 |     $domain =~ s/\.uribl.company.com//;
 | 
|---|
| 108 |     chomp $domain;
 | 
|---|
| 109 |     # now, see if it's multilisted
 | 
|---|
| 110 |     if ($domain =~ / on (\w+)\s*$/) {
 | 
|---|
| 111 |       my $sub = $1;
 | 
|---|
| 112 |       #$domain =~ s/ on \w+\s*$//;
 | 
|---|
| 113 |       $domlist[$domcount-1]->{otherlists} .= ",$sub";
 | 
|---|
| 114 |       next;
 | 
|---|
| 115 |     }
 | 
|---|
| 116 |     $row{domindex} = $domcount++;
 | 
|---|
| 117 |     $row{domain} = $domain;
 | 
|---|
| 118 |     push @domlist, \%row;
 | 
|---|
| 119 |   }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   $page->param(domlist => \@domlist);
 | 
|---|
| 122 | 
 | 
|---|
| 123 | } elsif ($webvar{page} eq 'dbreport') {
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   my $err = '';
 | 
|---|
| 126 |   my @domlist;
 | 
|---|
| 127 |   my $i = 0;
 | 
|---|
| 128 |   while (defined ($webvar{"dom$i"})) {
 | 
|---|
| 129 |     my $key = "dom$i";
 | 
|---|
| 130 |     my %row;
 | 
|---|
| 131 |     $row{domain} = $webvar{$key};
 | 
|---|
| 132 |     $row{listnum} = $webvar{$key."type"};
 | 
|---|
| 133 |     $row{listtext} = $URIdb::status{$webvar{$key."type"}};
 | 
|---|
| 134 |     $row{comment} = $webvar{$key."comment"};
 | 
|---|
| 135 |     if ($webvar{$key."type"} != 0) {
 | 
|---|
| 136 |       $uridb->report($webvar{$key},$webvar{$key."type"},$webvar{$key."comment"});
 | 
|---|
| 137 |     }
 | 
|---|
| 138 |     push @domlist, \%row;
 | 
|---|
| 139 |     $i++;
 | 
|---|
| 140 |   }
 | 
|---|
| 141 |   $page->param(domlist => \@domlist);
 | 
|---|
| 142 |   $page->param(err => $err);
 | 
|---|
| 143 | 
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | print $page->output;
 | 
|---|
| 147 | 
 | 
|---|
| 148 | exit 0;
 | 
|---|