]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/urlfilter/redirect_wrapper
core81: set need reboot flag and restart apache.
[people/teissler/ipfire-2.x.git] / config / urlfilter / redirect_wrapper
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2012 IPFire Team <info@ipfire.org> #
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 # Based on Steffen Schoch (sschoch@users.sourceforge.net) #
21 # #
22 ###############################################################################
23
24 use IPC::Open2;
25 use IO::Handle;
26
27 require '/var/ipfire/general-functions.pl';
28
29 my %proxysettings=();
30 $proxysettings{'ENABLE_FILTER'} = 'off';
31 $proxysettings{'ENABLE_CLAMAV'} = 'off';
32 $proxysettings{'ENABLE_UPDXLRATOR'} = 'off';
33 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
34
35 # define here your redirectors
36 my @redirectors = ();
37
38 if ($proxysettings{'ENABLE_FILTER'} eq 'on') {
39 push(@redirectors, "/usr/bin/squidGuard");
40 }
41
42 if ($proxysettings{'ENABLE_CLAMAV'} eq 'on') {
43 push(@redirectors, "/usr/bin/squidclamav");
44 }
45
46 if ($proxysettings{'ENABLE_UPDXLRATOR'} eq 'on') {
47 push(@redirectors, "/usr/sbin/updxlrator");
48 }
49
50 # Attention: keep in mind that the order of your redirectors is important.
51 # It doesn't make sense to scan for viruses on pages you restrict access to...
52 # So place first your tools which restrict access, then the tools which do the
53 # content filtering!
54
55 ##### no need to change anything below this line #####
56
57 # init
58 $| = 1;
59 STDOUT->autoflush(1);
60
61 my $debug=0; # enable only for debugging
62
63 if (-e "/var/ipfire/proxy/enable_redirector_debug") {
64 writetolog("Urlfilter = ".$proxysettings{'ENABLE_FILTER'}." Clamav = ".$proxysettings{'ENABLE_CLAMAV'}." Updxlrator = ".$proxysettings{'ENABLE_UPDXLRATOR'});
65 $debug = 1;
66 }
67
68 # Open one instance for each redirector in the list and
69 # put them into an array with the STDIN and STDOUT file
70 # descriptors.
71 my @instances = ();
72
73 foreach my $redirector (@redirectors) {
74 my $desc_out = new IO::Handle();
75 my $desc_in = new IO::Handle();
76
77 my $pid = open2($desc_out, $desc_in, $redirector);
78
79 if ($debug) {
80 &writetolog("Started an instance of $redirector with PID $pid");
81 }
82
83 push(@instances, [$redirector, $desc_out, $desc_in]);
84 }
85
86 # wait for data...
87 my $line;
88 while ($line = <>) {
89 my $return = "ERR\n";
90
91 foreach my $instance (@instances) {
92 my $redirector = @$instance[0];
93 my $desc_out = @$instance[1];
94 my $desc_in = @$instance[2];
95 my $response;
96
97 # Send request to the redirector.
98 $desc_in->print($line);
99
100 # Wait for a response.
101 $response = $desc_out->getline;
102
103 # Catch invalid responses from squidGuard.
104 if ($redirector eq "/usr/bin/squidGuard" && $response eq "Processing file and database") {
105 system("logger -t ipfire 'Emergency - squidGuard not initialised please run squidGuard -C all'");
106 next;
107 }
108
109 # Writing debug output.
110 if ($debug) {
111 my $len_response = length($response);
112
113 &writetolog("Queried $redirector for: $line");
114 &writetolog(" --> Response ($len_response): $response");
115 }
116
117 # If we got a decisive response, we send it back to squid
118 # and stop querying any more redirectors.
119 if ($response =~ /^(OK|BH)/) {
120 if ($debug) {
121 &writetolog(" -- Stopped querying redirectors");
122 }
123
124 $return = $response;
125 last;
126 }
127 }
128
129 # Send response back to squid.
130 if ($debug) {
131 &writetolog("Sending back to squid: $return");
132 }
133 print $return;
134 }
135
136 exit 0;
137
138 sub writetolog {
139 my $message = shift;
140 chomp($message);
141
142 open(FILE, ">>/var/log/squid/redirector_debug") || die "Unable to acces file /var/log/squid/redirector_debug";
143 print FILE "$message\n";
144 close(FILE);
145 }