From: Lumir Balhar Date: Wed, 15 Feb 2017 08:19:33 +0000 (+0100) Subject: python: samba.dcerpc: Port RPC related stuff to Python 3 X-Git-Tag: tdb-1.3.13~567 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3b1c0ba98a8497172d2124e8555e1c504781b8f9;p=thirdparty%2Fsamba.git python: samba.dcerpc: Port RPC related stuff to Python 3 Port RPC related stuff like samba.dcerpc.misc and samba.dcerpc Python modules and pyrpc_util to Python 3 compatible form. Signed-off-by: Lumir Balhar Reviewed-by: Andrew Bartlett Reviewed-by: Douglas Bagnall Pair-programmed-by: Andrew Bartlett --- diff --git a/source4/librpc/ndr/py_misc.c b/source4/librpc/ndr/py_misc.c index d8edff899ac..6316b9b6704 100644 --- a/source4/librpc/ndr/py_misc.c +++ b/source4/librpc/ndr/py_misc.c @@ -18,8 +18,34 @@ along with this program. If not, see . */ #include +#include "python/py3compat.h" #include "librpc/gen_ndr/misc.h" +#if PY_MAJOR_VERSION >= 3 +static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op) +{ + int ret; + struct GUID *self = pytalloc_get_ptr(py_self), *other; + other = pytalloc_get_ptr(py_other); + if (other == NULL) { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } + + ret = GUID_compare(self, other); + + switch (op) { + case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE; + case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE; + case Py_LT: if (ret < 0) Py_RETURN_TRUE; else Py_RETURN_FALSE; + case Py_GT: if (ret > 0) Py_RETURN_TRUE; else Py_RETURN_FALSE; + case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE; + case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE; + } + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; +} +#else static int py_GUID_cmp(PyObject *py_self, PyObject *py_other) { int ret; @@ -37,12 +63,13 @@ static int py_GUID_cmp(PyObject *py_self, PyObject *py_other) return 0; } } +#endif static PyObject *py_GUID_str(PyObject *py_self) { struct GUID *self = pytalloc_get_ptr(py_self); char *str = GUID_string(NULL, self); - PyObject *ret = PyString_FromString(str); + PyObject *ret = PyStr_FromString(str); talloc_free(str); return ret; } @@ -51,7 +78,7 @@ static PyObject *py_GUID_repr(PyObject *py_self) { struct GUID *self = pytalloc_get_ptr(py_self); char *str = GUID_string(NULL, self); - PyObject *ret = PyString_FromFormat("GUID('%s')", str); + PyObject *ret = PyStr_FromFormat("GUID('%s')", str); talloc_free(str); return ret; } @@ -68,13 +95,14 @@ static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs) if (str != NULL) { DATA_BLOB guid_val; + Py_ssize_t _size; - if (!PyString_Check(str)) { + if (!PyStr_Check(str)) { PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()"); return -1; } - guid_val.data = (uint8_t *)PyString_AsString(str); - guid_val.length = PyString_Size(str); + guid_val.data = (uint8_t *)PyStr_AsUTF8AndSize(str, &_size); + guid_val.length = _size; status = GUID_from_data_blob(&guid_val, guid); if (!NT_STATUS_IS_OK(status)) { PyErr_SetNTSTATUS(status); @@ -90,7 +118,11 @@ static void py_GUID_patch(PyTypeObject *type) type->tp_init = py_GUID_init; type->tp_str = py_GUID_str; type->tp_repr = py_GUID_repr; +#if PY_MAJOR_VERSION >= 3 + type->tp_richcompare = py_GUID_richcmp; +#else type->tp_compare = py_GUID_cmp; +#endif } #define PY_GUID_PATCH py_GUID_patch @@ -120,7 +152,7 @@ static PyObject *py_policy_handle_repr(PyObject *py_self) { struct policy_handle *self = pytalloc_get_ptr(py_self); char *uuid_str = GUID_string(NULL, &self->uuid); - PyObject *ret = PyString_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str); + PyObject *ret = PyStr_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str); talloc_free(uuid_str); return ret; } @@ -129,7 +161,7 @@ static PyObject *py_policy_handle_str(PyObject *py_self) { struct policy_handle *self = pytalloc_get_ptr(py_self); char *uuid_str = GUID_string(NULL, &self->uuid); - PyObject *ret = PyString_FromFormat("%d, %s", self->handle_type, uuid_str); + PyObject *ret = PyStr_FromFormat("%d, %s", self->handle_type, uuid_str); talloc_free(uuid_str); return ret; } diff --git a/source4/librpc/rpc/dcerpc.py b/source4/librpc/rpc/dcerpc.py index 830720cde00..64dd6e3a433 100644 --- a/source4/librpc/rpc/dcerpc.py +++ b/source4/librpc/rpc/dcerpc.py @@ -15,4 +15,4 @@ # along with this program. If not, see . # -from base import * +from samba.dcerpc.base import * diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index 834000c6574..d4f13ba89f6 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -18,6 +18,7 @@ */ #include +#include "python/py3compat.h" #include "includes.h" #include #include "librpc/rpc/pyrpc.h" @@ -37,7 +38,7 @@ static PyTypeObject *ndr_syntax_id_Type; static bool PyString_AsGUID(PyObject *object, struct GUID *uuid) { NTSTATUS status; - status = GUID_from_string(PyString_AsString(object), uuid); + status = GUID_from_string(PyStr_AsString(object), uuid); if (NT_STATUS_IS_ERR(status)) { PyErr_SetNTSTATUS(status); return false; @@ -49,7 +50,7 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy { ZERO_STRUCTP(syntax_id); - if (PyString_Check(object)) { + if (PyStr_Check(object)) { return PyString_AsGUID(object, &syntax_id->uuid); } else if (PyTuple_Check(object)) { if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) { @@ -57,7 +58,7 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy return false; } - if (!PyString_Check(PyTuple_GetItem(object, 0))) { + if (!PyStr_Check(PyTuple_GetItem(object, 0))) { PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple"); return false; } @@ -87,7 +88,7 @@ static PyObject *py_iface_server_name(PyObject *obj, void *closure) if (server_name == NULL) Py_RETURN_NONE; - return PyString_FromString(server_name); + return PyStr_FromString(server_name); } static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id) @@ -128,7 +129,7 @@ static PyObject *py_iface_session_key(PyObject *obj, void *closure) NTSTATUS status = dcerpc_fetch_session_key(iface->pipe, &session_key); PyErr_NTSTATUS_IS_ERR_RAISE(status); - return PyString_FromStringAndSize((const char *)session_key.data, session_key.length); + return PyBytes_FromStringAndSize((const char *)session_key.data, session_key.length); } static PyObject *py_iface_user_session_key(PyObject *obj, void *closure) @@ -166,7 +167,7 @@ static PyObject *py_iface_user_session_key(PyObject *obj, void *closure) return NULL; } - session_key_obj = PyString_FromStringAndSize((const char *)session_key.data, + session_key_obj = PyBytes_FromStringAndSize((const char *)session_key.data, session_key.length); talloc_free(mem_ctx); return session_key_obj; @@ -260,7 +261,7 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar return NULL; } - ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length); + ret = PyBytes_FromStringAndSize((char *)data_out.data, data_out.length); talloc_free(mem_ctx); return ret; @@ -337,7 +338,7 @@ static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObje } static PyTypeObject dcerpc_InterfaceType = { - PyObject_HEAD_INIT(NULL) 0, + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "dcerpc.ClientConnection", .tp_basicsize = sizeof(dcerpc_InterfaceObject), .tp_dealloc = dcerpc_interface_dealloc, @@ -359,7 +360,7 @@ static PyObject *py_transfer_syntax_ndr_new(PyTypeObject *type, PyObject *args, } static PyTypeObject py_transfer_syntax_ndr_SyntaxType = { - PyObject_HEAD_INIT(NULL) 0, + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "base.transfer_syntax_ndr", .tp_doc = "transfer_syntax_ndr()\n", .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, @@ -372,7 +373,7 @@ static PyObject *py_transfer_syntax_ndr64_new(PyTypeObject *type, PyObject *args } static PyTypeObject py_transfer_syntax_ndr64_SyntaxType = { - PyObject_HEAD_INIT(NULL) 0, + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "base.transfer_syntax_ndr64", .tp_doc = "transfer_syntax_ndr64()\n", .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, @@ -410,25 +411,32 @@ static PyObject *py_bind_time_features_syntax_new(PyTypeObject *type, PyObject * } static PyTypeObject py_bind_time_features_syntax_SyntaxType = { - PyObject_HEAD_INIT(NULL) 0, + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "base.bind_time_features_syntax", .tp_doc = "bind_time_features_syntax(features)\n", .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_new = py_bind_time_features_syntax_new, }; -void initbase(void) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + .m_name = "base", + .m_doc = "DCE/RPC protocol implementation", + .m_size = -1, +}; + +MODULE_INIT_FUNC(base) { PyObject *m; PyObject *dep_samba_dcerpc_misc; dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc"); if (dep_samba_dcerpc_misc == NULL) - return; + return NULL; ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id"); if (ndr_syntax_id_Type == NULL) - return; + return NULL; py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type; py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size(); @@ -438,18 +446,18 @@ void initbase(void) py_bind_time_features_syntax_SyntaxType.tp_basicsize = pytalloc_BaseObject_size(); if (PyType_Ready(&dcerpc_InterfaceType) < 0) - return; + return NULL; if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0) - return; + return NULL; if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0) - return; + return NULL; if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0) - return; + return NULL; - m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation"); + m = PyModule_Create(&moduledef); if (m == NULL) - return; + return NULL; Py_INCREF((PyObject *)&dcerpc_InterfaceType); PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType); @@ -460,4 +468,5 @@ void initbase(void) PyModule_AddObject(m, "transfer_syntax_ndr64", (PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType); Py_INCREF((PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType); PyModule_AddObject(m, "bind_time_features_syntax", (PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType); + return m; } diff --git a/source4/librpc/rpc/pyrpc_util.c b/source4/librpc/rpc/pyrpc_util.c index fe0e2de8355..21d94308cb9 100644 --- a/source4/librpc/rpc/pyrpc_util.c +++ b/source4/librpc/rpc/pyrpc_util.c @@ -21,6 +21,7 @@ */ #include +#include "python/py3compat.h" #include "includes.h" #include "librpc/rpc/pyrpc_util.h" #include "librpc/rpc/dcerpc.h" @@ -388,7 +389,7 @@ PyObject *PyString_FromStringOrNULL(const char *str) if (str == NULL) { Py_RETURN_NONE; } - return PyString_FromString(str); + return PyStr_FromString(str); } PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level, diff --git a/source4/librpc/wscript_build b/source4/librpc/wscript_build index 60404f8e08d..cc03e10fa16 100644 --- a/source4/librpc/wscript_build +++ b/source4/librpc/wscript_build @@ -174,18 +174,23 @@ bld.SAMBA_LIBRARY('dcerpc', vnum='0.0.1' ) -bld.SAMBA_SUBSYSTEM('pyrpc_util', - source='rpc/pyrpc_util.c', - public_deps='pytalloc-util pyparam_util dcerpc MESSAGING', - pyext=True, - ) +for env in bld.gen_python_environments(): + pyrpc_util = bld.pyembed_libname('pyrpc_util') + pytalloc_util = bld.pyembed_libname('pytalloc-util') + pyparam_util = bld.pyembed_libname('pyparam_util') + + bld.SAMBA_SUBSYSTEM(pyrpc_util, + source='rpc/pyrpc_util.c', + public_deps='%s %s dcerpc MESSAGING' % (pytalloc_util, pyparam_util), + pyext=True, + ) + bld.SAMBA_PYTHON('python_dcerpc', + source='rpc/pyrpc.c', + public_deps='LIBCLI_SMB samba-util samba-hostconfig dcerpc-samr RPC_NDR_LSA DYNCONFIG %s gensec' % (pyrpc_util), + realname='samba/dcerpc/base.so' + ) -bld.SAMBA_PYTHON('python_dcerpc', - source='rpc/pyrpc.c', - public_deps='LIBCLI_SMB samba-util samba-hostconfig dcerpc-samr RPC_NDR_LSA DYNCONFIG pyrpc_util gensec', - realname='samba/dcerpc/base.so' - ) bld.SAMBA_PYTHON('python_srvsvc', source='../../librpc/gen_ndr/py_srvsvc.c', @@ -224,11 +229,14 @@ bld.SAMBA_PYTHON('python_winreg', ) -bld.SAMBA_PYTHON('python_dcerpc_misc', - source='../../librpc/gen_ndr/py_misc.c', - deps='pytalloc-util pyrpc_util ndr-krb5pac', - realname='samba/dcerpc/misc.so' - ) +for env in bld.gen_python_environments(): + pytalloc_util = bld.pyembed_libname('pytalloc-util') + pyrpc_util = bld.pyembed_libname('pyrpc_util') + bld.SAMBA_PYTHON('python_dcerpc_misc', + source='../../librpc/gen_ndr/py_misc.c', + deps='%s %s ndr-krb5pac' % (pytalloc_util, pyrpc_util), + realname='samba/dcerpc/misc.so' + ) bld.SAMBA_PYTHON('python_initshutdown',