From 6913ed21c55a6a5045d8570b17686a23ec84f675 Mon Sep 17 00:00:00 2001 From: alena Date: Wed, 16 Feb 2011 15:25:43 -0800 Subject: [PATCH] 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. --- .../com/cloud/network/NetworkManagerImpl.java | 5 ++ .../src/com/cloud/vm/UserVmManagerImpl.java | 3 +- utils/src/com/cloud/utils/net/NetUtils.java | 48 +++++++++++++++---- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 03614253f49..b02a7b75b9e 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -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 diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 7ee63b7e11e..73b52729296 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -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"); } diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java index 080d111891e..153f5bc057e 100755 --- a/utils/src/com/cloud/utils/net/NetUtils.java +++ b/utils/src/com/cloud/utils/net/NetUtils.java @@ -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) {