[v2] general-functions.pl: Update to fix bug #12428

Message ID 20210102125445.3389-1-ahb.ipfire@gmail.com
State Accepted
Commit 5b4e33bc3b754dd14ecc279bc421ec232a489507
Headers
Series [v2] general-functions.pl: Update to fix bug #12428 |

Commit Message

Adolf Belka Jan. 2, 2021, 12:54 p.m. UTC
  - Patch of general-functions.pl for implementation of fix provided
	by Bernhard Bitsch in bug #12428.
	Had to be modified as that fix gave a failure for single character hostnames.
	Updated version prevents spaces being put into hostnames and works for single
	character hostnames
- Updated subroutine validfqdn to apply consistent rules for hostname & domain name
	portions of fqdn
- Minor updates for consistency across validhostname, validdomainname & validfqdn
- Patch implemented into testbed system and confirmed working for hostnames, domain names
	and FQDN's.

Signed-off-by: Adolf Belka <ahb.ipfire@gmail.com>
---
 config/cfgroot/general-functions.pl | 51 ++++++++++++++++-------------
 1 file changed, 29 insertions(+), 22 deletions(-)
  

Comments

Michael Tremer Jan. 4, 2021, 10:47 a.m. UTC | #1
Hello Adolf,

Thank you for submitting this patched.

I merged it, but it probably should have been a small series of patches. The changes do not really have a connection and it would have been easier to review the changes, or potentially revert one of them if something goes wrong later.

Just for next time :) But keep up the great work.

-Michael

> On 2 Jan 2021, at 12:54, Adolf Belka <ahb.ipfire@gmail.com> wrote:
> 
> - Patch of general-functions.pl for implementation of fix provided
> 	by Bernhard Bitsch in bug #12428.
> 	Had to be modified as that fix gave a failure for single character hostnames.
> 	Updated version prevents spaces being put into hostnames and works for single
> 	character hostnames
> - Updated subroutine validfqdn to apply consistent rules for hostname & domain name
> 	portions of fqdn
> - Minor updates for consistency across validhostname, validdomainname & validfqdn
> - Patch implemented into testbed system and confirmed working for hostnames, domain names
> 	and FQDN's.
> 
> Signed-off-by: Adolf Belka <ahb.ipfire@gmail.com>
> ---
> config/cfgroot/general-functions.pl | 51 ++++++++++++++++-------------
> 1 file changed, 29 insertions(+), 22 deletions(-)
> 
> diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
> index 9be1e7708..a6656ccf5 100644
> --- a/config/cfgroot/general-functions.pl
> +++ b/config/cfgroot/general-functions.pl
> @@ -635,12 +635,12 @@ sub validhostname
> 	# Checks a hostname against RFC1035
>         my $hostname = $_[0];
> 
> -	# Each part should be at least two characters in length
> +	# Hostname should be at least one character in length
> 	# but no more than 63 characters
> 	if (length ($hostname) < 1 || length ($hostname) > 63) {
> 		return 0;}
> 	# Only valid characters are a-z, A-Z, 0-9 and -
> -	if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
> +	if ($hostname !~ /^[a-zA-Z0-9-]*$/) {
> 		return 0;}
> 	# First character can only be a letter or a digit
> 	if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
> @@ -655,46 +655,53 @@ sub validdomainname
> {
> 	my $part;
> 
> -	# Checks a domain name against RFC1035
> +	# Checks a domain name against RFC1035 and RFC2181
>         my $domainname = $_[0];
> -	my @parts = split (/\./, $domainname);	# Split hostname at the '.'
> +	my @parts = split (/\./, $domainname);	# Split domain name at the '.'
> 
> 	foreach $part (@parts) {
> -		# Each part should be no more than 63 characters in length
> +		# Each part should be at least one character in length
> +		# but no more than 63 characters
> 		if (length ($part) < 1 || length ($part) > 63) {
> 			return 0;}
> 		# Only valid characters are a-z, A-Z, 0-9, _ and -
> 		if ($part !~ /^[a-zA-Z0-9_-]*$/) {
> -			return 0;
> -		}
> +			return 0;}
> 	}
> 	return 1;
> }
> 
> sub validfqdn
> {
> -	my $part;
> -
> -	# Checks a fully qualified domain name against RFC1035
> +	# Checks a fully qualified domain name against RFC1035 and RFC2181
>         my $fqdn = $_[0];
> -	my @parts = split (/\./, $fqdn);	# Split hostname at the '.'
> +	my @parts = split (/\./, $fqdn);	# Split FQDN at the '.'
> 	if (scalar(@parts) < 2) {		# At least two parts should
> 		return 0;}			# exist in a FQDN
> 						# (i.e.hostname.domain)
> -	foreach $part (@parts) {
> +
> +	for (my $index=0; $index < scalar(@parts); $index++) {
> 		# Each part should be at least one character in length
> 		# but no more than 63 characters
> -		if (length ($part) < 1 || length ($part) > 63) {
> -			return 0;}
> -		# Only valid characters are a-z, A-Z, 0-9 and -
> -		if ($part !~ /^[a-zA-Z0-9-]*$/) {
> -			return 0;}
> -		# First character can only be a letter or a digit
> -		if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
> -			return 0;}
> -		# Last character can only be a letter or a digit
> -		if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
> +		if (length ($parts[$index]) < 1 || length ($parts[$index]) > 63) {
> 			return 0;}
> +		if ($index eq 0) {		
> +			# This is the hostname part
> +			# Only valid characters are a-z, A-Z, 0-9 and -
> +			if ($parts[$index] !~ /^[a-zA-Z0-9-]*$/) {
> +				return 0;}
> +			# First character can only be a letter or a digit
> +			if (substr ($parts[$index], 0, 1) !~ /^[a-zA-Z0-9]*$/) {
> +				return 0;}
> +			# Last character can only be a letter or a digit
> +			if (substr ($parts[$index], -1, 1) !~ /^[a-zA-Z0-9]*$/) {
> +				return 0;}
> +		} else{				
> +			# This is the domain part
> +			# Only valid characters are a-z, A-Z, 0-9, _ and -
> +			if ($parts[$index] !~ /^[a-zA-Z0-9_-]*$/) {
> +				return 0;}
> +		}
> 	}
> 	return 1;
> }
> -- 
> 2.30.0
>
  
Adolf Belka Jan. 5, 2021, 9:22 a.m. UTC | #2
Hi Michael,


On 04/01/2021 11:47, Michael Tremer wrote:
> Hello Adolf,
> 
> Thank you for submitting this patched.
> 
> I merged it, but it probably should have been a small series of patches. The changes do not really have a connection and it would have been easier to review the changes, or potentially revert one of them if something goes wrong later.
> 
> Just for next time :) But keep up the great work.
Noted for future. Glad I can help with the smaller things to help take some load off from all of you.

Adolf.
> 
> -Michael
> 
>> On 2 Jan 2021, at 12:54, Adolf Belka <ahb.ipfire@gmail.com> wrote:
>>
>> - Patch of general-functions.pl for implementation of fix provided
>> 	by Bernhard Bitsch in bug #12428.
>> 	Had to be modified as that fix gave a failure for single character hostnames.
>> 	Updated version prevents spaces being put into hostnames and works for single
>> 	character hostnames
>> - Updated subroutine validfqdn to apply consistent rules for hostname & domain name
>> 	portions of fqdn
>> - Minor updates for consistency across validhostname, validdomainname & validfqdn
>> - Patch implemented into testbed system and confirmed working for hostnames, domain names
>> 	and FQDN's.
>>
>> Signed-off-by: Adolf Belka <ahb.ipfire@gmail.com>
>> ---
>> config/cfgroot/general-functions.pl | 51 ++++++++++++++++-------------
>> 1 file changed, 29 insertions(+), 22 deletions(-)
>>
>> diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
>> index 9be1e7708..a6656ccf5 100644
>> --- a/config/cfgroot/general-functions.pl
>> +++ b/config/cfgroot/general-functions.pl
>> @@ -635,12 +635,12 @@ sub validhostname
>> 	# Checks a hostname against RFC1035
>>          my $hostname = $_[0];
>>
>> -	# Each part should be at least two characters in length
>> +	# Hostname should be at least one character in length
>> 	# but no more than 63 characters
>> 	if (length ($hostname) < 1 || length ($hostname) > 63) {
>> 		return 0;}
>> 	# Only valid characters are a-z, A-Z, 0-9 and -
>> -	if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
>> +	if ($hostname !~ /^[a-zA-Z0-9-]*$/) {
>> 		return 0;}
>> 	# First character can only be a letter or a digit
>> 	if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
>> @@ -655,46 +655,53 @@ sub validdomainname
>> {
>> 	my $part;
>>
>> -	# Checks a domain name against RFC1035
>> +	# Checks a domain name against RFC1035 and RFC2181
>>          my $domainname = $_[0];
>> -	my @parts = split (/\./, $domainname);	# Split hostname at the '.'
>> +	my @parts = split (/\./, $domainname);	# Split domain name at the '.'
>>
>> 	foreach $part (@parts) {
>> -		# Each part should be no more than 63 characters in length
>> +		# Each part should be at least one character in length
>> +		# but no more than 63 characters
>> 		if (length ($part) < 1 || length ($part) > 63) {
>> 			return 0;}
>> 		# Only valid characters are a-z, A-Z, 0-9, _ and -
>> 		if ($part !~ /^[a-zA-Z0-9_-]*$/) {
>> -			return 0;
>> -		}
>> +			return 0;}
>> 	}
>> 	return 1;
>> }
>>
>> sub validfqdn
>> {
>> -	my $part;
>> -
>> -	# Checks a fully qualified domain name against RFC1035
>> +	# Checks a fully qualified domain name against RFC1035 and RFC2181
>>          my $fqdn = $_[0];
>> -	my @parts = split (/\./, $fqdn);	# Split hostname at the '.'
>> +	my @parts = split (/\./, $fqdn);	# Split FQDN at the '.'
>> 	if (scalar(@parts) < 2) {		# At least two parts should
>> 		return 0;}			# exist in a FQDN
>> 						# (i.e.hostname.domain)
>> -	foreach $part (@parts) {
>> +
>> +	for (my $index=0; $index < scalar(@parts); $index++) {
>> 		# Each part should be at least one character in length
>> 		# but no more than 63 characters
>> -		if (length ($part) < 1 || length ($part) > 63) {
>> -			return 0;}
>> -		# Only valid characters are a-z, A-Z, 0-9 and -
>> -		if ($part !~ /^[a-zA-Z0-9-]*$/) {
>> -			return 0;}
>> -		# First character can only be a letter or a digit
>> -		if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
>> -			return 0;}
>> -		# Last character can only be a letter or a digit
>> -		if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
>> +		if (length ($parts[$index]) < 1 || length ($parts[$index]) > 63) {
>> 			return 0;}
>> +		if ($index eq 0) {		
>> +			# This is the hostname part
>> +			# Only valid characters are a-z, A-Z, 0-9 and -
>> +			if ($parts[$index] !~ /^[a-zA-Z0-9-]*$/) {
>> +				return 0;}
>> +			# First character can only be a letter or a digit
>> +			if (substr ($parts[$index], 0, 1) !~ /^[a-zA-Z0-9]*$/) {
>> +				return 0;}
>> +			# Last character can only be a letter or a digit
>> +			if (substr ($parts[$index], -1, 1) !~ /^[a-zA-Z0-9]*$/) {
>> +				return 0;}
>> +		} else{				
>> +			# This is the domain part
>> +			# Only valid characters are a-z, A-Z, 0-9, _ and -
>> +			if ($parts[$index] !~ /^[a-zA-Z0-9_-]*$/) {
>> +				return 0;}
>> +		}
>> 	}
>> 	return 1;
>> }
>> -- 
>> 2.30.0
>>
>
  

Patch

diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
index 9be1e7708..a6656ccf5 100644
--- a/config/cfgroot/general-functions.pl
+++ b/config/cfgroot/general-functions.pl
@@ -635,12 +635,12 @@  sub validhostname
 	# Checks a hostname against RFC1035
         my $hostname = $_[0];
 
-	# Each part should be at least two characters in length
+	# Hostname should be at least one character in length
 	# but no more than 63 characters
 	if (length ($hostname) < 1 || length ($hostname) > 63) {
 		return 0;}
 	# Only valid characters are a-z, A-Z, 0-9 and -
-	if ($hostname !~ /^[a-zA-Z0-9-\s]*$/) {
+	if ($hostname !~ /^[a-zA-Z0-9-]*$/) {
 		return 0;}
 	# First character can only be a letter or a digit
 	if (substr ($hostname, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
@@ -655,46 +655,53 @@  sub validdomainname
 {
 	my $part;
 
-	# Checks a domain name against RFC1035
+	# Checks a domain name against RFC1035 and RFC2181
         my $domainname = $_[0];
-	my @parts = split (/\./, $domainname);	# Split hostname at the '.'
+	my @parts = split (/\./, $domainname);	# Split domain name at the '.'
 
 	foreach $part (@parts) {
-		# Each part should be no more than 63 characters in length
+		# Each part should be at least one character in length
+		# but no more than 63 characters
 		if (length ($part) < 1 || length ($part) > 63) {
 			return 0;}
 		# Only valid characters are a-z, A-Z, 0-9, _ and -
 		if ($part !~ /^[a-zA-Z0-9_-]*$/) {
-			return 0;
-		}
+			return 0;}
 	}
 	return 1;
 }
 
 sub validfqdn
 {
-	my $part;
-
-	# Checks a fully qualified domain name against RFC1035
+	# Checks a fully qualified domain name against RFC1035 and RFC2181
         my $fqdn = $_[0];
-	my @parts = split (/\./, $fqdn);	# Split hostname at the '.'
+	my @parts = split (/\./, $fqdn);	# Split FQDN at the '.'
 	if (scalar(@parts) < 2) {		# At least two parts should
 		return 0;}			# exist in a FQDN
 						# (i.e.hostname.domain)
-	foreach $part (@parts) {
+
+	for (my $index=0; $index < scalar(@parts); $index++) {
 		# Each part should be at least one character in length
 		# but no more than 63 characters
-		if (length ($part) < 1 || length ($part) > 63) {
-			return 0;}
-		# Only valid characters are a-z, A-Z, 0-9 and -
-		if ($part !~ /^[a-zA-Z0-9-]*$/) {
-			return 0;}
-		# First character can only be a letter or a digit
-		if (substr ($part, 0, 1) !~ /^[a-zA-Z0-9]*$/) {
-			return 0;}
-		# Last character can only be a letter or a digit
-		if (substr ($part, -1, 1) !~ /^[a-zA-Z0-9]*$/) {
+		if (length ($parts[$index]) < 1 || length ($parts[$index]) > 63) {
 			return 0;}
+		if ($index eq 0) {		
+			# This is the hostname part
+			# Only valid characters are a-z, A-Z, 0-9 and -
+			if ($parts[$index] !~ /^[a-zA-Z0-9-]*$/) {
+				return 0;}
+			# First character can only be a letter or a digit
+			if (substr ($parts[$index], 0, 1) !~ /^[a-zA-Z0-9]*$/) {
+				return 0;}
+			# Last character can only be a letter or a digit
+			if (substr ($parts[$index], -1, 1) !~ /^[a-zA-Z0-9]*$/) {
+				return 0;}
+		} else{				
+			# This is the domain part
+			# Only valid characters are a-z, A-Z, 0-9, _ and -
+			if ($parts[$index] !~ /^[a-zA-Z0-9_-]*$/) {
+				return 0;}
+		}
 	}
 	return 1;
 }