]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/outgoingfw/outgoingfw.pl
nochmal kleine Anpassung am Logwatch ggf fehlt noch der Confiy Symlink
[people/pmueller/ipfire-2.x.git] / config / outgoingfw / outgoingfw.pl
CommitLineData
70df8302
MT
1###############################################################################
2# #
3# IPFire.org - A linux based firewall #
4# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19###############################################################################
20
ebb9187c
MT
21
22use strict;
23# enable only the following on debugging purpose
24#use warnings;
25
26require '/var/ipfire/general-functions.pl';
27
28my %outfwsettings = ();
29my %checked = ();
30my %selected= () ;
31my %netsettings = ();
32my $errormessage = "";
33my $configentry = "";
34my @configs = ();
35my @configline = ();
36my $p2pentry = "";
37my @p2ps = ();
38my @p2pline = ();
b4f8d26c 39my @proto = ();
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
48&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49
50### Values that have to be initialized
51$outfwsettings{'ACTION'} = '';
52$outfwsettings{'VALID'} = 'yes';
53$outfwsettings{'EDIT'} = 'no';
54$outfwsettings{'NAME'} = '';
55$outfwsettings{'SNET'} = '';
56$outfwsettings{'SIP'} = '';
57$outfwsettings{'SPORT'} = '';
58$outfwsettings{'SMAC'} = '';
59$outfwsettings{'DIP'} = '';
60$outfwsettings{'DPORT'} = '';
61$outfwsettings{'PROT'} = '';
62$outfwsettings{'STATE'} = '';
63$outfwsettings{'DISPLAY_DIP'} = '';
64$outfwsettings{'DISPLAY_DPORT'} = '';
65$outfwsettings{'DISPLAY_SMAC'} = '';
66$outfwsettings{'DISPLAY_SIP'} = '';
67$outfwsettings{'POLICY'} = 'MODE0';
68my $SOURCE = "";
69my $DESTINATION = "";
70my $PROTO = "";
71my $DPORT = "";
72my $DEV = "";
73my $MAC = "";
74my $POLICY = "";
75my $DO = "";
76
77# read files
78&General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
79&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
80
81open( FILE, "< $configfile" ) or die "Unable to read $configfile";
82@configs = <FILE>;
83close FILE;
84
b4f8d26c 85if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
ebb9187c
MT
86 $outfwsettings{'STATE'} = "ALLOW";
87 $POLICY = "DROP";
4cb74dce 88 $DO = "ACCEPT";
ebb9187c
MT
89} elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
90 $outfwsettings{'STATE'} = "DENY";
91 $POLICY = "ACCEPT";
92 $DO = "DROP";
93}
94
95### Initialize IPTables
96system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
97system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
98system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
99
b4f8d26c
MT
100if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
101 exit 0
102}
103
104if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
4cb74dce 105 $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j ACCEPT";
b4f8d26c 106 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
4cb74dce 107 $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j ACCEPT";
b4f8d26c
MT
108 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
109}
110
ebb9187c
MT
111foreach $configentry (sort @configs)
112{
113 $SOURCE = "";
114 $DESTINATION = "";
115 $PROTO = "";
116 $DPORT = "";
117 $DEV = "";
118 $MAC = "";
119 @configline = split( /\;/, $configentry );
120 if ($outfwsettings{'STATE'} eq $configline[0]) {
121 if ($configline[2] eq 'green') {
122 $SOURCE = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
123 $DEV = $netsettings{'GREEN_DEV'};
124 } elsif ($configline[2] eq 'blue') {
125 $SOURCE = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
126 $DEV = $netsettings{'BLUE_DEV'};
127 } elsif ($configline[2] eq 'orange') {
128 $SOURCE = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
129 $DEV = $netsettings{'ORANGE_DEV'};
130 } elsif ($configline[2] eq 'ip') {
131 $SOURCE = "$configline[5]";
132 $DEV = "";
133 } else {
134 $SOURCE = "0/0";
135 $DEV = "";
136 }
137
138 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
b4f8d26c
MT
139
140 if ($configline[3] eq 'tcp') {
141 @proto = ("tcp");
142 } elsif ($configline[3] eq 'udp') {
143 @proto = ("udp");
144 } else {
145 @proto = ("tcp", "udp");
146 }
147
148 foreach $PROTO (@proto) {
149 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO";
150
ebb9187c
MT
151 if ($configline[8]) {
152 $DPORT = "$configline[8]";
153 $CMD = "$CMD --dport $DPORT";
154 }
b4f8d26c
MT
155
156 if ($DEV) {
157 $CMD = "$CMD -i $DEV";
158 }
159
160 if ($configline[6]) {
161 $MAC = "$configline[6]";
162 $CMD = "$CMD -m mac --mac-source $MAC";
163 }
4cb74dce 164
b4f8d26c 165 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
4cb74dce
MT
166
167 if ($configline[9] eq "aktiv") {
168 if ($DEBUG) {
169 print "$CMD -m state --state NEW -m limit --limit 10/minute -j LOG --log-prefix 'OUTGOINGFW '\n";
170 } else {
171 system("$CMD -m state --state NEW -m limit --limit 10/minute -j LOG --log-prefix 'OUTGOINGFW '");
172 }
173 }
174
b4f8d26c
MT
175 if ($DEBUG) {
176 print "$CMD -j $DO\n";
177 } else {
178 system("$CMD -j $DO");
179 }
b4f8d26c
MT
180 }
181 }
182}
ebb9187c 183
b4f8d26c
MT
184### Do the P2P-Stuff here
185open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
186@p2ps = <FILE>;
187close FILE;
ebb9187c 188
b4f8d26c 189$CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p";
ebb9187c 190
b4f8d26c
MT
191foreach $p2pentry (sort @p2ps)
192{
193 @p2pline = split( /\;/, $p2pentry );
194 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
195 $DO = "DROP";
196 if ("$p2pline[2]" eq "off") {
197 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
198 }
199 } else {
4cb74dce 200 $DO = "ACCEPT";
b4f8d26c
MT
201 if ("$p2pline[2]" eq "on") {
202 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
ebb9187c 203 }
b4f8d26c
MT
204 }
205}
206if ($P2PSTRING) {
207 if ($DEBUG) {
208 print "$CMD $P2PSTRING -j $DO\n";
209 } else {
210 system("$CMD $P2PSTRING -j $DO");
211 }
212}
ebb9187c 213
b4f8d26c 214if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
4cb74dce 215 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP";
b4f8d26c
MT
216 if ($DEBUG) {
217 print "$CMD\n";
218 } else {
219 system("$CMD");
ebb9187c
MT
220 }
221}