provision: Share more code for determine_netbios_name() with samba.valid_netbios_name().
authorJelmer Vernooij <jelmer@samba.org>
Sun, 26 Feb 2012 19:51:04 +0000 (20:51 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 26 Feb 2012 21:24:42 +0000 (22:24 +0100)
Autobuild-User: Jelmer Vernooij <jelmer@samba.org>
Autobuild-Date: Sun Feb 26 22:24:42 CET 2012 on sn-devel-104

source4/scripting/python/samba/__init__.py
source4/scripting/python/samba/provision/__init__.py

index 20e6e709200636ef1737a649f4150178fc4a42ae..f6af472f83b59abfd3bcf7658995911698c6d971 100644 (file)
@@ -300,16 +300,16 @@ def setup_file(template, fname, subst_vars=None):
     finally:
         f.close()
 
+MAX_NETBIOS_NAME_LEN = 15
+def is_valid_netbios_char(c):
+    return (c.isalnum() or c in " !#$%&'()-.@^_{}~")
 
 def valid_netbios_name(name):
     """Check whether a name is valid as a NetBIOS name. """
     # See crh's book (1.4.1.1)
-    if len(name) > 15:
+    if len(name) > MAX_NETBIOS_NAME_LEN:
         return False
-    for x in name:
-        if not x.isalnum() and not x in " !#$%&'()-.@^_{}~":
-            return False
-    return True
+    return all([is_valid_netbios_char(x) for x in name])
 
 
 def import_bundled_package(modulename, location):
index bff3ce6bf186534c1315f5a24e0bf496484dbc59..3a69a07177e267c7c6f69d579e028a0a9114ff52 100644 (file)
@@ -46,7 +46,9 @@ import samba
 from samba.dsdb import DS_DOMAIN_FUNCTION_2000
 from samba import (
     Ldb,
+    MAX_NETBIOS_NAME_LEN,
     check_all_substituted,
+    is_valid_netbios_char,
     setup_file,
     substitute_var,
     valid_netbios_name,
@@ -93,7 +95,6 @@ from samba.samdb import SamDB
 from samba.dbchecker import dbcheck
 
 
-VALID_NETBIOS_CHARS = " !#$%&'()-.@^_{}~"
 DEFAULT_POLICY_GUID = "31B2F340-016D-11D2-945F-00C04FB984F9"
 DEFAULT_DC_POLICY_GUID = "6AC1786C-016F-11D2-945F-00C04fB984F9"
 DEFAULTSITE = "Default-First-Site-Name"
@@ -482,14 +483,9 @@ def provision_paths_from_lp(lp, dnsdomain):
 
 def determine_netbios_name(hostname):
     """Determine a netbios name from a hostname."""
-    netbiosname = hostname
-    # remove forbidden chars
-    newnbname = ""
-    for x in netbiosname:
-        if x.isalnum() or x in VALID_NETBIOS_CHARS:
-            newnbname = "%s%c" % (newnbname, x)
-    # force the length to be <16
-    return newnbname[0:15].upper()
+    # remove forbidden chars and force the length to be <16
+    netbiosname = "".join([x for x in hostname if is_valid_netbios_char(x)])
+    return netbiosname[:MAX_NETBIOS_NAME_LEN].upper()
 
 
 def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None,