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