]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/outgoingfw/outgoingfw.pl
Fixed outgoingfw logging in DROP_ format to be ignored by kernel filter
[people/pmueller/ipfire-2.x.git] / config / outgoingfw / outgoingfw.pl
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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
22
23 use strict;
24 # enable only the following on debugging purpose
25 #use warnings;
26
27 require '/var/ipfire/general-functions.pl';
28
29 my %outfwsettings = ();
30 my %checked = ();
31 my %selected= () ;
32 my %netsettings = ();
33 my $errormessage = "";
34 my $configentry = "";
35 my @configs = ();
36 my @configline = ();
37 my $p2pentry = "";
38 my @p2ps = ();
39 my @p2pline = ();
40 my @proto = ();
41 my $CMD = "";
42 my $P2PSTRING = "";
43
44 my $DEBUG = 0;
45
46 my $configfile = "/var/ipfire/outgoing/rules";
47 my $p2pfile = "/var/ipfire/outgoing/p2protocols";
48
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';
67 my $SOURCE = "";
68 my $DESTINATION = "";
69 my $PROTO = "";
70 my $DPORT = "";
71 my $DEV = "";
72 my $MAC = "";
73 my $POLICY = "";
74 my $DO = "";
75
76 # read files
77 &General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
78 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
79
80 $netsettings{'RED_DEV'}=`cat /var/ipfire/red/iface`;
81
82 open( FILE, "< $configfile" ) or die "Unable to read $configfile";
83 @configs = <FILE>;
84 close FILE;
85
86 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
87 $outfwsettings{'STATE'} = "ALLOW";
88 $POLICY = "DROP";
89 $DO = "ACCEPT";
90 } elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
91 $outfwsettings{'STATE'} = "DENY";
92 $POLICY = "ACCEPT";
93 $DO = "DROP -m comment --comment 'DROP_OUTGOINGFW'";
94 }
95
96 ### Initialize IPTables
97 system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
98 system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
99 system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
100
101 if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
102 exit 0
103 }
104
105 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
106 $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j ACCEPT";
107 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
108 $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j ACCEPT";
109 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
110 }
111
112 foreach $configentry (sort @configs)
113 {
114 $SOURCE = "";
115 $DESTINATION = "";
116 $PROTO = "";
117 $DPORT = "";
118 $DEV = "";
119 $MAC = "";
120 @configline = split( /\;/, $configentry );
121 if ($outfwsettings{'STATE'} eq $configline[0]) {
122 if ($configline[2] eq 'green') {
123 $SOURCE = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
124 $DEV = $netsettings{'GREEN_DEV'};
125 } elsif ($configline[2] eq 'blue') {
126 $SOURCE = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
127 $DEV = $netsettings{'BLUE_DEV'};
128 } elsif ($configline[2] eq 'orange') {
129 $SOURCE = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
130 $DEV = $netsettings{'ORANGE_DEV'};
131 } elsif ($configline[2] eq 'ip') {
132 $SOURCE = "$configline[5]";
133 $DEV = "";
134 } else {
135 $SOURCE = "0/0";
136 $DEV = "";
137 }
138
139 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
140
141 if ($configline[3] eq 'tcp') {
142 @proto = ("tcp");
143 } elsif ($configline[3] eq 'udp') {
144 @proto = ("udp");
145 } else {
146 @proto = ("tcp", "udp");
147 }
148
149 foreach $PROTO (@proto) {
150 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO";
151
152 if ($configline[8]) {
153 $DPORT = "$configline[8]";
154 $CMD = "$CMD --dport $DPORT";
155 }
156
157 if ($DEV) {
158 $CMD = "$CMD -i $DEV";
159 }
160
161 if ($configline[6]) {
162 $MAC = "$configline[6]";
163 $CMD = "$CMD -m mac --mac-source $MAC";
164 }
165
166 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
167
168 if ($configline[9] eq "aktiv") {
169 if ($DEBUG) {
170 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'\n";
171 } else {
172 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'");
173 }
174 }
175
176 if ($DEBUG) {
177 print "$CMD -j $DO\n";
178 } else {
179 system("$CMD -j $DO");
180 }
181 }
182 }
183 }
184
185 ### Do the P2P-Stuff here
186 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
187 @p2ps = <FILE>;
188 close FILE;
189
190 $CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p";
191
192 foreach $p2pentry (sort @p2ps)
193 {
194 @p2pline = split( /\;/, $p2pentry );
195 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
196 $DO = "DROP";
197 if ("$p2pline[2]" eq "off") {
198 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
199 }
200 } else {
201 $DO = "ACCEPT";
202 if ("$p2pline[2]" eq "on") {
203 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
204 }
205 }
206 }
207 if ($P2PSTRING) {
208 if ($DEBUG) {
209 print "$CMD $P2PSTRING -j $DO\n";
210 } else {
211 system("$CMD $P2PSTRING -j $DO");
212 }
213 }
214
215 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
216 if ( $outfwsettings{'MODE1LOG'} eq 'on' ) {
217 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'";
218 if ($DEBUG) {
219 print "$CMD\n";
220 } else {
221 system("$CMD");
222 }
223 }
224
225 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW'";
226 if ($DEBUG) {
227 print "$CMD\n";
228 } else {
229 system("$CMD");
230 }
231 }