From b87632fac63ad3b615ad64a5e89dfa7fa19f83e6 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 12 Apr 2018 14:46:59 +0100 Subject: [PATCH 01/14] lib/ldb: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- lib/ldb/pyldb.c | 10 +++++----- lib/ldb/pyldb_util.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index 67ecafbc420..110ec8e60ad 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -1078,7 +1078,7 @@ static const char **PyList_AsStrList(TALLOC_CTX *mem_ctx, PyObject *list, const char *str = NULL; Py_ssize_t size; PyObject *item = PyList_GetItem(list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); talloc_free(ret); return NULL; @@ -2861,7 +2861,7 @@ static struct ldb_message_element *PyObject_AsMessageElement( me->name = talloc_strdup(me, attr_name); me->flags = flags; - if (PyBytes_Check(set_obj) || PyStr_Check(set_obj)) { + if (PyBytes_Check(set_obj) || PyUnicode_Check(set_obj)) { me->num_values = 1; me->values = talloc_array(me, struct ldb_val, me->num_values); if (PyBytes_Check(set_obj)) { @@ -2897,7 +2897,7 @@ static struct ldb_message_element *PyObject_AsMessageElement( return NULL; } msg = _msg; - } else if (PyStr_Check(obj)) { + } else if (PyUnicode_Check(obj)) { msg = PyStr_AsUTF8AndSize(obj, &size); if (msg == NULL) { talloc_free(me); @@ -3069,7 +3069,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb if (py_elements != NULL) { Py_ssize_t i; - if (PyBytes_Check(py_elements) || PyStr_Check(py_elements)) { + if (PyBytes_Check(py_elements) || PyUnicode_Check(py_elements)) { char *_msg = NULL; el->num_values = 1; el->values = talloc_array(el, struct ldb_val, 1); @@ -3110,7 +3110,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb char *_msg = NULL; result = PyBytes_AsStringAndSize(item, &_msg, &size); msg = _msg; - } else if (PyStr_Check(item)) { + } else if (PyUnicode_Check(item)) { msg = PyStr_AsUTF8AndSize(item, &size); result = (msg == NULL) ? -1 : 0; } else { diff --git a/lib/ldb/pyldb_util.c b/lib/ldb/pyldb_util.c index 3bda1dbc2a8..46ee4034122 100644 --- a/lib/ldb/pyldb_util.c +++ b/lib/ldb/pyldb_util.c @@ -70,7 +70,7 @@ bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, struct ldb_dn *odn; PyTypeObject *PyLdb_Dn_Type; - if (ldb_ctx != NULL && PyStr_Check(object)) { + if (ldb_ctx != NULL && (PyStr_Check(object) || PyUnicode_Check(object))) { odn = ldb_dn_new(mem_ctx, ldb_ctx, PyStr_AsUTF8(object)); *dn = odn; return true; From 08fe5590f190cf444056d322b1b1e45119bd93c2 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:17:20 +0100 Subject: [PATCH 02/14] lib/tevent: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- lib/tevent/pytevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tevent/pytevent.c b/lib/tevent/pytevent.c index 10d8a22a8cf..369ec6e02c8 100644 --- a/lib/tevent/pytevent.c +++ b/lib/tevent/pytevent.c @@ -188,7 +188,7 @@ static PyObject *py_register_backend(PyObject *self, PyObject *args) return NULL; } - if (!PyStr_Check(name)) { + if (!(PyStr_Check(name) || PyUnicode_Check(name))) { PyErr_SetNone(PyExc_TypeError); Py_DECREF(name); return NULL; From 0cf4bdf13493b3d07c976c0cf9a31fafeb497e60 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:32:15 +0100 Subject: [PATCH 03/14] libcli/nbt: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- libcli/nbt/pynbt.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libcli/nbt/pynbt.c b/libcli/nbt/pynbt.c index 254c98a4b89..032561a4bd8 100644 --- a/libcli/nbt/pynbt.c +++ b/libcli/nbt/pynbt.c @@ -57,7 +57,7 @@ static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject * static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, uint16_t *dest_port) { - if (PyStr_Check(obj)) { + if (PyStr_Check(obj) || PyUnicode_Check(obj)) { *dest_addr = PyStr_AsString(obj); *dest_port = NBT_NAME_SERVICE_PORT; return true; @@ -69,7 +69,7 @@ static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, u return false; } - if (!PyStr_Check(PyTuple_GetItem(obj, 0))) { + if (!(PyStr_Check(PyTuple_GetItem(obj, 0)) || PyUnicode_Check(PyTuple_GetItem(obj, 0)))) { PyErr_SetString(PyExc_TypeError, "Destination tuple first element not string"); return false; } @@ -111,7 +111,7 @@ static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socke } } - if (PyStr_Check(obj)) { + if (PyStr_Check(obj) || PyUnicode_Check(obj)) { /* FIXME: Parse string to be able to interpret things like RHONWYN<02> ? */ name->name = PyStr_AsString(obj); name->scope = NULL; From 4517cd7b8de013ffaea37d7eb73dfb332e8c6b72 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:33:10 +0100 Subject: [PATCH 04/14] s4/auth: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/auth/pyauth.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source4/auth/pyauth.c b/source4/auth/pyauth.c index 4cb12f882bc..fc22637564d 100644 --- a/source4/auth/pyauth.c +++ b/source4/auth/pyauth.c @@ -184,7 +184,7 @@ static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, const char *value; Py_ssize_t size; PyObject *item = PyList_GetItem(list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); return NULL; } From c4fbae86ef880e6eff5015a3e846686a530685cc Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:33:47 +0100 Subject: [PATCH 05/14] s4/dsdb: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/dsdb/pydsdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source4/dsdb/pydsdb.c b/source4/dsdb/pydsdb.c index 8d84a16dd18..d9177bbd1d7 100644 --- a/source4/dsdb/pydsdb.c +++ b/source4/dsdb/pydsdb.c @@ -580,7 +580,7 @@ static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args) for (i = 0; i < el->num_values; i++) { PyObject *item = PyList_GetItem(el_list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "ldif_elements should be strings"); talloc_free(tmp_ctx); return NULL; From defe82e5b81996ec2c99a4faf0904580d95a9851 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:34:19 +0100 Subject: [PATCH 06/14] s4/librpc: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/librpc/rpc/pyrpc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index 8b817b8b46d..e86ea0e94aa 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -50,28 +50,32 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy { ZERO_STRUCTP(syntax_id); - if (PyStr_Check(object)) { + if (PyStr_Check(object) || PyUnicode_Check(object)) { return PyString_AsGUID(object, &syntax_id->uuid); } else if (PyTuple_Check(object)) { + PyObject *item = NULL; if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) { PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size"); return false; } - if (!PyStr_Check(PyTuple_GetItem(object, 0))) { + item = PyTuple_GetItem(object, 0); + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple"); return false; } - if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) + if (!PyString_AsGUID(item, &syntax_id->uuid)) { return false; + } - if (!PyInt_Check(PyTuple_GetItem(object, 1))) { + item = PyTuple_GetItem(object, 1); + if (!PyInt_Check(item)) { PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple"); return false; } - syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1)); + syntax_id->if_version = PyInt_AsLong(item); return true; } From 813331aefeee5c1e4d214551939f3d5a40d8811d Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 17:34:40 +0100 Subject: [PATCH 07/14] s4/param: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power --- source4/param/pyparam.c | 2 +- source4/param/pyparam_util.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source4/param/pyparam.c b/source4/param/pyparam.c index 18d7017b9c6..e7e908fcac3 100644 --- a/source4/param/pyparam.c +++ b/source4/param/pyparam.c @@ -456,7 +456,7 @@ static Py_ssize_t py_lp_ctx_len(PyObject *self) static PyObject *py_lp_ctx_getitem(PyObject *self, PyObject *name) { struct loadparm_service *service; - if (!PyStr_Check(name)) { + if (!(PyStr_Check(name) || PyUnicode_Check(name))) { PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported"); return NULL; } diff --git a/source4/param/pyparam_util.c b/source4/param/pyparam_util.c index 512a8b1cdb7..917caf809e5 100644 --- a/source4/param/pyparam_util.c +++ b/source4/param/pyparam_util.c @@ -34,7 +34,7 @@ _PUBLIC_ struct loadparm_context *lpcfg_from_py_object(TALLOC_CTX *mem_ctx, PyOb PyTypeObject *lp_type; bool is_lpobj; - if (PyStr_Check(py_obj)) { + if (PyStr_Check(py_obj) || PyUnicode_Check(py_obj)) { lp_ctx = loadparm_init_global(false); if (lp_ctx == NULL) { return NULL; From 793b4084ec3da24b44cb962f17ded6ef0c595b6e Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 13 Apr 2018 11:19:10 +0100 Subject: [PATCH 08/14] python/samba: Add some compatability PY2/PY3 functions I hope these changes are a short term interim solution for the absence of the 'six' module/library. I also hope that soon this module can be removed and be replaced by usage of six. Signed-off-by: Noel Power --- python/samba/compat.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python/samba/compat.py b/python/samba/compat.py index 667a1a443b1..d17dc3c2ce9 100644 --- a/python/samba/compat.py +++ b/python/samba/compat.py @@ -22,8 +22,20 @@ PY3 = sys.version_info[0] == 3 if PY3: + # compat functions + from urllib.parse import quote as urllib_quote + from urllib.request import urlopen as urllib_urlopen + + # compat types integer_types = int, + string_types = str text_type = str else: + # compat functions + from urllib import quote as urllib_quote + from urllib import urlopen as urllib_urlopen + + # compat types integer_types = (int, long) + string_types = basestring text_type = unicode From 8d054c52ec11e37a4841fbec4357664b4aeb2b44 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Tue, 17 Apr 2018 13:52:58 +0100 Subject: [PATCH 09/14] Add aliases for StringIO.StringIO cStringIO doesn't handle unicode, StringIO does. With py2/py3 compatable code we can easily find ourselves getting passed unicode so we don't alias cStringIO Signed-off-by: Noel Power --- python/samba/compat.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/samba/compat.py b/python/samba/compat.py index d17dc3c2ce9..5662095fed5 100644 --- a/python/samba/compat.py +++ b/python/samba/compat.py @@ -30,6 +30,10 @@ integer_types = int, string_types = str text_type = str + + # alias + import io + StringIO = io.StringIO else: # compat functions from urllib import quote as urllib_quote @@ -39,3 +43,7 @@ integer_types = (int, long) string_types = basestring text_type = unicode + + # alias + import StringIO + StringIO = StringIO.StringIO From 2dd5aedaafd9859cb07679397010b2ce89633308 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Tue, 24 Apr 2018 16:28:41 +0100 Subject: [PATCH 10/14] python/samba: Add binary_type for p2/p3 testing. For helping test for binary types, binary_type evaluates to 'str' in py2, and 'bytes' in py3. Signed-off-by: Noel Power --- python/samba/compat.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/samba/compat.py b/python/samba/compat.py index 5662095fed5..042fc86a440 100644 --- a/python/samba/compat.py +++ b/python/samba/compat.py @@ -30,6 +30,7 @@ integer_types = int, string_types = str text_type = str + binary_type = bytes # alias import io @@ -43,6 +44,7 @@ integer_types = (int, long) string_types = basestring text_type = unicode + binary_type = str # alias import StringIO From dbdd153607c5421f5a2fcadaa43c47b51b748184 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Wed, 25 Apr 2018 18:28:30 +0100 Subject: [PATCH 11/14] python/samba: bulk conversion of caller to dsdb_Dn 2nd param. Convert second param to dsdb_Dn to be unicode so py2 & py3 code will work Signed-off-by: Noel Power --- python/samba/dbchecker.py | 14 +++++++------- python/samba/kcc/kcc_utils.py | 12 ++++++------ python/samba/kcc/ldif_import_export.py | 2 +- source4/torture/drs/python/repl_rodc.py | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/python/samba/dbchecker.py b/python/samba/dbchecker.py index fb968a56481..bdcbc34e011 100644 --- a/python/samba/dbchecker.py +++ b/python/samba/dbchecker.py @@ -286,7 +286,7 @@ def check_deleted_objects_containers(self): listwko = [] proposed_objectguid = None for o in wko: - dsdb_dn = dsdb_Dn(self.samdb, o, dsdb.DSDB_SYNTAX_BINARY_DN) + dsdb_dn = dsdb_Dn(self.samdb, o.decode('utf8'), dsdb.DSDB_SYNTAX_BINARY_DN) if self.is_deleted_objects_dn(dsdb_dn): self.report("wellKnownObjects had duplicate Deleted Objects value %s" % o) # We really want to put this back in the same spot @@ -917,7 +917,7 @@ def find_revealed_link(self, dn, attrname, guid): controls=["show_deleted:0", "extended_dn:0", "reveal_internals:0"]) syntax_oid = self.samdb_schema.get_syntax_oid_from_lDAPDisplayName(attrname) for val in res[0][attrname]: - dsdb_dn = dsdb_Dn(self.samdb, val, syntax_oid) + dsdb_dn = dsdb_Dn(self.samdb, val.decode('utf8'), syntax_oid) guid2 = dsdb_dn.dn.get_extended_component("GUID") if guid == guid2: return dsdb_dn @@ -943,7 +943,7 @@ def check_duplicate_links(self, obj, forward_attr, forward_syntax, forward_linkI self.duplicate_link_cache[duplicate_cache_key] = False for val in obj[forward_attr]: - dsdb_dn = dsdb_Dn(self.samdb, val, forward_syntax) + dsdb_dn = dsdb_Dn(self.samdb, val.decode('utf8'), forward_syntax) # all DNs should have a GUID component guid = dsdb_dn.dn.get_extended_component("GUID") @@ -1178,7 +1178,7 @@ def check_dn(self, obj, attrname, syntax_oid): obj[attrname] = ldb.MessageElement(vals, 0, attrname) for val in obj[attrname]: - dsdb_dn = dsdb_Dn(self.samdb, val, syntax_oid) + dsdb_dn = dsdb_Dn(self.samdb, val.decode('utf8'), syntax_oid) # all DNs should have a GUID component guid = dsdb_dn.dn.get_extended_component("GUID") @@ -1312,7 +1312,7 @@ def check_dn(self, obj, attrname, syntax_oid): match_count = 0 if reverse_link_name in res[0]: for v in res[0][reverse_link_name]: - v_dn = dsdb_Dn(self.samdb, v) + v_dn = dsdb_Dn(self.samdb, v.decode('utf8')) v_guid = v_dn.dn.get_extended_component("GUID") v_blob = v_dn.dn.get_extended_component("RMD_FLAGS") v_rmd_flags = 0 @@ -1329,7 +1329,7 @@ def check_dn(self, obj, attrname, syntax_oid): # Forward binary multi-valued linked attribute forward_count = 0 for w in obj[attrname]: - w_guid = dsdb_Dn(self.samdb, w).dn.get_extended_component("GUID") + w_guid = dsdb_Dn(self.samdb, w.decode('utf8')).dn.get_extended_component("GUID") if w_guid == guid: forward_count += 1 @@ -1337,7 +1337,7 @@ def check_dn(self, obj, attrname, syntax_oid): continue expected_count = 0 for v in obj[attrname]: - v_dn = dsdb_Dn(self.samdb, v) + v_dn = dsdb_Dn(self.samdb, v.decode('utf8')) v_guid = v_dn.dn.get_extended_component("GUID") v_blob = v_dn.dn.get_extended_component("RMD_FLAGS") v_rmd_flags = 0 diff --git a/python/samba/kcc/kcc_utils.py b/python/samba/kcc/kcc_utils.py index 2118570bbfc..23fbd40e904 100644 --- a/python/samba/kcc/kcc_utils.py +++ b/python/samba/kcc/kcc_utils.py @@ -739,7 +739,7 @@ def load_current_replica_table(self, samdb): for value in res[0][k]: # Turn dn into a dsdb_Dn so we can use # its methods to parse a binary DN - dsdn = dsdb_Dn(samdb, value) + dsdn = dsdb_Dn(samdb, value.decode('utf8')) flags = dsdn.get_binary_integer() dnstr = str(dsdn.dn) @@ -986,7 +986,7 @@ def load_connection(self, samdb): "for (%s)" % (self.dnstr)) if "transportType" in msg: - dsdn = dsdb_Dn(samdb, msg["transportType"][0]) + dsdn = dsdb_Dn(samdb, msg["transportType"][0].decode('utf8')) self.load_connection_transport(samdb, str(dsdn.dn)) if "schedule" in msg: @@ -996,7 +996,7 @@ def load_connection(self, samdb): self.whenCreated = ldb.string_to_time(msg["whenCreated"][0]) if "fromServer" in msg: - dsdn = dsdb_Dn(samdb, msg["fromServer"][0]) + dsdn = dsdb_Dn(samdb, msg["fromServer"][0].decode('utf8')) self.from_dnstr = str(dsdn.dn) assert self.from_dnstr is not None @@ -1363,7 +1363,7 @@ def load_partition(self, samdb): continue for value in msg[k]: - dsdn = dsdb_Dn(samdb, value) + dsdn = dsdb_Dn(samdb, value.decode('utf8')) dnstr = str(dsdn.dn) if k == "nCName": @@ -1926,7 +1926,7 @@ def load_transport(self, samdb): if "bridgeheadServerListBL" in msg: for value in msg["bridgeheadServerListBL"]: - dsdn = dsdb_Dn(samdb, value) + dsdn = dsdb_Dn(samdb, value.decode('utf8')) dnstr = str(dsdn.dn) if dnstr not in self.bridgehead_list: self.bridgehead_list.append(dnstr) @@ -2188,7 +2188,7 @@ def load_sitelink(self, samdb): if "siteList" in msg: for value in msg["siteList"]: - dsdn = dsdb_Dn(samdb, value) + dsdn = dsdb_Dn(samdb, value.decode('utf8')) guid = misc.GUID(dsdn.dn.get_extended_component('GUID')) if guid not in self.site_list: self.site_list.append(guid) diff --git a/python/samba/kcc/ldif_import_export.py b/python/samba/kcc/ldif_import_export.py index aab39eeb492..86453f1e5c9 100644 --- a/python/samba/kcc/ldif_import_export.py +++ b/python/samba/kcc/ldif_import_export.py @@ -233,7 +233,7 @@ def samdb_to_ldif_file(samdb, dburl, lp, creds, ldif_file): for value in msg[k]: # Some of these have binary DNs so # use dsdb_Dn to split out relevent parts - dsdn = dsdb_Dn(samdb, value) + dsdn = dsdb_Dn(samdb, value.decode('utf8')) dnstr = str(dsdn.dn) if dnstr not in nclist: nclist.append(dnstr) diff --git a/source4/torture/drs/python/repl_rodc.py b/source4/torture/drs/python/repl_rodc.py index 370673159ec..89c42f0e9d6 100644 --- a/source4/torture/drs/python/repl_rodc.py +++ b/source4/torture/drs/python/repl_rodc.py @@ -623,7 +623,7 @@ def _assert_in_revealed_users(self, user_dn, attrlist): packed_attrs = [] unpacked_attrs = [] for attribute in revealed_users: - dsdb_dn = dsdb_Dn(self.ldb_dc1, attribute) + dsdb_dn = dsdb_Dn(self.ldb_dc1, attribute.decode('utf8')) metadata = ndr_unpack(drsblobs.replPropertyMetaData1, dsdb_dn.get_bytes()) if user_dn in attribute: unpacked_attrs.append(metadata) From 4989e188968c9523cd01de62553fd8bb6eddfd0c Mon Sep 17 00:00:00 2001 From: Noel Power Date: Wed, 25 Apr 2018 20:01:49 +0100 Subject: [PATCH 12/14] python: Bulk conversion callers of ldb.Dn second param Convert second param passed to ldb.Dn to be unicode so py2 & py3 code will work Signed-off-by: Noel Power --- python/samba/dbchecker.py | 10 +++++----- python/samba/descriptor.py | 4 ++-- python/samba/join.py | 4 ++-- python/samba/kcc/__init__.py | 2 +- python/samba/netcmd/fsmo.py | 6 +++--- python/samba/provision/__init__.py | 6 +++--- python/samba/remove_dc.py | 4 ++-- python/samba/tests/kcc/ldif_import_export.py | 4 ++-- source4/dsdb/tests/python/rodc_rwdc.py | 6 +++--- source4/dsdb/tests/python/token_group.py | 4 ++-- source4/torture/drs/python/getnc_exop.py | 4 ++-- source4/torture/drs/python/ridalloc_exop.py | 23 +++++++++++------------ 12 files changed, 38 insertions(+), 39 deletions(-) diff --git a/python/samba/dbchecker.py b/python/samba/dbchecker.py index bdcbc34e011..9d72fc6ca94 100644 --- a/python/samba/dbchecker.py +++ b/python/samba/dbchecker.py @@ -144,11 +144,11 @@ def __init__(self, samdb, samdb_schema=None, verbose=False, fix=False, for nc in self.ncs: try: - dn = self.samdb.get_wellknown_dn(ldb.Dn(self.samdb, nc), + dn = self.samdb.get_wellknown_dn(ldb.Dn(self.samdb, nc.decode('utf8')), dsdb.DS_GUID_DELETED_OBJECTS_CONTAINER) self.deleted_objects_containers.append(dn) except KeyError: - self.ncs_lacking_deleted_containers.append(ldb.Dn(self.samdb, nc)) + self.ncs_lacking_deleted_containers.append(ldb.Dn(self.samdb, nc.decode('utf8'))) domaindns_zone = 'DC=DomainDnsZones,%s' % self.samdb.get_default_basedn() forestdns_zone = 'DC=ForestDnsZones,%s' % self.samdb.get_root_basedn() @@ -178,13 +178,13 @@ def __init__(self, samdb, samdb_schema=None, verbose=False, fix=False, res = self.samdb.search(base=ldb.Dn(self.samdb, self.samdb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - self.server_ref_dn = ldb.Dn(self.samdb, res[0]['serverReference'][0]) + self.server_ref_dn = ldb.Dn(self.samdb, res[0]['serverReference'][0].decode('utf8')) # 3. Get RID Set res = self.samdb.search(base=self.server_ref_dn, scope=ldb.SCOPE_BASE, attrs=['rIDSetReferences']) if "rIDSetReferences" in res[0]: - self.rid_set_dn = ldb.Dn(self.samdb, res[0]['rIDSetReferences'][0]) + self.rid_set_dn = ldb.Dn(self.samdb, res[0]['rIDSetReferences'][0].decode('utf8')) else: self.rid_set_dn = None @@ -2483,7 +2483,7 @@ def check_rootdse(self): error_count += 1 if not self.confirm('Change dsServiceName to GUID form?'): return error_count - res = self.samdb.search(base=ldb.Dn(self.samdb, obj['dsServiceName'][0]), + res = self.samdb.search(base=ldb.Dn(self.samdb, obj['dsServiceName'][0].decode('utf8')), scope=ldb.SCOPE_BASE, attrs=['objectGUID']) guid_str = str(ndr_unpack(misc.GUID, res[0]['objectGUID'][0])) m = ldb.Message() diff --git a/python/samba/descriptor.py b/python/samba/descriptor.py index a9c5e1580f0..1e56e5398f8 100644 --- a/python/samba/descriptor.py +++ b/python/samba/descriptor.py @@ -415,7 +415,7 @@ def get_wellknown_sds(samdb): for nc in current[0]["namingContexts"]: dnsforestdn = ldb.Dn(samdb, "DC=ForestDnsZones,%s" % (str(samdb.get_root_basedn()))) - if ldb.Dn(samdb, nc) == dnsforestdn: + if ldb.Dn(samdb, nc.decode('utf8')) == dnsforestdn: c = (ldb.Dn(samdb, "%s" % str(dnsforestdn)), get_dns_partition_descriptor) subcontainers.append(c) c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsforestdn)), @@ -430,7 +430,7 @@ def get_wellknown_sds(samdb): continue dnsdomaindn = ldb.Dn(samdb, "DC=DomainDnsZones,%s" % (str(samdb.domain_dn()))) - if ldb.Dn(samdb, nc) == dnsdomaindn: + if ldb.Dn(samdb, nc.decode('utf8')) == dnsdomaindn: c = (ldb.Dn(samdb, "%s" % str(dnsdomaindn)), get_dns_partition_descriptor) subcontainers.append(c) c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsdomaindn)), diff --git a/python/samba/join.py b/python/samba/join.py index b5cab7e7cb3..b35eb78e1c6 100644 --- a/python/samba/join.py +++ b/python/samba/join.py @@ -388,7 +388,7 @@ def get_naming_master(ctx): if not 'fSMORoleOwner' in res[0]: raise DCJoinException("Can't find naming master on partition DN %s in %s" % (ctx.partition_dn, ctx.samdb.url)) try: - master_guid = str(misc.GUID(ldb.Dn(ctx.samdb, res[0]['fSMORoleOwner'][0]).get_extended_component('GUID'))) + master_guid = str(misc.GUID(ldb.Dn(ctx.samdb, res[0]['fSMORoleOwner'][0].decode('utf8')).get_extended_component('GUID'))) except KeyError: raise DCJoinException("Can't find GUID in naming master on partition DN %s" % res[0]['fSMORoleOwner'][0]) @@ -880,7 +880,7 @@ def join_provision_own_domain(ctx): raise DCJoinException("Can't find naming context on partition DN %s in %s" % (ctx.partition_dn, ctx.samdb.url)) try: - ctx.names.domainguid = str(misc.GUID(ldb.Dn(ctx.samdb, res[0]['ncName'][0]).get_extended_component('GUID'))) + ctx.names.domainguid = str(misc.GUID(ldb.Dn(ctx.samdb, res[0]['ncName'][0].decode('utf8')).get_extended_component('GUID'))) except KeyError: raise DCJoinException("Can't find GUID in naming master on partition DN %s" % res[0]['ncName'][0]) diff --git a/python/samba/kcc/__init__.py b/python/samba/kcc/__init__.py index 1a962bb03c1..be1c4d82968 100644 --- a/python/samba/kcc/__init__.py +++ b/python/samba/kcc/__init__.py @@ -274,7 +274,7 @@ def load_my_dsa(self): scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(self.samdb, - service_name_res[0]["dsServiceName"][0]) + service_name_res[0]["dsServiceName"][0].decode('utf8')) res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) diff --git a/python/samba/netcmd/fsmo.py b/python/samba/netcmd/fsmo.py index 41e45592b46..91de5dad563 100644 --- a/python/samba/netcmd/fsmo.py +++ b/python/samba/netcmd/fsmo.py @@ -47,7 +47,7 @@ def get_fsmo_roleowner(samdb, roledn, role): raise if 'fSMORoleOwner' in res[0]: - master_owner = (ldb.Dn(samdb, res[0]["fSMORoleOwner"][0])) + master_owner = (ldb.Dn(samdb, res[0]["fSMORoleOwner"][0].decode('utf8'))) else: master_owner = None @@ -72,9 +72,9 @@ def transfer_dns_role(outf, sambaopts, credopts, role, samdb): if 'fSMORoleOwner' in res[0]: try: master_guid = str(misc.GUID(ldb.Dn(samdb, - res[0]['fSMORoleOwner'][0]) + res[0]['fSMORoleOwner'][0].decode('utf8')) .get_extended_component('GUID'))) - master_owner = str(ldb.Dn(samdb, res[0]['fSMORoleOwner'][0])) + master_owner = str(ldb.Dn(samdb, res[0]['fSMORoleOwner'][0].decode('utf8'))) except LdbError as e3: (num, msg) = e3.args raise CommandError("No GUID found in naming master DN %s : %s \n" % diff --git a/python/samba/provision/__init__.py b/python/samba/provision/__init__.py index 1709e75184e..43ccb0f8766 100644 --- a/python/samba/provision/__init__.py +++ b/python/samba/provision/__init__.py @@ -220,10 +220,10 @@ def find_provision_key_parameters(samdb, secretsdb, idmapdb, paths, smbconf, names.configdn = current[0]["configurationNamingContext"][0] names.schemadn = current[0]["schemaNamingContext"][0] if not (ldb.Dn(samdb, basedn) == (ldb.Dn(samdb, - current[0]["defaultNamingContext"][0]))): + current[0]["defaultNamingContext"][0].decode('utf8')))): raise ProvisioningError(("basedn in %s (%s) and from %s (%s)" "is not the same ..." % (paths.samdb, - str(current[0]["defaultNamingContext"][0]), + str(current[0]["defaultNamingContext"][0].decode('utf8')), paths.smbconf, basedn))) names.domaindn=current[0]["defaultNamingContext"][0] @@ -1920,7 +1920,7 @@ def provision_fill(samdb, secrets_ldb, logger, names, paths, msg = ldb.Message(ldb.Dn(samdb, samdb.searchone("distinguishedName", expression="samAccountName=%s$" % names.netbiosname, - scope=ldb.SCOPE_SUBTREE))) + scope=ldb.SCOPE_SUBTREE).decode('utf8'))) msg["msDS-SupportedEncryptionTypes"] = ldb.MessageElement( elements=kerberos_enctypes, flags=ldb.FLAG_MOD_REPLACE, name="msDS-SupportedEncryptionTypes") diff --git a/python/samba/remove_dc.py b/python/samba/remove_dc.py index 6b86a554efa..b9726f5b84f 100644 --- a/python/samba/remove_dc.py +++ b/python/samba/remove_dc.py @@ -230,7 +230,7 @@ def offline_remove_server(samdb, logger, dc_name = str(msgs[0]["cn"][0]) try: - computer_dn = ldb.Dn(samdb, msgs[0]["serverReference"][0]) + computer_dn = ldb.Dn(samdb, msgs[0]["serverReference"][0].decode('utf8')) except KeyError: computer_dn = None @@ -295,7 +295,7 @@ def offline_remove_ntds_dc(samdb, res = samdb.search("", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) assert len(res) == 1 - my_serviceName = ldb.Dn(samdb, res[0]["dsServiceName"][0]) + my_serviceName = ldb.Dn(samdb, res[0]["dsServiceName"][0].decode('utf8')) server_dn = ntds_dn.parent() if my_serviceName == ntds_dn: diff --git a/python/samba/tests/kcc/ldif_import_export.py b/python/samba/tests/kcc/ldif_import_export.py index 6fb3ecd0c37..5ea748ff3f0 100644 --- a/python/samba/tests/kcc/ldif_import_export.py +++ b/python/samba/tests/kcc/ldif_import_export.py @@ -104,7 +104,7 @@ def test_ldif_to_samdb(self): scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(samdb, - service_name_res[0]["dsServiceName"][0]) + service_name_res[0]["dsServiceName"][0].decode('utf8')) self.assertEqual(dn, ldb.Dn(samdb, "CN=NTDS Settings," + dsa)) self.remove_files(dburl) @@ -128,7 +128,7 @@ def test_ldif_to_samdb_forced_local_dsa(self): scope=ldb.SCOPE_BASE, attrs=["dsServiceName"]) dn = ldb.Dn(samdb, - service_name_res[0]["dsServiceName"][0]) + service_name_res[0]["dsServiceName"][0].decode('utf8')) self.assertEqual(dn, ldb.Dn(samdb, "CN=NTDS Settings," + dsa)) self.remove_files(dburl) diff --git a/source4/dsdb/tests/python/rodc_rwdc.py b/source4/dsdb/tests/python/rodc_rwdc.py index 1e84369f34f..2f4400e3e16 100644 --- a/source4/dsdb/tests/python/rodc_rwdc.py +++ b/source4/dsdb/tests/python/rodc_rwdc.py @@ -270,7 +270,7 @@ def test_login_lockout_krb5(self): scope=ldb.SCOPE_BASE, attrs=['msDS-RevealOnDemandGroup']) - group = res[0]['msDS-RevealOnDemandGroup'][0] + group = res[0]['msDS-RevealOnDemandGroup'][0].decode('utf8') m = ldb.Message() m.dn = ldb.Dn(self.rwdc_db, group) @@ -314,7 +314,7 @@ def test_login_lockout_ntlm(self): scope=ldb.SCOPE_BASE, attrs=['msDS-RevealOnDemandGroup']) - group = res[0]['msDS-RevealOnDemandGroup'][0] + group = res[0]['msDS-RevealOnDemandGroup'][0].decode('utf8') m = ldb.Message() m.dn = ldb.Dn(self.rwdc_db, group) @@ -1121,7 +1121,7 @@ def _test_ldap_change_password_reveal_on_demand(self, errno=None): scope=ldb.SCOPE_BASE, attrs=['msDS-RevealOnDemandGroup']) - group = res[0]['msDS-RevealOnDemandGroup'][0] + group = res[0]['msDS-RevealOnDemandGroup'][0].decode('utf8') user_dn, username, password = self._new_user() creds1 = make_creds(username, password) diff --git a/source4/dsdb/tests/python/token_group.py b/source4/dsdb/tests/python/token_group.py index 8c3441e8af9..ccc58b62fbb 100755 --- a/source4/dsdb/tests/python/token_group.py +++ b/source4/dsdb/tests/python/token_group.py @@ -418,7 +418,7 @@ def test_tokenGroups_manual(self): if "memberOf" in obj: for dn in obj["memberOf"]: first = obj.dn.get_casefold() - second = ldb.Dn(self.admin_ldb, dn).get_casefold() + second = ldb.Dn(self.admin_ldb, dn.decode('utf8')).get_casefold() aSet.add((first, second)) aSetR.add((second, first)) vSet.add(first) @@ -476,7 +476,7 @@ def filtered_closure(self, wSet, filter_grouptype): if "memberOf" in obj: for dn in obj["memberOf"]: first = obj.dn.get_casefold() - second = ldb.Dn(self.admin_ldb, dn).get_casefold() + second = ldb.Dn(self.admin_ldb, dn.decode('utf8')).get_casefold() aSet.add((first, second)) aSetR.add((second, first)) vSet.add(first) diff --git a/source4/torture/drs/python/getnc_exop.py b/source4/torture/drs/python/getnc_exop.py index d92a535932b..e29e346b3eb 100644 --- a/source4/torture/drs/python/getnc_exop.py +++ b/source4/torture/drs/python/getnc_exop.py @@ -110,11 +110,11 @@ def _determine_fSMORoleOwner(self, fsmo_obj_dn): "server_dn": self.ldb_dc2.get_serverName()} msgs = self.ldb_dc1.search(scope=ldb.SCOPE_BASE, base=fsmo_info_1["server_dn"], attrs=["serverReference"]) - fsmo_info_1["server_acct_dn"] = ldb.Dn(self.ldb_dc1, msgs[0]["serverReference"][0]) + fsmo_info_1["server_acct_dn"] = ldb.Dn(self.ldb_dc1, msgs[0]["serverReference"][0].decode('utf8')) fsmo_info_1["rid_set_dn"] = ldb.Dn(self.ldb_dc1, "CN=RID Set") + fsmo_info_1["server_acct_dn"] msgs = self.ldb_dc2.search(scope=ldb.SCOPE_BASE, base=fsmo_info_2["server_dn"], attrs=["serverReference"]) - fsmo_info_2["server_acct_dn"] = ldb.Dn(self.ldb_dc2, msgs[0]["serverReference"][0]) + fsmo_info_2["server_acct_dn"] = ldb.Dn(self.ldb_dc2, msgs[0]["serverReference"][0].decode('utf8')) fsmo_info_2["rid_set_dn"] = ldb.Dn(self.ldb_dc2, "CN=RID Set") + fsmo_info_2["server_acct_dn"] # determine the owner dc diff --git a/source4/torture/drs/python/ridalloc_exop.py b/source4/torture/drs/python/ridalloc_exop.py index 7604e1e2cab..f4c404111ce 100644 --- a/source4/torture/drs/python/ridalloc_exop.py +++ b/source4/torture/drs/python/ridalloc_exop.py @@ -73,11 +73,11 @@ def _determine_fSMORoleOwner(self, fsmo_obj_dn): "server_dn": self.ldb_dc2.get_serverName()} msgs = self.ldb_dc1.search(scope=ldb.SCOPE_BASE, base=fsmo_info_1["server_dn"], attrs=["serverReference"]) - fsmo_info_1["server_acct_dn"] = ldb.Dn(self.ldb_dc1, msgs[0]["serverReference"][0]) + fsmo_info_1["server_acct_dn"] = ldb.Dn(self.ldb_dc1, msgs[0]["serverReference"][0].decode('utf8')) fsmo_info_1["rid_set_dn"] = ldb.Dn(self.ldb_dc1, "CN=RID Set") + fsmo_info_1["server_acct_dn"] msgs = self.ldb_dc2.search(scope=ldb.SCOPE_BASE, base=fsmo_info_2["server_dn"], attrs=["serverReference"]) - fsmo_info_2["server_acct_dn"] = ldb.Dn(self.ldb_dc2, msgs[0]["serverReference"][0]) + fsmo_info_2["server_acct_dn"] = ldb.Dn(self.ldb_dc2, msgs[0]["serverReference"][0].decode('utf8')) fsmo_info_2["rid_set_dn"] = ldb.Dn(self.ldb_dc2, "CN=RID Set") + fsmo_info_2["server_acct_dn"] # determine the owner dc @@ -282,7 +282,7 @@ def test_offline_samba_tool_seized_ridalloc(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # Assert that no RID Set has been set res = new_ldb.search(base=server_ref_dn, @@ -356,7 +356,7 @@ def test_offline_manual_seized_ridalloc_with_dbcheck(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # Assert that no RID Set has been set res = new_ldb.search(base=server_ref_dn, @@ -407,7 +407,7 @@ def test_offline_manual_seized_ridalloc_add_user(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # Assert that no RID Set has been set res = new_ldb.search(base=server_ref_dn, @@ -457,7 +457,7 @@ def test_offline_manual_seized_ridalloc_add_user_as_admin(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # Assert that no RID Set has been set res = new_ldb.search(base=server_ref_dn, @@ -500,7 +500,7 @@ def test_join_time_ridalloc(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # 3. Assert we get the RID Set res = new_ldb.search(base=server_ref_dn, @@ -532,14 +532,14 @@ def test_rid_set_dbcheck(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # 3. Assert we get the RID Set res = new_ldb.search(base=server_ref_dn, scope=ldb.SCOPE_BASE, attrs=['rIDSetReferences']) self.assertTrue("rIDSetReferences" in res[0]) - rid_set_dn = ldb.Dn(new_ldb, res[0]["rIDSetReferences"][0]) + rid_set_dn = ldb.Dn(new_ldb, res[0]["rIDSetReferences"][0].decode('utf8')) # 4. Add a new user (triggers RID set work) new_ldb.newuser("ridalloctestuser", "P@ssword!") @@ -610,15 +610,14 @@ def test_rid_set_dbcheck_after_seize(self): res = new_ldb.search(base=ldb.Dn(new_ldb, new_ldb.get_serverName()), scope=ldb.SCOPE_BASE, attrs=["serverReference"]) # 2. Get server reference - server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0]) + server_ref_dn = ldb.Dn(new_ldb, res[0]['serverReference'][0].decode('utf8')) # 3. Assert we get the RID Set res = new_ldb.search(base=server_ref_dn, scope=ldb.SCOPE_BASE, attrs=['rIDSetReferences']) self.assertTrue("rIDSetReferences" in res[0]) - rid_set_dn = ldb.Dn(new_ldb, res[0]["rIDSetReferences"][0]) - + rid_set_dn = ldb.Dn(new_ldb, res[0]["rIDSetReferences"][0].decode('utf8')) # 4. Seize the RID Manager role (result, out, err) = self.runsubcmd("fsmo", "seize", "--role", "rid", "-H", ldb_url, "-s", smbconf, "--force") self.assertCmdSuccess(result, out, err) From e351470d31a2163bcd18f0d3a59c2082174f6c62 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 26 Apr 2018 10:38:57 +0100 Subject: [PATCH 13/14] python/samba: port some isinstance str checks (to cater for unicode) Signed-off-by: Noel Power --- python/samba/__init__.py | 5 +++-- python/samba/ms_schema.py | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/python/samba/__init__.py b/python/samba/__init__.py index f62f5e39202..20c7db2bc6c 100644 --- a/python/samba/__init__.py +++ b/python/samba/__init__.py @@ -29,6 +29,7 @@ import samba.param from samba import _glue from samba._ldb import Ldb as _Ldb +from samba.compat import string_types def source_tree_topdir(): @@ -249,8 +250,8 @@ def substitute_var(text, values): """ for (name, value) in values.items(): - assert isinstance(name, str), "%r is not a string" % name - assert isinstance(value, str), "Value %r for %s is not a string" % (value, name) + assert isinstance(name, string_types), "%r is not a string" % name + assert isinstance(value, string_types), "Value %r for %s is not a string" % (value, name) text = text.replace("${%s}" % name, value) return text diff --git a/python/samba/ms_schema.py b/python/samba/ms_schema.py index 889b7f5ef22..de6e4b28cdc 100644 --- a/python/samba/ms_schema.py +++ b/python/samba/ms_schema.py @@ -21,6 +21,7 @@ import re import base64 import uuid +from samba.compat import string_types bitFields = {} @@ -172,7 +173,7 @@ def fix_dn(dn): def __convert_bitfield(key, value): """Evaluate the OR expression in 'value'""" - assert(isinstance(value, str)) + assert(isinstance(value, string_types)) value = value.replace("\n ", "") value = value.replace(" ", "") @@ -194,7 +195,7 @@ def __write_ldif_one(entry): out = [] for l in entry: - if isinstance(l[1], str): + if isinstance(l[1], string_types): vl = [l[1]] else: vl = l[1] @@ -247,7 +248,7 @@ def __transform_entry(entry, objectClass): l[1] = oMObjectClassBER[l[1].strip()] l[2] = True - if isinstance(l[1], str): + if isinstance(l[1], string_types): l[1] = fix_dn(l[1]) if key == 'dn': From 8f15aeb9c98c4e5871a699fdd4dc7aa710dda996 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 27 Apr 2018 08:15:28 +0100 Subject: [PATCH 14/14] python/samba: Py2/Py3 compat change '/' to '//' to ensure int result Signed-off-by: Noel Power --- python/samba/graph.py | 5 +++-- python/samba/netcmd/domain.py | 3 ++- python/samba/upgradehelpers.py | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/python/samba/graph.py b/python/samba/graph.py index 6cdd2ef77ed..7dfc19015e7 100644 --- a/python/samba/graph.py +++ b/python/samba/graph.py @@ -19,6 +19,7 @@ # along with this program. If not, see . from __future__ import print_function +from __future__ import division from samba import colour import sys @@ -39,8 +40,8 @@ def reformat_graph_label(s): if '-' in p[2:20]: q, p = p.split('-', 1) else: - n = len(p) / 12 - b = len(p) / n + n = len(p) // 12 + b = len(p) // n q, p = p[:b], p[b:] pieces.append(q + '-') if p: diff --git a/python/samba/netcmd/domain.py b/python/samba/netcmd/domain.py index d2dd06a3d48..1e242dea62e 100644 --- a/python/samba/netcmd/domain.py +++ b/python/samba/netcmd/domain.py @@ -23,6 +23,7 @@ # from __future__ import print_function +from __future__ import division import samba.getopt as options import ldb import string @@ -2331,7 +2332,7 @@ def get_password(name): # 512 bytes and a 2 bytes confounder is required. # def random_trust_secret(length): - pw = samba.generate_random_machine_password(length/2, length/2) + pw = samba.generate_random_machine_password(length//2, length//2) return string_to_byte_array(pw.encode('utf-16-le')) if local_trust_info.trust_direction & lsa.LSA_TRUST_DIRECTION_INBOUND: diff --git a/python/samba/upgradehelpers.py b/python/samba/upgradehelpers.py index e219602e0c7..d4f69445234 100644 --- a/python/samba/upgradehelpers.py +++ b/python/samba/upgradehelpers.py @@ -20,6 +20,7 @@ # along with this program. If not, see . from __future__ import print_function +from __future__ import division """Helpers used for upgrading between different database formats.""" import os @@ -725,7 +726,7 @@ def findprovisionrange(samdb, basedn): for o in obj.array: # like a timestamp but with the resolution of 1 minute - minutestamp =_glue.nttime2unix(o.originating_change_time)/60 + minutestamp =_glue.nttime2unix(o.originating_change_time) // 60 hash_ts = hash_id.get(str(o.originating_invocation_id)) if hash_ts is None: