UPNP: changed firewall chain from PORTFW to UPNPFW
[people/teissler/ipfire-2.x.git] / html / cgi-bin / forwardfw.cgi
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. As a prerequisite i needed #
24 # something that makes sure the vpn roadwarrior are able to have a fixed #
25 # ip-address. So i developed the ccd extension for the vpn server. #
26 # #
27 # Now that the ccd extension is ready i am able to develop the main request. #
28 # Any feedback is appreciated. #
29 # #
30 #Copymaster #
31 ###############################################################################
32
33 use strict;
34 use Sort::Naturally;
35 no warnings 'uninitialized';
36 # enable only the following on debugging purpose
37 #use warnings;
38 #use CGI::Carp 'fatalsToBrowser';
39
40 require '/var/ipfire/general-functions.pl';
41 require "${General::swroot}/lang.pl";
42 require "${General::swroot}/header.pl";
43 require "${General::swroot}/forward/bin/firewall-lib.pl";
44
45 unless (-d "${General::swroot}/forward") { system("mkdir ${General::swroot}/forward"); }
46 unless (-e "${General::swroot}/forward/settings") { system("touch ${General::swroot}/forward/settings"); }
47 unless (-e "${General::swroot}/forward/config") { system("touch ${General::swroot}/forward/config"); }
48 unless (-e "${General::swroot}/forward/input") { system("touch ${General::swroot}/forward/input"); }
49 unless (-e "${General::swroot}/forward/outgoing") { system("touch ${General::swroot}/forward/outgoing"); }
50 unless (-e "${General::swroot}/forward/nat") { system("touch ${General::swroot}/forward/nat"); }
51
52 my %fwdfwsettings=();
53 my %selected=() ;
54 my %defaultNetworks=();
55 my %netsettings=();
56 my %customhost=();
57 my %customgrp=();
58 my %customnetworks=();
59 my %customservice=();
60 my %customservicegrp=();
61 my %ccdnet=();
62 my %customnetwork=();
63 my %ccdhost=();
64 my %configfwdfw=();
65 my %configinputfw=();
66 my %configoutgoingfw=();
67 my %ipsecconf=();
68 my %color=();
69 my %mainsettings=();
70 my %checked=();
71 my %icmptypes=();
72 my %ovpnsettings=();
73 my %ipsecsettings=();
74 my %aliases=();
75 my %optionsfw=();
76 my %ifaces=();
77
78 my $VERSION='0.9.9.8';
79 my $color;
80 my $confignet = "${General::swroot}/fwhosts/customnetworks";
81 my $confighost = "${General::swroot}/fwhosts/customhosts";
82 my $configgrp = "${General::swroot}/fwhosts/customgroups";
83 my $configsrv = "${General::swroot}/fwhosts/customservices";
84 my $configsrvgrp = "${General::swroot}/fwhosts/customservicegrp";
85 my $configccdnet = "${General::swroot}/ovpn/ccd.conf";
86 my $configccdhost = "${General::swroot}/ovpn/ovpnconfig";
87 my $configipsec = "${General::swroot}/vpn/config";
88 my $configipsecrw = "${General::swroot}/vpn/settings";
89 my $configfwdfw = "${General::swroot}/forward/config";
90 my $configinput = "${General::swroot}/forward/input";
91 my $configoutgoing = "${General::swroot}/forward/outgoing";
92 my $configovpn = "${General::swroot}/ovpn/settings";
93 my $fwoptions = "${General::swroot}/optionsfw/settings";
94 my $ifacesettings = "${General::swroot}/ethernet/settings";
95 my $errormessage='';
96 my $hint='';
97 my $ipgrp="${General::swroot}/outgoing/groups";
98 my $tdcolor='';
99 my $checkorange='';
100 my @protocols;
101 &General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings);
102 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
103 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
104 &General::readhash($fwoptions, \%optionsfw);
105 &General::readhash($ifacesettings, \%ifaces);
106 &General::readhash("$configovpn", \%ovpnsettings);
107 &General::readhash("$configipsecrw", \%ipsecsettings);
108 &General::readhasharray("$configipsec", \%ipsecconf);
109 &Header::showhttpheaders();
110 &Header::getcgihash(\%fwdfwsettings);
111 &Header::openpage($Lang::tr{'fwdfw menu'}, 1, '');
112 &Header::openbigbox('100%', 'center',$errormessage);
113 #### ACTION #####
114
115 if ($fwdfwsettings{'ACTION'} eq 'saverule')
116 {
117 &General::readhasharray("$configfwdfw", \%configfwdfw);
118 &General::readhasharray("$configinput", \%configinputfw);
119 &General::readhasharray("$configoutgoing", \%configoutgoingfw);
120 $errormessage=&checksource;
121 if(!$errormessage){&checktarget;}
122 if(!$errormessage){&checkrule;}
123
124 #check if manual ip (source) is orange network
125 if ($fwdfwsettings{'grp1'} eq 'src_addr'){
126 my ($sip,$scidr) = split("/",$fwdfwsettings{$fwdfwsettings{'grp1'}});
127 if ( &General::IpInSubnet($sip,$netsettings{'ORANGE_ADDRESS'},$netsettings{'ORANGE_NETMASK'})){
128 $checkorange='on';
129 }
130 }
131 #check useless rules
132 if( ($fwdfwsettings{$fwdfwsettings{'grp1'}} eq 'ORANGE' || $checkorange eq 'on') && $fwdfwsettings{'grp2'} eq 'ipfire'){
133 $errormessage.=$Lang::tr{'fwdfw useless rule'}."<br>";
134 }
135 #check if we try to break rules
136 if( $fwdfwsettings{$fwdfwsettings{'grp1'}} eq 'IPFire' && $fwdfwsettings{'grp2'} eq 'ipfire'){
137 $errormessage.=$Lang::tr{'fwdfw err same'};
138 }
139 #INPUT part
140 if($fwdfwsettings{'grp2'} eq 'ipfire' && $fwdfwsettings{$fwdfwsettings{'grp1'}} ne 'ORANGE'){
141 $fwdfwsettings{'config'}=$configinput;
142 $fwdfwsettings{'chain'} = 'INPUTFW';
143 my $maxkey=&General::findhasharraykey(\%configinputfw);
144 #check if we have an identical rule already
145 if($fwdfwsettings{'oldrulenumber'} eq $fwdfwsettings{'rulepos'}){
146 foreach my $key (sort keys %configinputfw){
147 if ("$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'}"
148 eq "$configinputfw{$key}[0],$configinputfw{$key}[2],$configinputfw{$key}[3],$configinputfw{$key}[4],$configinputfw{$key}[5],$configinputfw{$key}[6],$configinputfw{$key}[7],$configinputfw{$key}[8],$configinputfw{$key}[9],$configinputfw{$key}[10],$configinputfw{$key}[11],$configinputfw{$key}[12],$configinputfw{$key}[13],$configinputfw{$key}[14],$configinputfw{$key}[15],$configinputfw{$key}[17],$configinputfw{$key}[18],$configinputfw{$key}[19],$configinputfw{$key}[20],$configinputfw{$key}[21],$configinputfw{$key}[22],$configinputfw{$key}[23],$configinputfw{$key}[24],$configinputfw{$key}[25],$configinputfw{$key}[26],$configinputfw{$key}[27]"){
149 $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
150 if ($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on'){
151 $errormessage='';
152 }elsif($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
153 $errormessage=$Lang::tr{'fwdfw err remark'}."<br>";
154 }
155 if ($fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'}){
156 $fwdfwsettings{'nosave'} = 'on';
157 }
158 }
159 }
160 }
161 #check Rulepos on new Rule
162 if($fwdfwsettings{'rulepos'} > 0 && !$fwdfwsettings{'oldrulenumber'}){
163 $fwdfwsettings{'oldrulenumber'}=$maxkey;
164 foreach my $key (sort keys %configinputfw){
165 if ("$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'}"
166 eq "$configinputfw{$key}[0],$configinputfw{$key}[2],$configinputfw{$key}[3],$configinputfw{$key}[4],$configinputfw{$key}[5],$configinputfw{$key}[6],$configinputfw{$key}[7],$configinputfw{$key}[8],$configinputfw{$key}[9],$configinputfw{$key}[10],$configinputfw{$key}[11],$configinputfw{$key}[12],$configinputfw{$key}[13],$configinputfw{$key}[14],$configinputfw{$key}[15],$configinputfw{$key}[17],$configinputfw{$key}[18],$configinputfw{$key}[19],$configinputfw{$key}[20],$configinputfw{$key}[21],$configinputfw{$key}[22],$configinputfw{$key}[23],$configinputfw{$key}[24],$configinputfw{$key}[25],$configinputfw{$key}[26],$configinputfw{$key}[27]"){
167 $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
168 }
169 }
170 }
171 #check if we just close a rule
172 if( $fwdfwsettings{'oldgrp1a'} eq $fwdfwsettings{'grp1'} && $fwdfwsettings{'oldgrp1b'} eq $fwdfwsettings{$fwdfwsettings{'grp1'}} && $fwdfwsettings{'oldgrp2a'} eq $fwdfwsettings{'grp2'} && $fwdfwsettings{'oldgrp2b'} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{'oldgrp3a'} eq $fwdfwsettings{'grp3'} && $fwdfwsettings{'oldgrp3b'} eq $fwdfwsettings{$fwdfwsettings{'grp3'}} && $fwdfwsettings{'oldusesrv'} eq $fwdfwsettings{'USESRV'} && $fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'oldruletype'} eq $fwdfwsettings{'chain'}) {
173 if($fwdfwsettings{'nosave'} eq 'on' && $fwdfwsettings{'updatefwrule'} eq 'on'){
174 $errormessage='';
175 $fwdfwsettings{'nosave2'} = 'on';
176 }
177 }
178 &checkcounter($fwdfwsettings{'oldgrp1a'},$fwdfwsettings{'oldgrp1b'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}});
179 if ($fwdfwsettings{'nobase'} ne 'on'){
180 &checkcounter($fwdfwsettings{'oldgrp2a'},$fwdfwsettings{'oldgrp2b'},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}});
181 }
182 if($fwdfwsettings{'oldusesrv'} eq '' && $fwdfwsettings{'USESRV'} eq 'ON'){
183 &checkcounter(0,0,$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
184 }elsif ($fwdfwsettings{'USESRV'} eq '' && $fwdfwsettings{'oldusesrv'} eq 'ON') {
185 &checkcounter($fwdfwsettings{'oldgrp3a'},$fwdfwsettings{'oldgrp3b'},0,0);
186 }elsif ($fwdfwsettings{'oldusesrv'} eq $fwdfwsettings{'USESRV'} && $fwdfwsettings{'oldgrp3b'} ne $fwdfwsettings{$fwdfwsettings{'grp3'}} && $fwdfwsettings{'updatefwrule'} eq 'on'){
187 &checkcounter($fwdfwsettings{'oldgrp3a'},$fwdfwsettings{'oldgrp3b'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
188 }
189 if($fwdfwsettings{'nosave2'} ne 'on'){
190 &saverule(\%configinputfw,$configinput);
191 }
192 }elsif($fwdfwsettings{$fwdfwsettings{'grp1'}} eq 'IPFire' ){
193 # OUTGOING PART
194 $fwdfwsettings{'config'}=$configoutgoing;
195 $fwdfwsettings{'chain'} = 'OUTGOINGFW';
196 my $maxkey=&General::findhasharraykey(\%configoutgoingfw);
197 if($fwdfwsettings{'oldrulenumber'} eq $fwdfwsettings{'rulepos'}){
198 foreach my $key (sort keys %configoutgoingfw){
199 if ("$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'}"
200 eq "$configoutgoingfw{$key}[0],$configoutgoingfw{$key}[2],$configoutgoingfw{$key}[3],$configoutgoingfw{$key}[4],$configoutgoingfw{$key}[5],$configoutgoingfw{$key}[6],$configoutgoingfw{$key}[7],$configoutgoingfw{$key}[8],$configoutgoingfw{$key}[9],$configoutgoingfw{$key}[10],$configoutgoingfw{$key}[11],$configoutgoingfw{$key}[12],$configoutgoingfw{$key}[13],$configoutgoingfw{$key}[14],$configoutgoingfw{$key}[15],$configoutgoingfw{$key}[17],$configoutgoingfw{$key}[18],$configoutgoingfw{$key}[19],$configoutgoingfw{$key}[20],$configoutgoingfw{$key}[21],$configoutgoingfw{$key}[22],$configoutgoingfw{$key}[23],$configoutgoingfw{$key}[24],$configoutgoingfw{$key}[25],$configoutgoingfw{$key}[26],$configoutgoingfw{$key}[27]"){
201 $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
202 if ($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on'){
203 $errormessage='';
204 }elsif($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
205 $errormessage=$Lang::tr{'fwdfw err remark'}."<br>";
206 }
207 if ($fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'}){
208 $fwdfwsettings{'nosave'} = 'on';
209 }
210 }
211 }
212 }
213 #check Rulepos on new Rule
214 if($fwdfwsettings{'rulepos'} > 0 && !$fwdfwsettings{'oldrulenumber'}){
215 print"CHECK OUTGOING DOPPELTE REGEL<br>";
216 $fwdfwsettings{'oldrulenumber'}=$maxkey;
217 foreach my $key (sort keys %configoutgoingfw){
218 if ("$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'}"
219 eq "$configoutgoingfw{$key}[0],$configoutgoingfw{$key}[2],$configoutgoingfw{$key}[3],$configoutgoingfw{$key}[4],$configoutgoingfw{$key}[5],$configoutgoingfw{$key}[6],$configoutgoingfw{$key}[7],$configoutgoingfw{$key}[8],$configoutgoingfw{$key}[9],$configoutgoingfw{$key}[10],$configoutgoingfw{$key}[11],$configoutgoingfw{$key}[12],$configoutgoingfw{$key}[13],$configoutgoingfw{$key}[14],$configoutgoingfw{$key}[15],$configoutgoingfw{$key}[17],$configoutgoingfw{$key}[18],$configoutgoingfw{$key}[19],$configoutgoingfw{$key}[20],$configoutgoingfw{$key}[21],$configoutgoingfw{$key}[22],$configoutgoingfw{$key}[23],$configoutgoingfw{$key}[24],$configoutgoingfw{$key}[25],$configoutgoingfw{$key}[26],$configoutgoingfw{$key}[27]"){
220 $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
221 }
222 }
223 }
224 #check if we just close a rule
225 if( $fwdfwsettings{'oldgrp1a'} eq $fwdfwsettings{'grp1'} && $fwdfwsettings{'oldgrp1b'} eq $fwdfwsettings{$fwdfwsettings{'grp1'}} && $fwdfwsettings{'oldgrp2a'} eq $fwdfwsettings{'grp2'} && $fwdfwsettings{'oldgrp2b'} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{'oldgrp3a'} eq $fwdfwsettings{'grp3'} && $fwdfwsettings{'oldgrp3b'} eq $fwdfwsettings{$fwdfwsettings{'grp3'}} && $fwdfwsettings{'oldusesrv'} eq $fwdfwsettings{'USESRV'} && $fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'oldruletype'} eq $fwdfwsettings{'chain'}) {
226 if($fwdfwsettings{'nosave'} eq 'on' && $fwdfwsettings{'updatefwrule'} eq 'on'){
227 $fwdfwsettings{'nosave2'} = 'on';
228 $errormessage='';
229 }
230 }
231 #increase counters
232 &checkcounter($fwdfwsettings{'oldgrp1a'},$fwdfwsettings{'oldgrp1b'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}});
233 &checkcounter($fwdfwsettings{'oldgrp2a'},$fwdfwsettings{'oldgrp2b'},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}});
234 if($fwdfwsettings{'oldusesrv'} eq '' && $fwdfwsettings{'USESRV'} eq 'ON'){
235 &checkcounter(0,0,$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
236 }elsif ($fwdfwsettings{'USESRV'} eq '' && $fwdfwsettings{'oldusesrv'} eq 'ON') {
237 &checkcounter($fwdfwsettings{'oldgrp3a'},$fwdfwsettings{'oldgrp3b'},0,0);
238 }elsif ($fwdfwsettings{'oldusesrv'} eq $fwdfwsettings{'USESRV'} && $fwdfwsettings{'oldgrp3b'} ne $fwdfwsettings{$fwdfwsettings{'grp3'}} && $fwdfwsettings{'updatefwrule'} eq 'on'){
239 &checkcounter($fwdfwsettings{'oldgrp3a'},$fwdfwsettings{'oldgrp3b'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
240 }
241 if ($fwdfwsettings{'nobase'} eq 'on'){
242 &checkcounter(0,0,$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
243 }
244 if ($fwdfwsettings{'nosave2'} ne 'on'){
245 &saverule(\%configoutgoingfw,$configoutgoing);
246 }
247 }else{
248 #FORWARD PART
249 $fwdfwsettings{'config'}=$configfwdfw;
250 $fwdfwsettings{'chain'} = 'FORWARDFW';
251 my $maxkey=&General::findhasharraykey(\%configfwdfw);
252 if($fwdfwsettings{'oldrulenumber'} eq $fwdfwsettings{'rulepos'}){
253 #check if we have an identical rule already
254 foreach my $key (sort keys %configfwdfw){
255 if ("$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'}"
256 eq "$configfwdfw{$key}[0],$configfwdfw{$key}[2],$configfwdfw{$key}[3],$configfwdfw{$key}[4],$configfwdfw{$key}[5],$configfwdfw{$key}[6],$configfwdfw{$key}[7],$configfwdfw{$key}[8],$configfwdfw{$key}[9],$configfwdfw{$key}[10],$configfwdfw{$key}[11],$configfwdfw{$key}[12],$configfwdfw{$key}[13],$configfwdfw{$key}[14],$configfwdfw{$key}[15],$configfwdfw{$key}[17],$configfwdfw{$key}[19],$configfwdfw{$key}[20],$configfwdfw{$key}[21],$configfwdfw{$key}[22],$configfwdfw{$key}[23],$configfwdfw{$key}[24],$configfwdfw{$key}[25],$configfwdfw{$key}[26],$configfwdfw{$key}[27]"){
257 $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
258 if ($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' ){
259 $errormessage='';
260 }elsif($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
261 $errormessage=$Lang::tr{'fwdfw err remark'}."<br>";
262 }
263 if ($fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'}){
264 $fwdfwsettings{'nosave'} = 'on';
265 }
266 }
267 }
268 }
269 #check Rulepos on new Rule
270 if($fwdfwsettings{'rulepos'} > 0 && !$fwdfwsettings{'oldrulenumber'}){
271 $fwdfwsettings{'oldrulenumber'}=$maxkey;
272 foreach my $key (sort keys %configfwdfw){
273 if ("$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'}"
274 eq "$configfwdfw{$key}[0],$configfwdfw{$key}[2],$configfwdfw{$key}[3],$configfwdfw{$key}[4],$configfwdfw{$key}[5],$configfwdfw{$key}[6],$configfwdfw{$key}[7],$configfwdfw{$key}[8],$configfwdfw{$key}[9],$configfwdfw{$key}[10],$configfwdfw{$key}[11],$configfwdfw{$key}[12],$configfwdfw{$key}[13],$configfwdfw{$key}[14],$configfwdfw{$key}[15],$configfwdfw{$key}[17],$configfwdfw{$key}[19],$configfwdfw{$key}[20],$configfwdfw{$key}[21],$configfwdfw{$key}[22],$configfwdfw{$key}[23],$configfwdfw{$key}[24],$configfwdfw{$key}[25],$configfwdfw{$key}[26],$configfwdfw{$key}[27]"){
275 $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
276 }
277 }
278 }
279 #check if we just close a rule
280 if( $fwdfwsettings{'oldgrp1a'} eq $fwdfwsettings{'grp1'} && $fwdfwsettings{'oldgrp1b'} eq $fwdfwsettings{$fwdfwsettings{'grp1'}} && $fwdfwsettings{'oldgrp2a'} eq $fwdfwsettings{'grp2'} && $fwdfwsettings{'oldgrp2b'} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{'oldgrp3a'} eq $fwdfwsettings{'grp3'} && $fwdfwsettings{'oldgrp3b'} eq $fwdfwsettings{$fwdfwsettings{'grp3'}} && $fwdfwsettings{'oldusesrv'} eq $fwdfwsettings{'USESRV'} && $fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'oldruletype'} eq $fwdfwsettings{'chain'}) {
281 if($fwdfwsettings{'nosave'} eq 'on' && $fwdfwsettings{'updatefwrule'} eq 'on'){
282 $fwdfwsettings{'nosave2'} = 'on';
283 $errormessage='';
284 }
285 }
286 #increase counters
287 &checkcounter($fwdfwsettings{'oldgrp1a'},$fwdfwsettings{'oldgrp1b'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}});
288 &checkcounter($fwdfwsettings{'oldgrp2a'},$fwdfwsettings{'oldgrp2b'},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}});
289 if($fwdfwsettings{'oldusesrv'} eq '' && $fwdfwsettings{'USESRV'} eq 'ON'){
290 &checkcounter(0,0,$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
291 }elsif ($fwdfwsettings{'USESRV'} eq '' && $fwdfwsettings{'oldusesrv'} eq 'ON') {
292 &checkcounter($fwdfwsettings{'oldgrp3a'},$fwdfwsettings{'oldgrp3b'},0,0);
293 }elsif ($fwdfwsettings{'oldusesrv'} eq $fwdfwsettings{'USESRV'} && $fwdfwsettings{'oldgrp3b'} ne $fwdfwsettings{$fwdfwsettings{'grp3'}} && $fwdfwsettings{'updatefwrule'} eq 'on'){
294 &checkcounter($fwdfwsettings{'oldgrp3a'},$fwdfwsettings{'oldgrp3b'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
295 }
296 if ($fwdfwsettings{'nobase'} eq 'on'){
297 &checkcounter(0,0,$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
298 }
299 if ($fwdfwsettings{'nosave2'} ne 'on'){
300 &saverule(\%configfwdfw,$configfwdfw);
301 }
302 }
303 if ($errormessage){
304 &newrule;
305 }else{
306 if($fwdfwsettings{'nosave2'} ne 'on'){
307 &rules;
308 }
309 &base;
310 }
311 }
312 if ($fwdfwsettings{'ACTION'} eq $Lang::tr{'fwdfw newrule'})
313 {
314 &newrule;
315 }
316 if ($fwdfwsettings{'ACTION'} eq $Lang::tr{'fwdfw toggle'})
317 {
318 my %togglehash=();
319 &General::readhasharray($fwdfwsettings{'config'}, \%togglehash);
320 foreach my $key (sort keys %togglehash){
321 if ($key eq $fwdfwsettings{'key'}){
322 if ($togglehash{$key}[2] eq 'ON'){$togglehash{$key}[2]='';}else{$togglehash{$key}[2]='ON';}
323 }
324 }
325 &General::writehasharray($fwdfwsettings{'config'}, \%togglehash);
326 &rules;
327 &base;
328 }
329 if ($fwdfwsettings{'ACTION'} eq $Lang::tr{'fwdfw togglelog'})
330 {
331 my %togglehash=();
332 &General::readhasharray($fwdfwsettings{'config'}, \%togglehash);
333 foreach my $key (sort keys %togglehash){
334 if ($key eq $fwdfwsettings{'key'}){
335 if ($togglehash{$key}[17] eq 'ON'){$togglehash{$key}[17]='';}else{$togglehash{$key}[17]='ON';}
336 }
337 }
338 &General::writehasharray($fwdfwsettings{'config'}, \%togglehash);
339 &rules;
340 &base;
341 }
342 if ($fwdfwsettings{'ACTION'} eq $Lang::tr{'fwdfw reread'})
343 {
344 &reread_rules;
345 &base;
346 }
347 if ($fwdfwsettings{'ACTION'} eq 'editrule')
348 {
349 $fwdfwsettings{'updatefwrule'}='on';
350 &newrule;
351 }
352 if ($fwdfwsettings{'ACTION'} eq 'deleterule')
353 {
354 &deleterule;
355 }
356 if ($fwdfwsettings{'ACTION'} eq 'moveup')
357 {
358 &pos_up;
359 &base;
360 }
361 if ($fwdfwsettings{'ACTION'} eq 'movedown')
362 {
363 &pos_down;
364 &base;
365 }
366 if ($fwdfwsettings{'ACTION'} eq 'copyrule')
367 {
368 $fwdfwsettings{'copyfwrule'}='on';
369 &newrule;
370 }
371 if ($fwdfwsettings{'ACTION'} eq '')
372 {
373 &base;
374 }
375 ### Functions ####
376 sub addrule
377 {
378 &error;
379 if (-f "${General::swroot}/forward/reread"){
380 print "<table border='1' rules='groups' bgcolor='lightgreen' width='100%'><form method='post'><td><div style='font-size:11pt; font-weight: bold;vertical-align: middle; '><input type='submit' name='ACTION' value='$Lang::tr{'fwdfw reread'}' style='font-face: Comic Sans MS; color: green; font-weight: bold; font-size: 14pt;'>&nbsp &nbsp $Lang::tr{'fwhost reread'}</div</td></tr></table></form><hr><br>";
381 }
382 &Header::openbox('100%', 'left', $Lang::tr{'fwdfw menu'});
383 print "<form method='post'>";
384 print "<table border='0'>";
385 print "<tr><td><input type='submit' name='ACTION' value='$Lang::tr{'fwdfw newrule'}'></td>";
386 print"</tr></table></form><hr>";
387 &Header::closebox();
388 &viewtablerule;
389 }
390 sub base
391 {
392 &hint;
393 &addrule;
394 print "<br><br>";
395 print "<br><br><div align='right'><font size='1' color='grey'>Version: $VERSION</font></div>";
396 }
397 sub changerule
398 {
399 my $oldchain=shift;
400 $fwdfwsettings{'updatefwrule'}='';
401 $fwdfwsettings{'config'}=$oldchain;
402 $fwdfwsettings{'nobase'}='on';
403 &deleterule;
404 &checkcounter(0,0,$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}});
405 &checkcounter(0,0,$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}});
406 }
407 sub checksource
408 {
409 my ($ip,$subnet);
410 #check ip-address if manual
411 if ($fwdfwsettings{'src_addr'} eq $fwdfwsettings{$fwdfwsettings{'grp1'}} && $fwdfwsettings{'src_addr'} ne ''){
412 #check if ip with subnet
413 if ($fwdfwsettings{'src_addr'} =~ /^(.*?)\/(.*?)$/) {
414 ($ip,$subnet)=split (/\//,$fwdfwsettings{'src_addr'});
415 $subnet = &General::iporsubtocidr($subnet);
416 $fwdfwsettings{'isip'}='on';
417 }
418 #check if only ip
419 if($fwdfwsettings{'src_addr'}=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
420 $ip=$fwdfwsettings{'src_addr'};
421 $subnet = '32';
422 $fwdfwsettings{'isip'}='on';
423 }
424
425 if ($fwdfwsettings{'isip'} ne 'on'){
426 if (&General::validmac($fwdfwsettings{'src_addr'})){$fwdfwsettings{'ismac'}='on';}
427 }
428 if ($fwdfwsettings{'isip'} eq 'on'){
429 ##check if ip is valid
430 if (! &General::validip($ip)){
431 $errormessage.=$Lang::tr{'fwdfw err src_addr'}."<br>";
432 return $errormessage;
433 }
434 #check and form valid IP
435 $ip=&General::ip2dec($ip);
436 $ip=&General::dec2ip($ip);
437 #check if net or broadcast
438 my @tmp= split (/\./,$ip);
439 if (($tmp[3] eq "0") || ($tmp[3] eq "255"))
440 {
441 $errormessage=$Lang::tr{'fwhost err hostip'}."<br>";
442 return $errormessage;
443 }
444 $fwdfwsettings{'src_addr'}="$ip/$subnet";
445 if(!&General::validipandmask($fwdfwsettings{'src_addr'})){
446 $errormessage.=$Lang::tr{'fwdfw err src_addr'}."<br>";
447 return $errormessage;
448 }
449 }
450 if ($fwdfwsettings{'isip'} ne 'on' && $fwdfwsettings{'ismac'} ne 'on'){
451 $errormessage.=$Lang::tr{'fwdfw err src_addr'}."<br>";
452 return $errormessage;
453 }
454 }elsif($fwdfwsettings{'src_addr'} eq $fwdfwsettings{$fwdfwsettings{'grp1'}} && $fwdfwsettings{'src_addr'} eq ''){
455 $errormessage.=$Lang::tr{'fwdfw err nosrcip'};
456 return $errormessage;
457 }
458
459 #check empty fields
460 if ($fwdfwsettings{$fwdfwsettings{'grp1'}} eq ''){ $errormessage.=$Lang::tr{'fwdfw err nosrc'}."<br>";}
461 #check icmp source
462 if ($fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && $fwdfwsettings{'PROT'} eq 'ICMP'){
463 $fwdfwsettings{'SRC_PORT'}='';
464 &General::readhasharray("${General::swroot}/fwhosts/icmp-types", \%icmptypes);
465 foreach my $key (keys %icmptypes){
466 if($fwdfwsettings{'ICMP_TYPES'} eq "$icmptypes{$key}[0] ($icmptypes{$key}[1])"){
467 $fwdfwsettings{'ICMP_TYPES'}="$icmptypes{$key}[0]";
468 }
469 }
470 }elsif($fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && $fwdfwsettings{'PROT'} eq 'GRE'){
471 $fwdfwsettings{'SRC_PORT'}='';
472 $fwdfwsettings{'ICMP_TYPES'}='';
473 }elsif($fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && $fwdfwsettings{'PROT'} eq 'ESP'){
474 $fwdfwsettings{'SRC_PORT'}='';
475 $fwdfwsettings{'ICMP_TYPES'}='';
476 }elsif($fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && $fwdfwsettings{'PROT'} eq 'AH'){
477 $fwdfwsettings{'SRC_PORT'}='';
478 $fwdfwsettings{'ICMP_TYPES'}='';
479 }elsif($fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && $fwdfwsettings{'PROT'} ne 'ICMP'){
480 $fwdfwsettings{'ICMP_TYPES'}='';
481 }else{
482 $fwdfwsettings{'ICMP_TYPES'}='';
483 $fwdfwsettings{'SRC_PORT'}='';
484 $fwdfwsettings{'PROT'}='';
485 }
486
487 if($fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && ($fwdfwsettings{'PROT'} eq 'TCP' || $fwdfwsettings{'PROT'} eq 'UDP') && $fwdfwsettings{'SRC_PORT'} ne ''){
488 my @parts=split(",",$fwdfwsettings{'SRC_PORT'});
489 my @values=();
490 foreach (@parts){
491 chomp($_);
492 if ($_ =~ /^(\d+)\-(\d+)$/ || $_ =~ /^(\d+)\:(\d+)$/) {
493 my $check;
494 #change dashes with :
495 $_=~ tr/-/:/;
496 if ($_ eq "*") {
497 push(@values,"1:65535");
498 $check='on';
499 }
500 if ($_ =~ /^(\D)\:(\d+)$/ || $_ =~ /^(\D)\-(\d+)$/) {
501 push(@values,"1:$2");
502 $check='on';
503 }
504 if ($_ =~ /^(\d+)\:(\D)$/ || $_ =~ /^(\d+)\-(\D)$/ ) {
505 push(@values,"$1:65535");
506 $check='on'
507 }
508 $errormessage .= &General::validportrange($_, 'destination');
509 if(!$check){
510 push (@values,$_);
511 }
512 }else{
513 if (&General::validport($_)){
514 push (@values,$_);
515 }else{
516
517 }
518 }
519 }
520 $fwdfwsettings{'SRC_PORT'}=join("|",@values);
521 }
522 return $errormessage;
523 }
524 sub checktarget
525 {
526 my ($ip,$subnet);
527 &General::readhasharray("$configsrv", \%customservice);
528 #check DNAT settings (has to be single Host and single Port or portrange)
529 if ($fwdfwsettings{'USE_NAT'} eq 'ON' && $fwdfwsettings{'nat'} eq 'dnat'){
530 if($fwdfwsettings{'grp2'} eq 'tgt_addr' || $fwdfwsettings{'grp2'} eq 'cust_host_tgt' || $fwdfwsettings{'grp2'} eq 'ovpn_host_tgt'){
531 if ($fwdfwsettings{'USESRV'} eq '' && $fwdfwsettings{'dnatport'} eq ''){
532 $errormessage=$Lang::tr{'fwdfw target'}.": ".$Lang::tr{'fwdfw dnat porterr'}."<br>";
533 return $errormessage;
534 }
535 #check if manual ip is a single Host (if set)
536 if ($fwdfwsettings{'grp2'} eq 'tgt_addr'){
537 my @tmp= split (/\./,$fwdfwsettings{$fwdfwsettings{'grp2'}});
538 my @tmp1= split ("/",$tmp[3]);
539 if (($tmp1[0] eq "0") || ($tmp1[0] eq "255"))
540 {
541 $errormessage=$Lang::tr{'fwdfw dnat error'}."<br>";
542 return $errormessage;
543 }
544 }
545 #check if Port is a single Port or portrange
546 if ($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'grp3'} eq 'TGT_PORT'){
547 if(($fwdfwsettings{'TGT_PROT'} ne 'TCP'|| $fwdfwsettings{'TGT_PROT'} ne 'UDP') && $fwdfwsettings{'TGT_PORT'} eq ''){
548 $errormessage=$Lang::tr{'fwdfw target'}.": ".$Lang::tr{'fwdfw dnat porterr'}."<br>";
549 return $errormessage;
550 }
551 if (($fwdfwsettings{'TGT_PROT'} eq 'TCP'|| $fwdfwsettings{'TGT_PROT'} eq 'UDP') && $fwdfwsettings{'TGT_PORT'} ne '' && !&check_natport($fwdfwsettings{'TGT_PORT'})){
552 $errormessage=$Lang::tr{'fwdfw target'}.": ".$Lang::tr{'fwdfw dnat porterr'}."<br>";
553 return $errormessage;
554 }
555 }
556 }else{
557 $errormessage=$Lang::tr{'fwdfw dnat error'}."<br>";
558 return $errormessage;
559 }
560 }
561 if ($fwdfwsettings{'tgt_addr'} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{'tgt_addr'} ne ''){
562 #check if ip with subnet
563 if ($fwdfwsettings{'tgt_addr'} =~ /^(.*?)\/(.*?)$/) {
564 ($ip,$subnet)=split (/\//,$fwdfwsettings{'tgt_addr'});
565 $subnet = &General::iporsubtocidr($subnet);
566 }
567 #check if only ip
568 if($fwdfwsettings{'tgt_addr'}=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
569 $ip=$fwdfwsettings{'tgt_addr'};
570 $subnet='32';
571 }
572 #check if ip is valid
573 if (! &General::validip($ip)){
574 $errormessage.=$Lang::tr{'fwdfw err tgt_addr'}."<br>";
575 return $errormessage;
576 }
577 #check and form valid IP
578 $ip=&General::ip2dec($ip);
579 $ip=&General::dec2ip($ip);
580 $fwdfwsettings{'tgt_addr'}="$ip/$subnet";
581 if(!&General::validipandmask($fwdfwsettings{'tgt_addr'})){
582 $errormessage.=$Lang::tr{'fwdfw err tgt_addr'}."<br>";
583 return $errormessage;
584 }
585 }elsif($fwdfwsettings{'tgt_addr'} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{'tgt_addr'} eq ''){
586 $errormessage.=$Lang::tr{'fwdfw err notgtip'};
587 return $errormessage;
588 }
589 #check empty fields
590 if ($fwdfwsettings{$fwdfwsettings{'grp2'}} eq ''){ $errormessage.=$Lang::tr{'fwdfw err notgt'}."<br>";}
591 #check tgt services
592 if ($fwdfwsettings{'USESRV'} eq 'ON'){
593 if ($fwdfwsettings{'grp3'} eq 'cust_srv'){
594 $fwdfwsettings{'TGT_PROT'}='';
595 $fwdfwsettings{'ICMP_TGT'}='';
596 }
597 if ($fwdfwsettings{'grp3'} eq 'cust_srvgrp'){
598 $fwdfwsettings{'TGT_PROT'}='';
599 $fwdfwsettings{'ICMP_TGT'}='';
600 #check target service
601 if($fwdfwsettings{$fwdfwsettings{'grp3'}} eq ''){
602 $errormessage.=$Lang::tr{'fwdfw err tgt_grp'};
603 }
604 }
605 if ($fwdfwsettings{'grp3'} eq 'TGT_PORT'){
606 if ($fwdfwsettings{'TGT_PROT'} eq 'TCP' || $fwdfwsettings{'TGT_PROT'} eq 'UDP'){
607 if ($fwdfwsettings{'TGT_PORT'} ne ''){
608 if ($fwdfwsettings{'TGT_PORT'} =~ "," && $fwdfwsettings{'USE_NAT'} && $fwdfwsettings{'nat'} eq 'dnat') {
609 $errormessage=$Lang::tr{'fwdfw dnat porterr'}."<br>";
610 return $errormessage;
611 }
612 my @parts=split(",",$fwdfwsettings{'TGT_PORT'});
613 my @values=();
614 foreach (@parts){
615 chomp($_);
616 if ($_ =~ /^(\d+)\-(\d+)$/ || $_ =~ /^(\d+)\:(\d+)$/) {
617 my $check;
618 #change dashes with :
619 $_=~ tr/-/:/;
620 if ($_ eq "*") {
621 push(@values,"1:65535");
622 $check='on';
623 }
624 if ($_ =~ /^(\D)\:(\d+)$/ || $_ =~ /^(\D)\-(\d+)$/) {
625 push(@values,"1:$2");
626 $check='on';
627 }
628 if ($_ =~ /^(\d+)\:(\D)$/ || $_ =~ /^(\d+)\-(\D)$/) {
629 push(@values,"$1:65535");
630 $check='on'
631 }
632 $errormessage .= &General::validportrange($_, 'destination');
633 if(!$check){
634 push (@values,$_);
635 }
636 }else{
637 if (&General::validport($_)){
638 push (@values,$_);
639 }else{
640
641 }
642 }
643 }
644 $fwdfwsettings{'TGT_PORT'}=join("|",@values);
645 }
646 }elsif ($fwdfwsettings{'TGT_PROT'} eq 'GRE'){
647 $fwdfwsettings{$fwdfwsettings{'grp3'}} = '';
648 $fwdfwsettings{'TGT_PORT'} = '';
649 $fwdfwsettings{'ICMP_TGT'} = '';
650 }elsif($fwdfwsettings{'TGT_PROT'} eq 'ESP'){
651 $fwdfwsettings{$fwdfwsettings{'grp3'}} = '';
652 $fwdfwsettings{'TGT_PORT'} = '';
653 $fwdfwsettings{'ICMP_TGT'}='';
654 }elsif($fwdfwsettings{'TGT_PROT'} eq 'AH'){
655 $fwdfwsettings{$fwdfwsettings{'grp3'}} = '';
656 $fwdfwsettings{'TGT_PORT'} = '';
657 $fwdfwsettings{'ICMP_TGT'}='';
658 }elsif ($fwdfwsettings{'TGT_PROT'} eq 'ICMP'){
659 $fwdfwsettings{$fwdfwsettings{'grp3'}} = '';
660 $fwdfwsettings{'TGT_PORT'} = '';
661 &General::readhasharray("${General::swroot}/fwhosts/icmp-types", \%icmptypes);
662 foreach my $key (keys %icmptypes){
663
664 if ("$icmptypes{$key}[0] ($icmptypes{$key}[1])" eq $fwdfwsettings{'ICMP_TGT'}){
665 $fwdfwsettings{'ICMP_TGT'}=$icmptypes{$key}[0];
666 }
667 }
668 }
669 }
670 }
671 #check targetport
672 if ($fwdfwsettings{'USESRV'} ne 'ON'){
673 $fwdfwsettings{'grp3'}='';
674 $fwdfwsettings{$fwdfwsettings{'grp3'}}='';
675 $fwdfwsettings{'ICMP_TGT'}='';
676 }
677 #check timeframe
678 if($fwdfwsettings{'TIME'} eq 'ON'){
679 if($fwdfwsettings{'TIME_MON'} eq '' && $fwdfwsettings{'TIME_TUE'} eq '' && $fwdfwsettings{'TIME_WED'} eq '' && $fwdfwsettings{'TIME_THU'} eq '' && $fwdfwsettings{'TIME_FRI'} eq '' && $fwdfwsettings{'TIME_SAT'} eq '' && $fwdfwsettings{'TIME_SUN'} eq ''){
680 $errormessage=$Lang::tr{'fwdfw err time'};
681 return $errormessage;
682 }
683 }
684 return $errormessage;
685 }
686 sub check_natport
687 {
688 my $val=shift;
689 if($fwdfwsettings{'USE_NAT'} eq 'ON' && $fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'dnatport'} ne ''){
690 if ($fwdfwsettings{'dnatport'} =~ /^(\d+)\-(\d+)$/) {
691 $fwdfwsettings{'dnatport'} =~ tr/-/:/;
692 if ($fwdfwsettings{'dnatport'} eq "*") {
693 $fwdfwsettings{'dnatport'}="1:65535";
694 }
695 if ($fwdfwsettings{'dnatport'} =~ /^(\D)\:(\d+)$/) {
696 $fwdfwsettings{'dnatport'} = "1:$2";
697 }
698 if ($fwdfwsettings{'dnatport'} =~ /^(\d+)\:(\D)$/) {
699 $fwdfwsettings{'dnatport'} ="$1:65535";
700 }
701 }
702 return 1;
703 }
704 if ($val =~ "," || $val>65536 || $val<0){
705 return 0;
706 }
707 return 1;
708 }
709 sub checkrule
710 {
711 #check valid port for NAT
712 if($fwdfwsettings{'USE_NAT'} eq 'ON'){
713 #if no port is given in nat area, take target host port
714 if($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'grp3'} eq 'TGT_PORT' && $fwdfwsettings{'dnatport'} eq ''){$fwdfwsettings{'dnatport'}=$fwdfwsettings{'TGT_PORT'};}
715 #check if port given in nat area is a single valid port or portrange
716 if($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'TGT_PORT'} ne '' && !&check_natport($fwdfwsettings{'dnatport'})){
717 $errormessage=$Lang::tr{'fwdfw target'}.": ".$Lang::tr{'fwdfw dnat porterr'}."<br>";
718 }elsif($fwdfwsettings{'USESRV'} eq 'ON' && $fwdfwsettings{'grp3'} eq 'cust_srv'){
719 my $custsrvport;
720 #get servcie Protocol and Port
721 foreach my $key (sort keys %customservice){
722 if($fwdfwsettings{$fwdfwsettings{'grp3'}} eq $customservice{$key}[0]){
723 if ($customservice{$key}[2] ne 'TCP' && $customservice{$key}[2] ne 'UDP'){
724 $errormessage=$Lang::tr{'fwdfw target'}.": ".$Lang::tr{'fwdfw dnat porterr'}."<br>";
725 }
726 $custsrvport= $customservice{$key}[1];
727 }
728 }
729 if($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'dnatport'} eq ''){$fwdfwsettings{'dnatport'}=$custsrvport;}
730 }
731 #check if DNAT port is multiple
732 if($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'dnatport'} ne ''){
733 my @parts=split(",",$fwdfwsettings{'dnatport'});
734 my @values=();
735 foreach (@parts){
736 chomp($_);
737 if ($_ =~ /^(\d+)\-(\d+)$/ || $_ =~ /^(\d+)\:(\d+)$/) {
738 my $check;
739 #change dashes with :
740 $_=~ tr/-/:/;
741 if ($_ eq "*") {
742 push(@values,"1:65535");
743 $check='on';
744 }
745 if ($_ =~ /^(\D)\:(\d+)$/ || $_ =~ /^(\D)\-(\d+)$/) {
746 push(@values,"1:$2");
747 $check='on';
748 }
749 if ($_ =~ /^(\d+)\:(\D)$/ || $_ =~ /^(\d+)\-(\D)$/) {
750 push(@values,"$1:65535");
751 $check='on'
752 }
753 $errormessage .= &General::validportrange($_, 'destination');
754 if(!$check){
755 push (@values,$_);
756 }
757 }else{
758 if (&General::validport($_)){
759 push (@values,$_);
760 }else{
761
762 }
763 }
764 }
765 $fwdfwsettings{'dnatport'}=join("|",@values);
766 }
767 }
768 #check valid remark
769 if ($fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
770 $errormessage.=$Lang::tr{'fwdfw err remark'}."<br>";
771 }
772 #check if source and target identical
773 if ($fwdfwsettings{$fwdfwsettings{'grp1'}} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{$fwdfwsettings{'grp1'}} ne 'ALL'){
774 $errormessage.=$Lang::tr{'fwdfw err same'};
775 return $errormessage;
776 }
777 #get source and targetip address if possible
778 my ($sip,$scidr,$tip,$tcidr);
779 ($sip,$scidr)=&get_ip("src","grp1");
780 ($tip,$tcidr)=&get_ip("tgt","grp2");
781 #check same iprange in source and target
782 if ($sip ne '' && $scidr ne '' && $tip ne '' && $tcidr ne ''){
783 my $networkip1=&General::getnetworkip($sip,$scidr);
784 my $networkip2=&General::getnetworkip($tip,$tcidr);
785 if ($scidr gt $tcidr){
786 if ( &General::IpInSubnet($networkip1,$tip,&General::iporsubtodec($tcidr))){
787 $errormessage.=$Lang::tr{'fwdfw err samesub'};
788 }
789 }elsif($scidr eq $tcidr && $scidr eq '32'){
790 my ($sbyte1,$sbyte2,$sbyte3,$sbyte4)=split(/\./,$networkip1);
791 my ($tbyte1,$tbyte2,$tbyte3,$tbyte4)=split(/\./,$networkip2);
792 if ($sbyte1 eq $tbyte1 && $sbyte2 eq $tbyte2 && $sbyte3 eq $tbyte3){
793 $hint=$Lang::tr{'fwdfw hint ip1'}."<br>";
794 $hint.=$Lang::tr{'fwdfw hint ip2'}." Source: $networkip1/$scidr Target: $networkip2/$tcidr<br>";
795 }
796 }else{
797 if ( &General::IpInSubnet($networkip2,$sip,&General::iporsubtodec($scidr)) ){
798 $errormessage.=$Lang::tr{'fwdfw err samesub'};
799 }
800 }
801 }
802 #check source and destination protocol if manual
803 if( $fwdfwsettings{'USE_SRC_PORT'} eq 'ON' && $fwdfwsettings{'USESRV'} eq 'ON'){
804 if($fwdfwsettings{'PROT'} ne $fwdfwsettings{'TGT_PROT'} && $fwdfwsettings{'grp3'} eq 'TGT_PORT'){
805 $errormessage.=$Lang::tr{'fwdfw err prot'};
806 }
807 #check source and destination protocol if source manual and dest servicegrp
808 if ($fwdfwsettings{'grp3'} eq 'cust_srv'){
809 foreach my $key (sort keys %customservice){
810 if($customservice{$key}[0] eq $fwdfwsettings{$fwdfwsettings{'grp3'}}){
811 if ($customservice{$key}[2] ne $fwdfwsettings{'PROT'}){
812 $errormessage.=$Lang::tr{'fwdfw err prot'};
813 last;
814 }
815 }
816 }
817 }
818 }
819 }
820 sub checkcounter
821 {
822 my ($base1,$val1,$base2,$val2) = @_;
823
824 if($base1 eq 'cust_net_src' || $base1 eq 'cust_net_tgt'){
825 &dec_counter($confignet,\%customnetwork,$val1);
826 }elsif($base1 eq 'cust_host_src' || $base1 eq 'cust_host_tgt'){
827 &dec_counter($confighost,\%customhost,$val1);
828 }elsif($base1 eq 'cust_grp_src' || $base1 eq 'cust_grp_tgt'){
829 &dec_counter($configgrp,\%customgrp,$val1);
830 }elsif($base1 eq 'cust_srv'){
831 &dec_counter($configsrv,\%customservice,$val1);
832 }elsif($base1 eq 'cust_srvgrp'){
833 &dec_counter($configsrvgrp,\%customservicegrp,$val1);
834 }
835
836 if($base2 eq 'cust_net_src' || $base2 eq 'cust_net_tgt'){
837 &inc_counter($confignet,\%customnetwork,$val2);
838 }elsif($base2 eq 'cust_host_src' || $base2 eq 'cust_host_tgt'){
839 &inc_counter($confighost,\%customhost,$val2);
840 }elsif($base2 eq 'cust_grp_src' || $base2 eq 'cust_grp_tgt'){
841 &inc_counter($configgrp,\%customgrp,$val2);
842 }elsif($base2 eq 'cust_srv'){
843 &inc_counter($configsrv,\%customservice,$val2);
844 }elsif($base2 eq 'cust_srvgrp'){
845 &inc_counter($configsrvgrp,\%customservicegrp,$val2);
846 }
847 }
848 sub checkvpn
849 {
850 my $ip=shift;
851 #Test if manual IP is part of static OpenVPN networks
852 &General::readhasharray("$configccdnet", \%ccdnet);
853 foreach my $key (sort keys %ccdnet){
854 my ($vpnip,$vpnsubnet) = split ("/",$ccdnet{$key}[1]);
855 my $sub=&General::iporsubtodec($vpnsubnet);
856 if (&General::IpInSubnet($ip,$vpnip,$sub)){
857 return 0;
858 }
859 }
860 # A Test if manual ip is part of dynamic openvpn subnet is made in getcolor
861 # because if one creates a custom host with the ip, we need to check the color there!
862 # It does not make sense to check this here
863
864 # Test if manual IP is part of an OpenVPN N2N subnet does also not make sense here
865 # Is also checked in getcolor
866
867 # Test if manual ip is part of an IPsec Network is also checked in getcolor
868 return 1;
869 }
870 sub checkvpncolor
871 {
872
873 }
874 sub deleterule
875 {
876 my %delhash=();
877 &General::readhasharray($fwdfwsettings{'config'}, \%delhash);
878 foreach my $key (sort {$a <=> $b} keys %delhash){
879 if ($key == $fwdfwsettings{'key'}){
880 #check hosts/net and groups
881 &checkcounter($delhash{$key}[3],$delhash{$key}[4],,);
882 &checkcounter($delhash{$key}[5],$delhash{$key}[6],,);
883 #check services and groups
884 if ($delhash{$key}[11] eq 'ON'){
885 &checkcounter($delhash{$key}[14],$delhash{$key}[15],,);
886 }
887 }
888 if ($key >= $fwdfwsettings{'key'}) {
889 my $next = $key + 1;
890 if (exists $delhash{$next}) {
891 foreach my $i (0 .. $#{$delhash{$next}}) {
892 $delhash{$key}[$i] = $delhash{$next}[$i];
893 }
894 }
895 }
896 }
897 # Remove the very last entry.
898 my $last_key = (sort {$a <=> $b} keys %delhash)[-1];
899 delete $delhash{$last_key};
900
901 &General::writehasharray($fwdfwsettings{'config'}, \%delhash);
902 &rules;
903
904 if($fwdfwsettings{'nobase'} ne 'on'){
905 &base;
906 }
907 }
908 sub disable_rule
909 {
910 my $key1=shift;
911 &General::readhasharray("$configfwdfw", \%configfwdfw);
912 foreach my $key (sort keys %configfwdfw){
913 if ($key eq $key1 ){
914 if ($configfwdfw{$key}[2] eq 'ON'){$configfwdfw{$key}[2]='';}
915 }
916 }
917 &General::writehasharray("$configfwdfw", \%configfwdfw);
918 &rules;
919 }
920 sub dec_counter
921 {
922 my $config=shift;
923 my %hash=%{(shift)};
924 my $val=shift;
925 my $pos;
926 &General::readhasharray($config, \%hash);
927 foreach my $key (sort { uc($hash{$a}[0]) cmp uc($hash{$b}[0]) } keys %hash){
928 if($hash{$key}[0] eq $val){
929 $pos=$#{$hash{$key}};
930 $hash{$key}[$pos] = $hash{$key}[$pos]-1;
931 }
932 }
933 &General::writehasharray($config, \%hash);
934 }
935 sub error
936 {
937 if ($errormessage) {
938 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
939 print "<class name='base'>$errormessage\n";
940 print "&nbsp;</class>\n";
941 &Header::closebox();
942 print"<hr>";
943 }
944 }
945 sub fillselect
946 {
947 my %hash=%{(shift)};
948 my $val=shift;
949 my $key;
950 foreach my $key (sort { ncmp($hash{$a}[0],$hash{$b}[0]) } keys %hash){
951 if($hash{$key}[0] eq $val){
952 print"<option value='$hash{$key}[0]' selected>$hash{$key}[0]</option>";
953 }else{
954 print"<option value='$hash{$key}[0]'>$hash{$key}[0]</option>";
955 }
956 }
957 }
958 sub gen_dd_block
959 {
960 my $srctgt = shift;
961 my $grp=shift;
962 my $helper='';
963 my $show='';
964 $checked{'grp1'}{$fwdfwsettings{'grp1'}} = 'CHECKED';
965 $checked{'grp2'}{$fwdfwsettings{'grp2'}} = 'CHECKED';
966 $checked{'grp3'}{$fwdfwsettings{'grp3'}} = 'CHECKED';
967 $checked{'USE_SRC_PORT'}{$fwdfwsettings{'USE_SRC_PORT'}} = 'CHECKED';
968 $checked{'USESRV'}{$fwdfwsettings{'USESRV'}} = 'CHECKED';
969 $checked{'ACTIVE'}{$fwdfwsettings{'ACTIVE'}} = 'CHECKED';
970 $checked{'LOG'}{$fwdfwsettings{'LOG'}} = 'CHECKED';
971 $checked{'TIME'}{$fwdfwsettings{'TIME'}} = 'CHECKED';
972 $checked{'TIME_MON'}{$fwdfwsettings{'TIME_MON'}} = 'CHECKED';
973 $checked{'TIME_TUE'}{$fwdfwsettings{'TIME_TUE'}} = 'CHECKED';
974 $checked{'TIME_WED'}{$fwdfwsettings{'TIME_WED'}} = 'CHECKED';
975 $checked{'TIME_THU'}{$fwdfwsettings{'TIME_THU'}} = 'CHECKED';
976 $checked{'TIME_FRI'}{$fwdfwsettings{'TIME_FRI'}} = 'CHECKED';
977 $checked{'TIME_SAT'}{$fwdfwsettings{'TIME_SAT'}} = 'CHECKED';
978 $checked{'TIME_SUN'}{$fwdfwsettings{'TIME_SUN'}} = 'CHECKED';
979 $selected{'TIME_FROM'}{$fwdfwsettings{'TIME_FROM'}} = 'selected';
980 $selected{'TIME_TO'}{$fwdfwsettings{'TIME_TO'}} = 'selected';
981 $selected{'ipfire'}{$fwdfwsettings{$fwdfwsettings{'grp2'}}} ='selected';
982 print<<END;
983 <table width='100%' border='0'>
984 <tr><td width='50%' valign='top'>
985 <table width='100%' border='0'>
986 <tr><td width='1%'><input type='radio' name='$grp' value='std_net_$srctgt' $checked{$grp}{'std_net_'.$srctgt}></td><td>$Lang::tr{'fwhost stdnet'}</td><td align='right'><select name='std_net_$srctgt' style='width:200px;'>
987 END
988 foreach my $network (sort keys %defaultNetworks)
989 {
990 next if($defaultNetworks{$network}{'NAME'} eq "RED" && $srctgt eq 'src');
991 next if($defaultNetworks{$network}{'NAME'} eq "IPFire" && $srctgt eq 'tgt');
992 print "<option value='$defaultNetworks{$network}{'NAME'}'";
993 print " selected='selected'" if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $defaultNetworks{$network}{'NAME'});
994 my $defnet="$defaultNetworks{$network}{'NAME'}_NETADDRESS";
995 $ifaces{$defnet} = '0.0.0.0' if ($defaultNetworks{$network}{'NAME'} eq 'ALL');
996 $defnet = "RED_ADDRESS" if ($defaultNetworks{$network}{'NAME'} eq 'IPFire');
997 print ">$network $ifaces{$defnet} </option>";
998 }
999 print"</select></td></tr>";
1000 #custom networks
1001 if (! -z $confignet || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
1002 print"<tr><td><input type='radio' name='$grp' value='cust_net_$srctgt' $checked{$grp}{'cust_net_'.$srctgt}></td><td>$Lang::tr{'fwhost cust net'}</td><td align='right'><select name='cust_net_$srctgt' style='width:200px;'>";
1003 &fillselect(\%customnetwork,$fwdfwsettings{$fwdfwsettings{$grp}});
1004 print"</select></td>";
1005 }
1006 #custom hosts
1007 if (! -z $confighost || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
1008 print"<tr><td><input type='radio' name='$grp' value='cust_host_$srctgt' $checked{$grp}{'cust_host_'.$srctgt}></td><td>$Lang::tr{'fwhost cust addr'}</td><td align='right'><select name='cust_host_$srctgt' style='width:200px;'>";
1009 &fillselect(\%customhost,$fwdfwsettings{$fwdfwsettings{$grp}});
1010 print"</select></td>";
1011 }
1012 #custom groups
1013 if (! -z $configgrp || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
1014 print"<tr><td valign='top'><input type='radio' name='$grp' value='cust_grp_$srctgt' $checked{$grp}{'cust_grp_'.$srctgt}></td><td >$Lang::tr{'fwhost cust grp'}</td><td align='right'><select name='cust_grp_$srctgt' style='width:200px;'>";
1015 foreach my $key (sort { ncmp($customgrp{$a}[0],$customgrp{$b}[0]) } keys %customgrp) {
1016 if($helper ne $customgrp{$key}[0]){
1017 print"<option ";
1018 print "selected='selected' " if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $customgrp{$key}[0]);
1019 print ">$customgrp{$key}[0]</option>";
1020 }
1021 $helper=$customgrp{$key}[0];
1022 }
1023 print"</select></td>";
1024 }
1025 #End left table. start right table (vpn)
1026 print"</tr></table></td><td valign='top'><table width='100%' border='0'><tr>";
1027 # CCD networks
1028 if( ! -z $configccdnet || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
1029 print"<td width='1%'><input type='radio' name='$grp' value='ovpn_net_$srctgt' $checked{$grp}{'ovpn_net_'.$srctgt}></td><td nowrap='nowrap' width='16%'>$Lang::tr{'fwhost ccdnet'}</td><td nowrap='nowrap' width='1%' align='right'><select name='ovpn_net_$srctgt' style='width:200px;'>";
1030 &fillselect(\%ccdnet,$fwdfwsettings{$fwdfwsettings{$grp}});
1031 print"</select></td></tr>";
1032 }
1033 #OVPN CCD Hosts
1034 foreach my $key (sort { ncmp($ccdhost{$a}[0],$ccdhost{$b}[0]) } keys %ccdhost){
1035 if ($ccdhost{$key}[33] ne '' ){
1036 print"<tr><td width='1%'><input type='radio' name='$grp' value='ovpn_host_$srctgt' $checked{$grp}{'ovpn_host_'.$srctgt}></td><td nowrap='nowrap' width='16%'>$Lang::tr{'fwhost ccdhost'}</td><td nowrap='nowrap' width='1%' align='right'><select name='ovpn_host_$srctgt' style='width:200px;'>" if ($show eq '');
1037 $show='1';
1038 print "<option value='$ccdhost{$key}[1]'";
1039 print "selected='selected'" if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $ccdhost{$key}[1]);
1040 print ">$ccdhost{$key}[1]</option>";
1041 }
1042 }
1043 if($optionsfw{'SHOWDROPDOWN'} eq 'on' && $show eq ''){
1044 print"<tr><td width='1%'><input type='radio' name='$grp' value='ovpn_host_$srctgt' $checked{$grp}{'ovpn_host_'.$srctgt}></td><td nowrap='nowrap' width='16%'>$Lang::tr{'fwhost ccdhost'}</td><td nowrap='nowrap' width='1%' align='right'><select name='ovpn_host_$srctgt' style='width:200px;'></select></td></tr>" ;
1045 }
1046 if ($show eq '1'){$show='';print"</select></td></tr>";}
1047 #OVPN N2N
1048 foreach my $key (sort { ncmp($ccdhost{$a}[1],$ccdhost{$b}[1]) } keys %ccdhost){
1049 if ($ccdhost{$key}[3] eq 'net'){
1050 print"<tr><td width='1%'><input type='radio' name='$grp' value='ovpn_n2n_$srctgt' $checked{$grp}{'ovpn_n2n_'.$srctgt}></td><td nowrap='nowrap' width='16%'>$Lang::tr{'fwhost ovpn_n2n'}:</td><td nowrap='nowrap' width='1%' align='right'><select name='ovpn_n2n_$srctgt' style='width:200px;'>" if ($show eq '');
1051 $show='1';
1052 print "<option value='$ccdhost{$key}[1]'";
1053 print "selected='selected'" if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $ccdhost{$key}[1]);
1054 print ">$ccdhost{$key}[1]</option>";
1055 }
1056 }
1057 if($optionsfw{'SHOWDROPDOWN'} eq 'on' && $show eq ''){
1058 print"<tr><td width='1%'><input type='radio' name='$grp' value='ovpn_n2n_$srctgt' $checked{$grp}{'ovpn_n2n_'.$srctgt}></td><td nowrap='nowrap' width='16%'>$Lang::tr{'fwhost ovpn_n2n'}</td><td nowrap='nowrap' width='1%' align='right'><select name='ovpn_n2n_$srctgt' style='width:200px;'></select></td></tr>" ;
1059 }
1060 if ($show eq '1'){$show='';print"</select></td></tr>";}
1061 #IPsec netze
1062 foreach my $key (sort { ncmp($ipsecconf{$a}[1],$ipsecconf{$b}[1]) } keys %ipsecconf) {
1063 if ($ipsecconf{$key}[3] eq 'net' || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
1064 print"<tr><td valign='top'><input type='radio' name='$grp' value='ipsec_net_$srctgt' $checked{$grp}{'ipsec_net_'.$srctgt}></td><td >$Lang::tr{'fwhost ipsec net'}</td><td align='right'><select name='ipsec_net_$srctgt' style='width:200px;'>" if ($show eq '');
1065 $show='1';
1066 print "<option ";
1067 print "selected='selected'" if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $ipsecconf{$key}[1]);
1068 print ">$ipsecconf{$key}[1]</option>";
1069 }
1070 }
1071 if($optionsfw{'SHOWDROPDOWN'} eq 'on' && $show eq ''){
1072 print"<tr><td valign='top'><input type='radio' name='$grp' value='ipsec_net_$srctgt' $checked{$grp}{'ipsec_net_'.$srctgt}></td><td >$Lang::tr{'fwhost ipsec net'}</td><td align='right'><select name='ipsec_net_$srctgt' style='width:200px;'><select></td></tr>";
1073 }
1074 if ($show eq '1'){$show='';print"</select></td></tr>";}
1075
1076 print"</tr></table>";
1077 print"</td></tr></table><br>";
1078 }
1079 sub get_ip
1080 {
1081 my $val=shift;
1082 my $grp =shift;
1083 my $a;
1084 my $b;
1085 &General::readhash("/var/ipfire/ethernet/settings", \%netsettings);
1086 if ($fwdfwsettings{$grp} ne $Lang::tr{'fwhost any'}){
1087 if ($fwdfwsettings{$grp} eq $val.'_addr'){
1088 ($a,$b) = split (/\//, $fwdfwsettings{$fwdfwsettings{$grp}});
1089 }elsif($fwdfwsettings{$grp} eq 'std_net_'.$val){
1090 if ($fwdfwsettings{$fwdfwsettings{$grp}} =~ /Gr/i){
1091 $a=$netsettings{'GREEN_NETADDRESS'};
1092 $b=&General::iporsubtocidr($netsettings{'GREEN_NETMASK'});
1093 }elsif($fwdfwsettings{$fwdfwsettings{$grp}} =~ /Ora/i){
1094 $a=$netsettings{'ORANGE_NETADDRESS'};
1095 $b=&General::iporsubtocidr($netsettings{'ORANGE_NETMASK'});
1096 }elsif($fwdfwsettings{$fwdfwsettings{$grp}} =~ /Bl/i){
1097 $a=$netsettings{'BLUE_NETADDRESS'};
1098 $b=&General::iporsubtocidr($netsettings{'BLUE_NETMASK'});
1099 }elsif($fwdfwsettings{$fwdfwsettings{$grp}} =~ /OpenVPN/i){
1100 &General::readhash("$configovpn",\%ovpnsettings);
1101 ($a,$b) = split (/\//, $ovpnsettings{'DOVPN_SUBNET'});
1102 $b=&General::iporsubtocidr($b);
1103 }
1104 }elsif($fwdfwsettings{$grp} eq 'cust_net_'.$val){
1105 &General::readhasharray("$confignet", \%customnetwork);
1106 foreach my $key (keys %customnetwork){
1107 if($customnetwork{$key}[0] eq $fwdfwsettings{$fwdfwsettings{$grp}}){
1108 $a=$customnetwork{$key}[1];
1109 $b=&General::iporsubtocidr($customnetwork{$key}[2]);
1110 }
1111 }
1112 }elsif($fwdfwsettings{$grp} eq 'cust_host_'.$val){
1113 &General::readhasharray("$confighost", \%customhost);
1114 foreach my $key (keys %customhost){
1115 if($customhost{$key}[0] eq $fwdfwsettings{$fwdfwsettings{$grp}}){
1116 if ($customhost{$key}[1] eq 'ip'){
1117 ($a,$b)=split (/\//,$customhost{$key}[2]);
1118 $b=&General::iporsubtocidr($b);
1119 }else{
1120 if ($grp eq 'grp2'){
1121 $errormessage=$Lang::tr{'fwdfw err tgt_mac'};
1122 }
1123 }
1124 }
1125 }
1126 }
1127 }
1128 return $a,$b;
1129 }
1130 sub get_name
1131 {
1132 my $val=shift;
1133 &General::setup_default_networks(\%defaultNetworks);
1134 foreach my $network (sort keys %defaultNetworks)
1135 {
1136 return "$network" if ($val eq $defaultNetworks{$network}{'NAME'});
1137 }
1138 }
1139 sub getsrcport
1140 {
1141 my %hash=%{(shift)};
1142 my $key=shift;
1143 if($hash{$key}[7] eq 'ON' && $hash{$key}[8] ne '' && $hash{$key}[10]){
1144 $hash{$key}[10]=~ s/\|/,/g;
1145 print": $hash{$key}[10]";
1146 }elsif($hash{$key}[7] eq 'ON' && $hash{$key}[8] eq 'ICMP'){
1147 print": <br>$hash{$key}[9] ";
1148 }
1149 }
1150 sub gettgtport
1151 {
1152 my %hash=%{(shift)};
1153 my $key=shift;
1154 my $service;
1155 my $prot;
1156 if($hash{$key}[11] eq 'ON' && $hash{$key}[12] ne 'ICMP'){
1157 if($hash{$key}[14] eq 'cust_srv'){
1158 &General::readhasharray("$configsrv", \%customservice);
1159 foreach my $i (sort keys %customservice){
1160 if($customservice{$i}[0] eq $hash{$key}[15]){
1161 $service = $customservice{$i}[0];
1162 }
1163 }
1164 }elsif($hash{$key}[14] eq 'cust_srvgrp'){
1165 $service=$hash{$key}[15];
1166 }elsif($hash{$key}[14] eq 'TGT_PORT'){
1167 $hash{$key}[15]=~ s/\|/,/g;
1168 $service=$hash{$key}[15];
1169 }
1170 if($service){
1171 print": $service";
1172 }
1173 }elsif($hash{$key}[11] eq 'ON' && $hash{$key}[12] eq 'ICMP'){
1174 print":<br>$hash{$key}[13]";
1175 }
1176 }
1177 sub get_serviceports
1178 {
1179 my $type=shift;
1180 my $name=shift;
1181 &General::readhasharray("$configsrv", \%customservice);
1182 &General::readhasharray("$configsrvgrp", \%customservicegrp);
1183 my $tcp;
1184 my $udp;
1185 my $icmp;
1186 @protocols=();
1187 if($type eq 'service'){
1188 foreach my $key (sort { ncmp($customservice{$a}[0],$customservice{$b}[0]) } keys %customservice){
1189 if ($customservice{$key}[0] eq $name){
1190 push (@protocols,$customservice{$key}[2]);
1191 }
1192 }
1193 }elsif($type eq 'group'){
1194 foreach my $key (sort { ncmp($customservicegrp{$a}[0],$customservicegrp{$b}[0]) } keys %customservicegrp){
1195 if ($customservicegrp{$key}[0] eq $name){
1196 foreach my $key1 (sort { ncmp($customservice{$a}[0],$customservice{$b}[0]) } keys %customservice){
1197 if ($customservice{$key1}[0] eq $customservicegrp{$key}[2]){
1198 if($customservice{$key1}[2] eq 'TCP'){
1199 $tcp='TCP';
1200 }elsif($customservice{$key1}[2] eq 'ICMP'){
1201 $icmp='ICMP';
1202 }elsif($customservice{$key1}[2] eq 'UDP'){
1203 $udp='UDP';
1204 }
1205 }
1206 }
1207 }
1208 }
1209 }
1210 if($tcp && $udp && $icmp){
1211 push (@protocols,"All");
1212 return @protocols;
1213 }
1214 if($tcp){
1215 push (@protocols,"TCP");
1216 }
1217 if($udp){
1218 push (@protocols,"UDP");
1219 }
1220 if($icmp){
1221 push (@protocols,"ICMP");
1222 }
1223 return @protocols;
1224 }
1225 sub getcolor
1226 {
1227 my $nettype=shift;
1228 my $val=shift;
1229 my $hash=shift;
1230 if($optionsfw{'SHOWCOLORS'} eq 'on'){
1231 #standard networks
1232 if ($val eq 'GREEN'){
1233 $tdcolor="style='border: 1px solid $Header::colourgreen;'";
1234 return;
1235 }elsif ($val eq 'ORANGE'){
1236 $tdcolor="style='border: 1px solid $Header::colourorange;'";
1237 return;
1238 }elsif ($val eq 'BLUE'){
1239 $tdcolor="style='border: 1px solid $Header::colourblue;'";
1240 return;
1241 }elsif ($val eq 'RED' ||$val eq 'RED1' ){
1242 $tdcolor="style='border: 1px solid $Header::colourred;'";
1243 return;
1244 }elsif ($val eq 'IPFire' ){
1245 $tdcolor="style='border: 1px solid $Header::colourred;'";
1246 return;
1247 }elsif($val =~ /^(.*?)\/(.*?)$/){
1248 my ($sip,$scidr) = split ("/",$val);
1249 if ( &General::IpInSubnet($sip,$netsettings{'ORANGE_ADDRESS'},$netsettings{'ORANGE_NETMASK'})){
1250 $tdcolor="style='border: 1px solid $Header::colourorange;'";
1251 return;
1252 }
1253 if ( &General::IpInSubnet($sip,$netsettings{'GREEN_ADDRESS'},$netsettings{'GREEN_NETMASK'})){
1254 $tdcolor="style='border: 1px solid $Header::colourgreen;'";
1255 return;
1256 }
1257 if ( &General::IpInSubnet($sip,$netsettings{'BLUE_ADDRESS'},$netsettings{'BLUE_NETMASK'})){
1258 $tdcolor="style='border: 1px solid $Header::colourblue;'";
1259 return;
1260 }
1261 }elsif ($val eq 'Default IP'){
1262 $tdcolor="style='border: 1px solid $Header::colourred;'";
1263 return;
1264 }
1265 #Check if a manual IP is part of a VPN
1266 if ($nettype eq 'src_addr' || $nettype eq 'tgt_addr'){
1267 #Check if IP is part of OpenVPN dynamic subnet
1268 my ($a,$b) = split("/",$ovpnsettings{'DOVPN_SUBNET'});
1269 my ($c,$d) = split("/",$val);
1270 if (&General::IpInSubnet($c,$a,$b)){
1271 $tdcolor="style='border: 1px solid $Header::colourovpn;'";
1272 return;
1273 }
1274 #Check if IP is part of OpenVPN static subnet
1275 foreach my $key (sort keys %ccdnet){
1276 my ($a,$b) = split("/",$ccdnet{$key}[1]);
1277 $b =&General::iporsubtodec($b);
1278 if (&General::IpInSubnet($c,$a,$b)){
1279 $tdcolor="style='border: 1px solid $Header::colourovpn;'";
1280 return;
1281 }
1282 }
1283 #Check if IP is part of OpenVPN N2N subnet
1284 foreach my $key (sort keys %ccdhost){
1285 if ($ccdhost{$key}[3] eq 'net'){
1286 my ($a,$b) = split("/",$ccdhost{$key}[11]);
1287 if (&General::IpInSubnet($c,$a,$b)){
1288 $tdcolor="style='border: 1px solid $Header::colourovpn;'";
1289 return;
1290 }
1291 }
1292 }
1293 #Check if IP is part of IPsec RW network
1294 if ($ipsecsettings{'RW_NET'} ne ''){
1295 my ($a,$b) = split("/",$ipsecsettings{'RW_NET'});
1296 $b=&General::iporsubtodec($b);
1297 if (&General::IpInSubnet($c,$a,$b)){
1298 $tdcolor="style='border: 1px solid $Header::colourvpn;'";
1299 return;
1300 }
1301 }
1302 #Check if IP is part of a IPsec N2N network
1303 foreach my $key (sort keys %ipsecconf){
1304 my ($a,$b) = split("/",$ipsecconf{$key}[11]);
1305 if (&General::IpInSubnet($c,$a,$b)){
1306 $tdcolor="style='border: 1px solid $Header::colourvpn;'";
1307 return;
1308 }
1309 }
1310 }
1311 #VPN networks
1312 if ($nettype eq 'ovpn_n2n_src' || $nettype eq 'ovpn_n2n_tgt' || $nettype eq 'ovpn_net_src' || $nettype eq 'ovpn_net_tgt'|| $nettype eq 'ovpn_host_src' || $nettype eq 'ovpn_host_tgt'){
1313 $tdcolor="style='border: 1px solid $Header::colourovpn;'";
1314 return;
1315 }
1316 if ($nettype eq 'ipsec_net_src' || $nettype eq 'ipsec_net_tgt'){
1317 $tdcolor="style='border: 1px solid $Header::colourvpn;'";
1318 return;
1319 }
1320 #custom Hosts
1321 if ($nettype eq 'cust_host_src' || $nettype eq 'cust_host_tgt'){
1322 foreach my $key (sort keys %$hash){
1323 if ($$hash{$key}[0] eq $val){
1324 $val=$$hash{$key}[2];
1325 }
1326 }
1327 }
1328 #ALIASE
1329 foreach my $alias (sort keys %aliases)
1330 {
1331 if ($val eq $alias){
1332 $tdcolor="style='border: 1px solid $Header::colourred;'";
1333 return;
1334 }
1335 }
1336 }
1337 $tdcolor='';
1338 return;
1339 }
1340 sub hint
1341 {
1342 if ($hint) {
1343 &Header::openbox('100%', 'left', $Lang::tr{'fwhost hint'});
1344 print "<class name='base'>$hint\n";
1345 print "&nbsp;</class>\n";
1346 &Header::closebox();
1347 print"<hr>";
1348 }
1349 }
1350 sub inc_counter
1351 {
1352 my $config=shift;
1353 my %hash=%{(shift)};
1354 my $val=shift;
1355 my $pos;
1356
1357 &General::readhasharray($config, \%hash);
1358 foreach my $key (sort { uc($hash{$a}[0]) cmp uc($hash{$b}[0]) } keys %hash){
1359 if($hash{$key}[0] eq $val){
1360 $pos=$#{$hash{$key}};
1361 $hash{$key}[$pos] = $hash{$key}[$pos]+1;
1362 }
1363 }
1364 &General::writehasharray($config, \%hash);
1365 }
1366 sub newrule
1367 {
1368 &error;
1369 &General::setup_default_networks(\%defaultNetworks);
1370 &General::readhash("/var/ipfire/ethernet/settings", \%netsettings);
1371 #read all configfiles
1372 &General::readhasharray("$configccdnet", \%ccdnet);
1373 &General::readhasharray("$confignet", \%customnetwork);
1374 &General::readhasharray("$configccdhost", \%ccdhost);
1375 &General::readhasharray("$confighost", \%customhost);
1376 &General::readhasharray("$configccdhost", \%ccdhost);
1377 &General::readhasharray("$configgrp", \%customgrp);
1378 &General::readhasharray("$configipsec", \%ipsecconf);
1379 &General::get_aliases(\%aliases);
1380 my %checked=();
1381 my $helper;
1382 my $sum=0;
1383 if($fwdfwsettings{'config'} eq ''){$fwdfwsettings{'config'}=$configfwdfw;}
1384 my $config=$fwdfwsettings{'config'};
1385 my %hash=();
1386 $checked{'grp1'}{$fwdfwsettings{'grp1'}} = 'CHECKED';
1387 $checked{'grp2'}{$fwdfwsettings{'grp2'}} = 'CHECKED';
1388 $checked{'grp3'}{$fwdfwsettings{'grp3'}} = 'CHECKED';
1389 $checked{'USE_SRC_PORT'}{$fwdfwsettings{'USE_SRC_PORT'}} = 'CHECKED';
1390 $checked{'USESRV'}{$fwdfwsettings{'USESRV'}} = 'CHECKED';
1391 $checked{'ACTIVE'}{$fwdfwsettings{'ACTIVE'}} = 'CHECKED';
1392 $checked{'LOG'}{$fwdfwsettings{'LOG'}} = 'CHECKED';
1393 $checked{'TIME'}{$fwdfwsettings{'TIME'}} = 'CHECKED';
1394 $checked{'TIME_MON'}{$fwdfwsettings{'TIME_MON'}} = 'CHECKED';
1395 $checked{'TIME_TUE'}{$fwdfwsettings{'TIME_TUE'}} = 'CHECKED';
1396 $checked{'TIME_WED'}{$fwdfwsettings{'TIME_WED'}} = 'CHECKED';
1397 $checked{'TIME_THU'}{$fwdfwsettings{'TIME_THU'}} = 'CHECKED';
1398 $checked{'TIME_FRI'}{$fwdfwsettings{'TIME_FRI'}} = 'CHECKED';
1399 $checked{'TIME_SAT'}{$fwdfwsettings{'TIME_SAT'}} = 'CHECKED';
1400 $checked{'TIME_SUN'}{$fwdfwsettings{'TIME_SUN'}} = 'CHECKED';
1401 $checked{'USE_NAT'}{$fwdfwsettings{'USE_NAT'}} = 'CHECKED';
1402 $selected{'TIME_FROM'}{$fwdfwsettings{'TIME_FROM'}} = 'selected';
1403 $selected{'TIME_TO'}{$fwdfwsettings{'TIME_TO'}} = 'selected';
1404 $selected{'ipfire'}{$fwdfwsettings{$fwdfwsettings{'grp2'}}} ='selected';
1405 #check if update and get values
1406 if($fwdfwsettings{'updatefwrule'} eq 'on' || $fwdfwsettings{'copyfwrule'} eq 'on' && !$errormessage){
1407 &General::readhasharray("$config", \%hash);
1408 foreach my $key (sort keys %hash){
1409 $sum++;
1410 if ($key eq $fwdfwsettings{'key'}){
1411 $fwdfwsettings{'oldrulenumber'} = $fwdfwsettings{'key'};
1412 $fwdfwsettings{'RULE_ACTION'} = $hash{$key}[0];
1413 $fwdfwsettings{'chain'} = $hash{$key}[1];
1414 $fwdfwsettings{'ACTIVE'} = $hash{$key}[2];
1415 $fwdfwsettings{'grp1'} = $hash{$key}[3];
1416 $fwdfwsettings{$fwdfwsettings{'grp1'}} = $hash{$key}[4];
1417 $fwdfwsettings{'grp2'} = $hash{$key}[5];
1418 $fwdfwsettings{$fwdfwsettings{'grp2'}} = $hash{$key}[6];
1419 $fwdfwsettings{'USE_SRC_PORT'} = $hash{$key}[7];
1420 $fwdfwsettings{'PROT'} = $hash{$key}[8];
1421 $fwdfwsettings{'ICMP_TYPES'} = $hash{$key}[9];
1422 $fwdfwsettings{'SRC_PORT'} = $hash{$key}[10];
1423 $fwdfwsettings{'USESRV'} = $hash{$key}[11];
1424 $fwdfwsettings{'TGT_PROT'} = $hash{$key}[12];
1425 $fwdfwsettings{'ICMP_TGT'} = $hash{$key}[13];
1426 $fwdfwsettings{'grp3'} = $hash{$key}[14];
1427 $fwdfwsettings{$fwdfwsettings{'grp3'}} = $hash{$key}[15];
1428 $fwdfwsettings{'ruleremark'} = $hash{$key}[16];
1429 $fwdfwsettings{'LOG'} = $hash{$key}[17];
1430 $fwdfwsettings{'TIME'} = $hash{$key}[18];
1431 $fwdfwsettings{'TIME_MON'} = $hash{$key}[19];
1432 $fwdfwsettings{'TIME_TUE'} = $hash{$key}[20];
1433 $fwdfwsettings{'TIME_WED'} = $hash{$key}[21];
1434 $fwdfwsettings{'TIME_THU'} = $hash{$key}[22];
1435 $fwdfwsettings{'TIME_FRI'} = $hash{$key}[23];
1436 $fwdfwsettings{'TIME_SAT'} = $hash{$key}[24];
1437 $fwdfwsettings{'TIME_SUN'} = $hash{$key}[25];
1438 $fwdfwsettings{'TIME_FROM'} = $hash{$key}[26];
1439 $fwdfwsettings{'TIME_TO'} = $hash{$key}[27];
1440 $fwdfwsettings{'USE_NAT'} = $hash{$key}[28];
1441 $fwdfwsettings{'nat'} = $hash{$key}[31]; #changed order
1442 $fwdfwsettings{$fwdfwsettings{'nat'}} = $hash{$key}[29];
1443 $fwdfwsettings{'dnatport'} = $hash{$key}[30];
1444 $checked{'grp1'}{$fwdfwsettings{'grp1'}} = 'CHECKED';
1445 $checked{'grp2'}{$fwdfwsettings{'grp2'}} = 'CHECKED';
1446 $checked{'grp3'}{$fwdfwsettings{'grp3'}} = 'CHECKED';
1447 $checked{'USE_SRC_PORT'}{$fwdfwsettings{'USE_SRC_PORT'}} = 'CHECKED';
1448 $checked{'USESRV'}{$fwdfwsettings{'USESRV'}} = 'CHECKED';
1449 $checked{'ACTIVE'}{$fwdfwsettings{'ACTIVE'}} = 'CHECKED';
1450 $checked{'LOG'}{$fwdfwsettings{'LOG'}} = 'CHECKED';
1451 $checked{'TIME'}{$fwdfwsettings{'TIME'}} = 'CHECKED';
1452 $checked{'TIME_MON'}{$fwdfwsettings{'TIME_MON'}} = 'CHECKED';
1453 $checked{'TIME_TUE'}{$fwdfwsettings{'TIME_TUE'}} = 'CHECKED';
1454 $checked{'TIME_WED'}{$fwdfwsettings{'TIME_WED'}} = 'CHECKED';
1455 $checked{'TIME_THU'}{$fwdfwsettings{'TIME_THU'}} = 'CHECKED';
1456 $checked{'TIME_FRI'}{$fwdfwsettings{'TIME_FRI'}} = 'CHECKED';
1457 $checked{'TIME_SAT'}{$fwdfwsettings{'TIME_SAT'}} = 'CHECKED';
1458 $checked{'TIME_SUN'}{$fwdfwsettings{'TIME_SUN'}} = 'CHECKED';
1459 $checked{'USE_NAT'}{$fwdfwsettings{'USE_NAT'}} = 'CHECKED';
1460 $checked{'nat'}{$fwdfwsettings{'nat'}} = 'CHECKED';
1461 $selected{'TIME_FROM'}{$fwdfwsettings{'TIME_FROM'}} = 'selected';
1462 $selected{'TIME_TO'}{$fwdfwsettings{'TIME_TO'}} = 'selected';
1463 $selected{'ipfire'}{$fwdfwsettings{$fwdfwsettings{'grp2'}}} ='selected';
1464 $selected{'dnat'}{$fwdfwsettings{$fwdfwsettings{'nat'}}} ='selected';
1465 $selected{'snat'}{$fwdfwsettings{$fwdfwsettings{'nat'}}} ='selected';
1466 }
1467 }
1468 $fwdfwsettings{'oldgrp1a'}=$fwdfwsettings{'grp1'};
1469 $fwdfwsettings{'oldgrp1b'}=$fwdfwsettings{$fwdfwsettings{'grp1'}};
1470 $fwdfwsettings{'oldgrp2a'}=$fwdfwsettings{'grp2'};
1471 $fwdfwsettings{'oldgrp2b'}=$fwdfwsettings{$fwdfwsettings{'grp2'}};
1472 $fwdfwsettings{'oldgrp3a'}=$fwdfwsettings{'grp3'};
1473 $fwdfwsettings{'oldgrp3b'}=$fwdfwsettings{$fwdfwsettings{'grp3'}};
1474 $fwdfwsettings{'oldusesrv'}=$fwdfwsettings{'USESRV'};
1475 $fwdfwsettings{'oldruleremark'}=$fwdfwsettings{'ruleremark'};
1476 $fwdfwsettings{'oldnat'}=$fwdfwsettings{'USE_NAT'};
1477 $fwdfwsettings{'oldruletype'}=$fwdfwsettings{'chain'};
1478 #check if manual ip (source) is orange network
1479 if ($fwdfwsettings{'grp1'} eq 'src_addr'){
1480 my ($sip,$scidr) = split("/",$fwdfwsettings{$fwdfwsettings{'grp1'}});
1481 if ( &General::IpInSubnet($sip,$netsettings{'ORANGE_ADDRESS'},$netsettings{'ORANGE_NETMASK'})){
1482 $fwdfwsettings{'oldorange'} ='on';
1483 }
1484 }
1485 }else{
1486 $fwdfwsettings{'ACTIVE'}='ON';
1487 $checked{'ACTIVE'}{$fwdfwsettings{'ACTIVE'}} = 'CHECKED';
1488 $fwdfwsettings{'oldgrp1a'}=$fwdfwsettings{'grp1'};
1489 $fwdfwsettings{'oldgrp1b'}=$fwdfwsettings{$fwdfwsettings{'grp1'}};
1490 $fwdfwsettings{'oldgrp2a'}=$fwdfwsettings{'grp2'};
1491 $fwdfwsettings{'oldgrp2b'}=$fwdfwsettings{$fwdfwsettings{'grp2'}};
1492 $fwdfwsettings{'oldgrp3a'}=$fwdfwsettings{'grp3'};
1493 $fwdfwsettings{'oldgrp3b'}=$fwdfwsettings{$fwdfwsettings{'grp3'}};
1494 $fwdfwsettings{'oldusesrv'}=$fwdfwsettings{'USESRV'};
1495 $fwdfwsettings{'oldruleremark'}=$fwdfwsettings{'ruleremark'};
1496 $fwdfwsettings{'oldnat'}=$fwdfwsettings{'USE_NAT'};
1497 #check if manual ip (source) is orange network
1498 if ($fwdfwsettings{'grp1'} eq 'src_addr'){
1499 my ($sip,$scidr) = split("/",$fwdfwsettings{$fwdfwsettings{'grp1'}});
1500 if ( &General::IpInSubnet($sip,$netsettings{'ORANGE_ADDRESS'},$netsettings{'ORANGE_NETMASK'})){
1501 $fwdfwsettings{'oldorange'} ='on';
1502 }
1503 }
1504 }
1505 &Header::openbox('100%', 'left', $Lang::tr{'fwdfw addrule'});
1506 print "<form method='post'>";
1507 &Header::closebox();
1508 &Header::openbox('100%', 'left', $Lang::tr{'fwdfw source'});
1509 #------SOURCE-------------------------------------------------------
1510 print<<END;
1511 <table width='100%' border='0'>
1512 <tr><td width='1%'><input type='radio' name='grp1' value='src_addr' checked></td><td colspan='5'>$Lang::tr{'fwdfw sourceip'}<input type='TEXT' name='src_addr' value='$fwdfwsettings{'src_addr'}' size='16' maxlength='17'></td></tr>
1513 <tr><td colspan='7'><hr style='border:dotted #BFBFBF; border-width:1px 0 0 0 ; ' /></td></tr>
1514 </table>
1515 END
1516 &gen_dd_block('src','grp1');
1517 print<<END;
1518 <tr><td colspan='8'><hr style='border:dotted #BFBFBF; border-width:1px 0 0 0 ; ' /></td></tr></table>
1519 <table width='100%' border='0'>
1520 <tr><td width='1%'><input type='checkbox' name='USE_SRC_PORT' value='ON' $checked{'USE_SRC_PORT'}{'ON'}></td><td width='51%' colspan='3'>$Lang::tr{'fwdfw use srcport'}</td>
1521 <td width='15%' nowrap='nowrap'>$Lang::tr{'fwdfw man port'}</td><td><select name='PROT'>
1522 END
1523 foreach ("TCP","UDP","GRE","ESP","AH","ICMP")
1524 {
1525 if ($_ eq $fwdfwsettings{'PROT'})
1526 {
1527 print"<option selected>$_</option>";
1528 }else{
1529 print"<option>$_</option>";
1530 }
1531 }
1532 $fwdfwsettings{'SRC_PORT'}=~ s/\|/,/g;
1533 print<<END;
1534 </select></td><td align='right'><input type='text' name='SRC_PORT' value='$fwdfwsettings{'SRC_PORT'}' maxlength='20' size='18' ></td></tr>
1535 <tr><td></td><td></td><td></td><td></td><td nowrap='nowrap'>$Lang::tr{'fwhost icmptype'}</td><td colspan='2'><select name='ICMP_TYPES' style='width:230px;'>
1536 END
1537 &General::readhasharray("${General::swroot}/fwhosts/icmp-types", \%icmptypes);
1538 print"<option>All ICMP-Types</option>";
1539 foreach my $key (sort { ncmp($icmptypes{$a}[0],$icmptypes{$b}[0]) } keys %icmptypes){
1540 if($fwdfwsettings{'ICMP_TYPES'} eq "$icmptypes{$key}[0]"){
1541 print"<option selected>$icmptypes{$key}[0] ($icmptypes{$key}[1])</option>";
1542 }else{
1543 print"<option>$icmptypes{$key}[0] ($icmptypes{$key}[1])</option>";
1544 }
1545 }
1546 print<<END;
1547 </select></td></tr></table><br><hr>
1548 END
1549 &Header::closebox();
1550
1551 #---TARGET------------------------------------------------------
1552 &Header::openbox('100%', 'left', $Lang::tr{'fwdfw target'});
1553 print<<END;
1554 <table width='100%' border='0'>
1555 <tr><td width='1%'><input type='radio' name='grp2' value='tgt_addr' checked></td><td width='57%' nowrap='nowrap'>$Lang::tr{'fwdfw targetip'}<input type='TEXT' name='tgt_addr' value='$fwdfwsettings{'tgt_addr'}' size='16' maxlength='17'><td width='1%'><input type='radio' name='grp2' value='ipfire' $checked{'grp2'}{'ipfire'}></td><td><b>Firewall</b></td>
1556 END
1557 print"<td align='right'><select name='ipfire' style='width:200px;'>";
1558 print "<option value='ALL' $selected{'ipfire'}{'ALL'}>$Lang::tr{'all'} 0.0.0.0</option>";
1559 print "<option value='GREEN' $selected{'ipfire'}{'GREEN'}>$Lang::tr{'green'} $ifaces{'GREEN_ADDRESS'}</option>" if $ifaces{'GREEN_ADDRESS'};
1560 print "<option value='ORANGE' $selected{'ipfire'}{'ORANGE'}>$Lang::tr{'orange'} $ifaces{'ORANGE_ADDRESS'}</option>" if $ifaces{'ORANGE_ADDRESS'};
1561 print "<option value='BLUE' $selected{'ipfire'}{'BLUE'}>$Lang::tr{'blue'} $ifaces{'BLUE_ADDRESS'}</option>" if $ifaces{'BLUE_ADDRESS'};
1562 print "<option value='RED1' $selected{'ipfire'}{'RED1'}>$Lang::tr{'red1'} $ifaces{'RED_ADDRESS'}</option>" if $ifaces{'RED_ADDRESS'};
1563
1564 if (! -z "${General::swroot}/ethernet/aliases"){
1565 foreach my $alias (sort keys %aliases)
1566 {
1567 print "<option value='$alias' $selected{'ipfire'}{$alias}>$alias</option>";
1568 }
1569 }
1570 print<<END;
1571 </td></tr>
1572 <tr><td colspan='7'><hr style='border:dotted #BFBFBF; border-width:1px 0 0 0 ; ' /></td></tr></table>
1573 END
1574 &gen_dd_block('tgt','grp2');
1575 print<<END;
1576 <b>$Lang::tr{'fwhost attention'}:</b><br>
1577 $Lang::tr{'fwhost macwarn'}<br><hr style='border:dotted #BFBFBF; border-width:1px 0 0 0 ; '></hr><br>
1578 <table width='100%' border='0'>
1579 <tr><td width='1%'><input type='checkbox' name='USESRV' value='ON' $checked{'USESRV'}{'ON'} ></td><td width='48%'>$Lang::tr{'fwdfw use srv'}</td><td width='1%'><input type='radio' name='grp3' value='cust_srv' checked></td><td nowrap='nowrap'>$Lang::tr{'fwhost cust service'}</td><td width='1%' colspan='2'><select name='cust_srv'style='min-width:230px;' >
1580 END
1581 &General::readhasharray("$configsrv", \%customservice);
1582 foreach my $key (sort { ncmp($customservice{$a}[0],$customservice{$b}[0]) } keys %customservice){
1583 print"<option ";
1584 print"selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'grp3'}} eq $customservice{$key}[0]);
1585 print"value='$customservice{$key}[0]'>$customservice{$key}[0]</option>";
1586 }
1587 print<<END;
1588 </select></td></tr>
1589 <tr><td colspan='2'></td><td><input type='radio' name='grp3' value='cust_srvgrp' $checked{'grp3'}{'cust_srvgrp'}></td><td nowrap='nowrap'>$Lang::tr{'fwhost cust srvgrp'}:</td><td colspan='2'><select name='cust_srvgrp' style='min-width:230px;' >
1590 END
1591 &General::readhasharray("$configsrvgrp", \%customservicegrp);
1592 my $helper;
1593 foreach my $key (sort { ncmp($customservicegrp{$a}[0],$customservicegrp{$b}[0]) } keys %customservicegrp){
1594 if ($helper ne $customservicegrp{$key}[0]){
1595 print"<option ";
1596 print"selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'grp3'}} eq $customservicegrp{$key}[0]);
1597 print">$customservicegrp{$key}[0]</option>";
1598 }
1599 $helper=$customservicegrp{$key}[0];
1600 }
1601 print<<END;
1602 </select></td></tr>
1603 <tr><td colspan='2'></td><td><input type='radio' name='grp3' value='TGT_PORT' $checked{'grp3'}{'TGT_PORT'}></td><td>$Lang::tr{'fwdfw man port'}</td><td><select name='TGT_PROT'>
1604 END
1605 foreach ("TCP","UDP","GRE","ESP","AH","ICMP")
1606 {
1607 if ($_ eq $fwdfwsettings{'TGT_PROT'})
1608 {
1609 print"<option selected>$_</option>";
1610 }else{
1611 print"<option>$_</option>";
1612 }
1613 }
1614 $fwdfwsettings{'TGT_PORT'} =~ s/\|/,/g;
1615 print<<END;
1616 </select></td><td align='right'><input type='text' name='TGT_PORT' value='$fwdfwsettings{'TGT_PORT'}' maxlength='20' size='18' ></td></tr>
1617 <tr><td colspan='2'></td><td></td><td>$Lang::tr{'fwhost icmptype'}</td><td colspan='2'><select name='ICMP_TGT' style='min-width:230px;'>
1618 END
1619 &General::readhasharray("${General::swroot}/fwhosts/icmp-types", \%icmptypes);
1620 print"<option>All ICMP-Types</option>";
1621 foreach my $key (sort { ncmp($icmptypes{$a}[0],$icmptypes{$b}[0]) }keys %icmptypes){
1622 if($fwdfwsettings{'ICMP_TGT'} eq "$icmptypes{$key}[0]"){
1623 print"<option selected>$icmptypes{$key}[0] ($icmptypes{$key}[1])</option>";
1624 }else{
1625 print"<option>$icmptypes{$key}[0] ($icmptypes{$key}[1])</option>";
1626 }
1627 }
1628 print<<END;
1629 </select></td></tr>
1630 </table><br><hr>
1631
1632 END
1633 &Header::closebox;
1634 #---SNAT / DNAT ------------------------------------------------
1635 &Header::openbox('100%', 'left', 'NAT');
1636 print<<END;
1637 <table width='100%' border='0'>
1638 <tr><td width='1%'><input type='checkbox' name='USE_NAT' value='ON' $checked{'USE_NAT'}{'ON'}></td><td width='15%'>$Lang::tr{'fwdfw use nat'}</td><td colspan='5'></td></tr>
1639 <tr><td colspan='2'></td><td width='1%'><input type='radio' name='nat' value='dnat' checked ></td><td width='50%'>$Lang::tr{'fwdfw dnat'}</td>
1640 END
1641 print"<td width='8%'>IPFire: </td><td width='20%' align='right'><select name='dnat' style='width:140px;'>";
1642 print "<option value='ALL' $selected{'dnat'}{$Lang::tr{'all'}}>$Lang::tr{'all'}</option>";
1643 print "<option value='Default IP' $selected{'dnat'}{'Default IP'}>Default IP</option>";
1644 foreach my $alias (sort keys %aliases)
1645 {
1646 print "<option value='$alias' $selected{'dnat'}{$alias}>$alias</option>";
1647 }
1648 print"</td></tr>";
1649 $fwdfwsettings{'dnatport'}=~ tr/|/,/;
1650 print"<tr><td colspan='4'></td><td>Port: </td><td align='right'><input type='text' name='dnatport' style='width:130px;' value=$fwdfwsettings{'dnatport'}> </td></tr>";
1651 print"<tr><td colspan='8'><br></td></tr>";
1652 #SNAT
1653 print"<tr><td colspan='2'></td><td width='1%'><input type='radio' name='nat' value='snat' $checked{'nat'}{'snat'}></td><td width='20%'>$Lang::tr{'fwdfw snat'}</td>";
1654 print"<td width='8%'>IPFire: </td><td width='20%' align='right'><select name='snat' style='width:140px;'>";
1655 print "<option value='Default IP' $selected{'snat'}{'Default IP'}>Default IP</option>";
1656 foreach my $alias (sort keys %aliases)
1657 {
1658 print "<option value='$alias' $selected{'snat'}{$alias}>$alias</option>";
1659 }
1660 foreach my $network (sort keys %defaultNetworks)
1661 {
1662 next if($defaultNetworks{$network}{'NAME'} eq "RED");
1663 next if($defaultNetworks{$network}{'NAME'} eq "IPFire");
1664 next if($defaultNetworks{$network}{'NAME'} eq "ALL");
1665 next if($defaultNetworks{$network}{'NAME'} =~ /OpenVPN/i);
1666 print "<option value='$defaultNetworks{$network}{'NAME'}'";
1667 print " selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'nat'}} eq $defaultNetworks{$network}{'NAME'});
1668 print ">$network</option>";
1669 }
1670 print"</table>";
1671 print"<hr>";
1672 &Header::closebox();
1673 #---Activate/logging/remark-------------------------------------
1674 &Header::openbox('100%', 'left', $Lang::tr{'fwdfw additional'});
1675 print<<END;
1676 <table width='100%' border='0'>
1677 <tr><td nowrap>$Lang::tr{'fwdfw rule action'}</td><td><select name='RULE_ACTION'>
1678 END
1679 foreach ("ACCEPT","DROP","REJECT")
1680 {
1681 if($fwdfwsettings{'updatefwrule'} eq 'on'){
1682 print"<option value='$_'";
1683 print "selected='selected'" if ($fwdfwsettings{'RULE_ACTION'} eq $_);
1684 print">$Lang::tr{'fwdfw '.$_}</option>";
1685 }else{
1686 if($fwdfwsettings{'POLICY'} eq 'MODE2'){
1687 $fwdfwsettings{'RULE_ACTION'} = 'DROP';
1688 }
1689 if ($_ eq $fwdfwsettings{'RULE_ACTION'})
1690 {
1691 print"<option value='$_' selected>$Lang::tr{'fwdfw '.$_}</option>";
1692 }else{
1693 print"<option value='$_'>$Lang::tr{'fwdfw '.$_}</option>";
1694 }
1695 }
1696 }
1697 print"</select></td></tr>";
1698 print"<tr><td width='12%'>$Lang::tr{'remark'}:</td><td align='left'><input type='text' name='ruleremark' size='40' maxlength='255' value='$fwdfwsettings{'ruleremark'}'></td></tr>";
1699 if($fwdfwsettings{'updatefwrule'} eq 'on' || $fwdfwsettings{'copyfwrule'} eq 'on'){
1700 print "<tr><td width='12%'>$Lang::tr{'fwdfw rulepos'}:</td><td><select name='rulepos' >";
1701 for (my $count =1; $count <= $sum; $count++){
1702 print"<option value='$count' ";
1703 print"selected='selected'" if($fwdfwsettings{'oldrulenumber'} eq $count);
1704 print">$count</option>";
1705 }
1706 print"</select></td></tr>";
1707 }else{
1708 print "<tr><td width='12%'>$Lang::tr{'fwdfw rulepos'}:</td><td><input type='text' name='rulepos' size='2'></td></tr>";
1709 }
1710
1711 print<<END;
1712 </table><table width='100%'>
1713 <tr><td width='1%'><input type='checkbox' name='ACTIVE' value='ON' $checked{'ACTIVE'}{'ON'}></td><td>$Lang::tr{'fwdfw rule activate'}</td></tr>
1714 <tr><td width='1%'><input type='checkbox' name='LOG' value='ON' $checked{'LOG'}{'ON'} ></td><td>$Lang::tr{'fwdfw log rule'}</td></tr>
1715 </table><br><hr>
1716 END
1717 &Header::closebox();
1718 #---ADD TIMEFRAME-----------------------------------------------
1719 &Header::openbox('100%', 'left', $Lang::tr{'fwdfw timeframe'});
1720 print<<END;
1721 <table width='70%' border='0'>
1722 <tr><td width='1%'><input type='checkbox' name='TIME' value='ON' $checked{'TIME'}{'ON'}></td><td colspan='4'>$Lang::tr{'fwdfw timeframe'}</td></tr>
1723 <tr><td colspan='7'>&nbsp</td></tr>
1724 <tr>
1725 <td align='left'>$Lang::tr{'time'}:</td>
1726 <td width='30%' align='left'>$Lang::tr{'advproxy monday'} $Lang::tr{'advproxy tuesday'} $Lang::tr{'advproxy wednesday'} $Lang::tr{'advproxy thursday'} $Lang::tr{'advproxy friday'} $Lang::tr{'advproxy saturday'} $Lang::tr{'advproxy sunday'}</td>
1727 <td width='15%' align='left'>$Lang::tr{'advproxy from'}</td>
1728 <td width='15%' align='left'>$Lang::tr{'advproxy to'}</td>
1729 </tr>
1730 <tr>
1731 <td align='right'></td>
1732 <td width='30%' align='left'>
1733 <input type='checkbox' name='TIME_MON' value='on' $checked{'TIME_MON'}{'on'} />
1734 <input type='checkbox' name='TIME_TUE' value='on' $checked{'TIME_TUE'}{'on'} />
1735 <input type='checkbox' name='TIME_WED' value='on' $checked{'TIME_WED'}{'on'} />
1736 <input type='checkbox' name='TIME_THU' value='on' $checked{'TIME_THU'}{'on'} />
1737 <input type='checkbox' name='TIME_FRI' value='on' $checked{'TIME_FRI'}{'on'} />
1738 <input type='checkbox' name='TIME_SAT' value='on' $checked{'TIME_SAT'}{'on'} />
1739 <input type='checkbox' name='TIME_SUN' value='on' $checked{'TIME_SUN'}{'on'} />
1740 </td>
1741 <td><select name='TIME_FROM'>
1742 END
1743 for (my $i=0;$i<=23;$i++) {
1744 $i = sprintf("%02s",$i);
1745 for (my $j=0;$j<=45;$j+=15) {
1746 $j = sprintf("%02s",$j);
1747 my $time = $i.":".$j;
1748 print "\t\t\t\t\t<option $selected{'TIME_FROM'}{$time}>$i:$j</option>\n";
1749 }
1750 }
1751 print<<END;
1752 </select></td>
1753 <td><select name='TIME_TO'>
1754 END
1755 for (my $i=0;$i<=23;$i++) {
1756 $i = sprintf("%02s",$i);
1757 for (my $j=0;$j<=45;$j+=15) {
1758 $j = sprintf("%02s",$j);
1759 my $time = $i.":".$j;
1760 print "\t\t\t\t\t<option $selected{'TIME_TO'}{$time}>$i:$j</option>\n";
1761 }
1762 }
1763 print<<END;
1764 </select></td></tr>
1765 </table><br><hr>
1766 END
1767 #---ACTION------------------------------------------------------
1768 if($fwdfwsettings{'updatefwrule'} ne 'on'){
1769 print<<END;
1770 <table border='0' width='100%'>
1771 <tr><td align='right'><input type='submit' value='$Lang::tr{'add'}' style='min-width:100px;' />
1772 <input type='hidden' name='config' value='$config' >
1773 <input type='hidden' name='ACTION' value='saverule' >
1774 </form><form method='post' style='display:inline'><input type='submit' value='$Lang::tr{'fwhost back'}' style='min-width:100px;'><input type='hidden' name='ACTION' value'reset'></td></td>
1775 </table></form>
1776 END
1777 }else{
1778 print<<END;
1779 <table border='0' width='100%'>
1780 <tr><td align='right'><input type='submit' value='$Lang::tr{'fwdfw change'}' style='min-width:100px;' /><input type='hidden' name='updatefwrule' value='$fwdfwsettings{'updatefwrule'}'><input type='hidden' name='key' value='$fwdfwsettings{'key'}'>
1781 <input type='hidden' name='oldgrp1a' value='$fwdfwsettings{'oldgrp1a'}' />
1782 <input type='hidden' name='oldgrp1b' value='$fwdfwsettings{'oldgrp1b'}' />
1783 <input type='hidden' name='oldgrp2a' value='$fwdfwsettings{'oldgrp2a'}' />
1784 <input type='hidden' name='oldgrp2b' value='$fwdfwsettings{'oldgrp2b'}' />
1785 <input type='hidden' name='oldgrp3a' value='$fwdfwsettings{'oldgrp3a'}' />
1786 <input type='hidden' name='oldgrp3b' value='$fwdfwsettings{'oldgrp3b'}' />
1787 <input type='hidden' name='oldusesrv' value='$fwdfwsettings{'oldusesrv'}' />
1788 <input type='hidden' name='oldrulenumber' value='$fwdfwsettings{'oldrulenumber'}' />
1789 <input type='hidden' name='rulenumber' value='$fwdfwsettings{'rulepos'}' />
1790 <input type='hidden' name='oldruleremark' value='$fwdfwsettings{'oldruleremark'}' />
1791 <input type='hidden' name='oldorange' value='$fwdfwsettings{'oldorange'}' />
1792 <input type='hidden' name='oldnat' value='$fwdfwsettings{'oldnat'}' />
1793 <input type='hidden' name='oldruletype' value='$fwdfwsettings{'oldruletype'}' />
1794 <input type='hidden' name='ACTION' value='saverule' ></form><form method='post' style='display:inline'><input type='submit' value='$Lang::tr{'fwhost back'}' style='min-width:100px;'><input type='hidden' name='ACTION' value'reset'></td></td>
1795 </table></form>
1796 END
1797 }
1798 &Header::closebox();
1799 }
1800 sub pos_up
1801 {
1802 my %uphash=();
1803 my %tmp=();
1804 &General::readhasharray($fwdfwsettings{'config'}, \%uphash);
1805 foreach my $key (sort keys %uphash){
1806 if ($key eq $fwdfwsettings{'key'}) {
1807 my $last = $key -1;
1808 if (exists $uphash{$last}){
1809 #save rule last
1810 foreach my $y (0 .. $#{$uphash{$last}}) {
1811 $tmp{0}[$y] = $uphash{$last}[$y];
1812 }
1813 #copy active rule to last
1814 foreach my $i (0 .. $#{$uphash{$last}}) {
1815 $uphash{$last}[$i] = $uphash{$key}[$i];
1816 }
1817 #copy saved rule to actual position
1818 foreach my $x (0 .. $#{$tmp{0}}) {
1819 $uphash{$key}[$x] = $tmp{0}[$x];
1820 }
1821 }
1822 }
1823 }
1824 &General::writehasharray($fwdfwsettings{'config'}, \%uphash);
1825 &rules;
1826 }
1827 sub pos_down
1828 {
1829 my %downhash=();
1830 my %tmp=();
1831 &General::readhasharray($fwdfwsettings{'config'}, \%downhash);
1832 foreach my $key (sort keys %downhash){
1833 if ($key eq $fwdfwsettings{'key'}) {
1834 my $next = $key + 1;
1835 if (exists $downhash{$next}){
1836 #save rule next
1837 foreach my $y (0 .. $#{$downhash{$next}}) {
1838 $tmp{0}[$y] = $downhash{$next}[$y];
1839 }
1840 #copy active rule to next
1841 foreach my $i (0 .. $#{$downhash{$next}}) {
1842 $downhash{$next}[$i] = $downhash{$key}[$i];
1843 }
1844 #copy saved rule to actual position
1845 foreach my $x (0 .. $#{$tmp{0}}) {
1846 $downhash{$key}[$x] = $tmp{0}[$x];
1847 }
1848 }
1849 }
1850 }
1851 &General::writehasharray($fwdfwsettings{'config'}, \%downhash);
1852 &rules;
1853 }
1854 sub rules
1855 {
1856 if (!-f "${General::swroot}/forward/reread"){
1857 system("touch ${General::swroot}/forward/reread");
1858 system("touch ${General::swroot}/fwhosts/reread");
1859 }
1860 }
1861 sub reread_rules
1862 {
1863 system("/usr/local/bin/forwardfwctrl");
1864 if ( -f "${General::swroot}/forward/reread"){
1865 system("rm ${General::swroot}/forward/reread");
1866 system("rm ${General::swroot}/fwhosts/reread");
1867 }
1868 }
1869 sub saverule
1870 {
1871 my $hash=shift;
1872 my $config=shift;
1873 &General::readhasharray("$config", $hash);
1874 if (!$errormessage){
1875 ################################################################
1876 #check if we change an INPUT rule to a OUTGOING
1877 if($fwdfwsettings{'oldruletype'} eq 'INPUTFW' && $fwdfwsettings{'chain'} eq 'OUTGOINGFW' ){
1878 &changerule($configinput);
1879 #print"1";
1880 }
1881 #check if we change an INPUT rule to a FORWARD
1882 elsif($fwdfwsettings{'oldruletype'} eq 'INPUTFW' && $fwdfwsettings{'chain'} eq 'FORWARDFW' ){
1883 &changerule($configinput);
1884 #print"2";
1885 }
1886 ################################################################
1887 #check if we change an OUTGOING rule to an INPUT
1888 elsif($fwdfwsettings{'oldruletype'} eq 'OUTGOINGFW' && $fwdfwsettings{'chain'} eq 'INPUTFW' ){
1889 &changerule($configoutgoing);
1890 #print"3";
1891 }
1892 #check if we change an OUTGOING rule to a FORWARD
1893 elsif($fwdfwsettings{'oldruletype'} eq 'OUTGOINGFW' && $fwdfwsettings{'chain'} eq 'FORWARDFW' ){
1894 &changerule($configoutgoing);
1895 #print"4";
1896 }
1897 ################################################################
1898 #check if we change a FORWARD rule to an INPUT
1899 elsif($fwdfwsettings{'oldruletype'} eq 'FORWARDFW' && $fwdfwsettings{'chain'} eq 'INPUTFW'){
1900 &changerule($configfwdfw);
1901 #print"5";
1902 }
1903 #check if we change a FORWARD rule to an OUTGOING
1904 elsif($fwdfwsettings{'oldruletype'} eq 'FORWARDFW' && $fwdfwsettings{'chain'} eq 'OUTGOINGFW'){
1905 &changerule($configfwdfw);
1906 #print"6";
1907 }
1908 if ($fwdfwsettings{'updatefwrule'} ne 'on'){
1909 my $key = &General::findhasharraykey ($hash);
1910 $$hash{$key}[0] = $fwdfwsettings{'RULE_ACTION'};
1911 $$hash{$key}[1] = $fwdfwsettings{'chain'};
1912 $$hash{$key}[2] = $fwdfwsettings{'ACTIVE'};
1913 $$hash{$key}[3] = $fwdfwsettings{'grp1'};
1914 $$hash{$key}[4] = $fwdfwsettings{$fwdfwsettings{'grp1'}};
1915 $$hash{$key}[5] = $fwdfwsettings{'grp2'};
1916 $$hash{$key}[6] = $fwdfwsettings{$fwdfwsettings{'grp2'}};
1917 $$hash{$key}[7] = $fwdfwsettings{'USE_SRC_PORT'};
1918 $$hash{$key}[8] = $fwdfwsettings{'PROT'};
1919 $$hash{$key}[9] = $fwdfwsettings{'ICMP_TYPES'};
1920 $$hash{$key}[10] = $fwdfwsettings{'SRC_PORT'};
1921 $$hash{$key}[11] = $fwdfwsettings{'USESRV'};
1922 $$hash{$key}[12] = $fwdfwsettings{'TGT_PROT'};
1923 $$hash{$key}[13] = $fwdfwsettings{'ICMP_TGT'};
1924 $$hash{$key}[14] = $fwdfwsettings{'grp3'};
1925 $$hash{$key}[15] = $fwdfwsettings{$fwdfwsettings{'grp3'}};
1926 $$hash{$key}[16] = $fwdfwsettings{'ruleremark'};
1927 $$hash{$key}[17] = $fwdfwsettings{'LOG'};
1928 $$hash{$key}[18] = $fwdfwsettings{'TIME'};
1929 $$hash{$key}[19] = $fwdfwsettings{'TIME_MON'};
1930 $$hash{$key}[20] = $fwdfwsettings{'TIME_TUE'};
1931 $$hash{$key}[21] = $fwdfwsettings{'TIME_WED'};
1932 $$hash{$key}[22] = $fwdfwsettings{'TIME_THU'};
1933 $$hash{$key}[23] = $fwdfwsettings{'TIME_FRI'};
1934 $$hash{$key}[24] = $fwdfwsettings{'TIME_SAT'};
1935 $$hash{$key}[25] = $fwdfwsettings{'TIME_SUN'};
1936 $$hash{$key}[26] = $fwdfwsettings{'TIME_FROM'};
1937 $$hash{$key}[27] = $fwdfwsettings{'TIME_TO'};
1938 $$hash{$key}[28] = $fwdfwsettings{'USE_NAT'};
1939 $$hash{$key}[29] = $fwdfwsettings{$fwdfwsettings{'nat'}};
1940 $$hash{$key}[30] = $fwdfwsettings{'dnatport'};
1941 $$hash{$key}[31] = $fwdfwsettings{'nat'};
1942 &General::writehasharray("$config", $hash);
1943 }else{
1944 foreach my $key (sort {$a <=> $b} keys %$hash){
1945 if($key eq $fwdfwsettings{'key'}){
1946 $$hash{$key}[0] = $fwdfwsettings{'RULE_ACTION'};
1947 $$hash{$key}[1] = $fwdfwsettings{'chain'};
1948 $$hash{$key}[2] = $fwdfwsettings{'ACTIVE'};
1949 $$hash{$key}[3] = $fwdfwsettings{'grp1'};
1950 $$hash{$key}[4] = $fwdfwsettings{$fwdfwsettings{'grp1'}};
1951 $$hash{$key}[5] = $fwdfwsettings{'grp2'};
1952 $$hash{$key}[6] = $fwdfwsettings{$fwdfwsettings{'grp2'}};
1953 $$hash{$key}[7] = $fwdfwsettings{'USE_SRC_PORT'};
1954 $$hash{$key}[8] = $fwdfwsettings{'PROT'};
1955 $$hash{$key}[9] = $fwdfwsettings{'ICMP_TYPES'};
1956 $$hash{$key}[10] = $fwdfwsettings{'SRC_PORT'};
1957 $$hash{$key}[11] = $fwdfwsettings{'USESRV'};
1958 $$hash{$key}[12] = $fwdfwsettings{'TGT_PROT'};
1959 $$hash{$key}[13] = $fwdfwsettings{'ICMP_TGT'};
1960 $$hash{$key}[14] = $fwdfwsettings{'grp3'};
1961 $$hash{$key}[15] = $fwdfwsettings{$fwdfwsettings{'grp3'}};
1962 $$hash{$key}[16] = $fwdfwsettings{'ruleremark'};
1963 $$hash{$key}[17] = $fwdfwsettings{'LOG'};
1964 $$hash{$key}[18] = $fwdfwsettings{'TIME'};
1965 $$hash{$key}[19] = $fwdfwsettings{'TIME_MON'};
1966 $$hash{$key}[20] = $fwdfwsettings{'TIME_TUE'};
1967 $$hash{$key}[21] = $fwdfwsettings{'TIME_WED'};
1968 $$hash{$key}[22] = $fwdfwsettings{'TIME_THU'};
1969 $$hash{$key}[23] = $fwdfwsettings{'TIME_FRI'};
1970 $$hash{$key}[24] = $fwdfwsettings{'TIME_SAT'};
1971 $$hash{$key}[25] = $fwdfwsettings{'TIME_SUN'};
1972 $$hash{$key}[26] = $fwdfwsettings{'TIME_FROM'};
1973 $$hash{$key}[27] = $fwdfwsettings{'TIME_TO'};
1974 $$hash{$key}[28] = $fwdfwsettings{'USE_NAT'};
1975 $$hash{$key}[29] = $fwdfwsettings{$fwdfwsettings{'nat'}};
1976 $$hash{$key}[30] = $fwdfwsettings{'dnatport'};
1977 $$hash{$key}[31] = $fwdfwsettings{'nat'};
1978 last;
1979 }
1980 }
1981 }
1982 &General::writehasharray("$config", $hash);
1983 if($fwdfwsettings{'oldrulenumber'} > $fwdfwsettings{'rulepos'}){
1984 my %tmp=();
1985 my $val=$fwdfwsettings{'oldrulenumber'}-$fwdfwsettings{'rulepos'};
1986 for (my $z=0;$z<$val;$z++){
1987 foreach my $key (sort {$a <=> $b} keys %$hash){
1988 if ($key eq $fwdfwsettings{'oldrulenumber'}) {
1989 my $last = $key -1;
1990 if (exists $$hash{$last}){
1991 #save rule last
1992 foreach my $y (0 .. $#{$$hash{$last}}) {
1993 $tmp{0}[$y] = $$hash{$last}[$y];
1994 }
1995 #copy active rule to last
1996 foreach my $i (0 .. $#{$$hash{$last}}) {
1997 $$hash{$last}[$i] = $$hash{$key}[$i];
1998 }
1999 #copy saved rule to actual position
2000 foreach my $x (0 .. $#{$tmp{0}}) {
2001 $$hash{$key}[$x] = $tmp{0}[$x];
2002 }
2003 }
2004 }
2005 }
2006 $fwdfwsettings{'oldrulenumber'}--;
2007 }
2008 &General::writehasharray("$config", $hash);
2009 &rules;
2010 }elsif($fwdfwsettings{'rulepos'} > $fwdfwsettings{'oldrulenumber'}){
2011 my %tmp=();
2012 my $val=$fwdfwsettings{'rulepos'}-$fwdfwsettings{'oldrulenumber'};
2013 for (my $z=0;$z<$val;$z++){
2014 foreach my $key (sort {$a <=> $b} keys %$hash){
2015 if ($key eq $fwdfwsettings{'oldrulenumber'}) {
2016 my $next = $key + 1;
2017 if (exists $$hash{$next}){
2018 #save rule next
2019 foreach my $y (0 .. $#{$$hash{$next}}) {
2020 $tmp{0}[$y] = $$hash{$next}[$y];
2021 }
2022 #copy active rule to next
2023 foreach my $i (0 .. $#{$$hash{$next}}) {
2024 $$hash{$next}[$i] = $$hash{$key}[$i];
2025 }
2026 #copy saved rule to actual position
2027 foreach my $x (0 .. $#{$tmp{0}}) {
2028 $$hash{$key}[$x] = $tmp{0}[$x];
2029 }
2030 }
2031 }
2032 }
2033 $fwdfwsettings{'oldrulenumber'}++;
2034 }
2035 &General::writehasharray("$config", $hash);
2036 &rules;
2037 }
2038 }
2039 }
2040 sub validremark
2041 {
2042 # Checks a hostname against RFC1035
2043 my $remark = $_[0];
2044
2045 # Each part should be at least two characters in length
2046 # but no more than 63 characters
2047 if (length ($remark) < 1 || length ($remark) > 255) {
2048 return 0;}
2049 # Only valid characters are a-z, A-Z, 0-9 and -
2050 if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) {
2051 return 0;}
2052 # First character can only be a letter or a digit
2053 if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) {
2054 return 0;}
2055 # Last character can only be a letter or a digit
2056 if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) {
2057 return 0;}
2058 return 1;
2059 }
2060 sub viewtablerule
2061 {
2062 &General::readhash("/var/ipfire/ethernet/settings", \%netsettings);
2063 &viewtablenew(\%configfwdfw,$configfwdfw,"","Forward" );
2064 &viewtablenew(\%configinputfw,$configinput,"",$Lang::tr{'fwdfw xt access'} );
2065 &viewtablenew(\%configoutgoingfw,$configoutgoing,"","Outgoing" );
2066 }
2067 sub viewtablenew
2068 {
2069 my $hash=shift;
2070 my $config=shift;
2071 my $title=shift;
2072 my $title1=shift;
2073 my $go='';
2074 &General::get_aliases(\%aliases);
2075 &General::readhasharray("$confighost", \%customhost);
2076 &General::readhasharray("$config", $hash);
2077 &General::readhasharray("$configccdnet", \%ccdnet);
2078 &General::readhasharray("$configccdhost", \%ccdhost);
2079 if( ! -z $config){
2080 &Header::openbox('100%', 'left',$title);
2081 my $count=0;
2082 my ($gif,$log);
2083 my $ruletype;
2084 my $rulecolor;
2085 my $tooltip;
2086 my @tmpsrc=();
2087 my $coloryellow='';
2088 print"<b>$title1</b><br>";
2089 print"<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
2090 print"<tr><td align='center'><b>#</td><td ></td><td align='center'><b>$Lang::tr{'fwdfw source'}</td><td width='1%'><b>Log</td><td align='center'><b>$Lang::tr{'fwdfw target'}</td><td align='center' width='25'></td><td align='center' colspan='6' width='1%'><b>$Lang::tr{'fwdfw action'}</td></tr>";#<td align='center'><b>$Lang::tr{'fwdfw time'}</td><b>$Lang::tr{'protocol'}</b>
2091 foreach my $key (sort {$a <=> $b} keys %$hash){
2092 $tdcolor='';
2093 @tmpsrc=();
2094 #check if vpn hosts/nets have been deleted
2095 if($$hash{$key}[3] =~ /ipsec/i || $$hash{$key}[3] =~ /ovpn/i){
2096 push (@tmpsrc,$$hash{$key}[4]);
2097 }
2098 if($$hash{$key}[5] =~ /ipsec/i || $$hash{$key}[5] =~ /ovpn/i){
2099 push (@tmpsrc,$$hash{$key}[6]);
2100 }
2101 foreach my $host (@tmpsrc){
2102 if($$hash{$key}[3] eq 'ipsec_net_src' || $$hash{$key}[5] eq 'ipsec_net_tgt'){
2103 if(&fwlib::get_ipsec_net_ip($host,11) eq ''){
2104 $coloryellow='on';
2105 &disable_rule($key);
2106 $$hash{$key}[2]='';
2107 }
2108 }elsif($$hash{$key}[3] eq 'ovpn_net_src' || $$hash{$key}[5] eq 'ovpn_net_tgt'){
2109 if(&fwlib::get_ovpn_net_ip($host,1) eq ''){
2110 $coloryellow='on';
2111 &disable_rule($key);
2112 $$hash{$key}[2]='';