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