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