]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/urlfilter/redirect_wrapper
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / config / urlfilter / redirect_wrapper
index 7f66cd89dc4ca6c51dd69dcb7b5f85674b9fc39b..3e8b49eacd945c7f2be81b3e39b0eb3e7051e9a8 100644 (file)
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
+# Copyright (C) 2007-2012  IPFire Team  <info@ipfire.org>                     #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
 #                                                                             #
 ###############################################################################
 
-use strict;
 use IPC::Open2;
 use IO::Handle;
 
-# define here your redirectors (use a comma sperated list)
-my $redirectors = [ '/usr/bin/squidGuard', '/usr/sbin/updxlrator' ];
-
 require '/var/ipfire/general-functions.pl';
 
 my %proxysettings=();
@@ -36,57 +32,114 @@ $proxysettings{'ENABLE_CLAMAV'} = 'off';
 $proxysettings{'ENABLE_UPDXLRATOR'} = 'off';
 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
 
-# define here your redirectors (use a comma sperated list)
-if ( $proxysettings{'ENABLE_FILTER'} eq 'on' && $proxysettings{'ENABLE_CLAMAV'} eq 'on' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'on' ){my $redirectors = [ '/usr/bin/squidGuard', '/usr/bin/squidclamav', '/usr/sbin/updxlrator' ];}
-if ( $proxysettings{'ENABLE_FILTER'} eq 'on' && $proxysettings{'ENABLE_CLAMAV'} eq 'on' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'off' ){my $redirectors = [ '/usr/bin/squidGuard', '/usr/bin/squidclamav' ];}
-if ( $proxysettings{'ENABLE_FILTER'} eq 'on' && $proxysettings{'ENABLE_CLAMAV'} eq 'off' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'on' ){my $redirectors = [ '/usr/bin/squidGuard', '/usr/sbin/updxlrator' ];}
-if ( $proxysettings{'ENABLE_FILTER'} eq 'on' && $proxysettings{'ENABLE_CLAMAV'} eq 'off' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'off' ){my $redirectors = [ '/usr/bin/squidGuard' ];}
-if ( $proxysettings{'ENABLE_FILTER'} eq 'off' && $proxysettings{'ENABLE_CLAMAV'} eq 'on' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'on' ){my $redirectors = [ '/usr/bin/squidclamav', '/usr/sbin/updxlrator' ];}
-if ( $proxysettings{'ENABLE_FILTER'} eq 'off' && $proxysettings{'ENABLE_CLAMAV'} eq 'on' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'off' ){my $redirectors = [ '/usr/bin/squidclamav' ];}
-if ( $proxysettings{'ENABLE_FILTER'} eq 'off' && $proxysettings{'ENABLE_CLAMAV'} eq 'off' && $proxysettings{'ENABLE_UPDXLRATOR'} eq 'on' ){my $redirectors = [ '/usr/sbin/updxlrator' ];}
+# define here your redirectors
+my @redirectors = ();
+
+if ($proxysettings{'ENABLE_FILTER'} eq 'on') {
+       push(@redirectors, "/usr/bin/squidGuard");
+}
+
+if ($proxysettings{'ENABLE_CLAMAV'} eq 'on') {
+       push(@redirectors, "/usr/bin/squidclamav");
+}
+
+if ($proxysettings{'ENABLE_UPDXLRATOR'} eq 'on') {
+       push(@redirectors, "/usr/sbin/updxlrator");
+}
 
 # Attention: keep in mind that the order of your redirectors is important.
 # It doesn't make sense to scan for viruses on pages you restrict access to...
 # So place first your tools which restrict access, then the tools which do the
 # content filtering!
 
-
 ##### no need to change anything below this line #####
 
 # init
 $| = 1;
 STDOUT->autoflush(1);
-my $line;
-my $return;
-my $i;
+
 my $debug=0; # enable only for debugging
 
-# open progamms
-my $pidlist = [];
-my $rlist = [];
-my $wlist = [];
-for($i = 0; $i < @$redirectors; $i++) {
-        $pidlist->[$i] = open2($rlist->[$i], $wlist->[$i], $redirectors->[$i]);
+if (-e "/var/ipfire/proxy/enable_redirector_debug") {
+       writetolog("Urlfilter = ".$proxysettings{'ENABLE_FILTER'}." Clamav = ".$proxysettings{'ENABLE_CLAMAV'}." Updxlrator = ".$proxysettings{'ENABLE_UPDXLRATOR'});
+       $debug = 1;
+}
+
+# Open one instance for each redirector in the list and
+# put them into an array with the STDIN and STDOUT file
+# descriptors.
+my @instances = ();
+
+foreach my $redirector (@redirectors) {
+       my $desc_out = new IO::Handle();
+       my $desc_in  = new IO::Handle();
+
+       my $pid = open2($desc_out, $desc_in, $redirector);
+
+       if ($debug) {
+               &writetolog("Started an instance of $redirector with PID $pid");
+       }
+
+       push(@instances, [$redirector, $desc_out, $desc_in]);
 }
 
 # wait for data...
-while($line = <>) {
-        for($i = 0; $i < @$redirectors; $i++) {
-                $wlist->[$i]->print($line);
-                $return = $rlist->[$i]->getline;
-                last if($return ne "\n" and $return ne $line);
-                                                               # break if redirector changes data
-        }
-        print $return;
-
-                               if ($debug){
-                chomp $line;
-                chomp $return;
-                if ( $line ne $return ){
-                open(DATEI, ">>/var/log/squid/redirector_debug") || die "Unable to acces file /var/log/redirector_debug";
-                                        print DATEI "Requested ".$line."\nAnswer    ".$return."\n";
-                                        close(DATEI);
-                                        }
-                               }
+my $line;
+while ($line = <>) {
+       my $return = "ERR\n";
+
+       foreach my $instance (@instances) {
+               my $redirector = @$instance[0];
+               my $desc_out   = @$instance[1];
+               my $desc_in    = @$instance[2];
+               my $response;
+
+               # Send request to the redirector.
+               $desc_in->print($line);
+
+               # Wait for a response.
+               $response = $desc_out->getline;
+
+               # Catch invalid responses from squidGuard.
+               if ($redirector eq "/usr/bin/squidGuard" && $response eq "Processing file and database") {
+                       system("logger -t ipfire 'Emergency - squidGuard not initialised please run squidGuard -C all'");
+                       next;
+               }
+
+               # Writing debug output.
+               if ($debug) {
+                       my $len_response = length($response);
+
+                       &writetolog("Queried $redirector for: $line");
+                       &writetolog("  --> Response ($len_response): $response");
+               }
+
+               # If we got a decisive response, we send it back to squid
+               # and stop querying any more redirectors.
+               if ($response =~ /^(OK|BH)/) {
+                       if ($debug) {
+                               &writetolog("  --  Stopped querying redirectors");
+                       }
+
+                       $return = $response;
+                       last;
+               }
+       }
+
+       # Send response back to squid.
+       if ($debug) {
+               &writetolog("Sending back to squid: $return");
+       }
+       print $return;
 }
+
 exit 0;
+
+sub writetolog {
+       my $message = shift;
+       chomp($message);
+
+       open(FILE, ">>/var/log/squid/redirector_debug") || die "Unable to acces file /var/log/squid/redirector_debug";
+       print FILE "$message\n";
+       close(FILE);
+}