Fixed outgoing fw log messages in mode 1 and found a bug logging only
[people/pmueller/ipfire-2.x.git] / config / outgoingfw / outgoingfw.pl
CommitLineData
dba3aa2b 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
e4e42008 5# Copyright (C) 2007-2011 IPFire Team #
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###############################################################################
21
ebb9187c
MT
22
23use strict;
24# enable only the following on debugging purpose
25#use warnings;
26
27require '/var/ipfire/general-functions.pl';
e4e42008 28require "${General::swroot}/lang.pl";
ebb9187c
MT
29
30my %outfwsettings = ();
31my %checked = ();
32my %selected= () ;
33my %netsettings = ();
34my $errormessage = "";
35my $configentry = "";
36my @configs = ();
37my @configline = ();
38my $p2pentry = "";
39my @p2ps = ();
40my @p2pline = ();
ebb9187c 41my $CMD = "";
b4f8d26c
MT
42my $P2PSTRING = "";
43
ebb9187c
MT
44my $DEBUG = 0;
45
46my $configfile = "/var/ipfire/outgoing/rules";
47my $p2pfile = "/var/ipfire/outgoing/p2protocols";
48
ebb9187c
MT
49### Values that have to be initialized
50$outfwsettings{'ACTION'} = '';
51$outfwsettings{'VALID'} = 'yes';
52$outfwsettings{'EDIT'} = 'no';
53$outfwsettings{'NAME'} = '';
54$outfwsettings{'SNET'} = '';
55$outfwsettings{'SIP'} = '';
56$outfwsettings{'SPORT'} = '';
57$outfwsettings{'SMAC'} = '';
58$outfwsettings{'DIP'} = '';
59$outfwsettings{'DPORT'} = '';
60$outfwsettings{'PROT'} = '';
61$outfwsettings{'STATE'} = '';
62$outfwsettings{'DISPLAY_DIP'} = '';
63$outfwsettings{'DISPLAY_DPORT'} = '';
64$outfwsettings{'DISPLAY_SMAC'} = '';
65$outfwsettings{'DISPLAY_SIP'} = '';
66$outfwsettings{'POLICY'} = 'MODE0';
39008af7 67
fdeaa057 68my @SOURCE = "";
ebb9187c
MT
69my $SOURCE = "";
70my $DESTINATION = "";
fdeaa057 71my @PROTO = "";
ebb9187c
MT
72my $PROTO = "";
73my $DPORT = "";
74my $DEV = "";
75my $MAC = "";
76my $POLICY = "";
77my $DO = "";
39008af7 78my $DAY = "";
ebb9187c
MT
79
80# read files
81&General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
82&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
83
ed828642 84$netsettings{'RED_DEV'}=`cat /var/ipfire/red/iface`;
39008af7 85$netsettings{'RED_IP'}=`cat /var/ipfire/red/local-ipaddress`;
ed828642 86
ebb9187c
MT
87open( FILE, "< $configfile" ) or die "Unable to read $configfile";
88@configs = <FILE>;
89close FILE;
90
b4f8d26c 91if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
ebb9187c
MT
92 $outfwsettings{'STATE'} = "ALLOW";
93 $POLICY = "DROP";
4cb74dce 94 $DO = "ACCEPT";
ebb9187c
MT
95} elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
96 $outfwsettings{'STATE'} = "DENY";
97 $POLICY = "ACCEPT";
d9716b06 98 $DO = "DROP -m comment --comment 'DROP_OUTGOINGFW '";
ebb9187c
MT
99}
100
101### Initialize IPTables
102system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
103system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
104system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
105
d9716b06
CS
106system("/sbin/iptables --flush OUTGOINGFWMAC >/dev/null 2>&1");
107system("/sbin/iptables --delete-chain OUTGOINGFWMAC >/dev/null 2>&1");
108system("/sbin/iptables -N OUTGOINGFWMAC >/dev/null 2>&1");
109
b4f8d26c
MT
110if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
111 exit 0
112}
113
114if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
4cb74dce 115 $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j ACCEPT";
d9716b06
CS
116 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
117 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m state --state ESTABLISHED,RELATED -j ACCEPT";
b4f8d26c 118 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
4cb74dce 119 $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j ACCEPT";
b4f8d26c 120 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
d9716b06
CS
121 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -p icmp -j ACCEPT";
122 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
b4f8d26c
MT
123}
124
ebb9187c
MT
125foreach $configentry (sort @configs)
126{
fdeaa057 127 @SOURCE = "";
ebb9187c
MT
128 $DESTINATION = "";
129 $PROTO = "";
130 $DPORT = "";
131 $DEV = "";
132 $MAC = "";
133 @configline = split( /\;/, $configentry );
fdeaa057 134
ebb9187c
MT
135 if ($outfwsettings{'STATE'} eq $configline[0]) {
136 if ($configline[2] eq 'green') {
fdeaa057 137 @SOURCE = ("$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}");
ebb9187c 138 $DEV = $netsettings{'GREEN_DEV'};
39008af7 139 } elsif ($configline[2] eq 'red') {
fdeaa057 140 @SOURCE = ("$netsettings{'RED_IP'}");
39008af7 141 $DEV = "";
ebb9187c 142 } elsif ($configline[2] eq 'blue') {
fdeaa057 143 @SOURCE = ("$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}");
ebb9187c
MT
144 $DEV = $netsettings{'BLUE_DEV'};
145 } elsif ($configline[2] eq 'orange') {
fdeaa057 146 @SOURCE = ("$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}");
ebb9187c 147 $DEV = $netsettings{'ORANGE_DEV'};
fdeaa057
CS
148 } elsif ($configline[2] eq 'ipsec') {
149 @SOURCE = "";
150 $DEV = "ipsec+";
151 } elsif ($configline[2] eq 'ovpn') {
152 @SOURCE = "";
153 $DEV = "tun+";
ebb9187c 154 } elsif ($configline[2] eq 'ip') {
fdeaa057
CS
155 @SOURCE = ("$configline[5]");
156 $DEV = "";
d9716b06
CS
157 } elsif ($configline[2] eq 'mac') {
158 @SOURCE = ("$configline[6]");
159 $DEV = "";
70248550
CS
160 } elsif ($configline[2] eq 'all') {
161 @SOURCE = ("0/0");
162 $DEV = "";
fdeaa057 163 } else {
d9716b06 164 if ( -e "/var/ipfire/outgoing/groups/ipgroups/$configline[2]" ) {
fdeaa057 165 @SOURCE = `cat /var/ipfire/outgoing/groups/ipgroups/$configline[2]`;
d9716b06
CS
166 } elsif ( -e "/var/ipfire/outgoing/groups/macgroups/$configline[2]" ) {
167 @SOURCE = `cat /var/ipfire/outgoing/groups/macgroups/$configline[2]`;
bd4ea3c2 168 $configline[2] = "mac";
fdeaa057 169 }
ebb9187c
MT
170 $DEV = "";
171 }
172
173 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
d9716b06 174
b4f8d26c 175 if ($configline[3] eq 'tcp') {
fdeaa057 176 @PROTO = ("tcp");
b4f8d26c 177 } elsif ($configline[3] eq 'udp') {
fdeaa057 178 @PROTO = ("udp");
c791bb2a 179 } elsif ($configline[3] eq 'esp') {
fdeaa057 180 @PROTO = ("esp");
c791bb2a 181 } elsif ($configline[3] eq 'gre') {
fdeaa057 182 @PROTO = ("gre");
b4f8d26c 183 } else {
fdeaa057 184 @PROTO = ("tcp","udp");
b4f8d26c 185 }
4cb74dce 186
fdeaa057
CS
187 foreach $PROTO (@PROTO){
188 foreach $SOURCE (@SOURCE) {
189 $SOURCE =~ s/\s//gi;
190
68dd6726 191 if ( $SOURCE eq "" || $configline[1] eq "" ){next;}
fdeaa057 192
70248550 193 if ( ( $configline[6] ne "" || $configline[2] eq 'mac' ) && $configline[2] ne 'all'){
d9716b06
CS
194 $SOURCE =~ s/[^a-zA-Z0-9]/:/gi;
195 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m mac --mac-source $SOURCE -d $DESTINATION -p $PROTO";
196 } else {
197 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO";
198 }
fdeaa057
CS
199
200 if ($configline[8] && ( $configline[3] ne 'esp' || $configline[3] ne 'gre') ) {
201 $DPORT = "$configline[8]";
202 $CMD = "$CMD -m multiport --destination-port $DPORT";
203 }
204
205 if ($DEV) {
206 $CMD = "$CMD -i $DEV";
207 }
208
fdeaa057
CS
209 if ($configline[17] && $configline[18]) {
210 if ($configline[10]){$DAY = "Mon,"}
211 if ($configline[11]){$DAY .= "Tue,"}
212 if ($configline[12]){$DAY .= "Wed,"}
213 if ($configline[13]){$DAY .= "Thu,"}
214 if ($configline[14]){$DAY .= "Fri,"}
215 if ($configline[15]){$DAY .= "Sat,"}
216 if ($configline[16]){$DAY .= "Sun"}
217 $CMD = "$CMD -m time --timestart $configline[17] --timestop $configline[18] --weekdays $DAY";
218 }
219
220 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
221
e4e42008
CS
222 if ( $configline[9] eq $Lang::tr{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE1' ) {
223 if ($DEBUG) {
224 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '\n";
225 } else {
226 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '");
227 }
228 } elsif ( $configline[9] eq $Lang::tr{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE2' ) {
fdeaa057 229 if ($DEBUG) {
d92a7932 230 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '\n";
fdeaa057 231 } else {
d92a7932 232 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '");
fdeaa057
CS
233 }
234 }
235
4cb74dce 236 if ($DEBUG) {
fdeaa057 237 print "$CMD -j $DO\n";
4cb74dce 238 } else {
fdeaa057 239 system("$CMD -j $DO");
4cb74dce
MT
240 }
241 }
fdeaa057 242 }
b4f8d26c
MT
243 }
244}
ebb9187c 245
b4f8d26c
MT
246### Do the P2P-Stuff here
247open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
248@p2ps = <FILE>;
249close FILE;
ebb9187c 250
b4f8d26c 251$CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p";
ebb9187c 252
b4f8d26c
MT
253foreach $p2pentry (sort @p2ps)
254{
255 @p2pline = split( /\;/, $p2pentry );
256 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
257 $DO = "DROP";
258 if ("$p2pline[2]" eq "off") {
259 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
260 }
261 } else {
4cb74dce 262 $DO = "ACCEPT";
b4f8d26c
MT
263 if ("$p2pline[2]" eq "on") {
264 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
ebb9187c 265 }
b4f8d26c
MT
266 }
267}
268if ($P2PSTRING) {
269 if ($DEBUG) {
270 print "$CMD $P2PSTRING -j $DO\n";
271 } else {
272 system("$CMD $P2PSTRING -j $DO");
273 }
274}
ebb9187c 275
b4f8d26c 276if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
52802b5f 277 if ( $outfwsettings{'MODE1LOG'} eq 'on' ) {
d92a7932 278 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '";
52802b5f
CS
279 if ($DEBUG) {
280 print "$CMD\n";
281 } else {
282 system("$CMD");
283 }
284 }
285
d9716b06 286 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW '";
b4f8d26c
MT
287 if ($DEBUG) {
288 print "$CMD\n";
289 } else {
290 system("$CMD");
ebb9187c 291 }
d9716b06 292}