]>
Commit | Line | Data |
---|---|---|
27f4a6b1 | 1 | #!/usr/bin/perl |
8343fd12 AM |
2 | ######################################################################## |
3 | # Script: convert-outgoingfw | |
4 | # Date: 21.03.2013 | |
5 | # Author: Alexander Marx (amarx@ipfire.org) | |
6 | ######################################################################## | |
7 | # | |
8 | # This script converts old groups and firewallrules | |
9 | # to the new one. This is a 3-step process. | |
10 | # STEP1: convert groups ->LOG /var/log/converters | |
11 | # STEP2: convert rules ->LOG /var/log/converters | |
12 | # STEP3: convert P2P rules | |
13 | # | |
14 | ######################################################################## | |
27f4a6b1 AM |
15 | |
16 | require '/var/ipfire/general-functions.pl'; | |
5a9fd5db | 17 | |
27f4a6b1 | 18 | use Socket; |
8f0b047b | 19 | use File::Path; |
5a9fd5db AM |
20 | use File::Copy; |
21 | ||
27f4a6b1 AM |
22 | my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/"; |
23 | my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/"; | |
24 | my $outgoingrules = "${General::swroot}/outgoing/rules"; | |
25 | my $outfwsettings = "${General::swroot}/outgoing/settings"; | |
26 | my $host = "Converted "; | |
27 | my $confighosts = "${General::swroot}/fwhosts/customhosts"; | |
28 | my $confignets = "${General::swroot}/fwhosts/customnetworks"; | |
29 | my $configgroups = "${General::swroot}/fwhosts/customgroups"; | |
30 | my $ovpnsettings = "${General::swroot}/ovpn/settings"; | |
31 | my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig"; | |
32 | my $ccdconfig = "${General::swroot}/ovpn/ccd.conf"; | |
33 | my $fwdfwconfig = "${General::swroot}/forward/config"; | |
6128ded8 | 34 | my $outfwconfig = "${General::swroot}/forward/outgoing"; |
27f4a6b1 AM |
35 | my $fwdfwsettings = "${General::swroot}/forward/settings"; |
36 | my @ipgroups = qx(ls $ipgrouppath); | |
37 | my @macgroups = qx(ls $macgrouppath); | |
38 | my @hostarray=(); | |
39 | my %outsettings=(); | |
40 | my %hosts=(); | |
41 | my %nets=(); | |
42 | my %groups=(); | |
43 | my %settingsovpn=(); | |
44 | my %configovpn=(); | |
45 | my %ccdconf=(); | |
6128ded8 AM |
46 | my %fwconfig=(); |
47 | my %fwconfigout=(); | |
27f4a6b1 AM |
48 | my %fwdsettings=(); |
49 | &General::readhash($outfwsettings,\%outsettings); | |
8343fd12 AM |
50 | #ONLY RUN if /var/ipfire/outgoing exists |
51 | if ( -d "/var/ipfire/outgoing"){ | |
52 | &process_groups; | |
53 | &process_rules; | |
54 | &process_p2p; | |
55 | } | |
5238a871 | 56 | system("/usr/local/bin/forwardfwctrl"); |
27f4a6b1 AM |
57 | sub process_groups |
58 | { | |
2833f567 | 59 | if(! -d "/var/log/converters"){ mkdir("/var/log/converters");} |
8f0b047b | 60 | if( -f "/var/log/converters/groups-convert.log"){rmtree("var/log/converters");} |
2833f567 | 61 | open (LOG, ">/var/log/converters/groups-convert.log") or die $!; |
27f4a6b1 AM |
62 | #IP Group processing |
63 | foreach my $group (@ipgroups){ | |
e09884e0 | 64 | my $now=localtime; |
27f4a6b1 | 65 | chomp $group; |
e09884e0 | 66 | print LOG "\n$now Processing IP-GROUP: $group...\n"; |
27f4a6b1 AM |
67 | open (DATEI, "<$ipgrouppath/$group"); |
68 | my @zeilen = <DATEI>; | |
69 | foreach my $ip (@zeilen){ | |
70 | chomp($ip); | |
71 | $ip =~ s/\s//gi; | |
e09884e0 | 72 | print LOG "$now Check IP $ip from Group $group "; |
27f4a6b1 AM |
73 | my $val=&check_ip($ip); |
74 | if($val){ | |
75 | push(@hostarray,$val.",ip"); | |
e09884e0 | 76 | print LOG "$now -> OK\n"; |
27f4a6b1 AM |
77 | } |
78 | else{ | |
e09884e0 | 79 | print LOG "$now -> IP \"$ip\" from group $group not converted (invalid IP) \n"; |
27f4a6b1 AM |
80 | } |
81 | $val=''; | |
82 | } | |
83 | &new_hostgrp($group,'ip'); | |
84 | @hostarray=(); | |
85 | } | |
86 | $group=''; | |
87 | @zeilen=(); | |
88 | @hostarray=(); | |
89 | #MAC Group processing | |
90 | foreach my $group (@macgroups){ | |
91 | chomp $group; | |
5a9fd5db | 92 | print LOG "\nProcessing MAC-GROUP: $group...\n"; |
27f4a6b1 AM |
93 | open (DATEI, "<$macgrouppath/$group"); |
94 | my @zeilen = <DATEI>; | |
95 | foreach my $mac (@zeilen){ | |
96 | chomp($mac); | |
97 | $mac =~ s/\s//gi; | |
e09884e0 | 98 | print LOG "$now Checking MAC $mac from group $group "; |
27f4a6b1 AM |
99 | #MAC checking |
100 | if(&General::validmac($mac)){ | |
101 | $val=$mac; | |
102 | } | |
103 | if($val){ | |
104 | push(@hostarray,$val.",mac"); | |
e09884e0 | 105 | print LOG "$now -> OK\n"; |
27f4a6b1 AM |
106 | } |
107 | else{ | |
e09884e0 | 108 | print LOG "$now -> Mac $mac from group $group not converted (invalid MAC)\n"; |
27f4a6b1 AM |
109 | } |
110 | $val=''; | |
111 | } | |
112 | &new_hostgrp($group,'mac'); | |
113 | @hostarray=(); | |
05612a54 | 114 | @zeilen=(); |
27f4a6b1 AM |
115 | } |
116 | close (LOG); | |
117 | } | |
118 | sub check_ip | |
119 | { | |
120 | my $adr=shift; | |
121 | my $a; | |
122 | #ip with subnet in decimal | |
123 | if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d{1,2})$/){ | |
124 | $adr=int($1).".".int($2).".".int($3).".".int($4); | |
125 | my $b = &General::iporsubtodec($5); | |
126 | $a=$adr."/".$b; | |
e3afaf88 | 127 | }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){ |
27f4a6b1 AM |
128 | $adr=int($1).".".int($2).".".int($3).".".int($4); |
129 | if(&General::validip($adr)){ | |
130 | $a=$adr."/255.255.255.255"; | |
131 | } | |
132 | } | |
133 | if(&General::validipandmask($adr)){ | |
134 | $a=&General::iporsubtodec($adr); | |
135 | } | |
136 | return $a; | |
137 | } | |
138 | sub new_hostgrp | |
139 | { | |
140 | &General::readhasharray($confighosts,\%hosts); | |
141 | &General::readhasharray($confignets,\%nets); | |
142 | &General::readhasharray($configgroups,\%groups); | |
143 | my $grp=shift; | |
144 | my $run=shift; | |
145 | my $name; #"converted" | |
146 | my $name2; | |
147 | my $name3; #custom host/custom net | |
148 | foreach my $adr (@hostarray){ | |
149 | if($run eq 'ip'){ | |
150 | my ($ip,$type) = split(",",$adr); | |
151 | my ($ippart,$subnet) = split("/",$ip); | |
152 | my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet); | |
5a9fd5db AM |
153 | if($byte4 eq '255'){ |
154 | print LOG "Processing SINGLE HOST $ippart/$subnet from group $grp\n"; | |
27f4a6b1 AM |
155 | if(!&check_host($ip)){ |
156 | my $key = &General::findhasharraykey(\%hosts); | |
157 | $name="host "; | |
158 | $name2=$name.$ippart; | |
159 | $name3="Custom Host"; | |
160 | $hosts{$key}[0] = $name2; | |
161 | $hosts{$key}[1] = $type; | |
162 | $hosts{$key}[2] = $ip; | |
05612a54 AM |
163 | $hosts{$key}[3] = ''; |
164 | $hosts{$key}[4] = 1; | |
5a9fd5db | 165 | print LOG "->Host (IP) $ip added to custom hosts\n" |
27f4a6b1 | 166 | }else{ |
05612a54 AM |
167 | print LOG "->Host (IP) $ip already exists in custom hosts\n"; |
168 | $name="host "; | |
169 | $name2=$name.$ippart; | |
8343fd12 AM |
170 | foreach my $key (sort keys %hosts){ |
171 | if($hosts{$key}[0] eq $name2){ | |
172 | $hosts{$key}[4]++; | |
173 | } | |
174 | } | |
175 | $name="host "; | |
176 | $name2=$name.$ippart; | |
05612a54 | 177 | $name3="Custom Host"; |
27f4a6b1 AM |
178 | } |
179 | }elsif($byte4 < '255'){ | |
5a9fd5db | 180 | print LOG "Processing NETWORK $ippart/$subnet from Group $grp\n"; |
27f4a6b1 AM |
181 | if(!&check_net($ippart,$subnet)){ |
182 | my $netkey = &General::findhasharraykey(\%nets); | |
183 | $name="net "; | |
184 | $name2=$name.$ippart; | |
185 | $name3="Custom Network"; | |
186 | $nets{$netkey}[0] = $name2; | |
187 | $nets{$netkey}[1] = $ippart; | |
188 | $nets{$netkey}[2] = $subnet; | |
05612a54 AM |
189 | $nets{$netkey}[3] = ''; |
190 | $nets{$netkey}[4] = 1; | |
5a9fd5db | 191 | print LOG "->Network $ippart/$subnet added to custom networks\n"; |
27f4a6b1 | 192 | }else{ |
05612a54 AM |
193 | print LOG "Network $ippart already exists in custom networks\n"; |
194 | $name="net "; | |
195 | $name2=$name.$ippart; | |
8343fd12 AM |
196 | foreach my $key (sort keys %nets){ |
197 | if($nets{$key}[0] eq $name2){ | |
198 | $nets{$key}[4]++; | |
199 | } | |
200 | } | |
201 | $name="net "; | |
202 | $name2=$name.$ippart; | |
05612a54 | 203 | $name3="Custom Network"; |
27f4a6b1 AM |
204 | } |
205 | } | |
f2ab6fba | 206 | if($name2 && !&check_grp($grp,$name2)){ |
27f4a6b1 AM |
207 | my $grpkey = &General::findhasharraykey(\%groups); |
208 | $groups{$grpkey}[0] = $grp; | |
209 | $groups{$grpkey}[1] = ''; | |
210 | $groups{$grpkey}[2] = $name2; | |
211 | $groups{$grpkey}[3] = $name3; | |
212 | $groups{$grpkey}[4] = 0; | |
5a9fd5db | 213 | print LOG "->$name2 added to group $grp\n"; |
27f4a6b1 AM |
214 | } |
215 | }elsif($run eq 'mac'){ | |
216 | #MACRUN | |
217 | my ($mac,$type) = split(",",$adr); | |
5a9fd5db | 218 | print LOG "Processing HOST (MAC) $mac\n"; |
27f4a6b1 AM |
219 | if(!&check_host($mac)){ |
220 | my $key = &General::findhasharraykey(\%hosts); | |
221 | $name="host "; | |
222 | $name2=$name.$mac; | |
223 | $name3="Custom Host"; | |
224 | $hosts{$key}[0] = $name2; | |
225 | $hosts{$key}[1] = $type; | |
226 | $hosts{$key}[2] = $mac; | |
fccf52cf AM |
227 | $hosts{$key}[3] = ''; |
228 | $hosts{$key}[4] = 1; | |
5a9fd5db | 229 | print LOG "->Host (MAC) $mac added to custom hosts\n"; |
27f4a6b1 | 230 | }else{ |
05612a54 AM |
231 | print LOG "->Host (MAC) $mac already exists in custom hosts \n"; |
232 | $name="host "; | |
233 | $name2=$name.$mac; | |
8343fd12 AM |
234 | foreach my $key (sort keys %hosts){ |
235 | if($hosts{$key}[0] eq $name2){ | |
236 | $hosts{$key}[4]++; | |
237 | } | |
238 | } | |
239 | $name="host "; | |
240 | $name2=$name.$mac; | |
05612a54 | 241 | $name3="Custom Host"; |
27f4a6b1 | 242 | } |
f2ab6fba | 243 | if($name2 && !&check_grp($grp,$name2)){ |
27f4a6b1 AM |
244 | my $grpkey = &General::findhasharraykey(\%groups); |
245 | $groups{$grpkey}[0] = $grp; | |
246 | $groups{$grpkey}[1] = ''; | |
247 | $groups{$grpkey}[2] = $name2; | |
248 | $groups{$grpkey}[3] = $name3; | |
249 | $groups{$grpkey}[4] = 0; | |
5a9fd5db | 250 | print LOG "->$name2 added to group $grp\n"; |
27f4a6b1 AM |
251 | } |
252 | } | |
253 | } | |
5a9fd5db | 254 | @hostarray=(); |
27f4a6b1 AM |
255 | &General::writehasharray($confighosts,\%hosts); |
256 | &General::writehasharray($configgroups,\%groups); | |
257 | &General::writehasharray($confignets,\%nets); | |
70d38e50 | 258 | |
27f4a6b1 AM |
259 | } |
260 | sub check_host | |
261 | { | |
262 | my $ip=shift; | |
263 | foreach my $key (sort keys %hosts) | |
264 | { | |
265 | if($hosts{$key}[2] eq $ip) | |
266 | { | |
267 | return 1; | |
268 | } | |
269 | } | |
270 | return 0; | |
271 | } | |
272 | sub check_net | |
273 | { | |
274 | my $ip=shift; | |
275 | my $sub=shift; | |
276 | foreach my $key (sort keys %nets) | |
277 | { | |
278 | if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub) | |
279 | { | |
280 | return 1; | |
281 | } | |
282 | } | |
283 | return 0; | |
284 | } | |
285 | sub check_grp | |
286 | { | |
287 | my $grp=shift; | |
288 | my $value=shift; | |
289 | foreach my $key (sort keys %groups) | |
290 | { | |
291 | if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value) | |
292 | { | |
293 | return 1; | |
294 | } | |
295 | } | |
296 | return 0; | |
297 | } | |
298 | sub process_rules | |
299 | { | |
6128ded8 | 300 | my ($type,$action,$active,$grp1,$source,$grp2,$useport,$port,$prot,$grp3,$target,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to); |
e09884e0 AM |
301 | #open LOG |
302 | if( -f "/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");} | |
303 | open (LOG, ">/var/log/converters/outgoingfw-convert.log") or die $!; | |
304 | ||
27f4a6b1 | 305 | &General::readhash($fwdfwsettings,\%fwdsettings); |
12a43202 | 306 | if ($outsettings{'POLICY'} eq 'MODE1'){ |
e09884e0 AM |
307 | $fwdsettings{'POLICY'}='MODE1'; |
308 | $fwdsettings{'POLICY1'}='MODE2'; | |
27f4a6b1 AM |
309 | $type='ALLOW'; |
310 | $action='ACCEPT'; | |
e09884e0 | 311 | }else{ |
fccf52cf | 312 | $fwdsettings{'POLICY'}='MODE2'; |
e09884e0 | 313 | $fwdsettings{'POLICY1'}='MODE2'; |
27f4a6b1 AM |
314 | $type='DENY'; |
315 | $action='DROP'; | |
27f4a6b1 | 316 | } |
fccf52cf | 317 | &General::writehash($fwdfwsettings,\%fwdsettings); |
27f4a6b1 AM |
318 | open (DATEI, "<$outgoingrules"); |
319 | my @lines = <DATEI>; | |
320 | foreach my $rule (@lines) | |
321 | { | |
e09884e0 | 322 | my $now=localtime; |
27f4a6b1 | 323 | chomp($rule); |
99e698d0 | 324 | $port=''; |
e09884e0 | 325 | print LOG "$now processing: $rule\n"; |
99e698d0 AM |
326 | my @configline=(); |
327 | @configline = split( /\;/, $rule ); | |
27f4a6b1 AM |
328 | my @prot=(); |
329 | if($configline[0] eq $type){ | |
330 | #some variables we can use from old config | |
331 | if($configline[1] eq 'on'){ $active='ON';}else{$active='';} | |
5238a871 AM |
332 | if($configline[3] eq 'all' && $configline[8] ne ''){ |
333 | push(@prot,"TCP"); | |
334 | push(@prot,"UDP"); | |
335 | }elsif($configline[3] eq 'all' && $configline[8] eq ''){ | |
5a9fd5db | 336 | push(@prot,""); |
27f4a6b1 AM |
337 | }else{ |
338 | push(@prot,$configline[3]); | |
27f4a6b1 | 339 | } |
5238a871 AM |
340 | if($configline[4] ne ''){ |
341 | $configline[4] =~ s/,/;/g; | |
342 | $remark = $configline[4]; | |
343 | }else{$remark = '';} | |
8d1beadc | 344 | if($configline[9] eq 'Active'){ $log='ON';}else{$log='';} |
27f4a6b1 AM |
345 | if($configline[10] eq 'on' && $configline[11] eq 'on' && $configline[12] eq 'on' && $configline[13] eq 'on' && $configline[14] eq 'on' && $configline[15] eq 'on' && $configline[16] eq 'on'){ |
346 | if($configline[17] eq '00:00' && $configline[18] eq '00:00'){ | |
347 | $time=''; | |
348 | }else{ | |
349 | $time='ON'; | |
350 | } | |
351 | }else{ | |
352 | $time='ON'; | |
353 | } | |
354 | $time_mon=$configline[10]; | |
355 | $time_tue=$configline[11]; | |
356 | $time_wed=$configline[12]; | |
357 | $time_thu=$configline[13]; | |
358 | $time_fri=$configline[14]; | |
359 | $time_sat=$configline[15]; | |
360 | $time_sun=$configline[16]; | |
361 | $time_from=$configline[17]; | |
362 | $time_to=$configline[18]; | |
363 | ############################################################ | |
364 | #sourcepart | |
365 | if ($configline[2] eq 'green') { | |
366 | $grp1='std_net_src'; | |
367 | $source='GREEN'; | |
368 | }elsif ($configline[2] eq 'orange') { | |
369 | $grp1='std_net_src'; | |
370 | $source='ORANGE'; | |
371 | }elsif ($configline[2] eq 'red') { | |
6128ded8 AM |
372 | $grp1='std_net_src'; |
373 | $source='IPFire'; | |
374 | &General::readhash($fwdfwsettings,\%fwdsettings); | |
375 | $fwdsettings{'POLICY1'}=$outsettings{'POLICY'}; | |
fccf52cf | 376 | $fwdsettings{'POLICY'}=$outsettings{'POLICY'}; |
6128ded8 | 377 | &General::writehash($fwdfwsettings,\%fwdsettings); |
27f4a6b1 AM |
378 | }elsif ($configline[2] eq 'blue') { |
379 | $grp1='std_net_src'; | |
380 | $source='BLUE'; | |
381 | }elsif ($configline[2] eq 'ipsec') { | |
e09884e0 | 382 | print LOG "$now -> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n"; |
27f4a6b1 AM |
383 | next; |
384 | }elsif ($configline[2] eq 'ovpn') { | |
e09884e0 | 385 | print LOG "$now ->Creating networks/groups for OpenVPN...\n"; |
99e698d0 AM |
386 | &build_ovpn_grp; |
387 | $grp1='cust_grp_src'; | |
388 | $source='ovpn' | |
27f4a6b1 AM |
389 | }elsif ($configline[2] eq 'ip') { |
390 | my $z=&check_ip($configline[5]); | |
391 | if($z){ | |
3b81fad4 AM |
392 | my ($ipa,$subn) = split("/",$z); |
393 | $subn=&General::iporsubtocidr($subn); | |
27f4a6b1 | 394 | $grp1='src_addr'; |
3b81fad4 | 395 | $source="$ipa/$subn"; |
27f4a6b1 | 396 | }else{ |
e09884e0 | 397 | print LOG "$now -> Rule not converted, missing/invalid source ip \"$configline[5]\"\n"; |
27f4a6b1 AM |
398 | next; |
399 | } | |
400 | }elsif ($configline[2] eq 'mac') { | |
401 | if(&General::validmac($configline[6])){ | |
402 | $grp1='src_addr'; | |
403 | $source=$configline[6]; | |
404 | }else{ | |
e09884e0 | 405 | print LOG"$now -> Rule not converted, invalid MAC \"$configline[6]\" \n"; |
27f4a6b1 AM |
406 | next; |
407 | } | |
408 | }elsif ($configline[2] eq 'all') { | |
409 | $grp1='std_net_src'; | |
410 | $source='ALL'; | |
411 | }else{ | |
27f4a6b1 AM |
412 | foreach my $key (sort keys %groups){ |
413 | if($groups{$key}[0] eq $configline[2]){ | |
414 | $grp1='cust_grp_src'; | |
415 | $source=$configline[2]; | |
416 | } | |
417 | } | |
418 | if ($grp1 eq '' || $source eq ''){ | |
e09884e0 | 419 | print LOG "$now -> Rule not converted, no valid source recognised\n"; |
27f4a6b1 AM |
420 | } |
421 | } | |
422 | ############################################################ | |
423 | #destinationpart | |
424 | if($configline[7] ne ''){ | |
425 | my $address=&check_ip($configline[7]); | |
426 | if($address){ | |
3b81fad4 AM |
427 | my ($dip,$dsub) = split("/",$address); |
428 | $dsub=&General::iporsubtocidr($dsub); | |
27f4a6b1 | 429 | $grp2='tgt_addr'; |
3b81fad4 | 430 | $target="$dip/$dsub"; |
27f4a6b1 AM |
431 | }elsif(!$address){ |
432 | my $getwebsiteip=&get_ip_from_domain($configline[7]); | |
433 | if ($getwebsiteip){ | |
434 | $grp2='tgt_addr'; | |
435 | $target=$getwebsiteip; | |
5a9fd5db | 436 | $remark.=" $configline[7]"; |
27f4a6b1 | 437 | }else{ |
e09884e0 | 438 | print LOG "$now -> Rule not converted, invalid domain \"$configline[7]\"\n"; |
27f4a6b1 AM |
439 | next; |
440 | } | |
441 | } | |
442 | }else{ | |
443 | $grp2='std_net_tgt'; | |
444 | $target='ALL'; | |
445 | } | |
87946296 | 446 | if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){ |
8f0b047b AM |
447 | my @values=(); |
448 | my @parts=split(",",$configline[8]); | |
449 | foreach (@parts){ | |
fccf52cf | 450 | $_=~ tr/-/:/; |
8f0b047b AM |
451 | if (!($_ =~ /^(\d+)\:(\d+)$/)) { |
452 | if(&General::validport($_)){ | |
453 | $useport='ON'; | |
8f0b047b AM |
454 | push (@values,$_); |
455 | $grp3='TGT_PORT'; | |
456 | }else{ | |
e09884e0 | 457 | print LOG "$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n"; |
8f0b047b AM |
458 | next; |
459 | } | |
27f4a6b1 | 460 | }else{ |
8b3dd791 AM |
461 | my ($a1,$a2) = split(/\:/,$_); |
462 | if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){ | |
8f0b047b | 463 | $useport='ON'; |
8f0b047b AM |
464 | push (@values,"$a1:$a2"); |
465 | $grp3='TGT_PORT'; | |
8b3dd791 | 466 | }else{ |
e09884e0 | 467 | print LOG "$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n"; |
8f0b047b | 468 | next; |
8b3dd791 | 469 | } |
8f0b047b | 470 | } |
27f4a6b1 | 471 | } |
8b3dd791 AM |
472 | $port=join("|",@values); |
473 | @values=(); | |
99e698d0 | 474 | @parts=(); |
27f4a6b1 AM |
475 | } |
476 | }else{ | |
477 | print LOG "-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n"; | |
478 | } | |
479 | &General::readhasharray($fwdfwconfig,\%fwconfig); | |
6128ded8 | 480 | &General::readhasharray($outfwconfig,\%fwconfigout); |
27f4a6b1 | 481 | my $check; |
6128ded8 | 482 | my $chain; |
27f4a6b1 | 483 | foreach my $protocol (@prot){ |
e09884e0 | 484 | my $now=localtime; |
6128ded8 AM |
485 | if ($source eq 'IPFire'){ |
486 | $chain='OUTGOINGFW'; | |
487 | }else{ | |
488 | $chain='FORWARDFW'; | |
489 | } | |
27f4a6b1 | 490 | $protocol=uc($protocol); |
e09884e0 | 491 | print LOG "$now -> Converted: $action,$chain,$active,$grp1,$source,$grp2,$target,,,,,$useport,$protocol,,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to\n"; |
27f4a6b1 AM |
492 | #Put rules into system.... |
493 | ########################### | |
27f4a6b1 AM |
494 | #check for double rules |
495 | foreach my $key (sort keys %fwconfig){ | |
6128ded8 | 496 | if("$action,$chain,$active,$grp1,$source,$grp2,$target,,,,,$useport,$protocol,,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to" |
27f4a6b1 AM |
497 | eq "$fwconfig{$key}[0],$fwconfig{$key}[1],$fwconfig{$key}[2],$fwconfig{$key}[3],$fwconfig{$key}[4],$fwconfig{$key}[5],$fwconfig{$key}[6],,,,,$fwconfig{$key}[11],$fwconfig{$key}[12],,$fwconfig{$key}[14],$fwconfig{$key}[15],$fwconfig{$key}[16],$fwconfig{$key}[17],$fwconfig{$key}[18],$fwconfig{$key}[19],$fwconfig{$key}[20],$fwconfig{$key}[21],$fwconfig{$key}[22],$fwconfig{$key}[23],$fwconfig{$key}[24],$fwconfig{$key}[25],$fwconfig{$key}[26],$fwconfig{$key}[27]"){ |
498 | $check='on'; | |
499 | next; | |
500 | } | |
501 | } | |
502 | if($check ne 'on'){ | |
503 | #increase groupcounter | |
504 | my $check1; | |
505 | if($grp1 eq 'cust_grp_src'){ | |
506 | foreach my $key (sort keys %groups){ | |
507 | if($groups{$key}[0] eq $source){ | |
508 | $groups{$key}[4]++; | |
509 | $check1='on'; | |
510 | } | |
511 | } | |
512 | if($check1 eq 'on'){ | |
27f4a6b1 AM |
513 | &General::writehasharray($configgroups,\%groups); |
514 | } | |
515 | } | |
6128ded8 AM |
516 | if ($chain eq 'FORWARDFW'){ |
517 | my $key = &General::findhasharraykey(\%fwconfig); | |
518 | $fwconfig{$key}[0] = $action; | |
519 | $fwconfig{$key}[1] = $chain; | |
520 | $fwconfig{$key}[2] = $active; | |
521 | $fwconfig{$key}[3] = $grp1; | |
522 | $fwconfig{$key}[4] = $source; | |
523 | $fwconfig{$key}[5] = $grp2; | |
524 | $fwconfig{$key}[6] = $target; | |
525 | $fwconfig{$key}[11] = $useport; | |
526 | $fwconfig{$key}[12] = $protocol; | |
527 | $fwconfig{$key}[14] = $grp3; | |
528 | $fwconfig{$key}[15] = $port; | |
529 | $fwconfig{$key}[16] = $remark; | |
530 | $fwconfig{$key}[17] = $log; | |
531 | $fwconfig{$key}[18] = $time; | |
532 | $fwconfig{$key}[19] = $time_mon; | |
533 | $fwconfig{$key}[20] = $time_tue; | |
534 | $fwconfig{$key}[21] = $time_wed; | |
535 | $fwconfig{$key}[22] = $time_thu; | |
536 | $fwconfig{$key}[23] = $time_fri; | |
537 | $fwconfig{$key}[24] = $time_sat; | |
538 | $fwconfig{$key}[25] = $time_sun; | |
539 | $fwconfig{$key}[26] = $time_from; | |
540 | $fwconfig{$key}[27] = $time_to; | |
541 | }else{ | |
542 | my $key = &General::findhasharraykey(\%fwconfigout); | |
543 | $fwconfigout{$key}[0] = $action; | |
544 | $fwconfigout{$key}[1] = $chain; | |
545 | $fwconfigout{$key}[2] = $active; | |
546 | $fwconfigout{$key}[3] = $grp1; | |
547 | $fwconfigout{$key}[4] = $source; | |
548 | $fwconfigout{$key}[5] = $grp2; | |
549 | $fwconfigout{$key}[6] = $target; | |
550 | $fwconfigout{$key}[11] = $useport; | |
551 | $fwconfigout{$key}[12] = $protocol; | |
552 | $fwconfigout{$key}[14] = $grp3; | |
553 | $fwconfigout{$key}[15] = $port; | |
554 | $fwconfigout{$key}[16] = $remark; | |
555 | $fwconfigout{$key}[17] = $log; | |
556 | $fwconfigout{$key}[18] = $time; | |
557 | $fwconfigout{$key}[19] = $time_mon; | |
558 | $fwconfigout{$key}[20] = $time_tue; | |
559 | $fwconfigout{$key}[21] = $time_wed; | |
560 | $fwconfigout{$key}[22] = $time_thu; | |
561 | $fwconfigout{$key}[23] = $time_fri; | |
562 | $fwconfigout{$key}[24] = $time_sat; | |
563 | $fwconfigout{$key}[25] = $time_sun; | |
564 | $fwconfigout{$key}[26] = $time_from; | |
565 | $fwconfigout{$key}[27] = $time_to; | |
566 | } | |
567 | &General::writehasharray($fwdfwconfig,\%fwconfig); | |
568 | &General::writehasharray($outfwconfig,\%fwconfigout); | |
27f4a6b1 AM |
569 | } |
570 | } | |
27f4a6b1 AM |
571 | @prot=(); |
572 | } | |
573 | close(LOG); | |
574 | @lines=(); | |
575 | } | |
576 | sub get_ip_from_domain | |
577 | { | |
578 | $web=shift; | |
579 | my $resolvedip; | |
580 | my $checked; | |
581 | my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web); | |
582 | if(@addrs){ | |
583 | $resolvedip=inet_ntoa($addrs[0]); | |
584 | return $resolvedip; | |
585 | } | |
586 | return; | |
587 | } | |
588 | sub build_ovpn_grp | |
589 | { | |
e09884e0 | 590 | my $now=localtime; |
27f4a6b1 AM |
591 | &General::readhasharray($confighosts,\%hosts); |
592 | &General::readhasharray($confignets,\%nets); | |
593 | &General::readhasharray($configgroups,\%groups); | |
594 | &General::readhasharray($ovpnconfig,\%configovpn); | |
595 | &General::readhasharray($ccdconfig,\%ccdconf); | |
596 | &General::readhash($ovpnsettings,\%settingsovpn); | |
597 | #get ovpn nets | |
598 | my @ovpnnets=(); | |
599 | if($settingsovpn{'DOVPN_SUBNET'}){ | |
600 | my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'}); | |
601 | push (@ovpnnets,"$net,$subnet,dynamic"); | |
e09884e0 | 602 | print LOG "$now ->found dynamic OpenVPN net\n"; |
27f4a6b1 AM |
603 | } |
604 | foreach my $key (sort keys %ccdconf){ | |
605 | my ($net,$subnet)=split("/",$ccdconf{$key}[1]); | |
606 | $subnet=&General::iporsubtodec($subnet); | |
607 | push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]"); | |
e09884e0 | 608 | print LOG "$now ->found OpenVPN static net $net/$subnet\n"; |
27f4a6b1 AM |
609 | } |
610 | foreach my $key (sort keys %configovpn){ | |
611 | if ($configovpn{$key}[3] eq 'net'){ | |
612 | my ($net,$subnet)=split("/",$configovpn{$key}[27]); | |
613 | push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]"); | |
e09884e0 | 614 | print LOG "$now ->found OpenVPN $net/$subnet $configovpn{$key}[2]\n"; |
27f4a6b1 AM |
615 | } |
616 | } | |
617 | #add ovpn nets to customnetworks/groups | |
618 | foreach my $line (@ovpnnets){ | |
e09884e0 | 619 | my $now=localtime; |
27f4a6b1 AM |
620 | my ($net,$subnet,$name) = split(",",$line); |
621 | if (!&check_net($net,$subnet)){ | |
622 | my $netkey = &General::findhasharraykey(\%nets); | |
623 | $name2=$name."(ovpn)".$net; | |
624 | $name3="Custom Network"; | |
625 | $nets{$netkey}[0] = $name2; | |
626 | $nets{$netkey}[1] = $net; | |
627 | $nets{$netkey}[2] = $subnet; | |
e09884e0 AM |
628 | $nets{$netkey}[3] = ''; |
629 | $nets{$netkey}[4] = 1; | |
630 | print LOG "$now ->added $name2 $net/$subnet to customnetworks\n"; | |
27f4a6b1 AM |
631 | }else{ |
632 | print LOG "-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n"; | |
633 | } | |
634 | if($name2){ | |
635 | my $grpkey = &General::findhasharraykey(\%groups); | |
636 | $groups{$grpkey}[0] = "ovpn"; | |
637 | $groups{$grpkey}[1] = ''; | |
638 | $groups{$grpkey}[2] = $name2; | |
639 | $groups{$grpkey}[3] = "Custom Network"; | |
640 | $groups{$grpkey}[4] = 0; | |
e09884e0 | 641 | print LOG "$now ->added $name2 to customgroup ovpn\n"; |
27f4a6b1 AM |
642 | } |
643 | $name2=''; | |
644 | } | |
645 | @ovpnnets=(); | |
646 | &General::writehasharray($confighosts,\%hosts); | |
647 | &General::writehasharray($configgroups,\%groups); | |
648 | &General::writehasharray($confignets,\%nets); | |
e09884e0 | 649 | print LOG "$now ->finished OVPN\n"; |
27f4a6b1 | 650 | } |
5a9fd5db AM |
651 | sub process_p2p |
652 | { | |
653 | copy("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols"); | |
70d38e50 | 654 | chmod oct('0777'), '/var/ipfire/forward/p2protocols'; |
5a9fd5db | 655 | } |