]>
Commit | Line | Data |
---|---|---|
70df8302 MT |
1 | ############################################################################### |
2 | # # | |
3 | # IPFire.org - A linux based firewall # | |
4 | # Copyright (C) 2007 Michael Tremer & Christian Schmidt # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | ############################################################################### | |
20 | ||
ebb9187c MT |
21 | |
22 | use strict; | |
23 | # enable only the following on debugging purpose | |
24 | #use warnings; | |
25 | ||
26 | require '/var/ipfire/general-functions.pl'; | |
27 | ||
28 | my %outfwsettings = (); | |
29 | my %checked = (); | |
30 | my %selected= () ; | |
31 | my %netsettings = (); | |
32 | my $errormessage = ""; | |
33 | my $configentry = ""; | |
34 | my @configs = (); | |
35 | my @configline = (); | |
36 | my $p2pentry = ""; | |
37 | my @p2ps = (); | |
38 | my @p2pline = (); | |
b4f8d26c | 39 | my @proto = (); |
ebb9187c | 40 | my $CMD = ""; |
b4f8d26c MT |
41 | my $P2PSTRING = ""; |
42 | ||
ebb9187c MT |
43 | my $DEBUG = 0; |
44 | ||
45 | my $configfile = "/var/ipfire/outgoing/rules"; | |
46 | my $p2pfile = "/var/ipfire/outgoing/p2protocols"; | |
47 | ||
48 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
49 | ||
50 | ### Values that have to be initialized | |
51 | $outfwsettings{'ACTION'} = ''; | |
52 | $outfwsettings{'VALID'} = 'yes'; | |
53 | $outfwsettings{'EDIT'} = 'no'; | |
54 | $outfwsettings{'NAME'} = ''; | |
55 | $outfwsettings{'SNET'} = ''; | |
56 | $outfwsettings{'SIP'} = ''; | |
57 | $outfwsettings{'SPORT'} = ''; | |
58 | $outfwsettings{'SMAC'} = ''; | |
59 | $outfwsettings{'DIP'} = ''; | |
60 | $outfwsettings{'DPORT'} = ''; | |
61 | $outfwsettings{'PROT'} = ''; | |
62 | $outfwsettings{'STATE'} = ''; | |
63 | $outfwsettings{'DISPLAY_DIP'} = ''; | |
64 | $outfwsettings{'DISPLAY_DPORT'} = ''; | |
65 | $outfwsettings{'DISPLAY_SMAC'} = ''; | |
66 | $outfwsettings{'DISPLAY_SIP'} = ''; | |
67 | $outfwsettings{'POLICY'} = 'MODE0'; | |
68 | my $SOURCE = ""; | |
69 | my $DESTINATION = ""; | |
70 | my $PROTO = ""; | |
71 | my $DPORT = ""; | |
72 | my $DEV = ""; | |
73 | my $MAC = ""; | |
74 | my $POLICY = ""; | |
75 | my $DO = ""; | |
76 | ||
77 | # read files | |
78 | &General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings); | |
79 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
80 | ||
81 | open( FILE, "< $configfile" ) or die "Unable to read $configfile"; | |
82 | @configs = <FILE>; | |
83 | close FILE; | |
84 | ||
b4f8d26c | 85 | if ( $outfwsettings{'POLICY'} eq 'MODE1' ) { |
ebb9187c MT |
86 | $outfwsettings{'STATE'} = "ALLOW"; |
87 | $POLICY = "DROP"; | |
4cb74dce | 88 | $DO = "ACCEPT"; |
ebb9187c MT |
89 | } elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) { |
90 | $outfwsettings{'STATE'} = "DENY"; | |
91 | $POLICY = "ACCEPT"; | |
92 | $DO = "DROP"; | |
93 | } | |
94 | ||
95 | ### Initialize IPTables | |
96 | system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1"); | |
97 | system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1"); | |
98 | system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1"); | |
99 | ||
b4f8d26c MT |
100 | if ( $outfwsettings{'POLICY'} eq 'MODE0' ) { |
101 | exit 0 | |
102 | } | |
103 | ||
104 | if ( $outfwsettings{'POLICY'} eq 'MODE1' ) { | |
4cb74dce | 105 | $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j ACCEPT"; |
b4f8d26c | 106 | if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); } |
4cb74dce | 107 | $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j ACCEPT"; |
b4f8d26c MT |
108 | if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); } |
109 | } | |
110 | ||
ebb9187c MT |
111 | foreach $configentry (sort @configs) |
112 | { | |
113 | $SOURCE = ""; | |
114 | $DESTINATION = ""; | |
115 | $PROTO = ""; | |
116 | $DPORT = ""; | |
117 | $DEV = ""; | |
118 | $MAC = ""; | |
119 | @configline = split( /\;/, $configentry ); | |
120 | if ($outfwsettings{'STATE'} eq $configline[0]) { | |
121 | if ($configline[2] eq 'green') { | |
122 | $SOURCE = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}"; | |
123 | $DEV = $netsettings{'GREEN_DEV'}; | |
124 | } elsif ($configline[2] eq 'blue') { | |
125 | $SOURCE = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}"; | |
126 | $DEV = $netsettings{'BLUE_DEV'}; | |
127 | } elsif ($configline[2] eq 'orange') { | |
128 | $SOURCE = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}"; | |
129 | $DEV = $netsettings{'ORANGE_DEV'}; | |
130 | } elsif ($configline[2] eq 'ip') { | |
131 | $SOURCE = "$configline[5]"; | |
132 | $DEV = ""; | |
133 | } else { | |
134 | $SOURCE = "0/0"; | |
135 | $DEV = ""; | |
136 | } | |
137 | ||
138 | if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; } | |
b4f8d26c MT |
139 | |
140 | if ($configline[3] eq 'tcp') { | |
141 | @proto = ("tcp"); | |
142 | } elsif ($configline[3] eq 'udp') { | |
143 | @proto = ("udp"); | |
144 | } else { | |
145 | @proto = ("tcp", "udp"); | |
146 | } | |
147 | ||
148 | foreach $PROTO (@proto) { | |
149 | $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION -p $PROTO"; | |
150 | ||
ebb9187c MT |
151 | if ($configline[8]) { |
152 | $DPORT = "$configline[8]"; | |
153 | $CMD = "$CMD --dport $DPORT"; | |
154 | } | |
b4f8d26c MT |
155 | |
156 | if ($DEV) { | |
157 | $CMD = "$CMD -i $DEV"; | |
158 | } | |
159 | ||
160 | if ($configline[6]) { | |
161 | $MAC = "$configline[6]"; | |
162 | $CMD = "$CMD -m mac --mac-source $MAC"; | |
163 | } | |
4cb74dce | 164 | |
b4f8d26c | 165 | $CMD = "$CMD -o $netsettings{'RED_DEV'}"; |
4cb74dce MT |
166 | |
167 | if ($configline[9] eq "aktiv") { | |
168 | if ($DEBUG) { | |
169 | print "$CMD -m state --state NEW -m limit --limit 10/minute -j LOG --log-prefix 'OUTGOINGFW '\n"; | |
170 | } else { | |
171 | system("$CMD -m state --state NEW -m limit --limit 10/minute -j LOG --log-prefix 'OUTGOINGFW '"); | |
172 | } | |
173 | } | |
174 | ||
b4f8d26c MT |
175 | if ($DEBUG) { |
176 | print "$CMD -j $DO\n"; | |
177 | } else { | |
178 | system("$CMD -j $DO"); | |
179 | } | |
b4f8d26c MT |
180 | } |
181 | } | |
182 | } | |
ebb9187c | 183 | |
b4f8d26c MT |
184 | ### Do the P2P-Stuff here |
185 | open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile"; | |
186 | @p2ps = <FILE>; | |
187 | close FILE; | |
ebb9187c | 188 | |
b4f8d26c | 189 | $CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p"; |
ebb9187c | 190 | |
b4f8d26c MT |
191 | foreach $p2pentry (sort @p2ps) |
192 | { | |
193 | @p2pline = split( /\;/, $p2pentry ); | |
194 | if ( $outfwsettings{'POLICY'} eq 'MODE2' ) { | |
195 | $DO = "DROP"; | |
196 | if ("$p2pline[2]" eq "off") { | |
197 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; | |
198 | } | |
199 | } else { | |
4cb74dce | 200 | $DO = "ACCEPT"; |
b4f8d26c MT |
201 | if ("$p2pline[2]" eq "on") { |
202 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; | |
ebb9187c | 203 | } |
b4f8d26c MT |
204 | } |
205 | } | |
206 | if ($P2PSTRING) { | |
207 | if ($DEBUG) { | |
208 | print "$CMD $P2PSTRING -j $DO\n"; | |
209 | } else { | |
210 | system("$CMD $P2PSTRING -j $DO"); | |
211 | } | |
212 | } | |
ebb9187c | 213 | |
b4f8d26c | 214 | if ( $outfwsettings{'POLICY'} eq 'MODE1' ) { |
4cb74dce | 215 | $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP"; |
b4f8d26c MT |
216 | if ($DEBUG) { |
217 | print "$CMD\n"; | |
218 | } else { | |
219 | system("$CMD"); | |
ebb9187c MT |
220 | } |
221 | } |