[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH 2/2] Clean up sanityCheckHostname() in network.py (#559626)



Hostnames can start with digits.  Removed inStrRange() since
it seemed a little redundant.  Left hostname length check at
64 characters, but put a comment reminding me that POSIX
sets this limit to 255, so we should examine this in the
future again.
---
 network.py |   25 ++++++++++++-------------
 1 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/network.py b/network.py
index 0327ef9..0feb8fd 100644
--- a/network.py
+++ b/network.py
@@ -37,26 +37,25 @@ class IPError(Exception):
 class IPMissing(Exception):
     pass
 
-def inStrRange(v, s):
-    if string.find(s, v) == -1:
-	return 0
-    else:
-	return 1
-
 def sanityCheckHostname(hostname):
     if len(hostname) < 1:
 	return None
 
+    # XXX: POSIX says this limit is 255, but Linux also defines HOST_NAME_MAX
+    # as 64, so I don't know which we should believe.  --dcantrell
     if len(hostname) > 64:
-	return _("Hostname must be 64 or less characters in length.")
-    
-    if not inStrRange(hostname[0], string.ascii_letters):
-	return _("Hostname must start with a valid character in the range "
-		 "'a-z' or 'A-Z'")
+	return _("Hostname must be 64 or fewer characters in length.")
+
+    validStart = string.ascii_letters + string.digits
+    validAll = validStart + ".-"
+
+    if string.find(validStart, hostname[0]) == -1:
+	return _("Hostname must start with a valid character in the ranges "
+		 "'a-z', 'A-Z', or '0-9'")
 
     for i in range(1, len(hostname)):
-	if not inStrRange(hostname[i], string.ascii_letters+string.digits+".-"):
-	    return _("Hostnames can only contain the characters 'a-z', 'A-Z', '-', or '.'")
+	if string.find(validAll, hostname[i]) == -1:
+	    return _("Hostnames can only contain the characters 'a-z', 'A-Z', '0-9', '-', or '.'")
 
     return None
 	    
-- 
1.7.1.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]