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