]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/firewall/convert-dmz
Firewall: Fixed portfw-converter (rules where not converted correctly) And Standard...
[people/teissler/ipfire-2.x.git] / config / firewall / convert-dmz
CommitLineData
a60dbb4b
AM
1#!/usr/bin/perl
2
dc21519f
AM
3###############################################################################
4# #
5# IPFire.org - A linux based firewall #
5bee9a9d 6# Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
dc21519f
AM
7# #
8# This program is free software: you can redistribute it and/or modify #
9# it under the terms of the GNU General Public License as published by #
10# the Free Software Foundation, either version 3 of the License, or #
11# (at your option) any later version. #
12# #
13# This program is distributed in the hope that it will be useful, #
14# but WITHOUT ANY WARRANTY; without even the implied warranty of #
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16# GNU General Public License for more details. #
17# #
18# You should have received a copy of the GNU General Public License #
19# along with this program. If not, see <http://www.gnu.org/licenses/>. #
20# #
21###############################################################################
dc21519f
AM
22# #
23# This script converts old dmz holes rules from old firewall #
24# to the new one. This is a 2-step process. #
25# STEP1: read old config and normalize settings #
26# STEP2: check valid ip and save valid rules to new firewall #
27# #
28###############################################################################
a60dbb4b
AM
29my @current=();
30my @alias=();
31my %configdmz=();
32my %ifaces=();
33my %configfwdfw=();
34require '/var/ipfire/general-functions.pl';
35my $dmzconfig = "${General::swroot}/dmzholes/config";
6d8eb5de 36my $fwdfwconfig = "${General::swroot}/firewall/config";
a60dbb4b
AM
37my $ifacesettings = "${General::swroot}/ethernet/settings";
38my $field0 = 'ACCEPT';
39my $field1 = 'FORWARDFW';
40my $field2 = ''; #ON or emtpy
41my $field3 = ''; #std_net_src or src_addr
42my $field4 = ''; #ALL or IP-Address with /32
43my $field5 = ''; #std_net_tgt or tgt_addr
44my $field6 = ''; #IP or network name
45my $field11 = 'ON'; #use target port
46my $field12 = ''; #TCP or UDP
47my $field13 = 'All ICMP-Types';
48my $field14 = 'TGT_PORT';
49my $field15 = ''; #Port Number
50my $field16 = ''; #remark
51my $field26 = '00:00';
52my $field27 = '00:00';
ac9e77e3
AM
53my $field28 = '';
54my $field29 = 'ALL';
55my $field30 = '';
56my $field31 = 'dnat';
57
58
a60dbb4b
AM
59open(FILE, $dmzconfig) or die 'Unable to open config file.';
60my @current = <FILE>;
61close(FILE);
62#open LOGFILE
63open (LOG, ">/var/log/converters/dmz-convert.log") or die $!;
64&General::readhash($ifacesettings, \%ifaces);
65&General::readhasharray($fwdfwconfig,\%configfwdfw);
66&process_rules;
67sub process_rules{
68 foreach my $line (@current){
69 my $now=localtime;
70 #get values from old configfile
71 my ($a,$b,$c,$d,$e,$f,$g,$h) = split (",",$line);
f7e649dd
AM
72 $h =~ s/\s*\n//gi;
73 print LOG "$now Processing A: $a B: $b C: $c D: $d E: $e F: $f G: $g H: $h\n";
a60dbb4b
AM
74 #Now convert values and check ip addresses
75 $a=uc($a);
76 $e=uc($e);
77 $field2=$e if($e eq 'ON');
78 #SOURCE IP-check
79 $b=&check_ip($b);
80 if (&General::validipandmask($b)){
81 #When ip valid, check if we have a network
82 my ($ip,$subnet) = split ("/",$b);
83 if ($f eq 'orange' && $ip eq $ifaces{'ORANGE_NETADDRESS'}){
84 $field3='std_net_src';
85 $field4='ORANGE';
86 }elsif($f eq 'blue' && $ip eq $ifaces{'BLUE_NETADDRESS'}){
87 $field3='std_net_src';
88 $field4='BLUE';
89 }elsif($f eq 'orange' && &General::IpInSubnet($ip,$ifaces{'ORANGE_NETADDRESS'},$ifaces{'ORANGE_NETMASK'})){
90 $field3='src_addr';
91 $field4=$b;
92 }elsif($f eq 'blue' && &General::IpInSubnet($ip,$ifaces{'BLUE_NETADDRESS'},$ifaces{'BLUE_NETMASK'})){
93 $field3='src_addr';
94 $field4=$b;
95 }else{
96 print LOG "$now ->NOT Converted, source ip $b not part of source network $f \n\n";
97 next;
98 }
99 }else{
100 print LOG "$now -> SOURCE IP INVALID. \n\n";
101 next;
102 }
103 #TARGET IP-check
104 $c=&check_ip($c);
105 if (&General::validipandmask($c)){
106 my $now=localtime;
107 #When ip valid, check if we have a network
108 my ($ip,$subnet) = split ("/",$c);
109 if ($g eq 'green' && $ip eq $ifaces{'GREEN_NETADDRESS'}){
110 $field5='std_net_tgt';
111 $field6='GREEN';
112 }elsif($g eq 'blue' && $ip eq $ifaces{'BLUE_NETADDRESS'}){
113 $field5='std_net_tgt';
114 $field6='BLUE';
115 }elsif($g eq 'green' && &General::IpInSubnet($ip,$ifaces{'GREEN_NETADDRESS'},$ifaces{'GREEN_NETMASK'})){
116 $field5='tgt_addr';
117 $field6=$c;
118 }elsif($g eq 'blue' && &General::IpInSubnet($ip,$ifaces{'BLUE_NETADDRESS'},$ifaces{'BLUE_NETMASK'})){
119 $field5='tgt_addr';
120 $field6=$c;
121 }else{
f7e649dd 122 print LOG "$now ->NOT Converted, target ip $c not part of target network $g \n\n";
a60dbb4b
AM
123 next;
124 }
125 }else{
f7e649dd 126 print LOG "$now -> TARGET IP INVALID. \n\n";
a60dbb4b
AM
127 next;
128 }
129 $field12=$a;
130 #convert portrange
131 $d =~ tr/-/:/;
132 $field15=$d;
133 $field16=$h;
a60dbb4b
AM
134 my $key = &General::findhasharraykey (\%configfwdfw);
135 foreach my $i (0 .. 27) { $configfwdfw{$key}[$i] = "";}
136 $configfwdfw{$key}[0] = $field0;
137 $configfwdfw{$key}[1] = $field1;
138 $configfwdfw{$key}[2] = $field2;
139 $configfwdfw{$key}[3] = $field3;
140 $configfwdfw{$key}[4] = $field4;
141 $configfwdfw{$key}[5] = $field5;
142 $configfwdfw{$key}[6] = $field6;
143 $configfwdfw{$key}[7] = '';
144 $configfwdfw{$key}[8] = '';
145 $configfwdfw{$key}[9] = '';
146 $configfwdfw{$key}[10] = '';
147 $configfwdfw{$key}[11] = $field11;
148 $configfwdfw{$key}[12] = $field12;
149 $configfwdfw{$key}[13] = $field13;
150 $configfwdfw{$key}[14] = $field14;
151 $configfwdfw{$key}[15] = $field15;
152 $configfwdfw{$key}[16] = $field16;
153 $configfwdfw{$key}[17] = '';
154 $configfwdfw{$key}[18] = '';
155 $configfwdfw{$key}[19] = '';
156 $configfwdfw{$key}[20] = '';
157 $configfwdfw{$key}[21] = '';
158 $configfwdfw{$key}[22] = '';
159 $configfwdfw{$key}[23] = '';
160 $configfwdfw{$key}[24] = '';
161 $configfwdfw{$key}[25] = '';
162 $configfwdfw{$key}[26] = $field26;
163 $configfwdfw{$key}[27] = $field27;
ac9e77e3
AM
164 $configfwdfw{$key}[28] = $field28;
165 $configfwdfw{$key}[29] = $field29;
166 $configfwdfw{$key}[30] = $field30;
167 $configfwdfw{$key}[31] = $field31;
a60dbb4b
AM
168 print LOG "$Now -> Converted to $field0,$field1,$field2,$field3,$field4,$field5,$field6,,,,,$field11,$field12,$field13,$field14,$field15,$field16,,,,,,,,,,$field26,$field27\n";
169 }
170 &General::writehasharray($fwdfwconfig,\%configfwdfw);
171close (LOG);
172}
173
174sub check_ip
175{
176 my $adr=shift;
177 my $a;
178 #ip with subnet in decimal
179 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d{1,2})$/){
180 $adr=int($1).".".int($2).".".int($3).".".int($4);
181 my $b = &General::iporsubtodec($5);
182 $a=$adr."/".$b;
183 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
184 $adr=int($1).".".int($2).".".int($3).".".int($4);
185 if(&General::validip($adr)){
186 $a=$adr."/32";
187 }
188 }
189 if(&General::validipandmask($adr)){
190 $a=&General::iporsubtodec($adr);
191 }
192 return $a;
193}