Author: jelmer Date: 2007-10-24 12:59:09 +0000 (Wed, 24 Oct 2007) New Revision: 25717
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=25717 Log: Fix ldif parsing. Modified: branches/4.0-python/ branches/4.0-python/source/lib/ldb/swig/ldb.i branches/4.0-python/source/lib/ldb/tests/python/api.py branches/4.0-python/source/scripting/python/samba/provision.py Changeset: Property changes on: branches/4.0-python ___________________________________________________________________ Name: bzr:revision-info ...skipped... Name: bzr:revision-id:v3-trunk0 ...skipped... Modified: branches/4.0-python/source/lib/ldb/swig/ldb.i =================================================================== --- branches/4.0-python/source/lib/ldb/swig/ldb.i 2007-10-24 12:59:04 UTC (rev 25716) +++ branches/4.0-python/source/lib/ldb/swig/ldb.i 2007-10-24 12:59:09 UTC (rev 25717) @@ -368,14 +368,6 @@ SWIG_NewPointerObj(ldif->msg, SWIGTYPE_p_ldb_message, 0)); } } - - - static PyObject *next_ldif_fragment(PyObject *self, PyObject *args) - { - struct ldb_context *ldb = PyCObject_AsVoidPtr(PyTuple_GetItem(self, 0)); - const char **s = PyCObject_AsVoidPtr(PyTuple_GetItem(self, 1)); - return ldb_ldif_to_pyobject(ldb_ldif_read_string(ldb, s)); - } } /* @@ -449,20 +441,12 @@ PyObject *parse_ldif(const char *s) { - PyObject *selfobj, *callable; - PyMethodDef md = { - "next_ldif_fragment", - next_ldif_fragment, - METH_NOARGS, - NULL - }; - - selfobj = Py_BuildValue("(OO)", - PyCObject_FromVoidPtr($self, NULL), - PyCObject_FromVoidPtr(&s, NULL)); - callable = PyCFunction_New(&md, selfobj); - - return PyCallIter_New(callable, Py_None); + PyObject *list = PyList_New(0); + struct ldb_ldif *ldif; + while ((ldif = ldb_ldif_read_string($self, &s)) != NULL) { + PyList_Append(list, ldb_ldif_to_pyobject(ldif)); + } + return PyObject_GetIter(list); } #endif } Modified: branches/4.0-python/source/lib/ldb/tests/python/api.py =================================================================== --- branches/4.0-python/source/lib/ldb/tests/python/api.py 2007-10-24 12:59:04 UTC (rev 25716) +++ branches/4.0-python/source/lib/ldb/tests/python/api.py 2007-10-24 12:59:09 UTC (rev 25717) @@ -239,6 +239,13 @@ self.assertEquals("foo=bar", str(msg[1].dn)) self.assertTrue(isinstance(msg[1], ldb.Message)) + def test_parse_ldif_more(self): + msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar") + msg = msgs.next() + self.assertEquals("foo=bar", str(msg[1].dn)) + msg = msgs.next() + self.assertEquals("bar=bar", str(msg[1].dn)) + def test_canonical_string(self): x = ldb.Dn(self.ldb, "dc=foo,bar=bloe") self.assertEquals("/bloe/foo", x.canonical_str()) Modified: branches/4.0-python/source/scripting/python/samba/provision.py =================================================================== --- branches/4.0-python/source/scripting/python/samba/provision.py 2007-10-24 12:59:04 UTC (rev 25716) +++ branches/4.0-python/source/scripting/python/samba/provision.py 2007-10-24 12:59:09 UTC (rev 25717) @@ -156,16 +156,16 @@ """ % (sid, subobj.domaindn, desc) # deliberately ignore errors from this, as the records may # already exist - ldb.add(add) + for msg in ldb.parse_ldif(add): + ldb.add(msg[1]) # # setup a mapping between a sam name and a unix name # def setup_name_mapping(subobj, ldb, sid, unixname): - res = ldb.search(subobj.domaindn, SCOPE_SUBTREE, "objectSid=%s" % sid, - ["dn"]) - if len(res) != 1: - raise Error("Failed to find record for objectSid %s\n" % sid) + res = ldb.search(Dn(ldb, subobj.domaindn), SCOPE_SUBTREE, + "objectSid=%s" % sid, ["dn"]) + assert len(res) == 1, "Failed to find record for objectSid %s" % sid mod = """ dn: %s @@ -173,7 +173,7 @@ replace: unixName unixName: %s """ % (res[0].dn, unixname) - ldb.modify(mod) + ldb.modify(ldb.parse_ldif(mod).next()[1]) # # return first host IP @@ -194,7 +194,6 @@ print "Deleting %s\n" % ldb.filename os.unlink(ldb.filename) ldb.transaction_cancel() - ldb.close() ldb.connect(ldb.filename) ldb.transaction_start() @@ -287,15 +286,18 @@ data = open(src, 'r').read() data = substitute_var(data, subobj.subst_vars()) - add_res = ldb.add(ldb.parse_ldif(data)) + for msg in ldb.parse_ldif(data): + ldb.add(msg[1]) + def setup_modify_ldif(setup_dir, ldif, subobj, ldb): src = os.path.join(setup_dir, ldif) data = open(src, 'r').read() data = substitute_var(data, subobj.subst_vars()) - return ldb.modify(data) + for (changetype, msg) in ldb.parse_ldif(data): + return ldb.modify(msg) def setup_ldb(setup_dir, ldif, session_info, credentials, subobj, dbname, erase=True): @@ -322,7 +324,8 @@ data = open(src, 'r').read() data = substitute_var(data, subobj.subst_vars()) - return ldb.modify(data) + for (changetype, msg) in ldb.parse_ldif(data): + ldb.modify(msg) # # setup a file in the private dir @@ -338,6 +341,7 @@ open(f, 'w').write(data) + def provision_default_paths(lp, subobj): paths = ProvisionPaths() private_dir = lp.get("private dir") @@ -356,22 +360,23 @@ # setup reasonable name mappings for sam names to unix names # def setup_name_mappings(subobj, ldb): - res = ldb.search(subobj.domaindn, SCOPE_BASE, "objectSid=*", + res = ldb.search(Dn(ldb, subobj.domaindn), SCOPE_BASE, "objectSid=*", ["objectSid"]) - assert(len(res) == 1 and res[0].objectSid is not None) - sid = res[0]["objectSid"] + assert len(res) == 1 + assert "objectSid" in res[0] + sid = list(res[0]["objectSid"])[0] # add some foreign sids if they are not present already - add_foreign(ldb, subobj, "S-1-5-7", "Anonymous") - add_foreign(ldb, subobj, "S-1-1-0", "World") - add_foreign(ldb, subobj, "S-1-5-2", "Network") + add_foreign(ldb, subobj, "S-1-5-7", "Anonymous") + add_foreign(ldb, subobj, "S-1-1-0", "World") + add_foreign(ldb, subobj, "S-1-5-2", "Network") add_foreign(ldb, subobj, "S-1-5-18", "System") add_foreign(ldb, subobj, "S-1-5-11", "Authenticated Users") # some well known sids - setup_name_mapping(subobj, ldb, "S-1-5-7", subobj.nobody) - setup_name_mapping(subobj, ldb, "S-1-1-0", subobj.nogroup) - setup_name_mapping(subobj, ldb, "S-1-5-2", subobj.nogroup) + setup_name_mapping(subobj, ldb, "S-1-5-7", subobj.nobody) + setup_name_mapping(subobj, ldb, "S-1-1-0", subobj.nogroup) + setup_name_mapping(subobj, ldb, "S-1-5-2", subobj.nogroup) setup_name_mapping(subobj, ldb, "S-1-5-18", subobj.root) setup_name_mapping(subobj, ldb, "S-1-5-11", subobj.users) setup_name_mapping(subobj, ldb, "S-1-5-32-544", subobj.wheel) @@ -388,7 +393,8 @@ setup_name_mapping(subobj, ldb, sid + "-520", subobj.wheel) -def provision_become_dc(setup_dir, subobj, message, paths, session_info, credentials): +def provision_become_dc(setup_dir, subobj, message, paths, session_info, + credentials): subobj.fix(paths) # Also wipes the database @@ -420,14 +426,17 @@ samdb.transaction_commit() message("Setting up %s" % paths.secrets) - setup_ldb(setup_dir, "secrets_init.ldif", session_info, credentials, subobj, paths.secrets) + setup_ldb(setup_dir, "secrets_init.ldif", session_info, credentials, + subobj, paths.secrets) - setup_ldb(setup_dir, "secrets.ldif", session_info, credentials, subobj, paths.secrets, False) + setup_ldb(setup_dir, "secrets.ldif", session_info, credentials, subobj, + paths.secrets, False) # # provision samba4 - caution, this wipes all existing data! # -def provision(lp, setup_dir, subobj, message, blank, paths, session_info, credentials, ldapbackend): +def provision(lp, setup_dir, subobj, message, blank, paths, session_info, + credentials, ldapbackend): subobj.fix(paths) if subobj.domain_guid is not None: @@ -507,7 +516,6 @@ # (hack) Reload, now we have the schema loaded. samdb.transaction_commit() - samdb.close() samdb = open_ldb(session_info, credentials, paths.samdb) samdb.transaction_start() @@ -566,7 +574,7 @@ setup_name_mappings(subobj, samdb) message("Setting up sam.ldb index") - setup_add_ldif(setup_dir, "provision_index.ldif", info, samdb) + setup_add_ldif(setup_dir, "provision_index.ldif", subobj, samdb) message("Setting up sam.ldb rootDSE marking as syncronized") setup_modify_ldif(setup_dir, "provision_rootdse_modify.ldif", subobj, samdb) @@ -585,13 +593,13 @@ # These values may have changed, due to an incoming SamSync, # or may not have been specified, so fetch them from the database - res = ldb.search(subobj.domaindn, SCOPE_BASE, "objectGUID=*", + res = ldb.search(Dn(ldb, subobj.domaindn), SCOPE_BASE, "objectGUID=*", ["objectGUID"]) assert(len(res) == 1) assert(res[0]["objectGUID"] is not None) subobj.domainguid = res[0]["objectGUID"] - subobj.host_guid = searchone(ldb, subobj.domaindn, "(&(objectClass=computer)(cn=" + subobj.netbiosname + "))", "objectGUID") + subobj.host_guid = searchone(ldb, subobj.domaindn, "(&(objectClass=computer)(cn=%s))" % subobj.netbiosname, "objectGUID") assert subobj.host_guid is not None setup_file(setup_dir, "provision.zone", message, paths.dns, subobj)