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