From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexander Marx To: development@lists.ipfire.org Subject: [PATCH] BUG10940: remove leading zeros in ip address Date: Mon, 09 Nov 2015 12:42:47 +0100 Message-ID: <1447069367-7823-1-git-send-email-alexander.marx@ipfire.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============4272423756303773964==" List-Id: --===============4272423756303773964== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable in firewallgroups (hosts) an error was created when using ip adresses like 192.168.000.008. Now all leading zeros are deleted in firewallgroups and in the firewall itself when using single ip addresses as source or target. Signed-off-by: Alexander Marx --- config/cfgroot/network-functions.pl | 13 +++++++++++++ html/cgi-bin/firewall.cgi | 8 ++++++++ html/cgi-bin/fwhosts.cgi | 13 +++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-fun= ctions.pl index cb4ca3d..70fa5ed 100644 --- a/config/cfgroot/network-functions.pl +++ b/config/cfgroot/network-functions.pl @@ -122,6 +122,19 @@ sub network2bin($) { return ($network_start, $netmask_bin); } =20 +# Deletes leading zeros in ip address +sub ip_remove_zero{ + my $address =3D shift; + my @ip =3D split (/\./, $address); + + foreach my $octet (@ip) { + $octet =3D int($octet); + } + + $address =3D join (".", @ip); + + return $address; +} # Returns True for all valid IP addresses sub check_ip_address($) { my $address =3D shift; diff --git a/html/cgi-bin/firewall.cgi b/html/cgi-bin/firewall.cgi index 682c285..8007182 100644 --- a/html/cgi-bin/firewall.cgi +++ b/html/cgi-bin/firewall.cgi @@ -31,6 +31,7 @@ no warnings 'uninitialized'; #use CGI::Carp 'fatalsToBrowser'; =20 require '/var/ipfire/general-functions.pl'; +require '/var/ipfire/network-functions.pl'; require "${General::swroot}/lang.pl"; require "${General::swroot}/header.pl"; require "${General::swroot}/geoip-functions.pl"; @@ -465,6 +466,9 @@ sub checksource } } if ($fwdfwsettings{'isip'} eq 'on'){ + #remove leading zero + $ip =3D &Network::ip_remove_zero($ip); + ##check if ip is valid if (! &General::validip($ip)){ $errormessage.=3D$Lang::tr{'fwdfw err src_addr'}."
"; @@ -569,11 +573,15 @@ sub checktarget ($ip,$subnet)=3Dsplit (/\//,$fwdfwsettings{'tgt_addr'}); $subnet =3D &General::iporsubtocidr($subnet); } + #check if only ip if($fwdfwsettings{'tgt_addr'}=3D~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,= 3})$/){ $ip=3D$fwdfwsettings{'tgt_addr'}; $subnet=3D'32'; } + #remove leading zero + $ip =3D &Network::ip_remove_zero($ip); + #check if ip is valid if (! &General::validip($ip)){ $errormessage.=3D$Lang::tr{'fwdfw err tgt_addr'}."
"; diff --git a/html/cgi-bin/fwhosts.cgi b/html/cgi-bin/fwhosts.cgi index 994a50a..35afad3 100644 --- a/html/cgi-bin/fwhosts.cgi +++ b/html/cgi-bin/fwhosts.cgi @@ -27,6 +27,7 @@ use Sort::Naturally; use CGI::Carp 'fatalsToBrowser'; no warnings 'uninitialized'; require '/var/ipfire/general-functions.pl'; +require '/var/ipfire/network-functions.pl'; require "/var/ipfire/geoip-functions.pl"; require "/usr/lib/firewall/firewall-lib.pl"; require "${General::swroot}/lang.pl"; @@ -277,6 +278,9 @@ if ($fwhostsettings{'ACTION'} eq 'savenet' ) &addnet; &viewtablenet; }else{ + #convert ip if leading '0' exists + $fwhostsettings{'IP'} =3D &Network::ip_remove_zero($fwhostsettings{'IP'}); + #check valid ip=20 if (!&General::validipandmask($fwhostsettings{'IP'}."/".$fwhostsettings{'S= UBNET'})) { @@ -372,9 +376,6 @@ if ($fwhostsettings{'ACTION'} eq 'savenet' ) foreach my $i (0 .. 3) { $customnetwork{$key}[$i] =3D "";} $fwhostsettings{'SUBNET'} =3D &General::iporsubtocidr($fwhostsettings{'SU= BNET'}); $customnetwork{$key}[0] =3D $fwhostsettings{'HOSTNAME'}; - #convert ip when leading '0' in byte - $fwhostsettings{'IP'} =3D&General::ip2dec($fwhostsettings{'IP'}); - $fwhostsettings{'IP'} =3D&General::dec2ip($fwhostsettings{'IP'}); $customnetwork{$key}[1] =3D &General::getnetworkip($fwhostsettings{'IP'}= ,$fwhostsettings{'SUBNET'}) ; $customnetwork{$key}[2] =3D &General::iporsubtodec($fwhostsettings{'SUBN= ET'}) ; $customnetwork{$key}[3] =3D $fwhostsettings{'NETREMARK'}; @@ -423,6 +424,9 @@ if ($fwhostsettings{'ACTION'} eq 'savehost') } #CHECK IP-PART if ($fwhostsettings{'type'} eq 'ip'){ + #convert ip if leading '0' exists + $fwhostsettings{'IP'} =3D &Network::ip_remove_zero($fwhostsettings{'IP'}); + #check for subnet if (rindex($fwhostsettings{'IP'},'/') eq '-1' ){ if($fwhostsettings{'type'} eq 'ip' && !&General::validipandmask($fwhosts= ettings{'IP'}."/32")) @@ -503,9 +507,6 @@ if ($fwhostsettings{'ACTION'} eq 'savehost') $customhost{$key}[0] =3D $fwhostsettings{'HOSTNAME'} ; $customhost{$key}[1] =3D $fwhostsettings{'type'} ; if ($fwhostsettings{'type'} eq 'ip'){ - #convert ip when leading '0' in byte - $fwhostsettings{'IP'}=3D&General::ip2dec($fwhostsettings{'IP'}); - $fwhostsettings{'IP'}=3D&General::dec2ip($fwhostsettings{'IP'}); $customhost{$key}[2] =3D $fwhostsettings{'IP'}."/".&General::iporsubtode= c($fwhostsettings{'SUBNET'}); }else{ $customhost{$key}[2] =3D $fwhostsettings{'IP'}; --=20 1.9.1 --===============4272423756303773964==--