bug 8572: added more restrictions for network's domain name (see below)

status 8572: resolved fixed

1) As full domain name may not exceed a total length of 253 characters, and host name can be 63 chars long, so don't allow network domain length to exceed 190 chars.
2) Each label can have up to 63 chars
3) The characters allowed in a label are a subset of the ASCII character set, and includes the characters a through z, A through Z, digits 0 through 9, and the hyphen.Labels may not start or end with a hyphen.
This commit is contained in:
alena 2011-02-16 15:25:43 -08:00
parent 53adcc64a6
commit 6913ed21c5
3 changed files with 45 additions and 11 deletions

View File

@ -1419,6 +1419,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
// If networkDomain is not specified, take it from the global configuration
if (networkDomain == null) {
networkDomain = "cs"+Long.toHexString(owner.getId())+_networkDomain;
} else {
//validate network domain
if (!NetUtils.verifyDomainName(networkDomain)) {
throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters 'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
}
}
// Check if zone exists

View File

@ -2054,9 +2054,8 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
if (hostName == null) {
hostName = instanceName;
} else {
hostName = hostName.toLowerCase();
//verify hostName (hostname doesn't have to be unique)
if (!NetUtils.verifyHostName(hostName)) {
if (!NetUtils.verifyDomainNameLabel(hostName, true)) {
throw new InvalidParameterValueException("Invalid name. Vm name can contain ASCII letters 'a' through 'z', the digits '0' through '9', " +
"and the hyphen ('-'), must be between 1 and 63 characters long, and can't start or end with \"-\" and can't start with digit");
}

View File

@ -873,21 +873,51 @@ public class NetUtils {
}
}
public static boolean verifyHostName(String hostName) {
public static boolean verifyDomainNameLabel(String hostName, boolean isHostName) {
//must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner),
//the digits '0' through '9', and the hyphen ('-').
//Can not start with a hyphen and digit, and must not end with a hyphen
//If it's a host name, don't allow to start with digit
boolean result = true;
if (hostName.length() > 63 || hostName.length() < 1) {
result = false;
} else if (!hostName.matches("[a-zA-z0-9-]*")) {
result = false;
} else if (hostName.startsWith("-") || hostName.matches("^[0-9-].*")|| hostName.endsWith("-")) {
result = false;
}
s_logger.warn("Domain name label must be between 1 and 63 characters long");
return false;
} else if (!hostName.toLowerCase().matches("[a-zA-z0-9-]*")) {
s_logger.warn("Domain name label may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner)");
return false;
} else if (hostName.startsWith("-") || hostName.endsWith("-")) {
s_logger.warn("Domain name label can not start with a hyphen and digit, and must not end with a hyphen");
return false;
} else if (isHostName && hostName.matches("^[0-9-].*")) {
s_logger.warn("Host name can't start with digit");
return false;
}
return result;
return true;
}
public static boolean verifyDomainName(String domainName) {
//don't allow domain name length to exceed 190 chars (190 + 63 (max host name length) = 253 = max domainName length
if (domainName.length() < 1 || domainName.length() > 190) {
s_logger.trace("Domain name must be between 1 and 190 characters long");
return false;
}
if (domainName.startsWith(".") || domainName.endsWith(".")) {
s_logger.trace("Domain name can't start or end with .");
return false;
}
String[] domainNameLabels = domainName.split("\\.");
for (int i = 0; i < domainNameLabels.length; i++) {
if (!verifyDomainNameLabel(domainNameLabels[i], false)) {
s_logger.warn("Domain name label " + domainNameLabels[i] + " is incorrect");
return false;
}
}
return true;
}
public static String getDhcpRange(String cidr) {