Added all option to outgoingfw.
[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 } elsif ($configline[3] eq 'all') {
146 @proto = ("all");
147 } else {
148 @proto = ("tcp", "udp");
149 }
150
151 foreach $PROTO (@proto) {
152 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO";
153
154 if ($configline[8]) {
155 $DPORT = "$configline[8]";
156 $CMD = "$CMD --dport $DPORT";
157 }
158
159 if ($DEV) {
160 $CMD = "$CMD -i $DEV";
161 }
162
163 if ($configline[6]) {
164 $MAC = "$configline[6]";
165 $CMD = "$CMD -m mac --mac-source $MAC";
166 }
167
168 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
169
170 if ($configline[9] eq "aktiv") {
171 if ($DEBUG) {
172 print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'\n";
173 } else {
174 system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'");
175 }
176 }
177
178 if ($DEBUG) {
179 print "$CMD -j $DO\n";
180 } else {
181 system("$CMD -j $DO");
182 }
183 }
184 }
185 }
186
187 ### Do the P2P-Stuff here
188 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
189 @p2ps = <FILE>;
190 close FILE;
191
192 $CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p";
193
194 foreach $p2pentry (sort @p2ps)
195 {
196 @p2pline = split( /\;/, $p2pentry );
197 if ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
198 $DO = "DROP";
199 if ("$p2pline[2]" eq "off") {
200 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
201 }
202 } else {
203 $DO = "ACCEPT";
204 if ("$p2pline[2]" eq "on") {
205 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
206 }
207 }
208 }
209 if ($P2PSTRING) {
210 if ($DEBUG) {
211 print "$CMD $P2PSTRING -j $DO\n";
212 } else {
213 system("$CMD $P2PSTRING -j $DO");
214 }
215 }
216
217 if ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
218 if ( $outfwsettings{'MODE1LOG'} eq 'on' ) {
219 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'";
220 if ($DEBUG) {
221 print "$CMD\n";
222 } else {
223 system("$CMD");
224 }
225 }
226
227 $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW'";
228 if ($DEBUG) {
229 print "$CMD\n";
230 } else {
231 system("$CMD");
232 }
233 }