samdb: Add samdb.domain_netbios_name()
authorAndrew Bartlett <abartlet@samba.org>
Tue, 10 Nov 2020 00:46:28 +0000 (13:46 +1300)
committerGary Lockyer <gary@samba.org>
Wed, 11 Nov 2020 01:15:39 +0000 (01:15 +0000)
Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
python/samba/netcmd/user.py
python/samba/samdb.py
python/samba/tests/samdb.py
selftest/tests.py

index b483dcf55916229f53972f216098676c13f7cfbe..7e8204462d1d27d98715c90f5537c1aaefaada36 100644 (file)
@@ -3210,14 +3210,8 @@ The users gecos field will be set to 'User4 test'
 
         if unix_home is None:
             # obtain nETBIOS Domain Name
-            filter = "(&(objectClass=crossRef)(nETBIOSName=*))"
-            searchdn = ("CN=Partitions,CN=Configuration," + domaindn)
-            try:
-                res = samdb.search(searchdn,
-                                   scope=ldb.SCOPE_SUBTREE,
-                                   expression=filter)
-                unix_domain = res[0]["nETBIOSName"][0].decode()
-            except IndexError:
+            unix_domain = samdb.domain_netbios_name()
+            if unix_domain is None:
                 raise CommandError('Unable to find Unix domain')
 
             tmpl = lp.get('template homedir')
index e8aee496352ba08533be304d821dc41393352678..a0a7dbf1c509ada46102ded63c92c0926be6c4ac 100644 (file)
@@ -998,6 +998,21 @@ accountExpires: %u
         domain_dn = self.get_default_basedn()
         return domain_dn.canonical_str().split('/')[0]
 
+    def domain_netbios_name(self):
+        """return the NetBIOS name of the domain root"""
+        domain_dn = self.get_default_basedn()
+        dns_name = self.domain_dns_name()
+        filter = "(&(objectClass=crossRef)(nETBIOSName=*)(ncName=%s)(dnsroot=%s))" % (domain_dn, dns_name)
+        partitions_dn = self.get_partitions_dn()
+        res = self.search(partitions_dn,
+                          scope=ldb.SCOPE_ONELEVEL,
+                          expression=filter)
+        try:
+            netbios_domain = res[0]["nETBIOSName"][0].decode()
+        except IndexError:
+            return None
+        return netbios_domain
+
     def forest_dns_name(self):
         """return the DNS name of the forest root"""
         forest_dn = self.get_root_basedn()
index a185a1566e3576c820b422e7af2a942ad537c1ba..834c5a204a6c1382d2ce7380a65f3d68d6ffaa4b 100644 (file)
@@ -38,13 +38,13 @@ class SamDBTestCase(TestCaseInTempDir):
         super(SamDBTestCase, self).setUp()
         self.session = system_session()
         logger = logging.getLogger("selftest")
-        domain = "dsdb"
-        realm = "dsdb.samba.example.com"
+        self.domain = "dsdb"
+        self.realm = "dsdb.samba.example.com"
         host_name = "test"
         server_role = "active directory domain controller"
         self.result = provision(logger,
                                 self.session, targetdir=self.tempdir,
-                                realm=realm, domain=domain,
+                                realm=self.realm, domain=self.domain,
                                 hostname=host_name,
                                 use_ntvfs=True,
                                 serverrole=server_role,
@@ -61,3 +61,10 @@ class SamDBTestCase(TestCaseInTempDir):
             shutil.rmtree(os.path.join(self.tempdir, d))
 
         super(SamDBTestCase, self).tearDown()
+
+
+class SamDBTests(SamDBTestCase):
+
+    def test_get_domain(self):
+        self.assertEqual(self.samdb.domain_dns_name(), self.realm.lower())
+        self.assertEqual(self.samdb.domain_netbios_name(), self.domain.upper())
index 4a968cdbe8a3b2fec914585b67fc25a927118e03..9772fe8f8f1acde6efe8e305664ef18b1ecfbb7d 100644 (file)
@@ -215,6 +215,7 @@ planpythontestsuite("none", "samba.tests.graph")
 plantestsuite("wafsamba.duplicate_symbols", "none", [os.path.join(srcdir(), "buildtools/wafsamba/test_duplicate_symbol.sh")])
 planpythontestsuite("none", "samba.tests.glue")
 planpythontestsuite("none", "samba.tests.tdb_util")
+planpythontestsuite("none", "samba.tests.samdb")
 planpythontestsuite("none", "samba.tests.samdb_api")
 
 if with_pam: