]>
Commit | Line | Data |
---|---|---|
a393e0ba | 1 | #!/usr/bin/perl |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
88d73bef | 5 | # Copyright (C) 2007-2012 IPFire Team <info@ipfire.org> # |
70df8302 MT |
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 | ############################################################################### | |
a393e0ba | 23 | |
a393e0ba MT |
24 | use IPC::Open2; |
25 | use IO::Handle; | |
ff5ec02a | 26 | |
d12aede7 CS |
27 | require '/var/ipfire/general-functions.pl'; |
28 | ||
29 | my %proxysettings=(); | |
e5a63a6f CS |
30 | $proxysettings{'ENABLE_FILTER'} = 'off'; |
31 | $proxysettings{'ENABLE_CLAMAV'} = 'off'; | |
32 | $proxysettings{'ENABLE_UPDXLRATOR'} = 'off'; | |
d12aede7 | 33 | &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); |
a393e0ba | 34 | |
7119032e MT |
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 | } | |
a393e0ba MT |
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 | ||
a393e0ba MT |
55 | ##### no need to change anything below this line ##### |
56 | ||
57 | # init | |
58 | $| = 1; | |
59 | STDOUT->autoflush(1); | |
7119032e | 60 | |
ff5ec02a | 61 | my $debug=0; # enable only for debugging |
a393e0ba | 62 | |
7119032e | 63 | if (-e "/var/ipfire/proxy/enable_redirector_debug") { |
0acb39d3 | 64 | writetolog("Urlfilter = ".$proxysettings{'ENABLE_FILTER'}." Clamav = ".$proxysettings{'ENABLE_CLAMAV'}." Updxlrator = ".$proxysettings{'ENABLE_UPDXLRATOR'}); |
7119032e MT |
65 | $debug = 1; |
66 | } | |
e775d06c | 67 | |
7119032e MT |
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 = (); | |
0acb39d3 | 72 | |
7119032e MT |
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"); | |
0acb39d3 | 81 | } |
a393e0ba | 82 | |
7119032e MT |
83 | push(@instances, [$redirector, $desc_out, $desc_in]); |
84 | } | |
85 | ||
a393e0ba | 86 | # wait for data... |
7119032e MT |
87 | my $line; |
88 | while ($line = <>) { | |
89 | my $return = "ERR\n"; | |
0acb39d3 | 90 | |
7119032e MT |
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; | |
0acb39d3 | 96 | |
7119032e MT |
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") { | |
9ce08b2a | 105 | system("logger -t ipfire 'Emergency - squidGuard not initialised please run squidGuard -C all'"); |
7119032e MT |
106 | next; |
107 | } | |
ff5ec02a | 108 | |
7119032e MT |
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 | } | |
0acb39d3 | 116 | |
7119032e MT |
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"); | |
0acb39d3 | 122 | } |
7119032e MT |
123 | |
124 | $return = $response; | |
125 | last; | |
0ba563aa | 126 | } |
0acb39d3 CS |
127 | } |
128 | ||
7119032e MT |
129 | # Send response back to squid. |
130 | if ($debug) { | |
131 | &writetolog("Sending back to squid: $return"); | |
132 | } | |
133 | print $return; | |
134 | } | |
135 | ||
a393e0ba | 136 | exit 0; |
0ba563aa CS |
137 | |
138 | sub writetolog { | |
7119032e MT |
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 | } |