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