]>
Commit | Line | Data |
---|---|---|
dba3aa2b | 1 | #!/usr/bin/perl |
70df8302 MT |
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 | ||
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 = (); | |
b4f8d26c | 40 | my @proto = (); |
ebb9187c | 41 | my $CMD = ""; |
b4f8d26c MT |
42 | my $P2PSTRING = ""; |
43 | ||
ebb9187c MT |
44 | my $DEBUG = 0; |
45 | ||
46 | my $configfile = "/var/ipfire/outgoing/rules"; | |
47 | my $p2pfile = "/var/ipfire/outgoing/p2protocols"; | |
48 | ||
ebb9187c MT |
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 | ||
ed828642 CS |
80 | $netsettings{'RED_DEV'}=`cat /var/ipfire/red/iface`; |
81 | ||
ebb9187c MT |
82 | open( FILE, "< $configfile" ) or die "Unable to read $configfile"; |
83 | @configs = <FILE>; | |
84 | close FILE; | |
85 | ||
b4f8d26c | 86 | if ( $outfwsettings{'POLICY'} eq 'MODE1' ) { |
ebb9187c MT |
87 | $outfwsettings{'STATE'} = "ALLOW"; |
88 | $POLICY = "DROP"; | |
4cb74dce | 89 | $DO = "ACCEPT"; |
ebb9187c MT |
90 | } elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) { |
91 | $outfwsettings{'STATE'} = "DENY"; | |
92 | $POLICY = "ACCEPT"; | |
a332b303 | 93 | $DO = "DROP -m comment --comment 'DROP_OUTGOINGFW'"; |
ebb9187c MT |
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 | ||
b4f8d26c MT |
101 | if ( $outfwsettings{'POLICY'} eq 'MODE0' ) { |
102 | exit 0 | |
103 | } | |
104 | ||
105 | if ( $outfwsettings{'POLICY'} eq 'MODE1' ) { | |
4cb74dce | 106 | $CMD = "/sbin/iptables -A OUTGOINGFW -m state --state ESTABLISHED,RELATED -j ACCEPT"; |
b4f8d26c | 107 | if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); } |
4cb74dce | 108 | $CMD = "/sbin/iptables -A OUTGOINGFW -p icmp -j ACCEPT"; |
b4f8d26c MT |
109 | if ($DEBUG) { print "$CMD\n"; } else { system("$CMD"); } |
110 | } | |
111 | ||
ebb9187c MT |
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"; } | |
b4f8d26c MT |
140 | |
141 | if ($configline[3] eq 'tcp') { | |
142 | @proto = ("tcp"); | |
143 | } elsif ($configline[3] eq 'udp') { | |
144 | @proto = ("udp"); | |
29423370 CS |
145 | } elsif ($configline[3] eq 'all') { |
146 | @proto = ("all"); | |
b4f8d26c MT |
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 | ||
ebb9187c MT |
154 | if ($configline[8]) { |
155 | $DPORT = "$configline[8]"; | |
156 | $CMD = "$CMD --dport $DPORT"; | |
157 | } | |
b4f8d26c MT |
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 | } | |
4cb74dce | 167 | |
b4f8d26c | 168 | $CMD = "$CMD -o $netsettings{'RED_DEV'}"; |
4cb74dce MT |
169 | |
170 | if ($configline[9] eq "aktiv") { | |
171 | if ($DEBUG) { | |
52485124 | 172 | print "$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'\n"; |
4cb74dce | 173 | } else { |
52485124 | 174 | system("$CMD -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'"); |
4cb74dce MT |
175 | } |
176 | } | |
177 | ||
b4f8d26c MT |
178 | if ($DEBUG) { |
179 | print "$CMD -j $DO\n"; | |
180 | } else { | |
181 | system("$CMD -j $DO"); | |
182 | } | |
b4f8d26c MT |
183 | } |
184 | } | |
185 | } | |
ebb9187c | 186 | |
b4f8d26c MT |
187 | ### Do the P2P-Stuff here |
188 | open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile"; | |
189 | @p2ps = <FILE>; | |
190 | close FILE; | |
ebb9187c | 191 | |
b4f8d26c | 192 | $CMD = "/sbin/iptables -A OUTGOINGFW -m ipp2p"; |
ebb9187c | 193 | |
b4f8d26c MT |
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 { | |
4cb74dce | 203 | $DO = "ACCEPT"; |
b4f8d26c MT |
204 | if ("$p2pline[2]" eq "on") { |
205 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; | |
ebb9187c | 206 | } |
b4f8d26c MT |
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 | } | |
ebb9187c | 216 | |
b4f8d26c | 217 | if ( $outfwsettings{'POLICY'} eq 'MODE1' ) { |
52802b5f | 218 | if ( $outfwsettings{'MODE1LOG'} eq 'on' ) { |
52485124 | 219 | $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -m limit --limit 10/minute -j LOG --log-prefix 'DROP_OUTGOINGFW'"; |
52802b5f CS |
220 | if ($DEBUG) { |
221 | print "$CMD\n"; | |
222 | } else { | |
223 | system("$CMD"); | |
224 | } | |
225 | } | |
226 | ||
a332b303 | 227 | $CMD = "/sbin/iptables -A OUTGOINGFW -o $netsettings{'RED_DEV'} -j DROP -m comment --comment 'DROP_OUTGOINGFW'"; |
b4f8d26c MT |
228 | if ($DEBUG) { |
229 | print "$CMD\n"; | |
230 | } else { | |
231 | system("$CMD"); | |
ebb9187c MT |
232 | } |
233 | } |