]>
Commit | Line | Data |
---|---|---|
2a81ab0d AM |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2012 # | |
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 | # Hi folks! I hope this code is useful for all. I needed something to handle # | |
23 | # my VPN Connections in a comfortable way. # | |
24 | # This script builds firewallrules from the webinterface # | |
25 | ############################################################################### | |
26 | ||
2a81ab0d | 27 | use strict; |
472136c9 | 28 | use Time::Local; |
2a81ab0d AM |
29 | no warnings 'uninitialized'; |
30 | ||
31 | # enable only the following on debugging purpose | |
32 | #use warnings; | |
33 | #use CGI::Carp 'fatalsToBrowser'; | |
34 | ||
35 | my %fwdfwsettings=(); | |
36 | my %defaultNetworks=(); | |
37 | my %configfwdfw=(); | |
38 | my %color=(); | |
39 | my %icmptypes=(); | |
40 | my %ovpnSettings=(); | |
41 | my %customgrp=(); | |
42 | our %sourcehash=(); | |
43 | our %targethash=(); | |
44 | my @timeframe=(); | |
45 | my %configinputfw=(); | |
5d7faa45 | 46 | my %configoutgoingfw=(); |
a6edca5a | 47 | my %confignatfw=(); |
2a81ab0d AM |
48 | my %aliases=(); |
49 | my @DPROT=(); | |
36196d0d | 50 | my @p2ps=(); |
2a81ab0d AM |
51 | require '/var/ipfire/general-functions.pl'; |
52 | require "${General::swroot}/lang.pl"; | |
53 | require "${General::swroot}/forward/bin/firewall-lib.pl"; | |
54 | ||
55 | my $configfwdfw = "${General::swroot}/forward/config"; | |
56 | my $configinput = "${General::swroot}/forward/input"; | |
5d7faa45 | 57 | my $configoutgoing = "${General::swroot}/forward/outgoing"; |
36196d0d | 58 | my $p2pfile = "${General::swroot}/forward/p2protocols"; |
2a81ab0d | 59 | my $configgrp = "${General::swroot}/fwhosts/customgroups"; |
210ee67b | 60 | my $netsettings = "${General::swroot}/ethernet/settings"; |
2a81ab0d | 61 | my $errormessage=''; |
210ee67b AM |
62 | my $orange; |
63 | my $green; | |
6adcf156 | 64 | my $blue; |
2a81ab0d AM |
65 | my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT); |
66 | my $CHAIN="FORWARDFW"; | |
ddcec9d3 | 67 | my $conexists='off'; |
a6edca5a AM |
68 | my $command = 'iptables -A'; |
69 | my $dnat=''; | |
70 | my $snat=''; | |
2a81ab0d | 71 | &General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings); |
210ee67b | 72 | &General::readhash("$netsettings", \%defaultNetworks); |
2a81ab0d AM |
73 | &General::readhasharray($configfwdfw, \%configfwdfw); |
74 | &General::readhasharray($configinput, \%configinputfw); | |
5d7faa45 | 75 | &General::readhasharray($configoutgoing, \%configoutgoingfw); |
2a81ab0d AM |
76 | &General::readhasharray($configgrp, \%customgrp); |
77 | &General::get_aliases(\%aliases); | |
78 | ||
ddcec9d3 AM |
79 | #check if we have an internetconnection |
80 | open (CONN,"/var/ipfire/red/iface"); | |
81 | my $con = <CONN>; | |
82 | close(CONN); | |
83 | if (-f "/var/ipfire/red/active"){ | |
84 | $conexists='on'; | |
85 | } | |
a6edca5a AM |
86 | open (CONN1,"/var/ipfire/red/local-ipaddress"); |
87 | my $redip = <CONN1>; | |
88 | close(CONN1); | |
2a81ab0d AM |
89 | ################################ |
90 | # DEBUG/TEST # | |
91 | ################################ | |
54cb7ff0 | 92 | my $MODE=1; # 0 - normal operation |
2a81ab0d AM |
93 | # 1 - print configline and rules to console |
94 | # | |
95 | ################################ | |
96 | my $param=shift; | |
97 | ||
98 | if($param eq 'flush'){ | |
99 | if ($MODE eq '1'){ | |
100 | print " Flushing chains...\n"; | |
101 | } | |
102 | &flush; | |
103 | }else{ | |
104 | if ($MODE eq '1'){ | |
105 | print " Flushing chains...\n"; | |
106 | } | |
107 | &flush; | |
108 | if ($MODE eq '1'){ | |
109 | print " Preparing rules...\n"; | |
110 | } | |
111 | &preparerules; | |
112 | if($MODE eq '0'){ | |
113 | if ($fwdfwsettings{'POLICY'} eq 'MODE1'){ | |
af49e367 | 114 | &p2pblock; |
5d7faa45 | 115 | system ("/usr/sbin/firewall-policy"); |
2a81ab0d | 116 | }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){ |
6adcf156 | 117 | &p2pblock; |
b85d2a98 | 118 | system ("iptables -A $CHAIN -m conntrack --ctstate NEW -j ACCEPT"); |
5d7faa45 | 119 | system ("/usr/sbin/firewall-policy"); |
674f4e9d | 120 | system ("/etc/sysconfig/firewall.local reload"); |
2a81ab0d AM |
121 | } |
122 | } | |
123 | } | |
2a81ab0d AM |
124 | sub flush |
125 | { | |
126 | system ("iptables -F FORWARDFW"); | |
127 | system ("iptables -F INPUTFW"); | |
5d7faa45 | 128 | system ("iptables -F OUTGOINGFW"); |
28640b73 AM |
129 | system ("iptables -t nat -F NAT_DESTINATION"); |
130 | system ("iptables -t nat -F NAT_SOURCE"); | |
2a81ab0d AM |
131 | } |
132 | sub preparerules | |
133 | { | |
134 | if (! -z "${General::swroot}/forward/config"){ | |
135 | &buildrules(\%configfwdfw); | |
136 | } | |
137 | if (! -z "${General::swroot}/forward/input"){ | |
138 | &buildrules(\%configinputfw); | |
139 | } | |
5d7faa45 AM |
140 | if (! -z "${General::swroot}/forward/outgoing"){ |
141 | &buildrules(\%configoutgoingfw); | |
142 | } | |
2a81ab0d AM |
143 | } |
144 | sub buildrules | |
145 | { | |
146 | my $hash=shift; | |
b5269091 | 147 | my $STAG; |
a6edca5a AM |
148 | my $natip; |
149 | my $snatport; | |
150 | my $fireport; | |
bc912c6e | 151 | my $nat; |
98cee89f | 152 | my $fwaccessdport; |
c12392c0 | 153 | my $natchain; |
992394d5 | 154 | foreach my $key (sort {$a <=> $b} keys %$hash){ |
ff4770c7 | 155 | next if (($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1') && $conexists eq 'off' ); |
a6edca5a AM |
156 | if ($$hash{$key}[28] eq 'ON'){ |
157 | $command='iptables -t nat -A'; | |
08e1c65d | 158 | $natip=&get_nat_ip($$hash{$key}[29],$$hash{$key}[31]); |
a6edca5a | 159 | if($$hash{$key}[31] eq 'dnat'){ |
bc912c6e | 160 | $nat='DNAT'; |
98cee89f AM |
161 | if ($$hash{$key}[30] =~ /\|/){ |
162 | $$hash{$key}[30]=~ tr/|/,/; | |
163 | $fireport='-m multiport --dport '.$$hash{$key}[30]; | |
164 | }else{ | |
165 | $fireport='--dport '.$$hash{$key}[30] if ($$hash{$key}[30]>0); | |
166 | } | |
a6edca5a | 167 | }else{ |
bc912c6e | 168 | $nat='SNAT'; |
a6edca5a AM |
169 | } |
170 | } | |
b5269091 | 171 | $STAG=''; |
2a81ab0d AM |
172 | if($$hash{$key}[2] eq 'ON'){ |
173 | #get source ip's | |
174 | if ($$hash{$key}[3] eq 'cust_grp_src'){ | |
992394d5 | 175 | foreach my $grp (sort {$a <=> $b} keys %customgrp){ |
2a81ab0d AM |
176 | if($customgrp{$grp}[0] eq $$hash{$key}[4]){ |
177 | &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src"); | |
178 | } | |
179 | } | |
180 | }else{ | |
181 | &get_address($$hash{$key}[3],$$hash{$key}[4],"src"); | |
182 | } | |
183 | #get target ip's | |
184 | if ($$hash{$key}[5] eq 'cust_grp_tgt'){ | |
992394d5 | 185 | foreach my $grp (sort {$a <=> $b} keys %customgrp){ |
2a81ab0d AM |
186 | if($customgrp{$grp}[0] eq $$hash{$key}[6]){ |
187 | &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt"); | |
188 | } | |
189 | } | |
a0fb1099 | 190 | }elsif($$hash{$key}[5] eq 'ipfire' ){ |
05d4f131 AM |
191 | if($$hash{$key}[6] eq 'GREEN'){ |
192 | $targethash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'}; | |
193 | } | |
194 | if($$hash{$key}[6] eq 'BLUE'){ | |
195 | $targethash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'}; | |
196 | } | |
197 | if($$hash{$key}[6] eq 'ORANGE'){ | |
198 | $targethash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'}; | |
199 | } | |
8762442c AM |
200 | if($$hash{$key}[6] eq 'ALL'){ |
201 | $targethash{$key}[0]='0.0.0.0/0'; | |
202 | } | |
690b0bd7 | 203 | if($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1'){ |
ff4770c7 | 204 | open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress"; |
2a81ab0d AM |
205 | $targethash{$key}[0]= <FILE>; |
206 | close(FILE); | |
207 | }else{ | |
208 | foreach my $alias (sort keys %aliases){ | |
209 | if ($$hash{$key}[6] eq $alias){ | |
210 | $targethash{$key}[0]=$aliases{$alias}{'IPT'}; | |
211 | } | |
212 | } | |
213 | } | |
214 | }else{ | |
215 | &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt"); | |
216 | } | |
2a81ab0d AM |
217 | ##get source prot and port |
218 | $SRC_TGT='SRC'; | |
219 | $SPROT = &get_prot($hash,$key); | |
220 | $SPORT = &get_port($hash,$key); | |
221 | $SRC_TGT=''; | |
14f7cb87 | 222 | |
2a81ab0d AM |
223 | ##get target prot and port |
224 | $DPROT=&get_prot($hash,$key); | |
14f7cb87 | 225 | |
2a81ab0d AM |
226 | if ($DPROT eq ''){$DPROT=' ';} |
227 | @DPROT=split(",",$DPROT); | |
14f7cb87 | 228 | |
2a81ab0d AM |
229 | #get time if defined |
230 | if($$hash{$key}[18] eq 'ON'){ | |
472136c9 AM |
231 | my ($time1,$time2,$daylight); |
232 | my $daylight=$$hash{$key}[28]; | |
233 | $time1=&get_time($$hash{$key}[26],$daylight); | |
234 | $time2=&get_time($$hash{$key}[27],$daylight); | |
2a81ab0d AM |
235 | if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");} |
236 | if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");} | |
237 | if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");} | |
238 | if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");} | |
239 | if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");} | |
240 | if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");} | |
241 | if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");} | |
242 | $TIME=join(",",@timeframe); | |
472136c9 AM |
243 | |
244 | $TIMEFROM="--timestart $time1 "; | |
245 | $TIMETILL="--timestop $time2 "; | |
a0f267b9 | 246 | $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL"; |
2a81ab0d | 247 | } |
2a81ab0d AM |
248 | if ($MODE eq '1'){ |
249 | print "NR:$key "; | |
250 | foreach my $i (0 .. $#{$$hash{$key}}){ | |
251 | print "$i: $$hash{$key}[$i] "; | |
252 | } | |
253 | print "\n"; | |
254 | print"##################################\n"; | |
255 | #print rules to console | |
2a81ab0d AM |
256 | foreach my $DPROT (@DPROT){ |
257 | $DPORT = &get_port($hash,$key,$DPROT); | |
258 | if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;} | |
259 | $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' '); | |
260 | foreach my $a (sort keys %sourcehash){ | |
261 | foreach my $b (sort keys %targethash){ | |
d7dc9718 | 262 | if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none' || $sourcehash{$a}[0] eq '0.0.0.0/0.0.0.0'){ |
2a81ab0d | 263 | if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){ |
5d7faa45 | 264 | if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";} |
8cb1afc8 AM |
265 | if(substr($DPORT, 2, 4) eq 'icmp'){ |
266 | my @icmprule= split(",",substr($DPORT, 12,)); | |
267 | foreach (@icmprule){ | |
268 | if ($$hash{$key}[17] eq 'ON'){ | |
a6edca5a | 269 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n"; |
8cb1afc8 | 270 | } |
a6edca5a | 271 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n"; |
8cb1afc8 | 272 | } |
28640b73 | 273 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){ |
c12392c0 | 274 | $natchain='NAT_DESTINATION'; |
28640b73 | 275 | if ($$hash{$key}[17] eq 'ON'){ |
c12392c0 | 276 | print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n"; |
28640b73 | 277 | } |
28640b73 | 278 | my ($ip,$sub) =split("/",$targethash{$b}[0]); |
c12392c0 | 279 | print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n"; |
829697d0 | 280 | $DPORT =~ s/\-/:/g; |
98cee89f AM |
281 | if ($DPORT){ |
282 | $fwaccessdport="--dport ".substr($DPORT,1,); | |
283 | }elsif(! $DPORT && $$hash{$key}[30] ne ''){ | |
284 | if ($$hash{$key}[30]=~m/|/i){ | |
285 | $$hash{$key}[30] =~ s/\|/,/g; | |
286 | $fwaccessdport="-m multiport --dport $$hash{$key}[30]"; | |
287 | }else{ | |
288 | $fwaccessdport="--dport $$hash{$key}[30]"; | |
289 | } | |
290 | } | |
c12392c0 AM |
291 | print "iptables -A FORWARDFW $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n"; |
292 | next; | |
08e1c65d | 293 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){ |
c12392c0 AM |
294 | $natchain='NAT_SOURCE'; |
295 | print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip\n"; | |
2a81ab0d | 296 | } |
c12392c0 AM |
297 | if ($$hash{$key}[17] eq 'ON'){ |
298 | print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n"; | |
299 | } | |
93c2de1c AM |
300 | if ($PROT ne '-p ICMP'){ |
301 | print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n"; | |
302 | } | |
2a81ab0d AM |
303 | } |
304 | } | |
305 | } | |
306 | } | |
307 | print"\n"; | |
308 | } | |
2a81ab0d AM |
309 | }elsif($MODE eq '0'){ |
310 | foreach my $DPROT (@DPROT){ | |
311 | $DPORT = &get_port($hash,$key,$DPROT); | |
312 | if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;} | |
313 | $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' '); | |
314 | foreach my $a (sort keys %sourcehash){ | |
315 | foreach my $b (sort keys %targethash){ | |
d7dc9718 | 316 | if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none' || $sourcehash{$a}[0] eq '0.0.0.0/0.0.0.0'){ |
2a81ab0d | 317 | if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){ |
5d7faa45 | 318 | if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";} |
93c2de1c | 319 | #Process ICMP RULE |
8cb1afc8 AM |
320 | if(substr($DPORT, 2, 4) eq 'icmp'){ |
321 | my @icmprule= split(",",substr($DPORT, 12,)); | |
322 | foreach (@icmprule){ | |
323 | if ($$hash{$key}[17] eq 'ON'){ | |
a6edca5a | 324 | system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG"); |
8cb1afc8 | 325 | } |
a6edca5a AM |
326 | system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]"); |
327 | } | |
93c2de1c | 328 | #PROCESS DNAT RULE (Portforward) |
a6edca5a | 329 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){ |
c12392c0 | 330 | $natchain='NAT_DESTINATION'; |
a6edca5a | 331 | if ($$hash{$key}[17] eq 'ON'){ |
c12392c0 | 332 | system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n"; |
8cb1afc8 | 333 | } |
a6edca5a | 334 | my ($ip,$sub) =split("/",$targethash{$b}[0]); |
c12392c0 | 335 | system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n"; |
829697d0 | 336 | $DPORT =~ s/\-/:/g; |
98cee89f AM |
337 | if ($DPORT){ |
338 | $fwaccessdport="--dport ".substr($DPORT,1,); | |
339 | }elsif(! $DPORT && $$hash{$key}[30] ne ''){ | |
340 | if ($$hash{$key}[30]=~m/|/i){ | |
341 | $$hash{$key}[30] =~ s/\|/,/g; | |
342 | $fwaccessdport="-m multiport --dport $$hash{$key}[30]"; | |
343 | }else{ | |
344 | $fwaccessdport="--dport $$hash{$key}[30]"; | |
345 | } | |
346 | } | |
c12392c0 AM |
347 | system "iptables -A FORWARDFW $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n"; |
348 | next; | |
93c2de1c | 349 | #PROCESS SNAT RULE |
a6edca5a | 350 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){ |
c12392c0 AM |
351 | $natchain='NAT_SOURCE'; |
352 | system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip\n"; | |
353 | } | |
354 | if ($$hash{$key}[17] eq 'ON'){ | |
355 | system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n"; | |
2a81ab0d | 356 | } |
93c2de1c AM |
357 | #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double) |
358 | if ($PROT ne '-p ICMP'){ | |
359 | system "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n"; | |
360 | } | |
2a81ab0d AM |
361 | } |
362 | } | |
363 | } | |
364 | } | |
2a81ab0d AM |
365 | } |
366 | } | |
367 | } | |
368 | %sourcehash=(); | |
369 | %targethash=(); | |
370 | undef $TIME; | |
371 | undef $TIMEFROM; | |
372 | undef $TIMETILL; | |
a6edca5a | 373 | undef $fireport; |
2a81ab0d AM |
374 | } |
375 | } | |
a6edca5a AM |
376 | sub get_nat_ip |
377 | { | |
378 | my $val=shift; | |
08e1c65d | 379 | my $type=shift; |
a6edca5a AM |
380 | my $result; |
381 | if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){ | |
382 | $result=$defaultNetworks{$val.'_ADDRESS'}; | |
383 | }elsif($val eq 'ALL'){ | |
384 | $result='-i '.$con; | |
08e1c65d | 385 | }elsif($val eq 'Default IP' && $type eq 'dnat'){ |
a6edca5a | 386 | $result='-d '.$redip; |
08e1c65d AM |
387 | }elsif($val eq 'Default IP' && $type eq 'snat'){ |
388 | $result=$redip; | |
a6edca5a AM |
389 | }else{ |
390 | foreach my $al (sort keys %aliases){ | |
08e1c65d | 391 | if($val eq $al && $type eq 'dnat'){ |
a6edca5a | 392 | $result='-d '.$aliases{$al}{'IPT'}; |
08e1c65d AM |
393 | }elsif($val eq $al && $type eq 'snat'){ |
394 | $result=$aliases{$al}{'IPT'}; | |
a6edca5a AM |
395 | } |
396 | } | |
397 | } | |
398 | return $result; | |
399 | } | |
472136c9 AM |
400 | sub get_time |
401 | { | |
402 | my $val=shift; | |
403 | my $val1=shift; | |
404 | my $time; | |
405 | my $minutes; | |
406 | my $ruletime; | |
407 | $minutes = &utcmin($val); | |
408 | $ruletime = $minutes + &time_get_utc($val); | |
409 | if ($ruletime < 0){$ruletime +=1440;} | |
410 | if ($ruletime > 1440){$ruletime -=1440;} | |
411 | $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60; | |
412 | return $time; | |
413 | } | |
414 | sub time_get_utc | |
415 | { | |
416 | # Calculates the UTCtime from a given time | |
417 | my $val=shift; | |
418 | my @localtime=localtime(time); | |
419 | my @gmtime=gmtime(time); | |
420 | my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60); | |
421 | return $diff; | |
422 | } | |
423 | sub utcmin | |
424 | { | |
425 | my $ruletime=shift; | |
426 | my ($hrs,$min) = split(":",$ruletime); | |
427 | my $newtime = $hrs*60+$min; | |
428 | return $newtime; | |
429 | } | |
36196d0d AM |
430 | sub p2pblock |
431 | { | |
432 | my $P2PSTRING; | |
433 | my $DO; | |
434 | open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile"; | |
435 | @p2ps = <FILE>; | |
436 | close FILE; | |
437 | my $CMD = "-m ipp2p"; | |
438 | foreach my $p2pentry (sort @p2ps) { | |
439 | my @p2pline = split( /\;/, $p2pentry ); | |
8d1beadc AM |
440 | if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) { |
441 | $DO = "ACCEPT"; | |
5238a871 | 442 | if ("$p2pline[2]" eq "on") { |
36196d0d AM |
443 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; |
444 | } | |
8d1beadc | 445 | }else { |
36196d0d | 446 | $DO = "RETURN"; |
5238a871 | 447 | if ("$p2pline[2]" eq "off") { |
36196d0d AM |
448 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; |
449 | } | |
450 | } | |
451 | } | |
452 | if ($MODE eq 1){ | |
453 | if($P2PSTRING){ | |
454 | print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n"; | |
455 | } | |
456 | }else{ | |
457 | if($P2PSTRING){ | |
458 | system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO"); | |
459 | } | |
460 | } | |
461 | } | |
2a81ab0d AM |
462 | sub get_address |
463 | { | |
464 | my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey | |
465 | my $base2=shift; | |
466 | my $type=shift; #src or tgt | |
467 | my $hash; | |
468 | if ($type eq 'src'){ | |
469 | $hash=\%sourcehash; | |
470 | }else{ | |
471 | $hash=\%targethash; | |
472 | } | |
473 | my $key = &General::findhasharraykey($hash); | |
474 | if($base eq 'src_addr' || $base eq 'tgt_addr' ){ | |
b5269091 AM |
475 | if (&General::validmac($base2)){ |
476 | $$hash{$key}[0] = "-m mac --mac-source $base2"; | |
477 | }else{ | |
478 | $$hash{$key}[0] = $base2; | |
479 | } | |
2a81ab0d | 480 | }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){ |
ddcec9d3 | 481 | $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con); |
2a81ab0d AM |
482 | }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){ |
483 | $$hash{$key}[0]=&fwlib::get_net_ip($base2); | |
484 | }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){ | |
485 | $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type); | |
486 | }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){ | |
487 | $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1); | |
488 | }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){ | |
489 | $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33); | |
490 | }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){ | |
6fab5bca | 491 | $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11); |
2a81ab0d AM |
492 | }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){ |
493 | $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11); | |
a0fb1099 AM |
494 | }elsif($base eq 'ipfire_src' ){ |
495 | if($base2 eq 'GREEN'){ | |
496 | $$hash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'}; | |
497 | } | |
498 | if($base2 eq 'BLUE'){ | |
499 | $$hash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'}; | |
500 | } | |
501 | if($base2 eq 'ORANGE'){ | |
502 | $$hash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'}; | |
503 | } | |
504 | if($base2 eq 'ALL'){ | |
505 | $$hash{$key}[0]='0.0.0.0/0'; | |
506 | } | |
507 | if($base2 eq 'RED' || $base2 eq 'RED1'){ | |
508 | open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress"; | |
509 | $$hash{$key}[0]= <FILE>; | |
510 | close(FILE); | |
511 | }else{ | |
512 | foreach my $alias (sort keys %aliases){ | |
513 | if ($base2 eq $alias){ | |
514 | $$hash{$key}[0]=$aliases{$alias}{'IPT'}; | |
515 | } | |
516 | } | |
517 | } | |
2a81ab0d AM |
518 | } |
519 | } | |
520 | sub get_prot | |
521 | { | |
522 | my $hash=shift; | |
523 | my $key=shift; | |
524 | if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){ | |
525 | if ($$hash{$key}[10] ne ''){ | |
526 | return"$$hash{$key}[8]"; | |
527 | }elsif($$hash{$key}[9] ne ''){ | |
528 | return"$$hash{$key}[8]"; | |
529 | }else{ | |
530 | return "$$hash{$key}[8]"; | |
531 | } | |
532 | }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){ | |
533 | if ($$hash{$key}[14] eq 'TGT_PORT'){ | |
534 | if ($$hash{$key}[15] ne ''){ | |
535 | return "$$hash{$key}[12]"; | |
536 | }elsif($$hash{$key}[13] ne ''){ | |
537 | return "$$hash{$key}[12]"; | |
538 | }else{ | |
539 | return "$$hash{$key}[12]"; | |
540 | } | |
541 | }elsif($$hash{$key}[14] eq 'cust_srv'){ | |
542 | return &fwlib::get_srv_prot($$hash{$key}[15]); | |
543 | ||
544 | }elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
545 | return &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
546 | } | |
547 | } | |
98cee89f AM |
548 | #DNAT |
549 | if ($SRC_TGT eq '' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[11] eq '' && $$hash{$key}[12] ne ''){ | |
550 | return "$$hash{$key}[12]"; | |
551 | } | |
2a81ab0d AM |
552 | } |
553 | sub get_port | |
554 | { | |
555 | my $hash=shift; | |
556 | my $key=shift; | |
557 | my $prot=shift; | |
558 | if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){ | |
559 | if ($$hash{$key}[10] ne ''){ | |
8f0b047b | 560 | $$hash{$key}[10] =~ s/\|/,/g; |
93a5f4a5 AM |
561 | if(index($$hash{$key}[10],",") > 0){ |
562 | return "-m multiport --sport $$hash{$key}[10] "; | |
563 | }else{ | |
a6edca5a AM |
564 | if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ||($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat') ){ |
565 | return "--sport $$hash{$key}[10] "; | |
566 | }else{ | |
567 | return ":$$hash{$key}[10]"; | |
568 | } | |
93a5f4a5 | 569 | } |
62fc8511 | 570 | }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){ |
2a81ab0d | 571 | return "--icmp-type $$hash{$key}[9] "; |
62fc8511 AM |
572 | }elsif($$hash{$key}[9] eq 'All ICMP-Types'){ |
573 | return; | |
2a81ab0d AM |
574 | } |
575 | }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){ | |
2a81ab0d AM |
576 | if($$hash{$key}[14] eq 'TGT_PORT'){ |
577 | if ($$hash{$key}[15] ne ''){ | |
8f0b047b | 578 | $$hash{$key}[15] =~ s/\|/,/g; |
93a5f4a5 AM |
579 | if(index($$hash{$key}[15],",") > 0){ |
580 | return "-m multiport --dport $$hash{$key}[15] "; | |
581 | }else{ | |
a6edca5a AM |
582 | if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){ |
583 | return "--dport $$hash{$key}[15] "; | |
584 | }else{ | |
829697d0 | 585 | $$hash{$key}[15] =~ s/\:/-/g; |
653a71b9 | 586 | return ":$$hash{$key}[15]"; |
a6edca5a | 587 | } |
93a5f4a5 | 588 | } |
2a81ab0d AM |
589 | }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){ |
590 | return "--icmp-type $$hash{$key}[13] "; | |
591 | }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){ | |
592 | return; | |
593 | } | |
594 | }elsif($$hash{$key}[14] eq 'cust_srv'){ | |
595 | if ($prot ne 'ICMP'){ | |
653a71b9 | 596 | if($$hash{$key}[31] eq 'dnat' && $$hash{$key}[28] eq 'ON'){ |
6be32fe5 AM |
597 | return ":".&fwlib::get_srv_port($$hash{$key}[15],1,$prot); |
598 | }else{ | |
599 | return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot); | |
600 | } | |
2a81ab0d AM |
601 | }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){ |
602 | return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot); | |
603 | }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){ | |
604 | return; | |
605 | } | |
606 | }elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
607 | if ($prot ne 'ICMP'){ | |
608 | return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot); | |
609 | } | |
610 | elsif($prot eq 'ICMP'){ | |
611 | return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot); | |
612 | } | |
2a81ab0d AM |
613 | } |
614 | } | |
615 | } |