8bb49e0bd3fd2d9d789d729a68b78aaeda5b4ff5
[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-2011 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 require "${General::swroot}/lang.pl";
29
30 my %outfwsettings = ();
31 my %checked = ();
32 my %selected= () ;
33 my %netsettings = ();
34 my $errormessage = "";
35 my $configentry = "";
36 my @configs = ();
37 my @configline = ();
38 my $p2pentry = "";
39 my @p2ps = ();
40 my @p2pline = ();
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
68 my @SOURCE = "";
69 my $SOURCE = "";
70 my $DESTINATION = "";
71 my @PROTO = "";
72 my $PROTO = "";
73 my $DPORT = "";
74 my $DEV = "";
75 my $MAC = "";
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 $DO = "RETURN";
93 } elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
94 $outfwsettings{'STATE'} = "DENY";
95 $DO = "DROP -m comment --comment 'DROP_OUTGOINGFW '";
96 }
97
98 ### Initialize IPTables
99 system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
100 system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
101 system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
102
103 system("/sbin/iptables --flush OUTGOINGFWMAC >/dev/null 2>&1");
104 system("/sbin/iptables --delete-chain OUTGOINGFWMAC >/dev/null 2>&1");
105 system("/sbin/iptables -N OUTGOINGFWMAC >/dev/null 2>&1");
106
107 if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
108 exit 0
109 }
110
111 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
112 $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j RETURN";
113 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
114 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m state --state ESTABLISHED,RELATED -j RETURN";
115 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
116 $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j RETURN";
117 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
118 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -p icmp -j RETURN";
119 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
120 }
121
122 foreach $configentry (sort @configs)
123 {
124 @SOURCE = "";
125 $DESTINATION = "";
126 $PROTO = "";
127 $DPORT = "";
128 $DEV = "";
129 $MAC = "";
130 @configline = split( /\;/, $configentry );
131
132 if ($outfwsettings{'STATE'} eq $configline[0]) {
133 if ($configline[2] eq 'green') {
134 @SOURCE = ("$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}");
135 $DEV = $netsettings{'GREEN_DEV'};
136 } elsif ($configline[2] eq 'red') {
137 @SOURCE = ("$netsettings{'RED_IP'}");
138 $DEV = "";
139 } elsif ($configline[2] eq 'blue') {
140 @SOURCE = ("$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}");
141 $DEV = $netsettings{'BLUE_DEV'};
142 } elsif ($configline[2] eq 'orange') {
143 @SOURCE = ("$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}");
144 $DEV = $netsettings{'ORANGE_DEV'};
145 } elsif ($configline[2] eq 'ipsec') {
146 @SOURCE = "";
147 $DEV = "ipsec+";
148 } elsif ($configline[2] eq 'ovpn') {
149 @SOURCE = "";
150 $DEV = "tun+";
151 } elsif ($configline[2] eq 'ip') {
152 @SOURCE = ("$configline[5]");
153 $DEV = "";
154 } elsif ($configline[2] eq 'mac') {
155 @SOURCE = ("$configline[6]");
156 $DEV = "";
157 } elsif ($configline[2] eq 'all') {
158 @SOURCE = ("0/0");
159 $DEV = "";
160 } else {
161 if ( -e "/var/ipfire/outgoing/groups/ipgroups/$configline[2]" ) {
162 @SOURCE = `cat /var/ipfire/outgoing/groups/ipgroups/$configline[2]`;
163 } elsif ( -e "/var/ipfire/outgoing/groups/macgroups/$configline[2]" ) {
164 @SOURCE = `cat /var/ipfire/outgoing/groups/macgroups/$configline[2]`;
165 $configline[2] = "mac";
166 }
167 $DEV = "";
168 }
169
170 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
171
172 if ($configline[3] eq 'tcp') {
173 @PROTO = ("tcp");
174 } elsif ($configline[3] eq 'udp') {
175 @PROTO = ("udp");
176 } elsif ($configline[3] eq 'esp') {
177 @PROTO = ("esp");
178 } elsif ($configline[3] eq 'gre') {
179 @PROTO = ("gre");
180 } else {
181 @PROTO = ("tcp","udp");
182 }
183
184 foreach $PROTO (@PROTO){
185 foreach $SOURCE (@SOURCE) {
186 $SOURCE =~ s/\s//gi;
187
188 if ( $SOURCE eq "" || $configline[1] eq "" ){next;}
189
190 if ( ( $configline[6] ne "" || $configline[2] eq 'mac' ) && $configline[2] ne 'all'){
191 $SOURCE =~ s/[^a-zA-Z0-9]/:/gi;
192 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m mac --mac-source $SOURCE -d $DESTINATION -p $PROTO";
193 } else {
194 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO";
195 }
196
197 if ($configline[8] && ( $configline[3] ne 'esp' || $configline[3] ne 'gre') ) {
198 $DPORT = "$configline[8]";
199 $CMD = "$CMD -m multiport --destination-port $DPORT";
200 }
201
202 if ($DEV) {
203 $CMD = "$CMD -i $DEV";
204 }
205
206 if ($configline[17] && $configline[18]) {
207 $DAY = "";
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 $Lang::tr{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE1' ) {
221 if ($DEBUG) {
222 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '\n";
223 } else {
224 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '");
225 }
226 } elsif ( $configline[9] eq $Lang::tr{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE2' ) {
227 if ($DEBUG) {
228 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '\n";
229 } else {
230 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '");
231 }
232 }
233
234 if ($DEBUG) {
235 print "$CMD -j $DO\n";
236 } else {
237 system("$CMD -j $DO");
238 }
239 }
240 }
241 }
242 }
243
244 ### Do the P2P-Stuff here
245 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
246 @p2ps = <FILE>;
247 close FILE;
248
249 $CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p";
250
251 foreach $p2pentry (sort @p2ps)
252 {
253 @p2pline = split( /\;/, $p2pentry );
254 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
255 $DO = "DROP";
256 if ("$p2pline[2]" eq "off") {
257 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
258 }
259 } else {
260 $DO = "RETURN";
261 if ("$p2pline[2]" eq "on") {
262 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
263 }
264 }
265 }
266 if ($P2PSTRING) {
267 if ($DEBUG) {
268 print "$CMD $P2PSTRING -j $DO\n";
269 } else {
270 system("$CMD $P2PSTRING -j $DO");
271 }
272 }
273
274 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
275 if ( $outfwsettings{'MODE1LOG'} eq 'on' ) {
276 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '";
277 if ($DEBUG) {
278 print "$CMD\n";
279 } else {
280 system("$CMD");
281 }
282 }
283
284 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW '";
285 if ($DEBUG) {
286 print "$CMD\n";
287 } else {
288 system("$CMD");
289 }
290 }