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