Skip to content

Commit

Permalink
Part of IPv6 specific checking moved to yast-yast2.
Browse files Browse the repository at this point in the history
Minor refactoring in CheckHostname.
  • Loading branch information
mchf authored and mvidner committed Nov 22, 2012
1 parent e33aafc commit af57a16
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions src/routines.ycp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import "Package";
import "Report";
import "IP";
import "Hostname";
import "String";

/**
Expand Down Expand Up @@ -73,66 +74,35 @@
});
}

/**
* If param contains IPv6 in one of its various forms, extracts it.
*
* @param ip a buffer with address
* @return IPv6 part of ip param, unchanged ip param otherwise
*/
define string ParseIpv6( string ip)
{
// if ip is closed in [ ] or contain % then it can be special case of IPv6 syntax,
// so extract ipv6 (see description later) and continue with check.
//
// IPv6 syntax:
// - pure ipv6 blob (e.g. f008::1)
// - ipv6 blob with link local suffix (e.g. f008::1%eth0)
// - dtto in square brackets (e.g. [f008::1%eth0] )
if( regexpmatch( ip, "^\\[.*\\]") || regexpmatch( ip, "^[^][%]+(%[^][%]+){0,1}$"))
ip = regexpsub( ip, "^\\[?([^][%]+)(%[^][%]+){0,1}(\\]|$)", "\\1");

return ip;
}

/**
* Checks if given entry is valid ip address
*/
define boolean CheckIp( string ip)
{
return IP::Check( ParseIpv6( ip) );
}

/**
* Check for the validity of a hostname: nonempty, shorter than 50 chars,
* [-A-Za-z._]. If invalid, a message is displayed.
* @param name a hostname
* @return whether valid
*/
define boolean CheckHostName(string name) ``{
string dirname_forbidden = ":[]";

y2milestone( "CheckHostName: hostname=%1", name);

if (size(name) > 0 &&
size(name) < 50 &&
name == filterchars(name, "-_.:[]%" + String::CAlnum() ))
size(name) < 50)
{
if( CheckIp( name))
if( IP::Check4( name))
return true;
if( IP::Check6( IP::UndecorateIPv6( name)))
return true;
if( "" == filterchars( name, dirname_forbidden))
if( Hostname::CheckDomain( name))
return true;
}

Report::Error (sformat(_("The hostname entered is invalid.
Url contains forbidden characters or IP is incorrect.
Use url, IPv4 or IPv6.")));
// error popup message

return false;
}
else
{
// error popup message
Report::Error (sformat(_("The hostname entered is invalid. It must be
Report::Error ( sformat(_("The hostname entered is invalid. It must be
shorter than 50 characters and only use
0-9, A-Z, a-z, dots, -, and _.:[]%%")));
}
valid IPv4, IPv6 or domain name.
Valid IPv4: %1
Valid IPv6: %2
Valid domain: %3"), IP::Valid4(), IP::Valid6(), Hostname::ValidDomain()) );

return false;
};
Expand Down Expand Up @@ -260,8 +230,10 @@ and it must begin with a slash (/).")));
*/
define string FormatHostnameForFstab( string hostname)
{
if( IP::Check6( ParseIpv6( hostname)))
return sformat( regexpmatch( "\\[.*\\]", hostname) ? "%1" : "[%1]", hostname);
y2milestone( "FormatHostnameForFstab: hostname=%1", hostname);

if( IP::Check6( IP::UndecorateIPv6( hostname)))
return sformat( regexpmatch( hostname, "\\[.*\\]") ? "%1" : "[%1]", hostname);
return hostname;
}

Expand Down

0 comments on commit af57a16

Please sign in to comment.