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