]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/outgoingfw/outgoingfw.pl
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2011 IPFire Team #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
27 require '/var/ipfire/general-functions.pl';
28 require "${General::swroot}/lang.pl";
30 my %outfwsettings = ();
34 my $errormessage = "";
46 my $configfile = "/var/ipfire/outgoing/rules";
47 my $p2pfile = "/var/ipfire/outgoing/p2protocols";
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';
81 &General
::readhash
("${General::swroot}/outgoing/settings", \
%outfwsettings);
82 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
84 $netsettings{'RED_DEV'}=`cat /var/ipfire/red/iface`;
85 $netsettings{'RED_IP'}=`cat /var/ipfire/red/local-ipaddress`;
87 open( FILE
, "< $configfile" ) or die "Unable to read $configfile";
91 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
92 $outfwsettings{'STATE'} = "ALLOW";
95 } elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
96 $outfwsettings{'STATE'} = "DENY";
98 $DO = "DROP -m comment --comment 'DROP_OUTGOINGFW '";
101 ### Initialize IPTables
102 system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
103 system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
104 system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
106 system("/sbin/iptables --flush OUTGOINGFWMAC >/dev/null 2>&1");
107 system("/sbin/iptables --delete-chain OUTGOINGFWMAC >/dev/null 2>&1");
108 system("/sbin/iptables -N OUTGOINGFWMAC >/dev/null 2>&1");
110 if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
114 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
115 $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j ACCEPT";
116 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
117 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m state --state ESTABLISHED,RELATED -j ACCEPT";
118 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
119 $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j ACCEPT";
120 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
121 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -p icmp -j ACCEPT";
122 if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); }
125 foreach $configentry (sort @configs)
133 @configline = split( /\;/, $configentry );
135 if ($outfwsettings{'STATE'} eq $configline[0]) {
136 if ($configline[2] eq 'green') {
137 @SOURCE = ("$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}");
138 $DEV = $netsettings{'GREEN_DEV'};
139 } elsif ($configline[2] eq 'red') {
140 @SOURCE = ("$netsettings{'RED_IP'}");
142 } elsif ($configline[2] eq 'blue') {
143 @SOURCE = ("$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}");
144 $DEV = $netsettings{'BLUE_DEV'};
145 } elsif ($configline[2] eq 'orange') {
146 @SOURCE = ("$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}");
147 $DEV = $netsettings{'ORANGE_DEV'};
148 } elsif ($configline[2] eq 'ipsec') {
151 } elsif ($configline[2] eq 'ovpn') {
154 } elsif ($configline[2] eq 'ip') {
155 @SOURCE = ("$configline[5]");
157 } elsif ($configline[2] eq 'mac') {
158 @SOURCE = ("$configline[6]");
160 } elsif ($configline[2] eq 'all') {
164 if ( -e
"/var/ipfire/outgoing/groups/ipgroups/$configline[2]" ) {
165 @SOURCE = `cat /var/ipfire/outgoing/groups/ipgroups/$configline[2]`;
166 } elsif ( -e
"/var/ipfire/outgoing/groups/macgroups/$configline[2]" ) {
167 @SOURCE = `cat /var/ipfire/outgoing/groups/macgroups/$configline[2]`;
168 $configline[2] = "mac";
173 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
175 if ($configline[3] eq 'tcp') {
177 } elsif ($configline[3] eq 'udp') {
179 } elsif ($configline[3] eq 'esp') {
181 } elsif ($configline[3] eq 'gre') {
184 @PROTO = ("tcp","udp");
187 foreach $PROTO (@PROTO){
188 foreach $SOURCE (@SOURCE) {
191 if ( $SOURCE eq "" || $configline[1] eq "" ){next;}
193 if ( ( $configline[6] ne "" || $configline[2] eq 'mac' ) && $configline[2] ne 'all'){
194 $SOURCE =~ s/[^a-zA-Z0-9]/:/gi;
195 $CMD = "/sbin/iptables -A OUTGOINGFWMAC -m mac --mac-source $SOURCE -d $DESTINATION -p $PROTO";
197 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO";
200 if ($configline[8] && ( $configline[3] ne 'esp' || $configline[3] ne 'gre') ) {
201 $DPORT = "$configline[8]";
202 $CMD = "$CMD -m multiport --destination-port $DPORT";
206 $CMD = "$CMD -i $DEV";
209 if ($configline[17] && $configline[18]) {
211 if ($configline[10]){$DAY = "Mon,"}
212 if ($configline[11]){$DAY .= "Tue,"}
213 if ($configline[12]){$DAY .= "Wed,"}
214 if ($configline[13]){$DAY .= "Thu,"}
215 if ($configline[14]){$DAY .= "Fri,"}
216 if ($configline[15]){$DAY .= "Sat,"}
217 if ($configline[16]){$DAY .= "Sun"}
218 $CMD = "$CMD -m time --timestart $configline[17] --timestop $configline[18] --weekdays $DAY";
221 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
223 if ( $configline[9] eq $Lang::tr
{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE1' ) {
225 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '\n";
227 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'LOG_OUTGOINGFW '");
229 } elsif ( $configline[9] eq $Lang::tr
{'aktiv'} && $outfwsettings{'POLICY'} eq 'MODE2' ) {
231 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '\n";
233 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '");
238 print "$CMD -j $DO\n";
240 system("$CMD -j $DO");
247 ### Do the P2P-Stuff here
248 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
252 $CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p";
254 foreach $p2pentry (sort @p2ps)
256 @p2pline = split( /\;/, $p2pentry );
257 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
259 if ("$p2pline[2]" eq "off") {
260 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
264 if ("$p2pline[2]" eq "on") {
265 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
271 print "$CMD $P2PSTRING -j $DO\n";
273 system("$CMD $P2PSTRING -j $DO");
277 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
278 if ( $outfwsettings{'MODE1LOG'} eq 'on' ) {
279 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW '";
287 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW '";