[SCM] Samba Shared Repository - branch master updated

Jelmer Vernooij jelmer at samba.org
Sun Feb 26 14:25:04 MST 2012


The branch, master has been updated
       via  411119d provision: Share more code for determine_netbios_name() with samba.valid_netbios_name().
       via  d5485a1 provision/backend: Properly close all opened files.
       via  6b320d6 provision: Properly close opened files.
       via  171dc5a provision: Reuse determine_netbios_name.
       via  3b738a5 provision: Add tests for determine_netbios_name.
       via  16a4fa9 provision: Factor out determination of new netbios name.
      from  d09764e join: Don't print provision results when they're just noise.

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 411119db501b131c0632d852129c9f3819cb4190
Author: Jelmer Vernooij <jelmer at samba.org>
Date:   Sun Feb 26 20:51:04 2012 +0100

    provision: Share more code for determine_netbios_name() with samba.valid_netbios_name().
    
    Autobuild-User: Jelmer Vernooij <jelmer at samba.org>
    Autobuild-Date: Sun Feb 26 22:24:42 CET 2012 on sn-devel-104

commit d5485a1e5602ceac0ed37db665f5680c9a8ab43c
Author: Jelmer Vernooij <jelmer at samba.org>
Date:   Sun Feb 26 20:45:43 2012 +0100

    provision/backend: Properly close all opened files.

commit 6b320d63f3cc13de979739963e19a8d2a28ce135
Author: Jelmer Vernooij <jelmer at samba.org>
Date:   Sun Feb 26 20:42:41 2012 +0100

    provision: Properly close opened files.

commit 171dc5ad4f10bca0038653f770decc72b872220d
Author: Jelmer Vernooij <jelmer at samba.org>
Date:   Sun Feb 26 20:40:26 2012 +0100

    provision: Reuse determine_netbios_name.

commit 3b738a5914765afad2be30ba3f7db11170e87012
Author: Jelmer Vernooij <jelmer at samba.org>
Date:   Sun Feb 26 20:39:13 2012 +0100

    provision: Add tests for determine_netbios_name.

commit 16a4fa97046e0db460882c70a5c3c241daf55223
Author: Jelmer Vernooij <jelmer at samba.org>
Date:   Sun Feb 26 20:38:50 2012 +0100

    provision: Factor out determination of new netbios name.

-----------------------------------------------------------------------

Summary of changes:
 source4/scripting/python/samba/__init__.py         |   10 ++--
 .../scripting/python/samba/provision/__init__.py   |   48 ++++++++++---------
 .../scripting/python/samba/provision/backend.py    |   47 +++++++++++++++-----
 source4/scripting/python/samba/tests/provision.py  |    9 ++++
 4 files changed, 75 insertions(+), 39 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source4/scripting/python/samba/__init__.py b/source4/scripting/python/samba/__init__.py
index 20e6e70..f6af472 100644
--- a/source4/scripting/python/samba/__init__.py
+++ b/source4/scripting/python/samba/__init__.py
@@ -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):
diff --git a/source4/scripting/python/samba/provision/__init__.py b/source4/scripting/python/samba/provision/__init__.py
index 42da903..3a69a07 100644
--- a/source4/scripting/python/samba/provision/__init__.py
+++ b/source4/scripting/python/samba/provision/__init__.py
@@ -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"
@@ -480,6 +481,13 @@ def provision_paths_from_lp(lp, dnsdomain):
     return paths
 
 
+def determine_netbios_name(hostname):
+    """Determine a netbios name from a hostname."""
+    # 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,
                 serverrole=None, rootdn=None, domaindn=None, configdn=None,
                 schemadn=None, serverdn=None, sitename=None):
@@ -490,14 +498,7 @@ def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None,
 
     netbiosname = lp.get("netbios name")
     if netbiosname is None:
-        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
-        netbiosname = newnbname[0:15]
+        netbiosname = determine_netbios_name(hostname)
     assert netbiosname is not None
     netbiosname = netbiosname.upper()
     if not valid_netbios_name(netbiosname):
@@ -594,14 +595,7 @@ def make_smbconf(smbconf, hostname, domain, realm, serverrole,
     assert smbconf is not None
     if hostname is None:
         hostname = socket.gethostname().split(".")[0]
-        netbiosname = hostname.upper()
-        # 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
-        netbiosname = newnbname[0:15]
+        netbiosname = determine_netbios_name(hostname)
     else:
         netbiosname = hostname.upper()
 
@@ -682,8 +676,10 @@ def make_smbconf(smbconf, hostname, domain, realm, serverrole,
     # this ensures that any smb.conf parameters that were set
     # on the provision/join command line are set in the resulting smb.conf
     f = open(smbconf, mode='w')
-    lp.dump(f, False)
-    f.close()
+    try:
+        lp.dump(f, False)
+    finally:
+        f.close()
 
 
 def setup_name_mappings(idmap, sid, root_uid, nobody_uid,
@@ -1071,8 +1067,11 @@ def getpolicypath(sysvolpath, dnsdomain, guid):
 def create_gpo_struct(policy_path):
     if not os.path.exists(policy_path):
         os.makedirs(policy_path, 0775)
-    open(os.path.join(policy_path, "GPT.INI"), 'w').write(
-                      "[General]\r\nVersion=0")
+    f = open(os.path.join(policy_path, "GPT.INI"), 'w')
+    try:
+        f.write("[General]\r\nVersion=0")
+    finally:
+        f.close()
     p = os.path.join(policy_path, "MACHINE")
     if not os.path.exists(p):
         os.makedirs(p, 0775)
@@ -1653,8 +1652,11 @@ def provision(logger, session_info, credentials, smbconf=None,
         # if Samba Team members can't figure out the weird errors
         # loading an empty smb.conf gives, then we need to be smarter.
         # Pretend it just didn't exist --abartlet
-        data = open(smbconf, 'r').read()
-        data = data.lstrip()
+        f = open(smbconf, 'r')
+        try:
+            data = f.read().lstrip()
+        finally:
+            f.close()
         if data is None or data == "":
             make_smbconf(smbconf, hostname, domain, realm,
                          serverrole, targetdir, sid_generator, useeadb,
diff --git a/source4/scripting/python/samba/provision/backend.py b/source4/scripting/python/samba/provision/backend.py
index 1931bc1..dc127ed 100644
--- a/source4/scripting/python/samba/provision/backend.py
+++ b/source4/scripting/python/samba/provision/backend.py
@@ -217,9 +217,11 @@ class LDAPBackend(ProvisionBackend):
                 if err != errno.ENOENT:
                     raise
             else:
-                p = f.read()
-                f.close()
-                self.logger.info("Check for slapd Process with PID: %s and terminate it manually." % p)
+                try:
+                    p = f.read()
+                finally:
+                    f.close()
+                self.logger.info("Check for slapd process with PID: %s and terminate it manually." % p)
             raise SlapdAlreadyRunning(self.ldap_uri)
         except LdbError:
             # XXX: We should never be catching all Ldb errors
@@ -535,8 +537,11 @@ class OpenLDAPBackend(LDAPBackend):
         backend_schema = "backend-schema.schema"
 
         f = open(setup_path(mapping), 'r')
-        backend_schema_data = self.schema.convert_to_openldap(
-                "openldap", f.read())
+        try:
+            backend_schema_data = self.schema.convert_to_openldap(
+                    "openldap", f.read())
+        finally:
+            f.close()
         assert backend_schema_data is not None
         f = open(os.path.join(self.ldapdir, backend_schema), 'w')
         try:
@@ -699,7 +704,11 @@ class FDSBackend(LDAPBackend):
 
         lnkattr = self.schema.linked_attributes()
 
-        refint_config = open(setup_path("fedorads-refint-delete.ldif"), 'r').read()
+        f = open(setup_path("fedorads-refint-delete.ldif"), 'r')
+        try:
+            refint_config = f.read()
+        finally:
+            f.close()
         memberof_config = ""
         index_config = ""
         argnum = 3
@@ -718,8 +727,16 @@ class FDSBackend(LDAPBackend):
                     setup_path("fedorads-index.ldif"), { "ATTR" : attr })
                 argnum += 1
 
-        open(self.refint_ldif, 'w').write(refint_config)
-        open(self.linked_attrs_ldif, 'w').write(memberof_config)
+        f = open(self.refint_ldif, 'w')
+        try:
+            f.write(refint_config)
+        finally:
+            f.close()
+        f = open(self.linked_attrs_ldif, 'w')
+        try:
+            f.write(memberof_config)
+        finally:
+            f.close()
 
         attrs = ["lDAPDisplayName"]
         res = self.schema.ldb.search(expression="(&(objectclass=attributeSchema)(searchFlags:1.2.840.113556.1.4.803:=1))", base=self.names.schemadn, scope=SCOPE_ONELEVEL, attrs=attrs)
@@ -733,7 +750,11 @@ class FDSBackend(LDAPBackend):
             index_config += read_and_sub_file(
                 setup_path("fedorads-index.ldif"), { "ATTR" : attr })
 
-        open(self.index_ldif, 'w').write(index_config)
+        f = open(self.index_ldif, 'w')
+        try:
+            f.write(index_config)
+        finally:
+            f.close()
 
         setup_file(setup_path("fedorads-samba.ldif"), self.samba_ldif, {
             "SAMBADN": self.sambadn,
@@ -744,8 +765,12 @@ class FDSBackend(LDAPBackend):
         backend_schema = "99_ad.ldif"
 
         # Build a schema file in Fedora DS format
-        backend_schema_data = self.schema.convert_to_openldap("fedora-ds",
-            open(setup_path(mapping), 'r').read())
+        f = open(setup_path(mapping), 'r')
+        try:
+            backend_schema_data = self.schema.convert_to_openldap("fedora-ds",
+                f.read())
+        finally:
+            f.close()
         assert backend_schema_data is not None
         f = open(os.path.join(self.ldapdir, backend_schema), 'w')
         try:
diff --git a/source4/scripting/python/samba/tests/provision.py b/source4/scripting/python/samba/tests/provision.py
index 0ebe400..a96efdf 100644
--- a/source4/scripting/python/samba/tests/provision.py
+++ b/source4/scripting/python/samba/tests/provision.py
@@ -24,6 +24,7 @@ from samba.provision import (
     ProvisionNames,
     ProvisionPaths,
     ProvisionResult,
+    determine_netbios_name,
     sanitize_server_role,
     setup_secretsdb,
     findnss,
@@ -190,3 +191,11 @@ class ProvisionResultTests(TestCase):
         self.assertEquals(entries[1],
                 ("INFO", 'Admin password:        geheim'))
 
+
+class DetermineNetbiosNameTests(TestCase):
+
+    def test_limits_to_15(self):
+        self.assertEquals("A" * 15, determine_netbios_name("a" * 30))
+
+    def test_strips_invalid(self):
+        self.assertEquals("BLABLA", determine_netbios_name("bla/bla"))


-- 
Samba Shared Repository


More information about the samba-cvs mailing list