def test_ldif_to_samdb_forced_local_dsa(self): for dsa, site in MULTISITE_LDIF_DSAS: dburl = os.path.join(self.tempdir, "ldif-to-samba-forced-local-dsa" "-%s" % dsa) samdb = ldif_import_export.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF, forced_local_dsa=dsa) self.assertIsInstance(samdb, SamDB) self.assertEqual(samdb.server_site_name(), site) res = samdb.search(ldb.Dn(samdb, "CN=NTDS Settings," + dsa), scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) ntds_guid = misc.GUID(samdb.get_ntds_GUID()) self.assertEqual(misc.GUID(res[0]["objectGUID"][0]), ntds_guid) service_name_res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(samdb, service_name_res[0]["dsServiceName"][0].decode('utf8')) self.assertEqual(dn, ldb.Dn(samdb, "CN=NTDS Settings," + dsa)) self.remove_files(dburl)
def test_samdb_to_ldif_file(self): dburl = os.path.join(self.tempdir, "ldap") dburl2 = os.path.join(self.tempdir, "ldap_roundtrip") ldif_file = os.path.join(self.tempdir, "ldif") samdb = ldif_import_export.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF) self.assertIsInstance(samdb, SamDB) ldif_import_export.samdb_to_ldif_file(samdb, dburl, lp=self.lp, creds=None, ldif_file=ldif_file) self.assertGreater(os.path.getsize(ldif_file), 1000, "LDIF should be larger than 1000 bytes") samdb = ldif_import_export.ldif_to_samdb(dburl2, self.lp, ldif_file) self.assertIsInstance(samdb, SamDB) dsa = ( "CN=WIN01,CN=Servers,CN=Default-First-Site-Name,CN=Sites," "CN=Configuration,DC=ad,DC=samba,DC=example,DC=com" ) res = samdb.search(ldb.Dn(samdb, "CN=NTDS Settings," + dsa), scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) self.remove_files(dburl) self.remove_files(dburl2) self.remove_files(ldif_file)
def samdb_from_ldif(ldif, tempdir, lp, dsa=None, tag=''): if dsa is None: dsa_name = 'default-DSA' else: dsa_name = dsa[:5] dburl = os.path.join(tempdir, ("ldif-to-sambdb-%s-%s" % (tag, dsa_name))) samdb = ldif_import_export.ldif_to_samdb(dburl, lp, ldif, forced_local_dsa=dsa) return (samdb, dburl)
def test_samdb_to_ldif_file(self): dburl = os.path.join(self.tempdir, "ldap") dburl2 = os.path.join(self.tempdir, "ldap_roundtrip") ldif_file = os.path.join(self.tempdir, "ldif") samdb = ldif_import_export.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF) self.assertIsInstance(samdb, SamDB) ldif_import_export.samdb_to_ldif_file(samdb, dburl, lp=self.lp, creds=None, ldif_file=ldif_file) self.assertGreater(os.path.getsize(ldif_file), 1000, "LDIF should be larger than 1000 bytes") samdb = ldif_import_export.ldif_to_samdb(dburl2, self.lp, ldif_file) self.assertIsInstance(samdb, SamDB) dsa = ("CN=WIN01,CN=Servers,CN=Default-First-Site-Name,CN=Sites," "CN=Configuration,DC=ad,DC=samba,DC=example,DC=com") res = samdb.search(ldb.Dn(samdb, "CN=NTDS Settings," + dsa), scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) self.remove_files(dburl) self.remove_files(dburl2) self.remove_files(ldif_file)
def test_ldif_to_samdb_forced_local_dsa(self): for dsa, site in MULTISITE_LDIF_DSAS: dburl = os.path.join(self.tempdir, "ldif-to-samba-forced-local-dsa" "-%s" % dsa) samdb = ldif_import_export.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF, forced_local_dsa=dsa) self.assertIsInstance(samdb, SamDB) self.assertEqual(samdb.server_site_name(), site) res = samdb.search(ldb.Dn(samdb, "CN=NTDS Settings," + dsa), scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) ntds_guid = misc.GUID(samdb.get_ntds_GUID()) self.assertEqual(misc.GUID(res[0]["objectGUID"][0]), ntds_guid) service_name_res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(samdb, service_name_res[0]["dsServiceName"][0]) self.assertEqual(dn, ldb.Dn(samdb, "CN=NTDS Settings," + dsa)) self.remove_files(dburl)
def test_ldif_to_samdb(self): dburl = os.path.join(self.tempdir, "ldap") samdb = ldif_import_export.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF) self.assertIsInstance(samdb, SamDB) dsa = ( "CN=WIN01,CN=Servers,CN=Default-First-Site-Name,CN=Sites," "CN=Configuration,DC=ad,DC=samba,DC=example,DC=com" ) res = samdb.search(ldb.Dn(samdb, "CN=NTDS Settings," + dsa), scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) ntds_guid = misc.GUID(samdb.get_ntds_GUID()) self.assertEqual(misc.GUID(res[0]["objectGUID"][0]), ntds_guid) service_name_res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(samdb, service_name_res[0]["dsServiceName"][0]) self.assertEqual(dn, ldb.Dn(samdb, "CN=NTDS Settings," + dsa)) self.remove_files(dburl)
def test_ldif_to_samdb(self): dburl = os.path.join(self.tempdir, "ldap") samdb = ldif_import_export.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF) self.assertIsInstance(samdb, SamDB) dsa = ("CN=WIN01,CN=Servers,CN=Default-First-Site-Name,CN=Sites," "CN=Configuration,DC=ad,DC=samba,DC=example,DC=com") res = samdb.search(ldb.Dn(samdb, "CN=NTDS Settings," + dsa), scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) ntds_guid = misc.GUID(samdb.get_ntds_GUID()) self.assertEqual(misc.GUID(res[0]["objectGUID"][0]), ntds_guid) service_name_res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(samdb, service_name_res[0]["dsServiceName"][0]) self.assertEqual(dn, ldb.Dn(samdb, "CN=NTDS Settings," + dsa)) self.remove_files(dburl)
def import_ldif_db(self, ldif, lp): d = tempfile.mkdtemp(prefix='samba-tool-visualise') fn = os.path.join(d, 'imported.ldb') self._tmp_fn_to_delete = fn samdb = ldif_import_export.ldif_to_samdb(fn, lp, ldif) return fn